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 /* LCOV_EXCL_START */
224 ENTER_CRITICAL_SECTION(_db_handle_lock);
225 for (i = 0; i < MAX_DB_CLIENT; i++) {
226 if (EM_SAFE_STRCASECMP(_db_handle_list[i].user_name, multi_user_name) != 0)
229 if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id)) {
230 EM_DEBUG_LOG_DEV("found db handle at [%d]", i);
231 result_db_handle = _db_handle_list[i].db_handle;
235 LEAVE_CRITICAL_SECTION(_db_handle_lock);
238 ENTER_CRITICAL_SECTION(_db_handle_lock);
239 for (i = 0; i < MAX_DB_CLIENT; i++) {
240 if (EM_SAFE_STRLEN(_db_handle_list[i].user_name) > 0)
241 /* LCOV_EXCL_START */
245 if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id)) {
246 EM_DEBUG_LOG_DEV("found db handle at [%d]", i);
247 result_db_handle = _db_handle_list[i].db_handle;
251 LEAVE_CRITICAL_SECTION(_db_handle_lock);
254 if (!result_db_handle)
255 EM_DEBUG_LOG("no db_handle for [%lu] found", current_thread_id);
258 return result_db_handle;
261 int emstorage_set_db_handle(char *multi_user_name, sqlite3 *db_handle)
263 EM_DEBUG_FUNC_BEGIN();
264 int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
265 pthread_t current_thread_id = THREAD_SELF();
267 ENTER_CRITICAL_SECTION(_db_handle_lock);
268 for (i = 0; i < MAX_DB_CLIENT; i++) {
269 if (_db_handle_list[i].thread_id == 0) {
270 _db_handle_list[i].thread_id = current_thread_id;
271 _db_handle_list[i].db_handle = db_handle;
272 /* Only distinguished container and host */
273 _db_handle_list[i].user_name = EM_SAFE_STRDUP(multi_user_name);
274 EM_DEBUG_LOG("current_thread_id [%lu], index [%d]", current_thread_id, i);
275 error_code = EMAIL_ERROR_NONE;
279 LEAVE_CRITICAL_SECTION(_db_handle_lock);
281 if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
282 /* LCOV_EXCL_START */
283 EM_DEBUG_EXCEPTION("Exceeded the limitation of db client. Can't find empty slot in _db_handle_list.");
286 EM_DEBUG_FUNC_END("error_code [%d]", error_code);
289 /* LCOV_EXCL_START */
290 int emstorage_remove_db_handle()
292 EM_DEBUG_FUNC_BEGIN();
293 int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
294 ENTER_CRITICAL_SECTION(_db_handle_lock);
295 for (i = 0; i < MAX_DB_CLIENT; i++) {
296 if (_db_handle_list[i].thread_id == THREAD_SELF()) {
297 _db_handle_list[i].thread_id = 0;
298 _db_handle_list[i].db_handle = NULL;
299 EM_SAFE_FREE(_db_handle_list[i].user_name);
301 EM_DEBUG_LOG("index [%d]", i);
302 error_code = EMAIL_ERROR_NONE;
306 LEAVE_CRITICAL_SECTION(_db_handle_lock);
308 if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
309 EM_DEBUG_EXCEPTION("Can't find proper thread_id");
311 EM_DEBUG_FUNC_END("error_code [%d]", error_code);
315 int emstorage_reset_db_handle_list()
317 EM_DEBUG_FUNC_BEGIN();
320 ENTER_CRITICAL_SECTION(_db_handle_lock);
321 for (i = 0; i < MAX_DB_CLIENT; i++) {
322 _db_handle_list[i].thread_id = 0;
323 _db_handle_list[i].db_handle = NULL;
324 EM_SAFE_FREE(_db_handle_list[i].user_name);
326 LEAVE_CRITICAL_SECTION(_db_handle_lock)
329 return EMAIL_ERROR_NONE;
333 sqlite3 *emstorage_get_db_connection(char *multi_user_name)
335 /* LCOV_EXCL_START */
336 return emstorage_db_open(multi_user_name, NULL);
340 #else /* _MULTIPLE_DB_HANDLE */
342 sqlite3 *_db_handle = NULL;
344 sqlite3 *emstorage_get_db_connection()
346 if (NULL == _db_handle)
347 emstorage_db_open(NULL);
350 #endif /* _MULTIPLE_DB_HANDLE */
352 /* ------------------------------------------------------------------------------ */
353 /* Mutex using shared memory */
355 pthread_mutex_t mutex;
359 mmapped_t *mapped_for_db_lock = NULL;
360 int shm_fd_for_db_lock = 0;
362 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
363 mmapped_t *mapped_for_generating_mail_id = NULL;
364 int shm_fd_for_generating_mail_id = 0;
365 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
367 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__
368 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
370 _timedlock_shm_mutex(mapped_for_db_lock, 2);\
371 return_value = function_call;\
372 _unlockshm_mutex(mapped_for_db_lock);\
375 #else /* __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
376 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
377 { return_value = function_call; }
378 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
380 /* LCOV_EXCL_START */
381 static int emstorage_exec_query_by_prepare_v2(sqlite3 *local_db_handle, char *query_string)
383 EM_DEBUG_FUNC_BEGIN("local_db_handle[%p] query_string[%p]", local_db_handle, query_string);
384 int error = EMAIL_ERROR_NONE;
386 DB_STMT db_statement = NULL;
388 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, query_string, EM_SAFE_STRLEN(query_string), &db_statement, NULL), rc);
389 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
390 ("sqlite3_prepare failed [%d] [%s]", rc, query_string));
392 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(db_statement), rc);
393 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
394 ("sqlite3_step failed [%d] [%s]", rc, query_string));
398 if (db_statement != NULL) {
399 rc = sqlite3_finalize(db_statement);
400 if (rc != SQLITE_OK) {
401 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
402 error = EMAIL_ERROR_DB_FAILURE;
409 INTERNAL_FUNC int emstorage_shm_file_init(const char *shm_file_name)
411 EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
412 char errno_buf[ERRNO_BUF_SIZE] = {0};
414 if (!shm_file_name) {
415 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
416 return EMAIL_ERROR_INVALID_PARAM;
419 int fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); /* note: permission is not working */
421 EM_DEBUG_EXCEPTION("shm_open errno [%d] [%s]", errno, EM_STRERROR(errno_buf));
422 return EMAIL_ERROR_SYSTEM_FAILURE;
426 EM_DEBUG_LOG("** Create SHM FILE **");
427 if (ftruncate(fd, sizeof(mmapped_t)) != 0) {
428 EM_DEBUG_EXCEPTION("ftruncate errno [%d]", errno);
429 return EMAIL_ERROR_SYSTEM_FAILURE;
432 mmapped_t *m = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
433 if (m == MAP_FAILED) {
434 EM_DEBUG_EXCEPTION("mmap errno [%d]", errno);
436 return EMAIL_ERROR_SYSTEM_FAILURE;
441 pthread_mutexattr_t mattr;
442 pthread_mutexattr_init(&mattr);
443 pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
444 pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
445 pthread_mutex_init(&(m->mutex), &mattr);
446 pthread_mutexattr_destroy(&mattr);
448 pthread_mutex_destroy(&(m->mutex));
449 munmap(m, sizeof(mmapped_t));
453 return EMAIL_ERROR_NONE;
456 int emstorage_shm_file_destroy(const char *shm_file_name)
458 EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
459 char errno_buf[ERRNO_BUF_SIZE] = {0};
461 if (!shm_file_name) {
462 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
463 return EMAIL_ERROR_INVALID_PARAM;
466 if (shm_unlink(shm_file_name) != 0)
467 EM_DEBUG_EXCEPTION("shm_unlink failed: %s", EM_STRERROR(errno_buf));
469 return EMAIL_ERROR_NONE;
472 int _initialize_shm_mutex(const char *shm_file_name, int *param_shm_fd, mmapped_t **param_mapped)
474 EM_DEBUG_FUNC_BEGIN("shm_file_name [%p] param_shm_fd [%p], param_mapped [%p]", shm_file_name, param_shm_fd, param_mapped);
475 char errno_buf[ERRNO_BUF_SIZE] = {0};
477 if (!shm_file_name || !param_shm_fd || !param_mapped) {
478 /* LCOV_EXCL_START */
479 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
480 return EMAIL_ERROR_INVALID_PARAM;
484 if (!(*param_mapped)) {
485 EM_DEBUG_LOG("** mapping begin **");
486 if (!(*param_shm_fd)) { /* open shm_file_name at first. Otherwise, the num of files in /proc/pid/fd will be increasing */
487 *param_shm_fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
488 if ((*param_shm_fd) == -1) {
489 /* LCOV_EXCL_START */
490 EM_DEBUG_EXCEPTION("FAIL: shm_open(): %s", EM_STRERROR(errno_buf));
491 return EMAIL_ERROR_SYSTEM_FAILURE;
496 fchmod((*param_shm_fd), 0666);
497 EM_DEBUG_LOG("** Create SHM FILE **");
498 if (ftruncate((*param_shm_fd), sizeof(mmapped_t)) != 0) {
499 /* LCOV_EXCL_START */
500 EM_DEBUG_EXCEPTION("ftruncate errno [%d]", errno);
501 return EMAIL_ERROR_SYSTEM_FAILURE;
505 mmapped_t *tmp = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ|PROT_WRITE, MAP_SHARED, (*param_shm_fd), 0);
506 if (tmp == MAP_FAILED) {
507 /* LCOV_EXCL_START */
508 EM_DEBUG_EXCEPTION("mmap failed: %s", EM_STRERROR(errno_buf));
509 return EMAIL_ERROR_SYSTEM_FAILURE;
515 pthread_mutexattr_t mattr;
516 pthread_mutexattr_init(&mattr);
517 pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
518 pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
519 pthread_mutex_init(&(tmp->mutex), &mattr);
520 pthread_mutexattr_destroy(&mattr);
526 return EMAIL_ERROR_NONE;
528 /* LCOV_EXCL_START */
529 int _timedlock_shm_mutex(mmapped_t *param_mapped, int sec)
531 EM_DEBUG_FUNC_BEGIN("param_mapped [%p], sec [%d]", param_mapped, sec);
534 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
535 return EMAIL_ERROR_INVALID_PARAM;
538 struct timespec abs_time;
539 clock_gettime(CLOCK_REALTIME, &abs_time);
540 abs_time.tv_sec += sec;
541 char errno_buf[ERRNO_BUF_SIZE] = {0};
543 int err = pthread_mutex_timedlock(&(param_mapped->mutex), &abs_time);
545 if (err == EOWNERDEAD) {
546 err = pthread_mutex_consistent(&(param_mapped->mutex));
547 EM_DEBUG_EXCEPTION("Previous owner is dead with lock. Fix mutex : %s", EM_STRERROR(errno_buf));
548 } else if (err != 0) {
549 EM_DEBUG_EXCEPTION("ERROR : %s", EM_STRERROR(errno_buf));
554 return EMAIL_ERROR_NONE;
557 void _unlockshm_mutex(mmapped_t *param_mapped)
559 EM_DEBUG_FUNC_BEGIN();
560 pthread_mutex_unlock(&(param_mapped->mutex));
564 /* ------------------------------------------------------------------------------ */
567 static int _open_counter = 0;
569 static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name);
570 static int _read_password_from_secure_storage(char *file_name, char **password);
572 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
573 static int _get_cert_password_file_name(int index, char *cert_password_file_name);
577 const char *object_name;
578 unsigned int data_flag;
581 static const email_db_object_t _g_db_tables[] = {
582 { "mail_read_mail_uid_tbl", 1},
584 { "mail_attachment_tbl", 1},
588 static const email_db_object_t _g_db_indexes[] = {
589 { "mail_read_mail_uid_idx1", 1},
591 { "mail_attachment_idx1", 1},
596 CREATE_TABLE_MAIL_ACCOUNT_TBL,
597 CREATE_TABLE_MAIL_BOX_TBL,
598 CREATE_TABLE_MAIL_READ_MAIL_UID_TBL,
599 CREATE_TABLE_MAIL_RULE_TBL,
600 CREATE_TABLE_MAIL_TBL,
601 CREATE_TABLE_MAIL_ATTACHMENT_TBL,
602 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
603 CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL,
605 CREATE_TABLE_DUMMY_INDEX1,
607 CREATE_TABLE_MAIL_MEETING_TBL,
608 #ifdef __FEATURE_LOCAL_ACTIVITY__
609 CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL,
611 CREATE_TABLE_DUMMY_INDEX2,
613 CREATE_TABLE_MAIL_CERTIFICATE_TBL,
614 CREATE_TABLE_MAIL_TASK_TBL,
615 #ifdef __FEATURE_BODY_SEARCH__
616 CREATE_TABLE_MAIL_TEXT_TBL,
618 CREATE_TABLE_DUMMY_INDEX3,
621 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
622 CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
624 CREATE_TABLE_DUMMY_INDEX4,
628 CREATE_TABLE_MAIL_ACCOUNT_IDX,
629 CREATE_TABLE_MAIL_BOX_IDX,
630 CREATE_TABLE_MAIL_READ_MAIL_UID_IDX,
631 CREATE_TABLE_MAIL_IDX,
632 CREATE_TABLE_MAIL_ATTACHMENT_IDX,
633 CREATE_TABLE_MAIL_MEETING_IDX,
634 CREATE_TABLE_MAIL_TASK_IDX,
635 CREATE_TABLE_MAIL_DATETIME_IDX,
636 CREATE_TABLE_MAIL_THREAD_IDX,
641 DATA1_IDX_IN_MAIL_ACTIVITY_TBL = 0,
642 TRANSTYPE_IDX_IN_MAIL_ACTIVITY_TBL,
643 FLAG_IDX_IN_MAIL_ACTIVITY_TBL,
647 CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL = 0,
648 ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
649 ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
650 ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
651 EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
652 EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
653 EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
654 ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL,
655 EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL,
656 SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL,
657 FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL,
666 ACCOUNT_ID_IDX_IN_MAIL_BOX_TBL = 0,
667 LOCAL_YN_IDX_IN_MAIL_BOX_TBL,
668 MAILBOX_NAME_IDX_IN_MAIL_BOX_TBL,
669 MAILBOX_TYPE_IDX_IN_MAIL_BOX_TBL,
670 ALIAS_IDX_IN_MAIL_BOX_TBL,
671 SYNC_WITH_SERVER_YN_IDX_IN_MAIL_BOX_TBL,
672 MODIFIABLE_YN_IDX_IN_MAIL_BOX_TBL,
673 TOTAL_MAIL_COUNT_ON_SERVER_IDX_IN_MAIL_BOX_TBL,
674 ARCHIVE_IDX_IN_MAIL_BOX_TBL,
675 MAIL_SLOT_SIZE_IDX_IN_MAIL_BOX_TBL,
679 ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL = 0,
680 LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
681 MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL,
682 LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
683 SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
684 RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL,
685 SYNC_STATUS_IDX_IN_MAIL_READ_MAIL_UID_TBL,
686 FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL,
687 FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL,
688 IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, /* unused */
691 #ifdef __FEATURE_BODY_SEARCH__
693 MAIL_ID_IDX_IN_MAIL_TEXT_TBL = 0,
694 ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL,
695 MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL,
696 BODY_TEXT_IDX_IN_MAIL_TEXT_TBL,
697 FIELD_COUNT_OF_MAIL_TEXT_TBL,
702 ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL = 0,
703 RULE_ID_IDX_IN_MAIL_RULE_TBL,
704 FILTER_NAME_IDX_IN_MAIL_RULE_TBL,
705 TYPE_IDX_IN_MAIL_RULE_TBL,
706 VALUE_IDX_IN_MAIL_RULE_TBL,
707 VALUE2_IDX_IN_MAIL_RULE_TBL,
708 ACTION_TYPE_IDX_IN_MAIL_RULE_TBL,
709 TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL,
710 FLAG1_IDX_IN_MAIL_RULE_TBL,
711 FLAG2_IDX_IN_MAIL_RULE_TBL,
715 MAIL_ID_IDX_IN_MAIL_TBL = 0,
716 ACCOUNT_ID_IDX_IN_MAIL_TBL,
717 MAILBOX_ID_IDX_IN_MAIL_TBL,
718 MAILBOX_TYPE_IDX_IN_MAIL_TBL,
719 SUBJECT_IDX_IN_MAIL_TBL,
720 DATETIME_IDX_IN_MAIL_TBL,
721 SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL,
722 SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL,
723 SERVER_MAIL_ID_IDX_IN_MAIL_TBL,
724 MESSAGE_ID_IDX_IN_MAIL_TBL,
725 REFERENCE_ID_IDX_IN_MAIL_TBL,
726 FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL,
727 FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL,
728 FULL_ADDRESS_TO_IDX_IN_MAIL_TBL,
729 FULL_ADDRESS_CC_IDX_IN_MAIL_TBL,
730 FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL,
731 FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL,
732 EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL,
733 EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL,
734 ALIAS_SENDER_IDX_IN_MAIL_TBL,
735 ALIAS_RECIPIENT_IDX_IN_MAIL_TBL,
736 BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL,
737 FILE_PATH_PLAIN_IDX_IN_MAIL_TBL,
738 FILE_PATH_HTML_IDX_IN_MAIL_TBL,
739 FILE_PATH_MIME_ENTITY_IDX_IN_MAIL_TBL,
740 MAIL_SIZE_IDX_IN_MAIL_TBL,
741 FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL,
742 FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL,
743 FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL,
744 FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL,
745 FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL,
746 FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL,
747 FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL,
748 DRM_STATUS_IDX_IN_MAIL_TBL,
749 PRIORITY_IDX_IN_MAIL_TBL,
750 SAVE_STATUS_IDX_IN_MAIL_TBL,
751 LOCK_STATUS_IDX_IN_MAIL_TBL,
752 REPORT_STATUS_IDX_IN_MAIL_TBL,
753 ATTACHMENT_COUNT_IDX_IN_MAIL_TBL,
754 INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL,
755 THREAD_ID_IDX_IN_MAIL_TBL,
756 THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL,
757 PREVIEW_TEXT_IDX_IN_MAIL_TBL,
758 MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL,
759 MESSAGE_CLASS_IDX_IN_MAIL_TBL,
760 DIGEST_TYPE_IDX_IN_MAIL_TBL,
761 SMIME_TYPE_IDX_IN_MAIL_TBL,
762 SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL,
763 REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL,
764 TAG_ID_IDX_IN_MAIL_TBL,
765 REPLIED_TIME_IDX_IN_MAIL_TBL,
766 FORWARDED_TIME_IDX_IN_MAIL_TBL,
767 DEFAULT_CHARSET_IDX_IN_MAIL_TBL,
768 EAS_DATA_LENGTH_IDX_IN_MAIL_TBL,
769 EAS_DATA_IDX_IN_MAIL_TBL,
770 USER_NAME_IDX_IN_MAIL_TBL,
771 FIELD_COUNT_OF_MAIL_TBL, /* End of mail_tbl */
775 ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL = 0,
776 ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL,
777 ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL,
778 CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
779 ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL,
780 MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
781 ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
782 MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
783 ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
784 ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
785 ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL,
786 ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
787 ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
788 #ifdef __ATTACHMENT_OPTI__
789 ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL,
790 SECTION_IDX_IN_MAIL_ATTACHMENT_TBL,
795 IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL = 0,
796 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
797 MAIL_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
798 ACCOUNT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
799 ADDRESS_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
800 ADDRESS_IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL,
802 ADDRESS_IDX_IN_MAIL_CONTACT_SYNC_TBL,
803 CONTACT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
804 STORAGE_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
805 CONTACT_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
806 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
807 DISPLAY_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
808 FLAG1_IDX_IN_MAIL_CONTACT_SYNC_TBL,
812 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
814 ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL = 0,
815 STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
816 ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
817 MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
818 SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
819 MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
820 MULTI_USER_NAME_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
824 /* sowmya.kr 03032010, changes for get list of mails for given addr list */
825 typedef struct _em_mail_id_list {
827 struct _em_mail_id_list *next;
830 static char *g_test_query[] = {
831 /* 1. select mail_account_tbl */
834 " incoming_server_type, "
835 " incoming_server_address, "
836 " user_email_address, "
837 " incoming_server_user_name, "
838 " incoming_server_password, "
840 " incoming_server_port_number, "
841 " incoming_server_secure_connection, "
842 " incoming_server_authentication_method,"
843 " outgoing_server_type, "
844 " outgoing_server_address, "
845 " outgoing_server_port_number, "
846 " outgoing_server_need_authentication, "
847 " outgoing_server_secure_connection, "
848 " outgoing_server_user_name, "
849 " outgoing_server_password, "
854 " keep_mails_on_pop_server_after_download, "
855 " auto_resend_times, "
856 " outgoing_server_size_limit, "
857 " wifi_auto_download, "
859 " incoming_server_requires_apop,"
861 " is_preset_account, "
865 " req_delivery_receipt, "
866 " req_read_receipt, "
870 " display_name_from, "
872 " forward_with_files, "
876 ", add_my_address_to_bcc"
877 ", notification_status "
879 ", display_content_status "
880 ", default_ringtone_status "
881 ", alert_ringtone_path "
890 " FROM mail_account_tbl",
891 /* 2. select mail_box_tbl */
901 " total_mail_count_on_server, "
902 " has_archived_mails, "
906 " FROM mail_box_tbl ",
907 /* 3. select mail_read_mail_uid_tbl */
916 " flags_seen_field, "
918 " FROM mail_read_mail_uid_tbl ",
919 /* 4. select mail_rule_tbl */
928 " target_mailbox_id, "
931 " FROM mail_rule_tbl ",
932 /* 5. select mail_tbl */
941 " server_mail_status, "
942 " server_mailbox_name, "
945 " reference_mail_id, "
946 " full_address_from, "
947 " full_address_reply, "
950 " full_address_bcc, "
951 " full_address_return, "
952 " email_address_sender, "
953 " email_address_recipient, "
956 " body_download_status, "
959 " file_path_mime_entity, "
961 " flags_seen_field ,"
962 " flags_deleted_field ,"
963 " flags_flagged_field ,"
964 " flags_answered_field ,"
965 " flags_recent_field ,"
966 " flags_draft_field ,"
967 " flags_forwarded_field,"
973 " attachment_count, "
974 " inline_content_count, "
976 " thread_item_count, "
978 " meeting_request_status, "
983 /* 6. select mail_attachment_tbl */
992 " attachment_save_status, "
993 " attachment_drm_type, "
994 " attachment_drm_method, "
995 " attachment_inline_content_status, "
996 " attachment_mime_type "
997 " FROM mail_attachment_tbl ",
999 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
1009 " FROM mail_partial_body_activity_tbl ",
1016 " meeting_response, "
1020 " global_object_id, "
1023 " standard_time_start_date, "
1026 " daylight_time_start_date, "
1028 " FROM mail_meeting_tbl ",
1030 #ifdef __FEATURE_LOCAL_ACTIVITY__
1039 " FROM mail_local_activity_tbl ",
1046 " expiration_year, "
1047 " expiration_month, "
1049 " issue_organization_name, "
1054 " FROM mail_certificate_tbl ",
1060 " task_parameter_length, "
1061 " task_parameter , "
1063 " FROM mail_task_tbl ",
1064 #ifdef __FEATURE_BODY_SEARCH__
1070 " FROM mail_text_tbl ",
1072 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
1080 " multi_user_name, "
1081 " FROM mail_auto_download_activity_tbl ",
1086 int _field_count_of_table[CREATE_TABLE_MAX] = { 0, };
1088 static int _get_table_field_data_char(char **table, char *buf, int index)
1090 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1091 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1095 if (table[index] != NULL) {
1096 *buf = (char)atoi(table[index]);
1100 /* EM_DEBUG_LOG("Empty field. Set as zero"); */
1106 static int _get_table_field_data_int(char **table, int *buf, int index)
1108 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1109 /* LCOV_EXCL_START */
1110 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1112 /* LCOV_EXCL_STOP */
1115 if (table[index] != NULL) {
1116 *buf = atoi(table[index]);
1120 /* EM_DEBUG_LOG("Empty field. Set as zero"); */
1121 /* LCOV_EXCL_START */
1124 /* LCOV_EXCL_STOP */
1127 static int _get_table_field_data_time_t(char **table, time_t *buf, int index)
1129 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1130 /* LCOV_EXCL_START */
1131 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1133 /* LCOV_EXCL_STOP */
1136 if (table[index] != NULL) {
1137 *buf = (time_t)atol(table[index]);
1141 /* EM_DEBUG_LOG("Empty field. Set as zero"); */
1142 /* LCOV_EXCL_START */
1145 /* LCOV_EXCL_STOP */
1148 static int _get_table_field_data_string(char **table, char **buf, int ucs2, int index)
1152 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1153 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1157 char *pTemp = table[index];
1162 sLen = EM_SAFE_STRLEN(pTemp);
1164 *buf = (char *) em_malloc(sLen + 1);
1166 EM_DEBUG_EXCEPTION("malloc is failed");
1169 strncpy(*buf, pTemp, sLen+1);
1170 (*buf)[sLen] = '\0';
1174 #ifdef _PRINT_STORAGE_LOG_
1176 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", *buf, index);
1178 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1186 static int _get_table_field_data_string_without_allocation(char **table, char *buf, int buffer_size, int ucs2, int index)
1188 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1189 EM_DEBUG_EXCEPTION(" table[%p], buf[%p], index[%d]", table, buf, index);
1193 char *pTemp = table[index];
1198 memset(buf, 0, buffer_size);
1199 strncpy(buf, pTemp, buffer_size - 1);
1201 #ifdef _PRINT_STORAGE_LOG_
1203 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", buf, index);
1205 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1211 static int _get_table_field_data_blob(char **table, void **buffer, int buffer_size, int index)
1213 if ((table == NULL) || (buffer == NULL) || (index < 0)) {
1214 /* LCOV_EXCL_START */
1215 EM_DEBUG_EXCEPTION(" table[%p], buffer[%p], buffer_size [%d], index[%d]", table, buffer, buffer_size, index);
1217 /* LCOV_EXCL_STOP */
1220 char *temp_buffer = table[index];
1222 if (temp_buffer == NULL)
1225 /* LCOV_EXCL_START */
1226 *buffer = malloc(buffer_size);
1227 if (*buffer == NULL) {
1228 EM_DEBUG_EXCEPTION("allocation failed.");
1231 memset(*buffer, 0, buffer_size);
1232 memcpy(*buffer, temp_buffer, buffer_size);
1233 /* LCOV_EXCL_STOP */
1235 #ifdef _PRINT_STORAGE_LOG_
1237 EM_DEBUG_LOG("_get_table_field_data_string - buffer[%s], index[%d]", buffer, index);
1239 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1244 /* LCOV_EXCL_START */
1245 static int _get_stmt_field_data_char(DB_STMT hStmt, char *buf, int index)
1247 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1248 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1252 if (sqlite3_column_text(hStmt, index) != NULL) {
1253 *buf = (char)sqlite3_column_int(hStmt, index);
1254 #ifdef _PRINT_STORAGE_LOG_
1255 EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1260 EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1264 /* LCOV_EXCL_STOP */
1265 static int _get_stmt_field_data_int(DB_STMT hStmt, int *buf, int index)
1267 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1268 /* LCOV_EXCL_START */
1269 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1271 /* LCOV_EXCL_STOP */
1274 if (sqlite3_column_text(hStmt, index) != NULL) {
1275 *buf = sqlite3_column_int(hStmt, index);
1276 #ifdef _PRINT_STORAGE_LOG_
1277 EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1281 /* LCOV_EXCL_START */
1282 EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1285 /* LCOV_EXCL_STOP */
1287 /* LCOV_EXCL_START */
1288 static int _get_stmt_field_data_time_t(DB_STMT hStmt, time_t *buf, int index)
1290 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1291 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1295 if (sqlite3_column_text(hStmt, index) != NULL) {
1296 *buf = (time_t)sqlite3_column_int(hStmt, index);
1297 #ifdef _PRINT_STORAGE_LOG_
1298 EM_DEBUG_LOG("_get_stmt_field_data_time_t [%d]", *buf);
1303 EM_DEBUG_LOG("_get_stmt_field_data_time_t fail. index [%d]", index);
1306 /* LCOV_EXCL_STOP */
1307 static int _get_stmt_field_data_string(DB_STMT hStmt, char **buf, int ucs2, int index)
1309 if (!hStmt || !buf || (index < 0)) { /*prevent 39619*/
1310 EM_DEBUG_EXCEPTION("No hStmt, buf[%p], index[%d]", buf, index);
1315 sLen = sqlite3_column_bytes(hStmt, index);
1317 #ifdef _PRINT_STORAGE_LOG_
1318 EM_DEBUG_LOG("_get_stmt_field_data_string sqlite3_column_bytes sLen[%d]", sLen);
1322 *buf = (char *) em_malloc(sLen + 1);
1324 EM_DEBUG_EXCEPTION("em_mallocfailed");
1328 strncpy(*buf, (char *)sqlite3_column_text(hStmt, index), sLen);
1332 #ifdef _PRINT_STORAGE_LOG_
1334 EM_DEBUG_LOG("buf[%s], index[%d]", *buf, index);
1336 EM_DEBUG_LOG("_get_stmt_field_data_string - No string got");
1342 static void _get_stmt_field_data_blob(DB_STMT hStmt, void **buf, int index)
1344 if (!hStmt || !buf || (index < 0)) { /*prevent 39618*/
1345 /* LCOV_EXCL_START */
1346 EM_DEBUG_EXCEPTION("No hStmt, buf[%p], index[%d]", buf, index);
1348 /* LCOV_EXCL_STOP */
1352 sLen = sqlite3_column_bytes(hStmt, index);
1354 #ifdef _PRINT_STORAGE_LOG_
1355 EM_DEBUG_LOG("_get_stmt_field_data_blob sqlite3_column_bytes sLen[%d]", sLen);
1359 *buf = (char *) em_malloc(sLen);
1361 /* LCOV_EXCL_START */
1362 EM_DEBUG_EXCEPTION("em_mallocfailed");
1364 /* LCOV_EXCL_STOP */
1367 memcpy(*buf, (void *)sqlite3_column_blob(hStmt, index), sLen);
1369 /* LCOV_EXCL_START */
1371 /* LCOV_EXCL_STOP */
1374 /* LCOV_EXCL_START */
1375 static int _bind_stmt_field_data_char(DB_STMT hStmt, int index, char value)
1377 if ((hStmt == NULL) || (index < 0)) {
1378 EM_DEBUG_EXCEPTION("index[%d]", index);
1382 int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1384 if (ret != SQLITE_OK) {
1385 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1392 static int _bind_stmt_field_data_int(DB_STMT hStmt, int index, int value)
1394 if ((hStmt == NULL) || (index < 0)) {
1395 EM_DEBUG_EXCEPTION("index[%d]", index);
1399 int ret = sqlite3_bind_int(hStmt, index+1, value);
1401 if (ret != SQLITE_OK) {
1402 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1409 static int _bind_stmt_field_data_time_t(DB_STMT hStmt, int index, time_t value)
1411 if ((hStmt == NULL) || (index < 0)) {
1412 EM_DEBUG_EXCEPTION("index[%d]", index);
1416 int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1418 if (ret != SQLITE_OK) {
1419 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1425 /* LCOV_EXCL_STOP */
1426 static int _bind_stmt_field_data_string(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1428 if ((hStmt == NULL) || (index < 0)) {
1429 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1433 #ifdef _PRINT_STORAGE_LOG_
1434 EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1439 ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1441 ret = sqlite3_bind_text(hStmt, index+1, "", -1, SQLITE_STATIC);
1443 if (ret != SQLITE_OK) {
1444 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1450 static int _bind_stmt_field_data_nstring(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1452 if ((hStmt == NULL) || (index < 0)) {
1453 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1457 #ifdef _PRINT_STORAGE_LOG_
1458 EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1462 if (value != NULL) {
1463 if (strlen(value) <= max_len)
1464 ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1466 char *buf = (char*)em_malloc(sizeof(char) * (max_len));
1468 EM_DEBUG_EXCEPTION("em_mallocfailed");
1471 snprintf(buf, max_len-1, "%s", value);
1472 ret = sqlite3_bind_text(hStmt, index+1, buf, -1, SQLITE_TRANSIENT);
1476 ret = sqlite3_bind_text(hStmt, index+1, "", -1, SQLITE_STATIC);
1478 if (ret != SQLITE_OK) {
1479 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1485 /* LCOV_EXCL_START */
1486 static int _bind_stmt_field_data_blob(DB_STMT hStmt, int index, void *blob, int blob_size)
1488 if ((hStmt == NULL) || (index < 0)) {
1489 EM_DEBUG_EXCEPTION("index[%d], blob_size[%d]", index, blob_size);
1493 #ifdef _PRINT_STORAGE_LOG_
1494 EM_DEBUG_LOG("hStmt = %p, index = %d, blob_size = %d, blob = [%p]", hStmt, index, blob_size, blob);
1499 ret = sqlite3_bind_blob(hStmt, index+1, blob, blob_size, SQLITE_STATIC);
1501 ret = sqlite3_bind_null(hStmt, index+1);
1503 if (ret != SQLITE_OK) {
1504 EM_DEBUG_EXCEPTION("sqlite3_bind_blob fail [%d]", ret);
1510 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)
1513 EM_DEBUG_FUNC_BEGIN("Prepared Statement: local_db_handle[%p] query_string[%p]", local_db_handle, query_string);
1514 int error = EMAIL_ERROR_NONE;
1515 int rc = 0, count = 0;
1516 DB_STMT db_statement = NULL;
1518 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, query_string, EM_SAFE_STRLEN(query_string), &db_statement, NULL), rc);
1520 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
1521 ("sqlite3_prepare_stmt failed [%d] [%s]", rc, query_string));
1523 _bind_stmt_field_data_string(db_statement, count++, target_mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
1524 _bind_stmt_field_data_int(db_statement, count++, input_mailbox_id);
1525 _bind_stmt_field_data_int(db_statement, count++, target_account_id);
1529 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(db_statement), rc);
1530 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
1531 ("sqlite3_step_stmt failed [%d] [%s]", rc, query_string));
1535 if (db_statement != NULL) {
1536 rc = sqlite3_finalize(db_statement);
1537 if (rc != SQLITE_OK) {
1538 EM_DEBUG_EXCEPTION("sqlite3_finalize_stmt error [%d]", rc);
1539 error = EMAIL_ERROR_DB_FAILURE;
1548 static int _delete_temp_file(const char *path)
1550 EM_DEBUG_FUNC_BEGIN("path[%p]", path);
1553 struct dirent *entry = NULL;
1555 char buf[1024] = {0x00, };
1557 if ((dp = opendir(path)) == NULL) {
1558 EM_DEBUG_EXCEPTION("opendir(\"%s\") failed...", path);
1562 while ((entry = readdir(dp)) != NULL) {
1563 SNPRINTF(buf, sizeof(buf), "%s/%s", path, entry->d_name);
1564 if (remove(buf) != 0) {
1565 EM_DEBUG_EXCEPTION("File Deletion Failed`");
1570 EM_DEBUG_FUNC_END();
1574 char *cpy_str(char *src)
1579 if (!(p = em_malloc((int)EM_SAFE_STRLEN(src) + 1))) {
1580 EM_DEBUG_EXCEPTION("mailoc failed...");
1583 strncpy(p, src, EM_SAFE_STRLEN(src)+1);
1589 static void _emstorage_close_once(void)
1591 EM_DEBUG_FUNC_BEGIN();
1593 EM_DEBUG_FUNC_END();
1596 INTERNAL_FUNC int emstorage_close(int *err_code)
1598 EM_DEBUG_FUNC_BEGIN();
1601 int error = EMAIL_ERROR_NONE;
1603 if (!emstorage_db_close(NULL, &error))
1605 if (--_open_counter == 0)
1606 _emstorage_close_once();
1610 if (err_code != NULL)
1613 EM_DEBUG_FUNC_END("ret [%d]", ret);
1617 static void *_emstorage_open_once(char *multi_user_name, int *err_code)
1619 EM_DEBUG_FUNC_BEGIN();
1621 int error = EMAIL_ERROR_NONE;
1623 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1624 char buf[MAX_PATH] = {0};
1625 char *prefix_path = NULL;
1627 error = emcore_get_container_path(multi_user_name, &prefix_path);
1628 if (error != EMAIL_ERROR_NONE) {
1629 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
1633 memset(buf, 0x00, sizeof(buf));
1634 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, EMAIL_PATH);
1635 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
1636 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
1637 error = EMAIL_ERROR_SYSTEM_FAILURE;
1639 error = EMAIL_ERROR_OUT_OF_MEMORY;
1643 memset(buf, 0x00, sizeof(buf));
1644 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAIL_HOME);
1645 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
1646 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
1647 error = EMAIL_ERROR_SYSTEM_FAILURE;
1649 error = EMAIL_ERROR_OUT_OF_MEMORY;
1653 memset(buf, 0x00, sizeof(buf));
1654 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAIL_TEMP);
1655 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
1656 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
1657 error = EMAIL_ERROR_SYSTEM_FAILURE;
1659 error = EMAIL_ERROR_OUT_OF_MEMORY;
1663 _delete_temp_file(buf);
1664 EM_SAFE_FREE(prefix_path);
1666 if (mkdir(DATA_PATH, DIRECTORY_PERMISSION) != 0) {
1667 EM_DEBUG_EXCEPTION(" mkdir failed ");
1668 error = EMAIL_ERROR_SYSTEM_FAILURE;
1670 error = EMAIL_ERROR_OUT_OF_MEMORY;
1673 if (mkdir(EMAIL_PATH, DIRECTORY_PERMISSION) != 0) {
1674 EM_DEBUG_EXCEPTION(" mkdir failed ");
1675 error = EMAIL_ERROR_SYSTEM_FAILURE;
1677 error = EMAIL_ERROR_OUT_OF_MEMORY;
1680 if (mkdir(MAIL_HOME, DIRECTORY_PERMISSION) != 0) {
1681 EM_DEBUG_EXCEPTION(" mkdir failed ");
1682 error = EMAIL_ERROR_SYSTEM_FAILURE;
1684 error = EMAIL_ERROR_OUT_OF_MEMORY;
1687 if (mkdir(MAIL_TEMP, DIRECTORY_PERMISSION) != 0) {
1688 EM_DEBUG_EXCEPTION(" mkdir failed ");
1689 error = EMAIL_ERROR_SYSTEM_FAILURE;
1691 error = EMAIL_ERROR_OUT_OF_MEMORY;
1695 _delete_temp_file(MAIL_TEMP);
1698 if (!emstorage_create_table(multi_user_name, EMAIL_CREATE_DB_NORMAL, &error)) {
1699 EM_DEBUG_EXCEPTION(" emstorage_create_table failed - %d", error);
1706 if (err_code != NULL)
1712 /* pData : a parameter which is registered when busy handler is registerd */
1713 /* count : retry count */
1714 static int _callback_sqlite_busy_handler(void *pData, int count)
1716 if (10 - count > 0) {
1717 struct timespec time = {
1719 .tv_nsec = (count + 1) * 100 * 1000 * 1000
1721 EM_DEBUG_LOG("Busy handler called!!: PID[%d] / CNT [%d]", getpid(), count);
1722 nanosleep(&time, NULL);
1725 EM_DEBUG_EXCEPTION("Busy handler will be returned SQLITE_BUSY error PID[%d] / CNT[%d]", getpid(), count);
1730 static int _callback_collation_utf7_sort(void *data, int length_text_a, const void *text_a,
1731 int length_text_b, const void *text_b)
1733 EM_DEBUG_FUNC_BEGIN();
1735 char *converted_string_a = NULL;
1736 char *converted_string_b = NULL;
1738 EM_DEBUG_LOG_DEV("text_a : [%s]", text_a);
1739 converted_string_a = emcore_convert_mutf7_to_utf8((char *)text_a);
1740 EM_DEBUG_LOG_DEV("Converted text_a : [%s]", converted_string_a);
1742 EM_DEBUG_LOG_DEV("text_b : [%s]", text_b);
1743 converted_string_b = emcore_convert_mutf7_to_utf8((char *)text_b);
1744 EM_DEBUG_LOG_DEV("Converted text_b : [%s]", converted_string_b);
1746 if (converted_string_a && converted_string_b)
1747 result = strcmp(converted_string_a, converted_string_b);
1749 EM_SAFE_FREE(converted_string_a);
1750 EM_SAFE_FREE(converted_string_b);
1752 EM_DEBUG_FUNC_END();
1756 static int _delete_all_files_and_directories(char *db_file_path, int *err_code)
1758 EM_DEBUG_FUNC_BEGIN();
1760 int error = EMAIL_ERROR_NONE;
1763 if (!emstorage_delete_file(db_file_path, &error)) {
1764 if (error != EMAIL_ERROR_FILE_NOT_FOUND) {
1765 EM_DEBUG_EXCEPTION_SEC("remove failed - %s", EMAIL_SERVICE_DB_FILE_PATH);
1770 if (!emstorage_delete_dir((char *)MAIL_HOME, &error)) {
1771 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed");
1780 EM_DEBUG_FUNC_END();
1784 static int _recovery_from_malformed_db_file(char *db_file_path, int *err_code)
1786 EM_DEBUG_FUNC_BEGIN();
1788 int error = EMAIL_ERROR_NONE;
1791 /* Delete all files and directories */
1792 if (!_delete_all_files_and_directories(db_file_path, &error)) {
1793 EM_DEBUG_EXCEPTION("_delete_all_files_and_directories failed [%d]", error);
1797 /* Delete all accounts on MyAccount */
1799 /* Delete all managed connection to DB */
1800 emstorage_reset_db_handle_list();
1807 EM_DEBUG_FUNC_END();
1811 int _xsystem(const char *argv[])
1819 perror("fork failed");
1822 if (execvp(argv[0], (char *const *)argv) == -1) {
1823 perror("execute init db script");
1832 if (waitpid(pid, &status, 0) == -1) {
1833 perror("waitpid failed");
1837 if (WIFSIGNALED(status)) {
1842 if (!WIFEXITED(status)) {
1843 perror("should not happen");
1847 return WEXITSTATUS(status);
1850 #define SCRIPT_INIT_DB "/usr/bin/email-service_init_db.sh"
1852 INTERNAL_FUNC int emstorage_init_db(char *multi_user_name)
1854 EM_DEBUG_FUNC_BEGIN();
1855 int err = EMAIL_ERROR_NONE;
1856 char *prefix_path = NULL;
1857 char *output_file_path = NULL;
1858 char temp_file_path[MAX_PATH] = {0};
1859 gid_t email_gid = -1;
1860 uid_t email_uid = -1;
1862 struct group buf_group;
1864 struct passwd buf_passwd;
1865 char buf_gr[MAX_GRP_BUF_SIZE];
1866 char buf_pw[MAX_GRP_BUF_SIZE];
1869 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1870 err = emcore_get_container_path(multi_user_name, &prefix_path);
1871 if (err != EMAIL_ERROR_NONE) {
1872 if (err != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1873 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", err);
1878 prefix_path = strdup("");
1881 if (err == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1882 err = emcore_get_canonicalize_path((char *)EMAIL_SERVICE_DB_FILE_PATH, &output_file_path);
1883 if (err != EMAIL_ERROR_NONE) {
1884 EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", err);
1888 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
1890 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
1893 EM_DEBUG_LOG("db file path : [%s]", temp_file_path);
1895 if (!g_file_test(temp_file_path, G_FILE_TEST_EXISTS)) {
1897 const char *argv_script[] = {"/bin/sh", SCRIPT_INIT_DB, NULL};
1898 ret = _xsystem(argv_script);
1901 EM_DEBUG_EXCEPTION("_xsystem failed");
1902 err = EMAIL_ERROR_SYSTEM_FAILURE;
1905 result = getgrnam_r(GID, &buf_group, buf_gr, sizeof(buf_gr), &gr);
1906 if (result == 0 && NULL != gr)
1907 email_gid = gr->gr_gid;
1909 email_gid = GID_DEFAULT;
1912 result = getpwnam_r(UID, &buf_passwd, buf_pw, sizeof(buf_pw), &pw);
1913 if (result == 0 && NULL != pw)
1914 email_uid = pw->pw_uid;
1916 email_uid = UID_DEFAULT;
1919 if (chmod(EMAIL_SERVICE_DB_FILE_PATH, 0660) != 0)
1920 EM_DEBUG_EXCEPTION("Fail to change the permission \n");
1921 if (chmod(EMAIL_SERVICE_DB_JOURNAL_FILE_PATH, 0660) != 0)
1922 EM_DEBUG_EXCEPTION("Fail to change the permission \n");
1923 if (chmod(EMAIL_SQL_PATH, 0660) != 0)
1924 EM_DEBUG_EXCEPTION("Fail to change the permission \n");
1926 ret = chown(EMAIL_SERVICE_DB_FILE_PATH, email_uid, email_gid);
1928 EM_DEBUG_EXCEPTION("chown() Fail[errno=%d]", errno);
1929 ret = chown(EMAIL_SERVICE_DB_JOURNAL_FILE_PATH, email_uid, email_gid);
1931 EM_DEBUG_EXCEPTION("chown() Fail[errno=%d]", errno);
1932 ret = chown(EMAIL_SQL_PATH, email_uid, email_gid);
1934 EM_DEBUG_EXCEPTION("chown() Fail[errno=%d]", errno);
1940 EM_SAFE_FREE(prefix_path);
1941 EM_SAFE_FREE(output_file_path);
1943 EM_DEBUG_FUNC_END();
1946 /* LCOV_EXCL_STOP */
1947 INTERNAL_FUNC int em_db_open(char *db_file_path, sqlite3 **sqlite_handle, int *err_code)
1949 EM_DEBUG_FUNC_BEGIN();
1951 int error = EMAIL_ERROR_NONE;
1954 EM_DEBUG_LOG_DEV("*sqlite_handle[%p]", *sqlite_handle);
1956 if (*sqlite_handle) { /*prevent 33351*/
1957 /* LCOV_EXCL_START */
1958 EM_DEBUG_LOG_DEV(">>>>> DB Already Opened......");
1959 if (err_code != NULL)
1962 /* LCOV_EXCL_STOP */
1965 EM_DEBUG_LOG("DB file path : [%s]", db_file_path);
1968 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1969 if (SQLITE_OK != rc) {
1970 /* LCOV_EXCL_START */
1971 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1972 if (SQLITE_PERM == rc || SQLITE_CANTOPEN == rc)
1973 error = EMAIL_ERROR_PERMISSION_DENIED;
1975 error = EMAIL_ERROR_DB_FAILURE;
1977 sqlite3_close(*sqlite_handle);
1978 *sqlite_handle = NULL;
1980 if (SQLITE_CORRUPT == rc) /* SQLITE_CORRUPT : The database disk image is malformed */ {/* Recovery DB file */
1981 EM_DEBUG_LOG("The database disk image is malformed. Trying to remove and create database disk image and directories");
1982 if (!_recovery_from_malformed_db_file(db_file_path, &error)) {
1983 EM_DEBUG_EXCEPTION("_recovery_from_malformed_db_file failed [%d]", error);
1987 EM_DEBUG_LOG("Open DB again");
1988 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1989 if (SQLITE_OK != rc) {
1990 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1991 if (SQLITE_PERM == rc)
1992 error = EMAIL_ERROR_PERMISSION_DENIED;
1994 error = EMAIL_ERROR_DB_FAILURE;
1996 sqlite3_close(*sqlite_handle);
1997 *sqlite_handle = NULL;
1998 goto FINISH_OFF; /*prevent 33351*/
2002 /* LCOV_EXCL_STOP */
2005 /* register busy handler */
2006 EM_DEBUG_LOG_DEV(">>>>> Register DB Handle to busy handler: *sqlite_handle[%p]", *sqlite_handle);
2007 rc = sqlite3_busy_handler(*sqlite_handle, _callback_sqlite_busy_handler, NULL); /* Busy Handler registration, NULL is a parameter which will be passed to handler */
2008 if (SQLITE_OK != rc) {
2009 /* LCOV_EXCL_START */
2010 EM_DEBUG_EXCEPTION("sqlite3_busy_handler fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
2011 error = EMAIL_ERROR_DB_FAILURE;
2012 sqlite3_close(*sqlite_handle);
2013 *sqlite_handle = NULL;
2015 /* LCOV_EXCL_STOP */
2018 /* Register collation callback function */
2019 rc = sqlite3_create_collation(*sqlite_handle, "CONVERTUTF8", SQLITE_UTF8, NULL, _callback_collation_utf7_sort);
2020 if (SQLITE_OK != rc) {
2021 /* LCOV_EXCL_START */
2022 EM_DEBUG_EXCEPTION("sqlite3_create_collation failed : [%d][%s]", rc, sqlite3_errmsg(*sqlite_handle));
2023 error = EMAIL_ERROR_DB_FAILURE;
2024 sqlite3_close(*sqlite_handle);
2025 *sqlite_handle = NULL;
2027 /* LCOV_EXCL_STOP */
2033 if (err_code != NULL)
2036 EM_DEBUG_FUNC_END("ret [%d]", ret);
2040 INTERNAL_FUNC sqlite3* emstorage_db_open(char *multi_user_name, int *err_code)
2042 EM_DEBUG_FUNC_BEGIN();
2044 sqlite3 *_db_handle = NULL;
2046 int error = EMAIL_ERROR_NONE;
2047 char *prefix_path = NULL;
2049 _db_handle = emstorage_get_db_handle(multi_user_name);
2051 if (_db_handle == NULL) {
2052 char *output_file_path = NULL;
2053 char temp_file_path[MAX_PATH] = {0};
2055 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
2056 /* LCOV_EXCL_START */
2057 error = emcore_get_container_path(multi_user_name, &prefix_path);
2058 if (error != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION && error != EMAIL_ERROR_NONE) {
2059 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", error);
2064 prefix_path = strdup("");
2067 if (error == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
2068 /* LCOV_EXCL_START */
2069 if ((error = emcore_get_canonicalize_path((char *)EMAIL_SERVICE_DB_FILE_PATH, &output_file_path)) != EMAIL_ERROR_NONE) {
2070 EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", error);
2074 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
2075 EM_SAFE_FREE(output_file_path);
2077 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
2079 /* LCOV_EXCL_STOP */
2080 if (!em_db_open(temp_file_path, &_db_handle, &error)) {
2081 EM_DEBUG_EXCEPTION("em_db_open failed[%d]", error);
2085 _initialize_shm_mutex(SHM_FILE_FOR_DB_LOCK, &shm_fd_for_db_lock, &mapped_for_db_lock);
2087 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
2088 _initialize_shm_mutex(SHM_FILE_FOR_MAIL_ID_LOCK, &shm_fd_for_generating_mail_id, &mapped_for_generating_mail_id);
2089 #endif /*__FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
2091 emstorage_set_db_handle(multi_user_name, _db_handle);
2093 emstorage_initialize_field_count();
2098 EM_SAFE_FREE(prefix_path);
2100 if (err_code != NULL)
2101 /* LCOV_EXCL_START */
2103 /* LCOV_EXCL_STOP */
2105 EM_DEBUG_FUNC_END("ret [%p]", _db_handle);
2108 /* LCOV_EXCL_START */
2109 INTERNAL_FUNC int emstorage_db_close(char *multi_user_name, int *err_code)
2111 EM_DEBUG_FUNC_BEGIN();
2112 #ifdef _MULTIPLE_DB_HANDLE
2113 sqlite3 *_db_handle = emstorage_get_db_handle(multi_user_name);
2116 int error = EMAIL_ERROR_NONE;
2120 ret = sqlite3_close(_db_handle);
2121 if (ret != SQLITE_OK) {
2122 EM_DEBUG_EXCEPTION(" sqlite3_close fail - %d", ret);
2123 error = EMAIL_ERROR_DB_FAILURE;
2127 #ifdef _MULTIPLE_DB_HANDLE
2128 emstorage_remove_db_handle();
2136 if (err_code != NULL)
2139 EM_DEBUG_FUNC_END("ret [%d]", ret);
2143 INTERNAL_FUNC int emstorage_open(char *multi_user_name, int *err_code)
2145 EM_DEBUG_FUNC_BEGIN();
2148 int error = EMAIL_ERROR_NONE;
2150 char *prefix_path = NULL;
2151 char buf[MAX_PATH] = {0};
2153 if (EM_SAFE_STRLEN(multi_user_name) <= 0) {
2154 SNPRINTF(buf, sizeof(buf), "%s", DB_PATH);
2156 error = emcore_get_container_path(multi_user_name, &prefix_path);
2157 if (error != EMAIL_ERROR_NONE) {
2158 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
2161 SNPRINTF(buf, sizeof(buf), "%s/%s", prefix_path, DB_PATH);
2164 if (!g_file_test(buf, G_FILE_TEST_EXISTS)) {
2165 retValue = mkdir(buf, DIRECTORY_PERMISSION);
2167 EM_DEBUG_LOG("mkdir return- %d", retValue);
2168 EM_DEBUG_LOG("emstorage_open - before sqlite3_open - pid = %d", getpid());
2171 if (emstorage_db_open(multi_user_name, &error) == NULL) {
2172 EM_DEBUG_EXCEPTION("emstorage_db_open failed[%d]", error);
2176 if (_open_counter++ == 0)
2177 _emstorage_open_once(multi_user_name, &error);
2183 EM_SAFE_FREE(prefix_path);
2185 if (err_code != NULL)
2188 EM_DEBUG_FUNC_END("ret [%d]", ret);
2191 /* LCOV_EXCL_STOP */
2192 static int emstorage_get_field_count_from_create_table_query(char *input_create_table_query, int *output_field_count)
2194 EM_DEBUG_FUNC_BEGIN("input_create_table_query[%d], output_field_count[%p]", input_create_table_query, output_field_count);
2195 int err = EMAIL_ERROR_NONE;
2196 int field_count = 0;
2199 if (input_create_table_query == NULL || output_field_count == NULL) {
2200 /* LCOV_EXCL_START */
2201 err = EMAIL_ERROR_INVALID_PARAM;
2203 /* LCOV_EXCL_STOP */
2206 pos = input_create_table_query;
2210 if (pos == NULL || *pos == (char)0)
2213 pos = strchr(pos, ',');
2216 *output_field_count = field_count;
2218 EM_DEBUG_LOG_DEV("field_count [%d]", field_count);
2222 EM_DEBUG_FUNC_END("err [%d]", err);
2226 INTERNAL_FUNC int emstorage_initialize_field_count()
2228 EM_DEBUG_FUNC_BEGIN();
2229 int err = EMAIL_ERROR_NONE;
2231 char **create_table_query = NULL;
2233 if (_field_count_of_table[CREATE_TABLE_MAIL_ACCOUNT_TBL] != 0) {
2234 /* LCOV_EXCL_START */
2235 err = EMAIL_ERROR_ALREADY_INITIALIZED;
2237 /* LCOV_EXCL_STOP */
2240 err = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2241 if (err != EMAIL_ERROR_NONE) {
2242 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", err);
2246 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]));
2247 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]));
2248 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]));
2249 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]));
2250 emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_TBL]));
2251 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]));
2252 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]));
2253 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]));
2254 #ifdef __FEATURE_LOCAL_ACTIVITY__
2255 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]));
2256 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2257 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]));
2258 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]));
2259 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]));
2262 if (create_table_query) {
2264 for (i = 0; i < query_len; i++) {
2265 if (create_table_query[i])
2266 EM_SAFE_FREE(create_table_query[i]);
2268 EM_SAFE_FREE(create_table_query);
2271 EM_DEBUG_FUNC_END("err [%d]", err);
2274 /* LCOV_EXCL_START */
2275 INTERNAL_FUNC int emstorage_create_table(char *multi_user_name, emstorage_create_db_type_t type, int *err_code)
2277 EM_DEBUG_FUNC_BEGIN();
2279 int error = EMAIL_ERROR_NONE;
2280 int rc = -1, ret = false;
2282 char sql_query_string[QUERY_SIZE] = {0, };
2283 char **create_table_query = NULL;
2285 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2287 error = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2288 if (error != EMAIL_ERROR_NONE) {
2289 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
2293 if (query_len < CREATE_TABLE_MAX) {
2294 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
2295 error = EMAIL_ERROR_SYSTEM_FAILURE;
2299 EM_DEBUG_LOG("local_db_handle = %p.", local_db_handle);
2302 char **result = NULL;
2304 /* 1. create mail_account_tbl */
2305 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_account_tbl';";
2306 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2307 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2308 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2310 EM_DEBUG_LOG("emstorage_create_table - result[1] = %s %s", result[1], result[1]);
2312 if (atoi(result[1]) < 1) {
2313 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2314 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2315 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2317 EM_DEBUG_LOG("CREATE TABLE mail_account_tbl");
2318 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2319 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2320 if (error != EMAIL_ERROR_NONE) {
2321 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2325 /* create mail_account_tbl unique index */
2326 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ACCOUNT_IDX]);
2327 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2328 if (error != EMAIL_ERROR_NONE) {
2329 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2333 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2334 } /* mail_account_tbl */
2335 else if (type == EMAIL_CREATE_DB_CHECK) {
2336 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], NULL, NULL, NULL), rc);
2337 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)));
2340 sqlite3_free_table(result);
2343 /* 2. create mail_box_tbl */
2344 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_box_tbl';";
2346 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2347 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2348 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2350 if (atoi(result[1]) < 1) {
2351 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2352 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2353 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2355 EM_DEBUG_LOG("CREATE TABLE mail_box_tbl");
2357 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_BOX_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2358 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2359 if (error != EMAIL_ERROR_NONE) {
2360 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2364 /* create mail_local_mailbox_tbl unique index */
2365 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_BOX_IDX]);
2366 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2367 if (error != EMAIL_ERROR_NONE) {
2368 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2372 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2373 } /* mail_box_tbl */
2374 else if (type == EMAIL_CREATE_DB_CHECK) {
2375 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_BOX_TBL], NULL, NULL, NULL);
2376 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2377 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_BOX_TBL], rc, sqlite3_errmsg(local_db_handle)));
2379 sqlite3_free_table(result);
2382 /* 3. create mail_read_mail_uid_tbl */
2383 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_read_mail_uid_tbl';";
2384 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2385 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2386 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2389 if (atoi(result[1]) < 1) {
2390 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2391 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2392 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2394 EM_DEBUG_LOG("CREATE TABLE mail_read_mail_uid_tbl");
2396 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2397 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2398 if (error != EMAIL_ERROR_NONE) {
2399 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2403 /* create mail_read_mail_uid_tbl unique index */
2404 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_IDX]);
2405 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2406 if (error != EMAIL_ERROR_NONE) {
2407 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2411 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2412 } /* mail_read_mail_uid_tbl */
2413 else if (type == EMAIL_CREATE_DB_CHECK) {
2414 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], NULL, NULL, NULL), rc);
2415 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2416 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], rc, sqlite3_errmsg(local_db_handle)));
2418 sqlite3_free_table(result);
2421 /* 4. create mail_rule_tbl */
2422 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_rule_tbl';";
2424 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2425 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2426 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2428 if (atoi(result[1]) < 1) {
2429 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2430 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2431 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2433 EM_DEBUG_LOG("CREATE TABLE mail_rule_tbl");
2435 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_RULE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2436 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2437 if (error != EMAIL_ERROR_NONE) {
2438 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2442 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2443 } /* mail_rule_tbl */
2444 else if (type == EMAIL_CREATE_DB_CHECK) {
2445 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_RULE_TBL], NULL, NULL, NULL), rc);
2446 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2447 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_RULE_TBL], rc, sqlite3_errmsg(local_db_handle)));
2449 sqlite3_free_table(result);
2452 /* 5. create mail_tbl */
2453 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_tbl';";
2454 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2455 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2456 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2458 if (atoi(result[1]) < 1) {
2459 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2460 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2461 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2462 EM_DEBUG_LOG("CREATE TABLE mail_tbl");
2464 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2465 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2466 if (error != EMAIL_ERROR_NONE) {
2467 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2471 /* create mail_tbl unique index */
2472 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_IDX]);
2473 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2474 if (error != EMAIL_ERROR_NONE) {
2475 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2479 /* create mail_tbl index for date_time */
2480 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_DATETIME_IDX]);
2481 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2482 if (error != EMAIL_ERROR_NONE) {
2483 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2487 /* create mail_tbl index for thread_item_count */
2488 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_THREAD_IDX]);
2489 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2490 if (error != EMAIL_ERROR_NONE) {
2491 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2495 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2496 /* just one time call */
2497 /* EFTSInitFTSIndex(FTS_EMAIL_IDX); */
2499 else if (type == EMAIL_CREATE_DB_CHECK) {
2500 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TBL], NULL, NULL, NULL), rc);
2501 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2502 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TBL], rc, sqlite3_errmsg(local_db_handle)));
2504 sqlite3_free_table(result);
2507 /* 6. create mail_attachment_tbl */
2508 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_attachment_tbl';";
2509 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2510 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2511 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2513 if (atoi(result[1]) < 1) {
2514 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2515 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2516 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2518 EM_DEBUG_LOG("CREATE TABLE mail_attachment_tbl");
2520 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2521 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2522 if (error != EMAIL_ERROR_NONE) {
2523 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2527 /* create mail_attachment_tbl unique index */
2528 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_IDX]);
2529 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2530 if (error != EMAIL_ERROR_NONE) {
2531 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2535 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2536 } /* mail_attachment_tbl */
2537 else if (type == EMAIL_CREATE_DB_CHECK) {
2538 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], NULL, NULL, NULL), rc);
2539 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2540 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], rc, sqlite3_errmsg(local_db_handle)));
2542 sqlite3_free_table(result);
2545 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
2547 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_partial_body_activity_tbl';";
2548 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2549 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2550 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2552 if (atoi(result[1]) < 1) {
2554 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2555 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2556 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2558 EM_DEBUG_LOG("CREATE TABLE mail_partial_body_activity_tbl");
2560 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2561 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2562 if (error != EMAIL_ERROR_NONE) {
2563 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2567 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2568 } /* mail_rule_tbl */
2569 else if (type == EMAIL_CREATE_DB_CHECK) {
2570 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2571 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2572 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2574 sqlite3_free_table(result);
2577 #endif /* __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
2579 /* create mail_meeting_tbl */
2580 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_meeting_tbl';";
2581 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2582 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2583 ("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; },
2588 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2590 EM_DEBUG_LOG("CREATE TABLE mail_meeting_tbl");
2592 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_MEETING_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2593 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2594 if (error != EMAIL_ERROR_NONE) {
2595 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2599 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_MEETING_IDX]);
2600 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2601 if (error != EMAIL_ERROR_NONE) {
2602 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2606 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2607 } /* mail_contact_sync_tbl */
2608 else if (type == EMAIL_CREATE_DB_CHECK) {
2609 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], NULL, NULL, NULL), rc);
2610 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2611 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], rc, sqlite3_errmsg(local_db_handle)));
2613 sqlite3_free_table(result);
2615 /* LCOV_EXCL_STOP */
2616 #ifdef __FEATURE_LOCAL_ACTIVITY__
2618 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_local_activity_tbl';";
2619 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2620 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2621 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2623 if (atoi(result[1]) < 1) {
2625 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2626 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2627 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2629 EM_DEBUG_LOG(" CREATE TABLE mail_local_activity_tbl");
2631 SNPRINTF(sql_query_string, sizeof(sql_query_string), create_table_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL]);
2632 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2633 if (error != EMAIL_ERROR_NONE) {
2634 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2638 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2639 } /* mail_rule_tbl */
2640 else if (type == EMAIL_CREATE_DB_CHECK) {
2641 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL);
2642 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2643 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2644 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2646 sqlite3_free_table(result);
2648 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2649 /* create mail_certificate_tbl */
2650 /* LCOV_EXCL_START */
2651 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_certificate_tbl';";
2652 /* rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
2653 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2654 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)));
2656 if (atoi(result[1]) < 1) {
2657 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2658 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2660 EM_DEBUG_LOG("CREATE TABLE mail_certificate_tbl");
2662 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2663 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2664 if (error != EMAIL_ERROR_NONE) {
2665 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2669 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2670 } /* mail_contact_sync_tbl */
2671 else if (type == EMAIL_CREATE_DB_CHECK) {
2672 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], NULL, NULL, NULL), rc);
2673 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)));
2676 sqlite3_free_table(result);
2679 /* create mail_task_tbl */
2680 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_task_tbl';";
2681 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2682 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)));
2684 if (atoi(result[1]) < 1) {
2685 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2686 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2688 EM_DEBUG_LOG("CREATE TABLE mail_task_tbl");
2690 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TASK_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2691 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2692 if (error != EMAIL_ERROR_NONE) {
2693 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2697 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_TASK_IDX]);
2698 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2699 if (error != EMAIL_ERROR_NONE) {
2700 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2704 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2705 } /* mail_task_tbl */
2706 else if (type == EMAIL_CREATE_DB_CHECK) {
2707 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TASK_TBL], NULL, NULL, NULL), rc);
2708 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)));
2711 sqlite3_free_table(result);
2714 #ifdef __FEATURE_BODY_SEARCH__
2715 /* create mail_text_tbl */
2716 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_text_tbl';";
2717 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2718 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)));
2720 if (atoi(result[1]) < 1) {
2721 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2722 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2724 EM_DEBUG_LOG("CREATE TABLE mail_text_tbl");
2726 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TEXT_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2727 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2728 if (error != EMAIL_ERROR_NONE) {
2729 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2733 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2734 } /* mail_text_tbl */
2735 else if (type == EMAIL_CREATE_DB_CHECK) {
2736 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TEXT_TBL], NULL, NULL, NULL), rc);
2737 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)));
2740 sqlite3_free_table(result);
2745 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
2746 /* create mail_auto_download_activity_tbl */
2747 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_auto_download_activity_tbl';";
2748 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2749 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)));
2751 if (atoi(result[1]) < 1) {
2752 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2753 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2755 EM_DEBUG_LOG("CREATE TABLE mail_auto_download_activity_tbl");
2757 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], sizeof(sql_query_string)-1);
2758 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2759 if (error != EMAIL_ERROR_NONE) {
2760 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2764 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2765 } /* mail_auto_download_activity_tbl */
2766 else if (type == EMAIL_CREATE_DB_CHECK) {
2767 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2769 if(g_test_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL] != NULL) {
2770 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, \
2771 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], \
2772 rc, sqlite3_errmsg(local_db_handle)));
2776 sqlite3_free_table(result);
2785 if (result) sqlite3_free_table(result);
2788 EMSTORAGE_PROTECTED_FUNC_CALL(
2789 sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2791 EMSTORAGE_PROTECTED_FUNC_CALL(
2792 sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
2796 if (create_table_query) {
2798 for (i = 0; i < query_len; i++) {
2799 if (create_table_query[i])
2800 EM_SAFE_FREE(create_table_query[i]);
2802 EM_SAFE_FREE(create_table_query);
2805 if (err_code != NULL)
2808 EM_DEBUG_FUNC_END("ret [%d]", ret);
2812 /* Query series --------------------------------------------------------------*/
2813 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)
2815 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);
2818 int error = EMAIL_ERROR_NONE;
2819 DB_STMT hStmt = NULL;
2820 char *sql_query_string = NULL;
2822 sqlite3 *local_db_handle = NULL;
2824 if (!input_conditional_clause || (!output_total_mail_count && !output_unseen_mail_count)) {
2825 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2826 return EMAIL_ERROR_INVALID_PARAM;
2829 query_size = EM_SAFE_STRLEN(input_conditional_clause) + QUERY_SIZE;
2830 sql_query_string = em_malloc(query_size);
2831 if (sql_query_string == NULL) {
2832 EM_DEBUG_EXCEPTION("em_mallocfailed");
2833 error = EMAIL_ERROR_OUT_OF_MEMORY;
2837 local_db_handle = emstorage_get_db_connection(multi_user_name);
2839 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2841 SNPRINTF(sql_query_string, query_size, "SELECT COUNT(*) FROM mail_tbl");
2842 EM_SAFE_STRNCAT(sql_query_string, (char*)input_conditional_clause, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
2844 if (output_total_mail_count) {
2845 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
2846 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2847 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
2849 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2850 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2851 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
2852 _get_stmt_field_data_int(hStmt, output_total_mail_count, 0);
2855 if (output_unseen_mail_count) {
2856 EM_SAFE_STRNCAT(sql_query_string, " AND flags_seen_field = 0 ", query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
2858 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
2859 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2860 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2862 *output_unseen_mail_count = atoi(result[1]);
2863 sqlite3_free_table(result);
2868 if (hStmt != NULL) {
2869 rc = sqlite3_finalize(hStmt);
2870 if (rc != SQLITE_OK) {
2871 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
2872 error = EMAIL_ERROR_DB_FAILURE;
2876 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2878 EM_SAFE_FREE(sql_query_string);
2880 EM_DEBUG_FUNC_END("error [%d]", error);
2884 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)
2886 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);
2894 int error = EMAIL_ERROR_NONE;
2895 int *result_mail_id_list = NULL;
2896 char **result = NULL;
2897 char *sql_query_string = NULL;
2898 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2900 EM_IF_NULL_RETURN_VALUE(input_conditional_clause, EMAIL_ERROR_INVALID_PARAM);
2901 EM_IF_NULL_RETURN_VALUE(output_mail_id_list, EMAIL_ERROR_INVALID_PARAM);
2902 EM_IF_NULL_RETURN_VALUE(output_mail_id_count, EMAIL_ERROR_INVALID_PARAM);
2904 query_size = strlen(input_conditional_clause) + strlen("SELECT mail_id FROM mail_tbl ") + 10; // 10 is extra space
2905 sql_query_string = em_malloc(query_size);
2906 if (sql_query_string == NULL) {
2907 EM_DEBUG_EXCEPTION("em_mallocfailed");
2908 error = EMAIL_ERROR_OUT_OF_MEMORY;
2912 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2914 /* Composing query */
2915 SNPRINTF_OFFSET(sql_query_string, cur_query, query_size, "SELECT mail_id FROM mail_tbl ");
2916 EM_SAFE_STRNCAT(sql_query_string, (char*)input_conditional_clause, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
2918 EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
2920 /* Performing query */
2921 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2922 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2923 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2927 /* to get mail list */
2929 EM_DEBUG_LOG("No mail found...");
2930 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2934 EM_DEBUG_LOG("There are [%d] mails.", count);
2936 if (!(result_mail_id_list = (int*)em_malloc(sizeof(int) * count))) {
2937 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
2938 error = EMAIL_ERROR_OUT_OF_MEMORY;
2942 EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2944 for (i = 0; i < count; i++)
2945 _get_table_field_data_int(result, result_mail_id_list + i, col_index++);
2947 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >> ", count);
2949 *output_mail_id_list = result_mail_id_list;
2950 *output_mail_id_count = count;
2955 sqlite3_free_table(result);
2957 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2959 EM_SAFE_FREE(sql_query_string);
2961 if (error != EMAIL_ERROR_NONE)
2962 EM_SAFE_FREE(result_mail_id_list);
2964 EM_DEBUG_FUNC_END("error [%d]", error);
2968 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)
2970 EM_DEBUG_FUNC_BEGIN();
2971 EM_PROFILE_BEGIN(emstorage_query_mail_list_func);
2973 int i = 0, count = 0, rc = -1, to_get_count = (result_mail_list) ? 0 : 1;
2974 int sql_query_string_length = 0;
2975 int local_inline_content_count = 0, local_attachment_count = 0;
2976 int cur_query = 0, base_count = 0, col_index;
2977 int ret = false, error = EMAIL_ERROR_NONE;
2978 char *date_time_string = NULL;
2979 char **result = NULL;
2980 char *field_mail_id = "mail_id";
2981 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 ";
2982 char *select_query_form = "SELECT %s FROM mail_tbl ";
2983 char *target_field = NULL;
2984 char *sql_query_string = NULL;
2985 email_mail_list_item_t *mail_list_item_from_tbl = NULL;
2986 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2988 EM_IF_NULL_RETURN_VALUE(conditional_clause, false);
2989 EM_IF_NULL_RETURN_VALUE(result_count, false);
2991 EMSTORAGE_START_READ_TRANSACTION(transaction);
2994 if (to_get_count) /* count only */
2995 target_field = field_mail_id;
2996 else /* mail list in plain form */
2997 target_field = field_all;
2999 sql_query_string_length = EM_SAFE_STRLEN(select_query_form) + EM_SAFE_STRLEN(target_field) + EM_SAFE_STRLEN(conditional_clause);
3001 if (sql_query_string_length)
3002 sql_query_string = em_malloc(sql_query_string_length);
3004 if (sql_query_string == NULL) {
3005 EM_DEBUG_EXCEPTION("em_mallocfailed...");
3006 error = EMAIL_ERROR_OUT_OF_MEMORY;
3010 cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, sql_query_string_length, select_query_form, target_field);
3012 strncat(sql_query_string, conditional_clause, sql_query_string_length - cur_query);
3014 EM_DEBUG_LOG_DEV("query[%s]", sql_query_string);
3016 /* performing query */
3017 EM_PROFILE_BEGIN(emstorage_query_mail_list_performing_query);
3018 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3019 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3020 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
3021 EM_PROFILE_END(emstorage_query_mail_list_performing_query);
3027 for (tmp = field_all; tmp && *(tmp + 1); tmp = index(tmp + 1, ',')) i++ ;
3031 col_index = base_count;
3033 EM_DEBUG_LOG_DEV("base_count [%d]", base_count);
3038 EM_DEBUG_LOG_DEV("No mail found...");
3040 error = EMAIL_ERROR_MAIL_NOT_FOUND;
3043 EM_DEBUG_LOG_DEV("There are [%d] mails.", count);
3045 /* to get mail list */
3047 EM_DEBUG_LOG_DEV("No mail found...");
3049 error = EMAIL_ERROR_MAIL_NOT_FOUND;
3053 EM_DEBUG_LOG_DEV("There are [%d] mails.", count);
3054 if (!(mail_list_item_from_tbl = (email_mail_list_item_t*)em_malloc(sizeof(email_mail_list_item_t) * count))) {
3055 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
3056 error = EMAIL_ERROR_OUT_OF_MEMORY;
3060 EM_PROFILE_BEGIN(emstorage_query_mail_list_loop);
3061 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >> ");
3062 for (i = 0; i < count; i++) {
3063 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_id), col_index++);
3064 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].account_id), col_index++);
3065 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mailbox_id), col_index++);
3066 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].mailbox_type), col_index++);
3067 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].full_address_from, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
3068 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_sender, MAX_EMAIL_ADDRESS_LENGTH, 1, col_index++);
3069 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_recipient, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
3070 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].subject, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
3071 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].body_download_status), col_index++);
3072 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_size), col_index++);
3073 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_seen_field), col_index++);
3074 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_deleted_field), col_index++);
3075 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_flagged_field), col_index++);
3076 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_answered_field), col_index++);
3077 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_recent_field), col_index++);
3078 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_draft_field), col_index++);
3079 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_forwarded_field), col_index++);
3080 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].DRM_status), col_index++);
3081 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].priority), col_index++);
3082 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].save_status), col_index++);
3083 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].lock_status), col_index++);
3084 _get_table_field_data_int(result, &local_attachment_count, col_index++);
3085 _get_table_field_data_int(result, &local_inline_content_count, col_index++);
3086 _get_table_field_data_time_t(result, &(mail_list_item_from_tbl[i].date_time), col_index++);
3087 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].preview_text, MAX_PREVIEW_TEXT_LENGTH, 1, col_index++);
3088 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_id), col_index++);
3089 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_item_count), col_index++);
3090 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].meeting_request_status), col_index++);
3091 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].message_class), col_index++);
3092 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].smime_type), col_index++);
3093 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].scheduled_sending_time), col_index++);
3094 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].remaining_resend_times), col_index++);
3095 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].tag_id), col_index++);
3096 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].eas_data_length), col_index++);
3097 _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++);
3099 mail_list_item_from_tbl[i].attachment_count = (local_attachment_count > 0) ? 1 : 0;
3101 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >> ", count);
3102 EM_PROFILE_END(emstorage_query_mail_list_loop);
3108 EM_DEBUG_LOG("MAIL_COUNT [%d]", count);
3111 sqlite3_free_table(result);
3114 *result_count = count;
3117 if (result_mail_list)
3118 *result_mail_list = mail_list_item_from_tbl;
3120 EM_SAFE_FREE(mail_list_item_from_tbl);
3121 *result_count = count;
3123 EM_SAFE_FREE(mail_list_item_from_tbl);
3126 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3128 // sqlite3_db_release_memory(local_db_handle);
3130 EM_SAFE_FREE(sql_query_string);
3131 EM_SAFE_FREE(date_time_string);
3133 if (err_code != NULL)
3136 EM_PROFILE_END(emstorage_query_mail_list_func);
3137 EM_DEBUG_FUNC_END("ret [%d]", ret);
3141 /* LCOV_EXCL_STOP */
3142 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)
3144 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);
3146 if (!conditional_clause || !result_mail_tbl || !result_count) {
3147 /* LCOV_EXCL_START */
3148 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: [%p] [%p] [%p]", conditional_clause, result_mail_tbl, result_mail_tbl);
3149 if (err_code != NULL)
3150 *err_code = EMAIL_ERROR_INVALID_PARAM;
3152 /* LCOV_EXCL_STOP */
3155 int i, col_index = FIELD_COUNT_OF_MAIL_TBL, rc, ret = false, count;
3156 int error = EMAIL_ERROR_NONE;
3157 char **result = NULL, sql_query_string[QUERY_SIZE] = {0, };
3158 emstorage_mail_tbl_t* p_data_tbl = NULL;
3159 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3161 EMSTORAGE_START_READ_TRANSACTION(transaction);
3163 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl %s", conditional_clause);
3165 EM_DEBUG_LOG_DEV("Query[%s]", sql_query_string);
3167 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3168 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3169 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
3172 /* LCOV_EXCL_START */
3173 EM_DEBUG_LOG("No mail found...");
3175 error = EMAIL_ERROR_MAIL_NOT_FOUND;
3177 /* LCOV_EXCL_STOP */
3180 EM_DEBUG_LOG("There are [%d] mails.", count);
3181 if (!(p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * count))) {
3182 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
3183 error = EMAIL_ERROR_OUT_OF_MEMORY;
3187 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >> ");
3188 for (i = 0; i < count; i++) {
3189 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
3190 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3191 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3192 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_type), col_index++);
3193 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 1, col_index++);
3194 _get_table_field_data_time_t (result, &(p_data_tbl[i].date_time), col_index++);
3195 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
3196 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
3197 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
3198 _get_table_field_data_string(result, &(p_data_tbl[i].message_id), 0, col_index++);
3199 _get_table_field_data_int(result, &(p_data_tbl[i].reference_mail_id), col_index++);
3200 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_from), 1, col_index++);
3201 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_reply), 1, col_index++);
3202 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_to), 1, col_index++);
3203 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_cc), 1, col_index++);
3204 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_bcc), 1, col_index++);
3205 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_return), 1, col_index++);
3206 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_sender), 1, col_index++);
3207 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_recipient), 1, col_index++);
3208 _get_table_field_data_string(result, &(p_data_tbl[i].alias_sender), 1, col_index++);
3209 _get_table_field_data_string(result, &(p_data_tbl[i].alias_recipient), 1, col_index++);
3210 _get_table_field_data_int(result, &(p_data_tbl[i].body_download_status), col_index++);
3211 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
3212 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
3213 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
3214 _get_table_field_data_int(result, &(p_data_tbl[i].mail_size), col_index++);
3215 _get_table_field_data_char (result, &(p_data_tbl[i].flags_seen_field), col_index++);
3216 _get_table_field_data_char (result, &(p_data_tbl[i].flags_deleted_field), col_index++);
3217 _get_table_field_data_char (result, &(p_data_tbl[i].flags_flagged_field), col_index++);
3218 _get_table_field_data_char (result, &(p_data_tbl[i].flags_answered_field), col_index++);
3219 _get_table_field_data_char (result, &(p_data_tbl[i].flags_recent_field), col_index++);
3220 _get_table_field_data_char (result, &(p_data_tbl[i].flags_draft_field), col_index++);
3221 _get_table_field_data_char (result, &(p_data_tbl[i].flags_forwarded_field), col_index++);
3222 _get_table_field_data_int(result, &(p_data_tbl[i].DRM_status), col_index++);
3223 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].priority), col_index++);
3224 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
3225 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
3226 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].report_status), col_index++);
3227 _get_table_field_data_int(result, &(p_data_tbl[i].attachment_count), col_index++);
3228 _get_table_field_data_int(result, &(p_data_tbl[i].inline_content_count), col_index++);
3229 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
3230 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
3231 _get_table_field_data_string(result, &(p_data_tbl[i].preview_text), 1, col_index++);
3232 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].meeting_request_status), col_index++);
3233 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].message_class), col_index++);
3234 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].digest_type), col_index++);
3235 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].smime_type), col_index++);
3236 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].scheduled_sending_time), col_index++);
3237 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].remaining_resend_times), col_index++);
3238 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].tag_id), col_index++);
3239 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].replied_time), col_index++);
3240 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].forwarded_time), col_index++);
3241 _get_table_field_data_string(result, &(p_data_tbl[i].default_charset), 0, col_index++);
3242 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3243 _get_table_field_data_blob(result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3244 _get_table_field_data_string(result, &(p_data_tbl[i].user_name), 0, col_index++);
3251 sqlite3_free_table(result);
3254 *result_mail_tbl = p_data_tbl;
3255 *result_count = count;
3257 /* LCOV_EXCL_START */
3258 *result_mail_tbl = NULL;
3260 /* LCOV_EXCL_STOP */
3263 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3265 // sqlite3_db_release_memory(local_db_handle);
3267 if (err_code != NULL)
3270 EM_DEBUG_FUNC_END("ret [%d]", ret);
3274 #ifdef __FEATURE_BODY_SEARCH__
3275 /* LCOV_EXCL_START */
3276 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)
3278 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);
3280 if (!conditional_clause || !result_mail_text_tbl || !result_count) { /*prevent 50930*/
3281 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3282 if (err_code != NULL)
3283 *err_code = EMAIL_ERROR_INVALID_PARAM;
3291 int col_index = FIELD_COUNT_OF_MAIL_TEXT_TBL;
3292 int error = EMAIL_ERROR_NONE;
3293 char **result = NULL;
3294 char sql_query_string[QUERY_SIZE] = {0, };
3295 emstorage_mail_text_tbl_t* p_data_tbl = NULL;
3296 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3298 EMSTORAGE_START_READ_TRANSACTION(transaction);
3300 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_text_tbl %s", conditional_clause);
3302 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
3304 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3305 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3306 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3309 EM_DEBUG_LOG("No mail found...");
3311 error = EMAIL_ERROR_MAIL_NOT_FOUND;
3315 EM_DEBUG_LOG("There are [%d] mails.", count);
3316 if (!(p_data_tbl = (emstorage_mail_text_tbl_t *)em_malloc(sizeof(emstorage_mail_text_tbl_t) * count))) {
3317 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_text_tbl_t failed...");
3318 error = EMAIL_ERROR_OUT_OF_MEMORY;
3322 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
3323 for (i = 0; i < count; i++) {
3324 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
3325 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3326 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3327 _get_table_field_data_string(result, &(p_data_tbl[i].body_text), 0, col_index++);
3334 sqlite3_free_table(result);
3337 *result_mail_text_tbl = p_data_tbl;
3338 *result_count = count;
3340 EM_SAFE_FREE(p_data_tbl);
3342 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3344 // sqlite3_db_release_memory(local_db_handle);
3346 if (err_code != NULL)
3348 /* LCOV_EXCL_STOP */
3349 EM_DEBUG_FUNC_END("ret [%d]", ret);
3354 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)
3356 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);
3362 int error = EMAIL_ERROR_NONE;
3364 int total_count = 0;
3366 char sql_query_string[QUERY_SIZE] = {0, };
3367 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 ";
3368 emstorage_mailbox_tbl_t* p_data_tbl = NULL;
3369 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3371 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
3373 if (input_get_mail_count == 0) {
3374 /* LCOV_EXCL_START */
3375 /* without mail count */
3377 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);
3378 /* LCOV_EXCL_STOP */
3379 } else { /* with read count and total count */
3381 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);
3384 EM_DEBUG_LOG_DEV("query[%s]", sql_query_string);
3386 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
3387 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3388 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string))
3390 EM_DEBUG_LOG_DEV("result count [%d]", count);
3393 /* LCOV_EXCL_START */
3394 EM_DEBUG_LOG_SEC("Can't find mailbox query[%s]", sql_query_string);
3395 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
3397 /* LCOV_EXCL_STOP */
3400 if ((p_data_tbl = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t) * count)) == NULL) {
3401 /* LCOV_EXCL_START */
3402 EM_DEBUG_EXCEPTION("em_mallocfailed...");
3403 error = EMAIL_ERROR_OUT_OF_MEMORY;
3405 /* LCOV_EXCL_STOP */
3408 for (i = 0; i < count; i++) {
3409 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3410 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3411 _get_table_field_data_int(result, &(p_data_tbl[i].local_yn), col_index++);
3412 _get_table_field_data_string(result, &(p_data_tbl[i].mailbox_name), 0, col_index++);
3413 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].mailbox_type), col_index++);
3414 _get_table_field_data_string(result, &(p_data_tbl[i].alias), 0, col_index++);
3415 _get_table_field_data_int(result, &(p_data_tbl[i].deleted_flag), col_index++);
3416 _get_table_field_data_int(result, &(p_data_tbl[i].modifiable_yn), col_index++);
3417 _get_table_field_data_int(result, &(p_data_tbl[i].total_mail_count_on_server), col_index++);
3418 _get_table_field_data_int(result, &(p_data_tbl[i].has_archived_mails), col_index++);
3419 _get_table_field_data_int(result, &(p_data_tbl[i].mail_slot_size), col_index++);
3420 _get_table_field_data_int(result, &(p_data_tbl[i].no_select), col_index++);
3421 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].last_sync_time), col_index++);
3422 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3423 _get_table_field_data_blob(result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3425 if (input_get_mail_count == 1) {
3426 _get_table_field_data_int(result, &(total_count), col_index++);
3427 p_data_tbl[i].total_mail_count_on_local = total_count;
3428 _get_table_field_data_int(result, &(read_count), col_index++);
3429 p_data_tbl[i].unread_count = total_count - read_count; /* return unread count, NOT */
3436 sqlite3_free_table(result);
3438 if (error == EMAIL_ERROR_NONE) {
3439 *output_mailbox_list = p_data_tbl;
3440 *output_mailbox_count = count;
3441 EM_DEBUG_LOG("Mailbox Count [%d]", count);
3444 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
3446 // sqlite3_db_release_memory(local_db_handle);
3448 EM_DEBUG_FUNC_END("error [%d]", error);
3452 /* Query series --------------------------------------------------------------*/
3453 /* LCOV_EXCL_START */
3454 INTERNAL_FUNC int emstorage_check_duplicated_account(char *multi_user_name, email_account_t* account, int transaction, int *err_code)
3456 EM_DEBUG_FUNC_BEGIN();
3457 int rc = -1, ret = false;
3458 int error = EMAIL_ERROR_NONE;
3461 char sql_query_string[QUERY_SIZE] = {0, };
3462 sqlite3 *local_db_handle = NULL;
3464 if (account == NULL || account->user_email_address == NULL || account->incoming_server_user_name == NULL || account->incoming_server_address == NULL ||
3465 account->outgoing_server_user_name == NULL || account->outgoing_server_address == NULL) {
3466 if (err_code != NULL)
3467 *err_code = EMAIL_ERROR_INVALID_PARAM;
3471 local_db_handle = emstorage_get_db_connection(multi_user_name);
3473 EMSTORAGE_START_READ_TRANSACTION(transaction);
3475 SNPRINTF(sql_query_string, sizeof(sql_query_string),
3476 "SELECT COUNT(*) FROM mail_account_tbl "
3478 " user_email_address = '%s' AND "
3479 " incoming_server_user_name = '%s' AND "
3480 " incoming_server_type = %d AND "
3481 " incoming_server_address = '%s' AND "
3482 " outgoing_server_user_name = '%s' AND "
3483 " outgoing_server_type = %d AND "
3484 " outgoing_server_address = '%s'; ",
3485 account->user_email_address,
3486 account->incoming_server_user_name, account->incoming_server_type, account->incoming_server_address,
3487 account->outgoing_server_user_name, account->outgoing_server_type, account->outgoing_server_address
3489 EM_DEBUG_LOG_SEC("Query[%s]", sql_query_string);
3490 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
3491 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3492 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3494 count = atoi(result[1]);
3495 sqlite3_free_table(result);
3497 EM_DEBUG_LOG("Count of Duplicated Account Information: [%d]", count);
3499 if (count == 0) { /* not duplicated account */
3501 EM_DEBUG_LOG_SEC("NOT duplicated account: user_email_address[%s]", account->user_email_address);
3502 } else { /* duplicated account */
3504 EM_DEBUG_LOG_SEC("The same account already exists. Duplicated account: user_email_address[%s]", account->user_email_address);
3505 error = EMAIL_ERROR_ALREADY_EXISTS;
3510 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3512 if (err_code != NULL)
3515 EM_DEBUG_FUNC_END("ret [%d]", ret);
3520 INTERNAL_FUNC int emstorage_get_account_count(char *multi_user_name, int *count, int transaction, int *err_code)
3522 EM_DEBUG_FUNC_BEGIN();
3525 if (err_code != NULL)
3526 *err_code = EMAIL_ERROR_INVALID_PARAM;
3530 int rc = -1, ret = false;
3531 int error = EMAIL_ERROR_NONE;
3533 DB_STMT hStmt = NULL;
3534 char sql_query_string[QUERY_SIZE] = {0, };
3537 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3538 EMSTORAGE_START_READ_TRANSACTION(transaction);
3539 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_account_tbl");
3540 EM_DEBUG_LOG_SEC("SQL STMT [%s]", sql_query_string);
3541 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string,
3542 EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3543 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
3544 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3545 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3547 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3548 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3549 ("sqlite3_step fail:%d", rc));
3551 *count = sqlite3_column_int(hStmt, 0);
3557 if (hStmt != NULL) {
3558 rc = sqlite3_finalize(hStmt);
3560 if (rc != SQLITE_OK) {
3561 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
3562 error = EMAIL_ERROR_DB_FAILURE;
3566 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3568 if (err_code != NULL)
3571 EM_DEBUG_FUNC_END("ret [%d]", ret);
3575 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)
3577 EM_DEBUG_FUNC_BEGIN();
3579 int i = 0, count = 0, rc = -1, ret = false;
3580 int field_index = 0;
3582 int error = EMAIL_ERROR_NONE;
3583 emstorage_account_tbl_t *p_data_tbl = NULL;
3584 char sql_query_string[QUERY_SIZE] = {0, };
3585 char *sql = "SELECT count(*) FROM mail_account_tbl;";
3587 sqlite3 *local_db_handle = NULL;
3588 DB_STMT hStmt = NULL;
3590 if (!select_num || !account_list) {
3591 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, account_list);
3592 if (err_code != NULL)
3593 *err_code = EMAIL_ERROR_INVALID_PARAM;
3597 local_db_handle = emstorage_get_db_connection(multi_user_name);
3599 EMSTORAGE_START_READ_TRANSACTION(transaction);
3601 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
3602 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3603 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
3605 count = atoi(result[1]);
3606 sqlite3_free_table(result);
3608 EM_DEBUG_LOG_DEV("count = %d", rc);
3611 EM_DEBUG_LOG("no account found...");
3612 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3616 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
3617 sql_len = EM_SAFE_STRLEN(sql_query_string);
3619 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
3624 "user_data_length, "
3628 "default_mail_slot_size, "
3631 "user_display_name, "
3632 "user_email_address, "
3633 "reply_to_address, "
3635 "incoming_server_type, "
3636 "incoming_server_address, "
3637 "incoming_server_port_number, "
3638 "incoming_server_user_name, "
3639 "incoming_server_password, "
3640 "incoming_server_secure_connection, "
3641 "incoming_server_authentication_method, "
3643 "keep_mails_on_pop_server_after_download, "
3645 "auto_download_size, "
3650 "outgoing_server_type, "
3651 "outgoing_server_address, "
3652 "outgoing_server_port_number, "
3653 "outgoing_server_user_name, "
3654 "outgoing_server_password, "
3655 "outgoing_server_secure_connection, "
3656 "outgoing_server_need_authentication, "
3657 "outgoing_server_use_same_authenticator, "
3660 "req_delivery_receipt, "
3661 "req_read_receipt, "
3665 "display_name_from, "
3667 "forward_with_files, "
3671 "add_my_address_to_bcc, "
3672 "notification_status, "
3674 "display_content_status, "
3675 "default_ringtone_status, "
3676 "alert_ringtone_path, "
3677 "auto_resend_times, "
3678 "outgoing_server_size_limit, "
3679 "wifi_auto_download, "
3681 "incoming_server_requires_apop, "
3683 "certificate_path, "
3688 sql_len = EM_SAFE_STRLEN(sql_query_string);
3690 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " FROM mail_account_tbl ORDER BY account_id");
3692 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3694 EM_DEBUG_LOG_DEV("After sqlite3_prepare_v2 hStmt = %p", hStmt);
3695 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3696 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3698 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3699 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3700 ("sqlite3_step fail:%d", rc));
3701 if (rc == SQLITE_DONE) {
3702 EM_DEBUG_EXCEPTION("no account found...");
3704 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3710 if (!(p_data_tbl = (emstorage_account_tbl_t*)malloc(sizeof(emstorage_account_tbl_t) * count))) {
3711 EM_DEBUG_EXCEPTION("malloc failed...");
3712 error = EMAIL_ERROR_OUT_OF_MEMORY;
3715 memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * count);
3716 for (i = 0; i < count; i++) {
3719 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_id), field_index++);
3720 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].account_name), 0, field_index++);
3721 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].logo_icon_path), 0, field_index++);
3722 _get_stmt_field_data_blob(hStmt, &(p_data_tbl[i].user_data), field_index++);
3723 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].user_data_length), field_index++);
3724 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_svc_id), field_index++);
3725 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_status), field_index++);
3726 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_disabled), field_index++);
3727 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].default_mail_slot_size), field_index++);
3728 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].roaming_option), field_index++);
3729 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].color_label), field_index++);
3730 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_display_name), 0, field_index++);
3731 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_email_address), 0, field_index++);
3732 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].reply_to_address), 0, field_index++);
3733 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].return_address), 0, field_index++);
3734 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].incoming_server_type), field_index++);
3735 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_address), 0, field_index++);
3736 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_port_number), field_index++);
3737 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_user_name), 0, field_index++);
3738 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_password), 0, field_index++);
3739 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_secure_connection), field_index++);
3740 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].incoming_server_authentication_method), field_index++);
3741 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].retrieval_mode), field_index++);
3742 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].keep_mails_on_pop_server_after_download), field_index++);
3743 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].check_interval), field_index++);
3744 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_download_size), field_index++);
3745 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_interval), field_index++);
3746 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_days), field_index++);
3747 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_start_time), field_index++);
3748 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_end_time), field_index++);
3749 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].outgoing_server_type), field_index++);
3750 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_address), 0, field_index++);
3751 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_port_number), field_index++);
3752 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_user_name), 0, field_index++);
3753 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_password), 0, field_index++);
3754 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_secure_connection), field_index++);
3755 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_need_authentication), field_index++);
3756 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_use_same_authenticator), field_index++);
3757 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.priority), field_index++);
3758 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.keep_local_copy), field_index++);
3759 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_delivery_receipt), field_index++);
3760 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_read_receipt), field_index++);
3761 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.download_limit), field_index++);
3762 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_address), field_index++);
3763 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_subject), field_index++);
3764 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.display_name_from), 0, field_index++);
3765 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.reply_with_body), field_index++);
3766 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.forward_with_files), field_index++);
3767 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_myname_card), field_index++);
3768 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_signature), field_index++);
3769 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.signature), 0, field_index++);
3770 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.add_my_address_to_bcc), field_index++);
3771 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.notification_status), field_index++);
3772 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.vibrate_status), field_index++);
3773 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.display_content_status), field_index++);
3774 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.default_ringtone_status), field_index++);
3775 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.alert_ringtone_path), 0, field_index++);
3776 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_resend_times), field_index++);
3777 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_size_limit), field_index++);
3778 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].wifi_auto_download), field_index++);
3779 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].pop_before_smtp), field_index++);
3780 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_requires_apop), field_index++);
3781 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].smime_type), field_index++);
3782 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].certificate_path), 0, field_index++);
3783 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].cipher_type), field_index++);
3784 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].digest_type), field_index++);
3786 /* EAS passwd is not accessible */
3787 if (with_password == true && p_data_tbl[i].incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
3788 /* get password from the secure storage */
3789 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3790 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3792 EM_SAFE_FREE(p_data_tbl[i].incoming_server_password);
3793 EM_SAFE_FREE(p_data_tbl[i].outgoing_server_password);
3795 /* get password file name */
3796 error = _get_password_file_name(multi_user_name, p_data_tbl[i].account_id,
3797 recv_password_file_name,
3798 send_password_file_name);
3799 if (error != EMAIL_ERROR_NONE) {
3800 EM_DEBUG_EXCEPTION("_get_password_file_name error [%d]", error);
3801 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3805 /* read password from secure storage */
3806 error = _read_password_from_secure_storage(recv_password_file_name,
3807 &(p_data_tbl[i].incoming_server_password));
3809 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s] error [%d]",
3810 recv_password_file_name, error);
3811 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3814 error = _read_password_from_secure_storage(send_password_file_name,
3815 &(p_data_tbl[i].outgoing_server_password));
3817 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s] error [%d]",
3818 send_password_file_name, error);
3819 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3824 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3825 EM_DEBUG_LOG_DEV("after sqlite3_step(), i = %d, rc = %d.", i, rc);
3826 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3827 ("sqlite3_step fail:%d", rc));
3834 *account_list = p_data_tbl;
3835 *select_num = count;
3836 EM_DEBUG_LOG_DEV("COUNT : %d", count);
3837 } else if (p_data_tbl != NULL)
3838 emstorage_free_account(&p_data_tbl, count, NULL);
3840 if (hStmt != NULL) {
3841 rc = sqlite3_finalize(hStmt);
3843 if (rc != SQLITE_OK) {
3844 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
3845 error = EMAIL_ERROR_DB_FAILURE;
3849 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3851 if (err_code != NULL)
3854 EM_DEBUG_FUNC_END("ret [%d]", ret);
3858 INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(char *multi_user_name,
3859 char *server_mail_id,
3861 emstorage_mail_tbl_t **mail,
3865 EM_DEBUG_FUNC_BEGIN_SEC("mailbox_id [%d], server_mail_id[%s], mail[%p], transaction[%d], err_code[%p]",
3866 mailbox_id, server_mail_id, mail, transaction, err_code);
3868 int ret = false, error = EMAIL_ERROR_NONE, result_count;
3869 char conditional_clause[QUERY_SIZE] = {0, };
3870 emstorage_mail_tbl_t* p_data_tbl = NULL;
3872 if (!server_mail_id || !mail) {
3873 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3874 error = EMAIL_ERROR_INVALID_PARAM;
3878 sqlite3_snprintf(sizeof(conditional_clause), conditional_clause, "WHERE server_mail_id = '%q'", server_mail_id);
3880 if (mailbox_id > 0) {
3881 SNPRINTF(conditional_clause + strlen(conditional_clause),
3882 QUERY_SIZE - strlen(conditional_clause),
3883 " AND mailbox_id = %d",
3887 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
3888 if (!emstorage_query_mail_tbl(multi_user_name,
3894 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3905 if (err_code != NULL)
3908 EM_DEBUG_FUNC_END("ret [%d]", ret);
3913 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)
3915 int cur_clause = 0, conditional_clause_count = 0, i = 0;
3917 if (account_id < ALL_ACCOUNT) {
3918 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3919 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3923 if (account_id != ALL_ACCOUNT) {
3924 cur_clause += (conditional_clause_count++ == 0) ?
3925 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE account_id = %d", account_id) :
3926 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND account_id = %d", account_id);
3929 if (mailbox_id > 0) {
3930 cur_clause += (conditional_clause_count++ == 0) ?
3931 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_id = %d", mailbox_id) :
3932 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_id = %d", mailbox_id);
3935 if (thread_id > 0) {
3936 cur_clause += (conditional_clause_count++ == 0) ?
3937 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_id = %d ", thread_id) :
3938 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_id = %d ", thread_id);
3939 } else if (thread_id == EMAIL_LIST_TYPE_THREAD) {
3940 cur_clause += (conditional_clause_count++ == 0) ?
3941 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_item_count > 0") :
3942 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_item_count > 0");
3943 } else if (thread_id == EMAIL_LIST_TYPE_LOCAL) {
3944 cur_clause += (conditional_clause_count++ == 0) ?
3945 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE server_mail_status = 0") :
3946 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND server_mail_status = 0");
3947 } else if (thread_id == EMAIL_LIST_TYPE_UNREAD) {
3948 cur_clause += (conditional_clause_count++ == 0) ?
3949 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_seen_field == 0") :
3950 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_seen_field == 0");
3953 /* EM_DEBUG_LOG("where clause added [%s]", conditional_clause_string); */
3954 if (addr_list && addr_list->address_count > 0) {
3955 if (!addr_list->address_type) {
3956 cur_clause += (conditional_clause_count++ == 0) ?
3957 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE email_address_sender IN(\"%s\"", addr_list->address_list[0]) :
3958 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND email_address_sender IN(\"%s\"", addr_list->address_list[0]);
3960 for (i = 1; i < addr_list->address_count; i++)
3961 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3963 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3965 cur_clause += (conditional_clause_count++ == 0) ?
3966 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]) :
3967 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]);
3969 for (i = 1; i < addr_list->address_count; i++)
3970 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3972 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3976 if (input_except_delete_flagged_mails) {
3977 cur_clause += (conditional_clause_count++ == 0) ?
3978 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_deleted_field = 0") :
3979 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_deleted_field = 0");
3983 switch (search_type) {
3984 case EMAIL_SEARCH_FILTER_SUBJECT:
3985 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3986 " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3988 case EMAIL_SEARCH_FILTER_SENDER:
3989 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3990 " %s ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3991 ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3993 case EMAIL_SEARCH_FILTER_RECIPIENT:
3994 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3995 " %s ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3996 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3997 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3998 ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value);
4000 case EMAIL_SEARCH_FILTER_ALL:
4001 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
4002 " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\' "
4003 " OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
4004 " OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
4005 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
4006 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
4009 ")", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value, search_value, search_value);
4014 /* EM_DEBUG_LOG("where clause [%s]", conditional_clause_string); */
4015 static char sorting_str[][50] = {
4016 " ORDER BY date_time DESC", /* case EMAIL_SORT_DATETIME_HIGH: */
4017 " ORDER BY date_time ASC", /* case EMAIL_SORT_DATETIME_LOW: */
4018 " ORDER BY full_address_from DESC, date_time DESC", /* case EMAIL_SORT_SENDER_HIGH: */
4019 " ORDER BY full_address_from ASC, date_time DESC", /* case EMAIL_SORT_SENDER_LOW: */
4020 " ORDER BY full_address_to DESC, date_time DESC", /* case EMAIL_SORT_RCPT_HIGH: */
4021 " ORDER BY full_address_to ASC, date_time DESC", /* case EMAIL_SORT_RCPT_LOW: */
4022 " ORDER BY subject DESC, date_time DESC", /* case EMAIL_SORT_SUBJECT_HIGH: */
4023 " ORDER BY subject ASC, date_time DESC", /* case EMAIL_SORT_SUBJECT_LOW: */
4024 " ORDER BY priority DESC, date_time DESC", /* case EMAIL_SORT_PRIORITY_HIGH: */
4025 " ORDER BY priority ASC, date_time DESC", /* case EMAIL_SORT_PRIORITY_LOW: */
4026 " ORDER BY attachment_count DESC, date_time DESC", /* case EMAIL_SORT_ATTACHMENT_HIGH: */
4027 " ORDER BY attachment_count ASC, date_time DESC", /* case EMAIL_SORT_ATTACHMENT_LOW: */
4028 " ORDER BY lock_status DESC, date_time DESC", /* case EMAIL_SORT_FAVORITE_HIGH: */
4029 " ORDER BY lock_status ASC, date_time DESC", /* case EMAIL_SORT_FAVORITE_LOW: */
4030 " ORDER BY mailbox_id DESC, date_time DESC", /* case EMAIL_SORT_MAILBOX_ID_HIGH: */
4031 " ORDER BY mailbox_id ASC, date_time DESC", /* case EMAIL_SORT_MAILBOX_ID_LOW: */
4032 " ORDER BY flags_flagged_field DESC, date_time DESC", /* case EMAIL_SORT_FLAGGED_FLAG_HIGH: */
4033 " ORDER BY flags_flagged_field ASC, date_time DESC", /* case EMAIL_SORT_FLAGGED_FLAG_LOW: */
4034 " ORDER BY flags_seen_field DESC, date_time DESC", /* case EMAIL_SORT_SEEN_FLAG_HIGH: */
4035 " ORDER BY flags_seen_field ASC, date_time DESC" /* case EMAIL_SORT_SEEN_FLAG_LOW: */
4038 if (sorting < EMAIL_SORT_END)
4039 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " %s", sorting_str[sorting]);
4041 EM_DEBUG_LOG(" Invalid Sorting order ");
4044 if (start_index != -1 && limit_count != -1)
4045 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " LIMIT %d, %d", start_index, limit_count);
4052 * emstorage_get_mail_list - Get the mail list information.
4056 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)
4058 EM_DEBUG_FUNC_BEGIN();
4059 EM_PROFILE_BEGIN(emstorage_get_mail_list_func);
4061 int ret = false, error = EMAIL_ERROR_NONE;
4062 char conditional_clause_string[QUERY_SIZE] = { 0, };
4064 if (account_id < ALL_ACCOUNT) {
4065 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
4066 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
4068 EM_IF_NULL_RETURN_VALUE(result_count, false);
4070 _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);
4072 EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
4074 if (!emstorage_query_mail_list(multi_user_name, conditional_clause_string, transaction, mail_list, result_count, &error)) {
4075 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
4082 if (err_code != NULL)
4085 EM_PROFILE_END(emstorage_get_mail_list_func);
4086 EM_DEBUG_FUNC_END("ret [%d]", ret);
4092 * emstorage_get_mails - Get the Mail list information based on mailbox_name name
4096 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)
4098 EM_DEBUG_FUNC_BEGIN();
4099 EM_PROFILE_BEGIN(emStorageGetMails);
4101 int count = 0, ret = false, error = EMAIL_ERROR_NONE;
4102 emstorage_mail_tbl_t *p_data_tbl = NULL;
4103 char conditional_clause_string[QUERY_SIZE] = {0, };
4105 EM_IF_NULL_RETURN_VALUE(mail_list, false);
4106 EM_IF_NULL_RETURN_VALUE(result_count, false);
4108 if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
4109 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4110 if (err_code != NULL)
4111 *err_code = EMAIL_ERROR_INVALID_PARAM;
4115 _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);
4117 EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
4119 if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause_string, transaction, &p_data_tbl, &count, &error)) {
4120 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
4129 *mail_list = p_data_tbl;
4130 *result_count = count;
4131 EM_DEBUG_LOG("COUNT : %d", count);
4132 } else if (p_data_tbl != NULL)
4133 emstorage_free_mail(&p_data_tbl, count, NULL);
4135 if (err_code != NULL)
4138 EM_PROFILE_END(emStorageGetMails);
4139 EM_DEBUG_FUNC_END("ret [%d]", ret);
4146 * emstorage_get_searched_mail_list - Get the mail list information after filtering
4150 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)
4152 EM_DEBUG_FUNC_BEGIN();
4154 int ret = false, error = EMAIL_ERROR_NONE;
4155 char conditional_clause[QUERY_SIZE] = {0, };
4156 char *temp_search_value = NULL;
4157 char *temp_search_value2 = NULL;
4159 EM_IF_NULL_RETURN_VALUE(mail_list, false);
4160 EM_IF_NULL_RETURN_VALUE(result_count, false);
4162 if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
4163 EM_DEBUG_EXCEPTION("select_num[%p], Mail_list[%p]", result_count, mail_list);
4164 error = EMAIL_ERROR_INVALID_PARAM;
4168 temp_search_value = em_replace_all_string((char*)search_value, "_", "\\_");
4169 temp_search_value2 = em_replace_all_string(temp_search_value, "%", "\\%");
4171 _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);
4173 EM_DEBUG_LOG("conditional_clause[%s]", conditional_clause);
4175 if (!emstorage_query_mail_list(multi_user_name, conditional_clause, transaction, mail_list, result_count, &error)) {
4176 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
4183 EM_DEBUG_LOG("emstorage_get_searched_mail_list finish off");
4185 if (err_code != NULL)
4188 EM_SAFE_FREE(temp_search_value);
4189 EM_SAFE_FREE(temp_search_value2);
4191 EM_DEBUG_FUNC_END("ret [%d]", ret);
4196 static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name)
4198 EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
4200 if (account_id <= 0 || !recv_password_file_name || !send_password_file_name) {
4201 EM_DEBUG_EXCEPTION("Invalid parameter");
4202 return EMAIL_ERROR_INVALID_PARAM;
4205 EM_DEBUG_LOG("MULTI_USER_NAME : [%s]", multi_user_name);
4207 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
4208 snprintf(recv_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_recv_%s", account_id, multi_user_name);
4209 snprintf(send_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_send_%s", account_id, multi_user_name);
4211 snprintf(recv_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_recv", account_id);
4212 snprintf(send_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_send", account_id);
4215 EM_DEBUG_FUNC_END();
4216 return EMAIL_ERROR_NONE;
4219 static int _read_password_from_secure_storage(char *file_name, char **password)
4221 EM_DEBUG_FUNC_BEGIN_SEC("file_name[%s]", file_name);
4223 if (!file_name || !password) {
4224 EM_DEBUG_EXCEPTION("Invalid Parameter");
4225 return EMAIL_ERROR_INVALID_PARAM;
4228 int ret = EMAIL_ERROR_NONE;
4230 ret = emcore_get_password_in_key_manager(file_name, password);
4231 if (ret != EMAIL_ERROR_NONE) {
4232 EM_DEBUG_EXCEPTION("emcore_get_password_in_key_manager failed : [%d]", ret);
4238 EM_DEBUG_FUNC_END("ret [%d]", ret);
4242 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)
4244 EM_DEBUG_FUNC_BEGIN("account_id[%d], pulloption[%d], account[%p], transaction[%d], err_code[%p]", account_id, pulloption, account, transaction, err_code);
4247 /* LCOV_EXCL_START */
4248 EM_DEBUG_EXCEPTION("account_id[%d], account[%p]", account_id, account);
4249 if (err_code != NULL)
4250 *err_code = EMAIL_ERROR_INVALID_PARAM;
4252 /* LCOV_EXCL_STOP */
4256 int error = EMAIL_ERROR_NONE;
4257 emstorage_account_tbl_t* p_data_tbl = NULL;
4258 DB_STMT hStmt = NULL;
4259 char sql_query_string[QUERY_SIZE] = {0, };
4262 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4263 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4265 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4267 EMSTORAGE_START_READ_TRANSACTION(transaction);
4269 /* Make query string */
4270 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
4271 sql_len = EM_SAFE_STRLEN(sql_query_string);
4273 if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4274 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4275 "incoming_server_type,"
4276 "incoming_server_address,"
4277 "user_email_address,"
4278 "incoming_server_user_name,"
4280 "incoming_server_port_number,"
4281 "incoming_server_secure_connection,"
4282 "incoming_server_authentication_method,"
4283 "outgoing_server_type,"
4284 "outgoing_server_address,"
4285 "outgoing_server_port_number,"
4286 "outgoing_server_need_authentication,"
4287 "outgoing_server_secure_connection,"
4288 "outgoing_server_user_name,"
4289 "user_display_name,"
4293 "keep_mails_on_pop_server_after_download,"
4294 "auto_download_size,"
4299 "outgoing_server_use_same_authenticator,"
4300 "auto_resend_times,"
4301 "outgoing_server_size_limit,"
4302 "wifi_auto_download,"
4304 "incoming_server_requires_apop,"
4311 sql_len = EM_SAFE_STRLEN(sql_query_string);
4314 if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME) {
4315 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " account_name, ");
4316 sql_len = EM_SAFE_STRLEN(sql_query_string);
4319 /* get from secure storage, not from db */
4320 if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4321 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4324 "req_delivery_receipt,"
4329 "display_name_from,"
4331 "forward_with_files,"
4335 "add_my_address_to_bcc,"
4336 "notification_status,"
4338 "display_content_status,"
4339 "default_ringtone_status,"
4340 "alert_ringtone_path,"
4343 "default_mail_slot_size,"
4350 sql_len = EM_SAFE_STRLEN(sql_query_string);
4353 /* dummy value, FROM WHERE clause */
4354 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "0 FROM mail_account_tbl WHERE account_id = %d", account_id);
4357 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
4359 /* execute a sql and count rows */
4360 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4361 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4362 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4364 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4365 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4366 ("sqlite3_step fail:%d", rc));
4368 if (rc == SQLITE_DONE) {
4369 EM_DEBUG_EXCEPTION("no matched account found...");
4370 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4374 /* Assign query result to structure */
4375 if (!(p_data_tbl = (emstorage_account_tbl_t *)malloc(sizeof(emstorage_account_tbl_t) * 1))) {
4376 EM_DEBUG_EXCEPTION("malloc failed...");
4377 error = EMAIL_ERROR_OUT_OF_MEMORY;
4381 memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * 1);
4384 if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4385 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_type), col_index++);
4386 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_address), 0, col_index++);
4387 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_email_address), 0, col_index++);
4388 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_user_name), 0, col_index++);
4389 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->retrieval_mode), col_index++);
4390 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_port_number), col_index++);
4391 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_secure_connection), col_index++);
4392 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_authentication_method), col_index++);
4393 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->outgoing_server_type), col_index++);
4394 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_address), 0, col_index++);
4395 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_port_number), col_index++);
4396 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_need_authentication), col_index++);
4397 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_secure_connection), col_index++);
4398 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_user_name), 0, col_index++);
4399 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_display_name), 0, col_index++);
4400 _get_stmt_field_data_string(hStmt, &(p_data_tbl->reply_to_address), 0, col_index++);
4401 _get_stmt_field_data_string(hStmt, &(p_data_tbl->return_address), 0, col_index++);
4402 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
4403 _get_stmt_field_data_int(hStmt, &(p_data_tbl->keep_mails_on_pop_server_after_download), col_index++);
4404 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_download_size), col_index++);
4405 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_interval), col_index++);
4406 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_days), col_index++);
4407 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_start_time), col_index++);
4408 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_end_time), col_index++);
4409 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_use_same_authenticator), col_index++);
4410 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_resend_times), col_index++);
4411 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_size_limit), col_index++);
4412 _get_stmt_field_data_int(hStmt, &(p_data_tbl->wifi_auto_download), col_index++);
4413 _get_stmt_field_data_int(hStmt, &(p_data_tbl->pop_before_smtp), col_index++);
4414 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_requires_apop), col_index++);
4415 _get_stmt_field_data_string(hStmt, &(p_data_tbl->logo_icon_path), 0, col_index++);
4416 _get_stmt_field_data_blob(hStmt, &p_data_tbl->user_data, col_index++);
4417 _get_stmt_field_data_int(hStmt, &p_data_tbl->user_data_length, col_index++);
4418 _get_stmt_field_data_int(hStmt, &p_data_tbl->color_label, col_index++);
4419 _get_stmt_field_data_int(hStmt, &(p_data_tbl->check_interval), col_index++);
4420 _get_stmt_field_data_int(hStmt, &(p_data_tbl->sync_status), col_index++);
4423 if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME)
4424 _get_stmt_field_data_string(hStmt, &(p_data_tbl->account_name), 0, col_index++);
4426 if (pulloption & EMAIL_ACC_GET_OPT_PASSWORD) {
4427 /* get password file name */
4428 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) {
4429 EM_DEBUG_EXCEPTION("_get_password_file_name failed [%d]", error);
4433 /* read password from secure storage */
4434 if ((error = _read_password_from_secure_storage(recv_password_file_name, &(p_data_tbl->incoming_server_password))) < 0) {
4435 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4439 EM_DEBUG_LOG_DEV("recv_password_file_name[%s], password[%s]", recv_password_file_name, p_data_tbl->incoming_server_password);
4441 if (p_data_tbl->outgoing_server_use_same_authenticator == 0) {
4442 if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl->outgoing_server_password))) < 0) {
4443 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4446 EM_DEBUG_LOG_DEV("send_password_file_name[%s], password[%s]", send_password_file_name, p_data_tbl->outgoing_server_password);
4450 if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4451 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.priority), col_index++);
4452 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.keep_local_copy), col_index++);
4453 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_delivery_receipt), col_index++);
4454 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_read_receipt), col_index++);
4455 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.download_limit), col_index++);
4456 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_address), col_index++);
4457 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_subject), col_index++);
4458 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.display_name_from), 0, col_index++);
4459 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.reply_with_body), col_index++);
4460 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.forward_with_files), col_index++);
4461 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_myname_card), col_index++);
4462 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_signature), col_index++);
4463 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.signature), 0, col_index++);
4464 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.add_my_address_to_bcc), col_index++);
4465 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.notification_status), col_index++);
4466 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.vibrate_status), col_index++);
4467 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.display_content_status), col_index++);
4468 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.default_ringtone_status), col_index++);
4469 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.alert_ringtone_path), 0, col_index++);
4470 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_svc_id), col_index++);
4471 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->sync_disabled), col_index++);
4472 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->default_mail_slot_size), col_index++);
4473 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->roaming_option), col_index++);
4474 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->smime_type), col_index++);
4475 _get_stmt_field_data_string(hStmt, &(p_data_tbl->certificate_path), 0, col_index++);
4476 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->cipher_type), col_index++);
4477 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->digest_type), col_index++);
4484 *account = p_data_tbl;
4487 emstorage_free_account((emstorage_account_tbl_t **)&p_data_tbl, 1, NULL);
4489 if (hStmt != NULL) {
4490 rc = sqlite3_finalize(hStmt);
4491 if (rc != SQLITE_OK) {
4492 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
4493 error = EMAIL_ERROR_DB_FAILURE;
4497 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4499 if (err_code != NULL)
4502 EM_DEBUG_FUNC_END("ret [%d]", ret);
4505 /* LCOV_EXCL_START */
4506 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)
4508 EM_DEBUG_FUNC_BEGIN("account_id[%d], password_length[%p], err_code[%p]", account_id, password_length, err_code);
4510 if (account_id <= 0 || password_length == NULL) {
4511 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4512 if (err_code != NULL)
4513 *err_code = EMAIL_ERROR_INVALID_PARAM;
4518 int error = EMAIL_ERROR_NONE;
4519 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4520 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4521 char *temp_password = NULL;
4524 /* get password file name */
4525 if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4526 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4530 /* read password from secure storage */
4531 if (password_type == EMAIL_GET_INCOMING_PASSWORD_LENGTH) {
4532 if ((error = _read_password_from_secure_storage(recv_password_file_name, &temp_password)) < 0 || !temp_password) {
4533 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
4536 } else if (password_type == EMAIL_GET_OUTGOING_PASSWORD_LENGTH) {
4537 if ((error = _read_password_from_secure_storage(send_password_file_name, &temp_password)) < 0 || !temp_password) {
4538 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
4542 EM_DEBUG_LOG("Invalid password type");
4543 if (err_code != NULL)
4544 *err_code = EMAIL_ERROR_INVALID_PARAM;
4548 *password_length = EM_SAFE_STRLEN(temp_password);
4550 EM_DEBUG_LOG_SEC("recv_password_file_name[%s], *password_length[%d]", recv_password_file_name, *password_length);
4555 EM_SAFE_FREE(temp_password);
4557 if (err_code != NULL)
4560 EM_DEBUG_FUNC_END("ret [%d]", ret);
4564 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)
4566 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);
4568 int err = EMAIL_ERROR_NONE;
4569 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4570 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4572 if (input_incoming_server_password == NULL && input_outgoing_server_password == NULL) {
4573 EM_DEBUG_EXCEPTION_SEC("Invalid param");
4574 err = EMAIL_ERROR_INVALID_PARAM;
4578 /* get password file name */
4579 if ((err = _get_password_file_name(multi_user_name,
4581 recv_password_file_name,
4582 send_password_file_name)) != EMAIL_ERROR_NONE) {
4583 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4587 if (input_incoming_server_password) {
4588 EM_DEBUG_LOG_SEC("recv_password_file_name [%s] input_incoming_server_password [%s]",
4589 recv_password_file_name, input_incoming_server_password);
4591 if (input_outgoing_server_password) {
4592 EM_DEBUG_LOG_SEC("send_password_file_name [%s] input_outgoing_server_password [%s]",
4593 send_password_file_name, input_outgoing_server_password);
4596 if (input_incoming_server_password) {
4597 err = emcore_remove_password_in_key_manager(recv_password_file_name);
4598 if (err != EMAIL_ERROR_NONE) {
4599 EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", recv_password_file_name);
4603 /* save recv passwords to the secure storage */
4604 err = emcore_add_password_in_key_manager(recv_password_file_name, input_incoming_server_password);
4605 if (err != EMAIL_ERROR_NONE) {
4606 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4611 if (input_outgoing_server_password) {
4612 err = emcore_remove_password_in_key_manager(send_password_file_name);
4613 if (err != EMAIL_ERROR_NONE) {
4614 EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", send_password_file_name);
4618 /* save send passwords to the secure storage */
4619 err = emcore_add_password_in_key_manager(send_password_file_name, input_outgoing_server_password);
4620 if (err != EMAIL_ERROR_NONE) {
4621 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4627 EM_DEBUG_FUNC_END("err [%d]", err);
4631 INTERNAL_FUNC int emstorage_update_account(char *multi_user_name, int account_id, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
4633 EM_DEBUG_FUNC_BEGIN("account_id[%d], account[%p], transaction[%d], err_code[%p]", account_id, account_tbl, transaction, err_code);
4635 if (account_id < FIRST_ACCOUNT_ID || !account_tbl) {
4636 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4637 if (err_code != NULL)
4638 *err_code = EMAIL_ERROR_INVALID_PARAM;
4642 int error = EMAIL_ERROR_NONE;
4643 int rc, ret = false;
4645 DB_STMT hStmt = NULL;
4646 char sql_query_string[QUERY_SIZE] = {0, };
4648 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4650 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4652 SNPRINTF(sql_query_string, sizeof(sql_query_string),
4653 "UPDATE mail_account_tbl SET"
4655 ", logo_icon_path = ?"
4657 ", user_data_length = ?"
4658 ", account_svc_id = ?"
4660 ", sync_disabled = ?"
4661 ", default_mail_slot_size = ?"
4662 ", roaming_option = ?"
4664 ", user_display_name = ?"
4665 ", user_email_address = ?"
4666 ", reply_to_address = ?"
4667 ", return_address = ?"
4668 ", incoming_server_type = ?"
4669 ", incoming_server_address = ?"
4670 ", incoming_server_port_number = ?"
4671 ", incoming_server_user_name = ?"
4672 ", incoming_server_secure_connection = ?"
4673 ", incoming_server_authentication_method = ?"
4674 ", retrieval_mode = ?"
4675 ", keep_mails_on_pop_server_after_download = ?"
4676 ", check_interval = ?"
4677 ", auto_download_size = ?"
4678 ", peak_interval = ?"
4680 ", peak_start_time = ?"
4681 ", peak_end_time = ?"
4682 ", outgoing_server_type = ?"
4683 ", outgoing_server_address = ?"
4684 ", outgoing_server_port_number = ?"
4685 ", outgoing_server_user_name = ?"
4686 ", outgoing_server_secure_connection = ?"
4687 ", outgoing_server_need_authentication = ?"
4688 ", outgoing_server_use_same_authenticator = ?"
4690 ", keep_local_copy = ?"
4691 ", req_delivery_receipt = ?"
4692 ", req_read_receipt = ?"
4693 ", download_limit = ?"
4694 ", block_address = ?"
4695 ", block_subject = ?"
4696 ", display_name_from = ?"
4697 ", reply_with_body = ?"
4698 ", forward_with_files = ?"
4699 ", add_myname_card = ?"
4700 ", add_signature = ?"
4702 ", add_my_address_to_bcc = ?"
4703 ", notification_status = ?"
4704 ", vibrate_status = ?"
4705 ", display_content_status = ?"
4706 ", default_ringtone_status = ?"
4707 ", alert_ringtone_path = ?"
4708 ", auto_resend_times = ?"
4709 ", outgoing_server_size_limit = ?"
4710 ", wifi_auto_download = ?"
4711 ", pop_before_smtp = ?"
4712 ", incoming_server_requires_apop = ?"
4714 ", certificate_path = ?"
4718 " WHERE account_id = ?");
4720 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4721 EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
4722 EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
4723 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4724 ("sqlite3_prepare fail:(%d) %s", rc, sqlite3_errmsg(local_db_handle)));
4728 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4729 _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4730 _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4731 _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4732 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4733 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4734 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4735 _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4736 _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
4737 _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
4738 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4739 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4740 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4741 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4742 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4743 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4744 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
4745 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4746 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
4747 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
4748 _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
4749 _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
4750 _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
4751 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
4752 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
4753 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
4754 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
4755 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
4756 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
4757 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4758 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
4759 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
4760 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
4761 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
4762 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
4763 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
4764 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
4765 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
4766 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
4767 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
4768 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
4769 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
4770 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4771 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
4772 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
4773 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
4774 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
4775 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
4776 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
4777 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
4778 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
4779 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
4780 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
4781 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4782 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
4783 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
4784 _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
4785 _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
4786 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
4787 _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
4788 _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4789 _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
4790 _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
4791 _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4792 _bind_stmt_field_data_int(hStmt, i++, account_id);
4794 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4795 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
4796 ("sqlite3_step fail:%d", rc));
4797 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4798 ("sqlite3_step fail:%d", rc));
4800 /* validate account existence */
4801 rc = sqlite3_changes(local_db_handle);
4803 EM_DEBUG_EXCEPTION("no matched account found...");
4804 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4808 if (account_tbl->incoming_server_password || account_tbl->outgoing_server_password) {
4809 if ((error = emstorage_update_account_password(multi_user_name, account_id, account_tbl->incoming_server_password, account_tbl->outgoing_server_password)) != EMAIL_ERROR_NONE) {
4810 EM_DEBUG_EXCEPTION("emstorage_update_account_password failed [%d]", error);
4818 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4820 if (hStmt != NULL) {
4821 rc = sqlite3_finalize(hStmt);
4822 if (rc != SQLITE_OK) {
4823 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
4824 error = EMAIL_ERROR_DB_FAILURE;
4828 if (error == EMAIL_ERROR_NONE) {
4829 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_tbl->account_id, 0, NULL, 0))
4830 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_ACCOUNT_UPDATE] : Notification Failed >>> ");
4833 if (err_code != NULL)
4836 EM_DEBUG_FUNC_END("ret [%d]", ret);
4840 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)
4842 EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], field_name[%s], value[%d], transaction[%d]", account_id, field_name, value, transaction);
4843 int error = EMAIL_ERROR_NONE;
4845 char sql_query_string[QUERY_SIZE] = {0, };
4846 sqlite3 *local_db_handle = NULL;
4848 if (!account_id || !field_name) {
4849 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4850 return EMAIL_ERROR_INVALID_PARAM;
4853 local_db_handle = emstorage_get_db_connection(multi_user_name);
4855 /* Write query string */
4856 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_account_tbl SET %s = %d WHERE account_id = %d", field_name, value, account_id);
4858 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4861 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4863 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4864 if (error != EMAIL_ERROR_NONE) {
4865 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4869 if (sqlite3_changes(local_db_handle) == 0)
4870 EM_DEBUG_LOG("no mail matched...");
4874 result = (error == EMAIL_ERROR_NONE) ? true : false;
4875 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, error);
4877 if (error == EMAIL_ERROR_NONE) {
4878 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_id, 0, field_name, value))
4879 EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_ACCOUNT_UPDATE [%s,%d]", field_name, value);
4881 EM_DEBUG_FUNC_END("error [%d]", error);
4885 INTERNAL_FUNC int emstorage_get_sync_status_of_account(char *multi_user_name, int account_id, int *result_sync_status, int *err_code)
4887 EM_DEBUG_FUNC_BEGIN("account_id[%d], result_sync_status [%p], err_code[%p]", account_id, result_sync_status, err_code);
4889 if (!result_sync_status) {
4890 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4891 if (err_code != NULL)
4892 *err_code = EMAIL_ERROR_INVALID_PARAM;
4896 int error = EMAIL_ERROR_NONE, rc, ret = false, sync_status = 0, count, i, col_index;
4897 char sql_query_string[QUERY_SIZE] = {0, };
4898 char **result = NULL;
4899 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4902 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl WHERE account_id = %d", account_id);
4904 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl");
4906 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
4907 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
4908 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4911 EM_DEBUG_EXCEPTION("no matched account found...");
4912 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4917 *result_sync_status = 0;
4919 for (i = 0; i < count; i++) {
4920 _get_table_field_data_int(result, &sync_status, col_index++);
4921 *result_sync_status |= sync_status;
4924 EM_DEBUG_LOG("sync_status [%d]", sync_status);
4926 sqlite3_free_table(result);
4933 if (err_code != NULL)
4936 EM_DEBUG_FUNC_END("ret [%d]", ret);
4940 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)
4942 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);
4944 int error = EMAIL_ERROR_NONE, rc, ret = false, set_value = sync_status, result_sync_status;
4945 char sql_query_string[QUERY_SIZE] = {0, };
4946 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4948 if (set_operator != SET_TYPE_SET && account_id) {
4949 if (!emstorage_get_sync_status_of_account(multi_user_name, account_id, &result_sync_status, &error)) {
4950 EM_DEBUG_EXCEPTION("emstorage_get_sync_status_of_account failed [%d]", error);
4951 if (err_code != NULL)
4955 switch (set_operator) {
4956 case SET_TYPE_UNION:
4957 set_value = result_sync_status | set_value;
4959 case SET_TYPE_MINUS:
4960 set_value = result_sync_status & (~set_value);
4963 EM_DEBUG_EXCEPTION("EMAIL_ERROR_NOT_SUPPORTED [%d]", set_operator);
4964 error = EMAIL_ERROR_NOT_SUPPORTED;
4967 EM_DEBUG_LOG("set_value [%d]", set_value);
4970 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4973 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE account_id = %d", set_value, account_id);
4975 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE incoming_server_type <> 5", set_value);
4977 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4979 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4980 if (error != EMAIL_ERROR_NONE) {
4981 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4985 rc = sqlite3_changes(local_db_handle);
4988 EM_DEBUG_EXCEPTION("no matched account found...");
4989 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4996 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4998 if (error == EMAIL_ERROR_NONE) {
4999 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE_SYNC_STATUS, account_id, set_value, NULL, 0))
5000 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_UPDATE_SYNC_STATUS] : Notification failed");
5003 if (err_code != NULL)
5006 EM_DEBUG_FUNC_END("ret [%d]", ret);
5010 INTERNAL_FUNC int emstorage_add_account(char *multi_user_name, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
5012 EM_DEBUG_FUNC_BEGIN("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
5015 EM_DEBUG_EXCEPTION("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
5016 if (err_code != NULL)
5017 *err_code = EMAIL_ERROR_INVALID_PARAM;
5023 int rc = -1, ret = false;
5024 int error = EMAIL_ERROR_NONE;
5025 DB_STMT hStmt = NULL;
5026 char sql_query_string[QUERY_SIZE] = {0, };
5027 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5028 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5030 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5032 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5034 char *sql = "SELECT rowid FROM mail_account_tbl;";
5035 char **result = NULL;
5037 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, &row_count, NULL, NULL), rc);
5038 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
5039 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
5041 if (row_count == 0 || NULL == result[1]) rc = 1;
5043 for (i = 1; i <= row_count; i++) {
5044 if (i != atoi(result[i]))
5050 sqlite3_free_table(result);
5053 if (rc < 0 || rc > EMAIL_ACCOUNT_MAX) {
5054 EM_DEBUG_EXCEPTION("OVERFLOWED THE MAX ACCOUNT");
5055 error = EMAIL_ERROR_ACCOUNT_MAX_COUNT;
5059 account_tbl->account_id = rc;
5061 if ((error = _get_password_file_name(multi_user_name, account_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
5062 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
5066 EM_DEBUG_LOG(" >>>> ACCOUNT_ID [ %d ] ", account_tbl->account_id);
5067 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5068 "INSERT INTO mail_account_tbl VALUES "
5070 " ? " /* account_id */
5071 " , ? " /* account_name */
5072 " , ? " /* logo_icon_path */
5073 " , ? " /* user_data */
5074 " , ? " /* user_data_length */
5075 " , ? " /* account_svc_id */
5076 " , ? " /* sync_status */
5077 " , ? " /* sync_disabled */
5078 " , ? " /* default_mail_slot_size */
5079 " , ? " /* roaming_option */
5080 " , ? " /* color_label */
5081 " , ? " /* user_display_name */
5082 " , ? " /* user_email_address */
5083 " , ? " /* reply_to_address */
5084 " , ? " /* return_address */
5085 " , ? " /* incoming_server_type */
5086 " , ? " /* incoming_server_address */
5087 " , ? " /* incoming_server_port_number */
5088 " , ? " /* incoming_server_user_name */
5089 " , ? " /* incoming_server_password */
5090 " , ? " /* incoming_server_secure_connection */
5091 " , ? " /* incoming_server_authentication_method */
5092 " , ? " /* retrieval_mode */
5093 " , ? " /* keep_mails_on_pop_server_after_download */
5094 " , ? " /* check_interval */
5095 " , ? " /* auto_download_size */
5096 " , ? " /* peak_interval */
5097 " , ? " /* peak_days */
5098 " , ? " /* peak_start_time */
5099 " , ? " /* peak_end_time */
5100 " , ? " /* outgoing_server_type */
5101 " , ? " /* outgoing_server_address */
5102 " , ? " /* outgoing_server_port_number */
5103 " , ? " /* outgoing_server_user_name */
5104 " , ? " /* outgoing_server_password */
5105 " , ? " /* outgoing_server_secure_connection */
5106 " , ? " /* outgoing_server_need_authentication */
5107 " , ? " /* outgoing_server_use_same_authenticator */
5108 " , ? " /* priority */
5109 " , ? " /* keep_local_copy */
5110 " , ? " /* req_delivery_receipt */
5111 " , ? " /* req_read_receipt */
5112 " , ? " /* download_limit */
5113 " , ? " /* block_address */
5114 " , ? " /* block_subject */
5115 " , ? " /* display_name_from */
5116 " , ? " /* reply_with_body */
5117 " , ? " /* forward_with_files */
5118 " , ? " /* add_myname_card */
5119 " , ? " /* add_signature */
5120 " , ? " /* signature */
5121 " , ? " /* add_my_address_to_bcc */
5122 " , ? " /* auto_resend_times */
5123 " , ? " /* outgoing_server_size_limit */
5124 " , ? " /* wifi_auto_download */
5125 " , ? " /* pop_before_smtp */
5126 " , ? " /* incoming_server_requires_apop */
5127 " , ? " /* smime_type */
5128 " , ? " /* certificate_path */
5129 " , ? " /* cipher_type */
5130 " , ? " /* digest_type */
5131 " , ? " /* notification_status */
5132 " , ? " /* vibrate_status */
5133 " , ? " /* display_content_status */
5134 " , ? " /* default_ringtone_status */
5135 " , ? " /* alert_ringtone_path */
5136 " , ? " /* user_name */
5139 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5140 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5141 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5145 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_id);
5146 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
5147 _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5148 _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
5149 _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
5150 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
5151 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
5152 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
5153 _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
5154 _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
5155 _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
5156 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
5157 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5158 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5159 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5160 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
5161 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5162 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
5163 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
5164 _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
5165 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
5166 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
5167 _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
5168 _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
5169 _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
5170 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
5171 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
5172 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
5173 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
5174 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
5175 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
5176 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5177 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
5178 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
5179 _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
5180 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
5181 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
5182 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
5183 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
5184 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
5185 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
5186 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
5187 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
5188 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
5189 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
5190 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
5191 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
5192 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
5193 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
5194 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
5195 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
5196 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
5197 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
5198 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
5199 _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
5200 _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
5201 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
5202 _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
5203 _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
5204 _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
5205 _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
5206 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
5207 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
5208 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
5209 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
5210 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5211 _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5213 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5215 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
5216 ("sqlite3_step fail:%d", rc));
5217 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5218 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
5221 /* save passwords to the secure storage */
5222 EM_DEBUG_LOG_SEC("save to the secure storage : recv_file[%s], send_file[%s]", recv_password_file_name, send_password_file_name);
5223 error = emcore_add_password_in_key_manager(recv_password_file_name, account_tbl->incoming_server_password);
5224 if (error != EMAIL_ERROR_NONE) {
5225 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
5229 error = emcore_add_password_in_key_manager(send_password_file_name, account_tbl->outgoing_server_password);
5230 if (error != EMAIL_ERROR_NONE) {
5231 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
5238 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5240 if (hStmt != NULL) {
5241 rc = sqlite3_finalize(hStmt);
5242 if (rc != SQLITE_OK) {
5243 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5244 error = EMAIL_ERROR_DB_FAILURE;
5248 if (error == EMAIL_ERROR_NONE) {
5249 if (!emcore_notify_storage_event(NOTI_ACCOUNT_ADD, account_tbl->account_id, 0, NULL, 0))
5250 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_ADD] : Notification failed");
5253 if (err_code != NULL)
5256 EM_DEBUG_FUNC_END("ret [%d]", ret);
5260 INTERNAL_FUNC int emstorage_delete_account(char *multi_user_name, int account_id, int transaction, int *err_code)
5262 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
5264 if (account_id < FIRST_ACCOUNT_ID) {
5265 EM_DEBUG_EXCEPTION(" account_id[%d]", account_id);
5267 if (err_code != NULL)
5268 *err_code = EMAIL_ERROR_INVALID_PARAM;
5272 int rc = -1, ret = false;
5273 int error = EMAIL_ERROR_NONE;
5275 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5277 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5279 /* TODO : delete password files - file names can be obtained from db or a rule that makes a name */
5280 char sql_query_string[QUERY_SIZE] = {0, };
5281 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5282 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5284 /* get password file name */
5285 if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
5286 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
5290 /* delete from db */
5291 memset(sql_query_string, 0x00, sizeof(sql_query_string));
5292 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_account_tbl WHERE account_id = %d", account_id);
5293 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5294 if (error != EMAIL_ERROR_NONE) {
5295 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5299 /* validate account existence */
5300 rc = sqlite3_changes(local_db_handle);
5302 EM_DEBUG_EXCEPTION(" no matched account found...");
5303 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
5307 /* delete from secure storage */
5308 error = emcore_remove_password_in_key_manager(recv_password_file_name);
5309 if (error != EMAIL_ERROR_NONE) {
5310 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5314 error = emcore_remove_password_in_key_manager(send_password_file_name);
5315 if (error != EMAIL_ERROR_NONE) {
5316 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5319 /* LCOV_EXCL_STOP */
5323 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5325 if (err_code != NULL)
5328 EM_DEBUG_FUNC_END("ret [%d]", ret);
5333 INTERNAL_FUNC int emstorage_free_account(emstorage_account_tbl_t** account_list, int count, int *err_code)
5335 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%d], err_code[%p]", account_list, count, err_code);
5338 int error = EMAIL_ERROR_NONE;
5341 if (!account_list || !*account_list) {
5342 EM_DEBUG_EXCEPTION("account_list[%p], count[%d]", account_list, count);
5343 error = EMAIL_ERROR_INVALID_PARAM;
5347 emstorage_account_tbl_t* p = *account_list;
5350 for (; i < count; i++) {
5351 EM_SAFE_FREE(p[i].account_name);
5352 EM_SAFE_FREE(p[i].incoming_server_address);
5353 EM_SAFE_FREE(p[i].user_email_address);
5354 EM_SAFE_FREE(p[i].user_data);
5355 EM_SAFE_FREE(p[i].incoming_server_user_name);
5356 EM_SAFE_FREE(p[i].incoming_server_password);
5357 EM_SAFE_FREE(p[i].outgoing_server_address);
5358 EM_SAFE_FREE(p[i].outgoing_server_user_name);
5359 EM_SAFE_FREE(p[i].outgoing_server_password);
5360 EM_SAFE_FREE(p[i].user_display_name);
5361 EM_SAFE_FREE(p[i].reply_to_address);
5362 EM_SAFE_FREE(p[i].return_address);
5363 EM_SAFE_FREE(p[i].logo_icon_path);
5364 EM_SAFE_FREE(p[i].options.display_name_from);
5365 EM_SAFE_FREE(p[i].options.signature);
5366 EM_SAFE_FREE(p[i].options.alert_ringtone_path);
5367 EM_SAFE_FREE(p[i].certificate_path);
5368 EM_SAFE_FREE(p[i].user_name);
5372 *account_list = NULL;
5378 if (err_code != NULL)
5381 EM_DEBUG_FUNC_END("ret [%d]", ret);
5384 /* LCOV_EXCL_START */
5385 INTERNAL_FUNC int emstorage_get_mailbox_count(char *multi_user_name, int account_id, int local_yn, int *count, int transaction, int *err_code)
5387 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);
5389 if ((account_id < FIRST_ACCOUNT_ID) || (count == NULL)) {
5390 EM_DEBUG_EXCEPTION(" account_list[%d], local_yn[%d], count[%p]", account_id, local_yn, count);
5392 if (err_code != NULL)
5393 *err_code = EMAIL_ERROR_INVALID_PARAM;
5397 int rc = -1, ret = false;
5398 int error = EMAIL_ERROR_NONE;
5399 char sql_query_string[QUERY_SIZE] = {0, };
5401 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5402 EMSTORAGE_START_READ_TRANSACTION(transaction);
5404 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);
5408 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5409 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
5410 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5412 *count = atoi(result[1]);
5413 sqlite3_free_table(result);
5420 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5422 if (err_code != NULL)
5425 EM_DEBUG_FUNC_END("ret [%d]", ret);
5429 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)
5431 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);
5433 if (!select_num || !mailbox_list) {
5434 EM_DEBUG_EXCEPTION("Invalid parameters");
5436 if (err_code != NULL)
5437 *err_code = EMAIL_ERROR_INVALID_PARAM;
5443 int error = EMAIL_ERROR_NONE;
5444 char conditional_clause_string[QUERY_SIZE] = {0, };
5445 char ordering_clause_string[QUERY_SIZE] = {0, };
5447 if (account_id == ALL_ACCOUNT) {
5448 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5449 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);
5451 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d ", account_id);
5452 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5453 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);
5456 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5458 switch (sort_type) {
5459 case EMAIL_MAILBOX_SORT_BY_NAME_ASC:
5460 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name ASC");
5463 case EMAIL_MAILBOX_SORT_BY_NAME_DSC:
5464 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name DESC");
5467 case EMAIL_MAILBOX_SORT_BY_TYPE_ASC:
5468 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type ASC");
5471 case EMAIL_MAILBOX_SORT_BY_TYPE_DSC:
5472 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type DEC");
5476 EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5478 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5479 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5487 if (err_code != NULL)
5490 EM_DEBUG_FUNC_END("ret [%d]", ret);
5494 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)
5496 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);
5499 int error = EMAIL_ERROR_NONE;
5500 int where_clause_count = 0;
5501 char conditional_clause_string[QUERY_SIZE] = {0, };
5502 char ordering_clause_string[QUERY_SIZE] = {0, };
5504 if (!select_num || !mailbox_list) {
5505 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5506 error = EMAIL_ERROR_INVALID_PARAM;
5510 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL) {
5511 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);
5512 where_clause_count++;
5515 if (account_id > 0) {
5516 if (where_clause_count == 0) {
5517 SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " WHERE ");
5518 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);
5520 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);
5523 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY CASE WHEN MBT.mailbox_name"
5524 " GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\'"
5525 " THEN 2 ELSE 1 END ASC,"
5526 " MBT.mailbox_name COLLATE CONVERTUTF8 ASC ");
5527 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5528 EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5530 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 1, 1, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5531 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5539 if (err_code != NULL)
5542 EM_DEBUG_FUNC_END("ret [%d]", ret);
5546 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)
5548 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);
5551 int error = EMAIL_ERROR_NONE;
5552 char conditional_clause_string[QUERY_SIZE] = {0, };
5554 if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list || !parent_mailbox_name) {
5555 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);
5556 if (err_code != NULL)
5557 *err_code = EMAIL_ERROR_INVALID_PARAM;
5561 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);
5562 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5564 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) {
5565 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5574 if (err_code != NULL)
5577 EM_DEBUG_FUNC_END("ret [%d]", ret);
5581 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)
5583 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);
5584 if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list) {
5585 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5587 if (err_code != NULL)
5588 *err_code = EMAIL_ERROR_INVALID_PARAM;
5594 int error = EMAIL_ERROR_NONE;
5595 char conditional_clause_string[QUERY_SIZE] = {0, };
5597 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND modifiable_yn = 0", account_id);
5598 EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
5600 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) {
5601 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5609 if (err_code != NULL)
5612 EM_DEBUG_FUNC_END("ret [%d]", ret);
5616 INTERNAL_FUNC int emstorage_stamp_last_sync_time_of_mailbox(char *multi_user_name, int input_mailbox_id, int input_transaction)
5618 EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_transaction [%d]", input_mailbox_id, input_transaction);
5620 int result_code = false;
5621 int error = EMAIL_ERROR_NONE;
5622 time_t current_time = 0;
5623 char sql_query_string[QUERY_SIZE] = {0, };
5624 sqlite3 *local_db_handle = NULL;
5626 if (!input_mailbox_id) {
5627 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5628 return EMAIL_ERROR_INVALID_PARAM;
5631 time(¤t_time);
5633 local_db_handle = emstorage_get_db_connection(multi_user_name);
5634 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
5636 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5637 "UPDATE mail_box_tbl SET"
5638 " last_sync_time = %d"
5639 " WHERE mailbox_id = %d"
5641 , input_mailbox_id);
5643 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
5644 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5645 if (error != EMAIL_ERROR_NONE) {
5646 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5652 if (error == EMAIL_ERROR_NONE)
5655 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, result_code, error);
5657 EM_DEBUG_FUNC_END("error [%d]", error);
5661 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)
5663 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);
5664 EM_PROFILE_BEGIN(profile_emstorage_get_mailbox_by_name);
5666 if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !result_mailbox) {
5667 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], mailbox_name[%s], result_mailbox[%p]", account_id, local_yn, mailbox_name, result_mailbox);
5669 if (err_code != NULL)
5670 *err_code = EMAIL_ERROR_INVALID_PARAM;
5675 int error = EMAIL_ERROR_NONE;
5676 int result_count = 0;
5677 char conditional_clause_string[QUERY_SIZE] = {0, };
5679 if (strcmp(mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
5680 if (!(*result_mailbox = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t)))) {
5681 EM_DEBUG_EXCEPTION("malloc failed...");
5682 error = EMAIL_ERROR_OUT_OF_MEMORY;
5686 (*result_mailbox)->mailbox_id = 0;
5687 (*result_mailbox)->account_id = account_id;
5688 (*result_mailbox)->local_yn = 1;
5689 (*result_mailbox)->mailbox_name = EM_SAFE_STRDUP(mailbox_name);
5690 (*result_mailbox)->mailbox_type = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;
5691 (*result_mailbox)->alias = EM_SAFE_STRDUP(mailbox_name);
5692 (*result_mailbox)->deleted_flag = 0;
5693 (*result_mailbox)->modifiable_yn = 1;
5694 (*result_mailbox)->total_mail_count_on_server = 1;
5695 (*result_mailbox)->has_archived_mails = 0;
5696 (*result_mailbox)->mail_slot_size = 0x0FFFFFFF;
5697 (*result_mailbox)->no_select = 0;
5700 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string, "WHERE account_id = %d AND mailbox_name = '%q'", account_id, mailbox_name);
5702 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);
5704 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5706 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5707 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5711 /* LCOV_EXCL_STOP */
5716 if (err_code != NULL)
5719 EM_PROFILE_END(profile_emstorage_get_mailbox_by_name);
5720 EM_DEBUG_FUNC_END("ret [%d]", ret);
5724 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)
5726 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);
5728 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_USER_DEFINED) || !output_mailbox) {
5729 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_type[%d], output_mailbox[%p]", account_id, mailbox_type, output_mailbox);
5730 if (err_code != NULL)
5731 *err_code = EMAIL_ERROR_INVALID_PARAM;
5736 int error = EMAIL_ERROR_NONE;
5737 int result_count = 0;
5738 emstorage_mailbox_tbl_t *result_mailbox = NULL;
5739 char conditional_clause_string[QUERY_SIZE] = {0,};
5742 SNPRINTF(conditional_clause_string, QUERY_SIZE, "WHERE account_id = %d AND mailbox_type = %d ", account_id, mailbox_type);
5744 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5746 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", true, false, &result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5747 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl error [%d]", error);
5755 *output_mailbox = result_mailbox;
5757 if (err_code != NULL)
5760 EM_DEBUG_FUNC_END("ret [%d]", ret);
5763 /* LCOV_EXCL_START */
5764 INTERNAL_FUNC int emstorage_get_mailbox_by_id(char *multi_user_name, int input_mailbox_id, emstorage_mailbox_tbl_t** output_mailbox)
5766 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], output_mailbox[%p]", input_mailbox_id, output_mailbox);
5768 if (input_mailbox_id <= 0 || !output_mailbox) {
5769 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: input_mailbox_id[%d] output_mailbox[%p]", input_mailbox_id, output_mailbox);
5770 return EMAIL_ERROR_INVALID_PARAM;
5773 int ret = EMAIL_ERROR_NONE;
5774 int result_count = 0;
5775 char conditional_clause_string[QUERY_SIZE] = {0, };
5777 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE MBT.mailbox_id = %d", input_mailbox_id);
5779 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5781 if ((ret = emstorage_query_mailbox_tbl(multi_user_name,
5782 conditional_clause_string,
5783 "", true, false, output_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5784 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", ret);
5790 EM_DEBUG_FUNC_END("ret [%d]", ret);
5794 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)
5796 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);
5798 if (account_id < 0 || !keyword || !result_mailbox) {
5799 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], keyword[%s], result_mailbox[%p]", account_id, keyword, result_mailbox);
5801 if (err_code != NULL)
5802 *err_code = EMAIL_ERROR_INVALID_PARAM;
5807 int error = EMAIL_ERROR_NONE;
5808 char conditional_clause_string[QUERY_SIZE] = {0, };
5810 if (account_id == 0)
5811 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5812 "WHERE alias LIKE \'%%%q%%\'", keyword);
5813 else if (account_id > 0)
5814 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5815 "WHERE account_id = %d AND alias LIKE \'%%%q%%\'", account_id, keyword);
5817 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5819 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, result_count)) != EMAIL_ERROR_NONE) {
5820 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5828 if (err_code != NULL)
5831 EM_DEBUG_FUNC_END("ret [%d]", ret);
5835 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)
5837 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);
5838 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_id) {
5839 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_id[%p]", account_id, mailbox_type, mailbox_id);
5840 if (err_code != NULL)
5841 *err_code = EMAIL_ERROR_INVALID_PARAM;
5845 int rc, ret = false;
5846 int error = EMAIL_ERROR_NONE;
5847 emstorage_account_tbl_t* account = NULL;
5848 DB_STMT hStmt = NULL;
5849 char sql_query_string[QUERY_SIZE] = {0, };
5851 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5852 EMSTORAGE_START_READ_TRANSACTION(transaction);
5854 /* Check whether the account exists. */
5855 if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME, &account, true, &error) || !account) {
5856 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5861 emstorage_free_account(&account, 1, NULL);
5863 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);
5865 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5868 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5870 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5871 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5874 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5875 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5876 ("sqlite3_step fail:%d", rc));
5878 if (rc == SQLITE_DONE) {
5879 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5880 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5884 _get_stmt_field_data_int(hStmt, mailbox_id, 0);
5889 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5890 if (hStmt != NULL) {
5891 rc = sqlite3_finalize(hStmt);
5892 if (rc != SQLITE_OK) {
5893 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5894 error = EMAIL_ERROR_DB_FAILURE;
5898 if (err_code != NULL)
5901 EM_DEBUG_FUNC_END("ret [%d]", ret);
5905 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)
5907 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);
5908 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_name) {
5909 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
5910 if (err_code != NULL)
5911 *err_code = EMAIL_ERROR_INVALID_PARAM;
5915 int rc, ret = false;
5916 int error = EMAIL_ERROR_NONE;
5917 emstorage_account_tbl_t* account = NULL;
5918 DB_STMT hStmt = NULL;
5919 char sql_query_string[QUERY_SIZE] = {0, };
5921 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5922 EMSTORAGE_START_READ_TRANSACTION(transaction);
5924 /* Check whether the account exists. */
5925 if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME, &account, true, &error) || !account) {
5926 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5931 emstorage_free_account(&account, 1, NULL);
5933 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);
5935 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5938 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5940 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5941 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5944 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5945 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5946 ("sqlite3_step fail:%d", rc));
5948 if (rc == SQLITE_DONE) {
5949 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5950 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5954 _get_stmt_field_data_string(hStmt, mailbox_name, 0, 0);
5959 if (hStmt != NULL) {
5960 rc = sqlite3_finalize(hStmt);
5961 if (rc != SQLITE_OK) {
5962 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5963 error = EMAIL_ERROR_DB_FAILURE;
5967 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5969 if (err_code != NULL)
5972 EM_DEBUG_FUNC_END("ret [%d]", ret);
5976 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)
5978 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);
5980 int error = EMAIL_ERROR_NONE;
5981 char sql_query_string[QUERY_SIZE] = {0, };
5982 char *replaced_mailbox_name = NULL;
5985 if (strstr(mailbox_name, "'"))
5986 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5988 replaced_mailbox_name = strdup(mailbox_name);
5991 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5993 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5995 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5996 "UPDATE mail_box_tbl SET"
5997 " modifiable_yn = %d"
5998 " WHERE account_id = %d"
5999 " AND local_yn = %d"
6000 " AND mailbox_name = '%s'"
6004 , replaced_mailbox_name);
6006 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6007 if (error != EMAIL_ERROR_NONE) {
6008 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6015 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6017 EM_SAFE_FREE(replaced_mailbox_name);
6019 if (err_code != NULL)
6022 EM_DEBUG_FUNC_END("ret [%d]", ret);
6027 INTERNAL_FUNC int emstorage_update_mailbox_total_count(char *multi_user_name,
6029 int input_mailbox_id,
6030 int total_count_on_server,
6034 EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], total_count_on_server[%d], "
6035 "transaction[%d], err_code[%p]",
6036 account_id, input_mailbox_id, total_count_on_server, transaction, err_code);
6039 int error = EMAIL_ERROR_NONE;
6040 char sql_query_string[QUERY_SIZE] = {0, };
6042 if (account_id <= 0 || input_mailbox_id <= 0) {
6043 EM_DEBUG_EXCEPTION("account_id[%d], input_mailbox_id[%d]", account_id, input_mailbox_id);
6044 if (err_code != NULL)
6045 *err_code = EMAIL_ERROR_INVALID_PARAM;
6046 EM_DEBUG_FUNC_END("ret [%d]", ret);
6050 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6052 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6054 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6055 "UPDATE mail_box_tbl SET"
6056 " total_mail_count_on_server = %d"
6057 " WHERE account_id = %d"
6058 " AND mailbox_id = %d"
6059 , total_count_on_server
6061 , input_mailbox_id);
6063 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
6064 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6065 if (error != EMAIL_ERROR_NONE) {
6066 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6074 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6076 if (err_code != NULL)
6079 EM_DEBUG_FUNC_END("ret [%d]", ret);
6084 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)
6086 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);
6088 if (account_id < FIRST_ACCOUNT_ID || input_mailbox_id <= 0 || !result_mailbox) {
6089 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);
6091 if (err_code != NULL)
6092 *err_code = EMAIL_ERROR_INVALID_PARAM;
6093 EM_DEBUG_FUNC_END("ret [%d]", EMAIL_ERROR_INVALID_PARAM);
6097 int rc, ret = false;
6098 int error = EMAIL_ERROR_NONE;
6099 char sql_query_string[QUERY_SIZE] = {0, };
6100 DB_STMT hStmt = NULL;
6103 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6104 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6106 if (local_yn != -1) {
6107 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6108 "UPDATE mail_box_tbl SET"
6110 ", mailbox_name = ?"
6111 ", mailbox_type = ?"
6113 ", deleted_flag = ?"
6114 ", modifiable_yn= ?"
6115 ", mail_slot_size= ?"
6116 ", total_mail_count_on_server = ?"
6117 " WHERE account_id = %d"
6118 " AND local_yn = %d"
6119 " AND mailbox_id = '%d'"
6122 , input_mailbox_id);
6124 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6125 "UPDATE mail_box_tbl SET"
6127 ", mailbox_name = ?"
6128 ", mailbox_type = ?"
6130 ", deleted_flag = ?"
6131 ", modifiable_yn= ?"
6132 ", mail_slot_size= ?"
6133 ", total_mail_count_on_server = ?"
6134 " WHERE account_id = %d"
6135 " AND mailbox_id = '%d'"
6137 , input_mailbox_id);
6142 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6143 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6144 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6146 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_id);
6147 _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);
6148 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_type);
6149 _bind_stmt_field_data_string(hStmt, i++, (char *)result_mailbox->alias ? result_mailbox->alias : "", 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
6150 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->deleted_flag);
6151 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->modifiable_yn);
6152 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mail_slot_size);
6153 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->total_mail_count_on_server);
6156 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6157 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
6158 ("sqlite3_step fail:%d", rc));
6159 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6160 ("sqlite3_step fail:%d", rc));
6165 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6167 if (hStmt != NULL) {
6168 rc = sqlite3_finalize(hStmt);
6169 if (rc != SQLITE_OK) {
6170 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6171 error = EMAIL_ERROR_DB_FAILURE;
6175 if (err_code != NULL)
6178 EM_DEBUG_FUNC_END("ret [%d]", ret);
6182 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)
6184 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);
6186 if (account_id < FIRST_ACCOUNT_ID) {
6187 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
6189 if (err_code != NULL)
6190 *err_code = EMAIL_ERROR_INVALID_PARAM;
6194 int rc, ret = false;
6195 int error = EMAIL_ERROR_NONE;
6196 char sql_query_string[QUERY_SIZE] = {0, };
6198 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6199 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6201 EM_DEBUG_LOG("emstorage_update_mailbox_type");
6203 DB_STMT hStmt_box_tbl = NULL;
6204 DB_STMT hStmt_mail_tbl = NULL;
6207 /* Update mail_box_tbl */
6208 if (local_yn != -1) {
6209 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
6210 "UPDATE mail_box_tbl SET"
6212 " WHERE account_id = %d"
6213 " AND local_yn = %d"
6214 " AND mailbox_id = '%d'"
6217 , input_mailbox_id);
6219 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
6220 "UPDATE mail_box_tbl SET"
6222 " WHERE account_id = %d"
6223 " AND mailbox_id = '%d'"
6225 , input_mailbox_id);
6228 EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
6230 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_box_tbl, NULL), rc);
6232 if (SQLITE_OK != rc) {
6233 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6234 error = EMAIL_ERROR_DB_FAILURE;
6238 _bind_stmt_field_data_int(hStmt_box_tbl, i++, new_mailbox_type);
6241 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_box_tbl), rc);
6243 if (rc == SQLITE_FULL) {
6244 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6245 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
6249 if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
6250 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6251 error = EMAIL_ERROR_DB_FAILURE;
6256 /* Update mail_tbl */
6258 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6259 "UPDATE mail_tbl SET"
6261 " WHERE account_id = %d"
6262 " AND mailbox_id = '%d'"
6264 , input_mailbox_id);
6266 EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
6268 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_mail_tbl, NULL), rc);
6269 if (SQLITE_OK != rc) {
6270 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6271 error = EMAIL_ERROR_DB_FAILURE;
6275 _bind_stmt_field_data_int(hStmt_mail_tbl, i++, new_mailbox_type);
6277 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_mail_tbl), rc);
6278 if (rc == SQLITE_FULL) {
6279 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6280 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
6284 if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
6285 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6286 error = EMAIL_ERROR_DB_FAILURE;
6293 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6295 if (hStmt_box_tbl != NULL) {
6296 rc = sqlite3_finalize(hStmt_box_tbl);
6297 if (rc != SQLITE_OK) {
6298 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6299 error = EMAIL_ERROR_DB_FAILURE;
6303 if (hStmt_mail_tbl != NULL) {
6304 rc = sqlite3_finalize(hStmt_mail_tbl);
6305 if (rc != SQLITE_OK) {
6306 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6307 error = EMAIL_ERROR_DB_FAILURE;
6311 if (err_code != NULL)
6314 EM_DEBUG_FUNC_END("ret [%d]", ret);
6318 INTERNAL_FUNC int emstorage_set_local_mailbox(char *multi_user_name, int input_mailbox_id, int input_is_local_mailbox, int transaction)
6320 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);
6322 int rc, ret = false;
6323 int error = EMAIL_ERROR_NONE;
6324 char sql_query_string[QUERY_SIZE] = {0, };
6326 if (input_mailbox_id < 0) {
6327 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6328 return EMAIL_ERROR_INVALID_PARAM;
6331 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6333 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6335 EM_DEBUG_LOG("emstorage_update_mailbox_type");
6337 DB_STMT hStmt = NULL;
6340 /* Update mail_box_tbl */
6341 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6342 "UPDATE mail_box_tbl SET"
6344 " WHERE mailbox_id = %d"
6345 , input_mailbox_id);
6347 EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
6349 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6350 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6351 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6353 _bind_stmt_field_data_int(hStmt, i++, input_is_local_mailbox);
6355 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6356 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
6357 ("sqlite3_step fail:%d", rc));
6358 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6359 ("sqlite3_step fail:%d", rc));
6361 if (hStmt != NULL) {
6362 rc = sqlite3_finalize(hStmt);
6363 if (rc != SQLITE_OK) {
6364 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6365 error = EMAIL_ERROR_DB_FAILURE;
6373 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6375 if (hStmt != NULL) {
6376 rc = sqlite3_finalize(hStmt);
6377 if (rc != SQLITE_OK) {
6378 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6379 error = EMAIL_ERROR_DB_FAILURE;
6383 EM_DEBUG_FUNC_END("error [%d]", error);
6387 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)
6389 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);
6391 int err = EMAIL_ERROR_NONE;
6393 int cur_mailbox_id_string = 0;
6394 int mailbox_id_string_buffer_length = 0;
6395 char sql_query_string[QUERY_SIZE] = {0, };
6396 char *mailbox_id_string_buffer = NULL;
6397 char *parameter_string = NULL;
6398 sqlite3 *local_db_handle = NULL;
6400 if (input_mailbox_id_array == NULL || input_mailbox_id_count == 0 || input_field_name == NULL) {
6401 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6402 return EMAIL_ERROR_INVALID_PARAM;
6405 local_db_handle = emstorage_get_db_connection(multi_user_name);
6407 /* Generating mail id list string */
6408 mailbox_id_string_buffer_length = MAILBOX_ID_STRING_LENGTH * input_mailbox_id_count;
6410 mailbox_id_string_buffer = em_malloc(mailbox_id_string_buffer_length);
6412 if (!mailbox_id_string_buffer) {
6413 EM_DEBUG_EXCEPTION("em_mallocfailed");
6414 err = EMAIL_ERROR_OUT_OF_MEMORY;
6418 for (i = 0; i < input_mailbox_id_count; i++)
6419 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]);
6421 if (EM_SAFE_STRLEN(mailbox_id_string_buffer) > 1)
6422 mailbox_id_string_buffer[EM_SAFE_STRLEN(mailbox_id_string_buffer) - 1] = NULL_CHAR;
6424 /* Generating notification parameter string */
6425 parameter_string = em_malloc(mailbox_id_string_buffer_length + EM_SAFE_STRLEN(input_field_name) + 2);
6427 if (!parameter_string) {
6428 EM_DEBUG_EXCEPTION("em_mallocfailed");
6429 err = EMAIL_ERROR_OUT_OF_MEMORY;
6433 SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", input_field_name, 0x01, mailbox_id_string_buffer);
6435 /* Write query string */
6436 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);
6438 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
6441 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
6442 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6443 if (err != EMAIL_ERROR_NONE) {
6444 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
6448 if (sqlite3_changes(local_db_handle) == 0)
6449 EM_DEBUG_LOG("no mail matched...");
6454 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, err);
6456 if (err == EMAIL_ERROR_NONE && parameter_string) {
6457 if (!emcore_notify_storage_event(NOTI_MAILBOX_FIELD_UPDATE, input_account_id, 0, parameter_string, input_value))
6458 EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_MAILBOX_FIELD_UPDATE [%s,%d]",
6459 input_field_name, input_value);
6462 EM_SAFE_FREE(mailbox_id_string_buffer);
6463 EM_SAFE_FREE(parameter_string);
6465 EM_DEBUG_FUNC_END("err [%d]", err);
6469 INTERNAL_FUNC int emstorage_add_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox_tbl, int transaction, int *err_code)
6471 EM_DEBUG_FUNC_BEGIN("mailbox_tbl[%p], transaction[%d], err_code[%p]", mailbox_tbl, transaction, err_code);
6474 if (err_code != NULL)
6475 *err_code = EMAIL_ERROR_INVALID_PARAM;
6479 int rc, ret = false;
6480 int error = EMAIL_ERROR_NONE;
6481 DB_STMT hStmt = NULL;
6482 char sql_query_string[QUERY_SIZE] = {0,};
6483 char **result = NULL;
6484 time_t current_time;
6485 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6486 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6488 EM_SAFE_STRNCPY(sql_query_string, "SELECT max(rowid) FROM mail_box_tbl;", sizeof(sql_query_string));
6490 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6491 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6492 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6494 time(¤t_time);
6496 if (NULL == result[1])
6499 rc = atoi(result[1]) + 1;
6500 sqlite3_free_table(result);
6502 memset(sql_query_string, 0, sizeof(char) * QUERY_SIZE);
6504 mailbox_tbl->mailbox_id = rc;
6506 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6507 "INSERT INTO mail_box_tbl VALUES "
6508 "(?" /* mailbox_id */
6509 ", ?" /* account_id */
6510 ", ?" /* local_yn */
6511 ", ?" /* mailbox_name */
6512 ", ?" /* mailbox_type */
6514 ", ?" /* deleted_flag */
6515 ", ?" /* modifiable_yn */
6516 ", ?" /* total_mail_count_on_server */
6517 ", ?" /* has_archived_mails */
6518 ", ?" /* mail_slot_size */
6519 ", ?" /* no_select */
6520 ", ?" /* last_sync_time */
6521 ", ?" /* eas_data_length */
6522 ", ?" /* eas_data */
6526 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6527 EM_DEBUG_LOG_DEV("After sqlite3_prepare hStmt = %p", hStmt);
6528 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6529 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6533 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_id);
6534 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->account_id);
6535 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->local_yn);
6536 _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
6537 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_type);
6538 _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->alias, 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
6539 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->deleted_flag);
6540 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->modifiable_yn);
6541 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->total_mail_count_on_server);
6542 _bind_stmt_field_data_int(hStmt, col_index++, 0);
6543 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mail_slot_size);
6544 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->no_select);
6545 _bind_stmt_field_data_int(hStmt, col_index++, current_time);
6546 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->eas_data_length);
6547 _bind_stmt_field_data_blob(hStmt, col_index++, (void*)mailbox_tbl->eas_data, mailbox_tbl->eas_data_length);
6550 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6551 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6552 ("sqlite3_step fail:%dn", rc));
6557 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6558 if (hStmt != NULL) {
6559 rc = sqlite3_finalize(hStmt);
6560 if (rc != SQLITE_OK) {
6561 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6562 error = EMAIL_ERROR_DB_FAILURE;
6566 if (error == EMAIL_ERROR_NONE) {
6567 if (!emcore_notify_storage_event(NOTI_MAILBOX_ADD, mailbox_tbl->account_id, mailbox_tbl->mailbox_id,
6568 mailbox_tbl->mailbox_name, mailbox_tbl->mailbox_type))
6569 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6572 if (err_code != NULL)
6575 EM_DEBUG_FUNC_END("ret [%d]", ret);
6579 INTERNAL_FUNC int emstorage_set_all_mailbox_modifiable_yn(char *multi_user_name, int account_id, int modifiable_yn, int transaction, int *err_code)
6581 EM_DEBUG_FUNC_BEGIN("account_id[%d], modifiable_yn[%d], err_code[%p]", account_id, modifiable_yn, err_code);
6583 if (account_id < FIRST_ACCOUNT_ID) {
6585 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
6587 if (err_code != NULL)
6588 *err_code = EMAIL_ERROR_INVALID_PARAM;
6591 int rc, ret = false;
6592 int error = EMAIL_ERROR_NONE;
6593 char sql_query_string[QUERY_SIZE] = {0,};
6594 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6595 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6598 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET modifiable_yn = %d WHERE account_id = %d", modifiable_yn, account_id);
6600 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6601 if (error != EMAIL_ERROR_NONE) {
6602 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6606 rc = sqlite3_changes(local_db_handle);
6608 EM_DEBUG_EXCEPTION("All mailbox_name modifiable_yn set to 0 already");
6614 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6616 if (err_code != NULL)
6618 EM_DEBUG_FUNC_END("ret [%d]", ret);
6624 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)
6626 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);
6628 if (account_id < FIRST_ACCOUNT_ID) {
6629 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
6631 if (err_code != NULL)
6632 *err_code = EMAIL_ERROR_INVALID_PARAM;
6636 int rc, ret = false;
6637 int error = EMAIL_ERROR_NONE;
6638 char sql_query_string[QUERY_SIZE] = {0, };
6639 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6640 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6643 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_box_tbl WHERE account_id = %d ", account_id);
6645 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);
6647 if (input_mailbox_id > 0) { /* 0 means all mailbox */
6648 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);
6651 EM_DEBUG_LOG_SEC("mailbox sql_query_string [%s]", sql_query_string);
6652 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6653 if (error != EMAIL_ERROR_NONE) {
6654 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6658 rc = sqlite3_changes(local_db_handle);
6660 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
6661 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
6668 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6670 if (error == EMAIL_ERROR_NONE) {
6671 if (!emcore_notify_storage_event(NOTI_MAILBOX_DELETE, account_id, input_mailbox_id, NULL, 0))
6672 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6675 if (err_code != NULL)
6678 EM_DEBUG_FUNC_END("ret [%d]", ret);
6681 /* LCOV_EXCL_STOP */
6682 INTERNAL_FUNC int emstorage_free_mailbox(emstorage_mailbox_tbl_t** mailbox_list, int count, int *err_code)
6684 EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d], err_code[%p]", mailbox_list, count, err_code);
6687 int error = EMAIL_ERROR_NONE;
6690 if (!mailbox_list || !*mailbox_list) {
6691 EM_DEBUG_EXCEPTION(" mailbox_list[%p], count[%d]", mailbox_list, count);
6693 error = EMAIL_ERROR_INVALID_PARAM;
6697 emstorage_mailbox_tbl_t* p = *mailbox_list;
6700 for (; i < count; i++) {
6701 EM_SAFE_FREE(p[i].mailbox_name);
6702 EM_SAFE_FREE(p[i].alias);
6703 EM_SAFE_FREE(p[i].eas_data); /*valgrind*/
6706 EM_SAFE_FREE(p); *mailbox_list = NULL;
6712 if (err_code != NULL)
6715 EM_DEBUG_FUNC_END("ret [%d]", ret);
6718 /* LCOV_EXCL_START */
6719 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)
6721 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);
6723 if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !count) {
6724 EM_DEBUG_EXCEPTION("Invalid params");
6726 if (err_code != NULL)
6727 *err_code = EMAIL_ERROR_INVALID_PARAM;
6731 int rc = -1, ret = false;
6732 int error = EMAIL_ERROR_NONE;
6733 char sql_query_string[QUERY_SIZE] = {0, };
6734 char *replaced_mailbox_name = NULL;
6736 if (strstr(mailbox_name, "'"))
6737 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6739 replaced_mailbox_name = EM_SAFE_STRDUP(mailbox_name);
6741 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6744 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6745 EMSTORAGE_START_READ_TRANSACTION(transaction);
6746 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);
6747 EM_DEBUG_LOG_SEC(">>> SQL [ %s ] ", sql_query_string);
6751 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6752 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6753 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6755 *count = atoi(result[1]);
6756 sqlite3_free_table(result);
6761 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6763 EM_SAFE_FREE(replaced_mailbox_name);
6765 if (err_code != NULL)
6768 EM_DEBUG_FUNC_END("ret [%d]", ret);
6772 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)
6774 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);
6776 if (account_id < FIRST_ACCOUNT_ID || !uid || !exist) {
6777 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], uid[%p], exist[%p]", account_id, mailbox_name , uid, exist);
6779 if (err_code != NULL)
6780 *err_code = EMAIL_ERROR_INVALID_PARAM;
6784 int rc = -1, ret = false;
6785 int error = EMAIL_ERROR_NONE;
6786 char sql_query_string[QUERY_SIZE] = {0, };
6787 char *replaced_mailbox_name = NULL;
6789 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6790 EMSTORAGE_START_READ_TRANSACTION(transaction);
6793 if (strstr(mailbox_name, "'"))
6794 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6796 replaced_mailbox_name = strdup(mailbox_name);
6798 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);
6800 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);
6803 char **result = NULL;
6805 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6806 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6807 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6809 *exist = atoi(result[1]);
6810 sqlite3_free_table(result);
6820 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6822 EM_SAFE_FREE(replaced_mailbox_name);
6824 if (err_code != NULL)
6827 EM_DEBUG_FUNC_END("ret [%d]", ret);
6831 INTERNAL_FUNC int emstorage_get_downloaded_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code)
6833 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], err_code[%p]", mail_id, mail, err_code);
6835 if (!mail || mail_id <= 0) {
6836 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
6837 if (err_code != NULL)
6838 *err_code = EMAIL_ERROR_INVALID_PARAM;
6842 int rc, ret = false;
6843 int error = EMAIL_ERROR_NONE;
6844 DB_STMT hStmt = NULL;
6845 char sql_query_string[QUERY_SIZE] = {0, };
6847 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6848 EMSTORAGE_START_READ_TRANSACTION(transaction);
6850 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE local_uid = %d", mail_id);
6852 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6853 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6855 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6856 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6859 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6860 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6861 ("sqlite3_step fail:%d", rc));
6863 *mail = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
6864 if (*mail == NULL) {
6865 error = EMAIL_ERROR_OUT_OF_MEMORY;
6866 EM_DEBUG_EXCEPTION("Memory allocation for mail failed.");
6870 memset(*mail, 0x00, sizeof(emstorage_mail_tbl_t));
6872 _get_stmt_field_data_int(hStmt, &((*mail)->account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6873 _get_stmt_field_data_int(hStmt, &((*mail)->mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6874 _get_stmt_field_data_string(hStmt, &((*mail)->server_mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6875 _get_stmt_field_data_int(hStmt, &((*mail)->mail_id), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6876 _get_stmt_field_data_string(hStmt, &((*mail)->server_mail_id), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6877 _get_stmt_field_data_int(hStmt, &((*mail)->mail_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6878 _get_stmt_field_data_char(hStmt, &((*mail)->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6880 (*mail)->server_mail_status = 1;
6886 if (hStmt != NULL) {
6887 rc = sqlite3_finalize(hStmt);
6888 if (rc != SQLITE_OK) {
6889 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6890 error = EMAIL_ERROR_DB_FAILURE;
6894 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6896 if (err_code != NULL)
6899 EM_DEBUG_FUNC_END("ret [%d]", ret);
6903 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)
6905 EM_PROFILE_BEGIN(emStorageGetDownloadList);
6906 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);
6907 if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid || !count) {
6908 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_id[%d], read_mail_uid[%p], count[%p]", account_id, mailbox_id, read_mail_uid, count);
6910 if (err_code != NULL)
6911 *err_code = EMAIL_ERROR_INVALID_PARAM;
6915 int rc, ret = false;
6916 int error = EMAIL_ERROR_NONE;
6918 DB_STMT hStmt = NULL;
6919 char sql_query_string[QUERY_SIZE] = {0, };
6921 emstorage_read_mail_uid_tbl_t* p_data_tbl = NULL;
6924 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6925 EMSTORAGE_START_READ_TRANSACTION(transaction);
6928 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);
6930 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
6932 EM_DEBUG_LOG_SEC(" sql_query_string : %s", sql_query_string);
6936 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6937 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6938 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6939 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6942 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6943 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6944 ("sqlite3_step fail:%d", rc));
6947 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL); */
6948 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL), rc);
6949 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6950 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6952 sqlite3_free_table(result);
6954 EM_DEBUG_LOG("No mail found in mail_read_mail_uid_tbl");
6960 if (!(p_data_tbl = (emstorage_read_mail_uid_tbl_t*)malloc(sizeof(emstorage_read_mail_uid_tbl_t) * *count))) {
6961 EM_DEBUG_EXCEPTION(" malloc failed...");
6962 error = EMAIL_ERROR_OUT_OF_MEMORY;
6966 memset(p_data_tbl, 0x00, sizeof(emstorage_read_mail_uid_tbl_t)*(*count));
6968 for (i = 0; i < *count; ++i) {
6969 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6970 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6971 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6972 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].local_uid), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6973 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].server_uid), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6974 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rfc822_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6975 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6976 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_flagged_field), FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6978 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6979 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6980 ("sqlite3_step fail:%d", rc));
6987 *read_mail_uid = p_data_tbl;
6988 else if (p_data_tbl)
6989 emstorage_free_read_mail_uid(&p_data_tbl, *count, NULL);
6991 if (hStmt != NULL) {
6992 rc = sqlite3_finalize(hStmt);
6993 if (rc != SQLITE_OK) {
6994 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6995 error = EMAIL_ERROR_DB_FAILURE;
6999 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7001 if (err_code != NULL)
7004 EM_PROFILE_END(emStorageGetDownloadList);
7005 EM_DEBUG_FUNC_END("ret [%d]", ret);
7009 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)
7011 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);
7013 if (account_id < FIRST_ACCOUNT_ID || !mail_size) {
7014 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);
7016 if (err_code != NULL)
7017 *err_code = EMAIL_ERROR_INVALID_PARAM;
7021 int rc, ret = false;
7022 int error = EMAIL_ERROR_NONE;
7023 DB_STMT hStmt = NULL;
7024 char sql_query_string[QUERY_SIZE] = {0, };
7025 char *replaced_mailbox_name = NULL;
7027 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7028 EMSTORAGE_START_READ_TRANSACTION(transaction);
7031 if (strstr(mailbox_name, "'"))
7032 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
7034 replaced_mailbox_name = strdup(mailbox_name);
7036 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
7038 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7039 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
7040 "WHERE account_id = %d "
7041 "AND mailbox_id = '%s' "
7042 "AND local_uid = %d "
7043 "AND mailbox_name = '%s' "
7044 "AND server_uid = '%s'",
7045 account_id, mailbox_id, local_uid, replaced_mailbox_name, uid);
7047 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7048 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
7049 "WHERE account_id = %d "
7050 "AND mailbox_id = '%s' "
7051 "AND local_uid = %d "
7052 "AND server_uid = '%s'",
7053 account_id, mailbox_id, local_uid, uid);
7057 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7058 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7059 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7062 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7063 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7064 ("sqlite3_step fail:%d", rc));
7066 if (rc == SQLITE_DONE) {
7067 EM_DEBUG_LOG("no matched mail found....");
7068 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7072 _get_stmt_field_data_int(hStmt, mail_size, 0);
7077 EM_SAFE_FREE(replaced_mailbox_name);
7079 if (hStmt != NULL) {
7080 rc = sqlite3_finalize(hStmt);
7081 if (rc != SQLITE_OK) {
7082 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7083 error = EMAIL_ERROR_DB_FAILURE;
7087 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7089 if (err_code != NULL)
7092 EM_DEBUG_FUNC_END("ret [%d]", ret);
7096 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)
7098 EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], transaction[%d], err_code[%p]", read_mail_uid, transaction, err_code);
7100 if (!read_mail_uid) {
7101 EM_DEBUG_EXCEPTION("read_mail_uid[%p]", read_mail_uid);
7102 if (err_code != NULL)
7103 *err_code = EMAIL_ERROR_INVALID_PARAM;
7107 int rc, rc2, ret = false;
7108 int error = EMAIL_ERROR_NONE;
7109 DB_STMT hStmt = NULL;
7110 char sql_query_string[QUERY_SIZE] = {0, };
7112 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7113 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7115 char *sql = "SELECT max(rowid) FROM mail_read_mail_uid_tbl;";
7116 char **result = NULL;
7119 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7120 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7121 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7123 if (NULL == result[1]) rc = 1;
7124 else rc = atoi(result[1])+1;
7125 sqlite3_free_table(result);
7127 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7128 "INSERT INTO mail_read_mail_uid_tbl VALUES "
7129 "(?" /* account_id */
7130 ", ?" /* mailbox_id */
7131 ", ?" /* mailbox_name */
7132 ", ?" /* local_uid */
7133 ", ?" /* server_uid */
7134 ", ?" /* rfc822_size */
7135 ", ?" /* sync_status */
7136 ", ?" /* flags_seen_field */
7137 ", ?" /* flags_flagged_field */
7141 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
7142 if (rc2 != SQLITE_OK) {
7143 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7144 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
7146 error = EMAIL_ERROR_DB_FAILURE;
7150 EM_DEBUG_LOG("account_id[%d] mailbox_id[%d] local_uid [%d]"
7151 "server_uid[%s] rfc822_size[%d] rc[%d]",
7152 read_mail_uid->account_id, read_mail_uid->mailbox_id, read_mail_uid->local_uid,
7153 read_mail_uid->server_uid, read_mail_uid->rfc822_size, rc);
7155 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->account_id);
7156 _bind_stmt_field_data_int(hStmt, LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->mailbox_id);
7157 _bind_stmt_field_data_int(hStmt, LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->local_uid);
7158 _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);
7159 _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);
7160 _bind_stmt_field_data_int(hStmt, RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->rfc822_size);
7161 _bind_stmt_field_data_int(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_seen_field);
7162 _bind_stmt_field_data_int(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_flagged_field);
7163 _bind_stmt_field_data_int(hStmt, IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, rc);
7166 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7167 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
7168 ("sqlite3_step fail:%d", rc));
7169 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7170 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
7176 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7177 if (hStmt != NULL) {
7178 rc = sqlite3_finalize(hStmt);
7179 if (rc != SQLITE_OK) {
7180 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7181 error = EMAIL_ERROR_DB_FAILURE;
7185 if (err_code != NULL)
7188 EM_DEBUG_FUNC_END("ret [%d]", ret);
7192 #ifdef __FEATURE_BODY_SEARCH__
7193 INTERNAL_FUNC int emstorage_add_mail_text(char *multi_user_name, emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
7195 EM_DEBUG_FUNC_BEGIN("mail_text[%p], transaction[%d], err_code[%p]", mail_text, transaction, err_code);
7198 EM_DEBUG_EXCEPTION("mail_text[%p]", mail_text);
7199 if (err_code != NULL)
7200 *err_code = EMAIL_ERROR_INVALID_PARAM;
7204 int rc, rc2, ret = false;
7205 int error = EMAIL_ERROR_NONE;
7206 DB_STMT hStmt = NULL;
7207 char sql_query_string[QUERY_SIZE] = {0, };
7209 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7210 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7212 char *sql = "SELECT max(rowid) FROM mail_text_tbl;";
7213 char **result = NULL;
7215 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7216 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7217 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7218 sqlite3_free_table(result);
7220 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7221 "INSERT INTO mail_text_tbl VALUES "
7227 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
7228 if (rc2 != SQLITE_OK) {
7229 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7230 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc2, sqlite3_errmsg(local_db_handle));
7232 error = EMAIL_ERROR_DB_FAILURE;
7236 EM_DEBUG_LOG("mail_id[%d] account_id[%d] mailbox_id[%d]", mail_text->mail_id,
7237 mail_text->account_id, mail_text->mailbox_id);
7238 EM_DEBUG_LOG_DEV("body_text VALUE [%s] ", mail_text->body_text);
7240 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mail_id);
7241 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->account_id);
7242 _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mailbox_id);
7243 _bind_stmt_field_data_string(hStmt, BODY_TEXT_IDX_IN_MAIL_TEXT_TBL, (char *)mail_text->body_text, 0, -1);
7245 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7246 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
7247 ("sqlite3_step fail:%d", rc));
7248 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7249 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
7254 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7255 if (hStmt != NULL) {
7256 rc = sqlite3_finalize(hStmt);
7257 if (rc != SQLITE_OK) {
7258 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7259 error = EMAIL_ERROR_DB_FAILURE;
7263 if (err_code != NULL)
7266 EM_DEBUG_FUNC_END("ret [%d]", ret);
7271 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)
7273 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);
7275 if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid) {
7276 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);
7278 if (err_code != NULL)
7279 *err_code = EMAIL_ERROR_INVALID_PARAM;
7283 int rc, ret = false;
7284 int error = EMAIL_ERROR_NONE;
7285 DB_STMT hStmt = NULL;
7286 char sql_query_string[QUERY_SIZE] = {0, };
7288 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7290 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7292 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7293 "UPDATE mail_read_mail_uid_tbl SET"
7296 ", mailbox_name = ?"
7300 ", flags_seen_field = ?"
7301 ", flags_flagged_field = ?"
7302 " WHERE account_id = ?"
7303 " AND mailbox_id = ?"
7304 " AND local_uid = ?"
7305 " AND mailbox_name= ?"
7306 " AND server_uid = ?");
7309 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7310 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7311 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7312 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7317 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->account_id);
7318 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->mailbox_id);
7319 _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7320 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->local_uid);
7321 _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->server_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7322 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->rfc822_size);
7323 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_seen_field);
7324 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_flagged_field);
7325 _bind_stmt_field_data_int(hStmt, i++, account_id);
7326 _bind_stmt_field_data_int(hStmt, i++, mailbox_id);
7327 _bind_stmt_field_data_int(hStmt, i++, local_uid);
7328 _bind_stmt_field_data_string(hStmt, i++, (char*)mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7329 _bind_stmt_field_data_string(hStmt, i++, (char*)uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7332 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7333 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7334 ("sqlite3_step fail:%d", rc));
7339 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7340 if (hStmt != NULL) {
7341 rc = sqlite3_finalize(hStmt);
7342 if (rc != SQLITE_OK) {
7343 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7344 error = EMAIL_ERROR_DB_FAILURE;
7348 if (err_code != NULL)
7351 EM_DEBUG_FUNC_END("ret [%d]", ret);
7355 INTERNAL_FUNC int emstorage_remove_downloaded_mail(char *multi_user_name,
7363 EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], mailbox_id[%d], mailbox_name[%s], "
7364 "uid[%s], transaction[%d], err_code[%p]",
7365 account_id, mailbox_id, mailbox_name, uid, transaction, err_code);
7367 if (account_id < FIRST_ACCOUNT_ID) {
7368 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_name[%s], uid[%s]", account_id, mailbox_name, uid);
7370 if (err_code != NULL)
7371 *err_code = EMAIL_ERROR_INVALID_PARAM;
7376 int error = EMAIL_ERROR_NONE;
7377 char sql_query_string[QUERY_SIZE] = {0, };
7378 char *replaced_mailbox_name = NULL;
7379 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7381 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7383 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7384 "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d ", account_id);
7386 if (mailbox_id > 0) {
7387 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7388 sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7389 "AND mailbox_id = %d ", mailbox_id);
7392 if (mailbox_name) { /* NULL means all mailbox_name */
7393 if (strstr(mailbox_name, "'"))
7394 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
7396 replaced_mailbox_name = strdup(mailbox_name);
7398 SNPRINTF(sql_query_string+EM_SAFE_STRLEN(sql_query_string),
7399 sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7400 "AND mailbox_name = '%s' ", replaced_mailbox_name);
7403 if (uid) { /* NULL means all mail */
7404 sqlite3_snprintf(sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7405 sql_query_string + EM_SAFE_STRLEN(sql_query_string), "AND server_uid = '%q' ",
7409 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7410 if (error != EMAIL_ERROR_NONE) {
7411 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7418 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7420 EM_SAFE_FREE(replaced_mailbox_name);
7422 if (err_code != NULL)
7425 EM_DEBUG_FUNC_END("ret [%d]", ret);
7429 INTERNAL_FUNC int emstorage_free_read_mail_uid(emstorage_read_mail_uid_tbl_t** read_mail_uid, int count, int *err_code)
7431 EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], count[%d], err_code[%p]", read_mail_uid, count, err_code);
7434 int error = EMAIL_ERROR_NONE;
7437 if (!read_mail_uid || !*read_mail_uid) {
7438 EM_DEBUG_EXCEPTION(" read_mail_uid[%p], count[%d]", read_mail_uid, count);
7440 error = EMAIL_ERROR_INVALID_PARAM;
7444 emstorage_read_mail_uid_tbl_t* p = *read_mail_uid;
7447 for (i = 0; i < count; i++) {
7448 EM_SAFE_FREE(p[i].mailbox_name);
7449 EM_SAFE_FREE(p[i].server_uid);
7452 EM_SAFE_FREE(p); *read_mail_uid = NULL;
7458 if (err_code != NULL)
7461 EM_DEBUG_FUNC_END("ret [%d]", ret);
7465 INTERNAL_FUNC int emstorage_get_rule_count_by_account_id(char *multi_user_name, int account_id, int *count, int transaction, int *err_code)
7467 EM_DEBUG_FUNC_BEGIN("account_id [%d], count[%p], transaction[%d], err_code[%p]", count, transaction, err_code);
7470 EM_DEBUG_EXCEPTION("count[%p]", count);
7472 if (err_code != NULL)
7473 *err_code = EMAIL_ERROR_INVALID_PARAM;
7477 int rc = -1, ret = false;
7478 int error = EMAIL_ERROR_NONE;
7479 char sql_query_string[QUERY_SIZE] = {0, };
7481 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7482 EMSTORAGE_START_READ_TRANSACTION(transaction);
7484 if (account_id != ALL_ACCOUNT)
7485 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl where account_id = %d", account_id);
7487 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl");
7491 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7492 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7493 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7495 *count = atoi(result[1]);
7496 sqlite3_free_table(result);
7501 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7503 if (err_code != NULL)
7506 EM_DEBUG_FUNC_END("ret [%d]", ret);
7510 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)
7512 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);
7514 if (!select_num || !is_completed || !rule_list) { /* only global rule supported. */
7515 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);
7517 if (err_code != NULL)
7518 *err_code = EMAIL_ERROR_INVALID_PARAM;
7523 int error = EMAIL_ERROR_NONE;
7525 emstorage_rule_tbl_t* p_data_tbl = NULL;
7526 int i = 0, count = 0;
7527 DB_STMT hStmt = NULL;
7528 char sql_query_string[QUERY_SIZE] = {0, };
7532 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7533 EMSTORAGE_START_READ_TRANSACTION(transaction);
7535 if (account_id != ALL_ACCOUNT) {
7537 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d AND type = %d", account_id, type);
7539 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d ORDER BY type", account_id);
7542 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE type = %d", type);
7544 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl ORDER BY type");
7547 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7548 /* EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt); */
7549 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7550 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7553 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7554 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7555 ("sqlite3_step fail:%d", rc));
7558 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL); */
7559 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
7560 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7561 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7563 sqlite3_free_table(result);
7566 EM_DEBUG_LOG_DEV("No matching rule found...");
7568 error = EMAIL_ERROR_FILTER_NOT_FOUND; /*there is no matched rule*/
7573 if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t) * count))) {
7574 EM_DEBUG_EXCEPTION(" malloc failed...");
7576 error = EMAIL_ERROR_OUT_OF_MEMORY;
7580 memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t) * count);
7582 for (i = 0; i < count; i++) {
7583 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7584 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7585 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7586 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].type), TYPE_IDX_IN_MAIL_RULE_TBL);
7587 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7588 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7589 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7590 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7591 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7592 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7594 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7595 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7596 ("sqlite3_step fail:%d", rc));
7603 EM_DEBUG_LOG("[%d] rules found.", count);
7606 *rule_list = p_data_tbl;
7607 *select_num = count;
7608 } else if (p_data_tbl != NULL)
7609 emstorage_free_rule(&p_data_tbl, count, NULL); /* CID FIX */
7611 if (hStmt != NULL) {
7612 rc = sqlite3_finalize(hStmt);
7613 if (rc != SQLITE_OK) {
7614 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7615 error = EMAIL_ERROR_DB_FAILURE;
7619 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7621 if (err_code != NULL)
7624 EM_DEBUG_FUNC_END("ret [%d]", ret);
7628 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)
7630 EM_DEBUG_FUNC_BEGIN("rule_id[%d], rule[%p], transaction[%d], err_code[%p]", rule_id, rule, transaction, err_code);
7631 int error = EMAIL_ERROR_NONE;
7633 DB_STMT hStmt = NULL;
7636 EM_DEBUG_EXCEPTION("Invalid parameter");
7637 error = EMAIL_ERROR_INVALID_PARAM;
7642 EM_DEBUG_EXCEPTION("rule_id[%d], rule[%p]", rule_id, rule);
7643 error = EMAIL_ERROR_INVALID_PARAM;
7647 emstorage_rule_tbl_t* p_data_tbl = NULL;
7650 char sql_query_string[QUERY_SIZE] = {0, };
7651 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7652 EMSTORAGE_START_READ_TRANSACTION(transaction);
7654 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7656 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7657 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7658 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7661 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7662 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7663 ("sqlite3_step fail:%d", rc));
7665 if (rc == SQLITE_DONE) {
7666 EM_DEBUG_EXCEPTION(" no matched rule found...");
7667 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7671 if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t)))) {
7672 EM_DEBUG_EXCEPTION(" malloc failed...");
7673 error = EMAIL_ERROR_OUT_OF_MEMORY;
7677 memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t));
7678 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7679 _get_stmt_field_data_int(hStmt, &(p_data_tbl->rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7680 _get_stmt_field_data_string(hStmt, &(p_data_tbl->filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7681 _get_stmt_field_data_int(hStmt, &(p_data_tbl->type), TYPE_IDX_IN_MAIL_RULE_TBL);
7682 _get_stmt_field_data_string(hStmt, &(p_data_tbl->value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7683 _get_stmt_field_data_string(hStmt, &(p_data_tbl->value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7684 _get_stmt_field_data_int(hStmt, &(p_data_tbl->action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7685 _get_stmt_field_data_int(hStmt, &(p_data_tbl->target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7686 _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7687 _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7696 if (hStmt != NULL) {
7697 rc = sqlite3_finalize(hStmt);
7698 if (rc != SQLITE_OK) {
7699 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7700 error = EMAIL_ERROR_DB_FAILURE;
7704 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7706 if (err_code != NULL)
7709 EM_DEBUG_FUNC_END("ret [%d]", ret);
7713 INTERNAL_FUNC int emstorage_change_rule(char *multi_user_name, int rule_id, emstorage_rule_tbl_t* new_rule, int transaction, int *err_code)
7715 EM_DEBUG_FUNC_BEGIN("rule_id[%d], new_rule[%p], transaction[%d], err_code[%p]", rule_id, new_rule, transaction, err_code);
7717 if (!new_rule) { /* only global rule supported. */
7718 EM_DEBUG_EXCEPTION("rule_id[%d], new_rule[%p]", rule_id, new_rule);
7720 if (err_code != NULL)
7721 *err_code = EMAIL_ERROR_INVALID_PARAM;
7725 int rc, ret = false;
7726 int error = EMAIL_ERROR_NONE;
7728 DB_STMT hStmt = NULL;
7729 char sql_query_string[QUERY_SIZE] = {0, };
7730 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7731 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7733 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7734 "UPDATE mail_rule_tbl SET"
7740 ", target_mailbox_id = ?"
7745 " WHERE rule_id = %d"
7749 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7750 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
7751 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7752 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7756 _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->filter_name, 0, FILTER_NAME_LEN_IN_MAIL_RULE_TBL);
7757 _bind_stmt_field_data_int(hStmt, i++, new_rule->type);
7758 _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7759 _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7760 _bind_stmt_field_data_int(hStmt, i++, new_rule->action_type);
7761 _bind_stmt_field_data_int(hStmt, i++, new_rule->target_mailbox_id);
7762 _bind_stmt_field_data_int(hStmt, i++, new_rule->flag1);
7763 _bind_stmt_field_data_int(hStmt, i++, new_rule->flag2);
7764 _bind_stmt_field_data_int(hStmt, i++, new_rule->account_id);
7765 _bind_stmt_field_data_int(hStmt, i++, rule_id);
7768 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7769 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7770 ("sqlite3_step fail:%d", rc));
7775 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7776 if (hStmt != NULL) {
7777 rc = sqlite3_finalize(hStmt);
7778 if (rc != SQLITE_OK) {
7779 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7780 error = EMAIL_ERROR_DB_FAILURE;
7784 if (err_code != NULL)
7787 EM_DEBUG_FUNC_END("ret [%d]", ret);
7791 INTERNAL_FUNC int emstorage_find_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7793 EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7796 EM_DEBUG_LOG("rule is NULL");
7797 if (err_code != NULL)
7798 *err_code = EMAIL_ERROR_INVALID_PARAM;
7802 DB_STMT hStmt = NULL;
7803 char sql_query_string[QUERY_SIZE] = {0,};
7804 int error = EMAIL_ERROR_NONE;
7808 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7809 EMSTORAGE_START_READ_TRANSACTION(transaction);
7811 switch (rule->action_type) {
7812 case EMAIL_FILTER_MOVE:
7813 if (rule->type == EMAIL_PRIORITY_SENDER) {
7814 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7815 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7816 rule->action_type, rule->type, rule->value2);
7818 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7819 "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);
7822 case EMAIL_FILTER_BLOCK:
7823 if (rule->type == EMAIL_FILTER_FROM)
7824 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7825 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7826 rule->action_type, rule->type, rule->value2);
7827 else if (rule->type == EMAIL_FILTER_SUBJECT)
7828 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7829 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value) = UPPER(\'%q\')",
7830 rule->action_type, rule->type, rule->value);
7831 else if (rule->type == (EMAIL_FILTER_SUBJECT | EMAIL_FILTER_FROM))
7832 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7833 "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\'))",
7834 rule->action_type, EMAIL_FILTER_SUBJECT, rule->value, EMAIL_FILTER_FROM, rule->value2);
7838 EM_DEBUG_EXCEPTION("Invalid parameter : rule->action_type[%d]", rule->action_type);
7839 error = EMAIL_ERROR_INVALID_PARAM;
7844 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7845 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7846 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7848 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7849 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7850 ("sqlite3_step fail:%d", rc));
7852 if (rc == SQLITE_DONE) {
7853 EM_DEBUG_EXCEPTION(" no matched rule found...");
7854 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7861 if (hStmt != NULL) {
7862 rc = sqlite3_finalize(hStmt);
7863 if (rc != SQLITE_OK) {
7864 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7865 error = EMAIL_ERROR_DB_FAILURE;
7869 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7874 EM_DEBUG_FUNC_END("ret [%d]", ret);
7878 INTERNAL_FUNC int emstorage_add_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7880 EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7882 if (!rule) { /* only global rule supported. */
7883 EM_DEBUG_LOG("rule is NULL");
7884 if (err_code != NULL)
7885 *err_code = EMAIL_ERROR_INVALID_PARAM;
7889 int rc, rc_2, ret = false;
7890 int error = EMAIL_ERROR_NONE;
7891 DB_STMT hStmt = NULL;
7892 char sql_query_string[QUERY_SIZE] = {0, };
7894 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7896 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7900 sql = "SELECT max(rowid) FROM mail_rule_tbl;";
7902 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7903 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7904 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7906 if (NULL == result[1])
7909 rc = atoi(result[1])+1;
7911 sqlite3_free_table(result);
7915 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7916 "INSERT INTO mail_rule_tbl VALUES "
7917 "(?" /* account id */
7919 ", ?" /* filter_name */
7923 ", ?" /* action_type */
7924 ", ?" /* target_mailbox_id */
7926 ", ?)"); /* flag2 */
7928 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc_2);
7929 if (rc_2 != SQLITE_OK) {
7930 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc_2, sqlite3_errmsg(local_db_handle));
7931 error = EMAIL_ERROR_DB_FAILURE;
7935 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL, rule->account_id);
7936 _bind_stmt_field_data_int(hStmt, RULE_ID_IDX_IN_MAIL_RULE_TBL, rule->rule_id);
7937 _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);
7938 _bind_stmt_field_data_int(hStmt, TYPE_IDX_IN_MAIL_RULE_TBL, rule->type);
7939 _bind_stmt_field_data_string(hStmt, VALUE_IDX_IN_MAIL_RULE_TBL, (char*)rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7940 _bind_stmt_field_data_string(hStmt, VALUE2_IDX_IN_MAIL_RULE_TBL, (char*)rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7941 _bind_stmt_field_data_int(hStmt, ACTION_TYPE_IDX_IN_MAIL_RULE_TBL, rule->action_type);
7942 _bind_stmt_field_data_int(hStmt, TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL, rule->target_mailbox_id);
7943 _bind_stmt_field_data_int(hStmt, FLAG1_IDX_IN_MAIL_RULE_TBL, rule->flag1);
7944 _bind_stmt_field_data_int(hStmt, FLAG2_IDX_IN_MAIL_RULE_TBL, rule->flag2);
7946 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7947 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7948 ("sqlite3_step fail:%d", rc));
7953 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7954 if (hStmt != NULL) {
7955 rc = sqlite3_finalize(hStmt);
7956 if (rc != SQLITE_OK) {
7957 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7958 error = EMAIL_ERROR_DB_FAILURE;
7962 if (err_code != NULL)
7965 EM_DEBUG_FUNC_END("ret [%d]", ret);
7969 INTERNAL_FUNC int emstorage_delete_rule(char *multi_user_name, int rule_id, int transaction, int *err_code)
7971 EM_DEBUG_FUNC_BEGIN("rule_id[%d], transaction[%d], err_code[%p]", rule_id, transaction, err_code);
7973 if (rule_id <= 0) { /* only global rule supported. */
7974 EM_DEBUG_EXCEPTION("rule_id[%d]", rule_id);
7976 if (err_code != NULL)
7977 *err_code = EMAIL_ERROR_INVALID_PARAM;
7981 int rc, ret = false;
7982 int error = EMAIL_ERROR_NONE;
7983 char sql_query_string[QUERY_SIZE] = {0, };
7984 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7985 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7987 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7988 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7989 if (error != EMAIL_ERROR_NONE) {
7990 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7994 rc = sqlite3_changes(local_db_handle);
7996 EM_DEBUG_EXCEPTION(" no matched rule found...");
7998 error = EMAIL_ERROR_FILTER_NOT_FOUND;
8005 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
8007 if (err_code != NULL)
8010 EM_DEBUG_FUNC_END("ret [%d]", ret);
8014 INTERNAL_FUNC int emstorage_free_rule(emstorage_rule_tbl_t** rule_list, int count, int *err_code)
8016 EM_DEBUG_FUNC_BEGIN("rule_list[%p], conut[%d], err_code[%p]", rule_list, count, err_code);
8019 int error = EMAIL_ERROR_NONE;
8022 if (!rule_list || !*rule_list) {
8023 EM_DEBUG_EXCEPTION(" rule_list[%p], conut[%d]", rule_list, count);
8025 error = EMAIL_ERROR_INVALID_PARAM;
8029 emstorage_rule_tbl_t* p = *rule_list;
8032 for (; i < count; i++)
8033 EM_SAFE_FREE(p[i].value);
8035 EM_SAFE_FREE(p); *rule_list = NULL;
8039 if (err_code != NULL)
8042 EM_DEBUG_FUNC_END("ret [%d]", ret);
8046 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)
8048 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);
8050 if (!total && !unseen) {
8051 EM_DEBUG_EXCEPTION(" accoun_id[%d], mailbox_id[%d], total[%p], unseen[%p]", account_id, mailbox_id, total, unseen);
8052 if (err_code != NULL)
8053 *err_code = EMAIL_ERROR_INVALID_PARAM;
8057 int rc = -1, ret = false;
8058 int error = EMAIL_ERROR_NONE;
8059 DB_STMT hStmt = NULL;
8060 char sql_query_string[QUERY_SIZE] = {0, };
8061 char *replaced_mailbox_name = NULL;
8063 memset(&sql_query_string, 0x00, sizeof(sql_query_string));
8064 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8065 EMSTORAGE_START_READ_TRANSACTION(transaction);
8068 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl");
8070 if (account_id != ALL_ACCOUNT) {
8071 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);
8073 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);
8074 } else if (mailbox_id)
8075 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);
8077 #ifdef USE_GET_RECORD_COUNT_API
8080 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
8081 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF2; },
8082 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8084 *total = atoi(result[1]);
8085 sqlite3_free_table(result);
8088 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8089 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF2; },
8090 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8092 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8093 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF2; },
8094 ("sqlite3_step fail:%d", rc));
8095 _get_stmt_field_data_int(hStmt, total, 0);
8096 #endif /* USE_GET_RECORD_COUNT_API */
8100 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl WHERE flags_seen_field = 0"); /* fSEEN = 0x01 */
8102 if (account_id != ALL_ACCOUNT) {
8103 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
8104 sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND account_id = %d", account_id);
8108 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
8109 sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND mailbox_id = %d", mailbox_id);
8111 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
8112 sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND mailbox_type NOT IN (3, 5)");
8115 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
8116 EM_DEBUG_DB_EXEC(SQLITE_OK != rc,
8117 { error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
8118 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8120 *unseen = atoi(result[1]);
8121 sqlite3_free_table(result);
8129 #ifndef USE_PREPARED_QUERY_
8130 if (hStmt != NULL) {
8131 rc = sqlite3_finalize(hStmt);
8132 if (rc != SQLITE_OK) {
8133 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8134 error = EMAIL_ERROR_DB_FAILURE;
8139 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8141 EM_SAFE_FREE(replaced_mailbox_name);
8143 if (err_code != NULL)
8146 EM_DEBUG_FUNC_END("ret [%d]", ret);
8150 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)
8152 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
8154 if (mail_id <= 0 || !mail) {
8155 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
8156 if (err_code != NULL)
8157 *err_code = EMAIL_ERROR_INVALID_PARAM;
8162 emstorage_mail_tbl_t* p_data_tbl = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
8164 if (p_data_tbl == NULL) {
8165 EM_DEBUG_EXCEPTION("malloc failed...");
8166 if (err_code != NULL)
8167 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8171 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8172 DB_STMT hStmt = NULL;
8173 char sql_query_string[QUERY_SIZE] = {0, };
8175 int rc, ret = false;
8176 int error = EMAIL_ERROR_NONE;
8178 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8179 EMSTORAGE_START_READ_TRANSACTION(transaction);
8182 case RETRIEVE_SUMMARY:
8183 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8184 "SELECT account_id, "
8187 "server_mail_status, "
8188 "server_mailbox_name, "
8192 "file_path_mime_entity, "
8193 "flags_seen_field, "
8197 "thread_item_count "
8198 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8201 case RETRIEVE_FIELDS_FOR_DELETE:
8202 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8203 "SELECT account_id, "
8205 "server_mail_status, "
8206 "server_mailbox_name, "
8208 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8211 case RETRIEVE_ACCOUNT:
8212 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8213 "SELECT account_id "
8214 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8218 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8219 "SELECT account_id, "
8220 "flags_seen_field, "
8223 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8227 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
8228 error = EMAIL_ERROR_INVALID_PARAM;
8232 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
8234 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8235 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8236 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8239 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8240 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8241 ("sqlite3_step fail:%d", rc));
8243 if (rc == SQLITE_DONE) {
8244 EM_DEBUG_LOG("no matched mail found...");
8245 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8249 case RETRIEVE_SUMMARY:
8250 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8251 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
8252 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
8253 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
8254 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
8255 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8256 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, col_index++);
8257 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, col_index++);
8258 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, col_index++);
8259 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8260 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), col_index++);
8261 _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), col_index++);
8262 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8263 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_item_count), col_index++);
8266 case RETRIEVE_FIELDS_FOR_DELETE:
8267 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8268 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
8269 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
8270 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
8271 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8274 case RETRIEVE_ACCOUNT:
8275 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8279 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8280 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8281 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8282 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
8291 else if (p_data_tbl != NULL)
8292 emstorage_free_mail(&p_data_tbl, 1, NULL);
8294 if (hStmt != NULL) {
8295 rc = sqlite3_finalize(hStmt);
8296 if (rc != SQLITE_OK) {
8297 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8298 error = EMAIL_ERROR_DB_FAILURE;
8303 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8305 if (err_code != NULL)
8308 EM_DEBUG_FUNC_END("ret [%d]", ret);
8312 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)
8314 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);
8317 int error = EMAIL_ERROR_NONE;
8318 int query_string_length = 0;
8319 int i = 0, item_count = 0, rc = -1, field_count, col_index, cur_sql_query_string = 0;
8320 char **result = NULL;
8321 char *sql_query_string = NULL;
8322 emstorage_mail_tbl_t* p_data_tbl = NULL;
8323 sqlite3 *local_db_handle = NULL;
8325 if (number_of_mails <= 0 || !mail_ids) {
8326 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8327 if (err_code != NULL)
8328 *err_code = EMAIL_ERROR_INVALID_PARAM;
8332 p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * number_of_mails);
8334 query_string_length = (sizeof(char) * 8 * number_of_mails) + 512;
8335 sql_query_string = (char*)em_malloc(query_string_length);
8337 if (p_data_tbl == NULL || sql_query_string == NULL) {
8338 EM_DEBUG_EXCEPTION("malloc failed...");
8340 EM_SAFE_FREE(p_data_tbl);
8341 EM_SAFE_FREE(sql_query_string);
8343 if (err_code != NULL)
8344 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8348 local_db_handle = emstorage_get_db_connection(multi_user_name);
8350 EMSTORAGE_START_READ_TRANSACTION(transaction);
8353 case RETRIEVE_SUMMARY:
8354 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8355 "SELECT account_id, "
8358 "server_mail_status, "
8359 "server_mailbox_name, "
8363 "file_path_mime_entity, "
8365 "flags_seen_field, "
8369 "thread_item_count "
8370 "FROM mail_tbl WHERE mail_id in (");
8374 case RETRIEVE_FIELDS_FOR_DELETE:
8375 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8376 "SELECT account_id, "
8378 "server_mail_status, "
8379 "server_mailbox_name, "
8381 "FROM mail_tbl WHERE mail_id in (");
8385 case RETRIEVE_ACCOUNT:
8386 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8387 "SELECT account_id FROM mail_tbl WHERE mail_id in (");
8392 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8393 "SELECT account_id, "
8396 "flags_seen_field, "
8398 "FROM mail_tbl WHERE mail_id in (");
8403 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
8404 error = EMAIL_ERROR_INVALID_PARAM;
8408 for (i = 0; i < number_of_mails; i++)
8409 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_string_length, "%d,", mail_ids[i]);
8410 sql_query_string[EM_SAFE_STRLEN(sql_query_string) - 1] = ')';
8412 EM_DEBUG_LOG_SEC("Query [%s], Length [%zu]", sql_query_string, EM_SAFE_STRLEN(sql_query_string));
8414 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &item_count, 0, NULL), rc);
8415 if (SQLITE_OK != rc && -1 != rc) {
8416 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
8417 error = EMAIL_ERROR_DB_FAILURE;
8420 EM_DEBUG_LOG("item_count [%d]", item_count);
8422 if (number_of_mails != item_count) {
8423 EM_DEBUG_EXCEPTION("Can't find all emails");
8424 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8428 col_index = field_count;
8430 for (i = 0; i < item_count; i++) {
8432 case RETRIEVE_SUMMARY:
8433 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8434 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8435 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8436 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8437 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8438 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8439 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
8440 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
8441 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
8442 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 0, col_index++);
8443 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8444 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
8445 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
8446 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8447 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
8450 case RETRIEVE_FIELDS_FOR_DELETE:
8451 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8452 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8453 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8454 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8455 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8458 case RETRIEVE_ACCOUNT:
8459 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8463 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8464 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8465 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8466 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8467 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8478 emstorage_free_mail(&p_data_tbl, number_of_mails, NULL);
8481 sqlite3_free_table(result);
8483 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8485 EM_SAFE_FREE(sql_query_string);
8487 if (err_code != NULL)
8490 EM_DEBUG_FUNC_END("ret [%d]", ret);
8493 /* LCOV_EXCL_STOP */
8494 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)
8496 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8498 if (mail_id <= 0 || !mail) {
8499 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
8500 if (err_code != NULL)
8501 *err_code = EMAIL_ERROR_INVALID_PARAM;
8505 int ret = false, error = EMAIL_ERROR_NONE, count;
8506 char conditional_clause[QUERY_SIZE] = {0, };
8507 emstorage_mail_tbl_t* p_data_tbl = NULL;
8509 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8510 EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8512 if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8513 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8522 else if (p_data_tbl != NULL)
8523 emstorage_free_mail(&p_data_tbl, 1, &error);
8525 if (err_code != NULL)
8528 EM_DEBUG_FUNC_END("ret [%d]", ret);
8532 #ifdef __FEATURE_BODY_SEARCH__
8533 /* LCOV_EXCL_START */
8534 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)
8536 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
8538 if (mail_id <= 0 || !mail_text) {
8539 EM_DEBUG_EXCEPTION("mail_id[%d], mail_text[%p]", mail_id, mail_text);
8540 if (err_code != NULL)
8541 *err_code = EMAIL_ERROR_INVALID_PARAM;
8546 int error = EMAIL_ERROR_NONE;
8548 char conditional_clause[QUERY_SIZE] = {0, };
8549 emstorage_mail_text_tbl_t *p_data_tbl = NULL;
8551 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8552 EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8554 if (!emstorage_query_mail_text_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8555 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8563 *mail_text = p_data_tbl;
8564 else if (p_data_tbl != NULL)
8565 emstorage_free_mail_text(&p_data_tbl, 1, &error);
8567 if (err_code != NULL)
8570 EM_DEBUG_FUNC_END("ret [%d]", ret);
8575 INTERNAL_FUNC int emstorage_mail_search_start(char *multi_user_name,
8576 emstorage_search_filter_t *search,
8580 DB_STMT *search_handle,
8585 EM_DEBUG_FUNC_BEGIN("search[%p], account_id[%d], mailbox_id[%d], sorting[%d], "
8586 "search_handle[%p], searched[%p], transaction[%d], err_code[%p]",
8587 search, account_id, mailbox_id, sorting, search_handle,
8588 searched, transaction, err_code);
8590 if (!search_handle || !searched) {
8591 if (err_code != NULL)
8592 *err_code = EMAIL_ERROR_INVALID_PARAM;
8593 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8594 EM_DEBUG_FUNC_END("false");
8598 emstorage_search_filter_t* p = search;
8599 int error = EMAIL_ERROR_NONE;
8600 DB_STMT hStmt = NULL;
8601 char sql_query_string[QUERY_SIZE] = {0, };
8602 int rc, ret = false;
8603 int and = false, mail_count = 0;
8605 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8606 EMSTORAGE_START_READ_TRANSACTION(transaction);
8608 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl");
8610 if (account_id != ALL_ACCOUNT) {
8611 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);
8616 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);
8622 if (!strncmp(p->key_type, "subject", strlen("subject"))) {
8623 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);
8625 } else if (!strncmp(p->key_type, "full_address_from", strlen("full_address_from"))) {
8626 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);
8628 } else if (!strncmp(p->key_type, "full_address_to", strlen("full_address_to"))) {
8629 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);
8631 } else if (!strncmp(p->key_type, "email_address", strlen("email_address"))) {
8632 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);
8640 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");
8642 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
8645 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8646 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8647 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8650 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8651 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8652 ("sqlite3_step fail:%d", rc));
8656 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &mail_count, NULL, NULL), rc);
8657 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
8658 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8660 sqlite3_free_table(result);
8666 *search_handle = hStmt;
8667 *searched = mail_count;
8668 EM_DEBUG_LOG("mail_count [%d]", mail_count);
8670 if (hStmt != NULL) {
8671 rc = sqlite3_finalize(hStmt);
8672 if (rc != SQLITE_OK) {
8673 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8674 error = EMAIL_ERROR_DB_FAILURE;
8678 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8681 if (err_code != NULL)
8684 EM_DEBUG_FUNC_END("ret [%d]", ret);
8688 INTERNAL_FUNC int emstorage_mail_search_result(DB_STMT search_handle, emstorage_mail_field_type_t type, void** data, int transaction, int *err_code)
8690 EM_DEBUG_FUNC_BEGIN("search_handle[%d], type[%d], data[%p], transaction[%d], err_code[%p]", search_handle, type, data, transaction, err_code);
8692 if (search_handle == 0 || !data) {
8693 EM_DEBUG_EXCEPTION("No Search_handle type[%d], data[%p]", type, data);
8695 if (err_code != NULL)
8696 *err_code = EMAIL_ERROR_INVALID_PARAM;
8700 emstorage_mail_tbl_t* p_data_tbl = NULL;
8701 DB_STMT hStmt = search_handle;
8702 int rc, ret = false;
8703 int error = EMAIL_ERROR_NONE;
8707 _get_stmt_field_data_int(hStmt, (int *)data, MAIL_ID_IDX_IN_MAIL_TBL);
8710 case RETRIEVE_ENVELOPE:
8712 if (!(p_data_tbl = em_malloc(sizeof(emstorage_mail_tbl_t)))) {
8713 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
8714 error = EMAIL_ERROR_OUT_OF_MEMORY;
8718 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8719 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8720 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_size), MAIL_SIZE_IDX_IN_MAIL_TBL);
8721 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8722 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_from), 1, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL);
8723 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_to), 1, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL);
8724 _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject), 1, SUBJECT_IDX_IN_MAIL_TBL);
8725 _get_stmt_field_data_int(hStmt, &(p_data_tbl->body_download_status), BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL);
8726 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL);
8727 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8728 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8729 _get_stmt_field_data_time_t(hStmt, &(p_data_tbl->date_time), DATETIME_IDX_IN_MAIL_TBL);
8730 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL);
8731 _get_stmt_field_data_int(hStmt, &(p_data_tbl->DRM_status), DRM_STATUS_IDX_IN_MAIL_TBL);
8732 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->priority), PRIORITY_IDX_IN_MAIL_TBL);
8733 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), SAVE_STATUS_IDX_IN_MAIL_TBL);
8734 _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), LOCK_STATUS_IDX_IN_MAIL_TBL);
8735 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->report_status), REPORT_STATUS_IDX_IN_MAIL_TBL);
8736 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8737 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->meeting_request_status), MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL);
8738 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->message_class), MESSAGE_CLASS_IDX_IN_MAIL_TBL);
8739 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->digest_type), DIGEST_TYPE_IDX_IN_MAIL_TBL);
8740 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->smime_type), SMIME_TYPE_IDX_IN_MAIL_TBL);
8741 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->scheduled_sending_time), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8742 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->remaining_resend_times), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8743 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->tag_id), TAG_ID_IDX_IN_MAIL_TBL);
8744 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->eas_data_length), EAS_DATA_LENGTH_IDX_IN_MAIL_TBL);
8745 _get_stmt_field_data_blob(hStmt, (void**)&(p_data_tbl->eas_data), EAS_DATA_IDX_IN_MAIL_TBL);
8747 if (type == RETRIEVE_ALL) {
8748 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8749 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8750 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_reply), 1, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL);
8751 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_cc), 1, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL);
8752 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_bcc), 1, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL);
8753 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_return), 1, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL);
8754 _get_stmt_field_data_string(hStmt, &(p_data_tbl->message_id), 0, MESSAGE_ID_IDX_IN_MAIL_TBL);
8755 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8756 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8757 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_count), ATTACHMENT_COUNT_IDX_IN_MAIL_TBL);
8758 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8759 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->replied_time), REPLIED_TIME_IDX_IN_MAIL_TBL);
8760 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->forwarded_time), FORWARDED_TIME_IDX_IN_MAIL_TBL);
8761 _get_stmt_field_data_string(hStmt, &(p_data_tbl->default_charset), 0, DEFAULT_CHARSET_IDX_IN_MAIL_TBL);
8764 if (p_data_tbl->body_download_status) {
8767 if (p_data_tbl->file_path_html) {
8768 if (stat(p_data_tbl->file_path_html, &buf) == -1)
8769 p_data_tbl->body_download_status = 0;
8770 } else if (p_data_tbl->file_path_plain) {
8771 if (stat(p_data_tbl->file_path_plain, &buf) == -1)
8772 p_data_tbl->body_download_status = 0;
8774 p_data_tbl->body_download_status = 0;
8777 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8780 case RETRIEVE_SUMMARY:
8781 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
8782 EM_DEBUG_EXCEPTION(" malloc failed...");
8784 error = EMAIL_ERROR_OUT_OF_MEMORY;
8788 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8790 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8791 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8792 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8793 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8794 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8796 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8799 case RETRIEVE_ADDRESS:
8800 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
8801 EM_DEBUG_EXCEPTION(" malloc failed...");
8802 error = EMAIL_ERROR_OUT_OF_MEMORY;
8806 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8807 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8808 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8809 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8810 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8817 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8818 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8819 ("sqlite3_step fail:%d", rc));
8825 if (err_code != NULL)
8828 if (ret == false && p_data_tbl)
8829 emstorage_free_mail(&p_data_tbl, 1, NULL);
8831 EM_DEBUG_FUNC_END("ret [%d]", ret);
8835 INTERNAL_FUNC int emstorage_mail_search_end(DB_STMT search_handle, int transaction, int *err_code)
8837 EM_DEBUG_FUNC_BEGIN("search_handle[%d], transaction[%d], err_code[%p]", search_handle, transaction, err_code);
8839 int error = EMAIL_ERROR_NONE;
8840 int rc, ret = false;
8842 if (search_handle == 0) {
8843 EM_DEBUG_EXCEPTION("No search_handle");
8844 error = EMAIL_ERROR_INVALID_PARAM;
8848 DB_STMT hStmt = search_handle;
8850 rc = sqlite3_finalize(hStmt);
8851 if (rc != SQLITE_OK) {
8852 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8853 error = EMAIL_ERROR_DB_FAILURE;
8859 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8861 if (err_code != NULL)
8864 EM_DEBUG_FUNC_END("ret [%d]", ret);
8868 INTERNAL_FUNC int emstorage_change_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t *mail, int transaction, int *err_code)
8870 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8872 if (mail_id <= 0 || !mail) {
8873 EM_DEBUG_EXCEPTION(" mail_id[%d], mail[%p]", mail_id, mail);
8875 if (err_code != NULL)
8876 *err_code = EMAIL_ERROR_INVALID_PARAM;
8880 DB_STMT hStmt = NULL;
8881 char sql_query_string[QUERY_SIZE] = {0, };
8884 int error = EMAIL_ERROR_NONE;
8886 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8887 char mailbox_id_param_string[10] = {0,};
8889 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
8891 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8892 "UPDATE mail_tbl SET"
8897 ", server_mail_status = ?"
8898 ", server_mailbox_name = ?"
8899 ", server_mail_id = ?"
8900 ", reference_mail_id = ?"
8901 ", full_address_from = ?"
8902 ", full_address_reply = ?" /* 10 */
8903 ", full_address_to = ?"
8904 ", full_address_cc = ?"
8905 ", full_address_bcc = ?"
8906 ", full_address_return = ?"
8908 ", body_download_status = ?"
8909 ", file_path_plain = ?"
8910 ", file_path_html = ?"
8911 ", file_path_mime_entity = ?"
8913 ", flags_seen_field = ?"
8914 ", flags_deleted_field = ?"
8915 ", flags_flagged_field = ?"
8916 ", flags_answered_field = ?"
8917 ", flags_recent_field = ?"
8918 ", flags_draft_field = ?"
8919 ", flags_forwarded_field = ?"
8925 ", report_status = ?"
8926 ", preview_text = ?"
8928 ", scheduled_sending_time = ?"
8929 ", remaining_resend_times = ?"
8931 ", replied_time = ?"
8932 ", forwarded_time = ?"
8933 ", default_charset = ?"
8934 ", eas_data_length = ?"
8936 " WHERE mail_id = %d AND account_id != 0 "
8940 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8941 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8942 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8944 _bind_stmt_field_data_int(hStmt, i++, mail->mail_id);
8945 _bind_stmt_field_data_int(hStmt, i++, mail->account_id);
8946 _bind_stmt_field_data_int(hStmt, i++, mail->mailbox_id);
8947 _bind_stmt_field_data_int(hStmt, i++, mail->mail_size);
8948 _bind_stmt_field_data_int(hStmt, i++, mail->server_mail_status);
8949 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mailbox_name, 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
8950 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mail_id, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
8951 _bind_stmt_field_data_int(hStmt, i++, mail->reference_mail_id);
8952 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
8953 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
8954 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
8955 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
8956 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
8957 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
8958 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8959 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
8960 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8961 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8962 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
8963 _bind_stmt_field_data_int(hStmt, i++, mail->date_time);
8964 _bind_stmt_field_data_char(hStmt, i++, mail->flags_seen_field);
8965 _bind_stmt_field_data_char(hStmt, i++, mail->flags_deleted_field);
8966 _bind_stmt_field_data_char(hStmt, i++, mail->flags_flagged_field);
8967 _bind_stmt_field_data_char(hStmt, i++, mail->flags_answered_field);
8968 _bind_stmt_field_data_char(hStmt, i++, mail->flags_recent_field);
8969 _bind_stmt_field_data_char(hStmt, i++, mail->flags_draft_field);
8970 _bind_stmt_field_data_char(hStmt, i++, mail->flags_forwarded_field);
8971 _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
8972 _bind_stmt_field_data_int(hStmt, i++, mail->priority);
8973 _bind_stmt_field_data_int(hStmt, i++, mail->save_status);
8974 _bind_stmt_field_data_int(hStmt, i++, mail->lock_status);
8975 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->message_id, 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
8976 _bind_stmt_field_data_int(hStmt, i++, mail->report_status);
8977 _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8978 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
8979 _bind_stmt_field_data_int(hStmt, i++, mail->scheduled_sending_time);
8980 _bind_stmt_field_data_int(hStmt, i++, mail->remaining_resend_times);
8981 _bind_stmt_field_data_int(hStmt, i++, mail->tag_id);
8982 _bind_stmt_field_data_int(hStmt, i++, mail->replied_time);
8983 _bind_stmt_field_data_int(hStmt, i++, mail->forwarded_time);
8984 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
8985 _bind_stmt_field_data_int(hStmt, i++, mail->eas_data_length);
8986 _bind_stmt_field_data_blob(hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
8988 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8989 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8990 ("sqlite3_step fail:%d", rc));
8992 rc = sqlite3_changes(local_db_handle);
8994 EM_DEBUG_LOG(" no matched mail found...");
8995 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9002 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9003 if (hStmt != NULL) {
9004 rc = sqlite3_finalize(hStmt);
9005 if (rc != SQLITE_OK) {
9006 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9007 error = EMAIL_ERROR_DB_FAILURE;
9012 if (error == EMAIL_ERROR_NONE && mail) {
9013 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
9014 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail->mail_id, mailbox_id_param_string, 0))
9015 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventfailed [NOTI_MAIL_UPDATE]");
9018 if (err_code != NULL)
9021 EM_DEBUG_FUNC_END("ret [%d]", ret);
9026 * emstorage_clean_save_status(int save_status, int *err_code) - set the all mail status to the set value
9030 INTERNAL_FUNC int emstorage_clean_save_status(char *multi_user_name, int save_status, int *err_code)
9032 EM_DEBUG_FUNC_BEGIN("save_status[%d], err_code[%p]", save_status, err_code);
9034 EM_IF_NULL_RETURN_VALUE(err_code, false);
9037 int error = EMAIL_ERROR_NONE;
9039 char sql_query_string[QUERY_SIZE] = {0, };
9040 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9042 memset(sql_query_string, 0x00, sizeof(sql_query_string));
9043 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);
9045 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9046 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9047 if (error != EMAIL_ERROR_NONE) {
9048 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9052 rc = sqlite3_changes(local_db_handle);
9054 EM_DEBUG_LOG(" No Matched Mail Exists ");
9055 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9062 if (err_code != NULL)
9065 EM_DEBUG_FUNC_END("ret [%d]", ret);
9069 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)
9071 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);
9073 int error = EMAIL_ERROR_NONE;
9076 int cur_mail_id_string = 0;
9077 int mail_id_string_buffer_length = 0;
9078 int parameter_string_length = 0;
9079 char *sql_query_string = NULL;
9080 char *mail_id_string_buffer = NULL;
9081 char *parameter_string = NULL;
9082 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9083 email_mail_attribute_type target_mail_attribute_type = 0;
9085 if (!mail_ids || !field_name || account_id == 0) {
9086 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9087 if (err_code != NULL)
9088 *err_code = EMAIL_ERROR_INVALID_PARAM;
9092 if ((error = emcore_get_attribute_type_by_mail_field_name(field_name, &target_mail_attribute_type)) != EMAIL_ERROR_NONE) {
9093 EM_DEBUG_EXCEPTION("emstorageemcore_get_attribute_type_by_mail_field_name failed [%d]", error);
9094 if (err_code != NULL)
9099 /* Generating mail id list string */
9100 mail_id_string_buffer_length = MAIL_ID_STRING_LENGTH * mail_ids_count;
9102 mail_id_string_buffer = em_malloc(mail_id_string_buffer_length);
9104 if (!mail_id_string_buffer) {
9105 EM_DEBUG_EXCEPTION("em_mallocfailed");
9106 if (err_code != NULL)
9107 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
9111 for (i = 0; i < mail_ids_count; i++)
9112 cur_mail_id_string += SNPRINTF_OFFSET(mail_id_string_buffer, cur_mail_id_string, mail_id_string_buffer_length, "%d,", mail_ids[i]);
9114 if (EM_SAFE_STRLEN(mail_id_string_buffer) > 1)
9115 mail_id_string_buffer[EM_SAFE_STRLEN(mail_id_string_buffer) - 1] = NULL_CHAR;
9117 /* Generating notification parameter string */
9118 parameter_string_length = mail_id_string_buffer_length + EM_SAFE_STRLEN(field_name) + 2;
9119 parameter_string = em_malloc(parameter_string_length);
9121 if (!parameter_string) {
9122 EM_DEBUG_EXCEPTION("em_mallocfailed");
9123 if (err_code != NULL)
9124 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
9125 EM_SAFE_FREE(mail_id_string_buffer);
9129 SNPRINTF(parameter_string, parameter_string_length, "%s%c%s", field_name, 0x01, mail_id_string_buffer);
9130 query_size = EM_SAFE_STRLEN(mail_id_string_buffer) + EM_SAFE_STRLEN(field_name) + 250;
9132 sql_query_string = em_malloc(query_size);
9133 if (sql_query_string == NULL) {
9134 EM_DEBUG_EXCEPTION("em_mallocfailed");
9135 error = EMAIL_ERROR_OUT_OF_MEMORY;
9138 /* Write query string */
9139 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);
9141 EM_DEBUG_LOG_DEV("sql_query_string [%s]", sql_query_string);
9144 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9145 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9146 if (error != EMAIL_ERROR_NONE) {
9147 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9151 if (sqlite3_changes(local_db_handle) == 0)
9152 EM_DEBUG_LOG("no mail matched...");
9157 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9159 if (error == EMAIL_ERROR_NONE && parameter_string) {
9160 if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, target_mail_attribute_type, parameter_string, value))
9161 EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_MAIL_FIELD_UPDATE [%s,%d]", field_name, value);
9164 EM_SAFE_FREE(mail_id_string_buffer);
9165 EM_SAFE_FREE(parameter_string);
9166 EM_SAFE_FREE(sql_query_string);
9169 if (err_code != NULL)
9172 EM_DEBUG_FUNC_END("error [%d]", error);
9176 #ifdef __FEATURE_BODY_SEARCH__
9177 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)
9179 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
9181 if (mail_id <= 0 || !mail_text) {
9182 EM_DEBUG_EXCEPTION(" mail_id[%d], mail_text[%p]", mail_id, mail_text);
9183 if (err_code != NULL)
9184 *err_code = EMAIL_ERROR_INVALID_PARAM;
9189 int error = EMAIL_ERROR_NONE;
9190 DB_STMT hStmt = NULL;
9191 char sql_query_string[QUERY_SIZE] = {0, };
9196 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9197 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9199 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9200 "UPDATE mail_text_tbl SET"
9202 " WHERE mail_id = %d AND account_id != 0"
9204 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9206 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9207 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9208 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9211 _bind_stmt_field_data_string(hStmt, i++, (char *)mail_text->body_text, 0, -1);
9213 if (hStmt != NULL) {
9214 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9215 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9216 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9218 rc = sqlite3_changes(local_db_handle);
9220 EM_DEBUG_LOG(" no matched mail found...");
9221 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9229 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9231 if (hStmt != NULL) {
9232 rc = sqlite3_finalize(hStmt);
9233 if (rc != SQLITE_OK) {
9234 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9235 error = EMAIL_ERROR_DB_FAILURE;
9240 if (err_code != NULL)
9243 EM_DEBUG_FUNC_END("ret [%d]", ret);
9248 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)
9250 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
9253 int error = EMAIL_ERROR_NONE;
9258 DB_STMT hStmt = NULL;
9259 char sql_query_string[QUERY_SIZE] = {0, };
9260 char mailbox_id_param_string[10] = {0,};
9261 sqlite3 *local_db_handle = NULL;
9263 if (mail_id <= 0 || !mail) {
9264 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], mail[%p]", mail_id, type, mail);
9265 if (err_code != NULL)
9266 *err_code = EMAIL_ERROR_INVALID_PARAM;
9270 local_db_handle = emstorage_get_db_connection(multi_user_name);
9272 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9276 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9277 "UPDATE mail_tbl SET"
9278 " body_download_status = ?"
9279 ", file_path_plain = ?"
9280 ", file_path_html = ?"
9281 ", file_path_mime_entity = ?"
9282 ", flags_seen_field = ?"
9283 ", flags_deleted_field = ?"
9284 ", flags_flagged_field = ?"
9285 ", flags_answered_field = ?"
9286 ", flags_recent_field = ?"
9287 ", flags_draft_field = ?"
9288 ", flags_forwarded_field = ?"
9290 ", attachment_count = ?"
9292 ", meeting_request_status = ? "
9293 ", message_class = ? "
9294 ", digest_type = ? "
9296 " WHERE mail_id = %d AND account_id != 0"
9300 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9301 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9302 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9305 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9306 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9307 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9308 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9309 _bind_stmt_field_data_int(hStmt, i++, mail->flags_seen_field);
9310 _bind_stmt_field_data_int(hStmt, i++, mail->flags_deleted_field);
9311 _bind_stmt_field_data_int(hStmt, i++, mail->flags_flagged_field);
9312 _bind_stmt_field_data_int(hStmt, i++, mail->flags_answered_field);
9313 _bind_stmt_field_data_int(hStmt, i++, mail->flags_recent_field);
9314 _bind_stmt_field_data_int(hStmt, i++, mail->flags_draft_field);
9315 _bind_stmt_field_data_int(hStmt, i++, mail->flags_forwarded_field);
9316 _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
9317 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9318 _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9319 _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
9320 _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
9321 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9322 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9325 case UPDATE_MAILBOX: {
9327 emstorage_mailbox_tbl_t *mailbox_tbl;
9329 if ((err = emstorage_get_mailbox_by_id(multi_user_name, mail->mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
9330 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_id failed [%d]", err);
9334 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9335 "UPDATE mail_tbl SET"
9336 " mailbox_id = '%d'"
9337 ",mailbox_type = '%d'"
9338 " WHERE mail_id = %d AND account_id != 0"
9339 , mailbox_tbl->mailbox_id
9340 , mailbox_tbl->mailbox_type
9345 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9346 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9347 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9350 _bind_stmt_field_data_string(hStmt, i++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
9352 emstorage_free_mailbox(&mailbox_tbl, 1, NULL); /*prevent 26251*/
9358 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9359 "UPDATE mail_tbl SET"
9360 " flags_seen_field = %d"
9361 ",flags_deleted_field = %d"
9362 ",flags_flagged_field = %d"
9363 ",flags_answered_field = %d"
9364 ",flags_recent_field = %d"
9365 ",flags_draft_field = %d"
9366 ",flags_forwarded_field = %d"
9367 " WHERE mail_id = %d AND account_id != 0"
9368 , mail->flags_seen_field
9369 , mail->flags_deleted_field
9370 , mail->flags_flagged_field
9371 , mail->flags_answered_field
9372 , mail->flags_recent_field
9373 , mail->flags_draft_field
9374 , mail->flags_forwarded_field
9376 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9379 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9380 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9381 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9385 case UPDATE_EXTRA_FLAG:
9386 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9387 "UPDATE mail_tbl SET"
9389 ", save_status = %d"
9390 ", lock_status = %d"
9391 ", report_status = %d"
9393 " WHERE mail_id = %d AND account_id != 0"
9397 , mail->report_status
9400 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9403 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9404 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9405 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9408 case UPDATE_STICKY_EXTRA_FLAG:
9409 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9410 "UPDATE mail_tbl SET"
9412 " WHERE mail_id = %d AND account_id != 0"
9415 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9418 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9419 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9420 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9424 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9425 "UPDATE mail_tbl SET"
9426 " full_address_from = ?"
9427 ", full_address_reply = ?"
9428 ", full_address_to = ?"
9429 ", full_address_cc = ?"
9430 ", full_address_bcc = ?"
9431 ", full_address_return = ?"
9433 ", file_path_plain = ?"
9435 ", flags_seen_field = ?"
9436 ", flags_deleted_field = ?"
9437 ", flags_flagged_field = ?"
9438 ", flags_answered_field = ?"
9439 ", flags_recent_field = ?"
9440 ", flags_draft_field = ?"
9441 ", flags_forwarded_field = ?"
9445 ", report_status = ?"
9447 ", file_path_html = ?"
9448 ", file_path_mime_entity = ?"
9450 ", preview_text = ?"
9451 ", body_download_status = ?"
9452 ", attachment_count = ?"
9453 ", inline_content_count = ?"
9454 ", meeting_request_status = ?"
9455 ", message_class = ?"
9458 ", scheduled_sending_time = ?"
9459 ", remaining_resend_times = ?"
9461 ", replied_time = ?"
9462 ", forwarded_time = ?"
9463 ", default_charset = ?"
9464 ", eas_data_length = ?"
9466 " WHERE mail_id = %d AND account_id != 0"
9470 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9471 EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
9472 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9473 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9475 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
9476 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
9477 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
9478 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
9479 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
9480 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
9481 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9482 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9483 _bind_stmt_field_data_time_t(hStmt, i++, mail->date_time);
9484 _bind_stmt_field_data_char(hStmt, i++, mail->flags_seen_field);
9485 _bind_stmt_field_data_char(hStmt, i++, mail->flags_deleted_field);
9486 _bind_stmt_field_data_char(hStmt, i++, mail->flags_flagged_field);
9487 _bind_stmt_field_data_char(hStmt, i++, mail->flags_answered_field);
9488 _bind_stmt_field_data_char(hStmt, i++, mail->flags_recent_field);
9489 _bind_stmt_field_data_char(hStmt, i++, mail->flags_draft_field);
9490 _bind_stmt_field_data_char(hStmt, i++, mail->flags_forwarded_field);
9491 _bind_stmt_field_data_int(hStmt, i++, mail->priority);
9492 _bind_stmt_field_data_int(hStmt, i++, mail->save_status);
9493 _bind_stmt_field_data_int(hStmt, i++, mail->lock_status);
9494 _bind_stmt_field_data_int(hStmt, i++, mail->report_status);
9495 _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
9496 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9497 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9498 _bind_stmt_field_data_int(hStmt, i++, mail->mail_size);
9499 _bind_stmt_field_data_nstring(hStmt, i++, (char*)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9500 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9501 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9502 _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
9503 _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
9504 _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
9505 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9506 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9507 _bind_stmt_field_data_int(hStmt, i++, mail->scheduled_sending_time);
9508 _bind_stmt_field_data_int(hStmt, i++, mail->remaining_resend_times);
9509 _bind_stmt_field_data_int(hStmt, i++, mail->tag_id);
9510 _bind_stmt_field_data_int(hStmt, i++, mail->replied_time);
9511 _bind_stmt_field_data_int(hStmt, i++, mail->forwarded_time);
9512 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
9513 _bind_stmt_field_data_int(hStmt, i++, mail->eas_data_length);
9514 _bind_stmt_field_data_blob(hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
9517 case UPDATE_DATETIME: {
9518 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9519 "UPDATE mail_tbl SET"
9520 " date_time = '%ld'"
9521 " WHERE mail_id = %d AND account_id != 0"
9525 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9526 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9527 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9531 case UPDATE_FROM_CONTACT_INFO:
9532 EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_FROM_CONTACT_INFO");
9533 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9534 "UPDATE mail_tbl SET"
9535 " email_address_sender = ?,"
9536 " WHERE mail_id = %d",
9541 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9542 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9543 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9545 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9548 case UPDATE_TO_CONTACT_INFO:
9549 EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_TO_CONTACT_INFO");
9550 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9551 "UPDATE mail_tbl SET"
9552 " email_address_recipient = ?,"
9553 " WHERE mail_id = %d",
9558 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9559 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9560 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9562 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9565 case UPDATE_ALL_CONTACT_INFO:
9566 EM_DEBUG_LOG("emstorage_change_mail_field - mail change type is UPDATE_ALL_CONTACT_INFO");
9567 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9568 "UPDATE mail_tbl SET"
9569 " email_address_sender = ?,"
9570 " email_address_recipient = ?,"
9571 " WHERE mail_id = %d",
9576 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9577 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9578 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9580 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9581 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9585 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
9586 case UPDATE_PARTIAL_BODY_DOWNLOAD:
9588 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9589 "UPDATE mail_tbl SET"
9590 " body_download_status = ?"
9591 ", file_path_plain = ?"
9592 ", file_path_html = ?"
9593 ", file_path_mime_entity = ?"
9594 ", attachment_count = ?"
9595 ", inline_content_count = ?"
9596 ", preview_text = ?"
9599 " WHERE mail_id = %d"
9603 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9604 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9605 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9608 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9609 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9610 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9611 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9612 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9613 _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
9614 _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9615 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9616 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9621 case UPDATE_FILE_PATH:
9622 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9623 "UPDATE mail_tbl SET"
9624 ", file_path_plain = ?"
9625 ", file_path_html = ?"
9626 ", file_path_mime_entity = ?"
9627 " WHERE mail_id = %d"
9631 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9632 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
9633 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9634 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9636 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9637 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9638 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9642 EM_DEBUG_LOG(" type[%d]", type);
9644 error = EMAIL_ERROR_INVALID_PARAM;
9648 if (hStmt != NULL) {
9650 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9651 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
9652 ("sqlite3_step fail:%d", rc));
9653 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9654 ("sqlite3_step fail:%d", rc));
9655 rc = sqlite3_changes(local_db_handle);
9657 EM_DEBUG_LOG(" no matched mail found...");
9658 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9663 if (mail->account_id == 0) {
9664 emstorage_mail_tbl_t* mail_for_account_tbl = NULL;
9665 if (!emstorage_get_mail_field_by_id(multi_user_name, mail_id, RETRIEVE_ACCOUNT, &mail_for_account_tbl, true, &error) || !mail_for_account_tbl) {
9666 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id error [%d]", error);
9669 mail->account_id = mail_for_account_tbl->account_id;
9670 if (mail_for_account_tbl)
9671 emstorage_free_mail(&mail_for_account_tbl, 1, NULL);
9677 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9679 if (hStmt != NULL) {
9680 rc = sqlite3_finalize(hStmt);
9681 if (rc != SQLITE_OK) {
9682 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9683 error = EMAIL_ERROR_DB_FAILURE;
9688 if (error == EMAIL_ERROR_NONE && move_flag != 1 && transaction) {
9689 if (!emstorage_get_mailbox_id_by_mailbox_type(multi_user_name, mail->account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &mailbox_id, false, &error))
9690 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_id_by_mailbox_type error [%d]", error);
9692 if (mail->mailbox_id == mailbox_id) {
9693 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
9694 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, mailbox_id_param_string, type))
9695 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventerror [ NOTI_MAIL_UPDATE ] >>>> ");
9697 /* h.gahlaut@samsung.com: Jan 10, 2011 Publishing noti to refresh outbox when email sending status changes */
9698 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, NULL, type))
9699 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventerror [ NOTI_MAIL_UPDATE ]");
9703 if (err_code != NULL)
9706 EM_DEBUG_FUNC_END("ret [%d]", ret);
9710 INTERNAL_FUNC int emstorage_increase_mail_id(char *multi_user_name, int *mail_id, int transaction, int *err_code)
9712 EM_DEBUG_FUNC_BEGIN("mail_id[%p], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9714 int rc, ret = false;
9715 int error = EMAIL_ERROR_NONE;
9716 int latest_mail_id = 0;
9717 sqlite3 *local_db_handle = NULL;
9718 char *sql = "SELECT MAX(mail_id) FROM mail_tbl;";
9719 char **result = NULL;
9721 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9722 _timedlock_shm_mutex(mapped_for_generating_mail_id, 2);
9723 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9725 ret = vconf_get_int(VCONF_KEY_LATEST_MAIL_ID, &latest_mail_id);
9726 if (ret < 0 || latest_mail_id == 0) {
9727 EM_DEBUG_LOG("vconf_get_int() failed [%d] or latest_mail_id is zero", ret);
9729 local_db_handle = emstorage_get_db_connection(multi_user_name);
9731 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9732 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
9733 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9734 if (NULL == result[1])
9737 rc = atoi(result[1]) + 1;
9739 sqlite3_free_table(result);
9740 latest_mail_id = rc;
9745 ret = vconf_set_int(VCONF_KEY_LATEST_MAIL_ID, latest_mail_id);
9748 *mail_id = latest_mail_id;
9754 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9755 _unlockshm_mutex(mapped_for_generating_mail_id);
9756 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9758 if (err_code != NULL)
9761 EM_DEBUG_FUNC_END("ret [%d]", ret);
9765 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)
9767 EM_PROFILE_BEGIN(profile_emstorage_add_mail);
9768 EM_DEBUG_FUNC_BEGIN("mail_tbl_data[%p], get_id[%d], transaction[%d], err_code[%p]", mail_tbl_data, get_id, transaction, err_code);
9770 if (!mail_tbl_data) {
9771 EM_DEBUG_EXCEPTION("mail_tbl_data[%p], get_id[%d]", mail_tbl_data, get_id);
9772 if (err_code != NULL)
9773 *err_code = EMAIL_ERROR_INVALID_PARAM;
9777 int rc, ret = false;
9778 int error = EMAIL_ERROR_NONE;
9779 char sql_query_string[QUERY_SIZE] = {0, };
9780 DB_STMT hStmt = NULL;
9782 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9784 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9787 /* increase unique id */
9788 char *sql = "SELECT max(rowid) FROM mail_tbl;";
9791 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9792 /* EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
9793 ("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle))); */
9794 if (rc != SQLITE_OK) {
9795 EM_DEBUG_EXCEPTION("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle));
9796 error = EMAIL_ERROR_DB_FAILURE;
9797 sqlite3_free_table(result);
9801 if (NULL == result[1])
9804 rc = atoi(result[1])+1;
9806 sqlite3_free_table(result);
9808 mail_tbl_data->mail_id = rc;
9809 mail_tbl_data->thread_id = rc;
9812 if (mail_tbl_data->date_time == 0)
9813 mail_tbl_data->date_time = time(NULL);
9815 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9816 "INSERT INTO mail_tbl VALUES "
9818 ", ?" /* account_id */
9819 ", ?" /* mailbox_id */
9820 ", ?" /* mailbox_type */
9823 ", ?" /* date_time */
9824 ", ?" /* server_mail_status */
9825 ", ?" /* server_mailbox_name */
9826 ", ?" /* server_mail_id */
9827 ", ?" /* message_id */
9829 ", ?" /* reference_mail_id */
9830 ", ?" /* full_address_from */
9831 ", ?" /* full_address_reply */
9832 ", ?" /* full_address_to */
9833 ", ?" /* full_address_cc */
9835 ", ?" /* full_address_bcc */
9836 ", ?" /* full_address_return */
9837 ", ?" /* email_address_sender */
9838 ", ?" /* email_address_recipient */
9839 ", ?" /* alias_sender */
9841 ", ?" /* alias_recipient */
9842 ", ?" /* body_download_status */
9843 ", ?" /* file_path_plain */
9844 ", ?" /* file_path_html */
9845 ", ?" /* file_path_mime_entity */
9847 ", ?" /* mail_size */
9848 ", ?" /* flags_seen_field */
9849 ", ?" /* flags_deleted_field */
9850 ", ?" /* flags_flagged_field */
9851 ", ?" /* flags_answered_field */
9853 ", ?" /* flags_recent_field */
9854 ", ?" /* flags_draft_field */
9855 ", ?" /* flags_forwarded_field */
9856 ", ?" /* DRM_status */
9857 ", ?" /* priority */
9859 ", ?" /* save_status */
9860 ", ?" /* lock_status */
9861 ", ?" /* report_status */
9862 ", ?" /* attachment_count */
9863 ", ?" /* inline_content_count */
9865 ", ?" /* thread_id */
9866 ", ?" /* thread_item_count */
9867 ", ?" /* preview_text */
9868 ", ?" /* meeting_request_status */
9869 ", ?" /* message_class */
9871 ", ?" /* digest_type */
9872 ", ?" /* smime_type */
9873 ", ?" /* scheduled_sending_time */
9874 ", ?" /* remaining_resend_times */
9877 ", ?" /* replied_time */
9878 ", ?" /* forwarded_time */
9879 ", ?" /* default charset */
9880 ", ?" /* eas_data_length */
9881 ", ?" /* eas_data */
9882 ", ?" /* user_name */
9885 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle,
9887 EM_SAFE_STRLEN(sql_query_string),
9891 if (rc != SQLITE_OK) {
9892 EM_DEBUG_EXCEPTION("sqlite3_prepare error [%d] [%s] SQL(%s) ",
9893 rc, sql_query_string, sqlite3_errmsg(local_db_handle));
9894 error = EMAIL_ERROR_DB_FAILURE;
9898 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mail_id);
9899 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, mail_tbl_data->account_id);
9900 _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_id);
9901 _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_type);
9902 _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9904 _bind_stmt_field_data_int(hStmt, DATETIME_IDX_IN_MAIL_TBL, mail_tbl_data->date_time);
9905 _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->server_mail_status);
9906 _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);
9907 _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);
9908 _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);
9910 _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, mail_tbl_data->reference_mail_id);
9911 _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);
9912 _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);
9913 _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);
9914 _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);
9916 _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);
9917 _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);
9918 _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);
9919 _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);
9920 _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);
9922 _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);
9923 _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->body_download_status);
9924 _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);
9925 _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);
9926 _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);
9928 _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, mail_tbl_data->mail_size);
9929 _bind_stmt_field_data_int(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_seen_field);
9930 _bind_stmt_field_data_int(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_deleted_field);
9931 _bind_stmt_field_data_int(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_flagged_field);
9932 _bind_stmt_field_data_int(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_answered_field);
9934 _bind_stmt_field_data_int(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_recent_field);
9935 _bind_stmt_field_data_int(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_draft_field);
9936 _bind_stmt_field_data_int(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_forwarded_field);
9937 _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->DRM_status);
9938 _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, mail_tbl_data->priority);
9940 _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->save_status);
9941 _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->lock_status);
9942 _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->report_status);
9943 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->attachment_count);
9944 _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->inline_content_count);
9946 _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, mail_tbl_data->thread_id);
9947 _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->thread_item_count);
9948 _bind_stmt_field_data_nstring(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9949 _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->meeting_request_status);
9950 _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, mail_tbl_data->message_class);
9952 _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->digest_type);
9953 _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->smime_type);
9954 _bind_stmt_field_data_int(hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->scheduled_sending_time);
9955 _bind_stmt_field_data_int(hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, mail_tbl_data->remaining_resend_times);
9956 _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, mail_tbl_data->tag_id);
9958 _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->replied_time);
9959 _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->forwarded_time);
9960 _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);
9961 _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, mail_tbl_data->eas_data_length);
9962 _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, (void*)mail_tbl_data->eas_data, mail_tbl_data->eas_data_length);
9963 _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);
9965 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9966 if (rc == SQLITE_FULL) {
9967 EM_DEBUG_EXCEPTION("sqlite3_step error [%d]", rc);
9968 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
9971 if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
9972 EM_DEBUG_EXCEPTION("sqlite3_step error [%d]", rc);
9973 error = EMAIL_ERROR_DB_FAILURE;
9979 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9981 if (hStmt != NULL) {
9982 rc = sqlite3_finalize(hStmt);
9983 if (rc != SQLITE_OK) {
9984 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9985 error = EMAIL_ERROR_DB_FAILURE;
9989 if (err_code != NULL)
9992 EM_PROFILE_END(profile_emstorage_add_mail);
9993 EM_DEBUG_FUNC_END("ret [%d]", ret);
9997 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)
9999 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);
10001 int ret = false, i, cur_conditional_clause = 0;
10002 int error = EMAIL_ERROR_NONE;
10003 int target_account_id;
10004 int conditional_clause_len = 0;
10005 char *sql_query_string = NULL, *conditional_clause = NULL;
10006 emstorage_mailbox_tbl_t *result_mailbox = NULL;
10007 email_mailbox_type_e target_mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED;
10008 char* target_mailbox_name = NULL;
10010 if (!mail_ids || input_mailbox_id <= 0) {
10011 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10012 if (err_code != NULL)
10013 *err_code = EMAIL_ERROR_INVALID_PARAM;
10017 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10019 if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &result_mailbox)) != EMAIL_ERROR_NONE || !result_mailbox) {
10020 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", error);
10021 if (err_code != NULL)
10026 if (result_mailbox->mailbox_name) {
10027 if (strstr(result_mailbox->mailbox_name, "'"))
10028 target_mailbox_name = em_replace_all_string(result_mailbox->mailbox_name, "'", "''");
10030 target_mailbox_name = strdup(result_mailbox->mailbox_name);
10033 target_mailbox_type = result_mailbox->mailbox_type;
10034 target_account_id = result_mailbox->account_id;
10035 emstorage_free_mailbox(&result_mailbox, 1, NULL);
10037 conditional_clause_len = (sizeof(char) * 8 * number_of_mails) + 512;
10038 conditional_clause = em_malloc(conditional_clause_len);
10039 if (conditional_clause == NULL) {
10040 EM_DEBUG_EXCEPTION("em_mallocfailed");
10041 error = EMAIL_ERROR_OUT_OF_MEMORY;
10044 cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE mail_id in (");
10046 for (i = 0; i < number_of_mails; i++)
10047 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
10049 /* prevent 34415 */
10050 char *last_comma = rindex(conditional_clause, ',');
10051 if (last_comma) *last_comma = ')'; /* replace , with) */
10053 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10055 /* Updating a mail_tbl */
10057 sql_query_string = em_malloc(conditional_clause_len);
10058 if (sql_query_string == NULL) {
10059 EM_DEBUG_EXCEPTION("em_mallocfailed");
10060 error = EMAIL_ERROR_OUT_OF_MEMORY;
10063 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);
10064 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10066 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10067 if (error != EMAIL_ERROR_NONE) {
10068 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10072 /* Updating a mail_attachment_tbl */
10073 memset(sql_query_string, 0x00, conditional_clause_len);
10074 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);
10075 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10076 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10077 if (error != EMAIL_ERROR_NONE) {
10078 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10082 /* Updating a mail_meeting_tbl */
10083 memset(sql_query_string, 0x00, conditional_clause_len);
10084 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);
10085 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10086 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10087 if (error != EMAIL_ERROR_NONE) {
10088 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10092 #ifdef __FEATURE_BODY_SEARCH__
10093 /* Updating mail_text_tbl */
10094 memset(sql_query_string, 0x00, conditional_clause_len);
10095 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);
10096 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10097 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10098 if (error != EMAIL_ERROR_NONE) {
10099 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10104 /* Updating a mail_read_mail_uid_tbl */
10105 memset(conditional_clause, 0x00, conditional_clause_len);
10106 cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE local_uid in (");
10108 for (i = 0; i < number_of_mails; i++)
10109 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
10111 /* prevent 34415 */
10112 last_comma = rindex(conditional_clause, ',');
10113 if (last_comma) *last_comma = ')'; /* replace , with) */
10115 memset(sql_query_string, 0x00, conditional_clause_len);
10116 SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_read_mail_uid_tbl SET mailbox_name = ? mailbox_id = ? account_id = ? %s", conditional_clause);
10118 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10119 error = emstorage_exec_query_by_prepare_v2_stmt(local_db_handle, sql_query_string, target_mailbox_name, input_mailbox_id, target_account_id);
10120 if (error != EMAIL_ERROR_NONE) {
10121 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2_stmt failed:[%d]", error);
10128 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10130 EM_SAFE_FREE(target_mailbox_name);
10131 EM_SAFE_FREE(conditional_clause);
10132 EM_SAFE_FREE(sql_query_string);
10134 if (err_code != NULL)
10137 EM_DEBUG_FUNC_END("ret [%d]", ret);
10141 INTERNAL_FUNC int emstorage_delete_mail(char *multi_user_name, int mail_id, int from_server, int transaction, int *err_code)
10143 EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
10146 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
10147 if (err_code != NULL)
10148 *err_code = EMAIL_ERROR_INVALID_PARAM;
10153 int error = EMAIL_ERROR_NONE;
10154 char sql_query_string[QUERY_SIZE] = {0, };
10155 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10156 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10158 memset(sql_query_string, 0x00, sizeof(sql_query_string));
10160 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id = %d ", mail_id);
10161 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10162 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10163 if (error != EMAIL_ERROR_NONE) {
10164 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10171 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10173 if (err_code != NULL)
10176 EM_DEBUG_FUNC_END("ret [%d]", ret);
10180 INTERNAL_FUNC int emstorage_delete_multiple_mails(char *multi_user_name, int mail_ids[], int number_of_mails, int transaction, int *err_code)
10182 EM_DEBUG_FUNC_BEGIN("mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", mail_ids, number_of_mails, transaction, err_code);
10184 int ret = false, i, cur_sql_query_string = 0;
10185 int error = EMAIL_ERROR_NONE;
10186 int query_size = 0;
10187 char *sql_query_string = NULL;
10190 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10191 if (err_code != NULL)
10192 *err_code = EMAIL_ERROR_INVALID_PARAM;
10196 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10197 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10199 query_size = (sizeof(char) * 8 * number_of_mails) + 512;
10200 sql_query_string = em_malloc(query_size);
10201 if (sql_query_string == NULL) {
10202 EM_DEBUG_EXCEPTION("em_mallocfailed");
10203 error = EMAIL_ERROR_OUT_OF_MEMORY;
10207 cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_tbl WHERE mail_id in (");
10209 for (i = 0; i < number_of_mails; i++)
10210 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
10212 /* prevent 34414 */
10213 char *last_comma = rindex(sql_query_string, ',');
10214 if (last_comma != NULL) *last_comma = ')'; /* replace , with) */
10216 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10217 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10218 if (error != EMAIL_ERROR_NONE) {
10219 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10223 #ifdef __FEATURE_BODY_SEARCH__
10224 /* delete mail_text from mail_text_tbl */
10225 cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_text_tbl WHERE mail_id in (");
10227 for (i = 0; i < number_of_mails; i++)
10228 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
10230 last_comma = rindex(sql_query_string, ',');
10231 *last_comma = ')'; /* replace , with) */
10233 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10234 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10235 if (error != EMAIL_ERROR_NONE) {
10236 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10244 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10246 EM_SAFE_FREE(sql_query_string);
10247 if (err_code != NULL)
10250 EM_DEBUG_FUNC_END("ret [%d]", ret);
10254 INTERNAL_FUNC int emstorage_delete_mail_by_account(char *multi_user_name, int account_id, int transaction, int *err_code)
10256 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
10258 if (account_id < FIRST_ACCOUNT_ID) {
10259 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
10261 if (err_code != NULL)
10262 *err_code = EMAIL_ERROR_INVALID_PARAM;
10266 int rc, ret = false;
10267 int error = EMAIL_ERROR_NONE;
10268 char sql_query_string[QUERY_SIZE] = {0, };
10270 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10271 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10273 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d", account_id);
10274 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10275 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10276 if (error != EMAIL_ERROR_NONE) {
10277 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10281 rc = sqlite3_changes(local_db_handle);
10283 EM_DEBUG_LOG(" no mail found...");
10284 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10287 /* Delete all mails mail_read_mail_uid_tbl table based on account id */
10288 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
10289 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10290 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10291 if (error != EMAIL_ERROR_NONE) {
10292 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10296 rc = sqlite3_changes(local_db_handle);
10298 EM_DEBUG_LOG("No mail found...");
10299 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10302 #ifdef __FEATURE_BODY_SEARCH__
10303 /* Delete all mail_text in mail_text_tbl table based on account id */
10304 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE account_id = %d", account_id);
10305 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10306 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10307 if (error != EMAIL_ERROR_NONE) {
10308 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10312 rc = sqlite3_changes(local_db_handle);
10314 EM_DEBUG_LOG("No mail found...");
10315 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10322 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10324 if (error == EMAIL_ERROR_NONE) {
10325 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_WITH_ACCOUNT, account_id, 0 , NULL, 0))
10326 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAIL_DELETE_ALL ]");
10329 if (err_code != NULL)
10332 EM_DEBUG_FUNC_END("ret [%d]", ret);
10336 INTERNAL_FUNC int emstorage_delete_mail_by_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox, int transaction, int *err_code)
10338 EM_DEBUG_FUNC_BEGIN("mailbox[%p], transaction[%d], err_code[%p]", mailbox, transaction, err_code);
10340 if (mailbox == NULL) {
10341 EM_DEBUG_EXCEPTION("mailbox [%p]", mailbox);
10342 if (err_code != NULL)
10343 *err_code = EMAIL_ERROR_INVALID_PARAM;
10348 int error = EMAIL_ERROR_NONE;
10349 char sql_query_string[QUERY_SIZE] = {0, };
10350 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10351 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10353 if (strcmp(mailbox->mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
10354 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);
10355 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10356 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10357 if (error != EMAIL_ERROR_NONE) {
10358 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10362 /* Delete Mails from mail_read_mail_uid_tbl */
10363 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);
10364 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10365 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10366 if (error != EMAIL_ERROR_NONE) {
10367 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10371 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);
10372 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10373 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10374 if (error != EMAIL_ERROR_NONE) {
10375 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10379 /* Delete Mails from mail_read_mail_uid_tbl */
10380 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);
10381 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10382 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10383 if (error != EMAIL_ERROR_NONE) {
10384 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10388 #ifdef __FEATURE_BODY_SEARCH__
10389 /* Delete Mails from mail_text_tbl */
10390 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);
10391 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10392 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10393 if (error != EMAIL_ERROR_NONE) {
10394 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10402 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10404 if (error == EMAIL_ERROR_NONE) {
10405 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_ALL, mailbox->account_id, mailbox->mailbox_id , mailbox->mailbox_name, 0))
10406 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventFailed [ NOTI_MAIL_DELETE_ALL ] >>>> ");
10409 if (err_code != NULL)
10412 EM_DEBUG_FUNC_END("ret [%d]", ret);
10415 /* LCOV_EXCL_STOP */
10416 INTERNAL_FUNC int emstorage_free_mail(emstorage_mail_tbl_t** mail_list, int count, int *err_code)
10418 EM_DEBUG_FUNC_BEGIN("mail_list[%p], count[%d], err_code[%p]", mail_list, count, err_code);
10421 if ((mail_list == NULL) || (*mail_list == NULL)) {
10422 EM_DEBUG_EXCEPTION("mail_ilst[%p], count[%d]", mail_list, count);
10425 *err_code = EMAIL_ERROR_INVALID_PARAM;
10429 emstorage_mail_tbl_t* p = *mail_list;
10432 for (; i < count; i++, p++) {
10433 EM_SAFE_FREE(p->server_mailbox_name);
10434 EM_SAFE_FREE(p->server_mail_id);
10435 EM_SAFE_FREE(p->full_address_from);
10436 EM_SAFE_FREE(p->full_address_reply);
10437 EM_SAFE_FREE(p->full_address_to);
10438 EM_SAFE_FREE(p->full_address_cc);
10439 EM_SAFE_FREE(p->full_address_bcc);
10440 EM_SAFE_FREE(p->full_address_return);
10441 EM_SAFE_FREE(p->subject);
10442 EM_SAFE_FREE(p->file_path_plain);
10443 EM_SAFE_FREE(p->file_path_html);
10444 EM_SAFE_FREE(p->file_path_mime_entity);
10445 EM_SAFE_FREE(p->message_id);
10446 EM_SAFE_FREE(p->email_address_sender);
10447 EM_SAFE_FREE(p->email_address_recipient);
10448 EM_SAFE_FREE(p->preview_text);
10449 EM_SAFE_FREE(p->alias_sender);
10450 EM_SAFE_FREE(p->alias_recipient);
10451 EM_SAFE_FREE(p->default_charset);
10452 EM_SAFE_FREE(p->pgp_password);
10453 EM_SAFE_FREE(p->eas_data);
10454 EM_SAFE_FREE(p->user_name);
10456 EM_SAFE_FREE(*mail_list);
10459 if (err_code != NULL)
10460 *err_code = EMAIL_ERROR_NONE;
10462 EM_DEBUG_FUNC_END();
10466 #ifdef __FEATURE_BODY_SEARCH__
10467 /* LCOV_EXCL_START */
10468 INTERNAL_FUNC void emstorage_free_mail_text(emstorage_mail_text_tbl_t** mail_text_list, int count, int *err_code)
10470 EM_DEBUG_FUNC_BEGIN("mail_text_list[%p], count[%d], err_code[%p]", mail_text_list, count, err_code);
10473 if ((mail_text_list == NULL) || (*mail_text_list == NULL)) {
10474 EM_DEBUG_LOG("Nothing to free: mail_text_list[%p]", mail_text_list);
10478 emstorage_mail_text_tbl_t *p = *mail_text_list;
10481 for (; i < count; i++, p++)
10482 EM_SAFE_FREE(p->body_text);
10484 EM_SAFE_FREE(*mail_text_list);
10487 EM_DEBUG_FUNC_END();
10491 INTERNAL_FUNC int emstorage_get_attachment_count(char *multi_user_name, int mail_id, int *count, int transaction, int *err_code)
10493 EM_DEBUG_FUNC_BEGIN("mail_id[%d], count[%p], transaction[%d], err_code[%p]", mail_id, count, transaction, err_code);
10495 if (mail_id <= 0 || !count) {
10496 EM_DEBUG_EXCEPTION("mail_id[%d], count[%p]", mail_id, count);
10497 if (err_code != NULL)
10498 *err_code = EMAIL_ERROR_INVALID_PARAM;
10502 int rc = -1, ret = false;
10503 int error = EMAIL_ERROR_NONE;
10504 char sql_query_string[QUERY_SIZE] = {0, };
10506 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10507 EMSTORAGE_START_READ_TRANSACTION(transaction);
10509 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
10513 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10514 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10515 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10517 *count = atoi(result[1]);
10518 sqlite3_free_table(result);
10523 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10525 if (err_code != NULL)
10528 EM_DEBUG_FUNC_END("ret [%d]", ret);
10532 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)
10534 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);
10536 if (input_mail_id <= 0 || !output_attachment_list || !output_attachment_count) {
10537 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10538 return EMAIL_ERROR_INVALID_PARAM;
10541 int error = EMAIL_ERROR_NONE;
10544 char **result = NULL;
10545 char sql_query_string[QUERY_SIZE] = {0, };
10546 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10547 DB_STMT hStmt = NULL;
10548 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10550 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
10551 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", input_mail_id);
10552 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10553 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10554 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10556 *output_attachment_count = atoi(result[1]);
10557 sqlite3_free_table(result);
10559 if (*output_attachment_count == 0) {
10560 error = EMAIL_ERROR_NONE;
10564 p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * (*output_attachment_count));
10567 EM_DEBUG_EXCEPTION("em_mallocfailed...");
10568 error = EMAIL_ERROR_OUT_OF_MEMORY;
10572 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE mail_id = %d ORDER BY attachment_id", input_mail_id);
10573 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
10575 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10576 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)));
10578 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10579 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
10581 if (rc == SQLITE_DONE) {
10582 EM_DEBUG_EXCEPTION("no matched attachment found...");
10583 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10586 for (i = 0; i < *output_attachment_count; i++) {
10587 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10588 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10589 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10590 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10591 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10592 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10593 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10594 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10595 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10596 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10597 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10598 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10599 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10600 EM_DEBUG_LOG("attachment[%d].attachment_id : %d", i, p_data_tbl[i].attachment_id);
10601 EM_DEBUG_LOG("attachment_mime_type : %s", p_data_tbl[i].attachment_mime_type);
10602 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10603 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
10608 if (error == EMAIL_ERROR_NONE)
10609 *output_attachment_list = p_data_tbl;
10610 else if (p_data_tbl != NULL)
10611 emstorage_free_attachment(&p_data_tbl, *output_attachment_count, NULL);
10614 rc = sqlite3_finalize(hStmt);
10615 if (rc != SQLITE_OK) {
10616 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10617 error = EMAIL_ERROR_DB_FAILURE;
10618 if (*output_attachment_list)
10619 emstorage_free_attachment(output_attachment_list, *output_attachment_count, NULL); /* prevent */
10623 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
10625 EM_DEBUG_FUNC_END("error [%d]", error);
10629 INTERNAL_FUNC int emstorage_get_attachment(char *multi_user_name, int attachment_id, emstorage_attachment_tbl_t **attachment, int transaction, int *err_code)
10631 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], transaction[%d], err_code[%p]", attachment_id, attachment, transaction, err_code);
10633 if (attachment_id <= 0 || !attachment) {
10634 EM_DEBUG_EXCEPTION("attachment_id[%d], attachment[%p]", attachment_id, attachment);
10635 if (err_code != NULL)
10636 *err_code = EMAIL_ERROR_INVALID_PARAM;
10640 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10641 int rc, ret = false;
10642 int error = EMAIL_ERROR_NONE;
10643 char sql_query_string[QUERY_SIZE] = {0, };
10645 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10646 EMSTORAGE_START_READ_TRANSACTION(transaction);
10648 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
10650 sqlite3_stmt* hStmt = NULL;
10652 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10653 EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
10655 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10656 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10659 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10660 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10661 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10663 if (rc == SQLITE_DONE) {
10664 EM_DEBUG_LOG("no matched attachment found...");
10665 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10669 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10670 EM_DEBUG_EXCEPTION("malloc failed...");
10671 error = EMAIL_ERROR_OUT_OF_MEMORY;
10675 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10676 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10677 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10678 _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10679 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10680 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10681 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10682 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10683 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10684 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10685 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10686 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10687 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10689 #ifdef __ATTACHMENT_OPTI__
10690 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10691 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10698 *attachment = p_data_tbl;
10700 if (hStmt != NULL) {
10701 rc = sqlite3_finalize(hStmt);
10702 if (rc != SQLITE_OK) {
10703 EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10704 error = EMAIL_ERROR_DB_FAILURE;
10708 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10710 if (err_code != NULL)
10713 EM_DEBUG_FUNC_END("ret [%d]", ret);
10717 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)
10719 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);
10721 if (mail_id <= 0 || nth <= 0 || !attachment_tbl) {
10722 EM_DEBUG_EXCEPTION(" mail_id[%d], nth[%d], attachment[%p]", mail_id, nth, attachment_tbl);
10724 if (err_code != NULL)
10725 *err_code = EMAIL_ERROR_INVALID_PARAM;
10729 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10731 int rc, ret = false;
10732 int error = EMAIL_ERROR_NONE;
10733 char sql_query_string[QUERY_SIZE] = {0, };
10735 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10736 EMSTORAGE_START_READ_TRANSACTION(transaction);
10738 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));
10739 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10741 DB_STMT hStmt = NULL;
10743 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10744 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10745 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10748 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10749 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10750 ("sqlite3_step fail:%d", rc));
10752 if (rc == SQLITE_DONE) {
10753 EM_DEBUG_EXCEPTION("no matched attachment found: mail_id[%d] nth[%d]", mail_id, nth);
10754 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10758 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10759 EM_DEBUG_EXCEPTION(" malloc failed...");
10760 error = EMAIL_ERROR_OUT_OF_MEMORY;
10764 p_data_tbl->attachment_id = sqlite3_column_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10765 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10766 p_data_tbl->attachment_name = cpy_str(p);
10767 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10768 p_data_tbl->attachment_path = cpy_str(p);
10769 if ((p = (char *)sqlite3_column_text(hStmt, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10770 p_data_tbl->content_id = cpy_str(p);
10771 p_data_tbl->attachment_size = sqlite3_column_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10772 p_data_tbl->mail_id = sqlite3_column_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10773 p_data_tbl->account_id = sqlite3_column_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10774 p_data_tbl->mailbox_id = sqlite3_column_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10775 p_data_tbl->attachment_save_status = sqlite3_column_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10776 p_data_tbl->attachment_drm_type = sqlite3_column_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10777 p_data_tbl->attachment_drm_method = sqlite3_column_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10778 p_data_tbl->attachment_inline_content_status = sqlite3_column_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10779 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10780 p_data_tbl->attachment_mime_type = cpy_str(p);
10781 #ifdef __ATTACHMENT_OPTI__
10782 p_data_tbl->encoding = sqlite3_column_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10783 if ((p = (char *)sqlite3_column_text(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10784 p_data_tbl->section = cpy_str(p);
10790 *attachment_tbl = p_data_tbl;
10792 if (hStmt != NULL) {
10793 rc = sqlite3_finalize(hStmt);
10794 if (rc != SQLITE_OK) {
10795 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10796 error = EMAIL_ERROR_DB_FAILURE;
10800 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10802 if (err_code != NULL)
10805 EM_DEBUG_FUNC_END("ret [%d]", ret);
10809 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)
10811 EM_DEBUG_FUNC_BEGIN("attachment_path[%p], attachment[%p], transaction[%d], err_code[%p]", attachment_path, attachment, transaction, err_code);
10813 if (attachment_path == NULL || !attachment) {
10814 EM_DEBUG_EXCEPTION("attachment_path[%p], attachment[%p]", attachment_path, attachment);
10815 if (err_code != NULL)
10816 *err_code = EMAIL_ERROR_INVALID_PARAM;
10820 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10821 int rc, ret = false;
10822 int error = EMAIL_ERROR_NONE;
10823 char sql_query_string[QUERY_SIZE] = {0, };
10825 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10826 EMSTORAGE_START_READ_TRANSACTION(transaction);
10828 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_path = '%s'", attachment_path);
10830 sqlite3_stmt* hStmt = NULL;
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_DEV("before sqlite3_prepare hStmt = %p", hStmt);
10835 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10836 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10839 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10840 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10841 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10843 if (rc == SQLITE_DONE) {
10844 EM_DEBUG_LOG("no matched attachment found...");
10845 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10849 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10850 EM_DEBUG_EXCEPTION("malloc failed...");
10851 error = EMAIL_ERROR_OUT_OF_MEMORY;
10855 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10856 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10857 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10858 _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10859 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10860 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10861 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10862 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10863 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10864 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10865 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10866 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10867 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10869 #ifdef __ATTACHMENT_OPTI__
10870 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10871 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10878 *attachment = p_data_tbl;
10880 if (hStmt != NULL) {
10881 rc = sqlite3_finalize(hStmt);
10882 if (rc != SQLITE_OK) {
10883 EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10884 error = EMAIL_ERROR_DB_FAILURE;
10888 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10890 if (err_code != NULL)
10893 EM_DEBUG_FUNC_END("ret [%d]", ret);
10897 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)
10899 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], attachment[%p], transaction[%d], err_code[%p]", mail_id, type, attachment, transaction, err_code);
10901 if (mail_id <= 0 || !attachment) {
10902 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], attachment[%p]", mail_id, type, attachment);
10903 if (err_code != NULL)
10904 *err_code = EMAIL_ERROR_INVALID_PARAM;
10908 int rc, ret = false;
10909 int error = EMAIL_ERROR_NONE;
10910 DB_STMT hStmt = NULL;
10911 char sql_query_string[QUERY_SIZE] = {0, };
10915 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10917 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10920 case UPDATE_MAILBOX:
10921 EM_DEBUG_LOG("UPDATE_MAILBOX");
10922 if (!attachment->mailbox_id) {
10923 EM_DEBUG_EXCEPTION(" attachment->mailbox_id[%d]", attachment->mailbox_id);
10924 error = EMAIL_ERROR_INVALID_PARAM;
10927 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10928 "UPDATE mail_attachment_tbl SET mailbox_id = ? WHERE mail_id = %d", mail_id);
10930 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10931 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10932 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10933 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10935 _bind_stmt_field_data_int(hStmt, i++, attachment->mailbox_id);
10938 case UPDATE_SAVENAME:
10939 EM_DEBUG_LOG("UPDATE_SAVENAME");
10940 if (!attachment->attachment_path) {
10941 EM_DEBUG_EXCEPTION(" attachment->attachment_path[%p]", attachment->attachment_path);
10942 error = EMAIL_ERROR_INVALID_PARAM;
10946 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10947 "UPDATE mail_attachment_tbl SET"
10948 " attachment_size = ?"
10949 ", attachment_save_status = ?"
10950 ", attachment_path = ?"
10951 " WHERE mail_id = %d"
10952 " AND attachment_id = %d"
10953 , attachment->mail_id
10954 , attachment->attachment_id);
10957 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10958 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10959 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10960 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10962 _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_size);
10963 _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_save_status);
10964 _bind_stmt_field_data_string(hStmt, i++, (char *)attachment->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10968 EM_DEBUG_LOG("type[%d]", type);
10969 error = EMAIL_ERROR_INVALID_PARAM;
10972 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10974 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10975 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10976 ("sqlite3_step fail:%d", rc));
10981 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10983 if (hStmt != NULL) {
10984 rc = sqlite3_finalize(hStmt);
10985 if (rc != SQLITE_OK) {
10986 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10987 error = EMAIL_ERROR_DB_FAILURE;
10991 if (err_code != NULL)
10993 EM_DEBUG_FUNC_END("ret [%d]", ret);
10997 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)
10999 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);
11003 int error = EMAIL_ERROR_NONE;
11005 int account_id = 0;
11006 char sql_query_string[QUERY_SIZE] = {0, };
11007 char *replaced_mailbox_name = NULL;
11008 char *replaced_alias = NULL;
11009 sqlite3 *local_db_handle = NULL;
11010 DB_STMT hStmt = NULL;
11011 emstorage_mailbox_tbl_t *old_mailbox_data = NULL;
11013 if (input_mailbox_id <= 0 || !input_new_mailbox_name || !input_new_mailbox_alias) {
11014 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
11015 return EMAIL_ERROR_INVALID_PARAM;
11018 if (strstr(input_new_mailbox_name, "'"))
11019 replaced_mailbox_name = em_replace_all_string(input_new_mailbox_name, "'", "''");
11021 replaced_mailbox_name = strdup(input_new_mailbox_name);
11023 if (strstr(input_new_mailbox_alias, "'"))
11024 replaced_alias = em_replace_all_string(input_new_mailbox_alias, "'", "''");
11026 replaced_alias = strdup(input_new_mailbox_alias);
11028 local_db_handle = emstorage_get_db_connection(multi_user_name);
11030 if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &old_mailbox_data)) != EMAIL_ERROR_NONE) {
11031 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", error);
11032 EM_SAFE_FREE(replaced_mailbox_name);
11033 EM_SAFE_FREE(replaced_alias);
11037 if (old_mailbox_data == NULL) {
11038 EM_DEBUG_LOG("old_mailbox_data is NULL");
11039 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
11043 account_id = old_mailbox_data->account_id;
11045 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
11047 if (input_eas_data && input_eas_data_length > 0) {
11048 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11049 "UPDATE mail_box_tbl SET"
11050 " mailbox_name = ?"
11053 ",eas_data_length = ?"
11054 " WHERE mailbox_id = ?");
11056 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11058 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11059 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11061 _bind_stmt_field_data_string(hStmt, field_idx++, input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
11062 _bind_stmt_field_data_string(hStmt, field_idx++, input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
11063 _bind_stmt_field_data_blob(hStmt, field_idx++, input_eas_data, input_eas_data_length);
11064 _bind_stmt_field_data_int(hStmt, field_idx++, input_eas_data_length);
11065 _bind_stmt_field_data_int(hStmt, field_idx++, input_mailbox_id);
11067 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11068 "UPDATE mail_box_tbl SET"
11069 " mailbox_name = ?"
11071 " WHERE mailbox_id = ?");
11073 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11075 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11076 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11078 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
11079 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
11080 _bind_stmt_field_data_int(hStmt, field_idx++ , input_mailbox_id);
11084 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11085 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
11086 ("sqlite3_step fail:%d", rc));
11087 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11088 ("sqlite3_step fail:%d", rc));
11090 if (sqlite3_changes(local_db_handle) == 0)
11091 EM_DEBUG_LOG("no mail_meeting_tbl matched...");
11096 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, error);
11097 if (error == EMAIL_ERROR_NONE) {
11098 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME, account_id, input_mailbox_id, input_new_mailbox_name, 0))
11099 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAILBOX_RENAME ] >>>> ");
11101 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME_FAIL, account_id, input_mailbox_id, input_new_mailbox_name, error))
11102 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAILBOX_RENAME_FAIL ] >>>> ");
11105 EM_SAFE_FREE(replaced_mailbox_name);
11106 EM_SAFE_FREE(replaced_alias);
11108 if (old_mailbox_data)
11109 emstorage_free_mailbox(&old_mailbox_data, 1, NULL);
11111 if (hStmt != NULL) {
11112 rc = sqlite3_finalize(hStmt);
11113 if (rc != SQLITE_OK) {
11114 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11115 error = EMAIL_ERROR_DB_FAILURE;
11119 EM_DEBUG_FUNC_END("error [%d]", error);
11123 INTERNAL_FUNC int emstorage_get_new_attachment_no(char *multi_user_name, int *attachment_no, int *err_code)
11125 EM_DEBUG_FUNC_BEGIN("attachment_no [%p], err_code[%p]", attachment_no, err_code);
11126 int rc, ret = false;
11127 int error = EMAIL_ERROR_NONE;
11128 char *sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
11131 if (!attachment_no) {
11132 EM_DEBUG_EXCEPTION("Invalid attachment");
11133 if (err_code != NULL)
11134 *err_code = EMAIL_ERROR_INVALID_PARAM;
11138 *attachment_no = -1;
11140 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11143 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
11144 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
11145 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
11147 if (NULL == result[1])
11150 rc = atoi(result[1])+1;
11152 sqlite3_free_table(result);
11154 *attachment_no = rc;
11155 EM_DEBUG_LOG("attachment_no [%d]", *attachment_no);
11160 if (err_code != NULL)
11163 EM_DEBUG_FUNC_END("ret [%d]", ret);
11167 INTERNAL_FUNC int emstorage_add_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int iscopy, int transaction, int *err_code)
11169 EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], iscopy[%d], transaction[%d], err_code[%p]", attachment_tbl, iscopy, transaction, err_code);
11173 int rc, ret = false;
11174 int error = EMAIL_ERROR_NONE;
11175 char sql_query_string[QUERY_SIZE] = {0, };
11177 DB_STMT hStmt = NULL;
11178 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11180 if (!attachment_tbl) {
11181 EM_DEBUG_EXCEPTION("attachment_tbl[%p], iscopy[%d]", attachment_tbl, iscopy);
11182 if (err_code != NULL)
11183 *err_code = EMAIL_ERROR_INVALID_PARAM;
11187 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11189 sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
11191 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
11192 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
11193 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
11195 if (NULL == result[1]) rc = 1;
11196 else rc = atoi(result[1]) + 1;
11197 sqlite3_free_table(result);
11199 attachment_tbl->attachment_id = rc;
11201 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11202 "INSERT INTO mail_attachment_tbl VALUES "
11203 "(?" /* attachment_id */
11204 ", ?" /* attachment_name */
11205 ", ?" /* attachment_path */
11206 ", ?" /* content_id */
11207 ", ?" /* attachment_size */
11208 ", ?" /* mail_id */
11209 ", ?" /* account_id */
11210 ", ?" /* mailbox_id */
11211 ", ?" /* attachment_save_status */
11212 ", ?" /* attachment_drm_type */
11213 ", ?" /* attachment_drm_method */
11214 ", ?" /* attachment_inline_content_status */
11215 ", ?" /* attachment_mime_type */
11216 #ifdef __ATTACHMENT_OPTI__
11223 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11224 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11225 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11227 _bind_stmt_field_data_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_id);
11228 _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);
11229 _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);
11230 _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);
11231 _bind_stmt_field_data_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_size);
11232 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mail_id);
11233 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->account_id);
11234 _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mailbox_id);
11235 _bind_stmt_field_data_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_save_status);
11236 _bind_stmt_field_data_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_type);
11237 _bind_stmt_field_data_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_method);
11238 _bind_stmt_field_data_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_inline_content_status);
11239 _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);
11240 #ifdef __ATTACHMENT_OPTI__
11241 _bind_stmt_field_data_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->encoding);
11242 _bind_stmt_field_data_string(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->section, 0, ATTACHMENT_LEN_IN_MAIL_ATTACHMENT_TBL);
11246 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11247 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
11248 ("sqlite3_step fail:%d", rc));
11249 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11250 ("sqlite3_step fail:%d", rc));
11252 rc = sqlite3_changes(local_db_handle);
11254 EM_DEBUG_LOG(" no matched mail found...");
11255 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11262 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11264 if (hStmt != NULL) {
11265 rc = sqlite3_finalize(hStmt);
11266 if (rc != SQLITE_OK) {
11267 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11268 error = EMAIL_ERROR_DB_FAILURE;
11272 if (err_code != NULL)
11275 EM_DEBUG_FUNC_END("ret [%d]", ret);
11279 INTERNAL_FUNC int emstorage_update_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int transaction, int *err_code)
11281 EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], transaction[%d], err_code[%p]", attachment_tbl, transaction, err_code);
11283 int rc, ret = false, field_idx = 0;
11284 int error = EMAIL_ERROR_NONE;
11285 DB_STMT hStmt = NULL;
11286 char sql_query_string[QUERY_SIZE] = {0, };
11288 if (!attachment_tbl) {
11289 EM_DEBUG_EXCEPTION(" attachment_tbl[%p] ", attachment_tbl);
11291 *err_code = EMAIL_ERROR_INVALID_PARAM;
11295 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11297 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11299 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11300 "UPDATE mail_attachment_tbl SET "
11301 " attachment_name = ?"
11302 ", attachment_path = ?"
11304 ", attachment_size = ?"
11308 ", attachment_save_status = ?"
11309 ", attachment_drm_type = ?"
11310 ", attachment_drm_method = ?"
11311 ", attachment_inline_content_status = ? "
11312 ", attachment_mime_type = ? "
11313 " WHERE attachment_id = ?;");
11316 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11318 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11319 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11321 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
11322 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
11323 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->content_id, 0, CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL);
11324 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_size);
11325 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->mail_id);
11326 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->account_id);
11327 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->mailbox_id);
11328 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_save_status);
11329 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_drm_type);
11330 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_drm_method);
11331 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_inline_content_status);
11332 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_mime_type, 0, ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL);
11333 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_id);
11336 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11337 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
11338 ("sqlite3_step fail:%d", rc));
11339 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11340 ("sqlite3_step fail:%d", rc));
11342 rc = sqlite3_changes(local_db_handle);
11344 EM_DEBUG_LOG(" no matched mail found...");
11345 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11352 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11353 if (hStmt != NULL) {
11354 rc = sqlite3_finalize(hStmt);
11355 if (rc != SQLITE_OK) {
11356 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11357 error = EMAIL_ERROR_DB_FAILURE;
11361 if (err_code != NULL)
11364 EM_DEBUG_FUNC_END("ret [%d]", ret);
11368 INTERNAL_FUNC int emstorage_delete_attachment_on_db(char *multi_user_name, int attachment_id, int transaction, int *err_code)
11370 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], transaction[%d], err_code[%p]", attachment_id, transaction, err_code);
11372 if (attachment_id < 0) {
11373 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
11374 if (err_code != NULL)
11375 *err_code = EMAIL_ERROR_INVALID_PARAM;
11380 int error = EMAIL_ERROR_NONE;
11381 char sql_query_string[QUERY_SIZE] = {0, };
11382 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11384 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11386 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
11388 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11389 if (error != EMAIL_ERROR_NONE) {
11390 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11397 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11402 EM_DEBUG_FUNC_END("ret [%d]", ret);
11406 INTERNAL_FUNC int emstorage_delete_all_attachments_of_mail(char *multi_user_name, int mail_id, int transaction, int *err_code)
11408 EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
11410 int error = EMAIL_ERROR_NONE;
11411 char sql_query_string[QUERY_SIZE] = {0, };
11412 sqlite3 *local_db_handle = NULL;
11414 if (mail_id <= 0) {
11415 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
11416 if (err_code != NULL)
11417 *err_code = EMAIL_ERROR_INVALID_PARAM;
11421 local_db_handle = emstorage_get_db_connection(multi_user_name);
11423 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11425 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
11426 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11427 if (error != EMAIL_ERROR_NONE) {
11428 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11435 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11440 EM_DEBUG_FUNC_END("ret [%d]", ret);
11444 INTERNAL_FUNC int emstorage_delete_attachment_all_on_db(char *multi_user_name, int account_id, char *mailbox, int transaction, int *err_code)
11446 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox, transaction, err_code);
11448 int error = EMAIL_ERROR_NONE;
11450 char sql_query_string[QUERY_SIZE] = {0, };
11451 char *replaced_mailbox = NULL;
11452 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11454 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11456 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl");
11458 if (account_id != ALL_ACCOUNT) /* '0' means all account */
11459 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);
11461 if (mailbox) /* NULL means all mailbox_name */ {
11462 if (strstr(mailbox, "'"))
11463 replaced_mailbox = em_replace_all_string(mailbox, "'", "''");
11465 replaced_mailbox = strdup(mailbox);
11467 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);
11468 EM_SAFE_FREE(replaced_mailbox); /*prevent 49434*/
11471 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11472 if (error != EMAIL_ERROR_NONE) {
11473 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11480 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11482 if (err_code != NULL)
11485 EM_DEBUG_FUNC_END("ret [%d]", ret);
11489 INTERNAL_FUNC int emstorage_free_attachment(emstorage_attachment_tbl_t** attachment_tbl_list, int count, int *err_code)
11491 EM_DEBUG_FUNC_BEGIN("attachment_tbl_list[%p], count[%d], err_code[%p]", attachment_tbl_list, count, err_code);
11494 if ((attachment_tbl_list == NULL) || (*attachment_tbl_list == NULL)) {
11495 EM_DEBUG_LOG("Nothing to free: attachment_tbl_list[%p], count[%d]", attachment_tbl_list, count);
11496 if (err_code != NULL)
11497 *err_code = EMAIL_ERROR_INVALID_PARAM;
11501 emstorage_attachment_tbl_t* p = *attachment_tbl_list;
11504 for (i = 0; i < count; i++) {
11505 EM_SAFE_FREE(p[i].attachment_name);
11506 EM_SAFE_FREE(p[i].attachment_path);
11507 EM_SAFE_FREE(p[i].content_id);
11508 EM_SAFE_FREE(p[i].attachment_mime_type);
11509 #ifdef __ATTACHMENT_OPTI__
11510 EM_SAFE_FREE(p[i].section);
11515 *attachment_tbl_list = NULL;
11518 if (err_code != NULL)
11519 *err_code = EMAIL_ERROR_NONE;
11520 EM_DEBUG_FUNC_END();
11524 INTERNAL_FUNC int emstorage_begin_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11526 EM_DEBUG_FUNC_BEGIN();
11527 EM_PROFILE_BEGIN(emStorageBeginTransaction);
11530 _timedlock_shm_mutex(mapped_for_db_lock, 2);
11532 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11535 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN immediate;", NULL, NULL, NULL), rc);
11536 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11537 ("SQL(BEGIN) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11539 if (ret == false) {
11540 if (err_code != NULL) *err_code = EMAIL_ERROR_DB_FAILURE;
11543 EM_PROFILE_END(emStorageBeginTransaction);
11544 EM_DEBUG_FUNC_END("ret [%d]", ret);
11548 INTERNAL_FUNC int emstorage_commit_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11550 EM_DEBUG_FUNC_BEGIN();
11554 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11556 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
11557 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; }, ("SQL(END) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11559 if (ret == false && err_code != NULL)
11560 *err_code = EMAIL_ERROR_DB_FAILURE;
11562 _unlockshm_mutex(mapped_for_db_lock);
11564 EM_DEBUG_FUNC_END("ret [%d]", ret);
11568 INTERNAL_FUNC int emstorage_rollback_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11570 EM_DEBUG_FUNC_BEGIN();
11573 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11575 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "ROLLBACK;", NULL, NULL, NULL), rc);
11576 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11577 ("SQL(ROLLBACK) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11579 if (ret == false && err_code != NULL)
11580 *err_code = EMAIL_ERROR_DB_FAILURE;
11582 _unlockshm_mutex(mapped_for_db_lock);
11584 EM_DEBUG_FUNC_END("ret [%d]", ret);
11588 INTERNAL_FUNC int emstorage_is_mailbox_full(char *multi_user_name, int account_id, email_mailbox_t *mailbox, int *result, int *err_code)
11590 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], result[%p], err_code[%p]", account_id, mailbox, result, err_code);
11592 if (account_id < FIRST_ACCOUNT_ID || !mailbox || !result) {
11594 EM_DEBUG_EXCEPTION("Invalid Parameter. accoun_id[%d], mailbox[%p]", account_id, mailbox);
11596 if (err_code != NULL)
11597 *err_code = EMAIL_ERROR_INVALID_PARAM;
11603 int error = EMAIL_ERROR_NONE;
11604 int mail_count = 0;
11606 if (!emstorage_get_mail_count(multi_user_name, account_id, mailbox->mailbox_id, &mail_count, NULL, true, &error)) {
11607 EM_DEBUG_EXCEPTION("emstorage_get_mail_count failed [%d]", error);
11612 EM_DEBUG_LOG("mail_count[%d] mail_slot_size[%d]", mail_count, mailbox->mail_slot_size);
11613 if (mail_count >= mailbox->mail_slot_size)
11624 if (err_code != NULL)
11627 EM_DEBUG_FUNC_END("ret [%d]", ret);
11631 INTERNAL_FUNC int emstorage_clear_mail_data(char *multi_user_name, int transaction, int *err_code)
11633 EM_DEBUG_FUNC_BEGIN("transaction[%d], err_code[%p]", transaction, err_code);
11636 int error = EMAIL_ERROR_NONE;
11637 char sql_query_string[QUERY_SIZE] = {0, };
11638 const email_db_object_t* tables = _g_db_tables;
11639 const email_db_object_t* indexes = _g_db_indexes;
11641 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11642 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11644 if (!emstorage_delete_dir((char *)MAIL_HOME, &error)) {
11645 EM_DEBUG_EXCEPTION(" emstorage_delete_dir failed - %d", error);
11650 if (mkdir(MAIL_HOME, DIRECTORY_PERMISSION) != 0) {
11651 EM_DEBUG_EXCEPTION(" mkdir failed ");
11652 error = EMAIL_ERROR_SYSTEM_FAILURE;
11654 error = EMAIL_ERROR_OUT_OF_MEMORY;
11657 if (mkdir(MAIL_TEMP, DIRECTORY_PERMISSION) != 0) {
11658 EM_DEBUG_EXCEPTION(" mkdir failed ");
11659 error = EMAIL_ERROR_SYSTEM_FAILURE;
11661 error = EMAIL_ERROR_OUT_OF_MEMORY;
11664 if (chmod(MAIL_TEMP, 0777) != 0)
11665 EM_DEBUG_EXCEPTION("Fail to change the permission \n");
11667 /* first clear index. */
11668 while (indexes->object_name) {
11669 if (indexes->data_flag) {
11670 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP index %s", indexes->object_name);
11671 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11672 if (error != EMAIL_ERROR_NONE) {
11673 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11680 while (tables->object_name) {
11681 if (tables->data_flag) {
11682 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP table %s", tables->object_name);
11683 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11684 if (error != EMAIL_ERROR_NONE) {
11685 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11695 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11697 if (err_code != NULL)
11700 EM_DEBUG_FUNC_END("ret [%d]", ret);
11703 /*======================= DB File Utils =============================================*/
11704 #include <dirent.h>
11705 #include <sys/types.h>
11706 #define DIR_SEPERATOR "/"
11708 INTERNAL_FUNC char *emstorage_make_directory_path_from_file_path(char *file_name)
11710 EM_DEBUG_FUNC_BEGIN("Filename [ %p ]", file_name);
11711 char delims[] = "/";
11712 char *result = NULL;
11713 gchar **token = NULL;
11715 token = g_strsplit_set(file_name, delims, 1);
11717 if (token && token[0]) {
11718 EM_DEBUG_LOG_SEC(">>>> Directory_name [ %s ]", token[0]);
11719 result = EM_SAFE_STRDUP(token[0]);
11721 EM_DEBUG_LOG(">>>> No Need to create Directory");
11728 INTERNAL_FUNC int emstorage_get_save_name(char *multi_user_name, int account_id, int mail_id, int atch_id,
11729 char *fname, char *move_buf, char *path_buf, int maxlen, int *err_code)
11731 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);
11732 EM_PROFILE_BEGIN(profile_emstorage_get_save_name);
11735 int error = EMAIL_ERROR_NONE;
11736 char *dir_name = NULL;
11737 char create_dir[1024] = {0};
11738 char *temp_file = NULL;
11739 char *prefix_path = NULL;
11740 char *modified_fname = NULL;
11742 if (!move_buf || !path_buf || account_id < FIRST_ACCOUNT_ID || mail_id < 0 || atch_id < 0) {
11743 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);
11744 error = EMAIL_ERROR_INVALID_PARAM;
11748 snprintf(path_buf, 512, "%s", MAIL_HOME);
11749 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, account_id);
11752 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, mail_id);
11755 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, atch_id);
11758 temp_file = EM_SAFE_STRDUP(fname);
11759 if (temp_file && strstr(temp_file, "/"))
11760 dir_name = emstorage_make_directory_path_from_file_path(temp_file);
11764 snprintf(create_dir, sizeof(create_dir), "%s%s%s", path_buf, DIR_SEPERATOR, dir_name);
11765 EM_DEBUG_LOG(">>>>> DIR PATH [%s]", create_dir);
11766 ret = mkdir(create_dir, DIRECTORY_PERMISSION);
11767 EM_DEBUG_LOG("mkdir result = [%d]", ret);
11768 EM_SAFE_FREE(dir_name);
11772 EM_DEBUG_LOG_DEV(">>>>> fname [%s]", fname);
11774 /* Did not allow the slash */
11775 modified_fname = reg_replace_new(fname, "/", "_");
11776 EM_DEBUG_LOG_SEC("modified_fname : [%s]", modified_fname);
11778 if (modified_fname == NULL) modified_fname = g_strdup(fname);
11780 if (EM_SAFE_STRLEN(modified_fname) + EM_SAFE_STRLEN(path_buf) + strlen(DIR_SEPERATOR) > maxlen - 1) {
11781 char *modified_name = NULL;
11782 int remain_len = (maxlen - 1) - EM_SAFE_STRLEN(path_buf) - strlen(DIR_SEPERATOR);
11784 if (remain_len <= 0) {
11785 error = EMAIL_ERROR_MAX_EXCEEDED;
11789 if (remain_len > MAX_FILENAME)
11790 remain_len = MAX_FILENAME;
11792 modified_name = em_shrink_filename(modified_fname, remain_len);
11794 if (!modified_name) {
11795 error = EMAIL_ERROR_MAX_EXCEEDED;
11799 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),
11800 "%s%s", DIR_SEPERATOR, modified_name);
11801 EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11802 EM_SAFE_FREE(modified_name);
11804 if (EM_SAFE_STRLEN(modified_fname) > MAX_FILENAME - 1) {
11805 char *modified_name = NULL;
11807 modified_name = em_shrink_filename(modified_fname, MAX_FILENAME);
11808 if (!modified_name) {
11809 error = EMAIL_ERROR_MAX_EXCEEDED;
11813 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),
11814 "%s%s", DIR_SEPERATOR, modified_name);
11815 EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11816 EM_SAFE_FREE(modified_name);
11818 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),
11819 "%s%s", DIR_SEPERATOR, modified_fname);
11824 EM_DEBUG_LOG_SEC(">>>>> path_buf [%s]", path_buf);
11826 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11827 error = emcore_get_container_path(multi_user_name, &prefix_path);
11828 if (error != EMAIL_ERROR_NONE) {
11829 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11832 snprintf(move_buf, 512, "%s/%s", prefix_path, path_buf);
11833 EM_DEBUG_LOG_SEC("move_buf : [%s]", move_buf);
11835 snprintf(move_buf, 512, "%s", path_buf);
11836 EM_DEBUG_LOG_SEC("move_buf : [%s]", move_buf);
11843 EM_SAFE_FREE(temp_file);
11844 EM_SAFE_FREE(prefix_path);
11845 EM_SAFE_FREE(modified_fname);
11847 if (err_code != NULL)
11850 EM_PROFILE_END(profile_emstorage_get_save_name);
11851 EM_DEBUG_FUNC_END("ret [%d]", ret);
11856 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)
11858 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);
11860 if (!name_buf || account_id < FIRST_ACCOUNT_ID) {
11861 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);
11862 if (err_code != NULL)
11863 *err_code = EMAIL_ERROR_INVALID_PARAM;
11867 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
11870 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, mail_id);
11875 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, atch_id);
11880 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), ".DELE");
11882 EM_DEBUG_FUNC_END();
11887 INTERNAL_FUNC int emstorage_create_dir(char *multi_user_name, int account_id, int mail_id, int atch_id, int *err_code)
11889 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], err_code[%p]", account_id, mail_id, atch_id, err_code);
11890 EM_PROFILE_BEGIN(profile_emcore_save_create_dir);
11892 int error = EMAIL_ERROR_NONE;
11895 char *prefix_path = NULL;
11896 char errno_buf[ERRNO_BUF_SIZE] = {0};
11898 memset(buf, 0x00, sizeof(buf));
11900 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11901 error = emcore_get_container_path(multi_user_name, &prefix_path);
11902 if (error != EMAIL_ERROR_NONE) {
11903 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11907 prefix_path = strdup("");
11910 if (account_id >= FIRST_ACCOUNT_ID) {
11911 SNPRINTF(buf, sizeof(buf), "%s%s%s%s%d", prefix_path,
11917 if (stat(buf, &sbuf) == 0) {
11918 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11919 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11920 error = EMAIL_ERROR_SYSTEM_FAILURE;
11924 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11925 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11926 EM_DEBUG_EXCEPTION("mkdir failed: %s", EM_STRERROR(errno_buf));
11927 error = EMAIL_ERROR_SYSTEM_FAILURE;
11929 error = EMAIL_ERROR_OUT_OF_MEMORY;
11933 if (account_id == EML_FOLDER)
11939 int space_left_in_buffer = sizeof(buf) - EM_SAFE_STRLEN(buf);
11941 if (account_id < FIRST_ACCOUNT_ID) {
11942 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11943 error = EMAIL_ERROR_INVALID_PARAM;
11947 if (space_left_in_buffer + 10 > sizeof(buf)) {
11948 EM_DEBUG_EXCEPTION("Buffer overflowed");
11949 error = EMAIL_ERROR_OUT_OF_MEMORY;
11953 SNPRINTF(buf+EM_SAFE_STRLEN(buf), space_left_in_buffer, "%s%d", DIR_SEPERATOR, mail_id);
11955 if (stat(buf, &sbuf) == 0) {
11956 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11957 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11958 error = EMAIL_ERROR_SYSTEM_FAILURE;
11962 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11963 EM_DEBUG_EXCEPTION("mkdir failed [%s]", buf);
11964 EM_DEBUG_EXCEPTION("mkdir failed [%d][%s]", errno, EM_STRERROR(errno_buf));
11965 error = EMAIL_ERROR_SYSTEM_FAILURE;
11967 error = EMAIL_ERROR_OUT_OF_MEMORY;
11971 if (account_id == EML_FOLDER)
11972 if (chmod(buf, 0777) !=0)
11973 EM_DEBUG_EXCEPTION("chmod failed ");
11978 if (account_id < FIRST_ACCOUNT_ID || mail_id <= 0) {
11979 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11980 error = EMAIL_ERROR_INVALID_PARAM;
11984 SNPRINTF(buf+EM_SAFE_STRLEN(buf), sizeof(buf)-(EM_SAFE_STRLEN(buf)+1), "%s%d", DIR_SEPERATOR, atch_id);
11986 if (stat(buf, &sbuf) == 0) {
11987 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11988 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11989 error = EMAIL_ERROR_SYSTEM_FAILURE;
11993 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11994 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11995 error = EMAIL_ERROR_SYSTEM_FAILURE;
11997 error = EMAIL_ERROR_OUT_OF_MEMORY;
12001 if (account_id == EML_FOLDER)
12002 if (chmod(buf, 0777) != 0)
12003 EM_DEBUG_EXCEPTION("Fail to change the permission \n");
12011 EM_SAFE_FREE(prefix_path);
12013 if (err_code != NULL)
12016 EM_PROFILE_END(profile_emcore_save_create_dir);
12017 EM_DEBUG_FUNC_END("ret [%d]", ret);
12021 INTERNAL_FUNC int emstorage_copy_file(char *src_file, char *dst_file, int sync_status, int *err_code)
12023 EM_DEBUG_FUNC_BEGIN_SEC("src_file[%s], dst_file[%s], err_code[%p]", src_file, dst_file, err_code);
12024 EM_DEBUG_LOG("Using the fsync function");
12026 int error = EMAIL_ERROR_NONE;
12027 struct stat st_buf;
12032 char buf[FILE_MAX_BUFFER_SIZE] = {0};
12033 char errno_buf[ERRNO_BUF_SIZE] = {0};
12035 if (!src_file || !dst_file) {
12036 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
12038 error = EMAIL_ERROR_INVALID_PARAM;
12042 if (stat(src_file, &st_buf) < 0) {
12043 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", src_file);
12045 error = EMAIL_ERROR_SYSTEM_FAILURE; /* EMAIL_ERROR_INVALID_PATH; */
12049 error = em_open(src_file, O_RDONLY, 0, &fp_src);
12050 if (error != EMAIL_ERROR_NONE) {
12051 EM_DEBUG_EXCEPTION(">>>> Source Fail em_open %s Failed: %d", src_file, error);
12055 error = em_open(dst_file, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, &fp_dst); /*prevent 24474*/
12056 if (error != EMAIL_ERROR_NONE) {
12057 EM_DEBUG_EXCEPTION(">>>> Destination Fail em_open %s: %d", dst_file, error);
12061 memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
12063 while ((nread = read(fp_src, buf, FILE_MAX_BUFFER_SIZE)) > 0) {
12064 if (nread > 0 && nread <= FILE_MAX_BUFFER_SIZE) {
12065 EM_DEBUG_LOG("Nread Value [%d]", nread);
12067 ssize_t byte_written = 0;
12068 size_t remain_byte = nread;
12072 while (remain_byte > 0 && buf_ptr && errno == 0) {
12073 byte_written = write(fp_dst, buf_ptr, remain_byte);
12075 if (byte_written < 0) {
12076 /* interrupted by a signal */
12077 if (errno == EINTR) {
12082 EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
12083 error = EMAIL_ERROR_UNKNOWN;
12086 EM_DEBUG_LOG("NWRITTEN [%zu]", byte_written);
12087 remain_byte -= byte_written;
12088 buf_ptr += byte_written;
12092 memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
12098 EM_SAFE_CLOSE(fp_src);
12100 if (fp_dst >= 0) { /*prevent 24474*/
12102 EM_DEBUG_LOG("Before fsync");
12108 if (nread < 0 || error == EMAIL_ERROR_UNKNOWN)
12111 if (err_code != NULL)
12113 EM_DEBUG_FUNC_END("ret [%d]", ret);
12116 /* create Directory if user has deleted [deepam.p@samsung.com] */
12117 INTERNAL_FUNC void emstorage_create_dir_if_delete()
12119 EM_DEBUG_FUNC_BEGIN();
12121 if (mkdir(DATA_PATH, DIRECTORY_PERMISSION) != 0)
12122 EM_DEBUG_EXCEPTION("Fail to create the directory \n");
12123 if (mkdir(EMAIL_PATH, DIRECTORY_PERMISSION) != 0)
12124 EM_DEBUG_EXCEPTION("Fail to create the directory \n");
12125 if (mkdir(MAIL_HOME, DIRECTORY_PERMISSION) != 0)
12126 EM_DEBUG_EXCEPTION("Fail to create the directory \n");
12127 if (mkdir(MAIL_TEMP, DIRECTORY_PERMISSION) != 0)
12128 EM_DEBUG_EXCEPTION("Fail to create the directory \n");
12129 if (chmod(MAIL_TEMP, 0777) != 0)
12130 EM_DEBUG_EXCEPTION("Fail to change the permission \n");
12132 EM_DEBUG_FUNC_END();
12135 static int _get_temp_file_name(char **filename, int *err_code)
12137 EM_DEBUG_FUNC_BEGIN("filename[%p], err_code[%p]", filename, err_code);
12140 int error = EMAIL_ERROR_NONE;
12142 if (filename == NULL) {
12143 EM_DEBUG_EXCEPTION(" filename[%p]", filename);
12144 error = EMAIL_ERROR_INVALID_PARAM;
12148 char tempname[512] = {0x00, };
12151 gettimeofday(&tv, NULL);
12153 unsigned int seed = time(NULL);
12154 SNPRINTF(tempname, sizeof(tempname), "%s%c%d", MAIL_TEMP, '/', rand_r(&seed));
12156 char *p = EM_SAFE_STRDUP(tempname);
12158 EM_DEBUG_EXCEPTION(" strdup failed...");
12159 error = EMAIL_ERROR_OUT_OF_MEMORY;
12168 if (err_code != NULL)
12171 EM_DEBUG_FUNC_END("ret [%d]", ret);
12175 INTERNAL_FUNC int emstorage_add_content_type(char *file_path, char *char_set, int *err_code)
12177 EM_DEBUG_FUNC_BEGIN("File path [ %p ] Character Set [ %p ] err_code [ %p]", file_path, char_set, err_code);
12179 EM_IF_NULL_RETURN_VALUE(file_path, false);
12180 EM_IF_NULL_RETURN_VALUE(char_set, false);
12181 EM_IF_NULL_RETURN_VALUE(err_code, false);
12185 struct stat st_buf;
12187 char *low_char_set = NULL;
12188 char *match_str = NULL;
12191 int error = EMAIL_ERROR_NONE;
12192 int data_count_to_written = 0;
12193 char *temp_file_name = NULL;
12195 FILE* fp_src = NULL;
12196 FILE* fp_dest = NULL;
12200 if (stat(file_path, &st_buf) < 0) {
12201 EM_DEBUG_EXCEPTION_SEC(" stat(\"%s\") failed...", file_path);
12202 error = EMAIL_ERROR_SYSTEM_FAILURE;
12206 buf_size = st_buf.st_size;
12207 EM_DEBUG_LOG(">>>> File Size [ %d ] ", buf_size);
12208 buf = (char *)calloc(1, buf_size+1);
12211 EM_DEBUG_LOG(">>> Memory cannot be allocated ");
12215 error = em_fopen(file_path, "rb", &fp_src);
12216 if (error != EMAIL_ERROR_NONE || fp_src == NULL) {
12217 EM_DEBUG_EXCEPTION_SEC(" file_path fopen failed - %s [%d]", file_path, error);
12221 if ((nread = fread(buf, 1, buf_size, fp_src)) > 0) {
12222 if (nread > 0 && nread <= buf_size) {
12223 EM_DEBUG_LOG(">>>> Nread Value [ %d ] ", nread);
12226 * 1.Add check for whether content type is there.
12227 * 2. If not based on the character set, Append it in File
12230 low_char_set = calloc(1, EM_SAFE_STRLEN(char_set) + strlen(" \" /></head>") +1); /*prevent 34359*/
12231 if (low_char_set == NULL) {
12232 EM_DEBUG_EXCEPTION("calloc failed");
12233 error = EMAIL_ERROR_OUT_OF_MEMORY;
12237 strncat(low_char_set, char_set, sizeof(low_char_set)-EM_SAFE_STRLEN(low_char_set)-1);
12238 EM_DEBUG_LOG(">>>> CHAR SET [ %s ] ", low_char_set);
12239 strncat(low_char_set, " \" /></head>", sizeof(low_char_set)-EM_SAFE_STRLEN(low_char_set)-1); /*prevent 34359*/
12240 EM_DEBUG_LOG(">>> CHARSET [ %s ] ", low_char_set);
12243 match_str = strstr(buf, CONTENT_TYPE_DATA);
12245 if (match_str == NULL) {
12246 EM_DEBUG_LOG(">>>>emstorage_add_content_type 3 ");
12247 if (fp_src != NULL) {
12251 data_count_to_written = EM_SAFE_STRLEN(low_char_set)+strlen(CONTENT_DATA)+1; /*prevent 34359*/
12252 buf1 = (char *)calloc(1, data_count_to_written);
12255 strncat(buf1, CONTENT_DATA, sizeof(buf1)-EM_SAFE_STRLEN(buf1)-1); /*prevent 34359*/
12256 EM_DEBUG_LOG(">>>>> BUF 1 [ %s ] ", buf1);
12257 strncat(buf1, low_char_set, sizeof(buf1)-EM_SAFE_STRLEN(buf1)-1);
12258 EM_DEBUG_LOG(">>>> HTML TAG DATA [ %s ] ", buf1);
12260 /* 1. Create a temporary file name */
12261 if (!_get_temp_file_name(&temp_file_name, &err)) {
12262 EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed - %d", err);
12263 if (err_code != NULL) *err_code = err;
12266 EM_DEBUG_LOG_SEC(">>>>>>> TEMP APPEND FILE PATH [ %s ] ", temp_file_name);
12268 /* Open the Temp file in Append mode */
12269 error = em_fopen(temp_file_name, "ab", &fp_dest);
12270 if (error != EMAIL_ERROR_NONE) {
12271 EM_DEBUG_EXCEPTION_SEC(" fopen failed - %s [%d]", temp_file_name, error);
12275 /* 2. write the Latest data */
12276 nwritten = fwrite(buf1, data_count_to_written-1, 1, fp_dest);
12278 if (nwritten > 0) {
12279 EM_DEBUG_LOG(" Latest Data : [%d ] bytes written ", nwritten);
12281 /* 3. Append old data */
12282 nwritten = fwrite(buf, nread-1, 1, fp_dest);
12284 if (nwritten <= 0) {
12285 EM_DEBUG_EXCEPTION(" Error Occured while writing Old data : [%d ] bytes written ", nwritten);
12286 error = EMAIL_ERROR_SYSTEM_FAILURE;
12289 EM_DEBUG_LOG(">>>> OLD data appended [ %d ] ", nwritten);
12291 if (!emstorage_move_file(temp_file_name, file_path, false, &err)) {
12292 EM_DEBUG_EXCEPTION(" emstorage_move_file failed - %d", err);
12298 EM_DEBUG_EXCEPTION(" Error Occured while writing New data : [%d ] bytes written ", nwritten);
12299 error = EMAIL_ERROR_SYSTEM_FAILURE;
12311 EM_SAFE_FREE(buf1);
12312 EM_SAFE_FREE(low_char_set);
12313 EM_SAFE_FREE(temp_file_name);
12315 if (fp_src != NULL) {
12320 if (fp_dest != NULL) {
12328 EM_DEBUG_FUNC_END("ret [%d]", ret);
12333 INTERNAL_FUNC int emstorage_move_file(char *src_file, char *dst_file, int sync_status, int *err_code)
12335 EM_DEBUG_FUNC_BEGIN("src_file[%p], dst_file[%p], err_code[%p]", src_file, dst_file, err_code);
12338 int error = EMAIL_ERROR_NONE;
12339 char errno_buf[ERRNO_BUF_SIZE] = {0};
12341 if (src_file == NULL || dst_file == NULL) {
12342 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
12343 error = EMAIL_ERROR_INVALID_PARAM;
12347 EM_DEBUG_LOG_SEC("src_file[%s], dst_file[%s]", src_file, dst_file);
12349 if (strcmp(src_file, dst_file) != 0) {
12350 EM_DEBUG_LOG("oldpath and newpath are not on the same mounted file system.");
12351 if (!emstorage_copy_file(src_file, dst_file, sync_status, &error)) {
12352 EM_DEBUG_EXCEPTION("emstorage_copy_file failed - %d", error);
12354 struct stat temp_file_stat;
12355 if (stat(src_file, &temp_file_stat) < 0)
12356 EM_DEBUG_EXCEPTION("no src file found [%s] : %s", src_file, EM_STRERROR(errno_buf));
12358 if (stat(dst_file, &temp_file_stat) < 0)
12359 EM_DEBUG_EXCEPTION("no dst file found [%s] : %s", dst_file, EM_STRERROR(errno_buf));
12361 error = EMAIL_ERROR_FILE_NOT_FOUND;
12367 EM_DEBUG_LOG("src[%s] removed", src_file);
12369 EM_DEBUG_LOG("src[%s] = dst[%s]", src_file, dst_file);
12375 if (err_code != NULL)
12378 EM_DEBUG_FUNC_END("ret [%d]", ret);
12382 INTERNAL_FUNC int emstorage_delete_file(char *src_file, int *err_code)
12384 EM_DEBUG_FUNC_BEGIN("src_file[%p], err_code[%p]", src_file, err_code);
12387 int error = EMAIL_ERROR_NONE;
12389 if (src_file == NULL) {
12390 EM_DEBUG_EXCEPTION(" src_file[%p]", src_file);
12392 error = EMAIL_ERROR_INVALID_PARAM;
12396 if (remove(src_file) != 0) {
12397 if (errno != ENOENT) {
12398 EM_DEBUG_EXCEPTION(" remove failed - %d", errno);
12400 error = EMAIL_ERROR_SYSTEM_FAILURE;
12403 EM_DEBUG_EXCEPTION(" no file found...");
12405 error = EMAIL_ERROR_FILE_NOT_FOUND;
12412 if (err_code != NULL)
12415 EM_DEBUG_FUNC_END("ret [%d]", ret);
12420 INTERNAL_FUNC int emstorage_delete_dir(char *src_dir, int *err_code)
12422 EM_DEBUG_FUNC_BEGIN("src_dir[%p], err_code[%p]", src_dir, err_code);
12424 if (src_dir == NULL) {
12425 EM_DEBUG_EXCEPTION("src_dir[%p]", src_dir);
12427 if (err_code != NULL)
12428 *err_code = EMAIL_ERROR_INVALID_PARAM;
12432 int error = EMAIL_ERROR_NONE;
12439 dirp = opendir(src_dir);
12441 if (dirp == NULL) {
12442 if (errno == ENOENT) {
12443 EM_DEBUG_EXCEPTION("directory[%s] does not exist...", src_dir);
12444 if (err_code != NULL)
12445 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12448 EM_DEBUG_EXCEPTION("opendir failed [%s] [%d]", src_dir, errno);
12449 if (err_code != NULL)
12450 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12455 while ((dp = readdir(dirp))) {
12456 if (strncmp(dp->d_name, ".", 1) == 0 || strncmp(dp->d_name, "..", 2) == 0) /* prevent 34360 */
12459 SNPRINTF(buf, sizeof(buf), "%s/%s", src_dir, dp->d_name);
12461 if (lstat(buf, &sbuf) == 0 || stat(buf, &sbuf) == 0) {
12462 /* check directory */
12463 if ((sbuf.st_mode & S_IFMT) == S_IFDIR) { /* directory */
12464 /* recursive call */
12465 if (!emstorage_delete_dir(buf, &error)) {
12467 if (err_code != NULL)
12471 } else { /* file */
12472 if (remove(buf) < 0) {
12473 EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", buf, errno);
12475 if (err_code != NULL)
12476 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12481 EM_DEBUG_EXCEPTION("content does not exist...");
12486 EM_DEBUG_LOG_DEV("remove direcotory [%s]", src_dir);
12488 /* EM_DEBUG_FUNC_BEGIN(); */
12490 if (remove(src_dir) < 0) {
12491 EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", src_dir, errno);
12492 if (err_code != NULL)
12493 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12497 if (err_code != NULL)
12503 /* faizan.h@samsung.com */
12504 INTERNAL_FUNC int emstorage_update_server_uid(char *multi_user_name,
12506 char *old_server_uid,
12507 char *new_server_uid,
12510 EM_DEBUG_FUNC_BEGIN("new_server_uid[%s], old_server_uid[%s]", new_server_uid, old_server_uid);
12512 int error = EMAIL_ERROR_NONE;
12513 int transaction = true;
12514 int temp_strlen = 0;
12515 int and_operation = 0;
12516 char sql_query_string[QUERY_SIZE] = {0, };
12517 char conditional_clause_string[QUERY_SIZE] = {0};
12519 if ((mail_id <= 0 || !old_server_uid) && !new_server_uid) {
12520 EM_DEBUG_EXCEPTION("Invalid parameters");
12522 *err_code = EMAIL_ERROR_INVALID_PARAM;
12526 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12527 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12529 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE ");
12532 temp_strlen = strlen(conditional_clause_string);
12533 SNPRINTF(conditional_clause_string + temp_strlen, sizeof(conditional_clause_string) - temp_strlen,
12534 "mail_id = %d ", mail_id);
12538 if (old_server_uid) {
12539 temp_strlen = strlen(conditional_clause_string);
12540 if (!and_operation) {
12541 sqlite3_snprintf(sizeof(conditional_clause_string) - temp_strlen, conditional_clause_string + temp_strlen,
12542 "server_mail_id = '%q'", old_server_uid);
12544 sqlite3_snprintf(sizeof(conditional_clause_string) - temp_strlen, conditional_clause_string + temp_strlen,
12545 "and server_mail_id = '%q'", old_server_uid);
12549 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
12550 "UPDATE mail_tbl SET server_mail_id = '%q' %s", new_server_uid, conditional_clause_string);
12552 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12553 if (error != EMAIL_ERROR_NONE) {
12554 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12562 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12564 if (err_code != NULL)
12567 EM_DEBUG_FUNC_END("ret [%d]", ret);
12572 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)
12574 EM_DEBUG_FUNC_BEGIN_SEC("mail_id[%d], new_server_uid[%s], mbox_name[%s]", mail_id, new_server_uid, mbox_name);
12577 int error = EMAIL_ERROR_NONE;
12578 char sql_query_string[QUERY_SIZE] = {0, };
12579 int transaction = true;
12581 if (!mail_id || !new_server_uid || !mbox_name) {
12582 EM_DEBUG_EXCEPTION("Invalid parameters");
12583 if (err_code != NULL)
12584 *err_code = EMAIL_ERROR_INVALID_PARAM;
12588 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12589 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12592 SNPRINTF(sql_query_string, sizeof(sql_query_string),
12593 "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);
12595 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12596 if (error != EMAIL_ERROR_NONE) {
12597 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12605 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12607 if (err_code != NULL)
12610 EM_DEBUG_FUNC_END("ret [%d]", ret);
12615 INTERNAL_FUNC int emstorage_update_save_status(char *multi_user_name, int account_id, int *err_code)
12617 EM_DEBUG_FUNC_BEGIN();
12620 int transaction = true;
12621 int error = EMAIL_ERROR_NONE;
12622 char sql_query_string[QUERY_SIZE] = {0,};
12624 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12626 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12628 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12630 if (account_id <= ALL_ACCOUNT)
12631 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);
12633 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);
12635 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12636 if (error != EMAIL_ERROR_NONE) {
12637 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12644 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12646 if (err_code != NULL)
12649 EM_DEBUG_FUNC_END("ret [%d]", ret);
12654 int emstorage_get_unread_mailid(char *multi_user_name, int account_id, int vip_mode, int **mail_ids, int *mail_number, int *err_code)
12656 EM_DEBUG_FUNC_BEGIN();
12658 if ((!mail_ids) || (account_id <= 0 && account_id != -1)) {
12659 EM_DEBUG_EXCEPTION(" mail_id[%p], account_id[%d] ", mail_ids, account_id);
12660 if (err_code != NULL)
12661 *err_code = EMAIL_ERROR_INVALID_PARAM;
12667 int error = EMAIL_ERROR_NONE;
12671 int *p_mail_ids = NULL;
12672 int transaction = false;
12673 char **result = NULL;
12674 char sql_query_string[QUERY_SIZE] = {0, };
12675 char temp_query_string[QUERY_SIZE] = {0,};
12676 char sql_select_query_string[QUERY_SIZE] = {0, };
12679 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12680 EMSTORAGE_START_READ_TRANSACTION(transaction);
12682 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12684 if (account_id == -1) {
12685 SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string),
12686 "SELECT mail_id FROM mail_tbl WHERE flags_seen_field = 0 AND (save_status = %d or save_status = %d)",
12687 EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12689 SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string),
12690 "SELECT mail_id FROM mail_tbl WHERE account_id = %d AND flags_seen_field = 0 AND (save_status = %d or save_status = %d)",
12691 account_id, EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12695 wrn = SNPRINTF(temp_query_string, sizeof(temp_query_string),
12696 "%s AND tag_id < 0", sql_select_query_string);
12698 SNPRINTF(temp_query_string, sizeof(temp_query_string), "%s", sql_select_query_string);
12702 EM_DEBUG_LOG("WARNING: temp query string is truncated (%s)", temp_query_string);
12705 wrn = SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s ORDER BY date_time ASC", temp_query_string);
12708 EM_DEBUG_LOG("WARNING: sql query string is truncated (%s)", sql_query_string);
12711 EM_DEBUG_LOG_SEC("query: [%s]", sql_query_string);
12713 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
12714 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
12715 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12717 EM_DEBUG_LOG("Count : %d", count);
12720 EM_DEBUG_EXCEPTION("no Mails found...");
12722 error = EMAIL_ERROR_MAIL_NOT_FOUND;
12726 p_mail_ids = em_malloc(count * sizeof(int));
12727 if (p_mail_ids == NULL) {
12728 EM_DEBUG_EXCEPTION("em_mallocfailed...");
12729 error = EMAIL_ERROR_OUT_OF_MEMORY;
12735 for (i = 0; i < count; i++)
12736 _get_table_field_data_int(result, &(p_mail_ids[i]), col_index++);
12743 sqlite3_free_table(result);
12745 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12747 // sqlite3_db_release_memory(local_db_handle);
12750 if (mail_ids != NULL)
12751 *mail_ids = p_mail_ids;
12753 if (mail_number != NULL)
12754 *mail_number = count;
12756 EM_SAFE_FREE(p_mail_ids);
12759 if (err_code != NULL)
12762 EM_DEBUG_FUNC_END("ret [%d]", ret);
12766 int setting_system_command(const char *command)
12769 int pid = 0, status = 0;
12770 char *const environ[] = { NULL };
12785 argv[2] = (char *)command;
12788 execve("/bin/sh", argv, environ);
12793 if (waitpid(pid, &status, 0) == -1) {
12794 if (errno != EINTR)
12805 INTERNAL_FUNC int emstorage_mail_get_total_diskspace_usage(unsigned long *total_usage, int transaction, int *err_code)
12807 EM_DEBUG_FUNC_BEGIN("total_usage[%p], transaction[%d], err_code[%p]", total_usage, transaction, err_code);
12809 if (!total_usage) {
12810 EM_DEBUG_EXCEPTION("total_usage[%p]", total_usage);
12812 if (err_code != NULL)
12813 *err_code = EMAIL_ERROR_INVALID_PARAM;
12818 int error = EMAIL_ERROR_NONE;
12819 char syscmd[256] = {0, };
12820 char line[256] = {0, };
12821 char *line_from_file = NULL;
12823 unsigned long total_diskusage = 0;
12825 SNPRINTF(syscmd, sizeof(syscmd), "touch %s", SETTING_MEMORY_TEMP_FILE_PATH);
12826 if (setting_system_command(syscmd) == -1) {
12827 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
12829 error = EMAIL_ERROR_SYSTEM_FAILURE;
12833 SNPRINTF(syscmd, sizeof(syscmd), "du -hsk %s > %s", EMAIL_PATH, SETTING_MEMORY_TEMP_FILE_PATH);
12834 EM_DEBUG_LOG(" cmd : %s", syscmd);
12835 if (setting_system_command(syscmd) == -1) {
12836 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : Setting > Memory] System Command [%s] is failed", syscmd);
12838 error = EMAIL_ERROR_SYSTEM_FAILURE;
12842 error = em_fopen(SETTING_MEMORY_TEMP_FILE_PATH, "r", &fp);
12843 if (error != EMAIL_ERROR_NONE) {
12844 perror(SETTING_MEMORY_TEMP_FILE_PATH);
12848 line_from_file = fgets(line, sizeof(line), fp);
12850 if (line_from_file == NULL) {
12851 EM_DEBUG_EXCEPTION("fgets failed");
12852 error = EMAIL_ERROR_SYSTEM_FAILURE;
12855 total_diskusage = strtoul(line, NULL, 10);
12857 memset(syscmd, 0, sizeof(syscmd));
12858 SNPRINTF(syscmd, sizeof(syscmd), "rm -f %s", SETTING_MEMORY_TEMP_FILE_PATH);
12859 if (setting_system_command(syscmd) == -1) {
12860 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
12861 error = EMAIL_ERROR_SYSTEM_FAILURE;
12865 EM_DEBUG_LOG("[Setting > Memory] @@@@@ Size of Directory [%s] is %ld KB", EMAIL_PATH, total_diskusage);
12870 if (err_code != NULL)
12874 *total_usage = total_diskusage;
12878 if (fp) fclose(fp); /* prevent 32730 */
12880 EM_DEBUG_FUNC_END("ret [%d]", ret);
12883 #define MAILHOME_UTF8 tzplatform_mkpath(TZ_USER_DATA, "email/.email_data/7/348/UTF-8")
12885 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)
12887 DB_STMT hStmt = NULL;
12889 int error = EMAIL_ERROR_NONE;
12891 char sql_query_string[QUERY_SIZE] = {0, };
12893 SNPRINTF(sql_query_string, sizeof(sql_query_string),
12894 "INSERT INTO mail_tbl VALUES "
12896 ", ?" /* account_id */
12897 ", ?" /* mail_size */
12898 ", ?" /* server_mail_status */
12899 ", ?" /* server_mailbox_name */
12900 ", ?" /* server_mail_id */
12901 ", ?" /* reference_mail_id */
12902 ", ?" /* full_address_from */
12903 ", ?" /* full_address_reply */
12904 ", ?" /* full_address_to */
12905 ", ?" /* full_address_cc */
12906 ", ?" /* full_address_bcc */
12907 ", ?" /* full_address_return */
12908 ", ?" /* subject */
12909 ", ?" /* body_download_status */
12910 ", ?" /* file_path_plain */
12911 ", ?" /* file_path_html */
12912 ", ?" /* date_time */
12913 ", ?" /* flags_seen_field */
12914 ", ?" /* flags_deleted_field */
12915 ", ?" /* flags_flagged_field */
12916 ", ?" /* flags_answered_field */
12917 ", ?" /* flags_recent_field */
12918 ", ?" /* flags_draft_field */
12919 ", ?" /* flags_forwarded_field */
12920 ", ?" /* DRM_status */
12921 ", ?" /* priority */
12922 ", ?" /* save_status */
12923 ", ?" /* lock_status */
12924 ", ?" /* message_id */
12925 ", ?" /* report_status */
12926 ", ?" /* email_address_sender */
12927 ", ?" /* email_address_recipient */
12928 ", ?" /* attachment_count */
12929 ", ?" /* inline_content_count */
12930 ", ?" /* preview_text */
12931 ", ?" /* thread_id */
12932 ", ?" /* mailbox_type */
12933 ", ?" /* alias_sender */
12934 ", ?" /* alias_recipient */
12935 ", ?" /* thread_item_count */
12936 ", ?" /* meeting_request_status */
12937 ", ?" /* message_class */
12938 ", ?" /* digest_type */
12939 ", ?" /* smime_type */
12940 ", ?" /* scheduled_sending_time */
12941 ", ?" /* remaining_resend_times */
12943 ", ?" /* replied_time */
12944 ", ?" /* forwarded_time */
12945 ", ?" /* default_charset */
12946 ", ?" /* eas_data_length */
12947 ", ?" /* eas_data */
12950 int transaction = true;
12951 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12953 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12956 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12957 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
12958 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12960 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_id);
12961 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, account_id);
12962 _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, EMAIL_MAILBOX_TYPE_OUTBOX);
12963 _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, "save test - long", 1, SUBJECT_LEN_IN_MAIL_TBL);
12964 _bind_stmt_field_data_string(hStmt, DATETIME_IDX_IN_MAIL_TBL, "20100316052908", 0, DATETIME_LEN_IN_MAIL_TBL);
12965 _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, 0);
12966 _bind_stmt_field_data_string(hStmt, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL, "", 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
12967 _bind_stmt_field_data_string(hStmt, SERVER_MAIL_ID_IDX_IN_MAIL_TBL, "", 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
12968 _bind_stmt_field_data_string(hStmt, MESSAGE_ID_IDX_IN_MAIL_TBL, "", 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
12969 _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, 0);
12970 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL, "<test08@streaming.s3glab.net>", 1, FROM_LEN_IN_MAIL_TBL);
12971 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL, "", 1, REPLY_TO_LEN_IN_MAIL_TBL);
12972 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL, full_address_to, 1, TO_LEN_IN_MAIL_TBL);
12973 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL, full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
12974 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL, full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
12975 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL, "", 1, RETURN_PATH_LEN_IN_MAIL_TBL);
12976 _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);
12977 _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);
12978 _bind_stmt_field_data_string(hStmt, ALIAS_SENDER_IDX_IN_MAIL_TBL, "send_alias", 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12979 _bind_stmt_field_data_string(hStmt, ALIAS_RECIPIENT_IDX_IN_MAIL_TBL, "recipient_alias", 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12980 _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, 1);
12981 _bind_stmt_field_data_string(hStmt, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL, (char *)MAILHOME_UTF8, 0, TEXT_1_LEN_IN_MAIL_TBL);
12982 _bind_stmt_field_data_string(hStmt, FILE_PATH_HTML_IDX_IN_MAIL_TBL, "", 0, TEXT_2_LEN_IN_MAIL_TBL);
12983 _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, 4);
12984 _bind_stmt_field_data_char(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, 0);
12985 _bind_stmt_field_data_char(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, 0);
12986 _bind_stmt_field_data_char(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, 0);
12987 _bind_stmt_field_data_char(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, 0);
12988 _bind_stmt_field_data_char(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, 0);
12989 _bind_stmt_field_data_char(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, 0);
12990 _bind_stmt_field_data_char(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, 0);
12991 _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, 0);
12992 _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, 0);
12993 _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, 0);
12994 _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, 0);
12995 _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, 0);
12996 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12997 _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, 0);
12998 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12999 _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, 0);
13000 _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, 0);
13001 _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, "preview body", 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
13002 _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, 0);
13003 _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, 0);
13004 _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, 0);
13005 _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, 0);
13006 _bind_stmt_field_data_int(hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, 0);
13007 _bind_stmt_field_data_int(hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, 0);
13008 _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, 0);
13009 _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, 0);
13010 _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, 0);
13011 _bind_stmt_field_data_string(hStmt, DEFAULT_CHARSET_IDX_IN_MAIL_TBL, "UTF-8", 0, TEXT_2_LEN_IN_MAIL_TBL);
13012 _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, 0);
13013 _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, NULL, 0);
13015 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13016 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
13017 ("sqlite3_step fail:%d", rc));
13018 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13019 ("sqlite3_step fail:%d", rc));
13023 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13024 if (hStmt != NULL) {
13025 rc = sqlite3_finalize(hStmt);
13026 if (rc != SQLITE_OK) {
13027 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13028 error = EMAIL_ERROR_DB_FAILURE;
13032 if (err_code != NULL)
13035 EM_DEBUG_FUNC_END("ret [%d]", ret);
13040 INTERNAL_FUNC int emstorage_get_max_mail_count()
13042 return EMAIL_MAIL_MAX_COUNT;
13045 #define STRIPPED_SUBJECT_BUFFER_SIZE 4086
13047 INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(char *multi_user_name,
13048 emstorage_mail_tbl_t *mail_tbl,
13050 int *result_latest_mail_id_in_thread,
13051 int *thread_item_count)
13053 EM_DEBUG_FUNC_BEGIN("mail_tbl [%p], thread_id [%p], "
13054 "result_latest_mail_id_in_thread [%p], thread_item_count [%p]",
13055 mail_tbl, thread_id, result_latest_mail_id_in_thread, thread_item_count);
13056 EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
13058 int rc = 0, query_size = 0, query_size_account = 0;
13060 int search_thread = false;
13061 int account_id = 0;
13062 int err_code = EMAIL_ERROR_NONE;
13063 int count = 0, result_thread_id = -1, latest_mail_id_in_thread = -1;
13064 time_t latest_date_time = 0;
13065 char *subject = NULL;
13066 char *p_subject = NULL;
13067 char *sql_query_string = NULL, *sql_account = NULL;
13069 int temp_thread_id = -1;
13070 char *sql_format = "SELECT mail_id, thread_id, date_time, subject "
13071 "FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
13072 char *sql_format_account = " AND account_id = %d ";
13073 char *sql_format_order_by = " ORDER BY thread_id, date_time DESC ";
13074 char **result = NULL;
13075 char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
13076 char stripped_subject2[STRIPPED_SUBJECT_BUFFER_SIZE];
13078 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13080 EM_DEBUG_LOG("subject: [%p], mail_id: [%d]", subject, mail_tbl->mail_id);
13082 EM_IF_NULL_RETURN_VALUE(mail_tbl, EMAIL_ERROR_INVALID_PARAM);
13083 EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
13084 EM_IF_NULL_RETURN_VALUE(result_latest_mail_id_in_thread, EMAIL_ERROR_INVALID_PARAM);
13085 EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
13087 account_id = mail_tbl->account_id;
13088 subject = mail_tbl->subject;
13090 EM_DEBUG_LOG_SEC("subject: [%s]", subject);
13092 if (EM_SAFE_STRLEN(subject) == 0 && mail_tbl->mail_id != 0) {
13093 result_thread_id = mail_tbl->mail_id;
13098 if (em_find_pos_stripped_subject_for_thread_view(subject,
13100 STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE) {
13101 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
13102 err_code = EMAIL_ERROR_UNKNOWN;
13103 result_thread_id = -1;
13107 EM_DEBUG_LOG_SEC("stripped_subject: [%s]", stripped_subject);
13109 if (EM_SAFE_STRLEN(stripped_subject) == 0) {
13110 result_thread_id = -1;
13114 EM_DEBUG_LOG_SEC("em_find_pos_stripped_subject_for_thread_view returns[len = %zu] = %s",
13115 EM_SAFE_STRLEN(stripped_subject), stripped_subject);
13117 if (account_id > 0) {
13118 query_size_account = 10 + EM_SAFE_STRLEN(sql_format_account);
13119 sql_account = malloc(query_size_account);
13120 if (sql_account == NULL) {
13121 EM_DEBUG_EXCEPTION("malloc for sql_account is failed %d", query_size_account);
13122 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
13125 snprintf(sql_account, query_size_account, sql_format_account, account_id);
13128 /* prevent 34362 */
13129 query_size = strlen(sql_format) + strlen(stripped_subject)*2 + 50 + query_size_account + strlen(sql_format_order_by); /* + query_size_mailbox; */
13130 sql_query_string = malloc(query_size);
13132 if (sql_query_string == NULL) {
13133 EM_DEBUG_EXCEPTION("malloc for sql is failed %d", query_size);
13134 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
13138 sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mail_tbl->mailbox_id);
13140 if (account_id > 0)
13141 EM_SAFE_STRNCAT(sql_query_string, sql_account, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13143 EM_SAFE_STRNCAT(sql_query_string, sql_format_order_by, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13144 EM_SAFE_STRNCAT(sql_query_string, ";", query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13146 EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
13148 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL),
13151 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13152 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13154 EM_DEBUG_LOG("Result rows count : %d", count);
13157 result_thread_id = -1;
13159 for (i = 0; i < count; i++) {
13160 EM_SAFE_FREE(p_subject);
13162 _get_table_field_data_int(result, &latest_mail_id_in_thread, col_index++);
13163 _get_table_field_data_int(result, &result_thread_id, col_index++);
13164 _get_table_field_data_time_t(result, &latest_date_time, col_index++);
13165 _get_table_field_data_string(result, &p_subject, 0, col_index++);
13167 if (temp_thread_id == result_thread_id)
13170 temp_thread_id = result_thread_id;
13172 if (em_find_pos_stripped_subject_for_thread_view(p_subject,
13174 STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE) {
13175 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
13176 err_code = EMAIL_ERROR_UNKNOWN;
13177 result_thread_id = -1;
13181 if (g_strcmp0(stripped_subject2, stripped_subject) == 0) {
13182 if (latest_date_time < mail_tbl->date_time)
13183 *result_latest_mail_id_in_thread = latest_mail_id_in_thread;
13185 *result_latest_mail_id_in_thread = mail_tbl->mail_id;
13187 search_thread = true;
13190 if (search_thread) {
13191 EM_DEBUG_LOG("latest_mail_id_in_thread [%d], mail_id [%d]",
13192 latest_mail_id_in_thread, mail_tbl->mail_id);
13195 result_thread_id = -1;
13204 *thread_id = result_thread_id;
13205 *thread_item_count = count;
13207 EM_DEBUG_LOG("Result thread id : %d", *thread_id);
13208 EM_DEBUG_LOG("Result count : %d", *thread_item_count);
13209 EM_DEBUG_LOG("err_code : %d", err_code);
13211 EM_SAFE_FREE(sql_account);
13212 EM_SAFE_FREE(sql_query_string);
13213 EM_SAFE_FREE(p_subject);
13215 sqlite3_free_table(result);
13217 EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
13222 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)
13224 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);
13225 EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
13228 int query_size = 0;
13229 int query_size_account = 0;
13230 int err_code = EMAIL_ERROR_NONE;
13232 int result_thread_id = -1;
13233 char *sql_query_string = NULL;
13234 char *sql_account = NULL;
13235 char *sql_format = "SELECT thread_id FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
13236 char *sql_format_account = " AND account_id = %d ";
13237 char *sql_format_order_by = " ORDER BY date_time DESC ";
13238 char **result = NULL;
13239 char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
13240 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13242 EM_IF_NULL_RETURN_VALUE(mail_subject, EMAIL_ERROR_INVALID_PARAM);
13243 EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
13244 EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
13246 EM_DEBUG_LOG_SEC("subject: [%s]", mail_subject);
13248 if (em_find_pos_stripped_subject_for_thread_view(mail_subject, stripped_subject, STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE) {
13249 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
13250 err_code = EMAIL_ERROR_UNKNOWN;
13251 result_thread_id = -1;
13255 if (EM_SAFE_STRLEN(stripped_subject) == 0) {
13256 result_thread_id = -1;
13260 EM_DEBUG_LOG("em_find_pos_stripped_subject_for_thread_view returns[len = %zu] = %s", EM_SAFE_STRLEN(stripped_subject), stripped_subject);
13262 if (account_id > 0) {
13263 query_size_account = 10 + EM_SAFE_STRLEN(sql_format_account);
13264 sql_account = malloc(query_size_account);
13265 if (sql_account == NULL) {
13266 EM_DEBUG_EXCEPTION("malloc for sql_account is failed %d", query_size_account);
13267 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
13270 snprintf(sql_account, query_size_account, sql_format_account, account_id);
13273 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; */
13274 sql_query_string = malloc(query_size);
13276 if (sql_query_string == NULL) {
13277 EM_DEBUG_EXCEPTION("malloc for sql is failed %d", query_size);
13278 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
13282 sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mailbox_id);
13284 if (account_id > 0)
13285 EM_SAFE_STRNCAT(sql_query_string, sql_account, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13286 EM_SAFE_STRNCAT(sql_query_string, sql_format_order_by, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13287 EM_SAFE_STRNCAT(sql_query_string, ";", query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13289 EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
13291 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
13293 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13294 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13296 EM_DEBUG_LOG("Result rows count : %d", count);
13299 result_thread_id = -1;
13301 _get_table_field_data_int(result, &result_thread_id, 1);
13304 *thread_id = result_thread_id;
13305 *thread_item_count = count;
13307 EM_DEBUG_LOG("Result thread id : %d", *thread_id);
13308 EM_DEBUG_LOG("Result count : %d", *thread_item_count);
13309 EM_DEBUG_LOG("err_code : %d", err_code);
13311 EM_SAFE_FREE(sql_account);
13312 EM_SAFE_FREE(sql_query_string);
13314 sqlite3_free_table(result);
13316 EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
13321 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)
13323 EM_DEBUG_FUNC_BEGIN();
13325 int count = 0, ret = false;
13326 int error = EMAIL_ERROR_NONE;
13327 emstorage_mail_tbl_t *p_data_tbl = NULL;
13328 char conditional_clause[QUERY_SIZE] = {0, };
13330 EM_IF_NULL_RETURN_VALUE(mail_tbl, false);
13332 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE thread_id = %d AND thread_item_count > 0", thread_id);
13333 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
13335 if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
13336 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
13341 EM_DEBUG_LOG("thread_id : %d, thread_item_count : %d", p_data_tbl[0].thread_id, p_data_tbl[0].thread_item_count);
13347 *mail_tbl = p_data_tbl;
13348 else if (p_data_tbl != NULL)
13349 emstorage_free_mail(&p_data_tbl, 1, NULL);
13351 if (err_code != NULL)
13354 EM_DEBUG_FUNC_END("ret [%d]", ret);
13358 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)
13360 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]"
13361 , account_id , mailbox_id , search_type , search_value , sorting , sender_list, sender_count, err_code);
13363 if ((!sender_list) || (!sender_count)) {
13364 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
13365 if (err_code != NULL)
13366 *err_code = EMAIL_ERROR_INVALID_PARAM;
13370 int rc = -1, ret = false;
13371 int error = EMAIL_ERROR_NONE;
13373 int i, col_index = 0;
13374 int read_count = 0;
13375 email_sender_list_t *p_sender_list = NULL;
13376 char sql_query_string[QUERY_SIZE] = {0, };
13377 char **result = NULL;
13378 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13380 SNPRINTF(sql_query_string, sizeof(sql_query_string),
13381 "SELECT email_address_sender, alias_sender, COUNT(email_address_sender), SUM(flags_seen_field = 1) "
13386 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);
13387 else /* NULL means all mailbox_name. but except for trash(3), spambox(5), all emails(for GMail, 7) */
13388 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) ");
13391 /* '0' (ALL_ACCOUNT) means all account */
13392 if (account_id > ALL_ACCOUNT)
13393 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);
13395 if (search_value) {
13396 switch (search_type) {
13397 case EMAIL_SEARCH_FILTER_SUBJECT:
13398 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13399 " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\')) ", search_value);
13401 case EMAIL_SEARCH_FILTER_SENDER:
13402 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13403 " AND ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13404 ") ", search_value);
13406 case EMAIL_SEARCH_FILTER_RECIPIENT:
13407 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13408 " AND ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13409 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13410 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13411 ") ", search_value, search_value, search_value);
13413 case EMAIL_SEARCH_FILTER_ALL:
13414 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13415 " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') "
13416 " OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13417 " OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13418 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13419 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13422 ")", search_value, search_value, search_value, search_value, search_value);
13428 /* sorting option is not available now. The order of sender list is ascending order by display name */
13429 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
13430 "GROUP BY email_address_sender "
13431 "ORDER BY UPPER(alias_sender) ");
13433 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
13435 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
13436 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13437 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13439 EM_DEBUG_LOG("Count of Sender [%d]", count);
13441 if (!(p_sender_list = (email_sender_list_t*)em_malloc(sizeof(email_sender_list_t) * count))) {
13442 EM_DEBUG_EXCEPTION("em_mallocfailed...");
13443 error = EMAIL_ERROR_OUT_OF_MEMORY;
13449 EM_DEBUG_LOG(">>>> DATA ASSIGN START >>");
13450 for (i = 0; i < count; i++) {
13451 _get_table_field_data_string(result, &(p_sender_list[i].address), 1, col_index++);
13452 _get_table_field_data_string(result, &(p_sender_list[i].display_name), 1, col_index++);
13453 _get_table_field_data_int(result, &(p_sender_list[i].total_count), col_index++);
13454 _get_table_field_data_int(result, &(read_count), col_index++);
13455 p_sender_list[i].unread_count = p_sender_list[i].total_count - read_count; /* unread count = total - read */
13457 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >>", count);
13459 sqlite3_free_table(result);
13466 *sender_list = p_sender_list;
13467 *sender_count = count;
13468 EM_DEBUG_LOG(">>>> COUNT : %d >>", count);
13472 if (err_code != NULL)
13475 EM_DEBUG_FUNC_END("ret [%d]", ret);
13479 INTERNAL_FUNC int emstorage_free_sender_list(email_sender_list_t **sender_list, int count)
13481 EM_DEBUG_FUNC_BEGIN("sender_list[%p], count[%d]", sender_list, count);
13483 int err = EMAIL_ERROR_NONE;
13486 if (!sender_list || !*sender_list) {
13487 EM_DEBUG_EXCEPTION("sender_list[%p], count[%d]", sender_list, count);
13488 err = EMAIL_ERROR_INVALID_PARAM;
13492 email_sender_list_t* p = *sender_list;
13495 for (; i < count; i++) {
13496 EM_SAFE_FREE(p[i].address);
13497 EM_SAFE_FREE(p[i].display_name);
13501 *sender_list = NULL;
13508 INTERNAL_FUNC int emstorage_free_address_info_list(email_address_info_list_t **address_info_list)
13510 EM_DEBUG_FUNC_BEGIN("address_info_list[%p]", address_info_list);
13512 int err = EMAIL_ERROR_NONE;
13513 email_address_info_t *p_address_info = NULL;
13514 GList *list = NULL;
13515 GList *node = NULL;
13518 if (!address_info_list || !*address_info_list) {
13519 EM_DEBUG_EXCEPTION("address_info_list[%p]", address_info_list);
13520 err = EMAIL_ERROR_INVALID_PARAM;
13524 /* delete GLists */
13525 for (i = EMAIL_ADDRESS_TYPE_FROM; i <= EMAIL_ADDRESS_TYPE_BCC; i++) {
13527 case EMAIL_ADDRESS_TYPE_FROM:
13528 list = (*address_info_list)->from;
13530 case EMAIL_ADDRESS_TYPE_TO:
13531 list = (*address_info_list)->to;
13533 case EMAIL_ADDRESS_TYPE_CC:
13534 list = (*address_info_list)->cc;
13536 case EMAIL_ADDRESS_TYPE_BCC:
13537 list = (*address_info_list)->bcc;
13541 /* delete dynamic-allocated memory for each item */
13542 node = g_list_first(list);
13543 while (node != NULL) {
13544 p_address_info = (email_address_info_t*)node->data;
13545 EM_SAFE_FREE(p_address_info->address);
13546 EM_SAFE_FREE(p_address_info->display_name);
13547 EM_SAFE_FREE(node->data);
13549 node = g_list_next(node);
13554 EM_SAFE_FREE(*address_info_list);
13555 *address_info_list = NULL;
13557 EM_DEBUG_FUNC_END("err [%d]", err);
13560 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
13562 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)
13564 EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
13566 if (!local_activity || !activity_id) {
13567 EM_DEBUG_EXCEPTION("local_activity[%p], transaction[%d], activity_id[%p], err_code[%p]", local_activity, transaction, activity_id, err_code);
13568 if (err_code != NULL)
13569 *err_code = EMAIL_ERROR_INVALID_PARAM;
13575 int error = EMAIL_ERROR_NONE;
13578 char sql_query_string[QUERY_SIZE] = {0, };
13579 DB_STMT hStmt = NULL;
13580 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13582 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13583 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13584 SNPRINTF(sql_query_string, sizeof(sql_query_string),
13585 "INSERT INTO mail_partial_body_activity_tbl VALUES "
13587 "? " /* Account ID */
13588 ",?" /* Local Mail ID */
13589 ",?" /* Server mail ID */
13590 ",?" /* Activity ID */
13591 ",?" /* Activity type*/
13592 ",?" /* Mailbox ID*/
13593 ",?" /* Mailbox name*/
13594 ",?" /* Multi User Name */
13597 char *sql = "SELECT max(rowid) FROM mail_partial_body_activity_tbl;";
13598 char **result = NULL;
13601 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13603 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13604 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13606 if (NULL == result[1]) rc = 1;
13607 else rc = atoi(result[1])+1;
13608 sqlite3_free_table(result);
13611 *activity_id = local_activity->activity_id = rc;
13613 EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [ %d ], MAIL ID [ %d ], ACTIVITY TYPE [ %d ], SERVER MAIL ID [ %lu ]", \
13614 local_activity->activity_id, local_activity->mail_id, local_activity->activity_type, local_activity->server_mail_id);
13616 if (local_activity->mailbox_id)
13617 EM_DEBUG_LOG(" MAILBOX ID [ %d ]", local_activity->mailbox_id);
13620 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13621 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13622 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13625 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
13626 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
13627 _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
13628 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
13629 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
13630 _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
13631 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->mailbox_name, 0, 3999);
13632 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
13635 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13637 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
13638 ("sqlite3_step fail:%d", rc));
13639 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13640 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
13645 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13646 if (hStmt != NULL) {
13647 rc = sqlite3_finalize(hStmt);
13649 if (rc != SQLITE_OK) {
13650 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13651 error = EMAIL_ERROR_DB_FAILURE;
13655 if (err_code != NULL)
13658 EM_DEBUG_FUNC_END("ret [%d]", ret);
13662 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)
13664 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13666 if (account_id < FIRST_ACCOUNT_ID || NULL == mailbox_list || *mailbox_list == NULL || NULL == count) {
13667 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13668 if (err_code != NULL)
13669 *err_code = EMAIL_ERROR_INVALID_PARAM;
13674 int error = EMAIL_ERROR_NONE;
13676 int i = 0, rc = -1;
13677 int *mbox_list = NULL;
13678 DB_STMT hStmt = NULL;
13679 char sql_query_string[QUERY_SIZE] = {0, };
13681 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13682 EMSTORAGE_START_READ_TRANSACTION(transaction);
13684 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13685 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);
13688 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13689 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13690 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13692 *count = atoi(result[1]);
13693 sqlite3_free_table(result);
13696 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
13697 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13701 EM_DEBUG_LOG("Mailbox count = %d", *count);
13703 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13705 /* 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); */
13706 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);
13708 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13711 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13714 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13715 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13718 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13719 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13720 ("sqlite3_step fail:%d", rc));
13722 mbox_list = (int *)em_malloc(sizeof(int) * (*count));
13723 if (NULL == mbox_list) {
13724 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
13725 error = EMAIL_ERROR_OUT_OF_MEMORY;
13729 memset(mbox_list, 0x00, sizeof(int) * (*count));
13731 for (i = 0; i < (*count); i++) {
13732 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
13733 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13734 /* EM_DEBUG_LOG("In emstorage_get_pdb_mailbox_list() loop, After sqlite3_step(), , i = %d, rc = %d.", i, rc); */
13735 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13736 ("sqlite3_step fail:%d", rc));
13737 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
13744 *mailbox_list = mbox_list;
13746 EM_SAFE_FREE(mbox_list);
13748 if (hStmt != NULL) {
13749 rc = sqlite3_finalize(hStmt);
13751 if (rc != SQLITE_OK) {
13752 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13753 error = EMAIL_ERROR_DB_FAILURE;
13757 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13758 if (err_code != NULL)
13760 EM_DEBUG_FUNC_END("ret [%d]", ret);
13764 INTERNAL_FUNC int emstorage_get_pbd_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
13766 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13768 if (NULL == account_list || NULL == count) {
13769 EM_DEBUG_EXCEPTION("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13770 if (err_code != NULL)
13771 *err_code = EMAIL_ERROR_INVALID_PARAM;
13776 int error = EMAIL_ERROR_NONE;
13777 char *sql = "SELECT count(distinct account_id) FROM mail_partial_body_activity_tbl";
13779 int i = 0, rc = -1;
13780 int *result_account_list = NULL;
13781 DB_STMT hStmt = NULL;
13782 char sql_query_string[QUERY_SIZE] = {0, };
13784 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13786 EMSTORAGE_START_READ_TRANSACTION(transaction);
13788 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13789 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13790 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13792 *count = atoi(result[1]);
13793 sqlite3_free_table(result);
13796 EM_DEBUG_EXCEPTION("no account found...");
13797 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13802 EM_DEBUG_LOG("Account count [%d]", *count);
13804 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13806 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_partial_body_activity_tbl");
13808 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13811 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13813 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
13814 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13815 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13818 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13819 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13820 ("sqlite3_step fail:%d", rc));
13822 if (NULL == (result_account_list = (int *)em_malloc(sizeof(int) * (*count)))) {
13823 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
13824 error = EMAIL_ERROR_OUT_OF_MEMORY;
13828 memset(result_account_list, 0x00, sizeof(int) * (*count));
13830 for (i = 0; i < (*count); i++) {
13831 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
13833 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13834 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13835 ("sqlite3_step fail:%d", rc));
13836 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
13843 *account_list = result_account_list;
13845 EM_SAFE_FREE(result_account_list);
13847 if (hStmt != NULL) {
13848 rc = sqlite3_finalize(hStmt);
13850 if (rc != SQLITE_OK) {
13851 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13852 error = EMAIL_ERROR_DB_FAILURE;
13856 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13857 if (err_code != NULL)
13859 EM_DEBUG_FUNC_END("ret [%d]", ret);
13863 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)
13865 EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
13867 if (account_id < FIRST_ACCOUNT_ID || NULL == event_start || 0 == input_mailbox_id || NULL == count) {
13868 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);
13870 if (err_code != NULL)
13871 *err_code = EMAIL_ERROR_INVALID_PARAM;
13878 int error = EMAIL_ERROR_NONE;
13880 DB_STMT hStmt = NULL;
13881 email_event_partial_body_thd* event_list = NULL;
13882 char sql_query_string[QUERY_SIZE] = {0, };
13884 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13886 EMSTORAGE_START_READ_TRANSACTION(transaction);
13888 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13889 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);
13892 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13893 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13894 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13896 *count = atoi(result[1]);
13897 sqlite3_free_table(result);
13899 EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
13902 EM_DEBUG_LOG("No matched activity found in mail_partial_body_activity_tbl");
13903 error = EMAIL_ERROR_MAIL_NOT_FOUND;
13907 EM_DEBUG_LOG("Activity Count = %d", *count);
13909 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13910 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);
13912 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13915 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13917 EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
13918 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13919 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13922 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13923 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13924 ("sqlite3_step fail:%d", rc));
13926 if (!(event_list = (email_event_partial_body_thd*)em_malloc(sizeof(email_event_partial_body_thd) * (*count)))) {
13927 EM_DEBUG_EXCEPTION("Malloc failed");
13929 error = EMAIL_ERROR_OUT_OF_MEMORY;
13932 memset(event_list, 0x00, sizeof(email_event_partial_body_thd) * (*count));
13934 for (i = 0; i < (*count); i++) {
13935 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_IDX_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13936 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13937 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13938 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13939 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_type), ACTIVITY_TYPE_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13940 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13941 _get_stmt_field_data_string(hStmt, &(event_list[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13942 _get_stmt_field_data_string(hStmt, &(event_list[i].multi_user_name), 0, MULTI_USER_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13944 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13945 /* EM_DEBUG_LOG("In emstorage_get_pbd_activity_data() loop, After sqlite3_step(), , i = %d, rc = %d.", i, rc); */
13946 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13947 ("sqlite3_step fail:%d", rc));
13949 event_list[i].event_type = 0;
13956 *event_start = event_list;
13958 for (i = 0; i < (*count); i++)
13959 emcore_free_partial_body_thd_event(event_list, NULL);
13960 EM_SAFE_FREE(event_list); /*prevent 54559*/
13961 *event_start = NULL;
13965 if (hStmt != NULL) {
13966 rc = sqlite3_finalize(hStmt);
13968 if (rc != SQLITE_OK) {
13969 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13970 error = EMAIL_ERROR_DB_FAILURE;
13974 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13975 if (err_code != NULL)
13978 EM_DEBUG_FUNC_END("ret [%d]", ret);
13982 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)
13984 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);
13987 if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
13988 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);
13990 if (err_code != NULL)
13991 *err_code = EMAIL_ERROR_INVALID_PARAM;
13997 int error = EMAIL_ERROR_NONE;
13998 char sql_query_string[QUERY_SIZE] = {0, };
13999 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14001 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14002 memset(sql_query_string, 0x00, sizeof(sql_query_string));
14004 if (activity_id == 0)
14005 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);
14007 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);
14009 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14010 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14011 if (error != EMAIL_ERROR_NONE) {
14012 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14016 /* validate activity existence */
14017 rc = sqlite3_changes(local_db_handle);
14019 EM_DEBUG_EXCEPTION("No matching activity found");
14020 error = EMAIL_ERROR_DATA_NOT_FOUND;
14028 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14030 if (err_code != NULL)
14033 EM_DEBUG_FUNC_END("ret [%d]", ret);
14037 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)
14039 EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
14041 if (account_id < FIRST_ACCOUNT_ID || NULL == activity_count || NULL == err_code) {
14042 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
14043 if (err_code != NULL)
14044 *err_code = EMAIL_ERROR_INVALID_PARAM;
14049 int error = EMAIL_ERROR_NONE;
14050 char sql_query_string[QUERY_SIZE] = {0, };
14052 DB_STMT hStmt = NULL;
14054 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14056 EMSTORAGE_START_READ_TRANSACTION(transaction);
14057 memset(sql_query_string, 0x00, sizeof(sql_query_string));
14059 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);
14061 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
14064 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14065 EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
14066 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14067 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14070 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14071 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14072 ("sqlite3_step fail:%d", rc));
14074 _get_stmt_field_data_int(hStmt, activity_count, 0);
14076 EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
14082 if (hStmt != NULL) {
14083 rc = sqlite3_finalize(hStmt);
14085 if (rc != SQLITE_OK) {
14086 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14087 error = EMAIL_ERROR_DB_FAILURE;
14091 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14093 if (err_code != NULL)
14096 EM_DEBUG_FUNC_END("ret [%d]", ret);
14100 INTERNAL_FUNC int emstorage_get_pbd_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
14102 EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
14104 if (NULL == activity_count || NULL == err_code) {
14105 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
14106 if (err_code != NULL)
14107 *err_code = EMAIL_ERROR_INVALID_PARAM;
14112 int error = EMAIL_ERROR_NONE;
14113 DB_STMT hStmt = NULL;
14114 char sql_query_string[QUERY_SIZE] = {0, };
14116 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14118 EMSTORAGE_START_READ_TRANSACTION(transaction);
14119 memset(sql_query_string, 0x00, sizeof(sql_query_string));
14121 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl;");
14123 EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
14125 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14126 EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
14127 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14128 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14130 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14131 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14132 ("sqlite3_step fail:%d", rc));
14134 _get_stmt_field_data_int(hStmt, activity_count, 0);
14136 EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
14143 if (hStmt != NULL) {
14144 rc = sqlite3_finalize(hStmt);
14146 if (rc != SQLITE_OK) {
14147 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14148 error = EMAIL_ERROR_DB_FAILURE;
14152 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14153 if (err_code != NULL)
14156 EM_DEBUG_FUNC_END("ret [%d]", ret);
14160 INTERNAL_FUNC int emstorage_delete_full_pbd_activity_data(char *multi_user_name, int account_id, int transaction, int *err_code)
14162 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
14163 if (account_id < FIRST_ACCOUNT_ID) {
14164 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
14165 if (err_code != NULL)
14166 *err_code = EMAIL_ERROR_INVALID_PARAM;
14172 int error = EMAIL_ERROR_NONE;
14173 char sql_query_string[QUERY_SIZE] = {0, };
14175 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14177 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14178 memset(sql_query_string, 0x00, sizeof(sql_query_string));
14179 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d", account_id);
14181 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14182 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14183 if (error != EMAIL_ERROR_NONE) {
14184 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14188 rc = sqlite3_changes(local_db_handle);
14190 EM_DEBUG_EXCEPTION("No matching activities found in mail_partial_body_activity_tbl");
14191 error = EMAIL_ERROR_DATA_NOT_FOUND;
14200 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14202 if (err_code != NULL)
14205 EM_DEBUG_FUNC_END("ret [%d]", ret);
14209 /*Himanshu[h.gahlaut]-> Added below API to update mail_partial_body_activity_tbl
14210 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*/
14212 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)
14214 EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
14216 int rc = -1, ret = false;
14217 int error = EMAIL_ERROR_NONE;
14218 char sql_query_string[QUERY_SIZE] = {0, };
14220 int transaction = true;
14222 if (!old_server_uid || !new_server_uid || !mbox_name) {
14223 EM_DEBUG_EXCEPTION("Invalid parameters");
14224 error = EMAIL_ERROR_INVALID_PARAM;
14228 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14230 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14231 memset(sql_query_string, 0x00, sizeof(sql_query_string));
14232 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14233 "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);
14235 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14236 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14237 if (error != EMAIL_ERROR_NONE) {
14238 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14242 rc = sqlite3_changes(local_db_handle);
14244 EM_DEBUG_LOG("No matching found in mail_partial_body_activity_tbl");
14249 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14251 if (err_code != NULL)
14254 EM_DEBUG_FUNC_END("ret [%d]", ret);
14259 INTERNAL_FUNC int emstorage_create_file(char *data_string, size_t file_size, char *dst_file_name, int *err_code)
14261 EM_DEBUG_FUNC_BEGIN_SEC("file_size[%d] , dst_file_name[%s], err_code[%p]", file_size, dst_file_name, err_code);
14264 int error = EMAIL_ERROR_NONE;
14265 FILE* fp_dst = NULL;
14266 char errno_buf[ERRNO_BUF_SIZE] = {0};
14268 if (!data_string || !dst_file_name) {
14269 EM_DEBUG_LOG("data_string[%p], dst_file_name[%p]", data_string, dst_file_name);
14270 error = EMAIL_ERROR_INVALID_PARAM;
14274 error = em_fopen(dst_file_name, "w", &fp_dst);
14275 if (error != EMAIL_ERROR_NONE) {
14276 EM_DEBUG_EXCEPTION_SEC("em_fopen failed - %s: %d", dst_file_name, error);
14280 if (fwrite(data_string, 1, file_size, fp_dst) == 0) {
14281 EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
14282 error = EMAIL_ERROR_UNKNOWN;
14290 if (fp_dst != NULL)
14293 if (err_code != NULL)
14296 EM_DEBUG_FUNC_END("ret [%d]", ret);
14300 #endif /* __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
14304 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
14305 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)
14307 EM_DEBUG_FUNC_BEGIN();
14310 int error = EMAIL_ERROR_NONE;
14311 char sql_query_string[QUERY_SIZE] = {0, };
14313 int transaction = true;
14315 if (!old_server_uid || !new_server_uid || !mbox_name) {
14316 EM_DEBUG_EXCEPTION("Invalid parameters");
14317 if (err_code != NULL)
14318 *err_code = EMAIL_ERROR_INVALID_PARAM;
14322 EM_DEBUG_LOG_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
14324 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14325 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14328 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14329 "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);
14331 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
14332 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14333 if (error != EMAIL_ERROR_NONE) {
14334 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14338 rc = sqlite3_changes(local_db_handle);
14340 EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
14341 error = EMAIL_ERROR_DATA_NOT_FOUND;
14348 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14350 if (err_code != NULL)
14353 EM_DEBUG_FUNC_END("ret [%d]", ret);
14360 * @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);
14361 * Prepare an array of mail_id and corresponding server mail id.
14363 * @author h.gahlaut@samsung.com
14364 * @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)
14365 * where 88 is the length of fixed keywords including ending null character in the QUERY to be formed
14366 * @param[out] idset Returns the array of mail_id and corresponding server_mail_id sorted by server_mail_ids
14367 * @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
14368 * @param[out] err_code Returns the error code.
14369 * @remarks An Example of Query to be exexuted in this API:
14370 * SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (12, 13, 56, 78);
14371 * @return This function returns true on success or false on failure.
14373 INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *multi_user_name,
14375 email_id_set_t** idset,
14379 EM_DEBUG_FUNC_BEGIN();
14380 EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds);
14382 int error = EMAIL_ERROR_NONE;
14384 email_id_set_t* p_id_set = NULL;
14386 const int buf_size = QUERY_SIZE;
14387 char sql_query_string[QUERY_SIZE] = {0, };
14388 int space_left_in_query_buffer = buf_size;
14391 char *server_mail_id = NULL;
14392 char **result = NULL;
14396 if (NULL == mail_ids || NULL == idset || NULL == id_set_count) {
14397 EM_DEBUG_EXCEPTION("Invalid Parameters mail_ids[%p] idset[%p] id_set_count [%p]",
14398 mail_ids, idset, id_set_count);
14399 if (err_code != NULL)
14400 *err_code = EMAIL_ERROR_INVALID_PARAM;
14404 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14406 SNPRINTF(sql_query_string, space_left_in_query_buffer,
14407 "SELECT local_uid, server_uid FROM mail_read_mail_uid_tbl WHERE local_uid in (%s) ORDER BY server_uid",
14410 EM_DEBUG_LOG_SEC("SQL Query formed [%s] ", sql_query_string);
14412 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14413 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14414 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result);
14415 goto FINISH_OFF; }, ("SQL(%s) sqlite3_get_table fail:%d -%s",
14416 sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14418 EM_DEBUG_LOG(" Count of mails [%d ]", count);
14421 EM_DEBUG_EXCEPTION("Can't find proper mail");
14422 error = EMAIL_ERROR_DATA_NOT_FOUND;
14426 if (NULL == (p_id_set = (email_id_set_t*)em_malloc(sizeof(email_id_set_t) * count))) {
14427 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
14428 error = EMAIL_ERROR_OUT_OF_MEMORY;
14434 EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds_Loop);
14435 EM_DEBUG_LOG(">>>> DATA ASSIGN START");
14436 for (i = 0; i < count; i++) {
14437 _get_table_field_data_int(result, &(p_id_set[i].mail_id), col_index++);
14438 _get_table_field_data_string(result, &server_mail_id, 1, col_index++);
14439 if (server_mail_id) {
14440 p_id_set[i].server_mail_id = strtoul(server_mail_id, NULL, 10);
14441 EM_SAFE_FREE(server_mail_id);
14444 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d]", count);
14445 EM_PROFILE_END(EmStorageGetIdSetFromMailIds_Loop);
14447 sqlite3_free_table(result);
14456 *id_set_count = count;
14457 EM_DEBUG_LOG(" idset[%p] id_set_count [%d]", *idset, *id_set_count);
14459 EM_SAFE_FREE(p_id_set);
14462 if (err_code != NULL)
14465 EM_PROFILE_END(EmStorageGetIdSetFromMailIds);
14467 EM_DEBUG_FUNC_END("ret [%d]", ret);
14475 INTERNAL_FUNC int emstorage_delete_triggers_from_lucene(char *multi_user_name)
14477 EM_DEBUG_FUNC_BEGIN();
14478 int ret = true, transaction = true;
14479 int error = EMAIL_ERROR_NONE;
14480 char sql_query_string[QUERY_SIZE] = {0, };
14481 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14482 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14484 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerDelete;");
14485 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14486 if (error != EMAIL_ERROR_NONE) {
14487 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14491 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerInsert;");
14492 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14493 if (error != EMAIL_ERROR_NONE) {
14494 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14498 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerUpdate;");
14499 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14500 if (error != EMAIL_ERROR_NONE) {
14501 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14508 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14510 EM_DEBUG_FUNC_END("ret [%d]", ret);
14514 INTERNAL_FUNC int emstorage_update_tag_id(char *multi_user_name, int old_filter_id, int new_filter_id, int *err_code)
14516 EM_DEBUG_FUNC_BEGIN("new_filter_id[%d], old_filter_id[%d]", new_filter_id, old_filter_id);
14518 int error = EMAIL_ERROR_NONE;
14519 char sql_query_string[QUERY_SIZE] = {0, };
14520 int transaction = true;
14522 if (old_filter_id < 0 || new_filter_id < 0) {
14523 EM_DEBUG_EXCEPTION("Invalid parameters");
14524 if (err_code != NULL)
14525 *err_code = EMAIL_ERROR_INVALID_PARAM;
14529 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14531 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14533 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14534 "UPDATE mail_tbl SET tag_id=%d WHERE tag_id=%d ", new_filter_id, old_filter_id);
14536 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14537 if (error != EMAIL_ERROR_NONE) {
14538 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14545 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14547 if (err_code != NULL)
14550 EM_DEBUG_FUNC_END("ret [%d]", ret);
14554 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)
14556 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);
14558 if ((account_id < 0) || (dest_mailbox_id < 0) || (!rule)) {
14559 EM_DEBUG_EXCEPTION("Invalid Parameter");
14561 if (err_code != NULL)
14562 *err_code = EMAIL_ERROR_INVALID_PARAM;
14566 int rc = -1, ret = false, error = EMAIL_ERROR_NONE;
14567 int count = 0, col_index = 0, i = 0, where_pararaph_length = 0, *mail_list = NULL;
14568 int tag_id = rule->rule_id;
14569 char **result = NULL, *where_pararaph = NULL;
14570 char sql_query_string[QUERY_SIZE] = {0, };
14571 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14573 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl ");
14575 where_pararaph_length = EM_SAFE_STRLEN(rule->value) + 2 * (EM_SAFE_STRLEN(rule->value2)) + 100;
14576 where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
14577 if (where_pararaph == NULL) {
14578 EM_DEBUG_EXCEPTION("malloc failed for where_pararaph.");
14579 error = EMAIL_ERROR_OUT_OF_MEMORY;
14583 if (account_id != ALL_ACCOUNT)
14584 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (0)", account_id);
14586 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_type NOT in (0)");
14588 if (rule->type & EMAIL_FILTER_SUBJECT) {
14589 if (rule->flag2 == RULE_TYPE_INCLUDES)
14590 sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject like \'%%%q%%\'", rule->value);
14591 else /* RULE_TYPE_EXACTLY */
14592 sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject = \'%q\'", rule->value);
14595 if (rule->type & EMAIL_FILTER_FROM) {
14596 if (rule->flag2 == RULE_TYPE_INCLUDES)
14597 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);
14598 #ifdef __FEATURE_COMPARE_DOMAIN__
14599 else if (rule->flag2 == RULE_TYPE_COMPARE_DOMAIN)
14600 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);
14601 #endif /*__FEATURE_COMPARE_DOMAIN__ */
14602 else /* RULE_TYPE_EXACTLY */
14603 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);
14606 if (rule->type == EMAIL_PRIORITY_SENDER) {
14607 if (rule->flag2 == RULE_TYPE_INCLUDES)
14608 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);
14609 else /* RULE_TYPE_EXACTLY */
14610 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);
14612 tag_id = PRIORITY_SENDER_TAG_ID;
14615 /* prevent 34361 */
14616 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14617 EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14618 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14620 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14621 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14622 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
14623 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14625 EM_DEBUG_LOG("Count of mails [%d]", count);
14628 mail_list = malloc(sizeof(int) * count);
14629 if (mail_list == NULL) {
14630 EM_DEBUG_EXCEPTION("malloc failed for mail_list.");
14631 error = EMAIL_ERROR_OUT_OF_MEMORY;
14637 for (i = 0; i < count; i++)
14638 _get_table_field_data_int(result, &(mail_list[i]), col_index++);
14640 memset(sql_query_string, 0x00, QUERY_SIZE);
14642 switch (rule->action_type) {
14643 case EMAIL_FILTER_MOVE:
14644 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = 0 ");
14646 case EMAIL_FILTER_BLOCK:
14648 EM_DEBUG_LOG("Not support : action_type[%d]", rule->action_type);
14653 switch (rule->action_type) {
14654 case EMAIL_FILTER_MOVE:
14655 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = %d ", tag_id);
14657 case EMAIL_FILTER_BLOCK:
14658 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET mailbox_id = %d, mailbox_type = %d ", dest_mailbox_id, dest_mailbox_type);
14661 EM_DEBUG_LOG("Not support");
14666 /* prevent 34361 */
14667 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14668 EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14670 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14671 if (error != EMAIL_ERROR_NONE) {
14672 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14676 #ifdef __FEATURE_BODY_SEARCH__
14677 /* Updating mail_text_tbl */
14678 if (rule->action_type == EMAIL_FILTER_BLOCK) {
14679 memset(sql_query_string, 0x00, QUERY_SIZE);
14680 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_text_tbl SET mailbox_id = %d ", dest_mailbox_id);
14681 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14682 EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14684 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14685 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14686 if (error != EMAIL_ERROR_NONE) {
14687 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14699 if (filtered_mail_id_list)
14700 *filtered_mail_id_list = mail_list;
14702 if (count_of_mails)
14703 *count_of_mails = count;
14705 EM_SAFE_FREE(mail_list);
14707 sqlite3_free_table(result);
14711 EM_SAFE_FREE(where_pararaph);
14713 if (err_code != NULL)
14716 EM_DEBUG_FUNC_END("ret [%d]", ret);
14720 #define EMAIL_SLOT_UNIT 25
14721 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)
14723 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);
14724 int ret = false, err = EMAIL_ERROR_NONE;
14725 int where_pararaph_length = 0;
14726 char *where_pararaph = NULL;
14727 char sql_query_string[QUERY_SIZE] = {0, };
14729 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14731 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
14733 if (new_slot_size > 0)
14734 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = %d ", new_slot_size);
14735 else if (new_slot_size == 0)
14736 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", EMAIL_SLOT_UNIT);
14738 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", new_slot_size * -1);
14742 where_pararaph_length = 80;
14744 where_pararaph_length = 50;
14746 if (new_slot_size == 0)
14747 where_pararaph_length += 70;
14749 where_pararaph = malloc(sizeof(char) * where_pararaph_length);
14750 if (where_pararaph == NULL) {
14751 EM_DEBUG_EXCEPTION("Memory allocation failed for where_pararaph");
14752 err = EMAIL_ERROR_OUT_OF_MEMORY;
14755 memset(where_pararaph, 0x00, where_pararaph_length);
14757 if (account_id > ALL_ACCOUNT) {
14760 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_id = %d ", mailbox_id);
14762 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d ", account_id);
14765 if (new_slot_size == 0)
14766 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"));
14768 if (strlen(sql_query_string) + EM_SAFE_STRLEN(where_pararaph) < QUERY_SIZE) /* prevent 34363 */
14769 EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14771 EM_DEBUG_EXCEPTION("Query buffer overflowed !!!");
14772 err = EMAIL_ERROR_OUT_OF_MEMORY;
14776 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14777 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14778 if (err != EMAIL_ERROR_NONE) {
14779 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
14786 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
14788 EM_SAFE_FREE(where_pararaph);
14790 if (err_code != NULL)
14793 EM_DEBUG_FUNC_END("ret [%d]", ret);
14797 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)
14799 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);
14801 if (!meeting_req || meeting_req->mail_id <= 0) {
14803 EM_DEBUG_EXCEPTION("mail_id[%d]", meeting_req->mail_id);
14805 if (err_code != NULL)
14806 *err_code = EMAIL_ERROR_INVALID_PARAM;
14813 int error = EMAIL_ERROR_NONE;
14814 DB_STMT hStmt = NULL;
14815 char sql_query_string[QUERY_SIZE] = {0, };
14817 time_t temp_unix_time = 0;
14819 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14820 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14822 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14823 "INSERT INTO mail_meeting_tbl VALUES "
14825 ", ?" /* account_id */
14826 ", ?" /* mailbox_id */
14827 ", ?" /* meeting_response */
14828 ", ?" /* start_time */
14829 ", ?" /* end_time */
14830 ", ?" /* location */
14831 ", ?" /* global_object_id */
14833 ", ?" /* standard_name */
14834 ", ?" /* standard_time_start_date */
14835 ", ?" /* standard_biad */
14836 ", ?" /* daylight_name */
14837 ", ?" /* daylight_time_start_date */
14838 ", ?" /* daylight_bias */
14841 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14842 if (rc != SQLITE_OK) {
14843 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
14844 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
14846 error = EMAIL_ERROR_DB_FAILURE;
14852 EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
14853 EM_DEBUG_LOG_SEC(">>>>> account_id[%d]", account_id);
14854 EM_DEBUG_LOG_SEC(">>>>> mailbox_name[%s]", mailbox_name);
14855 EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
14856 EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
14857 EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
14858 EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
14859 EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
14860 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
14861 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
14862 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
14863 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
14864 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
14865 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
14866 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
14868 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->mail_id);
14869 _bind_stmt_field_data_int(hStmt, col_index++, account_id);
14870 _bind_stmt_field_data_int(hStmt, col_index++, input_mailbox_id);
14871 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
14873 temp_unix_time = timegm(&(meeting_req->start_time));
14874 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14875 temp_unix_time = timegm(&(meeting_req->end_time));
14876 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14878 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->location, 0, LOCATION_LEN_IN_MAIL_MEETING_TBL);
14879 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->global_object_id, 0, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
14881 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
14882 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.standard_name, 0, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
14883 temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
14884 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14885 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
14887 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.daylight_name, 0, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
14888 temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
14889 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14890 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
14893 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14894 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
14895 ("sqlite3_step fail:%d", rc));
14896 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14897 ("sqlite3_step fail:%d", rc));
14902 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14903 if (hStmt != NULL) {
14904 rc = sqlite3_finalize(hStmt);
14905 if (rc != SQLITE_OK) {
14906 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14907 error = EMAIL_ERROR_DB_FAILURE;
14911 if (err_code != NULL)
14914 EM_DEBUG_FUNC_END("ret [%d]", ret);
14918 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)
14920 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);
14927 int err = EMAIL_ERROR_NONE;
14928 char **result = NULL;
14929 char sql_query_string[QUERY_SIZE] = {0, };
14930 email_meeting_request_t* p_temp_meeting_req = NULL;
14931 sqlite3 *local_db_handle = NULL;
14932 time_t temp_unix_time;
14934 if (conditional_clause == NULL || output_meeting_req == NULL || output_result_count == NULL) {
14935 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14936 err = EMAIL_ERROR_INVALID_PARAM;
14940 col_index = _field_count_of_table[CREATE_TABLE_MAIL_MEETING_TBL];
14941 EM_DEBUG_LOG("col_index [%d]", col_index);
14943 local_db_handle = emstorage_get_db_connection(multi_user_name);
14945 EMSTORAGE_START_READ_TRANSACTION(transaction);
14947 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_meeting_tbl %s", conditional_clause);
14949 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14951 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14952 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
14953 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14956 EM_DEBUG_EXCEPTION("No meeting_request found...");
14957 err = EMAIL_ERROR_DATA_NOT_FOUND;
14961 EM_DEBUG_LOG("There are [%d] meeting requests.", count);
14962 if (!(p_temp_meeting_req = (email_meeting_request_t*)em_malloc(sizeof(email_meeting_request_t) * count))) {
14963 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
14964 err = EMAIL_ERROR_OUT_OF_MEMORY;
14968 EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
14970 for (i = 0; i < count; i++) {
14971 _get_table_field_data_int(result, &(p_temp_meeting_req[i].mail_id), col_index++);
14972 _get_table_field_data_int(result, &dummy, col_index++); /* account_id. but why should this field exist in DB table? */
14973 _get_table_field_data_int(result, &dummy, col_index++); /* mailbox_id */
14974 _get_table_field_data_int(result, (int*)&(p_temp_meeting_req[i].meeting_response), col_index++);
14975 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* start time */
14976 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].start_time));
14977 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* end time */
14978 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].end_time));
14979 _get_table_field_data_string(result, &p_temp_meeting_req[i].location, 1, col_index++);
14980 _get_table_field_data_string(result, &p_temp_meeting_req[i].global_object_id, 1, col_index++);
14981 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.offset_from_GMT), col_index++);
14982 _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++);
14983 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14984 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.standard_time_start_date));
14985 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.standard_bias), col_index++);
14986 _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++);
14987 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14988 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.daylight_time_start_date));
14989 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.daylight_bias), col_index++);
14995 sqlite3_free_table(result);
14997 if (err == EMAIL_ERROR_NONE) {
14998 if (p_temp_meeting_req)
14999 *output_meeting_req = p_temp_meeting_req;
15000 *output_result_count = count;
15002 EM_SAFE_FREE(p_temp_meeting_req);
15004 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15006 // sqlite3_db_release_memory(local_db_handle);
15008 EM_DEBUG_FUNC_END("err [%d]", err);
15012 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)
15014 EM_DEBUG_FUNC_BEGIN();
15018 int error = EMAIL_ERROR_NONE;
15019 char conditional_clause[QUERY_SIZE] = {0, };
15021 EM_IF_NULL_RETURN_VALUE(meeting_req, false);
15024 SNPRINTF(conditional_clause, QUERY_SIZE, " WHERE mail_id = %d", mail_id);
15025 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
15027 if ((error = emstorage_query_meeting_request(multi_user_name, conditional_clause, meeting_req, &count, transaction)) != EMAIL_ERROR_NONE) {
15028 EM_DEBUG_EXCEPTION("emstorage_query_meeting_request failed. [%d]", error);
15032 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->mail_id[%d]", (*meeting_req)->mail_id);
15033 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->meeting_response[%d]", (*meeting_req)->meeting_response);
15034 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->start_time[%s]", asctime(&((*meeting_req)->start_time)));
15035 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->end_time[%s]", asctime(&((*meeting_req)->end_time)));
15036 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->location[%s]", (*meeting_req)->location);
15037 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->global_object_id[%s]", (*meeting_req)->global_object_id);
15038 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.offset_from_GMT[%d]", (*meeting_req)->time_zone.offset_from_GMT);
15039 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_name[%s]", (*meeting_req)->time_zone.standard_name);
15040 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.standard_time_start_date)));
15041 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_bias[%d]", (*meeting_req)->time_zone.standard_bias);
15042 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_name[%s]", (*meeting_req)->time_zone.daylight_name);
15043 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.daylight_time_start_date)));
15044 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_bias[%d]", (*meeting_req)->time_zone.daylight_bias);
15049 if (err_code != NULL)
15052 EM_DEBUG_FUNC_END("ret [%d]", ret);
15056 INTERNAL_FUNC int emstorage_update_meeting_request(char *multi_user_name, email_meeting_request_t* meeting_req, int transaction, int *err_code)
15058 EM_DEBUG_FUNC_BEGIN("meeting_req[%p], transaction[%d], err_code[%p]", meeting_req, transaction, err_code);
15061 int error = EMAIL_ERROR_NONE;
15063 DB_STMT hStmt = NULL;
15064 char sql_query_string[QUERY_SIZE] = {0, };
15065 time_t temp_unix_time = 0;
15067 if (!meeting_req) {
15068 EM_DEBUG_EXCEPTION("Invalid Parameter!");
15069 if (err_code != NULL)
15070 *err_code = EMAIL_ERROR_INVALID_PARAM;
15074 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15075 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15077 memset(sql_query_string, 0x00, sizeof(sql_query_string));
15078 SNPRINTF(sql_query_string, sizeof(sql_query_string),
15079 "UPDATE mail_meeting_tbl "
15081 /* " account_id = ?, " // not update here, this can be changed when move or copy */
15082 /* " mailbox_name = ?, " // not update here, this can be changed when move or copy */
15083 " meeting_response = ?, "
15084 " start_time = ?, "
15087 " global_object_id = ?, "
15089 " standard_name = ?, "
15090 " standard_time_start_date = ?, "
15091 " standard_bias = ?, "
15092 " daylight_name = ?, "
15093 " daylight_time_start_date = ?, "
15094 " daylight_bias = ? "
15095 "WHERE mail_id = %d",
15096 meeting_req->mail_id);
15098 EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
15100 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15101 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15102 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15104 EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
15105 EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
15106 EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
15107 EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
15108 EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
15109 EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
15110 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
15111 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
15112 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
15113 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
15114 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
15115 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
15116 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
15120 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
15121 temp_unix_time = timegm(&(meeting_req->start_time));
15122 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
15123 temp_unix_time = timegm(&(meeting_req->end_time));
15124 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
15125 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->location, 1, LOCATION_LEN_IN_MAIL_MEETING_TBL);
15126 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->global_object_id, 1, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
15127 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
15128 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.standard_name, 1, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
15129 temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
15130 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
15131 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
15132 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.daylight_name, 1, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
15133 temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
15134 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
15135 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
15138 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15139 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15140 ("sqlite3_step fail:%d", rc));
15145 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15147 if (hStmt != NULL) {
15148 rc = sqlite3_finalize(hStmt);
15149 if (rc != SQLITE_OK) {
15150 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15151 error = EMAIL_ERROR_DB_FAILURE;
15155 if (err_code != NULL)
15158 EM_DEBUG_FUNC_END("ret [%d]", ret);
15162 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)
15164 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);
15166 if (account_id < ALL_ACCOUNT || mail_id < 0) {
15167 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d]", account_id, mail_id);
15169 if (err_code != NULL)
15170 *err_code = EMAIL_ERROR_INVALID_PARAM;
15175 int error = EMAIL_ERROR_NONE;
15177 char sql_query_string[QUERY_SIZE] = {0, };
15179 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15180 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15182 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_meeting_tbl ");
15184 if (account_id != ALL_ACCOUNT) { /* NOT '0' means a specific account. '0' means all account */
15185 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);
15189 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);
15192 if (input_mailbox_id > 0) { /* 0 means all mailbox_id */
15193 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);
15196 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15197 if (error != EMAIL_ERROR_NONE) {
15198 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
15205 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15210 EM_DEBUG_FUNC_END("ret [%d]", ret);
15215 INTERNAL_FUNC void emstorage_free_meeting_request(email_meeting_request_t *meeting_req)
15217 EM_DEBUG_FUNC_BEGIN();
15219 if (!meeting_req) return;
15221 EM_SAFE_FREE(meeting_req->location);
15222 EM_SAFE_FREE(meeting_req->global_object_id);
15224 EM_DEBUG_FUNC_END();
15227 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)
15229 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);
15230 EM_PROFILE_BEGIN(profile_emstorage_get_overflowed_mail_id_list);
15231 char sql_query_string[QUERY_SIZE] = {0, };
15232 char **result = NULL;
15233 int rc = -1, ret = false;
15234 int error = EMAIL_ERROR_NONE;
15235 int counter = 0, col_index = 0;
15236 int result_mail_id_count = 0;
15237 int *result_mail_id_list = NULL;
15239 if (input_mailbox_id <= 0 || !mail_id_list || !mail_id_count || account_id < 1) {
15240 EM_DEBUG_EXCEPTION("Invalid Parameter");
15242 *err_code = EMAIL_ERROR_INVALID_PARAM;
15246 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);
15248 EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
15250 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15251 EMSTORAGE_START_READ_TRANSACTION(transaction);
15253 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_mail_id_count, 0, NULL), rc);
15254 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15255 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15257 if (!result_mail_id_count) {
15258 EM_DEBUG_LOG("No mail found...");
15260 error = EMAIL_ERROR_MAIL_NOT_FOUND;
15264 EM_DEBUG_LOG("There are [%d] overflowed mails in mailbox_id [%d]", result_mail_id_count, input_mailbox_id);
15266 if (!(result_mail_id_list = (int *)malloc(sizeof(int) * result_mail_id_count))) {
15267 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
15268 error = EMAIL_ERROR_OUT_OF_MEMORY;
15269 sqlite3_free_table(result);
15273 memset(result_mail_id_list, 0x00, sizeof(int) * result_mail_id_count);
15277 for (counter = 0; counter < result_mail_id_count; counter++)
15278 _get_table_field_data_int(result, result_mail_id_list + counter, col_index++);
15283 EM_DEBUG_LOG("finish off [%d]", ret);
15286 sqlite3_free_table(result);
15289 *mail_id_list = result_mail_id_list;
15290 *mail_id_count = result_mail_id_count;
15292 EM_SAFE_FREE(result_mail_id_list);
15294 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15296 if (err_code != NULL)
15299 EM_PROFILE_END(profile_emstorage_get_overflowed_mail_id_list);
15300 EM_DEBUG_FUNC_END("ret [%d]", ret);
15304 INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(char *multi_user_name, int mail_id, int *thread_id, int *err_code)
15306 EM_DEBUG_FUNC_BEGIN("mail_id[%d], thread_id[%p], err_code[%p]", mail_id, thread_id, err_code);
15308 int rc = -1, ret = false;
15309 int err = EMAIL_ERROR_NONE;
15310 char sql_query_string[QUERY_SIZE] = {0, };
15312 int result_count = 0;
15314 if (mail_id == 0 || thread_id == NULL) {
15315 EM_DEBUG_EXCEPTION("Invalid Parameter");
15317 *err_code = EMAIL_ERROR_INVALID_PARAM;
15321 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15323 memset(sql_query_string, 0, QUERY_SIZE);
15324 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT thread_id FROM mail_tbl WHERE mail_id = %d", mail_id);
15326 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
15327 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
15328 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15329 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15331 if (!result_count) {
15332 EM_DEBUG_LOG("No mail found...");
15334 err = EMAIL_ERROR_MAIL_NOT_FOUND;
15335 /* sqlite3_free_table(result); */
15339 _get_table_field_data_int(result, thread_id, 1);
15341 sqlite3_free_table(result);
15347 if (err_code != NULL)
15350 EM_DEBUG_FUNC_END("ret [%d]", ret);
15354 INTERNAL_FUNC int emstorage_update_latest_thread_mail(char *multi_user_name,
15359 int *updated_thread_id,
15360 int latest_mail_id,
15361 int thread_item_count,
15366 EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id [%d], thread_id[%d], updated_thread_id[%p], "
15367 "latest_mail_id [%d], thread_item_count[%d], err_code[%p]",
15368 account_id, mailbox_id, thread_id, updated_thread_id,
15369 latest_mail_id, thread_item_count, err_code);
15371 int rc = -1, ret = false;
15372 int err = EMAIL_ERROR_NONE;
15373 char sql_query_string[QUERY_SIZE] = {0, };
15374 char **result = NULL;
15375 int result_count = 0;
15377 if (thread_id == 0) {
15378 EM_DEBUG_EXCEPTION("Invalid Parameter");
15380 *err_code = EMAIL_ERROR_INVALID_PARAM;
15384 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15386 if (thread_item_count == 0 && latest_mail_id == 0) {
15387 memset(sql_query_string, 0, QUERY_SIZE);
15388 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);
15390 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
15391 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
15392 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15393 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15394 EM_DEBUG_LOG("result_count[%d]", result_count);
15395 if (result_count == 0) {
15396 EM_DEBUG_LOG("No mail found...");
15399 *err_code = EMAIL_ERROR_MAIL_NOT_FOUND;
15400 sqlite3_free_table(result);
15404 _get_table_field_data_int(result, &latest_mail_id, 2);
15405 _get_table_field_data_int(result, &thread_item_count, 3);
15407 EM_DEBUG_LOG("latest_mail_id[%d]", latest_mail_id);
15408 EM_DEBUG_LOG("thread_item_count[%d]", thread_item_count);
15410 sqlite3_free_table(result);
15413 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15415 if (thread_item_count < 0) {
15416 memset(sql_query_string, 0, QUERY_SIZE);
15417 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);
15418 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15419 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15420 if (err != EMAIL_ERROR_NONE) {
15421 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15424 } else if (thread_id != latest_mail_id) {
15425 /* Initialize the thread id */
15426 memset(sql_query_string, 0, QUERY_SIZE);
15427 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);
15428 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15429 if (err != EMAIL_ERROR_NONE) {
15430 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15434 /* update the thread item count */
15435 memset(sql_query_string, 0, QUERY_SIZE);
15436 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);
15437 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15438 if (err != EMAIL_ERROR_NONE) {
15439 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15443 memset(sql_query_string, 0, QUERY_SIZE);
15444 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);
15445 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15446 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15447 if (err != EMAIL_ERROR_NONE) {
15448 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15455 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15457 if (thread_id != latest_mail_id) {
15458 if (err == EMAIL_ERROR_NONE) {
15459 EM_DEBUG_LOG("noti_type[%d]", noti_type);
15461 if (latest_mail_id > 0 && thread_id > 0 && noti_type > 0) {
15462 char mailbox_id_str[25] = {0,};
15463 snprintf(mailbox_id_str, sizeof(mailbox_id_str), "%d", mailbox_id);
15464 if (!emcore_notify_storage_event(noti_type, thread_id, latest_mail_id, mailbox_id_str, account_id))
15465 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventfailed [NOTI_THREAD_ID_CHANGED] >>>> ");
15467 if (updated_thread_id) *updated_thread_id = latest_mail_id;
15470 } else if (thread_item_count >= 0) {
15471 if (err == EMAIL_ERROR_NONE) {
15472 char parameter_string[500] = {0,};
15473 SNPRINTF(parameter_string, sizeof(parameter_string), "%s%c%d", "thread_item_count", 0x01, latest_mail_id);
15474 if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT, parameter_string, thread_item_count))
15475 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventfailed [NOTI_MAIL_FIELD_UPDATE] >>>> ");
15479 if (err_code != NULL)
15482 EM_DEBUG_FUNC_END("ret [%d]", ret);
15486 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)
15488 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);
15491 int err = EMAIL_ERROR_NONE;
15492 char sql_query_string[QUERY_SIZE] = {0, };
15494 if (thread_id == 0) {
15495 EM_DEBUG_EXCEPTION("Invalid Parameter");
15497 *err_code = EMAIL_ERROR_INVALID_PARAM;
15501 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15503 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15505 memset(sql_query_string, 0, QUERY_SIZE);
15506 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);
15507 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15508 if (err != EMAIL_ERROR_NONE) {
15509 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15516 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15518 if (err_code != NULL)
15520 /* LCOV_EXCL_STOP */
15521 EM_DEBUG_FUNC_END("ret [%d]", ret);
15525 #ifdef __FEATURE_LOCAL_ACTIVITY__
15527 * emstorage_add_activity - Add Email Local activity during OFFLINE mode
15530 INTERNAL_FUNC int emstorage_add_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15532 EM_DEBUG_FUNC_BEGIN();
15534 EM_DEBUG_LOG(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15536 int rc = -1, ret = false;
15537 int error = EMAIL_ERROR_NONE;
15538 DB_STMT hStmt = NULL;
15539 char sql_query_string[8192] = { 0x00, };
15542 if (!local_activity) {
15543 EM_DEBUG_EXCEPTION(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15544 if (err_code != NULL)
15545 *err_code = EMAIL_ERROR_INVALID_PARAM;
15549 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15551 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15553 memset(sql_query_string, 0x00 , sizeof(sql_query_string));
15554 SNPRINTF(sql_query_string, sizeof(sql_query_string), "INSERT INTO mail_local_activity_tbl VALUES (?, ?, ?, ?, ?, ?, ?)");
15556 EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ] ", local_activity->activity_id);
15557 EM_DEBUG_LOG(">>>>> MAIL ID [ %d ] ", local_activity->mail_id);
15558 EM_DEBUG_LOG(">>>>> ACCOUNT ID [ %d ] ", local_activity->account_id);
15559 EM_DEBUG_LOG(">>>>> ACTIVITY TYPE [ %d ] ", local_activity->activity_type);
15560 EM_DEBUG_LOG_SEC(">>>>> SERVER MAIL ID [ %s ] ", local_activity->server_mailid);
15561 EM_DEBUG_LOG(">>>>> SOURCE MAILBOX [ %s ] ", local_activity->src_mbox);
15562 EM_DEBUG_LOG(">>>>> DEST MAILBOX [ %s ] ", local_activity->dest_mbox);
15564 EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15567 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15568 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15569 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15571 EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15573 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
15574 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
15575 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
15576 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
15577 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->server_mailid, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
15578 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->src_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15579 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->dest_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15582 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15584 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
15585 ("sqlite3_step fail:%d", rc));
15586 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15587 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
15592 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15594 if (hStmt != NULL) {
15595 rc = sqlite3_finalize(hStmt);
15596 if (rc != SQLITE_OK) {
15597 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15598 error = EMAIL_ERROR_DB_FAILURE;
15602 if (err_code != NULL)
15605 EM_DEBUG_FUNC_END("ret [%d]", ret);
15610 * emstorage_get_activity - Get the Local activity Information
15614 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)
15616 EM_DEBUG_FUNC_BEGIN();
15618 int i = 0, count = 0, rc = -1, ret = false;
15619 int error = EMAIL_ERROR_NONE;
15620 emstorage_activity_tbl_t *p_activity_tbl = NULL;
15621 char sql_query_string[1024] = {0x00, };
15622 char **result = NULL;
15625 EM_IF_NULL_RETURN_VALUE(activity_list, false);
15626 EM_IF_NULL_RETURN_VALUE(select_num, false);
15629 if (!select_num || !activity_list || account_id <= 0 || activityid < 0) {
15630 EM_DEBUG_LOG(" select_num[%p], activity_list[%p] account_id [%d] activityid [%d] ", select_num, activity_list, account_id, activityid);
15632 *err_code = EMAIL_ERROR_INVALID_PARAM;
15636 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15638 EMSTORAGE_START_READ_TRANSACTION(transaction);
15640 memset(sql_query_string, 0x00, sizeof(sql_query_string));
15642 if (activityid == ALL_ACTIVITIES) {
15643 SNPRINTF(sql_query_string, sizeof(sql_query_string),
15644 "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d order by activity_id", account_id);
15646 SNPRINTF(sql_query_string, sizeof(sql_query_string),
15647 "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d AND activity_id = %d ", account_id, activityid);
15650 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
15654 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15655 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15656 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15660 if (!(p_activity_tbl = (emstorage_activity_tbl_t*)em_malloc(sizeof(emstorage_activity_tbl_t) * count))) {
15661 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
15662 error = EMAIL_ERROR_OUT_OF_MEMORY;
15667 for (i = 0; i < count; i++) {
15668 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15669 if (result[col_index])
15670 p_activity_tbl[i].activity_id = atoi(result[col_index++]);
15672 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15673 if (result[col_index])
15674 p_activity_tbl[i].account_id = atoi(result[col_index++]);
15676 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15677 if (result[col_index])
15678 p_activity_tbl[i].mail_id = atoi(result[col_index++]);
15680 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15681 if (result[col_index])
15682 p_activity_tbl[i].activity_type = atoi(result[col_index++]);
15685 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15686 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15687 p_activity_tbl[i].server_mailid = EM_SAFE_STRDUP(result[col_index++]);
15691 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15692 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15693 p_activity_tbl[i].src_mbox = EM_SAFE_STRDUP(result[col_index++]);
15697 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15698 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15699 p_activity_tbl[i].dest_mbox = EM_SAFE_STRDUP(result[col_index++]);
15706 sqlite3_free_table(result);
15712 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15715 *activity_list = p_activity_tbl;
15716 *select_num = count;
15717 EM_DEBUG_LOG(">>>> COUNT : %d >> ", count);
15718 } else if (p_activity_tbl != NULL) {
15719 emstorage_free_local_activity(&p_activity_tbl, count, NULL);
15723 if (err_code != NULL)
15726 EM_DEBUG_FUNC_END("ret [%d]", ret);
15731 INTERNAL_FUNC int emstorage_get_next_activity_id(int *activity_id, int *err_code)
15734 EM_DEBUG_FUNC_BEGIN();
15737 int err = EMAIL_ERROR_NONE;
15740 char **result = NULL;
15742 if (NULL == activity_id) {
15743 EM_DEBUG_EXCEPTION(" activity_id[%p]", activity_id);
15745 *err_code = EMAIL_ERROR_INVALID_PARAM;
15749 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15751 /* increase unique id */
15753 sql = "SELECT max(rowid) FROM mail_local_activity_tbl;";
15755 /* 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); */
15756 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15757 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
15759 if (NULL == result[1])
15762 rc = atoi(result[1])+1;
15767 sqlite3_free_table(result);
15776 EM_DEBUG_FUNC_END("ret [%d]", ret);
15781 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)
15784 EM_DEBUG_FUNC_BEGIN();
15786 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);
15788 if (account_id <= 0 || NULL == activity_id_list || NULL == activity_id_count || lowest_activity_type <= 0 || highest_activity_type <= 0) {
15789 if (err_code != NULL)
15790 *err_code = EMAIL_ERROR_INVALID_PARAM;
15795 int error = EMAIL_ERROR_NONE;
15796 int i = 0, rc = -1, count = 0;
15797 char sql_query_string[1024] = {0x00, };
15798 int *activity_ids = NULL;
15800 char **result = NULL;
15801 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15802 EMSTORAGE_START_READ_TRANSACTION(transaction);
15804 memset(sql_query_string, 0x00, sizeof(sql_query_string));
15806 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);
15808 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
15811 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15812 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15813 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15817 EM_DEBUG_LOG(" Activity COUNT : %d ... ", count);
15819 if (NULL == (activity_ids = (int *)em_malloc(sizeof(int) * count))) {
15820 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
15821 error = EMAIL_ERROR_OUT_OF_MEMORY;
15825 for (i = 0; i < count; i++) {
15826 activity_ids[i] = atoi(result[col_index]);
15828 EM_DEBUG_LOG("activity_id %d", activity_ids[i]);
15837 *activity_id_count = count;
15838 *activity_id_list = activity_ids;
15839 } else if (activity_ids != NULL) /* Prevent defect - 216566 */
15840 EM_SAFE_FREE(activity_ids);
15843 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15844 if (err_code != NULL)
15847 EM_DEBUG_FUNC_END("ret [%d]", ret);
15851 INTERNAL_FUNC int emstorage_free_activity_id_list(int *activity_id_list, int *error_code)
15853 EM_DEBUG_FUNC_BEGIN();
15855 int error = EMAIL_ERROR_NONE;
15858 EM_DEBUG_LOG(" activity_id_list [%p]", activity_id_list);
15860 if (NULL == activity_id_list) {
15861 error = EMAIL_ERROR_INVALID_PARAM;
15864 EM_SAFE_FREE(activity_id_list);
15872 if (NULL != error_code)
15873 *error_code = error;
15875 EM_DEBUG_FUNC_END("ret [%d]", ret);
15880 * emstorage_delete_local_activity - Deletes the Local acitivity Generated based on activity_type
15881 * or based on server mail id
15884 INTERNAL_FUNC int emstorage_delete_local_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15886 EM_DEBUG_FUNC_BEGIN();
15889 EM_DEBUG_LOG(" local_activity[%p] ", local_activity);
15891 if (!local_activity) {
15892 EM_DEBUG_EXCEPTION(" local_activity[%p] ", local_activity);
15893 if (err_code != NULL)
15894 *err_code = EMAIL_ERROR_INVALID_PARAM;
15898 int rc = -1, ret = false; /* Prevent_FIX */
15899 int err = EMAIL_ERROR_NONE;
15901 int query_where = 0;
15902 char sql_query_string[8192] = { 0x00, };
15903 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15904 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15906 memset(sql_query_string, 0x00, sizeof(sql_query_string));
15908 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_local_activity_tbl ");
15910 EM_DEBUG_LOG_SEC(">>> Query [ %s ] ", sql_query_string);
15912 if (local_activity->account_id) {
15913 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15914 " WHERE account_id = %d ", local_activity->account_id);
15919 if (local_activity->server_mailid) {
15920 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15921 " %s %s server_mailid = '%s' ", query_where ? "" : "WHERE", query_and ? "AND" : "", local_activity->server_mailid);
15927 if (local_activity->mail_id) {
15928 EM_DEBUG_LOG(">>>> MAIL ID [ %d ] , ACTIVITY TYPE [%d ]", local_activity->mail_id, local_activity->activity_type);
15930 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15931 " %s %s mail_id = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "", local_activity->mail_id);
15938 if (local_activity->activity_type > 0) {
15939 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15940 " %s %s activity_type = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "" , local_activity->activity_type);
15943 if (local_activity->activity_id > 0) {
15944 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15945 " %s %s activity_id = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "" , local_activity->activity_id);
15949 EM_DEBUG_LOG_SEC(">>>>> Query [ %s ] ", sql_query_string);
15950 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15951 if (error != EMAIL_ERROR_NONE) {
15952 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
15956 rc = sqlite3_changes(local_db_handle);
15958 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
15959 err = EMAIL_ERROR_MAILBOX_NOT_FOUND;
15965 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15967 if (hStmt != NULL) {
15968 rc = sqlite3_finalize(hStmt);
15969 if (rc != SQLITE_OK) {
15970 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15971 error = EMAIL_ERROR_DB_FAILURE;
15975 if (err_code != NULL)
15977 EM_DEBUG_FUNC_END("ret [%d]", ret);
15982 * emstorage_free_local_activity - Free the Local Activity data
15984 INTERNAL_FUNC int emstorage_free_local_activity(emstorage_activity_tbl_t **local_activity_list, int count, int *err_code)
15986 EM_DEBUG_FUNC_BEGIN();
15988 EM_DEBUG_LOG(" local_activity_list[%p], count[%d], err_code[%p]", local_activity_list, count, err_code);
15991 int error = EMAIL_ERROR_INVALID_PARAM;
15994 if (!local_activity_list || !*local_activity_list) {
15995 EM_DEBUG_EXCEPTION(" local_activity_list[%p], count[%d]", local_activity_list, count);
15997 error = EMAIL_ERROR_INVALID_PARAM;
16001 emstorage_activity_tbl_t* p = *local_activity_list;
16004 for (; i < count; i++) {
16005 EM_SAFE_FREE(p[i].dest_mbox);
16006 EM_SAFE_FREE(p[i].src_mbox);
16007 EM_SAFE_FREE(p[i].server_mailid);
16011 *local_activity_list = NULL;
16019 if (err_code != NULL)
16022 EM_DEBUG_FUNC_END("ret [%d]", ret);
16026 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
16028 /* LCOV_EXCL_START */
16029 static int _get_key_value_string_for_list_filter_rule(email_list_filter_rule_t *input_list_filter_rule, char **output_key_value_string)
16031 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_key_value_string [%p]", input_list_filter_rule, output_key_value_string);
16033 int ret = EMAIL_ERROR_NONE;
16034 char key_value_string[QUERY_SIZE] = { 0, };
16035 char *temp_key_value_1 = NULL;
16036 char *temp_key_value_2 = NULL;
16038 if (input_list_filter_rule == NULL || output_key_value_string == NULL) {
16039 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16040 return EMAIL_ERROR_INVALID_PARAM;
16043 switch (input_list_filter_rule->target_attribute) {
16044 case EMAIL_MAIL_ATTRIBUTE_MAIL_ID:
16045 case EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID:
16046 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID:
16047 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE:
16048 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_STATUS:
16049 case EMAIL_MAIL_ATTRIBUTE_REFERENCE_MAIL_ID:
16050 case EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS:
16051 case EMAIL_MAIL_ATTRIBUTE_MAIL_SIZE:
16052 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN:
16053 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML:
16054 case EMAIL_MAIL_ATTRIBUTE_FILE_SIZE:
16055 case EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD:
16056 case EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD:
16057 case EMAIL_MAIL_ATTRIBUTE_FLAGS_FLAGGED_FIELD:
16058 case EMAIL_MAIL_ATTRIBUTE_FLAGS_ANSWERED_FIELD:
16059 case EMAIL_MAIL_ATTRIBUTE_FLAGS_RECENT_FIELD:
16060 case EMAIL_MAIL_ATTRIBUTE_FLAGS_DRAFT_FIELD:
16061 case EMAIL_MAIL_ATTRIBUTE_FLAGS_FORWARDED_FIELD:
16062 case EMAIL_MAIL_ATTRIBUTE_DRM_STATUS:
16063 case EMAIL_MAIL_ATTRIBUTE_PRIORITY:
16064 case EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS:
16065 case EMAIL_MAIL_ATTRIBUTE_LOCK_STATUS:
16066 case EMAIL_MAIL_ATTRIBUTE_REPORT_STATUS:
16067 case EMAIL_MAIL_ATTRIBUTE_ATTACHMENT_COUNT:
16068 case EMAIL_MAIL_ATTRIBUTE_INLINE_CONTENT_COUNT:
16069 case EMAIL_MAIL_ATTRIBUTE_THREAD_ID:
16070 case EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT:
16071 case EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS:
16072 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_CLASS:
16073 case EMAIL_MAIL_ATTRIBUTE_DIGEST_TYPE:
16074 case EMAIL_MAIL_ATTRIBUTE_SMIME_TYPE:
16075 case EMAIL_MAIL_ATTRIBUTE_REMAINING_RESEND_TIMES:
16076 case EMAIL_MAIL_ATTRIBUTE_TAG_ID:
16077 case EMAIL_MAIL_ATTRIBUTE_EAS_DATA_LENGTH_TYPE:
16078 SNPRINTF(key_value_string, QUERY_SIZE, "%d", input_list_filter_rule->key_value.integer_type_value);
16081 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
16082 case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
16083 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
16084 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
16085 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
16086 case EMAIL_MAIL_ATTRIBUTE_FROM:
16087 case EMAIL_MAIL_ATTRIBUTE_TO:
16088 case EMAIL_MAIL_ATTRIBUTE_CC:
16089 case EMAIL_MAIL_ATTRIBUTE_BCC:
16090 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
16091 if (input_list_filter_rule->key_value.string_type_value == NULL) {
16092 EM_DEBUG_EXCEPTION("Invalid string_type_value [%p]", input_list_filter_rule->key_value.string_type_value);
16093 ret = EMAIL_ERROR_INVALID_PARAM;
16097 temp_key_value_1 = input_list_filter_rule->key_value.string_type_value;
16099 temp_key_value_2 = em_replace_all_string(temp_key_value_1, "_", "\\_");
16100 temp_key_value_1 = em_replace_all_string(temp_key_value_2, "%", "\\%");
16102 if (input_list_filter_rule->rule_type == EMAIL_LIST_FILTER_RULE_INCLUDE)
16103 SNPRINTF(key_value_string, QUERY_SIZE, "\'%%%s%%\'", temp_key_value_1);
16105 SNPRINTF(key_value_string, QUERY_SIZE, "\'%s\'", temp_key_value_1);
16108 case EMAIL_MAIL_ATTRIBUTE_DATE_TIME:
16109 case EMAIL_MAIL_ATTRIBUTE_SCHEDULED_SENDING_TIME:
16110 case EMAIL_MAIL_ATTRIBUTE_REPLIED_TIME:
16111 case EMAIL_MAIL_ATTRIBUTE_FORWARDED_TIME:
16112 SNPRINTF(key_value_string, QUERY_SIZE, "%d", (int)input_list_filter_rule->key_value.datetime_type_value);
16116 ret = EMAIL_ERROR_INVALID_PARAM;
16117 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
16121 if (ret == EMAIL_ERROR_NONE && EM_SAFE_STRLEN(key_value_string) > 0)
16122 *output_key_value_string = strdup(key_value_string);
16126 EM_SAFE_FREE(temp_key_value_1);
16127 EM_SAFE_FREE(temp_key_value_2);
16129 EM_DEBUG_FUNC_END("ret [%d]", ret);
16133 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
16134 static int _get_cert_password_file_name(int index, char *cert_password_file_name)
16136 EM_DEBUG_FUNC_BEGIN("index : [%d]", index);
16138 if (index <= 0 || !cert_password_file_name) {
16139 EM_DEBUG_EXCEPTION("Invalid parameter");
16140 return EMAIL_ERROR_INVALID_PARAM;
16143 sprintf(cert_password_file_name, ".email_cert_%d", index);
16145 EM_DEBUG_FUNC_END();
16146 return EMAIL_ERROR_NONE;
16150 static int _make_filter_rule_string(email_list_filter_rule_t *input_list_filter_rule, char **output_string)
16152 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16154 int ret = EMAIL_ERROR_NONE;
16156 int length_field_name = 0;
16157 int length_value = 0;
16158 char result_rule_string[QUERY_SIZE] = { 0 , };
16159 char *mod_field_name_string = NULL;
16160 char *mod_value_string = NULL;
16161 char *temp_field_name_string = NULL;
16162 char *temp_key_value_string = NULL;
16164 if (input_list_filter_rule == NULL || output_string == NULL) {
16165 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16166 return EMAIL_ERROR_INVALID_PARAM;
16169 temp_field_name_string = emcore_get_mail_field_name_by_attribute_type(input_list_filter_rule->target_attribute);
16171 if (temp_field_name_string == NULL) {
16172 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
16173 return EMAIL_ERROR_INVALID_PARAM;
16176 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) {
16177 EM_DEBUG_EXCEPTION("_get_key_value_string_for_list_filter_rule failed");
16178 return EMAIL_ERROR_INVALID_PARAM;
16181 length_field_name = EM_SAFE_STRLEN(temp_field_name_string);
16182 length_value = EM_SAFE_STRLEN(temp_key_value_string);
16184 switch (input_list_filter_rule->target_attribute) {
16185 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
16186 case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
16187 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
16188 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
16189 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
16190 case EMAIL_MAIL_ATTRIBUTE_FROM:
16191 case EMAIL_MAIL_ATTRIBUTE_TO:
16192 case EMAIL_MAIL_ATTRIBUTE_CC:
16193 case EMAIL_MAIL_ATTRIBUTE_BCC:
16194 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
16202 if (is_alpha == 1 && input_list_filter_rule->case_sensitivity == false) {
16203 length_field_name += strlen("UPPER() ");
16204 length_value += strlen("UPPER() ");
16205 mod_field_name_string = em_malloc(sizeof(char) * length_field_name);
16206 if (mod_field_name_string == NULL) {
16207 EM_DEBUG_EXCEPTION("em_mallocfailed");
16208 EM_SAFE_FREE(temp_field_name_string);
16209 ret = EMAIL_ERROR_OUT_OF_MEMORY;
16213 mod_value_string = em_malloc(sizeof(char) * length_value);
16214 if (mod_value_string == NULL) {
16215 EM_DEBUG_EXCEPTION("em_mallocfailed");
16216 EM_SAFE_FREE(temp_field_name_string);
16217 ret = EMAIL_ERROR_OUT_OF_MEMORY;
16221 SNPRINTF(mod_field_name_string, length_field_name, "UPPER(%s)", temp_field_name_string);
16222 SNPRINTF(mod_value_string, length_value, "UPPER(%s)", temp_key_value_string);
16223 EM_SAFE_FREE(temp_key_value_string);
16225 mod_field_name_string = strdup(temp_field_name_string);
16226 mod_value_string = temp_key_value_string;
16229 switch (input_list_filter_rule->rule_type) {
16230 case EMAIL_LIST_FILTER_RULE_EQUAL:
16231 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = %s ", mod_field_name_string, mod_value_string);
16233 case EMAIL_LIST_FILTER_RULE_NOT_EQUAL:
16234 SNPRINTF(result_rule_string, QUERY_SIZE, "%s != %s ", mod_field_name_string, mod_value_string);
16236 case EMAIL_LIST_FILTER_RULE_LESS_THAN:
16237 SNPRINTF(result_rule_string, QUERY_SIZE, "%s < %s ", mod_field_name_string, mod_value_string);
16239 case EMAIL_LIST_FILTER_RULE_GREATER_THAN:
16240 SNPRINTF(result_rule_string, QUERY_SIZE, "%s > %s ", mod_field_name_string, mod_value_string);
16242 case EMAIL_LIST_FILTER_RULE_LESS_THAN_OR_EQUAL:
16243 SNPRINTF(result_rule_string, QUERY_SIZE, "%s <= %s ", mod_field_name_string, mod_value_string);
16245 case EMAIL_LIST_FILTER_RULE_GREATER_THAN_OR_EQUAL:
16246 SNPRINTF(result_rule_string, QUERY_SIZE, "%s >= %s ", mod_field_name_string, mod_value_string);
16248 case EMAIL_LIST_FILTER_RULE_INCLUDE:
16249 SNPRINTF(result_rule_string, QUERY_SIZE, "%s LIKE %s ", mod_field_name_string, mod_value_string);
16251 case EMAIL_LIST_FILTER_RULE_IN:
16252 SNPRINTF(result_rule_string, QUERY_SIZE, "%s IN (%s) ", mod_field_name_string, mod_value_string);
16254 case EMAIL_LIST_FILTER_RULE_NOT_IN:
16255 SNPRINTF(result_rule_string, QUERY_SIZE, "%s NOT IN (%s) ", mod_field_name_string, mod_value_string);
16258 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16259 ret = EMAIL_ERROR_INVALID_PARAM;
16263 *output_string = strdup(result_rule_string);
16266 EM_SAFE_FREE(mod_field_name_string);
16267 EM_SAFE_FREE(mod_value_string);
16269 EM_DEBUG_FUNC_END("ret [%d]", ret);
16273 static int _make_filter_attach_rule_string(char *multi_user_name, email_list_filter_rule_attach_t *input_list_filter_rule, char **output_string)
16275 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16277 char *field_name_string = NULL;
16278 char key_value_string[QUERY_SIZE] = {0,};
16279 char result_rule_string[QUERY_SIZE] = {0,};
16282 int query_size = 0;
16285 int error = EMAIL_ERROR_NONE;
16286 char **result = NULL;
16287 char sql_query_string[QUERY_SIZE] = {0,};
16288 char *sql_query_string2 = NULL;
16289 sqlite3 *local_db_handle = NULL;
16290 int *mail_ids = NULL;
16293 if (input_list_filter_rule == NULL || output_string == NULL) {
16294 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16295 return EMAIL_ERROR_INVALID_PARAM;
16298 field_name_string = EM_SAFE_STRDUP("attachment_name");
16299 SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
16301 switch (input_list_filter_rule->rule_type) {
16303 case EMAIL_LIST_FILTER_RULE_INCLUDE:
16304 ret = SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
16307 case EMAIL_LIST_FILTER_RULE_MATCH:
16308 ret = SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
16312 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16313 error = EMAIL_ERROR_INVALID_PARAM;
16318 EM_DEBUG_LOG("WARNING: result rule string is truncated (%s)", result_rule_string);
16321 local_db_handle = emstorage_get_db_connection(multi_user_name);
16323 EMSTORAGE_START_READ_TRANSACTION(true);
16324 ret = SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_attachment_tbl %s", result_rule_string);
16327 EM_DEBUG_LOG("WARNING: sql query string is truncated (%s)", sql_query_string);
16330 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16331 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16332 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16337 EM_DEBUG_LOG("No mail found...");
16338 error = EMAIL_ERROR_MAIL_NOT_FOUND;
16339 *output_string = strdup("mail_id IN () ");
16343 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
16345 if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
16346 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
16347 error = EMAIL_ERROR_OUT_OF_MEMORY;
16351 for (i = 0; i < count; i++) {
16352 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16353 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16356 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >>", count);
16357 sqlite3_free_table(result);
16358 EMSTORAGE_FINISH_READ_TRANSACTION(true);
16360 // sqlite3_db_release_memory(local_db_handle);
16363 query_size = (10 * count) + strlen("mail_id IN () ");
16365 sql_query_string2 = em_malloc(query_size);
16366 if (sql_query_string2 == NULL) {
16367 EM_DEBUG_EXCEPTION("em_mallocfailed");
16368 error = EMAIL_ERROR_OUT_OF_MEMORY;
16372 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN (");
16373 for (i = 0; i < count-1; i++)
16374 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16376 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d) ", mail_ids[count-1]);
16378 *output_string = strdup(sql_query_string2);
16381 EM_SAFE_FREE(mail_ids); /* prevent */
16382 EM_SAFE_FREE(sql_query_string2);
16383 EM_SAFE_FREE(field_name_string);
16384 EM_DEBUG_FUNC_END("error [%d]", error);
16388 static int _make_filter_fts_rule_string(char *multi_user_name, email_list_filter_rule_fts_t *input_list_filter_rule, char **output_string)
16390 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16391 char *field_name_string = NULL;
16392 char key_value_string[QUERY_SIZE] = {0,};
16393 char result_rule_string[QUERY_SIZE] = {0,};
16397 int query_size = 0;
16398 int error = EMAIL_ERROR_NONE;
16399 char **result = NULL;
16400 char sql_query_string[QUERY_SIZE] = {0,};
16401 char *sql_query_string2 = NULL;
16402 sqlite3 *local_db_handle = NULL;
16403 int *mail_ids = NULL;
16406 if (input_list_filter_rule == NULL || output_string == NULL) {
16407 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16408 return EMAIL_ERROR_INVALID_PARAM;
16411 field_name_string = EM_SAFE_STRDUP("body_text");
16412 SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
16414 switch (input_list_filter_rule->rule_type) {
16416 case EMAIL_LIST_FILTER_RULE_INCLUDE:
16417 ret = SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
16420 case EMAIL_LIST_FILTER_RULE_MATCH:
16421 ret = SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
16425 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16426 error = EMAIL_ERROR_INVALID_PARAM;
16431 EM_DEBUG_LOG("WARNING: result rule string is truncated (%s)", result_rule_string);
16434 local_db_handle = emstorage_get_db_connection(multi_user_name);
16436 EMSTORAGE_START_READ_TRANSACTION(true);
16437 ret = SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_text_tbl %s", result_rule_string);
16440 EM_DEBUG_LOG("WARNING: sql query string is truncated (%s)", sql_query_string);
16443 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16444 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16445 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16450 EM_DEBUG_LOG("No mail found...");
16451 error = EMAIL_ERROR_MAIL_NOT_FOUND;
16452 *output_string = strdup("mail_id IN () ");
16456 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
16459 if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
16460 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
16461 error = EMAIL_ERROR_OUT_OF_MEMORY;
16465 for (i = 0; i < count; i++) {
16466 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16467 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16470 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >>", count);
16471 sqlite3_free_table(result);
16472 EMSTORAGE_FINISH_READ_TRANSACTION(true);
16474 // sqlite3_db_release_memory(local_db_handle);
16477 query_size = (10 * count) + strlen("mail_id IN () ");
16478 sql_query_string2 = em_malloc(query_size);
16479 if (sql_query_string2 == NULL) {
16480 EM_DEBUG_EXCEPTION("em_mallocfailed");
16481 error = EMAIL_ERROR_OUT_OF_MEMORY;
16485 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN (");
16486 for (i = 0; i < count-1; i++)
16487 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16489 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d) ", mail_ids[count-1]);
16491 *output_string = strdup(sql_query_string2);
16494 EM_SAFE_FREE(mail_ids); /* prevent */
16495 EM_SAFE_FREE(sql_query_string2);
16496 EM_SAFE_FREE(field_name_string);
16497 EM_DEBUG_FUNC_END("error [%d]", error);
16501 static int _make_order_rule_string(char *multi_user_name, email_list_sorting_rule_t *input_sorting_rule, char **output_string)
16503 EM_DEBUG_FUNC_BEGIN("input_sorting_rule [%p], output_string [%p]", input_sorting_rule, output_string);
16505 char result_rule_string[QUERY_SIZE] = { 0 , };
16506 int ret = EMAIL_ERROR_NONE;
16508 emstorage_account_tbl_t *account_tbl_array = NULL;
16511 char *result_str = NULL;
16512 char *tmp_str1 = NULL;
16513 char *tmp_str2 = NULL;
16514 char query_per_account[QUERY_SIZE] = { 0 , };
16516 if (input_sorting_rule->force_boolean_check) {
16517 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = 0 ",
16518 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16520 EM_SAFE_STRNCPY(result_rule_string,
16521 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16522 QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16525 switch (input_sorting_rule->sort_order) {
16526 case EMAIL_SORT_ORDER_ASCEND:
16527 EM_SAFE_STRNCAT(result_rule_string, " ASC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16530 case EMAIL_SORT_ORDER_DESCEND:
16531 EM_SAFE_STRNCAT(result_rule_string, " DESC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16534 case EMAIL_SORT_ORDER_NOCASE_ASCEND:
16535 EM_SAFE_STRNCAT(result_rule_string, " COLLATE NOCASE ASC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16538 case EMAIL_SORT_ORDER_NOCASE_DESCEND:
16539 EM_SAFE_STRNCAT(result_rule_string, " COLLATE NOCASE DESC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16542 case EMAIL_SORT_ORDER_TO_CCBCC:
16543 memset(result_rule_string, 0, QUERY_SIZE);
16544 if (input_sorting_rule->key_value.string_type_value)
16545 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16546 " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 ELSE 2 END ",
16547 input_sorting_rule->key_value.string_type_value);
16550 case EMAIL_SORT_ORDER_TO_CC_BCC:
16551 memset(result_rule_string, 0, QUERY_SIZE);
16552 if (input_sorting_rule->key_value.string_type_value)
16553 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16554 " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 WHEN full_address_cc LIKE \'%%%q%%\' THEN 2 ELSE 3 END ",
16555 input_sorting_rule->key_value.string_type_value, input_sorting_rule->key_value.string_type_value);
16558 case EMAIL_SORT_ORDER_TO_CCBCC_ALL:
16559 if (!emstorage_get_account_list(multi_user_name, &count, &account_tbl_array, true, false, NULL)) {
16560 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed");
16565 EM_DEBUG_LOG("No account exist");
16566 ret = EMAIL_ERROR_INVALID_PARAM;
16570 for (i = 0; i < count; i++) {
16571 if (i > 0 && result_str) {
16572 tmp_str2 = result_str;
16573 result_str = g_strconcat(tmp_str2, " OR ", NULL);
16574 EM_SAFE_FREE(tmp_str2);
16577 memset(query_per_account, 0, QUERY_SIZE);
16578 snprintf(query_per_account, QUERY_SIZE,
16579 "(account_id = %d AND full_address_to LIKE \'%%%s%%\')",
16580 account_tbl_array[i].account_id, account_tbl_array[i].user_email_address);
16582 tmp_str1 = result_str;
16584 result_str = g_strconcat(tmp_str1, query_per_account, NULL);
16586 result_str = g_strdup(query_per_account);
16587 EM_SAFE_FREE(tmp_str1);
16590 snprintf(result_rule_string, QUERY_SIZE,
16591 " CASE WHEN %s THEN 1 ELSE 2 END ", result_str);
16593 EM_SAFE_FREE(result_str);
16594 if (account_tbl_array)
16595 emstorage_free_account(&account_tbl_array, count, NULL);
16598 case EMAIL_SORT_ORDER_LOCALIZE_ASCEND:
16599 memset(result_rule_string, 0, QUERY_SIZE);
16600 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16601 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END ASC, %s COLLATE NOCASE ASC ",
16602 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16603 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16606 case EMAIL_SORT_ORDER_LOCALIZE_DESCEND:
16607 memset(result_rule_string, 0, QUERY_SIZE);
16608 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16609 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END DESC, %s COLLATE NOCASE DESC ",
16610 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16611 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16615 EM_DEBUG_EXCEPTION("Invalid sort_order [%d]", input_sorting_rule->sort_order);
16616 ret = EMAIL_ERROR_INVALID_PARAM;
16620 *output_string = strdup(result_rule_string);
16623 EM_DEBUG_FUNC_END("ret [%d]", ret);
16627 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)
16629 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);
16630 int ret = EMAIL_ERROR_NONE;
16632 int string_offset = 0;
16633 int query_size = 0;
16634 int new_query_size = 0;
16635 char *conditional_clause_string = NULL;
16636 char *result_string_for_a_item = NULL;
16638 if ((input_filter_count > 0 && !input_filter_list) || (input_sorting_rule_count > 0 && !input_sorting_rule_list) || output_conditional_clause == NULL) {
16639 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16640 return EMAIL_ERROR_INVALID_PARAM;
16643 conditional_clause_string = em_malloc(QUERY_SIZE);
16644 if (conditional_clause_string == NULL) {
16645 EM_DEBUG_EXCEPTION("Memory is full");
16646 return EMAIL_ERROR_OUT_OF_MEMORY;
16649 if (input_filter_count > 0) {
16650 query_size = QUERY_SIZE;
16651 g_strlcpy(conditional_clause_string, " WHERE ", QUERY_SIZE);
16653 for (i = 0; i < input_filter_count; i++) {
16654 switch (input_filter_list[i].list_filter_item_type) {
16655 case EMAIL_LIST_FILTER_ITEM_RULE:
16656 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE", i);
16657 _make_filter_rule_string(&(input_filter_list[i].list_filter_item.rule), &result_string_for_a_item);
16660 case EMAIL_LIST_FILTER_ITEM_RULE_FTS:
16661 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_FTS", i);
16662 _make_filter_fts_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_fts), &result_string_for_a_item);
16665 case EMAIL_LIST_FILTER_ITEM_RULE_ATTACH:
16666 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_ATTACH", i);
16667 _make_filter_attach_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_attach), &result_string_for_a_item);
16670 case EMAIL_LIST_FILTER_ITEM_OPERATOR:
16671 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_OPERATOR", i);
16672 switch (input_filter_list[i].list_filter_item.operator_type) {
16673 case EMAIL_LIST_FILTER_OPERATOR_AND:
16674 result_string_for_a_item = strdup("AND ");
16676 case EMAIL_LIST_FILTER_OPERATOR_OR:
16677 result_string_for_a_item = strdup("OR ");
16679 case EMAIL_LIST_FILTER_OPERATOR_LEFT_PARENTHESIS:
16680 result_string_for_a_item = strdup(" (");
16682 case EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS:
16683 result_string_for_a_item = strdup(") ");
16689 EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", input_filter_list[i].list_filter_item_type);
16690 ret = EMAIL_ERROR_INVALID_PARAM;
16694 if (result_string_for_a_item == NULL) {
16695 EM_DEBUG_EXCEPTION("result_string_for_a_item is null");
16696 ret = EMAIL_ERROR_INVALID_PARAM;
16700 if (strlen(conditional_clause_string) + EM_SAFE_STRLEN(result_string_for_a_item) >= query_size) { /* prevent 34364 */
16701 EM_DEBUG_LOG("QUERY is too long");
16702 new_query_size = EM_SAFE_STRLEN(result_string_for_a_item) + EM_SAFE_STRLEN(conditional_clause_string) + QUERY_SIZE;
16703 conditional_clause_string = realloc(conditional_clause_string, new_query_size);
16704 if (conditional_clause_string == NULL) {
16705 EM_DEBUG_EXCEPTION("realloc failed");
16706 ret = EMAIL_ERROR_OUT_OF_MEMORY;
16710 query_size = new_query_size;
16713 EM_SAFE_STRNCAT(conditional_clause_string, result_string_for_a_item , QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16714 EM_SAFE_FREE(result_string_for_a_item);
16718 if (input_sorting_rule_count > 0) {
16719 EM_SAFE_STRNCAT(conditional_clause_string, "ORDER BY ", QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16721 for (i = 0; i < input_sorting_rule_count; i++) {
16722 if ((ret = _make_order_rule_string(multi_user_name, &input_sorting_rule_list[i], &result_string_for_a_item)) != EMAIL_ERROR_NONE) {
16723 EM_DEBUG_EXCEPTION("_make_order_rule_string failed. [%d]", ret);
16727 EM_SAFE_STRNCAT(conditional_clause_string, ", " , QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16728 EM_SAFE_STRNCAT(conditional_clause_string, result_string_for_a_item , QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16729 EM_SAFE_FREE(result_string_for_a_item);
16733 if (input_start_index != -1 && input_limit_count != -1) {
16734 string_offset = strlen(conditional_clause_string);
16735 SNPRINTF_OFFSET(conditional_clause_string, string_offset, query_size, " LIMIT %d, %d", input_start_index, input_limit_count);
16738 *output_conditional_clause = strdup(conditional_clause_string);
16741 EM_SAFE_FREE(result_string_for_a_item);
16742 EM_SAFE_FREE(conditional_clause_string);
16744 EM_DEBUG_FUNC_END("ret [%d]", ret);
16748 INTERNAL_FUNC int emstorage_free_list_filter(email_list_filter_t **input_filter_list, int input_filter_count)
16750 EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d]", input_filter_list, input_filter_count);
16751 int err = EMAIL_ERROR_NONE;
16753 email_list_filter_t *temp_filter_list = NULL;
16755 EM_IF_NULL_RETURN_VALUE(input_filter_list, EMAIL_ERROR_INVALID_PARAM);
16757 for (i = 0; i < input_filter_count; i++) {
16758 temp_filter_list = (*input_filter_list) + i;
16759 if (!temp_filter_list)
16762 if (temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE) {
16763 switch (temp_filter_list->list_filter_item.rule.target_attribute) {
16764 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
16765 case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
16766 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
16767 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
16768 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
16769 case EMAIL_MAIL_ATTRIBUTE_FROM:
16770 case EMAIL_MAIL_ATTRIBUTE_TO:
16771 case EMAIL_MAIL_ATTRIBUTE_CC:
16772 case EMAIL_MAIL_ATTRIBUTE_BCC:
16773 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN:
16774 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML:
16775 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
16776 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule.key_value.string_type_value);
16781 } 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) {
16782 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_fts.key_value.string_type_value);
16783 } 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) {
16784 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_attach.key_value.string_type_value);
16788 EM_SAFE_FREE(*input_filter_list);
16790 EM_DEBUG_FUNC_END("err [%d]", err);
16794 /* Tasks --------------------------------------------------------------------------*/
16795 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)
16797 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);
16801 int err = EMAIL_ERROR_NONE;
16803 DB_STMT hStmt = NULL;
16804 char sql_query_string[QUERY_SIZE] = {0, };
16805 sqlite3 *local_db_handle = NULL;
16806 char *sql = "SELECT max(rowid) FROM mail_task_tbl;";
16807 char **result = NULL;
16809 if (input_task_parameter == NULL || output_task_id == NULL) {
16810 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16811 return EMAIL_ERROR_INVALID_PARAM;
16814 local_db_handle = emstorage_get_db_connection(multi_user_name);
16815 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, err);
16817 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
16818 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16819 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
16821 if (NULL == result[1])
16824 task_id = atoi(result[1])+1;
16826 *output_task_id = task_id;
16828 sqlite3_free_table(result);
16831 SNPRINTF(sql_query_string, sizeof(sql_query_string),
16832 "INSERT INTO mail_task_tbl VALUES "
16834 " ? " /* task_id */
16835 " , ? " /* task_type */
16836 " , ? " /* task_status */
16837 " , ? " /* task_priority */
16838 " , ? " /* task_parameter_length */
16839 " , ? " /* task_parameter */
16840 " , ? " /* date_time */
16843 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16844 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16845 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16847 EM_DEBUG_LOG_SEC(">>>> SQL STMT [%s] ", sql_query_string);
16850 _bind_stmt_field_data_int(hStmt, i++, task_id);
16851 _bind_stmt_field_data_int(hStmt, i++, input_task_type);
16852 _bind_stmt_field_data_int(hStmt, i++, EMAIL_TASK_STATUS_WAIT);
16853 _bind_stmt_field_data_int(hStmt, i++, input_task_priority);
16854 _bind_stmt_field_data_int(hStmt, i++, input_task_parameter_length);
16855 _bind_stmt_field_data_blob(hStmt, i++, input_task_parameter, input_task_parameter_length);
16856 _bind_stmt_field_data_int(hStmt, i++, time(NULL));
16858 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16860 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {err = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
16861 ("sqlite3_step fail:%d", rc));
16862 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16863 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
16865 ret = (err == EMAIL_ERROR_NONE);
16868 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, err);
16870 if (hStmt != NULL) {
16871 rc = sqlite3_finalize(hStmt);
16872 if (rc != SQLITE_OK) {
16873 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16874 err = EMAIL_ERROR_DB_FAILURE;
16878 EM_DEBUG_FUNC_END("err [%d]", err);
16882 INTERNAL_FUNC int emstorage_delete_task(char *multi_user_name, int task_id, int transaction)
16884 EM_DEBUG_FUNC_BEGIN("task_id[%d], transaction[%d]", task_id, transaction);
16886 int err = EMAIL_ERROR_NONE;
16887 char sql_query_string[QUERY_SIZE] = {0, };
16889 sqlite3 *local_db_handle = NULL;
16892 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16893 return EMAIL_ERROR_INVALID_PARAM;
16896 local_db_handle = emstorage_get_db_connection(multi_user_name);
16898 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
16900 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_task_tbl WHERE task_id = %d", task_id);
16901 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16902 if (err != EMAIL_ERROR_NONE) {
16903 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16910 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
16912 EM_DEBUG_FUNC_END("err [%d]", err);
16916 INTERNAL_FUNC int emstorage_update_task_status(char *multi_user_name, int task_id, email_task_status_type_t task_status, int transaction)
16918 EM_DEBUG_FUNC_BEGIN("task_id[%d] task_status[%d] transaction[%d]", task_id, task_status, transaction);
16920 int err = EMAIL_ERROR_NONE;
16921 char sql_query_string[QUERY_SIZE] = {0, };
16923 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16924 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
16926 SNPRINTF(sql_query_string, sizeof(sql_query_string),
16927 "UPDATE mail_task_tbl SET"
16928 " task_status = %d"
16929 " WHERE task_id = %d"
16932 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16933 if (err != EMAIL_ERROR_NONE) {
16934 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16941 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
16943 EM_DEBUG_FUNC_END("err [%d]", err);
16947 INTERNAL_FUNC int emstorage_query_task(char *multi_user_name,
16948 const char *input_conditional_clause,
16949 const char *input_ordering_clause,
16950 email_task_t **output_task_list,
16951 int *output_task_count)
16953 EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_ordering_clause [%p], "
16954 "output_task_list[%p], output_task_count[%d]",
16955 input_conditional_clause, input_ordering_clause, output_task_list, output_task_count);
16956 int i = 0, count = 0, rc = -1;
16958 int field_index = 0;
16959 int err = EMAIL_ERROR_NONE;
16960 email_task_t *task_item_from_tbl = NULL;
16961 char sql_query_string[QUERY_SIZE] = {0, };
16962 char *field_list = "task_id, task_type, task_status, task_priority, task_parameter_length, task_parameter ";
16964 sqlite3 *local_db_handle = NULL;
16965 DB_STMT hStmt = NULL;
16967 EM_IF_NULL_RETURN_VALUE(input_conditional_clause, false);
16968 EM_IF_NULL_RETURN_VALUE(output_task_count, false);
16970 local_db_handle = emstorage_get_db_connection(multi_user_name);
16972 SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
16973 "SELECT COUNT(*) FROM mail_task_tbl %s", input_conditional_clause);
16974 EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
16976 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
16977 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16978 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16980 count = atoi(result[1]);
16981 sqlite3_free_table(result);
16983 EM_DEBUG_LOG("count = %d", rc);
16986 EM_DEBUG_EXCEPTION("no task found...");
16987 err = EMAIL_ERROR_TASK_NOT_FOUND;
16991 SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
16992 "SELECT %s FROM mail_task_tbl %s %s", field_list, input_conditional_clause, input_ordering_clause);
16993 EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
16995 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16997 EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
16998 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16999 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17001 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17002 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17003 ("sqlite3_step fail:%d", rc));
17005 if (rc == SQLITE_DONE) {
17006 EM_DEBUG_EXCEPTION("no task found...");
17007 err = EMAIL_ERROR_TASK_NOT_FOUND;
17012 if (!(task_item_from_tbl = (email_task_t*)em_malloc(sizeof(email_task_t) * count))) {
17013 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
17014 err = EMAIL_ERROR_OUT_OF_MEMORY;
17018 for (i = 0; i < count; i++) {
17019 /* get recordset */
17022 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_id), field_index++);
17023 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_type), field_index++);
17024 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_status), field_index++);
17025 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_priority), field_index++);
17026 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_parameter_length), field_index++);
17027 _get_stmt_field_data_blob(hStmt, (void**)&(task_item_from_tbl[i].task_parameter), field_index++);
17029 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17030 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i, rc);
17031 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17032 ("sqlite3_step fail:%d", rc));
17037 if (err == EMAIL_ERROR_NONE && output_task_list) {
17038 *output_task_list = task_item_from_tbl;
17039 *output_task_count = count;
17041 if (task_item_from_tbl) {
17042 for (i = 0; i < count; i++)
17043 EM_SAFE_FREE(task_item_from_tbl[i].task_parameter);
17045 free(task_item_from_tbl);
17049 if (hStmt != NULL) {
17050 rc = sqlite3_finalize(hStmt);
17052 if (rc != SQLITE_OK) {
17053 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17054 err = EMAIL_ERROR_DB_FAILURE;
17058 EM_DEBUG_FUNC_END("err [%d]", err);
17062 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)
17064 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);
17065 int err = EMAIL_ERROR_NONE;
17067 if (message_id == NULL) {
17068 EM_DEBUG_EXCEPTION("Invalid parameter");
17069 err = EMAIL_ERROR_INVALID_PARAM;
17075 int temp_mail_id = 0;
17076 int where_pararaph_length = 0;
17077 char *where_pararaph = NULL;
17078 char sql_query_string[QUERY_SIZE] = {0, };
17079 char **result = NULL;
17080 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17082 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id from mail_tbl ");
17084 where_pararaph_length = EM_SAFE_STRLEN(message_id) + 100;
17085 where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
17086 if (where_pararaph == NULL) {
17087 EM_DEBUG_EXCEPTION("em_mallocfailed");
17088 err = EMAIL_ERROR_OUT_OF_MEMORY;
17092 if (account_id != ALL_ACCOUNT)
17093 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);
17095 sqlite3_snprintf(where_pararaph_length, where_pararaph, "WHERE mailbox_type = %d AND message_id like '%q'", input_mailbox_type, message_id);
17097 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
17098 EM_SAFE_STRNCAT(sql_query_string, where_pararaph , QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
17100 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
17102 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
17103 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17104 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17107 EM_DEBUG_LOG("Count of mails [%d]", count);
17110 _get_table_field_data_int(result, &temp_mail_id, 1);
17111 EM_DEBUG_LOG("Searched mail_id [%d]", temp_mail_id);
17113 memset(sql_query_string, 0x00, QUERY_SIZE);
17114 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string, "UPDATE mail_tbl set server_mail_id = '%q'", server_uid);
17116 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE) {
17117 EM_SAFE_STRNCAT(sql_query_string, where_pararaph,
17118 QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
17121 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17122 if (err != EMAIL_ERROR_NONE) {
17123 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
17128 err = EMAIL_ERROR_MAIL_NOT_FOUND;
17133 sqlite3_free_table(result);
17137 EM_SAFE_FREE(where_pararaph);
17139 if (mail_id != NULL)
17140 *mail_id = temp_mail_id;
17142 EM_DEBUG_FUNC_END("err : [%d]", err);
17145 /* Tasks --------------------------------------------------------------------------*/
17147 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
17148 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)
17150 EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
17152 if (!local_activity || !activity_id) {
17153 EM_DEBUG_EXCEPTION("local_activity[%p], activity_id[%p]", local_activity, activity_id);
17154 if (err_code != NULL)
17155 *err_code = EMAIL_ERROR_INVALID_PARAM;
17161 int error = EMAIL_ERROR_NONE;
17164 char sql_query_string[QUERY_SIZE] = {0, };
17165 DB_STMT hStmt = NULL;
17167 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17168 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17170 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17171 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17172 "INSERT INTO mail_auto_download_activity_tbl VALUES "
17174 "? " /* Activity ID */
17176 ",?" /* Account ID */
17177 ",?" /* Local Mail ID */
17178 ",?" /* Server mail ID */
17179 ",?" /* Mailbox ID*/
17180 ",?" /* Multi USER NAME */
17183 char *sql = "SELECT max(rowid) FROM mail_auto_download_activity_tbl;";
17184 char **result = NULL;
17187 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
17189 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17190 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
17192 if (NULL == result[1]) rc = 1;
17193 else rc = atoi(result[1])+1;
17194 sqlite3_free_table(result);
17197 *activity_id = local_activity->activity_id = rc;
17199 EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [%d], MAIL ID [%d], SERVER MAIL ID [%lu]",
17200 local_activity->activity_id, local_activity->mail_id, local_activity->server_mail_id);
17202 if (local_activity->mailbox_id)
17203 EM_DEBUG_LOG(" MAILBOX ID [%d]", local_activity->mailbox_id);
17206 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17207 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17208 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17211 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
17212 _bind_stmt_field_data_int(hStmt, i++, local_activity->status);
17213 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
17214 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
17215 _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
17216 _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
17217 _bind_stmt_field_data_string(hStmt, i++, (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
17220 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17222 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
17223 ("sqlite3_step fail:%d", rc));
17224 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17225 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
17230 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17231 if (hStmt != NULL) {
17232 rc = sqlite3_finalize(hStmt);
17234 if (rc != SQLITE_OK) {
17235 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17236 error = EMAIL_ERROR_DB_FAILURE;
17240 if (err_code != NULL)
17243 EM_DEBUG_FUNC_END("ret [%d]", ret);
17248 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)
17250 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);
17252 if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
17253 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);
17255 if (err_code != NULL)
17256 *err_code = EMAIL_ERROR_INVALID_PARAM;
17262 int error = EMAIL_ERROR_NONE;
17263 char sql_query_string[QUERY_SIZE] = {0, };
17264 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17266 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17267 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17269 if (activity_id == 0)
17270 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);
17272 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);
17274 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17275 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17276 if (error != EMAIL_ERROR_NONE) {
17277 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17281 /* validate activity existence */
17282 rc = sqlite3_changes(local_db_handle);
17284 EM_DEBUG_EXCEPTION("No matching activity found");
17285 error = EMAIL_ERROR_DATA_NOT_FOUND;
17293 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17295 if (err_code != NULL)
17298 EM_DEBUG_FUNC_END("ret [%d]", ret);
17302 INTERNAL_FUNC int emstorage_delete_all_auto_download_activity(char *multi_user_name, int account_id, int transaction, int *err_code)
17304 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17306 if (account_id < FIRST_ACCOUNT_ID) {
17307 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17308 if (err_code != NULL)
17309 *err_code = EMAIL_ERROR_INVALID_PARAM;
17315 int error = EMAIL_ERROR_NONE;
17316 char sql_query_string[QUERY_SIZE] = {0, };
17318 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17320 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17321 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17322 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d", account_id);
17324 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17325 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17326 if (error != EMAIL_ERROR_NONE) {
17327 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17331 rc = sqlite3_changes(local_db_handle);
17333 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17334 error = EMAIL_ERROR_DATA_NOT_FOUND;
17342 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17344 if (err_code != NULL)
17347 EM_DEBUG_FUNC_END("ret [%d]", ret);
17352 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)
17354 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17356 if (account_id < FIRST_ACCOUNT_ID || mailbox_id < 0) {
17357 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17358 if (err_code != NULL)
17359 *err_code = EMAIL_ERROR_INVALID_PARAM;
17365 int error = EMAIL_ERROR_NONE;
17366 char sql_query_string[QUERY_SIZE] = {0, };
17368 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17370 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17371 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17372 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);
17374 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17375 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17376 if (error != EMAIL_ERROR_NONE) {
17377 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17381 rc = sqlite3_changes(local_db_handle);
17383 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17384 error = EMAIL_ERROR_DATA_NOT_FOUND;
17392 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17394 if (err_code != NULL)
17397 EM_DEBUG_FUNC_END("ret [%d]", ret);
17402 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)
17404 EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
17406 if (account_id < FIRST_ACCOUNT_ID || !event_start || input_mailbox_id <= 0 || !count) {
17407 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);
17409 if (err_code != NULL)
17410 *err_code = EMAIL_ERROR_INVALID_PARAM;
17417 int error = EMAIL_ERROR_NONE;
17419 DB_STMT hStmt = NULL;
17420 email_event_auto_download *event_list = NULL;
17421 char sql_query_string[QUERY_SIZE] = {0, };
17423 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17425 EMSTORAGE_START_READ_TRANSACTION(transaction);
17427 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17428 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);
17431 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17432 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17433 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17435 *count = atoi(result[1]);
17436 sqlite3_free_table(result);
17438 EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
17441 EM_DEBUG_LOG("No matched activity found in mail_auto_download_activity_tbl");
17442 error = EMAIL_ERROR_MAIL_NOT_FOUND;
17446 EM_DEBUG_LOG("Activity Count = %d", *count);
17448 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17449 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);
17451 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17454 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17456 EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
17457 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17458 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17461 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17462 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17463 ("sqlite3_step fail:%d", rc));
17465 if (!(event_list = (email_event_auto_download *)em_malloc(sizeof(email_event_auto_download)*(*count)))) {
17466 EM_DEBUG_EXCEPTION("Malloc failed");
17468 error = EMAIL_ERROR_OUT_OF_MEMORY;
17472 for (i = 0; i < (*count); i++) {
17473 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17474 _get_stmt_field_data_int(hStmt, &(event_list[i].status), STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17475 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17476 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17477 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17478 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17480 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17481 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17482 ("sqlite3_step fail:%d", rc));
17490 *event_start = event_list;
17492 EM_SAFE_FREE(event_list);
17493 *event_start = NULL;
17497 if (hStmt != NULL) {
17498 rc = sqlite3_finalize(hStmt);
17500 if (rc != SQLITE_OK) {
17501 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17502 error = EMAIL_ERROR_DB_FAILURE;
17506 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17507 if (err_code != NULL)
17510 EM_DEBUG_FUNC_END("ret [%d]", ret);
17515 INTERNAL_FUNC int emstorage_get_auto_download_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
17517 EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
17519 if (!activity_count || !err_code) {
17520 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
17521 if (err_code != NULL)
17522 *err_code = EMAIL_ERROR_INVALID_PARAM;
17528 int error = EMAIL_ERROR_NONE;
17529 DB_STMT hStmt = NULL;
17530 char sql_query_string[QUERY_SIZE] = {0, };
17532 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17534 EMSTORAGE_START_READ_TRANSACTION(transaction);
17535 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17537 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl;");
17539 EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
17542 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17543 EM_DEBUG_LOG_DEV("before sqlite3_prepare hStmt = %p", hStmt);
17544 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17545 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17548 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17549 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17550 ("sqlite3_step fail:%d", rc));
17552 _get_stmt_field_data_int(hStmt, activity_count, 0);
17554 EM_DEBUG_LOG("counts of activities in activity table [%d]", *activity_count);
17560 if (hStmt != NULL) {
17561 rc = sqlite3_finalize(hStmt);
17563 if (rc != SQLITE_OK) {
17564 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17565 error = EMAIL_ERROR_DB_FAILURE;
17569 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17570 if (err_code != NULL)
17573 EM_DEBUG_FUNC_END("ret [%d]", ret);
17578 INTERNAL_FUNC int emstorage_get_auto_download_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
17580 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
17582 if (!account_list || !count) {
17583 EM_DEBUG_EXCEPTION("account_list[%p], count[%p]", account_list, count);
17584 if (err_code != NULL)
17585 *err_code = EMAIL_ERROR_INVALID_PARAM;
17590 int error = EMAIL_ERROR_NONE;
17591 char *sql = "SELECT count(distinct account_id) FROM mail_auto_download_activity_tbl";
17593 int i = 0, rc = -1;
17594 int *result_account_list = NULL;
17595 DB_STMT hStmt = NULL;
17596 char sql_query_string[QUERY_SIZE] = {0, };
17597 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17599 EMSTORAGE_START_READ_TRANSACTION(transaction);
17601 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
17602 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17603 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
17605 *count = atoi(result[1]);
17606 sqlite3_free_table(result);
17609 EM_DEBUG_EXCEPTION("no account found...");
17610 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
17615 EM_DEBUG_LOG("Account count [%d]", *count);
17617 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17619 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_auto_download_activity_tbl");
17621 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17624 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17626 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
17627 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17628 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17631 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17632 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17633 ("sqlite3_step fail:%d", rc));
17635 if (NULL == (result_account_list = (int *)em_malloc(sizeof(int)*(*count)))) {
17636 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
17637 error = EMAIL_ERROR_OUT_OF_MEMORY;
17641 for (i = 0; i < (*count); i++) {
17642 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
17644 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17645 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17646 ("sqlite3_step fail:%d", rc));
17647 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
17655 *account_list = result_account_list;
17657 EM_SAFE_FREE(result_account_list);
17659 if (hStmt != NULL) {
17660 rc = sqlite3_finalize(hStmt);
17662 if (rc != SQLITE_OK) {
17663 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17664 error = EMAIL_ERROR_DB_FAILURE;
17668 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17669 if (err_code != NULL)
17671 EM_DEBUG_FUNC_END("ret [%d]", ret);
17676 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)
17678 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
17680 if (account_id < FIRST_ACCOUNT_ID || !mailbox_list || !count) {
17681 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p]", account_id, mailbox_list, count);
17682 if (err_code != NULL)
17683 *err_code = EMAIL_ERROR_INVALID_PARAM;
17688 int error = EMAIL_ERROR_NONE;
17690 int i = 0, rc = -1;
17691 int *mbox_list = NULL;
17692 DB_STMT hStmt = NULL;
17693 char sql_query_string[QUERY_SIZE] = {0, };
17695 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17697 EMSTORAGE_START_READ_TRANSACTION(transaction);
17699 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17700 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);
17703 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17704 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17705 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17707 *count = atoi(result[1]);
17708 sqlite3_free_table(result);
17711 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
17712 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
17716 EM_DEBUG_LOG("Mailbox count = %d", *count);
17718 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17720 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);
17722 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
17725 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17728 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17729 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17732 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17733 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17734 ("sqlite3_step fail:%d", rc));
17736 mbox_list = (int *)em_malloc(sizeof(int)*(*count)); /* prevent */
17737 if (mbox_list == NULL) {
17738 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
17739 error = EMAIL_ERROR_OUT_OF_MEMORY;
17743 for (i = 0; i < (*count); i++) {
17744 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
17745 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17747 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17748 ("sqlite3_step fail:%d", rc));
17749 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
17757 *mailbox_list = mbox_list;
17759 EM_SAFE_FREE(mbox_list);
17761 if (hStmt != NULL) {
17762 rc = sqlite3_finalize(hStmt);
17764 if (rc != SQLITE_OK) {
17765 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17766 error = EMAIL_ERROR_DB_FAILURE;
17770 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17771 if (err_code != NULL)
17773 EM_DEBUG_FUNC_END("ret [%d]", ret);
17778 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)
17780 EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
17782 if (account_id < FIRST_ACCOUNT_ID || !activity_count || !err_code) {
17783 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
17784 if (err_code != NULL)
17785 *err_code = EMAIL_ERROR_INVALID_PARAM;
17791 int error = EMAIL_ERROR_NONE;
17792 char sql_query_string[QUERY_SIZE] = {0, };
17793 DB_STMT hStmt = NULL;
17795 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17797 EMSTORAGE_START_READ_TRANSACTION(transaction);
17798 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17799 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);
17801 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
17803 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17804 EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
17805 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17806 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17808 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17809 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17810 ("sqlite3_step fail:%d", rc));
17812 _get_stmt_field_data_int(hStmt, activity_count, 0);
17814 EM_DEBUG_LOG("count of activities in activity table [%d]", *activity_count);
17820 if (hStmt != NULL) {
17821 rc = sqlite3_finalize(hStmt);
17823 if (rc != SQLITE_OK) {
17824 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17825 error = EMAIL_ERROR_DB_FAILURE;
17829 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17831 if (err_code != NULL)
17834 EM_DEBUG_FUNC_END("ret [%d]", ret);
17839 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)
17841 EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mailbox_id[%d]", old_server_uid, new_server_uid, mailbox_id);
17843 int rc = -1, ret = false;
17844 int error = EMAIL_ERROR_NONE;
17845 char sql_query_string[QUERY_SIZE] = {0, };
17846 int transaction = true;
17848 if (!old_server_uid || !new_server_uid || (!mailbox_name && mailbox_id < 0)) {
17849 EM_DEBUG_EXCEPTION("Invalid parameters");
17850 error = EMAIL_ERROR_INVALID_PARAM;
17854 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17856 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17857 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17859 if (mailbox_id > 0)
17860 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17861 "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);
17862 else if (mailbox_name)
17863 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17864 "UPDATE mail_auto_download_activity_tbl SET server_mail_id = %s WHERE server_mail_id = %s ", new_server_uid, old_server_uid);
17866 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17867 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17868 if (error != EMAIL_ERROR_NONE) {
17869 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17873 rc = sqlite3_changes(local_db_handle);
17875 EM_DEBUG_LOG("No matching found in mail_auto_download_activity_tbl");
17881 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17883 if (err_code != NULL)
17886 EM_DEBUG_FUNC_END("ret [%d]", ret);
17892 #ifdef __FEATURE_UPDATE_DB_TABLE_SCHEMA__
17897 } email_column_info_t;
17899 static int get_column_information_from_table_callback(void *arg1, int argc, char **argv, char **input_column_name)
17901 EM_DEBUG_FUNC_BEGIN("arg1[%p] argc[%d] argv[%p] column_name[%p]", arg1, argc, argv, input_column_name);
17905 char *column_name = NULL;
17906 char *column_type = NULL;
17907 GList *new_list = *((GList**)arg1);
17908 email_column_info_t *column_info_item = NULL;
17910 for (i = 0; i < argc; ++i) {
17911 /* EM_DEBUG_LOG("%s = %s", input_column_name[i], argv[i]); */
17912 if (EM_SAFE_STRCMP(input_column_name[i], "name") == 0) {
17914 EM_SAFE_FREE(column_name);
17916 column_name = EM_SAFE_STRDUP(argv[i]);
17918 } else if (EM_SAFE_STRCMP(input_column_name[i], "type") == 0) {
17920 EM_SAFE_FREE(column_type);
17922 column_type = EM_SAFE_STRDUP(argv[i]);
17927 EM_DEBUG_LOG("column_name[%s] column_type[%s]", column_name, column_type);
17928 column_info_item = em_malloc(sizeof(email_column_info_t));
17929 if (column_info_item == NULL) {
17930 EM_DEBUG_EXCEPTION("em_mallocfailed");
17934 column_info_item->column_name = EM_SAFE_STRDUP(column_name);
17935 column_info_item->column_type = EM_SAFE_STRDUP(column_type);
17936 new_list = g_list_append(new_list, (gpointer)column_info_item);
17937 *((GList**)arg1) = new_list;
17942 EM_SAFE_FREE(column_name);
17943 EM_SAFE_FREE(column_type);
17945 EM_DEBUG_FUNC_END();
17949 static int emstorage_get_column_information_from_table(char *multi_user_name, const char *input_table_name, GList **output_column_info)
17951 EM_DEBUG_FUNC_BEGIN("input_table_name[%p] output_column_info[%p]", input_table_name, output_column_info);
17952 int err = EMAIL_ERROR_NONE;
17953 int result_from_sqlite = 0;
17954 char *error_message_from_sqlite = NULL;
17955 char sql_query_string[QUERY_SIZE] = {0, };
17956 GList *new_list = NULL;
17957 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17959 SNPRINTF(sql_query_string, QUERY_SIZE, "pragma table_info(%s);", input_table_name);
17961 result_from_sqlite = sqlite3_exec(local_db_handle, sql_query_string, get_column_information_from_table_callback, &new_list, &error_message_from_sqlite);
17963 if (result_from_sqlite != SQLITE_OK)
17964 EM_DEBUG_EXCEPTION("sqlite3_exec returns [%d]", result_from_sqlite);
17966 EM_DEBUG_LOG("new_list[%p] output_column_info[%p]", new_list, output_column_info);
17968 if (new_list && output_column_info) {
17969 EM_DEBUG_LOG("g_list_length[%d]", g_list_length(new_list));
17970 *output_column_info = new_list;
17973 EM_DEBUG_FUNC_END("err [%d]", err);
17977 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)
17979 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);
17980 int error = EMAIL_ERROR_NONE;
17982 sqlite3 *local_db_handle = NULL;
17983 char sql_query_string[QUERY_SIZE] = {0, };
17985 local_db_handle = emstorage_get_db_connection(multi_user_name);
17987 if (input_full_query == NULL || input_source_table_name == NULL || input_new_table_name == NULL) {
17988 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17989 error = EMAIL_ERROR_INVALID_PARAM;
17993 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17994 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17996 EM_DEBUG_LOG("[%s] will be replaced by [%s]", input_source_table_name, input_new_table_name);
17998 EM_SAFE_STRNCPY(sql_query_string, input_full_query[input_query_index], sizeof(sql_query_string)-1); /*prevent 21984*/
17999 reg_replace(sql_query_string, input_source_table_name, input_new_table_name);
18001 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
18004 if (error == EMAIL_ERROR_NONE) {
18005 EMSTORAGE_PROTECTED_FUNC_CALL(
18006 sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
18008 EMSTORAGE_PROTECTED_FUNC_CALL(
18009 sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
18012 EM_DEBUG_FUNC_END("error [%d]", error);
18016 static int emstorage_add_column(char *multi_user_name, char *input_table_name, email_column_info_t *input_new_column)
18018 EM_DEBUG_FUNC_BEGIN("input_table_name[%p] input_new_column[%p]", input_table_name, input_new_column);
18019 int error = EMAIL_ERROR_NONE;
18021 sqlite3 *local_db_handle = NULL;
18022 char sql_query_string[QUERY_SIZE] = {0, };
18024 local_db_handle = emstorage_get_db_connection(multi_user_name);
18026 if (input_table_name == NULL || input_new_column == NULL) {
18027 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
18028 error = EMAIL_ERROR_INVALID_PARAM;
18032 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
18033 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
18034 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);
18035 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
18038 if (error == EMAIL_ERROR_NONE) {
18039 EMSTORAGE_PROTECTED_FUNC_CALL(
18040 sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
18042 EMSTORAGE_PROTECTED_FUNC_CALL(
18043 sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
18046 EM_DEBUG_FUNC_END("error [%d]", error);
18050 static int emstorage_drop_table(char *multi_user_name, char *input_table_name)
18052 EM_DEBUG_FUNC_BEGIN("input_table_name[%p]", input_table_name);
18053 int error = EMAIL_ERROR_NONE;
18055 sqlite3 *local_db_handle = NULL;
18056 char sql_query_string[QUERY_SIZE] = {0, };
18058 local_db_handle = emstorage_get_db_connection(multi_user_name);
18060 if (input_table_name == NULL) {
18061 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
18062 error = EMAIL_ERROR_INVALID_PARAM;
18066 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
18067 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
18068 SNPRINTF(sql_query_string, QUERY_SIZE, "DROP TABLE %s;", input_table_name);
18069 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
18072 if (error == EMAIL_ERROR_NONE) {
18073 EMSTORAGE_PROTECTED_FUNC_CALL(
18074 sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
18076 EMSTORAGE_PROTECTED_FUNC_CALL(
18077 sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
18080 EM_DEBUG_FUNC_END("error [%d]", error);
18084 gint glist_compare_column_name(gconstpointer old_column_info, gconstpointer new_column_info)
18086 EM_DEBUG_FUNC_BEGIN("old_column_info[%p] new_column_info[%p]", old_column_info, new_column_info);
18087 email_column_info_t *left_one = (email_column_info_t*)old_column_info;
18088 email_column_info_t *right_one = (email_column_info_t*)new_column_info;
18090 if (old_column_info == NULL || new_column_info == NULL)
18093 return EM_SAFE_STRCMP((char*)left_one->column_name, (char*)right_one->column_name);
18096 INTERNAL_FUNC int emstorage_update_db_table_schema(char *multi_user_name)
18098 EM_DEBUG_FUNC_BEGIN();
18101 int error = EMAIL_ERROR_NONE;
18103 email_column_info_t *new_column_info = NULL;
18104 email_column_info_t *p_column_info = NULL;
18105 char **create_table_query = NULL;
18106 GList *found_data = NULL;
18107 GList *column_list_of_old_table = NULL;
18108 GList *column_list_of_new_table = NULL;
18109 char table_names[CREATE_TABLE_MAX][2][50] = { { "mail_account_tbl", "mail_account_tbl_new" },
18110 { "mail_box_tbl", "mail_box_tbl_new" },
18111 { "mail_read_mail_uid_tbl", "mail_read_mail_uid_tbl_new" },
18112 { "mail_rule_tbl", "mail_rule_tbl_new" },
18113 { "mail_tbl", "mail_tbl_new" },
18114 { "mail_attachment_tbl", "mail_attachment_tbl_new" },
18115 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
18116 { "mail_partial_body_activity_tbl", "mail_partial_body_activity_tbl_new" },
18120 { "mail_meeting_tbl", "mail_meeting_tbl_new" },
18121 #ifdef __FEATURE_LOCAL_ACTIVITY__
18122 { "mail_local_activity_tbl", "mail_local_activity_tbl_new" },
18126 { "mail_certificate_tbl", "mail_certificate_tbl_new" },
18127 { "mail_task_tbl", "mail_task_tbl_new" },
18128 #ifdef __FEATURE_BODY_SEARCH__
18129 { "mail_text_tbl", "mail_text_tbl_new" },
18134 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
18135 { "mail_auto_download_activity_tbl", "mail_auto_download_activity_tbl_new" }
18142 error = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
18144 if (error != EMAIL_ERROR_NONE) {
18145 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
18149 if (query_len < CREATE_TABLE_MAX) {
18150 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
18151 error = EMAIL_ERROR_SYSTEM_FAILURE;
18155 for (i = CREATE_TABLE_MAIL_ACCOUNT_TBL; i < CREATE_TABLE_MAX; i++) {
18156 EM_DEBUG_LOG("table [%s] new_table [%s]", table_names[i][0], table_names[i][1]);
18157 if (EM_SAFE_STRLEN(table_names[i][0]) && EM_SAFE_STRLEN(table_names[i][1])) {
18158 /* Check existing of _new table */
18159 emstorage_drop_table(multi_user_name, table_names[i][1]);
18160 error = emstorage_create_renamed_table(multi_user_name, create_table_query, i, table_names[i][0], table_names[i][1]);
18161 if (error != EMAIL_ERROR_NONE) {
18162 EM_DEBUG_EXCEPTION("emstorage_create_renamed_table failed [%d]", error);
18166 emstorage_get_column_information_from_table(multi_user_name, table_names[i][0], &column_list_of_old_table);
18167 emstorage_get_column_information_from_table(multi_user_name, table_names[i][1], &column_list_of_new_table);
18169 /* Compare fields and add new field */
18170 for (j = 0; j < g_list_length(column_list_of_new_table); j++) {
18171 new_column_info = (email_column_info_t*)g_list_nth_data(column_list_of_new_table, j);
18172 found_data = g_list_find_custom(column_list_of_old_table, (gconstpointer)new_column_info, glist_compare_column_name);
18173 if (found_data == NULL) {
18175 emstorage_add_column(multi_user_name, table_names[i][0], new_column_info);
18179 emstorage_drop_table(multi_user_name, table_names[i][1]);
18181 EM_DEBUG_LOG("Skipped");
18186 if (create_table_query) {
18188 for (i = 0; i < query_len; i++) {
18189 if (create_table_query[i])
18190 EM_SAFE_FREE(create_table_query[i]);
18192 EM_SAFE_FREE(create_table_query);
18195 found_data = g_list_first(column_list_of_old_table);
18196 while (found_data != NULL) {
18197 p_column_info = (email_column_info_t *)found_data->data;
18198 EM_SAFE_FREE(p_column_info->column_name);
18199 EM_SAFE_FREE(p_column_info->column_type);
18200 EM_SAFE_FREE(p_column_info);
18202 found_data = g_list_next(found_data);
18204 g_list_free(column_list_of_old_table);
18206 found_data = g_list_first(column_list_of_new_table);
18207 while (found_data != NULL) {
18208 p_column_info = (email_column_info_t *)found_data->data;
18209 EM_SAFE_FREE(p_column_info->column_name);
18210 EM_SAFE_FREE(p_column_info->column_type);
18211 EM_SAFE_FREE(p_column_info);
18213 found_data = g_list_next(found_data);
18215 g_list_free(column_list_of_new_table);
18217 EM_DEBUG_FUNC_END("error [%d]", error);
18220 #endif /* __FEATURE_UPDATE_DB_TABLE_SCHEMA__ */
18221 /* LCOV_EXCL_STOP */