4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
23 /******************************************************************************
24 * File: email-storage.c
25 * Desc: email-service Storage Library on Sqlite3
30 * 2007.03.02 : created
31 *****************************************************************************/
38 #include <sys/types.h>
49 #include <tzplatform_config.h>
57 #include "email-internal-types.h"
58 #include "email-convert.h"
59 #include "email-core-utils.h"
60 #include "email-utilities.h"
61 #include "email-storage.h"
62 #include "email-debug-log.h"
63 #include "email-types.h"
64 #include "email-convert.h"
65 #include "email-core-signal.h"
66 #include "email-core-event.h"
67 #include "email-core-container.h"
68 #include "email-core-gmime.h"
69 #include "email-core-key-manager.h"
71 #define SETTING_MEMORY_TEMP_FILE_PATH "/tmp/email_tmp_file"
72 #define CONTENT_DATA "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset="
73 #define CONTENT_TYPE_DATA "<meta http-equiv=\"Content-Type\" content=\"text/html; charset="
75 #define FLAG_SEEN 0x01
76 #define FLAG_DELETED 0x02
77 #define FLAG_FLAGGED 0x04
78 #define FLAG_ANSWERED 0x08
80 #define FLAG_DRAFT 0x20
84 #define ISSUE_ORGANIZATION_LEN_IN_MAIL_CERTIFICATE_TBL 256
85 #define EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL 128
86 #define SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL 1027
87 #define FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL 256
89 #define ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL 50
90 #define RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 50
91 #define EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 128
92 #define USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL 50
93 #define PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL 50
94 #define SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 50
95 #define SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL 50
96 #define SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL 50
97 #define DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL 30
98 #define REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 128
99 #define RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 128
100 #define LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL 256
101 #define DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL 256
102 #define SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL 256
103 #define MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL 128
104 #define ALIAS_LEN_IN_MAIL_BOX_TBL 128
105 #define LOCAL_MBOX_LEN_IN_MAIL_READ_MAIL_UID_TBL 128
106 #define MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL 128
107 #define S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL 128
108 #define DATA2_LEN_IN_MAIL_READ_MAIL_UID_TBL 256
109 #define FILTER_NAME_LEN_IN_MAIL_RULE_TBL 256
110 #define VALUE_LEN_IN_MAIL_RULE_TBL 256
111 #define VALUE2_LEN_IN_MAIL_RULE_TBL 256
112 #define DEST_MAILBOX_LEN_IN_MAIL_RULE_TBL 128
113 #define MAILBOX_NAME_LEN_IN_MAIL_ATTACHMENT_TBL 128
114 #define ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL 256
115 #define ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL 256
116 #define CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL 256
117 #define ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL 128
118 #define MAILBOX_LEN_IN_MAIL_TBL 128
119 #define SERVER_MAILBOX_LEN_IN_MAIL_TBL 128
120 #define SERVER_MAIL_ID_LEN_IN_MAIL_TBL 128
121 #define FROM_LEN_IN_MAIL_TBL 256
122 #define SENDER_LEN_IN_MAIL_TBL 256
123 #define REPLY_TO_LEN_IN_MAIL_TBL 256
124 #define TO_LEN_IN_MAIL_TBL 3999
125 #define CC_LEN_IN_MAIL_TBL 3999
126 #define BCC_LEN_IN_MAIL_TBL 3999
127 #define RETURN_PATH_LEN_IN_MAIL_TBL 3999
128 #define SUBJECT_LEN_IN_MAIL_TBL 1027
129 #define THREAD_TOPIC_LEN_IN_MAIL_TBL 256
130 #define TEXT_1_LEN_IN_MAIL_TBL 256
131 #define TEXT_2_LEN_IN_MAIL_TBL 256
132 #define MIME_ENTITY_LEN_IN_MAIL_TBL 256
133 #define DATETIME_LEN_IN_MAIL_TBL 128
134 #define MESSAGE_ID_LEN_IN_MAIL_TBL 256
135 #define FROM_CONTACT_NAME_LEN_IN_MAIL_TBL 3999
136 #define FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL 3999
137 #define TO_CONTACT_NAME_LEN_IN_MAIL_TBL 3999
138 #define TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL 3999
139 #define MAILBOX_LEN_IN_MAIL_MEETING_TBL 128
140 #define LOCATION_LEN_IN_MAIL_MEETING_TBL 1024
141 #define GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL 512
142 #define STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL 32
143 #define DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL 32
144 #define PREVIEWBODY_LEN_IN_MAIL_TBL 512
145 #define CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL 256
146 #define UID "root" /* UID = root */
147 #define UID_DEFAULT 0 /* UID = root */
148 #define GID "priv_email" /* GID = priv_email */
149 #define GID_DEFAULT 10901 /* GID = priv_email */
150 #define MAX_GRP_BUF_SIZE (1024 * 4)
152 /* this define is used for query to change data (delete, insert, update) */
153 #define EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction_flag, error_code) \
155 if (transaction_flag) { \
156 if (emstorage_begin_transaction(multi_user_name, NULL, NULL, &error_code) == false) { \
157 EM_DEBUG_EXCEPTION("emstorage_begin_transaction error [%d]", error_code); \
163 /* this define is used for query to change data (delete, insert, update) */
164 #define EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction_flag, result_code, error_code) \
166 if (transaction_flag) { \
167 if (result_code == true) { \
168 if (emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL) == false) { \
169 EM_DEBUG_EXCEPTION("emstorage_commit_transaction error"); \
170 error_code = EMAIL_ERROR_DB_FAILURE; \
171 result_code = false; \
175 if (emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL) == false) { \
176 EM_DEBUG_EXCEPTION("emstorage_rollback_transaction error"); \
177 error_code = EMAIL_ERROR_DB_FAILURE; \
183 /* this define is used for query to read (select) */
184 #define EMSTORAGE_START_READ_TRANSACTION(transaction_flag) \
185 if (transaction_flag) { \
186 /*_timedlock_shm_mutex(mapped_for_db_lock, 2);*/\
189 /* this define is used for query to read (select) */
190 #define EMSTORAGE_FINISH_READ_TRANSACTION(transaction_flag) \
191 if (transaction_flag) { \
192 /*_unlockshm_mutex(mapped_for_db_lock);*/\
195 /* for safety DB operation */
196 static pthread_mutex_t _db_handle_lock = PTHREAD_MUTEX_INITIALIZER;
199 #define _MULTIPLE_DB_HANDLE
201 #ifdef _MULTIPLE_DB_HANDLE
209 #define MAX_DB_CLIENT 100
211 /* static int _db_handle_count = 0; */
212 db_handle_t _db_handle_list[MAX_DB_CLIENT] = {{NULL, 0, 0}, };
215 sqlite3 *emstorage_get_db_handle(char *multi_user_name)
217 EM_DEBUG_FUNC_BEGIN();
219 pthread_t current_thread_id = THREAD_SELF();
220 sqlite3 *result_db_handle = NULL;
222 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
223 ENTER_CRITICAL_SECTION(_db_handle_lock);
224 for (i = 0; i < MAX_DB_CLIENT; i++) {
225 if (EM_SAFE_STRCASECMP(_db_handle_list[i].user_name, multi_user_name) != 0)
228 if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id)) {
229 EM_DEBUG_LOG_DEV("found db handle at [%d]", i);
230 result_db_handle = _db_handle_list[i].db_handle;
234 LEAVE_CRITICAL_SECTION(_db_handle_lock);
236 ENTER_CRITICAL_SECTION(_db_handle_lock);
237 for (i = 0; i < MAX_DB_CLIENT; i++) {
238 if (EM_SAFE_STRLEN(_db_handle_list[i].user_name) > 0)
241 if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id)) {
242 EM_DEBUG_LOG_DEV("found db handle at [%d]", i);
243 result_db_handle = _db_handle_list[i].db_handle;
247 LEAVE_CRITICAL_SECTION(_db_handle_lock);
250 if (!result_db_handle)
251 EM_DEBUG_LOG("no db_handle for [%lu] found", current_thread_id);
254 return result_db_handle;
257 int emstorage_set_db_handle(char *multi_user_name, sqlite3 *db_handle)
259 EM_DEBUG_FUNC_BEGIN();
260 int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
261 pthread_t current_thread_id = THREAD_SELF();
263 ENTER_CRITICAL_SECTION(_db_handle_lock);
264 for (i = 0; i < MAX_DB_CLIENT; i++) {
265 if (_db_handle_list[i].thread_id == 0) {
266 _db_handle_list[i].thread_id = current_thread_id;
267 _db_handle_list[i].db_handle = db_handle;
268 /* Only distinguished container and host */
269 _db_handle_list[i].user_name = EM_SAFE_STRDUP(multi_user_name);
270 EM_DEBUG_LOG("current_thread_id [%lu], index [%d]", current_thread_id, i);
271 error_code = EMAIL_ERROR_NONE;
275 LEAVE_CRITICAL_SECTION(_db_handle_lock);
277 if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
278 EM_DEBUG_EXCEPTION("Exceeded the limitation of db client. Can't find empty slot in _db_handle_list.");
280 EM_DEBUG_FUNC_END("error_code [%d]", error_code);
284 int emstorage_remove_db_handle()
286 EM_DEBUG_FUNC_BEGIN();
287 int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
288 ENTER_CRITICAL_SECTION(_db_handle_lock);
289 for (i = 0; i < MAX_DB_CLIENT; i++) {
290 if (_db_handle_list[i].thread_id == THREAD_SELF()) {
291 _db_handle_list[i].thread_id = 0;
292 _db_handle_list[i].db_handle = NULL;
293 EM_SAFE_FREE(_db_handle_list[i].user_name);
295 EM_DEBUG_LOG("index [%d]", i);
296 error_code = EMAIL_ERROR_NONE;
300 LEAVE_CRITICAL_SECTION(_db_handle_lock);
302 if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
303 EM_DEBUG_EXCEPTION("Can't find proper thread_id");
305 EM_DEBUG_FUNC_END("error_code [%d]", error_code);
309 int emstorage_reset_db_handle_list()
311 EM_DEBUG_FUNC_BEGIN();
314 ENTER_CRITICAL_SECTION(_db_handle_lock);
315 for (i = 0; i < MAX_DB_CLIENT; i++) {
316 _db_handle_list[i].thread_id = 0;
317 _db_handle_list[i].db_handle = NULL;
318 EM_SAFE_FREE(_db_handle_list[i].user_name);
320 LEAVE_CRITICAL_SECTION(_db_handle_lock)
323 return EMAIL_ERROR_NONE;
327 sqlite3 *emstorage_get_db_connection(char *multi_user_name)
329 return emstorage_db_open(multi_user_name, NULL);
332 #else /* _MULTIPLE_DB_HANDLE */
334 sqlite3 *_db_handle = NULL;
336 sqlite3 *emstorage_get_db_connection()
338 if (NULL == _db_handle)
339 emstorage_db_open(NULL);
342 #endif /* _MULTIPLE_DB_HANDLE */
344 /* ------------------------------------------------------------------------------ */
345 /* Mutex using shared memory */
347 pthread_mutex_t mutex;
351 mmapped_t *mapped_for_db_lock = NULL;
352 int shm_fd_for_db_lock = 0;
354 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
355 mmapped_t *mapped_for_generating_mail_id = NULL;
356 int shm_fd_for_generating_mail_id = 0;
357 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
359 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__
360 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
362 _timedlock_shm_mutex(mapped_for_db_lock, 2);\
363 return_value = function_call;\
364 _unlockshm_mutex(mapped_for_db_lock);\
367 #else /* __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
368 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
369 { return_value = function_call; }
370 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
373 static int emstorage_exec_query_by_prepare_v2(sqlite3 *local_db_handle, char *query_string)
375 EM_DEBUG_FUNC_BEGIN("local_db_handle[%p] query_string[%p]", local_db_handle, query_string);
376 int error = EMAIL_ERROR_NONE;
378 DB_STMT db_statement = NULL;
380 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, query_string, EM_SAFE_STRLEN(query_string), &db_statement, NULL), rc);
381 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
382 ("sqlite3_prepare failed [%d] [%s]", rc, query_string));
384 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(db_statement), rc);
385 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
386 ("sqlite3_step failed [%d] [%s]", rc, query_string));
390 if (db_statement != NULL) {
391 rc = sqlite3_finalize(db_statement);
392 if (rc != SQLITE_OK) {
393 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
394 error = EMAIL_ERROR_DB_FAILURE;
401 INTERNAL_FUNC int emstorage_shm_file_init(const char *shm_file_name)
403 EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
404 char errno_buf[ERRNO_BUF_SIZE] = {0};
406 if (!shm_file_name) {
407 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
408 return EMAIL_ERROR_INVALID_PARAM;
411 int fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); /* note: permission is not working */
413 EM_DEBUG_EXCEPTION("shm_open errno [%d] [%s]", errno, EM_STRERROR(errno_buf));
414 return EMAIL_ERROR_SYSTEM_FAILURE;
418 EM_DEBUG_LOG("** Create SHM FILE **");
419 if (ftruncate(fd, sizeof(mmapped_t)) != 0) {
420 EM_DEBUG_EXCEPTION("ftruncate errno [%d]", errno);
421 return EMAIL_ERROR_SYSTEM_FAILURE;
424 mmapped_t *m = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
425 if (m == MAP_FAILED) {
426 EM_DEBUG_EXCEPTION("mmap errno [%d]", errno);
428 return EMAIL_ERROR_SYSTEM_FAILURE;
433 pthread_mutexattr_t mattr;
434 pthread_mutexattr_init(&mattr);
435 pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
436 pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
437 pthread_mutex_init(&(m->mutex), &mattr);
438 pthread_mutexattr_destroy(&mattr);
440 pthread_mutex_destroy(&(m->mutex));
441 munmap(m, sizeof(mmapped_t));
445 return EMAIL_ERROR_NONE;
448 int emstorage_shm_file_destroy(const char *shm_file_name)
450 EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
451 char errno_buf[ERRNO_BUF_SIZE] = {0};
453 if (!shm_file_name) {
454 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
455 return EMAIL_ERROR_INVALID_PARAM;
458 if (shm_unlink(shm_file_name) != 0)
459 EM_DEBUG_EXCEPTION("shm_unlink failed: %s", EM_STRERROR(errno_buf));
461 return EMAIL_ERROR_NONE;
464 int _initialize_shm_mutex(const char *shm_file_name, int *param_shm_fd, mmapped_t **param_mapped)
466 EM_DEBUG_FUNC_BEGIN("shm_file_name [%p] param_shm_fd [%p], param_mapped [%p]", shm_file_name, param_shm_fd, param_mapped);
467 char errno_buf[ERRNO_BUF_SIZE] = {0};
469 if (!shm_file_name || !param_shm_fd || !param_mapped) {
470 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
471 return EMAIL_ERROR_INVALID_PARAM;
474 if (!(*param_mapped)) {
475 EM_DEBUG_LOG("** mapping begin **");
476 if (!(*param_shm_fd)) { /* open shm_file_name at first. Otherwise, the num of files in /proc/pid/fd will be increasing */
477 *param_shm_fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
478 if ((*param_shm_fd) == -1) {
479 EM_DEBUG_EXCEPTION("FAIL: shm_open(): %s", EM_STRERROR(errno_buf));
480 return EMAIL_ERROR_SYSTEM_FAILURE;
484 fchmod((*param_shm_fd), 0666);
485 EM_DEBUG_LOG("** Create SHM FILE **");
486 if (ftruncate((*param_shm_fd), sizeof(mmapped_t)) != 0) {
487 EM_DEBUG_EXCEPTION("ftruncate errno [%d]", errno);
488 return EMAIL_ERROR_SYSTEM_FAILURE;
491 mmapped_t *tmp = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ|PROT_WRITE, MAP_SHARED, (*param_shm_fd), 0);
492 if (tmp == MAP_FAILED) {
493 EM_DEBUG_EXCEPTION("mmap failed: %s", EM_STRERROR(errno_buf));
494 return EMAIL_ERROR_SYSTEM_FAILURE;
499 pthread_mutexattr_t mattr;
500 pthread_mutexattr_init(&mattr);
501 pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
502 pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
503 pthread_mutex_init(&(tmp->mutex), &mattr);
504 pthread_mutexattr_destroy(&mattr);
510 return EMAIL_ERROR_NONE;
513 int _timedlock_shm_mutex(mmapped_t *param_mapped, int sec)
515 EM_DEBUG_FUNC_BEGIN("param_mapped [%p], sec [%d]", param_mapped, sec);
518 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
519 return EMAIL_ERROR_INVALID_PARAM;
522 struct timespec abs_time;
523 clock_gettime(CLOCK_REALTIME, &abs_time);
524 abs_time.tv_sec += sec;
525 char errno_buf[ERRNO_BUF_SIZE] = {0};
527 int err = pthread_mutex_timedlock(&(param_mapped->mutex), &abs_time);
529 if (err == EOWNERDEAD) {
530 err = pthread_mutex_consistent(&(param_mapped->mutex));
531 EM_DEBUG_EXCEPTION("Previous owner is dead with lock. Fix mutex : %s", EM_STRERROR(errno_buf));
532 } else if (err != 0) {
533 EM_DEBUG_EXCEPTION("ERROR : %s", EM_STRERROR(errno_buf));
538 return EMAIL_ERROR_NONE;
541 void _unlockshm_mutex(mmapped_t *param_mapped)
543 EM_DEBUG_FUNC_BEGIN();
544 pthread_mutex_unlock(&(param_mapped->mutex));
547 /* ------------------------------------------------------------------------------ */
550 static int _open_counter = 0;
552 static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name);
553 static int _read_password_from_secure_storage(char *file_name, char **password);
555 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
556 static int _get_cert_password_file_name(int index, char *cert_password_file_name);
560 const char *object_name;
561 unsigned int data_flag;
564 static const email_db_object_t _g_db_tables[] = {
565 { "mail_read_mail_uid_tbl", 1},
567 { "mail_attachment_tbl", 1},
571 static const email_db_object_t _g_db_indexes[] = {
572 { "mail_read_mail_uid_idx1", 1},
574 { "mail_attachment_idx1", 1},
579 CREATE_TABLE_MAIL_ACCOUNT_TBL,
580 CREATE_TABLE_MAIL_BOX_TBL,
581 CREATE_TABLE_MAIL_READ_MAIL_UID_TBL,
582 CREATE_TABLE_MAIL_RULE_TBL,
583 CREATE_TABLE_MAIL_TBL,
584 CREATE_TABLE_MAIL_ATTACHMENT_TBL,
585 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
586 CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL,
588 CREATE_TABLE_DUMMY_INDEX1,
590 CREATE_TABLE_MAIL_MEETING_TBL,
591 #ifdef __FEATURE_LOCAL_ACTIVITY__
592 CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL,
594 CREATE_TABLE_DUMMY_INDEX2,
596 CREATE_TABLE_MAIL_CERTIFICATE_TBL,
597 CREATE_TABLE_MAIL_TASK_TBL,
598 #ifdef __FEATURE_BODY_SEARCH__
599 CREATE_TABLE_MAIL_TEXT_TBL,
601 CREATE_TABLE_DUMMY_INDEX3,
604 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
605 CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
607 CREATE_TABLE_DUMMY_INDEX4,
611 CREATE_TABLE_MAIL_ACCOUNT_IDX,
612 CREATE_TABLE_MAIL_BOX_IDX,
613 CREATE_TABLE_MAIL_READ_MAIL_UID_IDX,
614 CREATE_TABLE_MAIL_IDX,
615 CREATE_TABLE_MAIL_ATTACHMENT_IDX,
616 CREATE_TABLE_MAIL_MEETING_IDX,
617 CREATE_TABLE_MAIL_TASK_IDX,
618 CREATE_TABLE_MAIL_DATETIME_IDX,
619 CREATE_TABLE_MAIL_THREAD_IDX,
624 DATA1_IDX_IN_MAIL_ACTIVITY_TBL = 0,
625 TRANSTYPE_IDX_IN_MAIL_ACTIVITY_TBL,
626 FLAG_IDX_IN_MAIL_ACTIVITY_TBL,
630 CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL = 0,
631 ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
632 ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
633 ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
634 EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
635 EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
636 EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
637 ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL,
638 EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL,
639 SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL,
640 FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL,
649 ACCOUNT_ID_IDX_IN_MAIL_BOX_TBL = 0,
650 LOCAL_YN_IDX_IN_MAIL_BOX_TBL,
651 MAILBOX_NAME_IDX_IN_MAIL_BOX_TBL,
652 MAILBOX_TYPE_IDX_IN_MAIL_BOX_TBL,
653 ALIAS_IDX_IN_MAIL_BOX_TBL,
654 SYNC_WITH_SERVER_YN_IDX_IN_MAIL_BOX_TBL,
655 MODIFIABLE_YN_IDX_IN_MAIL_BOX_TBL,
656 TOTAL_MAIL_COUNT_ON_SERVER_IDX_IN_MAIL_BOX_TBL,
657 ARCHIVE_IDX_IN_MAIL_BOX_TBL,
658 MAIL_SLOT_SIZE_IDX_IN_MAIL_BOX_TBL,
662 ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL = 0,
663 LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
664 MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL,
665 LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
666 SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
667 RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL,
668 SYNC_STATUS_IDX_IN_MAIL_READ_MAIL_UID_TBL,
669 FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL,
670 FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL,
671 IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, /* unused */
674 #ifdef __FEATURE_BODY_SEARCH__
676 MAIL_ID_IDX_IN_MAIL_TEXT_TBL = 0,
677 ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL,
678 MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL,
679 BODY_TEXT_IDX_IN_MAIL_TEXT_TBL,
680 FIELD_COUNT_OF_MAIL_TEXT_TBL,
685 ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL = 0,
686 RULE_ID_IDX_IN_MAIL_RULE_TBL,
687 FILTER_NAME_IDX_IN_MAIL_RULE_TBL,
688 TYPE_IDX_IN_MAIL_RULE_TBL,
689 VALUE_IDX_IN_MAIL_RULE_TBL,
690 VALUE2_IDX_IN_MAIL_RULE_TBL,
691 ACTION_TYPE_IDX_IN_MAIL_RULE_TBL,
692 TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL,
693 FLAG1_IDX_IN_MAIL_RULE_TBL,
694 FLAG2_IDX_IN_MAIL_RULE_TBL,
698 MAIL_ID_IDX_IN_MAIL_TBL = 0,
699 ACCOUNT_ID_IDX_IN_MAIL_TBL,
700 MAILBOX_ID_IDX_IN_MAIL_TBL,
701 MAILBOX_TYPE_IDX_IN_MAIL_TBL,
702 SUBJECT_IDX_IN_MAIL_TBL,
703 DATETIME_IDX_IN_MAIL_TBL,
704 SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL,
705 SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL,
706 SERVER_MAIL_ID_IDX_IN_MAIL_TBL,
707 MESSAGE_ID_IDX_IN_MAIL_TBL,
708 REFERENCE_ID_IDX_IN_MAIL_TBL,
709 FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL,
710 FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL,
711 FULL_ADDRESS_TO_IDX_IN_MAIL_TBL,
712 FULL_ADDRESS_CC_IDX_IN_MAIL_TBL,
713 FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL,
714 FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL,
715 EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL,
716 EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL,
717 ALIAS_SENDER_IDX_IN_MAIL_TBL,
718 ALIAS_RECIPIENT_IDX_IN_MAIL_TBL,
719 BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL,
720 FILE_PATH_PLAIN_IDX_IN_MAIL_TBL,
721 FILE_PATH_HTML_IDX_IN_MAIL_TBL,
722 FILE_PATH_MIME_ENTITY_IDX_IN_MAIL_TBL,
723 MAIL_SIZE_IDX_IN_MAIL_TBL,
724 FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL,
725 FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL,
726 FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL,
727 FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL,
728 FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL,
729 FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL,
730 FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL,
731 DRM_STATUS_IDX_IN_MAIL_TBL,
732 PRIORITY_IDX_IN_MAIL_TBL,
733 SAVE_STATUS_IDX_IN_MAIL_TBL,
734 LOCK_STATUS_IDX_IN_MAIL_TBL,
735 REPORT_STATUS_IDX_IN_MAIL_TBL,
736 ATTACHMENT_COUNT_IDX_IN_MAIL_TBL,
737 INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL,
738 THREAD_ID_IDX_IN_MAIL_TBL,
739 THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL,
740 PREVIEW_TEXT_IDX_IN_MAIL_TBL,
741 MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL,
742 MESSAGE_CLASS_IDX_IN_MAIL_TBL,
743 DIGEST_TYPE_IDX_IN_MAIL_TBL,
744 SMIME_TYPE_IDX_IN_MAIL_TBL,
745 SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL,
746 REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL,
747 TAG_ID_IDX_IN_MAIL_TBL,
748 REPLIED_TIME_IDX_IN_MAIL_TBL,
749 FORWARDED_TIME_IDX_IN_MAIL_TBL,
750 DEFAULT_CHARSET_IDX_IN_MAIL_TBL,
751 EAS_DATA_LENGTH_IDX_IN_MAIL_TBL,
752 EAS_DATA_IDX_IN_MAIL_TBL,
753 USER_NAME_IDX_IN_MAIL_TBL,
754 FIELD_COUNT_OF_MAIL_TBL, /* End of mail_tbl */
758 ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL = 0,
759 ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL,
760 ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL,
761 CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
762 ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL,
763 MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
764 ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
765 MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
766 ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
767 ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
768 ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL,
769 ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
770 ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
771 #ifdef __ATTACHMENT_OPTI__
772 ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL,
773 SECTION_IDX_IN_MAIL_ATTACHMENT_TBL,
778 IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL = 0,
779 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
780 MAIL_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
781 ACCOUNT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
782 ADDRESS_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
783 ADDRESS_IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL,
785 ADDRESS_IDX_IN_MAIL_CONTACT_SYNC_TBL,
786 CONTACT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
787 STORAGE_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
788 CONTACT_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
789 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
790 DISPLAY_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
791 FLAG1_IDX_IN_MAIL_CONTACT_SYNC_TBL,
795 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
797 ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL = 0,
798 STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
799 ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
800 MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
801 SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
802 MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
803 MULTI_USER_NAME_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
807 /* sowmya.kr 03032010, changes for get list of mails for given addr list */
808 typedef struct _em_mail_id_list {
810 struct _em_mail_id_list *next;
813 static char *g_test_query[] = {
814 /* 1. select mail_account_tbl */
817 " incoming_server_type, "
818 " incoming_server_address, "
819 " user_email_address, "
820 " incoming_server_user_name, "
821 " incoming_server_password, "
823 " incoming_server_port_number, "
824 " incoming_server_secure_connection, "
825 " incoming_server_authentication_method,"
826 " outgoing_server_type, "
827 " outgoing_server_address, "
828 " outgoing_server_port_number, "
829 " outgoing_server_need_authentication, "
830 " outgoing_server_secure_connection, "
831 " outgoing_server_user_name, "
832 " outgoing_server_password, "
837 " keep_mails_on_pop_server_after_download, "
838 " auto_resend_times, "
839 " outgoing_server_size_limit, "
840 " wifi_auto_download, "
842 " incoming_server_requires_apop,"
844 " is_preset_account, "
848 " req_delivery_receipt, "
849 " req_read_receipt, "
853 " display_name_from, "
855 " forward_with_files, "
859 ", add_my_address_to_bcc"
860 ", notification_status "
862 ", display_content_status "
863 ", default_ringtone_status "
864 ", alert_ringtone_path "
873 " FROM mail_account_tbl",
874 /* 2. select mail_box_tbl */
884 " total_mail_count_on_server, "
885 " has_archived_mails, "
889 " FROM mail_box_tbl ",
890 /* 3. select mail_read_mail_uid_tbl */
899 " flags_seen_field, "
901 " FROM mail_read_mail_uid_tbl ",
902 /* 4. select mail_rule_tbl */
911 " target_mailbox_id, "
914 " FROM mail_rule_tbl ",
915 /* 5. select mail_tbl */
924 " server_mail_status, "
925 " server_mailbox_name, "
928 " reference_mail_id, "
929 " full_address_from, "
930 " full_address_reply, "
933 " full_address_bcc, "
934 " full_address_return, "
935 " email_address_sender, "
936 " email_address_recipient, "
939 " body_download_status, "
942 " file_path_mime_entity, "
944 " flags_seen_field ,"
945 " flags_deleted_field ,"
946 " flags_flagged_field ,"
947 " flags_answered_field ,"
948 " flags_recent_field ,"
949 " flags_draft_field ,"
950 " flags_forwarded_field,"
956 " attachment_count, "
957 " inline_content_count, "
959 " thread_item_count, "
961 " meeting_request_status, "
966 /* 6. select mail_attachment_tbl */
975 " attachment_save_status, "
976 " attachment_drm_type, "
977 " attachment_drm_method, "
978 " attachment_inline_content_status, "
979 " attachment_mime_type "
980 " FROM mail_attachment_tbl ",
982 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
992 " FROM mail_partial_body_activity_tbl ",
999 " meeting_response, "
1003 " global_object_id, "
1006 " standard_time_start_date, "
1009 " daylight_time_start_date, "
1011 " FROM mail_meeting_tbl ",
1013 #ifdef __FEATURE_LOCAL_ACTIVITY__
1022 " FROM mail_local_activity_tbl ",
1029 " expiration_year, "
1030 " expiration_month, "
1032 " issue_organization_name, "
1037 " FROM mail_certificate_tbl ",
1043 " task_parameter_length, "
1044 " task_parameter , "
1046 " FROM mail_task_tbl ",
1047 #ifdef __FEATURE_BODY_SEARCH__
1053 " FROM mail_text_tbl ",
1055 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
1063 " multi_user_name, "
1064 " FROM mail_auto_download_activity_tbl ",
1069 int _field_count_of_table[CREATE_TABLE_MAX] = { 0, };
1071 static int _get_table_field_data_char(char **table, char *buf, int index)
1073 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1074 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1078 if (table[index] != NULL) {
1079 *buf = (char)atoi(table[index]);
1083 /* EM_DEBUG_LOG("Empty field. Set as zero"); */
1089 static int _get_table_field_data_int(char **table, int *buf, int index)
1091 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1092 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1096 if (table[index] != NULL) {
1097 *buf = atoi(table[index]);
1101 /* EM_DEBUG_LOG("Empty field. Set as zero"); */
1107 static int _get_table_field_data_time_t(char **table, time_t *buf, int index)
1109 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1110 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1114 if (table[index] != NULL) {
1115 *buf = (time_t)atol(table[index]);
1119 /* EM_DEBUG_LOG("Empty field. Set as zero"); */
1125 static int _get_table_field_data_string(char **table, char **buf, int ucs2, int index)
1129 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1130 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1134 char *pTemp = table[index];
1139 sLen = EM_SAFE_STRLEN(pTemp);
1141 *buf = (char *) em_malloc(sLen + 1);
1143 EM_DEBUG_EXCEPTION("malloc is failed");
1146 strncpy(*buf, pTemp, sLen+1);
1147 (*buf)[sLen] = '\0';
1151 #ifdef _PRINT_STORAGE_LOG_
1153 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", *buf, index);
1155 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1163 static int _get_table_field_data_string_without_allocation(char **table, char *buf, int buffer_size, int ucs2, int index)
1165 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1166 EM_DEBUG_EXCEPTION(" table[%p], buf[%p], index[%d]", table, buf, index);
1170 char *pTemp = table[index];
1175 memset(buf, 0, buffer_size);
1176 strncpy(buf, pTemp, buffer_size - 1);
1178 #ifdef _PRINT_STORAGE_LOG_
1180 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", buf, index);
1182 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1188 static int _get_table_field_data_blob(char **table, void **buffer, int buffer_size, int index)
1190 if ((table == NULL) || (buffer == NULL) || (index < 0)) {
1191 EM_DEBUG_EXCEPTION(" table[%p], buffer[%p], buffer_size [%d], index[%d]", table, buffer, buffer_size, index);
1195 char *temp_buffer = table[index];
1197 if (temp_buffer == NULL)
1200 *buffer = malloc(buffer_size);
1201 if (*buffer == NULL) {
1202 EM_DEBUG_EXCEPTION("allocation failed.");
1205 memset(*buffer, 0, buffer_size);
1206 memcpy(*buffer, temp_buffer, buffer_size);
1208 #ifdef _PRINT_STORAGE_LOG_
1210 EM_DEBUG_LOG("_get_table_field_data_string - buffer[%s], index[%d]", buffer, index);
1212 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1218 static int _get_stmt_field_data_char(DB_STMT hStmt, char *buf, int index)
1220 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1221 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1225 if (sqlite3_column_text(hStmt, index) != NULL) {
1226 *buf = (char)sqlite3_column_int(hStmt, index);
1227 #ifdef _PRINT_STORAGE_LOG_
1228 EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1233 EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1238 static int _get_stmt_field_data_int(DB_STMT hStmt, int *buf, int index)
1240 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1241 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1245 if (sqlite3_column_text(hStmt, index) != NULL) {
1246 *buf = sqlite3_column_int(hStmt, index);
1247 #ifdef _PRINT_STORAGE_LOG_
1248 EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1253 EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1258 static int _get_stmt_field_data_time_t(DB_STMT hStmt, time_t *buf, int index)
1260 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1261 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1265 if (sqlite3_column_text(hStmt, index) != NULL) {
1266 *buf = (time_t)sqlite3_column_int(hStmt, index);
1267 #ifdef _PRINT_STORAGE_LOG_
1268 EM_DEBUG_LOG("_get_stmt_field_data_time_t [%d]", *buf);
1273 EM_DEBUG_LOG("_get_stmt_field_data_time_t fail. index [%d]", index);
1277 static int _get_stmt_field_data_string(DB_STMT hStmt, char **buf, int ucs2, int index)
1279 if (!hStmt || !buf || (index < 0)) { /*prevent 39619*/
1280 EM_DEBUG_EXCEPTION("No hStmt, buf[%p], index[%d]", buf, index);
1285 sLen = sqlite3_column_bytes(hStmt, index);
1287 #ifdef _PRINT_STORAGE_LOG_
1288 EM_DEBUG_LOG("_get_stmt_field_data_string sqlite3_column_bytes sLen[%d]", sLen);
1292 *buf = (char *) em_malloc(sLen + 1);
1294 EM_DEBUG_EXCEPTION("em_mallocfailed");
1298 strncpy(*buf, (char *)sqlite3_column_text(hStmt, index), sLen);
1302 #ifdef _PRINT_STORAGE_LOG_
1304 EM_DEBUG_LOG("buf[%s], index[%d]", *buf, index);
1306 EM_DEBUG_LOG("_get_stmt_field_data_string - No string got");
1312 static void _get_stmt_field_data_blob(DB_STMT hStmt, void **buf, int index)
1314 if (!hStmt || !buf || (index < 0)) { /*prevent 39618*/
1315 EM_DEBUG_EXCEPTION("No hStmt, buf[%p], index[%d]", buf, index);
1320 sLen = sqlite3_column_bytes(hStmt, index);
1322 #ifdef _PRINT_STORAGE_LOG_
1323 EM_DEBUG_LOG("_get_stmt_field_data_blob sqlite3_column_bytes sLen[%d]", sLen);
1327 *buf = (char *) em_malloc(sLen);
1329 EM_DEBUG_EXCEPTION("em_mallocfailed");
1333 memcpy(*buf, (void *)sqlite3_column_blob(hStmt, index), sLen);
1339 static int _bind_stmt_field_data_char(DB_STMT hStmt, int index, char value)
1341 if ((hStmt == NULL) || (index < 0)) {
1342 EM_DEBUG_EXCEPTION("index[%d]", index);
1346 int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1348 if (ret != SQLITE_OK) {
1349 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1356 static int _bind_stmt_field_data_int(DB_STMT hStmt, int index, int value)
1358 if ((hStmt == NULL) || (index < 0)) {
1359 EM_DEBUG_EXCEPTION("index[%d]", index);
1363 int ret = sqlite3_bind_int(hStmt, index+1, value);
1365 if (ret != SQLITE_OK) {
1366 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1373 static int _bind_stmt_field_data_time_t(DB_STMT hStmt, int index, time_t value)
1375 if ((hStmt == NULL) || (index < 0)) {
1376 EM_DEBUG_EXCEPTION("index[%d]", index);
1380 int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1382 if (ret != SQLITE_OK) {
1383 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1390 static int _bind_stmt_field_data_string(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1392 if ((hStmt == NULL) || (index < 0)) {
1393 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1397 #ifdef _PRINT_STORAGE_LOG_
1398 EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1403 ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1405 ret = sqlite3_bind_text(hStmt, index+1, "", -1, SQLITE_STATIC);
1407 if (ret != SQLITE_OK) {
1408 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1414 static int _bind_stmt_field_data_nstring(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1416 if ((hStmt == NULL) || (index < 0)) {
1417 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1421 #ifdef _PRINT_STORAGE_LOG_
1422 EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1426 if (value != NULL) {
1427 if (strlen(value) <= max_len)
1428 ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1430 char *buf = (char*)em_malloc(sizeof(char) * (max_len));
1432 EM_DEBUG_EXCEPTION("em_mallocfailed");
1435 snprintf(buf, max_len-1, "%s", value);
1436 ret = sqlite3_bind_text(hStmt, index+1, buf, -1, SQLITE_TRANSIENT);
1440 ret = sqlite3_bind_text(hStmt, index+1, "", -1, SQLITE_STATIC);
1442 if (ret != SQLITE_OK) {
1443 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1450 static int _bind_stmt_field_data_blob(DB_STMT hStmt, int index, void *blob, int blob_size)
1452 if ((hStmt == NULL) || (index < 0)) {
1453 EM_DEBUG_EXCEPTION("index[%d], blob_size[%d]", index, blob_size);
1457 #ifdef _PRINT_STORAGE_LOG_
1458 EM_DEBUG_LOG("hStmt = %p, index = %d, blob_size = %d, blob = [%p]", hStmt, index, blob_size, blob);
1463 ret = sqlite3_bind_blob(hStmt, index+1, blob, blob_size, SQLITE_STATIC);
1465 ret = sqlite3_bind_null(hStmt, index+1);
1467 if (ret != SQLITE_OK) {
1468 EM_DEBUG_EXCEPTION("sqlite3_bind_blob fail [%d]", ret);
1474 static int emstorage_exec_query_by_prepare_v2_stmt(sqlite3 *local_db_handle, char *query_string, char * target_mailbox_name, int input_mailbox_id, int target_account_id)
1477 EM_DEBUG_FUNC_BEGIN("Prepared Statement: local_db_handle[%p] query_string[%p]", local_db_handle, query_string);
1478 int error = EMAIL_ERROR_NONE;
1479 int rc = 0, count = 0;
1480 DB_STMT db_statement = NULL;
1482 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, query_string, EM_SAFE_STRLEN(query_string), &db_statement, NULL), rc);
1484 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
1485 ("sqlite3_prepare_stmt failed [%d] [%s]", rc, query_string));
1487 _bind_stmt_field_data_string(db_statement, count++, target_mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
1488 _bind_stmt_field_data_int(db_statement, count++, input_mailbox_id);
1489 _bind_stmt_field_data_int(db_statement, count++, target_account_id);
1493 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(db_statement), rc);
1494 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
1495 ("sqlite3_step_stmt failed [%d] [%s]", rc, query_string));
1499 if (db_statement != NULL) {
1500 rc = sqlite3_finalize(db_statement);
1501 if (rc != SQLITE_OK) {
1502 EM_DEBUG_EXCEPTION("sqlite3_finalize_stmt error [%d]", rc);
1503 error = EMAIL_ERROR_DB_FAILURE;
1512 static int _delete_temp_file(const char *path)
1514 EM_DEBUG_FUNC_BEGIN("path[%p]", path);
1517 struct dirent *entry = NULL;
1519 char buf[1024] = {0x00, };
1521 if ((dp = opendir(path)) == NULL) {
1522 EM_DEBUG_EXCEPTION("opendir(\"%s\") failed...", path);
1526 while ((entry = readdir(dp)) != NULL) {
1527 SNPRINTF(buf, sizeof(buf), "%s/%s", path, entry->d_name);
1528 if (remove(buf) != 0) {
1529 EM_DEBUG_EXCEPTION("File Deletion Failed`");
1534 EM_DEBUG_FUNC_END();
1538 char *cpy_str(char *src)
1543 if (!(p = em_malloc((int)EM_SAFE_STRLEN(src) + 1))) {
1544 EM_DEBUG_EXCEPTION("mailoc failed...");
1547 strncpy(p, src, EM_SAFE_STRLEN(src)+1);
1553 static void _emstorage_close_once(void)
1555 EM_DEBUG_FUNC_BEGIN();
1557 EM_DEBUG_FUNC_END();
1560 INTERNAL_FUNC int emstorage_close(int *err_code)
1562 EM_DEBUG_FUNC_BEGIN();
1565 int error = EMAIL_ERROR_NONE;
1567 if (!emstorage_db_close(NULL, &error))
1569 if (--_open_counter == 0)
1570 _emstorage_close_once();
1574 if (err_code != NULL)
1577 EM_DEBUG_FUNC_END("ret [%d]", ret);
1581 static void *_emstorage_open_once(char *multi_user_name, int *err_code)
1583 EM_DEBUG_FUNC_BEGIN();
1585 int error = EMAIL_ERROR_NONE;
1587 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1588 char buf[MAX_PATH] = {0};
1589 char *prefix_path = NULL;
1591 error = emcore_get_container_path(multi_user_name, &prefix_path);
1592 if (error != EMAIL_ERROR_NONE) {
1593 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
1597 memset(buf, 0x00, sizeof(buf));
1598 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, EMAIL_PATH);
1599 mkdir(buf, DIRECTORY_PERMISSION);
1601 memset(buf, 0x00, sizeof(buf));
1602 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAIL_HOME);
1603 mkdir(buf, DIRECTORY_PERMISSION);
1605 memset(buf, 0x00, sizeof(buf));
1606 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAIL_TEMP);
1607 mkdir(buf, DIRECTORY_PERMISSION);
1609 _delete_temp_file(buf);
1610 EM_SAFE_FREE(prefix_path);
1612 if (mkdir(DATA_PATH, DIRECTORY_PERMISSION) != 0)
1613 EM_DEBUG_EXCEPTION("Fail to create the directory \n");
1614 if (mkdir(EMAIL_PATH, DIRECTORY_PERMISSION) != 0)
1615 EM_DEBUG_EXCEPTION("Fail to create the directory \n");
1616 if (mkdir(MAIL_HOME, DIRECTORY_PERMISSION) != 0)
1617 EM_DEBUG_EXCEPTION("Fail to create the directory \n");
1618 if (mkdir(MAIL_TEMP, DIRECTORY_PERMISSION) != 0)
1619 EM_DEBUG_EXCEPTION("Fail to create the directory \n");
1621 _delete_temp_file(MAIL_TEMP);
1624 if (!emstorage_create_table(multi_user_name, EMAIL_CREATE_DB_NORMAL, &error)) {
1625 EM_DEBUG_EXCEPTION(" emstorage_create_table failed - %d", error);
1632 if (err_code != NULL)
1638 /* pData : a parameter which is registered when busy handler is registerd */
1639 /* count : retry count */
1640 static int _callback_sqlite_busy_handler(void *pData, int count)
1642 if (10 - count > 0) {
1643 struct timespec time = {
1645 .tv_nsec = (count + 1) * 100 * 1000 * 1000
1647 EM_DEBUG_LOG("Busy handler called!!: PID[%d] / CNT [%d]", getpid(), count);
1648 nanosleep(&time, NULL);
1651 EM_DEBUG_EXCEPTION("Busy handler will be returned SQLITE_BUSY error PID[%d] / CNT[%d]", getpid(), count);
1656 static int _callback_collation_utf7_sort(void *data, int length_text_a, const void *text_a,
1657 int length_text_b, const void *text_b)
1659 EM_DEBUG_FUNC_BEGIN();
1661 char *converted_string_a = NULL;
1662 char *converted_string_b = NULL;
1664 EM_DEBUG_LOG_DEV("text_a : [%s]", text_a);
1665 converted_string_a = emcore_convert_mutf7_to_utf8((char *)text_a);
1666 EM_DEBUG_LOG_DEV("Converted text_a : [%s]", converted_string_a);
1668 EM_DEBUG_LOG_DEV("text_b : [%s]", text_b);
1669 converted_string_b = emcore_convert_mutf7_to_utf8((char *)text_b);
1670 EM_DEBUG_LOG_DEV("Converted text_b : [%s]", converted_string_b);
1672 if (converted_string_a && converted_string_b)
1673 result = strcmp(converted_string_a, converted_string_b);
1675 EM_SAFE_FREE(converted_string_a);
1676 EM_SAFE_FREE(converted_string_b);
1678 EM_DEBUG_FUNC_END();
1682 static int _delete_all_files_and_directories(char *db_file_path, int *err_code)
1684 EM_DEBUG_FUNC_BEGIN();
1686 int error = EMAIL_ERROR_NONE;
1689 if (!emstorage_delete_file(db_file_path, &error)) {
1690 if (error != EMAIL_ERROR_FILE_NOT_FOUND) {
1691 EM_DEBUG_EXCEPTION_SEC("remove failed - %s", EMAIL_SERVICE_DB_FILE_PATH);
1696 if (!emstorage_delete_dir((char *)MAIL_HOME, &error)) {
1697 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed");
1706 EM_DEBUG_FUNC_END();
1710 static int _recovery_from_malformed_db_file(char *db_file_path, int *err_code)
1712 EM_DEBUG_FUNC_BEGIN();
1714 int error = EMAIL_ERROR_NONE;
1717 /* Delete all files and directories */
1718 if (!_delete_all_files_and_directories(db_file_path, &error)) {
1719 EM_DEBUG_EXCEPTION("_delete_all_files_and_directories failed [%d]", error);
1723 /* Delete all accounts on MyAccount */
1725 /* Delete all managed connection to DB */
1726 emstorage_reset_db_handle_list();
1733 EM_DEBUG_FUNC_END();
1737 int _xsystem(const char *argv[])
1745 perror("fork failed");
1748 if (execvp(argv[0], (char *const *)argv) == -1) {
1749 perror("execute init db script");
1758 if (waitpid(pid, &status, 0) == -1) {
1759 perror("waitpid failed");
1763 if (WIFSIGNALED(status)) {
1768 if (!WIFEXITED(status)) {
1769 perror("should not happen");
1773 return WEXITSTATUS(status);
1776 #define SCRIPT_INIT_DB "/usr/bin/email-service_init_db.sh"
1778 INTERNAL_FUNC int emstorage_init_db(char *multi_user_name)
1780 EM_DEBUG_FUNC_BEGIN();
1781 int err = EMAIL_ERROR_NONE;
1782 char *prefix_path = NULL;
1783 char *output_file_path = NULL;
1784 char temp_file_path[MAX_PATH] = {0};
1785 gid_t email_gid = -1;
1786 uid_t email_uid = -1;
1788 struct group buf_group;
1790 struct passwd buf_passwd;
1791 char buf_gr[MAX_GRP_BUF_SIZE];
1792 char buf_pw[MAX_GRP_BUF_SIZE];
1795 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1796 err = emcore_get_container_path(multi_user_name, &prefix_path);
1797 if (err != EMAIL_ERROR_NONE) {
1798 if (err != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1799 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", err);
1804 prefix_path = strdup("");
1807 if (err == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1808 err = emcore_get_canonicalize_path((char *)EMAIL_SERVICE_DB_FILE_PATH, &output_file_path);
1809 if (err != EMAIL_ERROR_NONE) {
1810 EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", err);
1814 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
1816 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
1819 EM_DEBUG_LOG("db file path : [%s]", temp_file_path);
1821 if (!g_file_test(temp_file_path, G_FILE_TEST_EXISTS)) {
1823 const char *argv_script[] = {"/bin/sh", SCRIPT_INIT_DB, NULL};
1824 ret = _xsystem(argv_script);
1827 EM_DEBUG_EXCEPTION("_xsystem failed");
1828 err = EMAIL_ERROR_SYSTEM_FAILURE;
1831 result = getgrnam_r(GID, &buf_group, buf_gr, sizeof(buf_gr), &gr);
1832 if (result == 0 && NULL != gr)
1833 email_gid = gr->gr_gid;
1835 email_gid = GID_DEFAULT;
1838 result = getpwnam_r(UID, &buf_passwd, buf_pw, sizeof(buf_pw), &pw);
1839 if (result == 0 && NULL != pw)
1840 email_uid = pw->pw_uid;
1842 email_uid = UID_DEFAULT;
1845 if (chmod(EMAIL_SERVICE_DB_FILE_PATH, 0660) != 0)
1846 EM_DEBUG_EXCEPTION("Fail to change the permission \n");
1847 if (chmod(EMAIL_SERVICE_DB_JOURNAL_FILE_PATH, 0660) != 0)
1848 EM_DEBUG_EXCEPTION("Fail to change the permission \n");
1849 if (chmod(EMAIL_SQL_PATH, 0660) != 0)
1850 EM_DEBUG_EXCEPTION("Fail to change the permission \n");
1852 ret = chown(EMAIL_SERVICE_DB_FILE_PATH, email_uid, email_gid);
1854 EM_DEBUG_EXCEPTION("chown() Fail[errno=%d]", errno);
1855 ret = chown(EMAIL_SERVICE_DB_JOURNAL_FILE_PATH, email_uid, email_gid);
1857 EM_DEBUG_EXCEPTION("chown() Fail[errno=%d]", errno);
1858 ret = chown(EMAIL_SQL_PATH, email_uid, email_gid);
1860 EM_DEBUG_EXCEPTION("chown() Fail[errno=%d]", errno);
1866 EM_SAFE_FREE(prefix_path);
1867 EM_SAFE_FREE(output_file_path);
1869 EM_DEBUG_FUNC_END();
1873 INTERNAL_FUNC int em_db_open(char *db_file_path, sqlite3 **sqlite_handle, int *err_code)
1875 EM_DEBUG_FUNC_BEGIN();
1877 int error = EMAIL_ERROR_NONE;
1880 EM_DEBUG_LOG_DEV("*sqlite_handle[%p]", *sqlite_handle);
1882 if (*sqlite_handle) { /*prevent 33351*/
1883 EM_DEBUG_LOG_DEV(">>>>> DB Already Opened......");
1884 if (err_code != NULL)
1889 EM_DEBUG_LOG("DB file path : [%s]", db_file_path);
1892 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1893 if (SQLITE_OK != rc) {
1894 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1895 if (SQLITE_PERM == rc || SQLITE_CANTOPEN == rc)
1896 error = EMAIL_ERROR_PERMISSION_DENIED;
1898 error = EMAIL_ERROR_DB_FAILURE;
1900 sqlite3_close(*sqlite_handle);
1901 *sqlite_handle = NULL;
1903 if (SQLITE_CORRUPT == rc) /* SQLITE_CORRUPT : The database disk image is malformed */ {/* Recovery DB file */
1904 EM_DEBUG_LOG("The database disk image is malformed. Trying to remove and create database disk image and directories");
1905 if (!_recovery_from_malformed_db_file(db_file_path, &error)) {
1906 EM_DEBUG_EXCEPTION("_recovery_from_malformed_db_file failed [%d]", error);
1910 EM_DEBUG_LOG("Open DB again");
1911 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1912 if (SQLITE_OK != rc) {
1913 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1914 if (SQLITE_PERM == rc)
1915 error = EMAIL_ERROR_PERMISSION_DENIED;
1917 error = EMAIL_ERROR_DB_FAILURE;
1919 sqlite3_close(*sqlite_handle);
1920 *sqlite_handle = NULL;
1921 goto FINISH_OFF; /*prevent 33351*/
1927 /* register busy handler */
1928 EM_DEBUG_LOG_DEV(">>>>> Register DB Handle to busy handler: *sqlite_handle[%p]", *sqlite_handle);
1929 rc = sqlite3_busy_handler(*sqlite_handle, _callback_sqlite_busy_handler, NULL); /* Busy Handler registration, NULL is a parameter which will be passed to handler */
1930 if (SQLITE_OK != rc) {
1931 EM_DEBUG_EXCEPTION("sqlite3_busy_handler fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1932 error = EMAIL_ERROR_DB_FAILURE;
1933 sqlite3_close(*sqlite_handle);
1934 *sqlite_handle = NULL;
1938 /* Register collation callback function */
1939 rc = sqlite3_create_collation(*sqlite_handle, "CONVERTUTF8", SQLITE_UTF8, NULL, _callback_collation_utf7_sort);
1940 if (SQLITE_OK != rc) {
1941 EM_DEBUG_EXCEPTION("sqlite3_create_collation failed : [%d][%s]", rc, sqlite3_errmsg(*sqlite_handle));
1942 error = EMAIL_ERROR_DB_FAILURE;
1943 sqlite3_close(*sqlite_handle);
1944 *sqlite_handle = NULL;
1951 if (err_code != NULL)
1954 EM_DEBUG_FUNC_END("ret [%d]", ret);
1958 INTERNAL_FUNC sqlite3* emstorage_db_open(char *multi_user_name, int *err_code)
1960 EM_DEBUG_FUNC_BEGIN();
1962 sqlite3 *_db_handle = NULL;
1964 int error = EMAIL_ERROR_NONE;
1965 char *prefix_path = NULL;
1967 _db_handle = emstorage_get_db_handle(multi_user_name);
1969 if (_db_handle == NULL) {
1970 char *output_file_path = NULL;
1971 char temp_file_path[MAX_PATH] = {0};
1973 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1974 error = emcore_get_container_path(multi_user_name, &prefix_path);
1975 if (error != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION && error != EMAIL_ERROR_NONE) {
1976 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", error);
1980 prefix_path = strdup("");
1983 if (error == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1984 if ((error = emcore_get_canonicalize_path((char *)EMAIL_SERVICE_DB_FILE_PATH, &output_file_path)) != EMAIL_ERROR_NONE) {
1985 EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", error);
1989 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
1990 EM_SAFE_FREE(output_file_path);
1992 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
1995 if (!em_db_open(temp_file_path, &_db_handle, &error)) {
1996 EM_DEBUG_EXCEPTION("em_db_open failed[%d]", error);
2000 _initialize_shm_mutex(SHM_FILE_FOR_DB_LOCK, &shm_fd_for_db_lock, &mapped_for_db_lock);
2002 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
2003 _initialize_shm_mutex(SHM_FILE_FOR_MAIL_ID_LOCK, &shm_fd_for_generating_mail_id, &mapped_for_generating_mail_id);
2004 #endif /*__FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
2006 emstorage_set_db_handle(multi_user_name, _db_handle);
2008 emstorage_initialize_field_count();
2013 EM_SAFE_FREE(prefix_path);
2015 if (err_code != NULL)
2018 EM_DEBUG_FUNC_END("ret [%p]", _db_handle);
2022 INTERNAL_FUNC int emstorage_db_close(char *multi_user_name, int *err_code)
2024 EM_DEBUG_FUNC_BEGIN();
2025 #ifdef _MULTIPLE_DB_HANDLE
2026 sqlite3 *_db_handle = emstorage_get_db_handle(multi_user_name);
2029 int error = EMAIL_ERROR_NONE;
2033 ret = sqlite3_close(_db_handle);
2034 if (ret != SQLITE_OK) {
2035 EM_DEBUG_EXCEPTION(" sqlite3_close fail - %d", ret);
2036 error = EMAIL_ERROR_DB_FAILURE;
2040 #ifdef _MULTIPLE_DB_HANDLE
2041 emstorage_remove_db_handle();
2049 if (err_code != NULL)
2052 EM_DEBUG_FUNC_END("ret [%d]", ret);
2056 INTERNAL_FUNC int emstorage_open(char *multi_user_name, int *err_code)
2058 EM_DEBUG_FUNC_BEGIN();
2061 int error = EMAIL_ERROR_NONE;
2063 char *prefix_path = NULL;
2064 char buf[MAX_PATH] = {0};
2066 if (EM_SAFE_STRLEN(multi_user_name) <= 0) {
2067 SNPRINTF(buf, sizeof(buf), "%s", DB_PATH);
2069 error = emcore_get_container_path(multi_user_name, &prefix_path);
2070 if (error != EMAIL_ERROR_NONE) {
2071 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
2074 SNPRINTF(buf, sizeof(buf), "%s/%s", prefix_path, DB_PATH);
2077 if (!g_file_test(buf, G_FILE_TEST_EXISTS)) {
2078 retValue = mkdir(buf, DIRECTORY_PERMISSION);
2080 EM_DEBUG_LOG("mkdir return- %d", retValue);
2081 EM_DEBUG_LOG("emstorage_open - before sqlite3_open - pid = %d", getpid());
2084 if (emstorage_db_open(multi_user_name, &error) == NULL) {
2085 EM_DEBUG_EXCEPTION("emstorage_db_open failed[%d]", error);
2089 if (_open_counter++ == 0)
2090 _emstorage_open_once(multi_user_name, &error);
2096 EM_SAFE_FREE(prefix_path);
2098 if (err_code != NULL)
2101 EM_DEBUG_FUNC_END("ret [%d]", ret);
2105 static int emstorage_get_field_count_from_create_table_query(char *input_create_table_query, int *output_field_count)
2107 EM_DEBUG_FUNC_BEGIN("input_create_table_query[%d], output_field_count[%p]", input_create_table_query, output_field_count);
2108 int err = EMAIL_ERROR_NONE;
2109 int field_count = 0;
2112 if (input_create_table_query == NULL || output_field_count == NULL) {
2113 err = EMAIL_ERROR_INVALID_PARAM;
2117 pos = input_create_table_query;
2121 if (pos == NULL || *pos == (char)0)
2124 pos = strchr(pos, ',');
2127 *output_field_count = field_count;
2129 EM_DEBUG_LOG_DEV("field_count [%d]", field_count);
2133 EM_DEBUG_FUNC_END("err [%d]", err);
2137 INTERNAL_FUNC int emstorage_initialize_field_count()
2139 EM_DEBUG_FUNC_BEGIN();
2140 int err = EMAIL_ERROR_NONE;
2142 char **create_table_query = NULL;
2144 if (_field_count_of_table[CREATE_TABLE_MAIL_ACCOUNT_TBL] != 0) {
2145 err = EMAIL_ERROR_ALREADY_INITIALIZED;
2149 err = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2150 if (err != EMAIL_ERROR_NONE) {
2151 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", err);
2155 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]));
2156 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]));
2157 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]));
2158 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]));
2159 emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_TBL]));
2160 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]));
2161 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]));
2162 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]));
2163 #ifdef __FEATURE_LOCAL_ACTIVITY__
2164 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]));
2165 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2166 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]));
2167 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]));
2168 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]));
2171 if (create_table_query) {
2173 for (i = 0; i < query_len; i++) {
2174 if (create_table_query[i])
2175 EM_SAFE_FREE(create_table_query[i]);
2177 EM_SAFE_FREE(create_table_query);
2180 EM_DEBUG_FUNC_END("err [%d]", err);
2184 INTERNAL_FUNC int emstorage_create_table(char *multi_user_name, emstorage_create_db_type_t type, int *err_code)
2186 EM_DEBUG_FUNC_BEGIN();
2188 int error = EMAIL_ERROR_NONE;
2189 int rc = -1, ret = false;
2191 char sql_query_string[QUERY_SIZE] = {0, };
2192 char **create_table_query = NULL;
2194 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2196 error = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2197 if (error != EMAIL_ERROR_NONE) {
2198 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
2202 if (query_len < CREATE_TABLE_MAX) {
2203 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
2204 error = EMAIL_ERROR_SYSTEM_FAILURE;
2208 EM_DEBUG_LOG("local_db_handle = %p.", local_db_handle);
2211 char **result = NULL;
2213 /* 1. create mail_account_tbl */
2214 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_account_tbl';";
2215 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2216 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2217 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2219 EM_DEBUG_LOG("emstorage_create_table - result[1] = %s %s", result[1], result[1]);
2221 if (atoi(result[1]) < 1) {
2222 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2223 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2224 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2226 EM_DEBUG_LOG("CREATE TABLE mail_account_tbl");
2227 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2228 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2229 if (error != EMAIL_ERROR_NONE) {
2230 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2234 /* create mail_account_tbl unique index */
2235 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ACCOUNT_IDX]);
2236 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2237 if (error != EMAIL_ERROR_NONE) {
2238 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2242 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2243 } /* mail_account_tbl */
2244 else if (type == EMAIL_CREATE_DB_CHECK) {
2245 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], NULL, NULL, NULL), rc);
2246 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)));
2249 sqlite3_free_table(result);
2252 /* 2. create mail_box_tbl */
2253 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_box_tbl';";
2255 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2256 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2257 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2259 if (atoi(result[1]) < 1) {
2260 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2261 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2262 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2264 EM_DEBUG_LOG("CREATE TABLE mail_box_tbl");
2266 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_BOX_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2267 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2268 if (error != EMAIL_ERROR_NONE) {
2269 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2273 /* create mail_local_mailbox_tbl unique index */
2274 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_BOX_IDX]);
2275 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2276 if (error != EMAIL_ERROR_NONE) {
2277 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2281 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2282 } /* mail_box_tbl */
2283 else if (type == EMAIL_CREATE_DB_CHECK) {
2284 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_BOX_TBL], NULL, NULL, NULL);
2285 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2286 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_BOX_TBL], rc, sqlite3_errmsg(local_db_handle)));
2288 sqlite3_free_table(result);
2291 /* 3. create mail_read_mail_uid_tbl */
2292 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_read_mail_uid_tbl';";
2293 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2294 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2295 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2298 if (atoi(result[1]) < 1) {
2299 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2300 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2301 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2303 EM_DEBUG_LOG("CREATE TABLE mail_read_mail_uid_tbl");
2305 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2306 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2307 if (error != EMAIL_ERROR_NONE) {
2308 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2312 /* create mail_read_mail_uid_tbl unique index */
2313 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_IDX]);
2314 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2315 if (error != EMAIL_ERROR_NONE) {
2316 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2320 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2321 } /* mail_read_mail_uid_tbl */
2322 else if (type == EMAIL_CREATE_DB_CHECK) {
2323 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], NULL, NULL, NULL), rc);
2324 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2325 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], rc, sqlite3_errmsg(local_db_handle)));
2327 sqlite3_free_table(result);
2330 /* 4. create mail_rule_tbl */
2331 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_rule_tbl';";
2333 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2334 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2335 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2337 if (atoi(result[1]) < 1) {
2338 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2339 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2340 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2342 EM_DEBUG_LOG("CREATE TABLE mail_rule_tbl");
2344 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_RULE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2345 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2346 if (error != EMAIL_ERROR_NONE) {
2347 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2351 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2352 } /* mail_rule_tbl */
2353 else if (type == EMAIL_CREATE_DB_CHECK) {
2354 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_RULE_TBL], NULL, NULL, NULL), rc);
2355 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2356 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_RULE_TBL], rc, sqlite3_errmsg(local_db_handle)));
2358 sqlite3_free_table(result);
2361 /* 5. create mail_tbl */
2362 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_tbl';";
2363 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2364 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2365 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2367 if (atoi(result[1]) < 1) {
2368 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2369 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2370 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2371 EM_DEBUG_LOG("CREATE TABLE mail_tbl");
2373 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2374 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2375 if (error != EMAIL_ERROR_NONE) {
2376 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2380 /* create mail_tbl unique index */
2381 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_IDX]);
2382 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2383 if (error != EMAIL_ERROR_NONE) {
2384 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2388 /* create mail_tbl index for date_time */
2389 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_DATETIME_IDX]);
2390 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2391 if (error != EMAIL_ERROR_NONE) {
2392 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2396 /* create mail_tbl index for thread_item_count */
2397 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_THREAD_IDX]);
2398 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2399 if (error != EMAIL_ERROR_NONE) {
2400 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2404 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2405 /* just one time call */
2406 /* EFTSInitFTSIndex(FTS_EMAIL_IDX); */
2408 else if (type == EMAIL_CREATE_DB_CHECK) {
2409 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TBL], NULL, NULL, NULL), rc);
2410 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2411 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TBL], rc, sqlite3_errmsg(local_db_handle)));
2413 sqlite3_free_table(result);
2416 /* 6. create mail_attachment_tbl */
2417 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_attachment_tbl';";
2418 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2419 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2420 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2422 if (atoi(result[1]) < 1) {
2423 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2424 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2425 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2427 EM_DEBUG_LOG("CREATE TABLE mail_attachment_tbl");
2429 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2430 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2431 if (error != EMAIL_ERROR_NONE) {
2432 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2436 /* create mail_attachment_tbl unique index */
2437 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_IDX]);
2438 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2439 if (error != EMAIL_ERROR_NONE) {
2440 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2444 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2445 } /* mail_attachment_tbl */
2446 else if (type == EMAIL_CREATE_DB_CHECK) {
2447 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], NULL, NULL, NULL), rc);
2448 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2449 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], rc, sqlite3_errmsg(local_db_handle)));
2451 sqlite3_free_table(result);
2454 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
2456 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_partial_body_activity_tbl';";
2457 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2458 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2459 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2461 if (atoi(result[1]) < 1) {
2463 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2464 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2465 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2467 EM_DEBUG_LOG("CREATE TABLE mail_partial_body_activity_tbl");
2469 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2470 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2471 if (error != EMAIL_ERROR_NONE) {
2472 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2476 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2477 } /* mail_rule_tbl */
2478 else if (type == EMAIL_CREATE_DB_CHECK) {
2479 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2480 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2481 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2483 sqlite3_free_table(result);
2486 #endif /* __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
2488 /* create mail_meeting_tbl */
2489 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_meeting_tbl';";
2490 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2491 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2492 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2494 if (atoi(result[1]) < 1) {
2495 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2496 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2497 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2499 EM_DEBUG_LOG("CREATE TABLE mail_meeting_tbl");
2501 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_MEETING_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2502 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2503 if (error != EMAIL_ERROR_NONE) {
2504 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2508 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_MEETING_IDX]);
2509 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2510 if (error != EMAIL_ERROR_NONE) {
2511 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2515 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2516 } /* mail_contact_sync_tbl */
2517 else if (type == EMAIL_CREATE_DB_CHECK) {
2518 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], NULL, NULL, NULL), rc);
2519 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2520 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], rc, sqlite3_errmsg(local_db_handle)));
2522 sqlite3_free_table(result);
2525 #ifdef __FEATURE_LOCAL_ACTIVITY__
2527 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_local_activity_tbl';";
2528 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2529 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2530 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2532 if (atoi(result[1]) < 1) {
2534 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2535 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2536 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2538 EM_DEBUG_LOG(" CREATE TABLE mail_local_activity_tbl");
2540 SNPRINTF(sql_query_string, sizeof(sql_query_string), create_table_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL]);
2541 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2542 if (error != EMAIL_ERROR_NONE) {
2543 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2547 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2548 } /* mail_rule_tbl */
2549 else if (type == EMAIL_CREATE_DB_CHECK) {
2550 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL);
2551 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2552 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2553 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2555 sqlite3_free_table(result);
2557 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2558 /* create mail_certificate_tbl */
2559 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_certificate_tbl';";
2560 /* rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
2561 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2562 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2564 if (atoi(result[1]) < 1) {
2565 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2566 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2568 EM_DEBUG_LOG("CREATE TABLE mail_certificate_tbl");
2570 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2571 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2572 if (error != EMAIL_ERROR_NONE) {
2573 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2577 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2578 } /* mail_contact_sync_tbl */
2579 else if (type == EMAIL_CREATE_DB_CHECK) {
2580 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], NULL, NULL, NULL), rc);
2581 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], rc, sqlite3_errmsg(local_db_handle)));
2584 sqlite3_free_table(result);
2587 /* create mail_task_tbl */
2588 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_task_tbl';";
2589 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2590 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)));
2592 if (atoi(result[1]) < 1) {
2593 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2594 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2596 EM_DEBUG_LOG("CREATE TABLE mail_task_tbl");
2598 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TASK_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2599 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2600 if (error != EMAIL_ERROR_NONE) {
2601 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2605 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_TASK_IDX]);
2606 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2607 if (error != EMAIL_ERROR_NONE) {
2608 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2612 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2613 } /* mail_task_tbl */
2614 else if (type == EMAIL_CREATE_DB_CHECK) {
2615 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TASK_TBL], NULL, NULL, NULL), rc);
2616 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)));
2619 sqlite3_free_table(result);
2622 #ifdef __FEATURE_BODY_SEARCH__
2623 /* create mail_text_tbl */
2624 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_text_tbl';";
2625 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2626 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)));
2628 if (atoi(result[1]) < 1) {
2629 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2630 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2632 EM_DEBUG_LOG("CREATE TABLE mail_text_tbl");
2634 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TEXT_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2635 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2636 if (error != EMAIL_ERROR_NONE) {
2637 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2641 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2642 } /* mail_text_tbl */
2643 else if (type == EMAIL_CREATE_DB_CHECK) {
2644 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TEXT_TBL], NULL, NULL, NULL), rc);
2645 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)));
2648 sqlite3_free_table(result);
2653 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
2654 /* create mail_auto_download_activity_tbl */
2655 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_auto_download_activity_tbl';";
2656 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2657 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)));
2659 if (atoi(result[1]) < 1) {
2660 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2661 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2663 EM_DEBUG_LOG("CREATE TABLE mail_auto_download_activity_tbl");
2665 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], sizeof(sql_query_string)-1);
2666 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2667 if (error != EMAIL_ERROR_NONE) {
2668 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2672 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2673 } /* mail_auto_download_activity_tbl */
2674 else if (type == EMAIL_CREATE_DB_CHECK) {
2675 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2676 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2679 sqlite3_free_table(result);
2688 if (result) sqlite3_free_table(result);
2691 EMSTORAGE_PROTECTED_FUNC_CALL(
2692 sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2694 EMSTORAGE_PROTECTED_FUNC_CALL(
2695 sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
2699 if (create_table_query) {
2701 for (i = 0; i < query_len; i++) {
2702 if (create_table_query[i])
2703 EM_SAFE_FREE(create_table_query[i]);
2705 EM_SAFE_FREE(create_table_query);
2708 if (err_code != NULL)
2711 EM_DEBUG_FUNC_END("ret [%d]", ret);
2715 /* Query series --------------------------------------------------------------*/
2716 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)
2718 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);
2721 int error = EMAIL_ERROR_NONE;
2722 DB_STMT hStmt = NULL;
2723 char *sql_query_string = NULL;
2725 sqlite3 *local_db_handle = NULL;
2727 if (!input_conditional_clause || (!output_total_mail_count && !output_unseen_mail_count)) {
2728 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2729 return EMAIL_ERROR_INVALID_PARAM;
2732 query_size = EM_SAFE_STRLEN(input_conditional_clause) + QUERY_SIZE;
2733 sql_query_string = em_malloc(query_size);
2734 if (sql_query_string == NULL) {
2735 EM_DEBUG_EXCEPTION("em_mallocfailed");
2736 error = EMAIL_ERROR_OUT_OF_MEMORY;
2740 local_db_handle = emstorage_get_db_connection(multi_user_name);
2742 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2744 SNPRINTF(sql_query_string, query_size, "SELECT COUNT(*) FROM mail_tbl");
2745 EM_SAFE_STRNCAT(sql_query_string, (char*)input_conditional_clause, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
2747 if (output_total_mail_count) {
2748 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
2749 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2750 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
2752 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2753 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2754 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
2755 _get_stmt_field_data_int(hStmt, output_total_mail_count, 0);
2758 if (output_unseen_mail_count) {
2759 EM_SAFE_STRNCAT(sql_query_string, " AND flags_seen_field = 0 ", query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
2761 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
2762 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2763 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2765 *output_unseen_mail_count = atoi(result[1]);
2766 sqlite3_free_table(result);
2771 if (hStmt != NULL) {
2772 rc = sqlite3_finalize(hStmt);
2773 if (rc != SQLITE_OK) {
2774 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
2775 error = EMAIL_ERROR_DB_FAILURE;
2779 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2781 EM_SAFE_FREE(sql_query_string);
2783 EM_DEBUG_FUNC_END("error [%d]", error);
2787 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)
2789 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);
2797 int error = EMAIL_ERROR_NONE;
2798 int *result_mail_id_list = NULL;
2799 char **result = NULL;
2800 char *sql_query_string = NULL;
2801 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2803 EM_IF_NULL_RETURN_VALUE(input_conditional_clause, EMAIL_ERROR_INVALID_PARAM);
2804 EM_IF_NULL_RETURN_VALUE(output_mail_id_list, EMAIL_ERROR_INVALID_PARAM);
2805 EM_IF_NULL_RETURN_VALUE(output_mail_id_count, EMAIL_ERROR_INVALID_PARAM);
2807 query_size = strlen(input_conditional_clause) + strlen("SELECT mail_id FROM mail_tbl ") + 10; // 10 is extra space
2808 sql_query_string = em_malloc(query_size);
2809 if (sql_query_string == NULL) {
2810 EM_DEBUG_EXCEPTION("em_mallocfailed");
2811 error = EMAIL_ERROR_OUT_OF_MEMORY;
2815 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2817 /* Composing query */
2818 SNPRINTF_OFFSET(sql_query_string, cur_query, query_size, "SELECT mail_id FROM mail_tbl ");
2819 EM_SAFE_STRNCAT(sql_query_string, (char*)input_conditional_clause, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
2821 EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
2823 /* Performing query */
2824 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2825 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2826 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2830 /* to get mail list */
2832 EM_DEBUG_LOG("No mail found...");
2833 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2837 EM_DEBUG_LOG("There are [%d] mails.", count);
2839 if (!(result_mail_id_list = (int*)em_malloc(sizeof(int) * count))) {
2840 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
2841 error = EMAIL_ERROR_OUT_OF_MEMORY;
2845 EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2847 for (i = 0; i < count; i++)
2848 _get_table_field_data_int(result, result_mail_id_list + i, col_index++);
2850 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >> ", count);
2852 *output_mail_id_list = result_mail_id_list;
2853 *output_mail_id_count = count;
2858 sqlite3_free_table(result);
2860 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2862 EM_SAFE_FREE(sql_query_string);
2864 if (error != EMAIL_ERROR_NONE)
2865 EM_SAFE_FREE(result_mail_id_list);
2867 EM_DEBUG_FUNC_END("error [%d]", error);
2871 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)
2873 EM_DEBUG_FUNC_BEGIN();
2874 EM_PROFILE_BEGIN(emstorage_query_mail_list_func);
2876 int i = 0, count = 0, rc = -1, to_get_count = (result_mail_list) ? 0 : 1;
2877 int sql_query_string_length = 0;
2878 int local_inline_content_count = 0, local_attachment_count = 0;
2879 int cur_query = 0, base_count = 0, col_index;
2880 int ret = false, error = EMAIL_ERROR_NONE;
2881 char *date_time_string = NULL;
2882 char **result = NULL;
2883 char *field_mail_id = "mail_id";
2884 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 ";
2885 char *select_query_form = "SELECT %s FROM mail_tbl ";
2886 char *target_field = NULL;
2887 char *sql_query_string = NULL;
2888 email_mail_list_item_t *mail_list_item_from_tbl = NULL;
2889 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2891 EM_IF_NULL_RETURN_VALUE(conditional_clause, false);
2892 EM_IF_NULL_RETURN_VALUE(result_count, false);
2894 EMSTORAGE_START_READ_TRANSACTION(transaction);
2897 if (to_get_count) /* count only */
2898 target_field = field_mail_id;
2899 else /* mail list in plain form */
2900 target_field = field_all;
2902 sql_query_string_length = EM_SAFE_STRLEN(select_query_form) + EM_SAFE_STRLEN(target_field) + EM_SAFE_STRLEN(conditional_clause);
2904 if (sql_query_string_length)
2905 sql_query_string = em_malloc(sql_query_string_length);
2907 if (sql_query_string == NULL) {
2908 EM_DEBUG_EXCEPTION("em_mallocfailed...");
2909 error = EMAIL_ERROR_OUT_OF_MEMORY;
2913 cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, sql_query_string_length, select_query_form, target_field);
2915 strncat(sql_query_string, conditional_clause, sql_query_string_length - cur_query);
2917 EM_DEBUG_LOG_DEV("query[%s]", sql_query_string);
2919 /* performing query */
2920 EM_PROFILE_BEGIN(emstorage_query_mail_list_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; goto FINISH_OFF; },
2923 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2924 EM_PROFILE_END(emstorage_query_mail_list_performing_query);
2930 for (tmp = field_all; tmp && *(tmp + 1); tmp = index(tmp + 1, ',')) i++ ;
2934 col_index = base_count;
2936 EM_DEBUG_LOG_DEV("base_count [%d]", base_count);
2941 EM_DEBUG_LOG_DEV("No mail found...");
2943 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2946 EM_DEBUG_LOG_DEV("There are [%d] mails.", count);
2948 /* to get mail list */
2950 EM_DEBUG_LOG_DEV("No mail found...");
2952 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2956 EM_DEBUG_LOG_DEV("There are [%d] mails.", count);
2957 if (!(mail_list_item_from_tbl = (email_mail_list_item_t*)em_malloc(sizeof(email_mail_list_item_t) * count))) {
2958 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
2959 error = EMAIL_ERROR_OUT_OF_MEMORY;
2963 EM_PROFILE_BEGIN(emstorage_query_mail_list_loop);
2964 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >> ");
2965 for (i = 0; i < count; i++) {
2966 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_id), col_index++);
2967 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].account_id), col_index++);
2968 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mailbox_id), col_index++);
2969 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].mailbox_type), col_index++);
2970 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].full_address_from, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2971 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_sender, MAX_EMAIL_ADDRESS_LENGTH, 1, col_index++);
2972 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_recipient, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2973 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].subject, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2974 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].body_download_status), col_index++);
2975 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_size), col_index++);
2976 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_seen_field), col_index++);
2977 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_deleted_field), col_index++);
2978 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_flagged_field), col_index++);
2979 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_answered_field), col_index++);
2980 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_recent_field), col_index++);
2981 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_draft_field), col_index++);
2982 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_forwarded_field), col_index++);
2983 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].DRM_status), col_index++);
2984 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].priority), col_index++);
2985 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].save_status), col_index++);
2986 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].lock_status), col_index++);
2987 _get_table_field_data_int(result, &local_attachment_count, col_index++);
2988 _get_table_field_data_int(result, &local_inline_content_count, col_index++);
2989 _get_table_field_data_time_t(result, &(mail_list_item_from_tbl[i].date_time), col_index++);
2990 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].preview_text, MAX_PREVIEW_TEXT_LENGTH, 1, col_index++);
2991 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_id), col_index++);
2992 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_item_count), col_index++);
2993 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].meeting_request_status), col_index++);
2994 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].message_class), col_index++);
2995 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].smime_type), col_index++);
2996 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].scheduled_sending_time), col_index++);
2997 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].remaining_resend_times), col_index++);
2998 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].tag_id), col_index++);
2999 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].eas_data_length), col_index++);
3000 _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++);
3002 mail_list_item_from_tbl[i].attachment_count = (local_attachment_count > 0) ? 1 : 0;
3004 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >> ", count);
3005 EM_PROFILE_END(emstorage_query_mail_list_loop);
3011 EM_DEBUG_LOG("MAIL_COUNT [%d]", count);
3014 sqlite3_free_table(result);
3017 *result_count = count;
3020 if (result_mail_list)
3021 *result_mail_list = mail_list_item_from_tbl;
3023 EM_SAFE_FREE(mail_list_item_from_tbl);
3024 *result_count = count;
3026 EM_SAFE_FREE(mail_list_item_from_tbl);
3029 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3031 // sqlite3_db_release_memory(local_db_handle);
3033 EM_SAFE_FREE(sql_query_string);
3034 EM_SAFE_FREE(date_time_string);
3036 if (err_code != NULL)
3039 EM_PROFILE_END(emstorage_query_mail_list_func);
3040 EM_DEBUG_FUNC_END("ret [%d]", ret);
3045 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)
3047 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);
3049 if (!conditional_clause || !result_mail_tbl || !result_count) {
3050 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: [%p] [%p] [%p]", conditional_clause, result_mail_tbl, result_mail_tbl);
3051 if (err_code != NULL)
3052 *err_code = EMAIL_ERROR_INVALID_PARAM;
3056 int i, col_index = FIELD_COUNT_OF_MAIL_TBL, rc, ret = false, count;
3057 int error = EMAIL_ERROR_NONE;
3058 char **result = NULL, sql_query_string[QUERY_SIZE] = {0, };
3059 emstorage_mail_tbl_t* p_data_tbl = NULL;
3060 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3062 EMSTORAGE_START_READ_TRANSACTION(transaction);
3064 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl %s", conditional_clause);
3066 EM_DEBUG_LOG_DEV("Query[%s]", sql_query_string);
3068 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3069 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3070 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
3073 EM_DEBUG_LOG("No mail found...");
3075 error = EMAIL_ERROR_MAIL_NOT_FOUND;
3079 EM_DEBUG_LOG("There are [%d] mails.", count);
3080 if (!(p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * count))) {
3081 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
3082 error = EMAIL_ERROR_OUT_OF_MEMORY;
3086 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >> ");
3087 for (i = 0; i < count; i++) {
3088 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
3089 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3090 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3091 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_type), col_index++);
3092 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 1, col_index++);
3093 _get_table_field_data_time_t (result, &(p_data_tbl[i].date_time), col_index++);
3094 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
3095 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
3096 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
3097 _get_table_field_data_string(result, &(p_data_tbl[i].message_id), 0, col_index++);
3098 _get_table_field_data_int(result, &(p_data_tbl[i].reference_mail_id), col_index++);
3099 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_from), 1, col_index++);
3100 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_reply), 1, col_index++);
3101 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_to), 1, col_index++);
3102 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_cc), 1, col_index++);
3103 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_bcc), 1, col_index++);
3104 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_return), 1, col_index++);
3105 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_sender), 1, col_index++);
3106 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_recipient), 1, col_index++);
3107 _get_table_field_data_string(result, &(p_data_tbl[i].alias_sender), 1, col_index++);
3108 _get_table_field_data_string(result, &(p_data_tbl[i].alias_recipient), 1, col_index++);
3109 _get_table_field_data_int(result, &(p_data_tbl[i].body_download_status), col_index++);
3110 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
3111 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
3112 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
3113 _get_table_field_data_int(result, &(p_data_tbl[i].mail_size), col_index++);
3114 _get_table_field_data_char (result, &(p_data_tbl[i].flags_seen_field), col_index++);
3115 _get_table_field_data_char (result, &(p_data_tbl[i].flags_deleted_field), col_index++);
3116 _get_table_field_data_char (result, &(p_data_tbl[i].flags_flagged_field), col_index++);
3117 _get_table_field_data_char (result, &(p_data_tbl[i].flags_answered_field), col_index++);
3118 _get_table_field_data_char (result, &(p_data_tbl[i].flags_recent_field), col_index++);
3119 _get_table_field_data_char (result, &(p_data_tbl[i].flags_draft_field), col_index++);
3120 _get_table_field_data_char (result, &(p_data_tbl[i].flags_forwarded_field), col_index++);
3121 _get_table_field_data_int(result, &(p_data_tbl[i].DRM_status), col_index++);
3122 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].priority), col_index++);
3123 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
3124 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
3125 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].report_status), col_index++);
3126 _get_table_field_data_int(result, &(p_data_tbl[i].attachment_count), col_index++);
3127 _get_table_field_data_int(result, &(p_data_tbl[i].inline_content_count), col_index++);
3128 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
3129 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
3130 _get_table_field_data_string(result, &(p_data_tbl[i].preview_text), 1, col_index++);
3131 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].meeting_request_status), col_index++);
3132 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].message_class), col_index++);
3133 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].digest_type), col_index++);
3134 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].smime_type), col_index++);
3135 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].scheduled_sending_time), col_index++);
3136 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].remaining_resend_times), col_index++);
3137 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].tag_id), col_index++);
3138 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].replied_time), col_index++);
3139 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].forwarded_time), col_index++);
3140 _get_table_field_data_string(result, &(p_data_tbl[i].default_charset), 0, col_index++);
3141 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3142 _get_table_field_data_blob(result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3143 _get_table_field_data_string(result, &(p_data_tbl[i].user_name), 0, col_index++);
3150 sqlite3_free_table(result);
3153 *result_mail_tbl = p_data_tbl;
3154 *result_count = count;
3156 *result_mail_tbl = NULL;
3160 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3162 // sqlite3_db_release_memory(local_db_handle);
3164 if (err_code != NULL)
3167 EM_DEBUG_FUNC_END("ret [%d]", ret);
3171 #ifdef __FEATURE_BODY_SEARCH__
3172 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)
3174 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);
3176 if (!conditional_clause || !result_mail_text_tbl || !result_count) { /*prevent 50930*/
3177 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3178 if (err_code != NULL)
3179 *err_code = EMAIL_ERROR_INVALID_PARAM;
3187 int col_index = FIELD_COUNT_OF_MAIL_TEXT_TBL;
3188 int error = EMAIL_ERROR_NONE;
3189 char **result = NULL;
3190 char sql_query_string[QUERY_SIZE] = {0, };
3191 emstorage_mail_text_tbl_t* p_data_tbl = NULL;
3192 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3194 EMSTORAGE_START_READ_TRANSACTION(transaction);
3196 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_text_tbl %s", conditional_clause);
3198 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
3200 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3201 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3202 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3205 EM_DEBUG_LOG("No mail found...");
3207 error = EMAIL_ERROR_MAIL_NOT_FOUND;
3211 EM_DEBUG_LOG("There are [%d] mails.", count);
3212 if (!(p_data_tbl = (emstorage_mail_text_tbl_t *)em_malloc(sizeof(emstorage_mail_text_tbl_t) * count))) {
3213 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_text_tbl_t failed...");
3214 error = EMAIL_ERROR_OUT_OF_MEMORY;
3218 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
3219 for (i = 0; i < count; i++) {
3220 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
3221 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3222 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3223 _get_table_field_data_string(result, &(p_data_tbl[i].body_text), 0, col_index++);
3230 sqlite3_free_table(result);
3233 *result_mail_text_tbl = p_data_tbl;
3234 *result_count = count;
3236 EM_SAFE_FREE(p_data_tbl);
3238 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3240 // sqlite3_db_release_memory(local_db_handle);
3242 if (err_code != NULL)
3245 EM_DEBUG_FUNC_END("ret [%d]", ret);
3250 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)
3252 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);
3258 int error = EMAIL_ERROR_NONE;
3260 int total_count = 0;
3262 char sql_query_string[QUERY_SIZE] = {0, };
3263 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 ";
3264 emstorage_mailbox_tbl_t* p_data_tbl = NULL;
3265 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3267 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
3269 if (input_get_mail_count == 0) { /* without mail count */
3271 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);
3272 } else { /* with read count and total count */
3274 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);
3277 EM_DEBUG_LOG_DEV("query[%s]", sql_query_string);
3279 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
3280 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3281 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string))
3283 EM_DEBUG_LOG_DEV("result count [%d]", count);
3286 EM_DEBUG_LOG_SEC("Can't find mailbox query[%s]", sql_query_string);
3287 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
3291 if ((p_data_tbl = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t) * count)) == NULL) {
3292 EM_DEBUG_EXCEPTION("em_mallocfailed...");
3293 error = EMAIL_ERROR_OUT_OF_MEMORY;
3297 for (i = 0; i < count; i++) {
3298 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3299 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3300 _get_table_field_data_int(result, &(p_data_tbl[i].local_yn), col_index++);
3301 _get_table_field_data_string(result, &(p_data_tbl[i].mailbox_name), 0, col_index++);
3302 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].mailbox_type), col_index++);
3303 _get_table_field_data_string(result, &(p_data_tbl[i].alias), 0, col_index++);
3304 _get_table_field_data_int(result, &(p_data_tbl[i].deleted_flag), col_index++);
3305 _get_table_field_data_int(result, &(p_data_tbl[i].modifiable_yn), col_index++);
3306 _get_table_field_data_int(result, &(p_data_tbl[i].total_mail_count_on_server), col_index++);
3307 _get_table_field_data_int(result, &(p_data_tbl[i].has_archived_mails), col_index++);
3308 _get_table_field_data_int(result, &(p_data_tbl[i].mail_slot_size), col_index++);
3309 _get_table_field_data_int(result, &(p_data_tbl[i].no_select), col_index++);
3310 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].last_sync_time), col_index++);
3311 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3312 _get_table_field_data_blob(result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3314 if (input_get_mail_count == 1) {
3315 _get_table_field_data_int(result, &(total_count), col_index++);
3316 p_data_tbl[i].total_mail_count_on_local = total_count;
3317 _get_table_field_data_int(result, &(read_count), col_index++);
3318 p_data_tbl[i].unread_count = total_count - read_count; /* return unread count, NOT */
3325 sqlite3_free_table(result);
3327 if (error == EMAIL_ERROR_NONE) {
3328 *output_mailbox_list = p_data_tbl;
3329 *output_mailbox_count = count;
3330 EM_DEBUG_LOG("Mailbox Count [%d]", count);
3333 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
3335 // sqlite3_db_release_memory(local_db_handle);
3337 EM_DEBUG_FUNC_END("error [%d]", error);
3341 /* Query series --------------------------------------------------------------*/
3342 INTERNAL_FUNC int emstorage_check_duplicated_account(char *multi_user_name, email_account_t* account, int transaction, int *err_code)
3344 EM_DEBUG_FUNC_BEGIN();
3345 int rc = -1, ret = false;
3346 int error = EMAIL_ERROR_NONE;
3349 char sql_query_string[QUERY_SIZE] = {0, };
3350 sqlite3 *local_db_handle = NULL;
3352 if (account == NULL || account->user_email_address == NULL || account->incoming_server_user_name == NULL || account->incoming_server_address == NULL ||
3353 account->outgoing_server_user_name == NULL || account->outgoing_server_address == NULL) {
3354 if (err_code != NULL)
3355 *err_code = EMAIL_ERROR_INVALID_PARAM;
3359 local_db_handle = emstorage_get_db_connection(multi_user_name);
3361 EMSTORAGE_START_READ_TRANSACTION(transaction);
3363 SNPRINTF(sql_query_string, sizeof(sql_query_string),
3364 "SELECT COUNT(*) FROM mail_account_tbl "
3366 " user_email_address = '%s' AND "
3367 " incoming_server_user_name = '%s' AND "
3368 " incoming_server_type = %d AND "
3369 " incoming_server_address = '%s' AND "
3370 " outgoing_server_user_name = '%s' AND "
3371 " outgoing_server_type = %d AND "
3372 " outgoing_server_address = '%s'; ",
3373 account->user_email_address,
3374 account->incoming_server_user_name, account->incoming_server_type, account->incoming_server_address,
3375 account->outgoing_server_user_name, account->outgoing_server_type, account->outgoing_server_address
3377 EM_DEBUG_LOG_SEC("Query[%s]", sql_query_string);
3378 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
3379 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3380 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3382 count = atoi(result[1]);
3383 sqlite3_free_table(result);
3385 EM_DEBUG_LOG("Count of Duplicated Account Information: [%d]", count);
3387 if (count == 0) { /* not duplicated account */
3389 EM_DEBUG_LOG_SEC("NOT duplicated account: user_email_address[%s]", account->user_email_address);
3390 } else { /* duplicated account */
3392 EM_DEBUG_LOG_SEC("The same account already exists. Duplicated account: user_email_address[%s]", account->user_email_address);
3393 error = EMAIL_ERROR_ALREADY_EXISTS;
3398 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3400 if (err_code != NULL)
3403 EM_DEBUG_FUNC_END("ret [%d]", ret);
3408 INTERNAL_FUNC int emstorage_get_account_count(char *multi_user_name, int *count, int transaction, int *err_code)
3410 EM_DEBUG_FUNC_BEGIN();
3413 if (err_code != NULL)
3414 *err_code = EMAIL_ERROR_INVALID_PARAM;
3418 int rc = -1, ret = false;
3419 int error = EMAIL_ERROR_NONE;
3421 DB_STMT hStmt = NULL;
3422 char sql_query_string[QUERY_SIZE] = {0, };
3425 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3426 EMSTORAGE_START_READ_TRANSACTION(transaction);
3427 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_account_tbl");
3428 EM_DEBUG_LOG_SEC("SQL STMT [%s]", sql_query_string);
3429 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string,
3430 EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3431 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
3432 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3433 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3435 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3436 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3437 ("sqlite3_step fail:%d", rc));
3439 *count = sqlite3_column_int(hStmt, 0);
3445 if (hStmt != NULL) {
3446 rc = sqlite3_finalize(hStmt);
3448 if (rc != SQLITE_OK) {
3449 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
3450 error = EMAIL_ERROR_DB_FAILURE;
3454 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3456 if (err_code != NULL)
3459 EM_DEBUG_FUNC_END("ret [%d]", ret);
3463 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)
3465 EM_DEBUG_FUNC_BEGIN();
3467 int i = 0, count = 0, rc = -1, ret = false;
3468 int field_index = 0;
3470 int error = EMAIL_ERROR_NONE;
3471 emstorage_account_tbl_t *p_data_tbl = NULL;
3472 char sql_query_string[QUERY_SIZE] = {0, };
3473 char *sql = "SELECT count(*) FROM mail_account_tbl;";
3475 sqlite3 *local_db_handle = NULL;
3476 DB_STMT hStmt = NULL;
3478 if (!select_num || !account_list) {
3479 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, account_list);
3480 if (err_code != NULL)
3481 *err_code = EMAIL_ERROR_INVALID_PARAM;
3485 local_db_handle = emstorage_get_db_connection(multi_user_name);
3487 EMSTORAGE_START_READ_TRANSACTION(transaction);
3489 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
3490 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3491 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
3493 count = atoi(result[1]);
3494 sqlite3_free_table(result);
3496 EM_DEBUG_LOG_DEV("count = %d", rc);
3499 EM_DEBUG_LOG("no account found...");
3500 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3504 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
3505 sql_len = EM_SAFE_STRLEN(sql_query_string);
3507 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
3512 "user_data_length, "
3516 "default_mail_slot_size, "
3519 "user_display_name, "
3520 "user_email_address, "
3521 "reply_to_address, "
3523 "incoming_server_type, "
3524 "incoming_server_address, "
3525 "incoming_server_port_number, "
3526 "incoming_server_user_name, "
3527 "incoming_server_password, "
3528 "incoming_server_secure_connection, "
3529 "incoming_server_authentication_method, "
3531 "keep_mails_on_pop_server_after_download, "
3533 "auto_download_size, "
3538 "outgoing_server_type, "
3539 "outgoing_server_address, "
3540 "outgoing_server_port_number, "
3541 "outgoing_server_user_name, "
3542 "outgoing_server_password, "
3543 "outgoing_server_secure_connection, "
3544 "outgoing_server_need_authentication, "
3545 "outgoing_server_use_same_authenticator, "
3548 "req_delivery_receipt, "
3549 "req_read_receipt, "
3553 "display_name_from, "
3555 "forward_with_files, "
3559 "add_my_address_to_bcc, "
3560 "notification_status, "
3562 "display_content_status, "
3563 "default_ringtone_status, "
3564 "alert_ringtone_path, "
3565 "auto_resend_times, "
3566 "outgoing_server_size_limit, "
3567 "wifi_auto_download, "
3569 "incoming_server_requires_apop, "
3571 "certificate_path, "
3576 sql_len = EM_SAFE_STRLEN(sql_query_string);
3578 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " FROM mail_account_tbl ORDER BY account_id");
3580 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3582 EM_DEBUG_LOG_DEV("After sqlite3_prepare_v2 hStmt = %p", hStmt);
3583 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3584 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3586 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3587 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3588 ("sqlite3_step fail:%d", rc));
3589 if (rc == SQLITE_DONE) {
3590 EM_DEBUG_EXCEPTION("no account found...");
3592 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3598 if (!(p_data_tbl = (emstorage_account_tbl_t*)malloc(sizeof(emstorage_account_tbl_t) * count))) {
3599 EM_DEBUG_EXCEPTION("malloc failed...");
3600 error = EMAIL_ERROR_OUT_OF_MEMORY;
3603 memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * count);
3604 for (i = 0; i < count; i++) {
3607 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_id), field_index++);
3608 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].account_name), 0, field_index++);
3609 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].logo_icon_path), 0, field_index++);
3610 _get_stmt_field_data_blob(hStmt, &(p_data_tbl[i].user_data), field_index++);
3611 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].user_data_length), field_index++);
3612 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_svc_id), field_index++);
3613 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_status), field_index++);
3614 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_disabled), field_index++);
3615 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].default_mail_slot_size), field_index++);
3616 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].roaming_option), field_index++);
3617 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].color_label), field_index++);
3618 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_display_name), 0, field_index++);
3619 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_email_address), 0, field_index++);
3620 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].reply_to_address), 0, field_index++);
3621 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].return_address), 0, field_index++);
3622 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].incoming_server_type), field_index++);
3623 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_address), 0, field_index++);
3624 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_port_number), field_index++);
3625 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_user_name), 0, field_index++);
3626 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_password), 0, field_index++);
3627 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_secure_connection), field_index++);
3628 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].incoming_server_authentication_method), field_index++);
3629 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].retrieval_mode), field_index++);
3630 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].keep_mails_on_pop_server_after_download), field_index++);
3631 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].check_interval), field_index++);
3632 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_download_size), field_index++);
3633 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_interval), field_index++);
3634 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_days), field_index++);
3635 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_start_time), field_index++);
3636 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_end_time), field_index++);
3637 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].outgoing_server_type), field_index++);
3638 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_address), 0, field_index++);
3639 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_port_number), field_index++);
3640 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_user_name), 0, field_index++);
3641 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_password), 0, field_index++);
3642 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_secure_connection), field_index++);
3643 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_need_authentication), field_index++);
3644 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_use_same_authenticator), field_index++);
3645 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.priority), field_index++);
3646 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.keep_local_copy), field_index++);
3647 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_delivery_receipt), field_index++);
3648 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_read_receipt), field_index++);
3649 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.download_limit), field_index++);
3650 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_address), field_index++);
3651 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_subject), field_index++);
3652 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.display_name_from), 0, field_index++);
3653 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.reply_with_body), field_index++);
3654 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.forward_with_files), field_index++);
3655 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_myname_card), field_index++);
3656 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_signature), field_index++);
3657 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.signature), 0, field_index++);
3658 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.add_my_address_to_bcc), field_index++);
3659 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.notification_status), field_index++);
3660 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.vibrate_status), field_index++);
3661 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.display_content_status), field_index++);
3662 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.default_ringtone_status), field_index++);
3663 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.alert_ringtone_path), 0, field_index++);
3664 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_resend_times), field_index++);
3665 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_size_limit), field_index++);
3666 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].wifi_auto_download), field_index++);
3667 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].pop_before_smtp), field_index++);
3668 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_requires_apop), field_index++);
3669 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].smime_type), field_index++);
3670 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].certificate_path), 0, field_index++);
3671 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].cipher_type), field_index++);
3672 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].digest_type), field_index++);
3674 /* EAS passwd is not accessible */
3675 if (with_password == true && p_data_tbl[i].incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
3676 /* get password from the secure storage */
3677 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3678 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3680 EM_SAFE_FREE(p_data_tbl[i].incoming_server_password);
3681 EM_SAFE_FREE(p_data_tbl[i].outgoing_server_password);
3683 /* get password file name */
3684 error = _get_password_file_name(multi_user_name, p_data_tbl[i].account_id,
3685 recv_password_file_name,
3686 send_password_file_name);
3687 if (error != EMAIL_ERROR_NONE) {
3688 EM_DEBUG_EXCEPTION("_get_password_file_name error [%d]", error);
3689 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3693 /* read password from secure storage */
3694 error = _read_password_from_secure_storage(recv_password_file_name,
3695 &(p_data_tbl[i].incoming_server_password));
3697 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s] error [%d]",
3698 recv_password_file_name, error);
3699 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3702 error = _read_password_from_secure_storage(send_password_file_name,
3703 &(p_data_tbl[i].outgoing_server_password));
3705 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s] error [%d]",
3706 send_password_file_name, error);
3707 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3712 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3713 EM_DEBUG_LOG_DEV("after sqlite3_step(), i = %d, rc = %d.", i, rc);
3714 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3715 ("sqlite3_step fail:%d", rc));
3722 *account_list = p_data_tbl;
3723 *select_num = count;
3724 EM_DEBUG_LOG_DEV("COUNT : %d", count);
3725 } else if (p_data_tbl != NULL)
3726 emstorage_free_account(&p_data_tbl, count, NULL);
3728 if (hStmt != NULL) {
3729 rc = sqlite3_finalize(hStmt);
3731 if (rc != SQLITE_OK) {
3732 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
3733 error = EMAIL_ERROR_DB_FAILURE;
3737 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3739 if (err_code != NULL)
3742 EM_DEBUG_FUNC_END("ret [%d]", ret);
3746 INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(char *multi_user_name,
3747 char *server_mail_id,
3749 emstorage_mail_tbl_t **mail,
3753 EM_DEBUG_FUNC_BEGIN_SEC("mailbox_id [%d], server_mail_id[%s], mail[%p], transaction[%d], err_code[%p]",
3754 mailbox_id, server_mail_id, mail, transaction, err_code);
3756 int ret = false, error = EMAIL_ERROR_NONE, result_count;
3757 char conditional_clause[QUERY_SIZE] = {0, };
3758 emstorage_mail_tbl_t* p_data_tbl = NULL;
3760 if (!server_mail_id || !mail) {
3761 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3762 error = EMAIL_ERROR_INVALID_PARAM;
3766 sqlite3_snprintf(sizeof(conditional_clause), conditional_clause, "WHERE server_mail_id = '%q'", server_mail_id);
3768 if (mailbox_id > 0) {
3769 SNPRINTF(conditional_clause + strlen(conditional_clause),
3770 QUERY_SIZE - strlen(conditional_clause),
3771 " AND mailbox_id = %d",
3775 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
3776 if (!emstorage_query_mail_tbl(multi_user_name,
3782 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3793 if (err_code != NULL)
3796 EM_DEBUG_FUNC_END("ret [%d]", ret);
3801 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)
3803 int cur_clause = 0, conditional_clause_count = 0, i = 0;
3805 if (account_id < ALL_ACCOUNT) {
3806 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3807 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3811 if (account_id != ALL_ACCOUNT) {
3812 cur_clause += (conditional_clause_count++ == 0) ?
3813 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE account_id = %d", account_id) :
3814 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND account_id = %d", account_id);
3817 if (mailbox_id > 0) {
3818 cur_clause += (conditional_clause_count++ == 0) ?
3819 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_id = %d", mailbox_id) :
3820 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_id = %d", mailbox_id);
3823 if (thread_id > 0) {
3824 cur_clause += (conditional_clause_count++ == 0) ?
3825 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_id = %d ", thread_id) :
3826 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_id = %d ", thread_id);
3827 } else if (thread_id == EMAIL_LIST_TYPE_THREAD) {
3828 cur_clause += (conditional_clause_count++ == 0) ?
3829 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_item_count > 0") :
3830 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_item_count > 0");
3831 } else if (thread_id == EMAIL_LIST_TYPE_LOCAL) {
3832 cur_clause += (conditional_clause_count++ == 0) ?
3833 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE server_mail_status = 0") :
3834 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND server_mail_status = 0");
3835 } else if (thread_id == EMAIL_LIST_TYPE_UNREAD) {
3836 cur_clause += (conditional_clause_count++ == 0) ?
3837 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_seen_field == 0") :
3838 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_seen_field == 0");
3841 /* EM_DEBUG_LOG("where clause added [%s]", conditional_clause_string); */
3842 if (addr_list && addr_list->address_count > 0) {
3843 if (!addr_list->address_type) {
3844 cur_clause += (conditional_clause_count++ == 0) ?
3845 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE email_address_sender IN(\"%s\"", addr_list->address_list[0]) :
3846 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND email_address_sender IN(\"%s\"", addr_list->address_list[0]);
3848 for (i = 1; i < addr_list->address_count; i++)
3849 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3851 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3853 cur_clause += (conditional_clause_count++ == 0) ?
3854 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]) :
3855 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]);
3857 for (i = 1; i < addr_list->address_count; i++)
3858 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3860 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3864 if (input_except_delete_flagged_mails) {
3865 cur_clause += (conditional_clause_count++ == 0) ?
3866 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_deleted_field = 0") :
3867 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_deleted_field = 0");
3871 switch (search_type) {
3872 case EMAIL_SEARCH_FILTER_SUBJECT:
3873 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3874 " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3876 case EMAIL_SEARCH_FILTER_SENDER:
3877 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3878 " %s ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3879 ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3881 case EMAIL_SEARCH_FILTER_RECIPIENT:
3882 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3883 " %s ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3884 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3885 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3886 ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value);
3888 case EMAIL_SEARCH_FILTER_ALL:
3889 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3890 " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\' "
3891 " OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3892 " OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3893 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3894 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3897 ")", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value, search_value, search_value);
3902 /* EM_DEBUG_LOG("where clause [%s]", conditional_clause_string); */
3903 static char sorting_str[][50] = {
3904 " ORDER BY date_time DESC", /* case EMAIL_SORT_DATETIME_HIGH: */
3905 " ORDER BY date_time ASC", /* case EMAIL_SORT_DATETIME_LOW: */
3906 " ORDER BY full_address_from DESC, date_time DESC", /* case EMAIL_SORT_SENDER_HIGH: */
3907 " ORDER BY full_address_from ASC, date_time DESC", /* case EMAIL_SORT_SENDER_LOW: */
3908 " ORDER BY full_address_to DESC, date_time DESC", /* case EMAIL_SORT_RCPT_HIGH: */
3909 " ORDER BY full_address_to ASC, date_time DESC", /* case EMAIL_SORT_RCPT_LOW: */
3910 " ORDER BY subject DESC, date_time DESC", /* case EMAIL_SORT_SUBJECT_HIGH: */
3911 " ORDER BY subject ASC, date_time DESC", /* case EMAIL_SORT_SUBJECT_LOW: */
3912 " ORDER BY priority DESC, date_time DESC", /* case EMAIL_SORT_PRIORITY_HIGH: */
3913 " ORDER BY priority ASC, date_time DESC", /* case EMAIL_SORT_PRIORITY_LOW: */
3914 " ORDER BY attachment_count DESC, date_time DESC", /* case EMAIL_SORT_ATTACHMENT_HIGH: */
3915 " ORDER BY attachment_count ASC, date_time DESC", /* case EMAIL_SORT_ATTACHMENT_LOW: */
3916 " ORDER BY lock_status DESC, date_time DESC", /* case EMAIL_SORT_FAVORITE_HIGH: */
3917 " ORDER BY lock_status ASC, date_time DESC", /* case EMAIL_SORT_FAVORITE_LOW: */
3918 " ORDER BY mailbox_id DESC, date_time DESC", /* case EMAIL_SORT_MAILBOX_ID_HIGH: */
3919 " ORDER BY mailbox_id ASC, date_time DESC", /* case EMAIL_SORT_MAILBOX_ID_LOW: */
3920 " ORDER BY flags_flagged_field DESC, date_time DESC", /* case EMAIL_SORT_FLAGGED_FLAG_HIGH: */
3921 " ORDER BY flags_flagged_field ASC, date_time DESC", /* case EMAIL_SORT_FLAGGED_FLAG_LOW: */
3922 " ORDER BY flags_seen_field DESC, date_time DESC", /* case EMAIL_SORT_SEEN_FLAG_HIGH: */
3923 " ORDER BY flags_seen_field ASC, date_time DESC" /* case EMAIL_SORT_SEEN_FLAG_LOW: */
3926 if (sorting < EMAIL_SORT_END)
3927 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " %s", sorting_str[sorting]);
3929 EM_DEBUG_LOG(" Invalid Sorting order ");
3932 if (start_index != -1 && limit_count != -1)
3933 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " LIMIT %d, %d", start_index, limit_count);
3940 * emstorage_get_mail_list - Get the mail list information.
3944 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)
3946 EM_DEBUG_FUNC_BEGIN();
3947 EM_PROFILE_BEGIN(emstorage_get_mail_list_func);
3949 int ret = false, error = EMAIL_ERROR_NONE;
3950 char conditional_clause_string[QUERY_SIZE] = { 0, };
3952 if (account_id < ALL_ACCOUNT) {
3953 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3954 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3956 EM_IF_NULL_RETURN_VALUE(result_count, false);
3958 _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);
3960 EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
3962 if (!emstorage_query_mail_list(multi_user_name, conditional_clause_string, transaction, mail_list, result_count, &error)) {
3963 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
3970 if (err_code != NULL)
3973 EM_PROFILE_END(emstorage_get_mail_list_func);
3974 EM_DEBUG_FUNC_END("ret [%d]", ret);
3980 * emstorage_get_mails - Get the Mail list information based on mailbox_name name
3984 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)
3986 EM_DEBUG_FUNC_BEGIN();
3987 EM_PROFILE_BEGIN(emStorageGetMails);
3989 int count = 0, ret = false, error = EMAIL_ERROR_NONE;
3990 emstorage_mail_tbl_t *p_data_tbl = NULL;
3991 char conditional_clause_string[QUERY_SIZE] = {0, };
3993 EM_IF_NULL_RETURN_VALUE(mail_list, false);
3994 EM_IF_NULL_RETURN_VALUE(result_count, false);
3996 if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3997 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3998 if (err_code != NULL)
3999 *err_code = EMAIL_ERROR_INVALID_PARAM;
4003 _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);
4005 EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
4007 if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause_string, transaction, &p_data_tbl, &count, &error)) {
4008 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
4017 *mail_list = p_data_tbl;
4018 *result_count = count;
4019 EM_DEBUG_LOG("COUNT : %d", count);
4020 } else if (p_data_tbl != NULL)
4021 emstorage_free_mail(&p_data_tbl, count, NULL);
4023 if (err_code != NULL)
4026 EM_PROFILE_END(emStorageGetMails);
4027 EM_DEBUG_FUNC_END("ret [%d]", ret);
4034 * emstorage_get_searched_mail_list - Get the mail list information after filtering
4038 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)
4040 EM_DEBUG_FUNC_BEGIN();
4042 int ret = false, error = EMAIL_ERROR_NONE;
4043 char conditional_clause[QUERY_SIZE] = {0, };
4044 char *temp_search_value = NULL;
4045 char *temp_search_value2 = NULL;
4047 EM_IF_NULL_RETURN_VALUE(mail_list, false);
4048 EM_IF_NULL_RETURN_VALUE(result_count, false);
4050 if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
4051 EM_DEBUG_EXCEPTION("select_num[%p], Mail_list[%p]", result_count, mail_list);
4052 error = EMAIL_ERROR_INVALID_PARAM;
4056 temp_search_value = em_replace_all_string((char*)search_value, "_", "\\_");
4057 temp_search_value2 = em_replace_all_string(temp_search_value, "%", "\\%");
4059 _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);
4061 EM_DEBUG_LOG("conditional_clause[%s]", conditional_clause);
4063 if (!emstorage_query_mail_list(multi_user_name, conditional_clause, transaction, mail_list, result_count, &error)) {
4064 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
4071 EM_DEBUG_LOG("emstorage_get_searched_mail_list finish off");
4073 if (err_code != NULL)
4076 EM_SAFE_FREE(temp_search_value);
4077 EM_SAFE_FREE(temp_search_value2);
4079 EM_DEBUG_FUNC_END("ret [%d]", ret);
4084 static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name)
4086 EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
4088 if (account_id <= 0 || !recv_password_file_name || !send_password_file_name) {
4089 EM_DEBUG_EXCEPTION("Invalid parameter");
4090 return EMAIL_ERROR_INVALID_PARAM;
4093 EM_DEBUG_LOG("MULTI_USER_NAME : [%s]", multi_user_name);
4095 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
4096 snprintf(recv_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_recv_%s", account_id, multi_user_name);
4097 snprintf(send_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_send_%s", account_id, multi_user_name);
4099 snprintf(recv_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_recv", account_id);
4100 snprintf(send_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_send", account_id);
4103 EM_DEBUG_FUNC_END();
4104 return EMAIL_ERROR_NONE;
4107 static int _read_password_from_secure_storage(char *file_name, char **password)
4109 EM_DEBUG_FUNC_BEGIN_SEC("file_name[%s]", file_name);
4111 if (!file_name || !password) {
4112 EM_DEBUG_EXCEPTION("Invalid Parameter");
4113 return EMAIL_ERROR_INVALID_PARAM;
4116 int ret = EMAIL_ERROR_NONE;
4118 ret = emcore_get_password_in_key_manager(file_name, password);
4119 if (ret != EMAIL_ERROR_NONE) {
4120 EM_DEBUG_EXCEPTION("emcore_get_password_in_key_manager failed : [%d]", ret);
4126 EM_DEBUG_FUNC_END("ret [%d]", ret);
4130 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)
4132 EM_DEBUG_FUNC_BEGIN("account_id[%d], pulloption[%d], account[%p], transaction[%d], err_code[%p]", account_id, pulloption, account, transaction, err_code);
4135 EM_DEBUG_EXCEPTION("account_id[%d], account[%p]", account_id, account);
4136 if (err_code != NULL)
4137 *err_code = EMAIL_ERROR_INVALID_PARAM;
4142 int error = EMAIL_ERROR_NONE;
4143 emstorage_account_tbl_t* p_data_tbl = NULL;
4144 DB_STMT hStmt = NULL;
4145 char sql_query_string[QUERY_SIZE] = {0, };
4148 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4149 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4151 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4153 EMSTORAGE_START_READ_TRANSACTION(transaction);
4155 /* Make query string */
4156 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
4157 sql_len = EM_SAFE_STRLEN(sql_query_string);
4159 if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4160 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4161 "incoming_server_type,"
4162 "incoming_server_address,"
4163 "user_email_address,"
4164 "incoming_server_user_name,"
4166 "incoming_server_port_number,"
4167 "incoming_server_secure_connection,"
4168 "incoming_server_authentication_method,"
4169 "outgoing_server_type,"
4170 "outgoing_server_address,"
4171 "outgoing_server_port_number,"
4172 "outgoing_server_need_authentication,"
4173 "outgoing_server_secure_connection,"
4174 "outgoing_server_user_name,"
4175 "user_display_name,"
4179 "keep_mails_on_pop_server_after_download,"
4180 "auto_download_size,"
4185 "outgoing_server_use_same_authenticator,"
4186 "auto_resend_times,"
4187 "outgoing_server_size_limit,"
4188 "wifi_auto_download,"
4190 "incoming_server_requires_apop,"
4197 sql_len = EM_SAFE_STRLEN(sql_query_string);
4200 if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME) {
4201 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " account_name, ");
4202 sql_len = EM_SAFE_STRLEN(sql_query_string);
4205 /* get from secure storage, not from db */
4206 if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4207 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4210 "req_delivery_receipt,"
4215 "display_name_from,"
4217 "forward_with_files,"
4221 "add_my_address_to_bcc,"
4222 "notification_status,"
4224 "display_content_status,"
4225 "default_ringtone_status,"
4226 "alert_ringtone_path,"
4229 "default_mail_slot_size,"
4236 sql_len = EM_SAFE_STRLEN(sql_query_string);
4239 /* dummy value, FROM WHERE clause */
4240 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "0 FROM mail_account_tbl WHERE account_id = %d", account_id);
4243 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
4245 /* execute a sql and count rows */
4246 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4247 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4248 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4250 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4251 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4252 ("sqlite3_step fail:%d", rc));
4254 if (rc == SQLITE_DONE) {
4255 EM_DEBUG_EXCEPTION("no matched account found...");
4256 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4260 /* Assign query result to structure */
4261 if (!(p_data_tbl = (emstorage_account_tbl_t *)malloc(sizeof(emstorage_account_tbl_t) * 1))) {
4262 EM_DEBUG_EXCEPTION("malloc failed...");
4263 error = EMAIL_ERROR_OUT_OF_MEMORY;
4267 memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * 1);
4270 if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4271 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_type), col_index++);
4272 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_address), 0, col_index++);
4273 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_email_address), 0, col_index++);
4274 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_user_name), 0, col_index++);
4275 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->retrieval_mode), col_index++);
4276 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_port_number), col_index++);
4277 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_secure_connection), col_index++);
4278 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_authentication_method), col_index++);
4279 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->outgoing_server_type), col_index++);
4280 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_address), 0, col_index++);
4281 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_port_number), col_index++);
4282 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_need_authentication), col_index++);
4283 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_secure_connection), col_index++);
4284 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_user_name), 0, col_index++);
4285 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_display_name), 0, col_index++);
4286 _get_stmt_field_data_string(hStmt, &(p_data_tbl->reply_to_address), 0, col_index++);
4287 _get_stmt_field_data_string(hStmt, &(p_data_tbl->return_address), 0, col_index++);
4288 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
4289 _get_stmt_field_data_int(hStmt, &(p_data_tbl->keep_mails_on_pop_server_after_download), col_index++);
4290 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_download_size), col_index++);
4291 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_interval), col_index++);
4292 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_days), col_index++);
4293 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_start_time), col_index++);
4294 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_end_time), col_index++);
4295 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_use_same_authenticator), col_index++);
4296 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_resend_times), col_index++);
4297 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_size_limit), col_index++);
4298 _get_stmt_field_data_int(hStmt, &(p_data_tbl->wifi_auto_download), col_index++);
4299 _get_stmt_field_data_int(hStmt, &(p_data_tbl->pop_before_smtp), col_index++);
4300 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_requires_apop), col_index++);
4301 _get_stmt_field_data_string(hStmt, &(p_data_tbl->logo_icon_path), 0, col_index++);
4302 _get_stmt_field_data_blob(hStmt, &p_data_tbl->user_data, col_index++);
4303 _get_stmt_field_data_int(hStmt, &p_data_tbl->user_data_length, col_index++);
4304 _get_stmt_field_data_int(hStmt, &p_data_tbl->color_label, col_index++);
4305 _get_stmt_field_data_int(hStmt, &(p_data_tbl->check_interval), col_index++);
4306 _get_stmt_field_data_int(hStmt, &(p_data_tbl->sync_status), col_index++);
4309 if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME)
4310 _get_stmt_field_data_string(hStmt, &(p_data_tbl->account_name), 0, col_index++);
4312 if (pulloption & EMAIL_ACC_GET_OPT_PASSWORD) {
4313 /* get password file name */
4314 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) {
4315 EM_DEBUG_EXCEPTION("_get_password_file_name failed [%d]", error);
4319 /* read password from secure storage */
4320 if ((error = _read_password_from_secure_storage(recv_password_file_name, &(p_data_tbl->incoming_server_password))) < 0) {
4321 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4325 EM_DEBUG_LOG_DEV("recv_password_file_name[%s], password[%s]", recv_password_file_name, p_data_tbl->incoming_server_password);
4327 if (p_data_tbl->outgoing_server_use_same_authenticator == 0) {
4328 if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl->outgoing_server_password))) < 0) {
4329 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4332 EM_DEBUG_LOG_DEV("send_password_file_name[%s], password[%s]", send_password_file_name, p_data_tbl->outgoing_server_password);
4336 if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4337 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.priority), col_index++);
4338 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.keep_local_copy), col_index++);
4339 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_delivery_receipt), col_index++);
4340 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_read_receipt), col_index++);
4341 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.download_limit), col_index++);
4342 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_address), col_index++);
4343 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_subject), col_index++);
4344 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.display_name_from), 0, col_index++);
4345 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.reply_with_body), col_index++);
4346 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.forward_with_files), col_index++);
4347 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_myname_card), col_index++);
4348 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_signature), col_index++);
4349 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.signature), 0, col_index++);
4350 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.add_my_address_to_bcc), col_index++);
4351 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.notification_status), col_index++);
4352 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.vibrate_status), col_index++);
4353 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.display_content_status), col_index++);
4354 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.default_ringtone_status), col_index++);
4355 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.alert_ringtone_path), 0, col_index++);
4356 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_svc_id), col_index++);
4357 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->sync_disabled), col_index++);
4358 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->default_mail_slot_size), col_index++);
4359 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->roaming_option), col_index++);
4360 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->smime_type), col_index++);
4361 _get_stmt_field_data_string(hStmt, &(p_data_tbl->certificate_path), 0, col_index++);
4362 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->cipher_type), col_index++);
4363 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->digest_type), col_index++);
4370 *account = p_data_tbl;
4373 emstorage_free_account((emstorage_account_tbl_t **)&p_data_tbl, 1, NULL);
4375 if (hStmt != NULL) {
4376 rc = sqlite3_finalize(hStmt);
4377 if (rc != SQLITE_OK) {
4378 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
4379 error = EMAIL_ERROR_DB_FAILURE;
4383 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4385 if (err_code != NULL)
4388 EM_DEBUG_FUNC_END("ret [%d]", ret);
4392 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)
4394 EM_DEBUG_FUNC_BEGIN("account_id[%d], password_length[%p], err_code[%p]", account_id, password_length, err_code);
4396 if (account_id <= 0 || password_length == NULL) {
4397 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4398 if (err_code != NULL)
4399 *err_code = EMAIL_ERROR_INVALID_PARAM;
4404 int error = EMAIL_ERROR_NONE;
4405 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4406 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4407 char *temp_password = NULL;
4410 /* get password file name */
4411 if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4412 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4416 /* read password from secure storage */
4417 if (password_type == EMAIL_GET_INCOMING_PASSWORD_LENGTH) {
4418 if ((error = _read_password_from_secure_storage(recv_password_file_name, &temp_password)) < 0 || !temp_password) {
4419 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
4422 } else if (password_type == EMAIL_GET_OUTGOING_PASSWORD_LENGTH) {
4423 if ((error = _read_password_from_secure_storage(send_password_file_name, &temp_password)) < 0 || !temp_password) {
4424 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
4428 EM_DEBUG_LOG("Invalid password type");
4429 if (err_code != NULL)
4430 *err_code = EMAIL_ERROR_INVALID_PARAM;
4434 *password_length = EM_SAFE_STRLEN(temp_password);
4436 EM_DEBUG_LOG_SEC("recv_password_file_name[%s], *password_length[%d]", recv_password_file_name, *password_length);
4441 EM_SAFE_FREE(temp_password);
4443 if (err_code != NULL)
4446 EM_DEBUG_FUNC_END("ret [%d]", ret);
4450 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)
4452 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);
4454 int err = EMAIL_ERROR_NONE;
4455 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4456 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4458 if (input_incoming_server_password == NULL && input_outgoing_server_password == NULL) {
4459 EM_DEBUG_EXCEPTION_SEC("Invalid param");
4460 err = EMAIL_ERROR_INVALID_PARAM;
4464 /* get password file name */
4465 if ((err = _get_password_file_name(multi_user_name,
4467 recv_password_file_name,
4468 send_password_file_name)) != EMAIL_ERROR_NONE) {
4469 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4473 EM_DEBUG_LOG_SEC("recv_password_file_name [%s] input_incoming_server_password [%s]",
4474 recv_password_file_name, input_incoming_server_password);
4475 EM_DEBUG_LOG_SEC("send_password_file_name [%s] input_outgoing_server_password [%s]",
4476 send_password_file_name, input_outgoing_server_password);
4478 if (input_incoming_server_password) {
4479 err = emcore_remove_password_in_key_manager(recv_password_file_name);
4480 if (err != EMAIL_ERROR_NONE) {
4481 EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", recv_password_file_name);
4485 /* save recv passwords to the secure storage */
4486 err = emcore_add_password_in_key_manager(recv_password_file_name, input_incoming_server_password);
4487 if (err != EMAIL_ERROR_NONE) {
4488 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4493 if (input_outgoing_server_password) {
4494 err = emcore_remove_password_in_key_manager(send_password_file_name);
4495 if (err != EMAIL_ERROR_NONE) {
4496 EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", send_password_file_name);
4500 /* save send passwords to the secure storage */
4501 err = emcore_add_password_in_key_manager(send_password_file_name, input_outgoing_server_password);
4502 if (err != EMAIL_ERROR_NONE) {
4503 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4509 EM_DEBUG_FUNC_END("err [%d]", err);
4513 INTERNAL_FUNC int emstorage_update_account(char *multi_user_name, int account_id, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
4515 EM_DEBUG_FUNC_BEGIN("account_id[%d], account[%p], transaction[%d], err_code[%p]", account_id, account_tbl, transaction, err_code);
4517 if (account_id < FIRST_ACCOUNT_ID || !account_tbl) {
4518 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4519 if (err_code != NULL)
4520 *err_code = EMAIL_ERROR_INVALID_PARAM;
4524 int error = EMAIL_ERROR_NONE;
4525 int rc, ret = false;
4527 DB_STMT hStmt = NULL;
4528 char sql_query_string[QUERY_SIZE] = {0, };
4530 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4532 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4534 SNPRINTF(sql_query_string, sizeof(sql_query_string),
4535 "UPDATE mail_account_tbl SET"
4537 ", logo_icon_path = ?"
4539 ", user_data_length = ?"
4540 ", account_svc_id = ?"
4542 ", sync_disabled = ?"
4543 ", default_mail_slot_size = ?"
4544 ", roaming_option = ?"
4546 ", user_display_name = ?"
4547 ", user_email_address = ?"
4548 ", reply_to_address = ?"
4549 ", return_address = ?"
4550 ", incoming_server_type = ?"
4551 ", incoming_server_address = ?"
4552 ", incoming_server_port_number = ?"
4553 ", incoming_server_user_name = ?"
4554 ", incoming_server_secure_connection = ?"
4555 ", incoming_server_authentication_method = ?"
4556 ", retrieval_mode = ?"
4557 ", keep_mails_on_pop_server_after_download = ?"
4558 ", check_interval = ?"
4559 ", auto_download_size = ?"
4560 ", peak_interval = ?"
4562 ", peak_start_time = ?"
4563 ", peak_end_time = ?"
4564 ", outgoing_server_type = ?"
4565 ", outgoing_server_address = ?"
4566 ", outgoing_server_port_number = ?"
4567 ", outgoing_server_user_name = ?"
4568 ", outgoing_server_secure_connection = ?"
4569 ", outgoing_server_need_authentication = ?"
4570 ", outgoing_server_use_same_authenticator = ?"
4572 ", keep_local_copy = ?"
4573 ", req_delivery_receipt = ?"
4574 ", req_read_receipt = ?"
4575 ", download_limit = ?"
4576 ", block_address = ?"
4577 ", block_subject = ?"
4578 ", display_name_from = ?"
4579 ", reply_with_body = ?"
4580 ", forward_with_files = ?"
4581 ", add_myname_card = ?"
4582 ", add_signature = ?"
4584 ", add_my_address_to_bcc = ?"
4585 ", notification_status = ?"
4586 ", vibrate_status = ?"
4587 ", display_content_status = ?"
4588 ", default_ringtone_status = ?"
4589 ", alert_ringtone_path = ?"
4590 ", auto_resend_times = ?"
4591 ", outgoing_server_size_limit = ?"
4592 ", wifi_auto_download = ?"
4593 ", pop_before_smtp = ?"
4594 ", incoming_server_requires_apop = ?"
4596 ", certificate_path = ?"
4600 " WHERE account_id = ?");
4602 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4603 EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
4604 EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
4605 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4606 ("sqlite3_prepare fail:(%d) %s", rc, sqlite3_errmsg(local_db_handle)));
4610 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4611 _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4612 _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4613 _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4614 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4615 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4616 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4617 _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4618 _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
4619 _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
4620 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4621 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4622 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4623 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4624 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4625 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4626 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
4627 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4628 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
4629 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
4630 _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
4631 _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
4632 _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
4633 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
4634 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
4635 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
4636 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
4637 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
4638 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
4639 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4640 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
4641 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
4642 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
4643 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
4644 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
4645 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
4646 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
4647 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
4648 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
4649 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
4650 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
4651 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
4652 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4653 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
4654 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
4655 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
4656 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
4657 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
4658 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
4659 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
4660 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
4661 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
4662 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
4663 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4664 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
4665 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
4666 _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
4667 _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
4668 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
4669 _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
4670 _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4671 _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
4672 _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
4673 _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4674 _bind_stmt_field_data_int(hStmt, i++, account_id);
4676 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4677 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
4678 ("sqlite3_step fail:%d", rc));
4679 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4680 ("sqlite3_step fail:%d", rc));
4682 /* validate account existence */
4683 rc = sqlite3_changes(local_db_handle);
4685 EM_DEBUG_EXCEPTION("no matched account found...");
4686 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4690 if (account_tbl->incoming_server_password || account_tbl->outgoing_server_password) {
4691 if ((error = emstorage_update_account_password(multi_user_name, account_id, account_tbl->incoming_server_password, account_tbl->outgoing_server_password)) != EMAIL_ERROR_NONE) {
4692 EM_DEBUG_EXCEPTION("emstorage_update_account_password failed [%d]", error);
4700 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4702 if (hStmt != NULL) {
4703 rc = sqlite3_finalize(hStmt);
4704 if (rc != SQLITE_OK) {
4705 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
4706 error = EMAIL_ERROR_DB_FAILURE;
4710 if (error == EMAIL_ERROR_NONE) {
4711 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_tbl->account_id, 0, NULL, 0))
4712 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_ACCOUNT_UPDATE] : Notification Failed >>> ");
4715 if (err_code != NULL)
4718 EM_DEBUG_FUNC_END("ret [%d]", ret);
4722 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)
4724 EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], field_name[%s], value[%d], transaction[%d]", account_id, field_name, value, transaction);
4725 int error = EMAIL_ERROR_NONE;
4727 char sql_query_string[QUERY_SIZE] = {0, };
4728 sqlite3 *local_db_handle = NULL;
4730 if (!account_id || !field_name) {
4731 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4732 return EMAIL_ERROR_INVALID_PARAM;
4735 local_db_handle = emstorage_get_db_connection(multi_user_name);
4737 /* Write query string */
4738 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_account_tbl SET %s = %d WHERE account_id = %d", field_name, value, account_id);
4740 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4743 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4745 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4746 if (error != EMAIL_ERROR_NONE) {
4747 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4751 if (sqlite3_changes(local_db_handle) == 0)
4752 EM_DEBUG_LOG("no mail matched...");
4756 result = (error == EMAIL_ERROR_NONE) ? true : false;
4757 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, error);
4759 if (error == EMAIL_ERROR_NONE) {
4760 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_id, 0, field_name, value))
4761 EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_ACCOUNT_UPDATE [%s,%d]", field_name, value);
4763 EM_DEBUG_FUNC_END("error [%d]", error);
4767 INTERNAL_FUNC int emstorage_get_sync_status_of_account(char *multi_user_name, int account_id, int *result_sync_status, int *err_code)
4769 EM_DEBUG_FUNC_BEGIN("account_id[%d], result_sync_status [%p], err_code[%p]", account_id, result_sync_status, err_code);
4771 if (!result_sync_status) {
4772 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4773 if (err_code != NULL)
4774 *err_code = EMAIL_ERROR_INVALID_PARAM;
4778 int error = EMAIL_ERROR_NONE, rc, ret = false, sync_status = 0, count, i, col_index;
4779 char sql_query_string[QUERY_SIZE] = {0, };
4780 char **result = NULL;
4781 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4784 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl WHERE account_id = %d", account_id);
4786 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl");
4788 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
4789 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
4790 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4793 EM_DEBUG_EXCEPTION("no matched account found...");
4794 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4799 *result_sync_status = 0;
4801 for (i = 0; i < count; i++) {
4802 _get_table_field_data_int(result, &sync_status, col_index++);
4803 *result_sync_status |= sync_status;
4806 EM_DEBUG_LOG("sync_status [%d]", sync_status);
4808 sqlite3_free_table(result);
4815 if (err_code != NULL)
4818 EM_DEBUG_FUNC_END("ret [%d]", ret);
4822 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)
4824 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);
4826 int error = EMAIL_ERROR_NONE, rc, ret = false, set_value = sync_status, result_sync_status;
4827 char sql_query_string[QUERY_SIZE] = {0, };
4828 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4830 if (set_operator != SET_TYPE_SET && account_id) {
4831 if (!emstorage_get_sync_status_of_account(multi_user_name, account_id, &result_sync_status, &error)) {
4832 EM_DEBUG_EXCEPTION("emstorage_get_sync_status_of_account failed [%d]", error);
4833 if (err_code != NULL)
4837 switch (set_operator) {
4838 case SET_TYPE_UNION:
4839 set_value = result_sync_status | set_value;
4841 case SET_TYPE_MINUS:
4842 set_value = result_sync_status & (~set_value);
4845 EM_DEBUG_EXCEPTION("EMAIL_ERROR_NOT_SUPPORTED [%d]", set_operator);
4846 error = EMAIL_ERROR_NOT_SUPPORTED;
4849 EM_DEBUG_LOG("set_value [%d]", set_value);
4852 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4855 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE account_id = %d", set_value, account_id);
4857 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE incoming_server_type <> 5", set_value);
4859 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4861 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4862 if (error != EMAIL_ERROR_NONE) {
4863 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4867 rc = sqlite3_changes(local_db_handle);
4870 EM_DEBUG_EXCEPTION("no matched account found...");
4871 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4878 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4880 if (error == EMAIL_ERROR_NONE) {
4881 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE_SYNC_STATUS, account_id, set_value, NULL, 0))
4882 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_UPDATE_SYNC_STATUS] : Notification failed");
4885 if (err_code != NULL)
4888 EM_DEBUG_FUNC_END("ret [%d]", ret);
4892 INTERNAL_FUNC int emstorage_add_account(char *multi_user_name, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
4894 EM_DEBUG_FUNC_BEGIN("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4897 EM_DEBUG_EXCEPTION("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4898 if (err_code != NULL)
4899 *err_code = EMAIL_ERROR_INVALID_PARAM;
4905 int rc = -1, ret = false;
4906 int error = EMAIL_ERROR_NONE;
4907 DB_STMT hStmt = NULL;
4908 char sql_query_string[QUERY_SIZE] = {0, };
4909 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4910 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4912 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4914 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4916 char *sql = "SELECT rowid FROM mail_account_tbl;";
4917 char **result = NULL;
4919 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, &row_count, NULL, NULL), rc);
4920 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
4921 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
4923 if (NULL == result[1]) rc = 1;
4925 for (i = 1; i <= row_count; i++) {
4926 if (i != atoi(result[i]))
4932 sqlite3_free_table(result);
4935 if (rc < 0 || rc > EMAIL_ACCOUNT_MAX) {
4936 EM_DEBUG_EXCEPTION("OVERFLOWED THE MAX ACCOUNT");
4937 error = EMAIL_ERROR_ACCOUNT_MAX_COUNT;
4941 account_tbl->account_id = rc;
4943 if ((error = _get_password_file_name(multi_user_name, account_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4944 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4948 EM_DEBUG_LOG(" >>>> ACCOUNT_ID [ %d ] ", account_tbl->account_id);
4949 SNPRINTF(sql_query_string, sizeof(sql_query_string),
4950 "INSERT INTO mail_account_tbl VALUES "
4952 " ? " /* account_id */
4953 " , ? " /* account_name */
4954 " , ? " /* logo_icon_path */
4955 " , ? " /* user_data */
4956 " , ? " /* user_data_length */
4957 " , ? " /* account_svc_id */
4958 " , ? " /* sync_status */
4959 " , ? " /* sync_disabled */
4960 " , ? " /* default_mail_slot_size */
4961 " , ? " /* roaming_option */
4962 " , ? " /* color_label */
4963 " , ? " /* user_display_name */
4964 " , ? " /* user_email_address */
4965 " , ? " /* reply_to_address */
4966 " , ? " /* return_address */
4967 " , ? " /* incoming_server_type */
4968 " , ? " /* incoming_server_address */
4969 " , ? " /* incoming_server_port_number */
4970 " , ? " /* incoming_server_user_name */
4971 " , ? " /* incoming_server_password */
4972 " , ? " /* incoming_server_secure_connection */
4973 " , ? " /* incoming_server_authentication_method */
4974 " , ? " /* retrieval_mode */
4975 " , ? " /* keep_mails_on_pop_server_after_download */
4976 " , ? " /* check_interval */
4977 " , ? " /* auto_download_size */
4978 " , ? " /* peak_interval */
4979 " , ? " /* peak_days */
4980 " , ? " /* peak_start_time */
4981 " , ? " /* peak_end_time */
4982 " , ? " /* outgoing_server_type */
4983 " , ? " /* outgoing_server_address */
4984 " , ? " /* outgoing_server_port_number */
4985 " , ? " /* outgoing_server_user_name */
4986 " , ? " /* outgoing_server_password */
4987 " , ? " /* outgoing_server_secure_connection */
4988 " , ? " /* outgoing_server_need_authentication */
4989 " , ? " /* outgoing_server_use_same_authenticator */
4990 " , ? " /* priority */
4991 " , ? " /* keep_local_copy */
4992 " , ? " /* req_delivery_receipt */
4993 " , ? " /* req_read_receipt */
4994 " , ? " /* download_limit */
4995 " , ? " /* block_address */
4996 " , ? " /* block_subject */
4997 " , ? " /* display_name_from */
4998 " , ? " /* reply_with_body */
4999 " , ? " /* forward_with_files */
5000 " , ? " /* add_myname_card */
5001 " , ? " /* add_signature */
5002 " , ? " /* signature */
5003 " , ? " /* add_my_address_to_bcc */
5004 " , ? " /* auto_resend_times */
5005 " , ? " /* outgoing_server_size_limit */
5006 " , ? " /* wifi_auto_download */
5007 " , ? " /* pop_before_smtp */
5008 " , ? " /* incoming_server_requires_apop */
5009 " , ? " /* smime_type */
5010 " , ? " /* certificate_path */
5011 " , ? " /* cipher_type */
5012 " , ? " /* digest_type */
5013 " , ? " /* notification_status */
5014 " , ? " /* vibrate_status */
5015 " , ? " /* display_content_status */
5016 " , ? " /* default_ringtone_status */
5017 " , ? " /* alert_ringtone_path */
5018 " , ? " /* user_name */
5021 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5022 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5023 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5027 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_id);
5028 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
5029 _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5030 _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
5031 _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
5032 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
5033 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
5034 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
5035 _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
5036 _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
5037 _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
5038 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
5039 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5040 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5041 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5042 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
5043 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5044 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
5045 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
5046 _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
5047 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
5048 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
5049 _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
5050 _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
5051 _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
5052 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
5053 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
5054 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
5055 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
5056 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
5057 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
5058 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5059 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
5060 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
5061 _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
5062 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
5063 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
5064 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
5065 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
5066 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
5067 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
5068 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
5069 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
5070 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
5071 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
5072 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
5073 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
5074 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
5075 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
5076 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
5077 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
5078 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
5079 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
5080 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
5081 _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
5082 _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
5083 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
5084 _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
5085 _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
5086 _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
5087 _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
5088 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
5089 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
5090 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
5091 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
5092 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5093 _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5095 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5097 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
5098 ("sqlite3_step fail:%d", rc));
5099 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5100 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
5103 /* save passwords to the secure storage */
5104 EM_DEBUG_LOG_SEC("save to the secure storage : recv_file[%s], send_file[%s]", recv_password_file_name, send_password_file_name);
5105 error = emcore_add_password_in_key_manager(recv_password_file_name, account_tbl->incoming_server_password);
5106 if (error != EMAIL_ERROR_NONE) {
5107 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
5111 error = emcore_add_password_in_key_manager(send_password_file_name, account_tbl->outgoing_server_password);
5112 if (error != EMAIL_ERROR_NONE) {
5113 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
5120 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5122 if (hStmt != NULL) {
5123 rc = sqlite3_finalize(hStmt);
5124 if (rc != SQLITE_OK) {
5125 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5126 error = EMAIL_ERROR_DB_FAILURE;
5130 if (error == EMAIL_ERROR_NONE) {
5131 if (!emcore_notify_storage_event(NOTI_ACCOUNT_ADD, account_tbl->account_id, 0, NULL, 0))
5132 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_ADD] : Notification failed");
5135 if (err_code != NULL)
5138 EM_DEBUG_FUNC_END("ret [%d]", ret);
5142 INTERNAL_FUNC int emstorage_delete_account(char *multi_user_name, int account_id, int transaction, int *err_code)
5144 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
5146 if (account_id < FIRST_ACCOUNT_ID) {
5147 EM_DEBUG_EXCEPTION(" account_id[%d]", account_id);
5149 if (err_code != NULL)
5150 *err_code = EMAIL_ERROR_INVALID_PARAM;
5154 int rc = -1, ret = false;
5155 int error = EMAIL_ERROR_NONE;
5157 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5159 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5161 /* TODO : delete password files - file names can be obtained from db or a rule that makes a name */
5162 char sql_query_string[QUERY_SIZE] = {0, };
5163 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5164 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5166 /* get password file name */
5167 if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
5168 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
5172 /* delete from db */
5173 memset(sql_query_string, 0x00, sizeof(sql_query_string));
5174 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_account_tbl WHERE account_id = %d", account_id);
5175 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5176 if (error != EMAIL_ERROR_NONE) {
5177 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5181 /* validate account existence */
5182 rc = sqlite3_changes(local_db_handle);
5184 EM_DEBUG_EXCEPTION(" no matched account found...");
5185 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
5189 /* delete from secure storage */
5190 error = emcore_remove_password_in_key_manager(recv_password_file_name);
5191 if (error != EMAIL_ERROR_NONE) {
5192 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5196 error = emcore_remove_password_in_key_manager(send_password_file_name);
5197 if (error != EMAIL_ERROR_NONE) {
5198 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5205 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5207 if (err_code != NULL)
5210 EM_DEBUG_FUNC_END("ret [%d]", ret);
5215 INTERNAL_FUNC int emstorage_free_account(emstorage_account_tbl_t** account_list, int count, int *err_code)
5217 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%d], err_code[%p]", account_list, count, err_code);
5220 int error = EMAIL_ERROR_NONE;
5223 if (!account_list || !*account_list) {
5224 EM_DEBUG_EXCEPTION("account_list[%p], count[%d]", account_list, count);
5225 error = EMAIL_ERROR_INVALID_PARAM;
5229 emstorage_account_tbl_t* p = *account_list;
5232 for (; i < count; i++) {
5233 EM_SAFE_FREE(p[i].account_name);
5234 EM_SAFE_FREE(p[i].incoming_server_address);
5235 EM_SAFE_FREE(p[i].user_email_address);
5236 EM_SAFE_FREE(p[i].user_data);
5237 EM_SAFE_FREE(p[i].incoming_server_user_name);
5238 EM_SAFE_FREE(p[i].incoming_server_password);
5239 EM_SAFE_FREE(p[i].outgoing_server_address);
5240 EM_SAFE_FREE(p[i].outgoing_server_user_name);
5241 EM_SAFE_FREE(p[i].outgoing_server_password);
5242 EM_SAFE_FREE(p[i].user_display_name);
5243 EM_SAFE_FREE(p[i].reply_to_address);
5244 EM_SAFE_FREE(p[i].return_address);
5245 EM_SAFE_FREE(p[i].logo_icon_path);
5246 EM_SAFE_FREE(p[i].options.display_name_from);
5247 EM_SAFE_FREE(p[i].options.signature);
5248 EM_SAFE_FREE(p[i].options.alert_ringtone_path);
5249 EM_SAFE_FREE(p[i].certificate_path);
5250 EM_SAFE_FREE(p[i].user_name);
5254 *account_list = NULL;
5260 if (err_code != NULL)
5263 EM_DEBUG_FUNC_END("ret [%d]", ret);
5267 INTERNAL_FUNC int emstorage_get_mailbox_count(char *multi_user_name, int account_id, int local_yn, int *count, int transaction, int *err_code)
5269 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);
5271 if ((account_id < FIRST_ACCOUNT_ID) || (count == NULL)) {
5272 EM_DEBUG_EXCEPTION(" account_list[%d], local_yn[%d], count[%p]", account_id, local_yn, count);
5274 if (err_code != NULL)
5275 *err_code = EMAIL_ERROR_INVALID_PARAM;
5279 int rc = -1, ret = false;
5280 int error = EMAIL_ERROR_NONE;
5281 char sql_query_string[QUERY_SIZE] = {0, };
5283 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5284 EMSTORAGE_START_READ_TRANSACTION(transaction);
5286 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);
5290 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5291 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
5292 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5294 *count = atoi(result[1]);
5295 sqlite3_free_table(result);
5302 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5304 if (err_code != NULL)
5307 EM_DEBUG_FUNC_END("ret [%d]", ret);
5311 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)
5313 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);
5315 if (!select_num || !mailbox_list) {
5316 EM_DEBUG_EXCEPTION("Invalid parameters");
5318 if (err_code != NULL)
5319 *err_code = EMAIL_ERROR_INVALID_PARAM;
5325 int error = EMAIL_ERROR_NONE;
5326 char conditional_clause_string[QUERY_SIZE] = {0, };
5327 char ordering_clause_string[QUERY_SIZE] = {0, };
5329 if (account_id == ALL_ACCOUNT) {
5330 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5331 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);
5333 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d ", account_id);
5334 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5335 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);
5338 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5340 switch (sort_type) {
5341 case EMAIL_MAILBOX_SORT_BY_NAME_ASC:
5342 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name ASC");
5345 case EMAIL_MAILBOX_SORT_BY_NAME_DSC:
5346 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name DESC");
5349 case EMAIL_MAILBOX_SORT_BY_TYPE_ASC:
5350 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type ASC");
5353 case EMAIL_MAILBOX_SORT_BY_TYPE_DSC:
5354 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type DEC");
5358 EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5360 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5361 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5369 if (err_code != NULL)
5372 EM_DEBUG_FUNC_END("ret [%d]", ret);
5376 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)
5378 EM_DEBUG_FUNC_BEGIN("account_id[%d], local_yn[%d], select_num[%p], mailbox_list[%p], transaction[%d], err_code[%p]", account_id, local_yn, select_num, mailbox_list, transaction, err_code);
5381 int error = EMAIL_ERROR_NONE;
5382 int where_clause_count = 0;
5383 char conditional_clause_string[QUERY_SIZE] = {0, };
5384 char ordering_clause_string[QUERY_SIZE] = {0, };
5386 if (!select_num || !mailbox_list) {
5387 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5388 error = EMAIL_ERROR_INVALID_PARAM;
5392 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL) {
5393 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);
5394 where_clause_count++;
5397 if (account_id > 0) {
5398 if (where_clause_count == 0) {
5399 SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " WHERE ");
5400 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);
5402 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);
5405 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY CASE WHEN MBT.mailbox_name"
5406 " GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\'"
5407 " THEN 2 ELSE 1 END ASC,"
5408 " MBT.mailbox_name COLLATE CONVERTUTF8 ASC ");
5409 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5410 EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5412 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 1, 1, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5413 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5421 if (err_code != NULL)
5424 EM_DEBUG_FUNC_END("ret [%d]", ret);
5428 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)
5430 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);
5433 int error = EMAIL_ERROR_NONE;
5434 char conditional_clause_string[QUERY_SIZE] = {0, };
5436 if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list || !parent_mailbox_name) {
5437 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);
5438 if (err_code != NULL)
5439 *err_code = EMAIL_ERROR_INVALID_PARAM;
5443 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);
5444 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5446 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) {
5447 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5456 if (err_code != NULL)
5459 EM_DEBUG_FUNC_END("ret [%d]", ret);
5463 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)
5465 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);
5466 if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list) {
5467 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5469 if (err_code != NULL)
5470 *err_code = EMAIL_ERROR_INVALID_PARAM;
5476 int error = EMAIL_ERROR_NONE;
5477 char conditional_clause_string[QUERY_SIZE] = {0, };
5479 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND modifiable_yn = 0", account_id);
5480 EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
5482 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) {
5483 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5491 if (err_code != NULL)
5494 EM_DEBUG_FUNC_END("ret [%d]", ret);
5498 INTERNAL_FUNC int emstorage_stamp_last_sync_time_of_mailbox(char *multi_user_name, int input_mailbox_id, int input_transaction)
5500 EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_transaction [%d]", input_mailbox_id, input_transaction);
5502 int result_code = false;
5503 int error = EMAIL_ERROR_NONE;
5504 time_t current_time = 0;
5505 char sql_query_string[QUERY_SIZE] = {0, };
5506 sqlite3 *local_db_handle = NULL;
5508 if (!input_mailbox_id) {
5509 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5510 return EMAIL_ERROR_INVALID_PARAM;
5513 time(¤t_time);
5515 local_db_handle = emstorage_get_db_connection(multi_user_name);
5516 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
5518 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5519 "UPDATE mail_box_tbl SET"
5520 " last_sync_time = %d"
5521 " WHERE mailbox_id = %d"
5523 , input_mailbox_id);
5525 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
5526 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5527 if (error != EMAIL_ERROR_NONE) {
5528 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5534 if (error == EMAIL_ERROR_NONE)
5537 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, result_code, error);
5539 EM_DEBUG_FUNC_END("error [%d]", error);
5543 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)
5545 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);
5546 EM_PROFILE_BEGIN(profile_emstorage_get_mailbox_by_name);
5548 if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !result_mailbox) {
5549 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], mailbox_name[%s], result_mailbox[%p]", account_id, local_yn, mailbox_name, result_mailbox);
5551 if (err_code != NULL)
5552 *err_code = EMAIL_ERROR_INVALID_PARAM;
5557 int error = EMAIL_ERROR_NONE;
5558 int result_count = 0;
5559 char conditional_clause_string[QUERY_SIZE] = {0, };
5561 if (strcmp(mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
5562 if (!(*result_mailbox = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t)))) {
5563 EM_DEBUG_EXCEPTION("malloc failed...");
5564 error = EMAIL_ERROR_OUT_OF_MEMORY;
5568 (*result_mailbox)->mailbox_id = 0;
5569 (*result_mailbox)->account_id = account_id;
5570 (*result_mailbox)->local_yn = 1;
5571 (*result_mailbox)->mailbox_name = EM_SAFE_STRDUP(mailbox_name);
5572 (*result_mailbox)->mailbox_type = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;
5573 (*result_mailbox)->alias = EM_SAFE_STRDUP(mailbox_name);
5574 (*result_mailbox)->deleted_flag = 0;
5575 (*result_mailbox)->modifiable_yn = 1;
5576 (*result_mailbox)->total_mail_count_on_server = 1;
5577 (*result_mailbox)->has_archived_mails = 0;
5578 (*result_mailbox)->mail_slot_size = 0x0FFFFFFF;
5579 (*result_mailbox)->no_select = 0;
5582 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string, "WHERE account_id = %d AND mailbox_name = '%q'", account_id, mailbox_name);
5584 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);
5586 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5588 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5589 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5598 if (err_code != NULL)
5601 EM_PROFILE_END(profile_emstorage_get_mailbox_by_name);
5602 EM_DEBUG_FUNC_END("ret [%d]", ret);
5606 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)
5608 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);
5610 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_USER_DEFINED) || !output_mailbox) {
5611 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_type[%d], output_mailbox[%p]", account_id, mailbox_type, output_mailbox);
5612 if (err_code != NULL)
5613 *err_code = EMAIL_ERROR_INVALID_PARAM;
5618 int error = EMAIL_ERROR_NONE;
5619 int result_count = 0;
5620 emstorage_mailbox_tbl_t *result_mailbox = NULL;
5621 char conditional_clause_string[QUERY_SIZE] = {0,};
5624 SNPRINTF(conditional_clause_string, QUERY_SIZE, "WHERE account_id = %d AND mailbox_type = %d ", account_id, mailbox_type);
5626 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5628 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", true, false, &result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5629 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl error [%d]", error);
5637 *output_mailbox = result_mailbox;
5639 if (err_code != NULL)
5642 EM_DEBUG_FUNC_END("ret [%d]", ret);
5646 INTERNAL_FUNC int emstorage_get_mailbox_by_id(char *multi_user_name, int input_mailbox_id, emstorage_mailbox_tbl_t** output_mailbox)
5648 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], output_mailbox[%p]", input_mailbox_id, output_mailbox);
5650 if (input_mailbox_id <= 0 || !output_mailbox) {
5651 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: input_mailbox_id[%d] output_mailbox[%p]", input_mailbox_id, output_mailbox);
5652 return EMAIL_ERROR_INVALID_PARAM;
5655 int ret = EMAIL_ERROR_NONE;
5656 int result_count = 0;
5657 char conditional_clause_string[QUERY_SIZE] = {0, };
5659 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE MBT.mailbox_id = %d", input_mailbox_id);
5661 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5663 if ((ret = emstorage_query_mailbox_tbl(multi_user_name,
5664 conditional_clause_string,
5665 "", true, false, output_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5666 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", ret);
5672 EM_DEBUG_FUNC_END("ret [%d]", ret);
5676 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)
5678 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);
5680 if (account_id < 0 || !keyword || !result_mailbox) {
5681 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], keyword[%s], result_mailbox[%p]", account_id, keyword, result_mailbox);
5683 if (err_code != NULL)
5684 *err_code = EMAIL_ERROR_INVALID_PARAM;
5689 int error = EMAIL_ERROR_NONE;
5690 char conditional_clause_string[QUERY_SIZE] = {0, };
5692 if (account_id == 0)
5693 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5694 "WHERE alias LIKE \'%%%q%%\'", keyword);
5695 else if (account_id > 0)
5696 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5697 "WHERE account_id = %d AND alias LIKE \'%%%q%%\'", account_id, keyword);
5699 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5701 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, result_count)) != EMAIL_ERROR_NONE) {
5702 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5710 if (err_code != NULL)
5713 EM_DEBUG_FUNC_END("ret [%d]", ret);
5717 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)
5719 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);
5720 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_id) {
5721 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_id[%p]", account_id, mailbox_type, mailbox_id);
5722 if (err_code != NULL)
5723 *err_code = EMAIL_ERROR_INVALID_PARAM;
5727 int rc, ret = false;
5728 int error = EMAIL_ERROR_NONE;
5729 emstorage_account_tbl_t* account = NULL;
5730 DB_STMT hStmt = NULL;
5731 char sql_query_string[QUERY_SIZE] = {0, };
5733 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5734 EMSTORAGE_START_READ_TRANSACTION(transaction);
5736 /* Check whether the account exists. */
5737 if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME, &account, true, &error) || !account) {
5738 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5743 emstorage_free_account(&account, 1, NULL);
5745 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);
5747 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5750 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5752 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5753 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5756 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5757 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5758 ("sqlite3_step fail:%d", rc));
5760 if (rc == SQLITE_DONE) {
5761 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5762 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5766 _get_stmt_field_data_int(hStmt, mailbox_id, 0);
5771 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5772 if (hStmt != NULL) {
5773 rc = sqlite3_finalize(hStmt);
5774 if (rc != SQLITE_OK) {
5775 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5776 error = EMAIL_ERROR_DB_FAILURE;
5780 if (err_code != NULL)
5783 EM_DEBUG_FUNC_END("ret [%d]", ret);
5787 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)
5789 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);
5790 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_name) {
5791 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
5792 if (err_code != NULL)
5793 *err_code = EMAIL_ERROR_INVALID_PARAM;
5797 int rc, ret = false;
5798 int error = EMAIL_ERROR_NONE;
5799 emstorage_account_tbl_t* account = NULL;
5800 DB_STMT hStmt = NULL;
5801 char sql_query_string[QUERY_SIZE] = {0, };
5803 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5804 EMSTORAGE_START_READ_TRANSACTION(transaction);
5806 /* Check whether the account exists. */
5807 if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME, &account, true, &error) || !account) {
5808 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5813 emstorage_free_account(&account, 1, NULL);
5815 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);
5817 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5820 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5822 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5823 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5826 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5827 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5828 ("sqlite3_step fail:%d", rc));
5830 if (rc == SQLITE_DONE) {
5831 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5832 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5836 _get_stmt_field_data_string(hStmt, mailbox_name, 0, 0);
5841 if (hStmt != NULL) {
5842 rc = sqlite3_finalize(hStmt);
5843 if (rc != SQLITE_OK) {
5844 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5845 error = EMAIL_ERROR_DB_FAILURE;
5849 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5851 if (err_code != NULL)
5854 EM_DEBUG_FUNC_END("ret [%d]", ret);
5858 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)
5860 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);
5862 int error = EMAIL_ERROR_NONE;
5863 char sql_query_string[QUERY_SIZE] = {0, };
5864 char *replaced_mailbox_name = NULL;
5867 if (strstr(mailbox_name, "'"))
5868 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5870 replaced_mailbox_name = strdup(mailbox_name);
5873 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5875 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5877 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5878 "UPDATE mail_box_tbl SET"
5879 " modifiable_yn = %d"
5880 " WHERE account_id = %d"
5881 " AND local_yn = %d"
5882 " AND mailbox_name = '%s'"
5886 , replaced_mailbox_name);
5888 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5889 if (error != EMAIL_ERROR_NONE) {
5890 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5897 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5899 EM_SAFE_FREE(replaced_mailbox_name);
5901 if (err_code != NULL)
5904 EM_DEBUG_FUNC_END("ret [%d]", ret);
5909 INTERNAL_FUNC int emstorage_update_mailbox_total_count(char *multi_user_name,
5911 int input_mailbox_id,
5912 int total_count_on_server,
5916 EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], total_count_on_server[%d], "
5917 "transaction[%d], err_code[%p]",
5918 account_id, input_mailbox_id, total_count_on_server, transaction, err_code);
5921 int error = EMAIL_ERROR_NONE;
5922 char sql_query_string[QUERY_SIZE] = {0, };
5924 if (account_id <= 0 || input_mailbox_id <= 0) {
5925 EM_DEBUG_EXCEPTION("account_id[%d], input_mailbox_id[%d]", account_id, input_mailbox_id);
5926 if (err_code != NULL)
5927 *err_code = EMAIL_ERROR_INVALID_PARAM;
5928 EM_DEBUG_FUNC_END("ret [%d]", ret);
5932 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5934 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5936 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5937 "UPDATE mail_box_tbl SET"
5938 " total_mail_count_on_server = %d"
5939 " WHERE account_id = %d"
5940 " AND mailbox_id = %d"
5941 , total_count_on_server
5943 , input_mailbox_id);
5945 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
5946 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5947 if (error != EMAIL_ERROR_NONE) {
5948 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5956 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5958 if (err_code != NULL)
5961 EM_DEBUG_FUNC_END("ret [%d]", ret);
5966 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)
5968 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);
5970 if (account_id < FIRST_ACCOUNT_ID || input_mailbox_id <= 0 || !result_mailbox) {
5971 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);
5973 if (err_code != NULL)
5974 *err_code = EMAIL_ERROR_INVALID_PARAM;
5975 EM_DEBUG_FUNC_END("ret [%d]", EMAIL_ERROR_INVALID_PARAM);
5979 int rc, ret = false;
5980 int error = EMAIL_ERROR_NONE;
5981 char sql_query_string[QUERY_SIZE] = {0, };
5982 DB_STMT hStmt = NULL;
5985 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5986 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5988 if (local_yn != -1) {
5989 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5990 "UPDATE mail_box_tbl SET"
5992 ", mailbox_name = ?"
5993 ", mailbox_type = ?"
5995 ", deleted_flag = ?"
5996 ", modifiable_yn= ?"
5997 ", mail_slot_size= ?"
5998 ", total_mail_count_on_server = ?"
5999 " WHERE account_id = %d"
6000 " AND local_yn = %d"
6001 " AND mailbox_id = '%d'"
6004 , input_mailbox_id);
6006 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6007 "UPDATE mail_box_tbl SET"
6009 ", mailbox_name = ?"
6010 ", mailbox_type = ?"
6012 ", deleted_flag = ?"
6013 ", modifiable_yn= ?"
6014 ", mail_slot_size= ?"
6015 ", total_mail_count_on_server = ?"
6016 " WHERE account_id = %d"
6017 " AND mailbox_id = '%d'"
6019 , input_mailbox_id);
6024 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6025 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6026 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6028 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_id);
6029 _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);
6030 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_type);
6031 _bind_stmt_field_data_string(hStmt, i++, (char *)result_mailbox->alias ? result_mailbox->alias : "", 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
6032 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->deleted_flag);
6033 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->modifiable_yn);
6034 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mail_slot_size);
6035 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->total_mail_count_on_server);
6038 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6039 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
6040 ("sqlite3_step fail:%d", rc));
6041 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6042 ("sqlite3_step fail:%d", rc));
6047 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6049 if (hStmt != NULL) {
6050 rc = sqlite3_finalize(hStmt);
6051 if (rc != SQLITE_OK) {
6052 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6053 error = EMAIL_ERROR_DB_FAILURE;
6057 if (err_code != NULL)
6060 EM_DEBUG_FUNC_END("ret [%d]", ret);
6064 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)
6066 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);
6068 if (account_id < FIRST_ACCOUNT_ID) {
6069 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
6071 if (err_code != NULL)
6072 *err_code = EMAIL_ERROR_INVALID_PARAM;
6076 int rc, ret = false;
6077 int error = EMAIL_ERROR_NONE;
6078 char sql_query_string[QUERY_SIZE] = {0, };
6080 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6081 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6083 EM_DEBUG_LOG("emstorage_update_mailbox_type");
6085 DB_STMT hStmt_box_tbl = NULL;
6086 DB_STMT hStmt_mail_tbl = NULL;
6089 /* Update mail_box_tbl */
6090 if (local_yn != -1) {
6091 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
6092 "UPDATE mail_box_tbl SET"
6094 " WHERE account_id = %d"
6095 " AND local_yn = %d"
6096 " AND mailbox_id = '%d'"
6099 , input_mailbox_id);
6101 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
6102 "UPDATE mail_box_tbl SET"
6104 " WHERE account_id = %d"
6105 " AND mailbox_id = '%d'"
6107 , input_mailbox_id);
6110 EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
6112 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_box_tbl, NULL), rc);
6114 if (SQLITE_OK != rc) {
6115 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6116 error = EMAIL_ERROR_DB_FAILURE;
6120 _bind_stmt_field_data_int(hStmt_box_tbl, i++, new_mailbox_type);
6123 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_box_tbl), rc);
6125 if (rc == SQLITE_FULL) {
6126 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6127 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
6131 if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
6132 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6133 error = EMAIL_ERROR_DB_FAILURE;
6138 /* Update mail_tbl */
6140 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6141 "UPDATE mail_tbl SET"
6143 " WHERE account_id = %d"
6144 " AND mailbox_id = '%d'"
6146 , input_mailbox_id);
6148 EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
6150 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_mail_tbl, NULL), rc);
6151 if (SQLITE_OK != rc) {
6152 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6153 error = EMAIL_ERROR_DB_FAILURE;
6157 _bind_stmt_field_data_int(hStmt_mail_tbl, i++, new_mailbox_type);
6159 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_mail_tbl), rc);
6160 if (rc == SQLITE_FULL) {
6161 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6162 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
6166 if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
6167 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6168 error = EMAIL_ERROR_DB_FAILURE;
6175 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6177 if (hStmt_box_tbl != NULL) {
6178 rc = sqlite3_finalize(hStmt_box_tbl);
6179 if (rc != SQLITE_OK) {
6180 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6181 error = EMAIL_ERROR_DB_FAILURE;
6185 if (hStmt_mail_tbl != NULL) {
6186 rc = sqlite3_finalize(hStmt_mail_tbl);
6187 if (rc != SQLITE_OK) {
6188 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6189 error = EMAIL_ERROR_DB_FAILURE;
6193 if (err_code != NULL)
6196 EM_DEBUG_FUNC_END("ret [%d]", ret);
6200 INTERNAL_FUNC int emstorage_set_local_mailbox(char *multi_user_name, int input_mailbox_id, int input_is_local_mailbox, int transaction)
6202 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);
6204 int rc, ret = false;
6205 int error = EMAIL_ERROR_NONE;
6206 char sql_query_string[QUERY_SIZE] = {0, };
6208 if (input_mailbox_id < 0) {
6209 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6210 return EMAIL_ERROR_INVALID_PARAM;
6213 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6215 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6217 EM_DEBUG_LOG("emstorage_update_mailbox_type");
6219 DB_STMT hStmt = NULL;
6222 /* Update mail_box_tbl */
6223 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6224 "UPDATE mail_box_tbl SET"
6226 " WHERE mailbox_id = %d"
6227 , input_mailbox_id);
6229 EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
6231 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6232 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6233 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6235 _bind_stmt_field_data_int(hStmt, i++, input_is_local_mailbox);
6237 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6238 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
6239 ("sqlite3_step fail:%d", rc));
6240 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6241 ("sqlite3_step fail:%d", rc));
6243 if (hStmt != NULL) {
6244 rc = sqlite3_finalize(hStmt);
6245 if (rc != SQLITE_OK) {
6246 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6247 error = EMAIL_ERROR_DB_FAILURE;
6255 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6257 if (hStmt != NULL) {
6258 rc = sqlite3_finalize(hStmt);
6259 if (rc != SQLITE_OK) {
6260 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6261 error = EMAIL_ERROR_DB_FAILURE;
6265 EM_DEBUG_FUNC_END("error [%d]", error);
6269 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)
6271 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);
6273 int err = EMAIL_ERROR_NONE;
6275 int cur_mailbox_id_string = 0;
6276 int mailbox_id_string_buffer_length = 0;
6277 char sql_query_string[QUERY_SIZE] = {0, };
6278 char *mailbox_id_string_buffer = NULL;
6279 char *parameter_string = NULL;
6280 sqlite3 *local_db_handle = NULL;
6282 if (input_mailbox_id_array == NULL || input_mailbox_id_count == 0 || input_field_name == NULL) {
6283 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6284 return EMAIL_ERROR_INVALID_PARAM;
6287 local_db_handle = emstorage_get_db_connection(multi_user_name);
6289 /* Generating mail id list string */
6290 mailbox_id_string_buffer_length = MAILBOX_ID_STRING_LENGTH * input_mailbox_id_count;
6292 mailbox_id_string_buffer = em_malloc(mailbox_id_string_buffer_length);
6294 if (!mailbox_id_string_buffer) {
6295 EM_DEBUG_EXCEPTION("em_mallocfailed");
6296 err = EMAIL_ERROR_OUT_OF_MEMORY;
6300 for (i = 0; i < input_mailbox_id_count; i++)
6301 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]);
6303 if (EM_SAFE_STRLEN(mailbox_id_string_buffer) > 1)
6304 mailbox_id_string_buffer[EM_SAFE_STRLEN(mailbox_id_string_buffer) - 1] = NULL_CHAR;
6306 /* Generating notification parameter string */
6307 parameter_string = em_malloc(mailbox_id_string_buffer_length + EM_SAFE_STRLEN(input_field_name) + 2);
6309 if (!parameter_string) {
6310 EM_DEBUG_EXCEPTION("em_mallocfailed");
6311 err = EMAIL_ERROR_OUT_OF_MEMORY;
6315 SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", input_field_name, 0x01, mailbox_id_string_buffer);
6317 /* Write query string */
6318 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);
6320 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
6323 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
6324 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6325 if (err != EMAIL_ERROR_NONE) {
6326 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
6330 if (sqlite3_changes(local_db_handle) == 0)
6331 EM_DEBUG_LOG("no mail matched...");
6336 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, err);
6338 if (err == EMAIL_ERROR_NONE && parameter_string) {
6339 if (!emcore_notify_storage_event(NOTI_MAILBOX_FIELD_UPDATE, input_account_id, 0, parameter_string, input_value))
6340 EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_MAILBOX_FIELD_UPDATE [%s,%d]",
6341 input_field_name, input_value);
6344 EM_SAFE_FREE(mailbox_id_string_buffer);
6345 EM_SAFE_FREE(parameter_string);
6347 EM_DEBUG_FUNC_END("err [%d]", err);
6351 INTERNAL_FUNC int emstorage_add_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox_tbl, int transaction, int *err_code)
6353 EM_DEBUG_FUNC_BEGIN("mailbox_tbl[%p], transaction[%d], err_code[%p]", mailbox_tbl, transaction, err_code);
6356 if (err_code != NULL)
6357 *err_code = EMAIL_ERROR_INVALID_PARAM;
6361 int rc, ret = false;
6362 int error = EMAIL_ERROR_NONE;
6363 DB_STMT hStmt = NULL;
6364 char sql_query_string[QUERY_SIZE] = {0,};
6365 char **result = NULL;
6366 time_t current_time;
6367 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6368 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6370 EM_SAFE_STRNCPY(sql_query_string, "SELECT max(rowid) FROM mail_box_tbl;", sizeof(sql_query_string));
6372 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6373 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6374 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6376 time(¤t_time);
6378 if (NULL == result[1])
6381 rc = atoi(result[1]) + 1;
6382 sqlite3_free_table(result);
6384 memset(sql_query_string, 0, sizeof(char) * QUERY_SIZE);
6386 mailbox_tbl->mailbox_id = rc;
6388 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6389 "INSERT INTO mail_box_tbl VALUES "
6390 "(?" /* mailbox_id */
6391 ", ?" /* account_id */
6392 ", ?" /* local_yn */
6393 ", ?" /* mailbox_name */
6394 ", ?" /* mailbox_type */
6396 ", ?" /* deleted_flag */
6397 ", ?" /* modifiable_yn */
6398 ", ?" /* total_mail_count_on_server */
6399 ", ?" /* has_archived_mails */
6400 ", ?" /* mail_slot_size */
6401 ", ?" /* no_select */
6402 ", ?" /* last_sync_time */
6403 ", ?" /* eas_data_length */
6404 ", ?" /* eas_data */
6408 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6409 EM_DEBUG_LOG_DEV("After sqlite3_prepare hStmt = %p", hStmt);
6410 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6411 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6415 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_id);
6416 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->account_id);
6417 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->local_yn);
6418 _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
6419 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_type);
6420 _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->alias, 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
6421 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->deleted_flag);
6422 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->modifiable_yn);
6423 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->total_mail_count_on_server);
6424 _bind_stmt_field_data_int(hStmt, col_index++, 0);
6425 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mail_slot_size);
6426 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->no_select);
6427 _bind_stmt_field_data_int(hStmt, col_index++, current_time);
6428 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->eas_data_length);
6429 _bind_stmt_field_data_blob(hStmt, col_index++, (void*)mailbox_tbl->eas_data, mailbox_tbl->eas_data_length);
6432 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6433 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6434 ("sqlite3_step fail:%dn", rc));
6439 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6440 if (hStmt != NULL) {
6441 rc = sqlite3_finalize(hStmt);
6442 if (rc != SQLITE_OK) {
6443 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6444 error = EMAIL_ERROR_DB_FAILURE;
6448 if (error == EMAIL_ERROR_NONE) {
6449 if (!emcore_notify_storage_event(NOTI_MAILBOX_ADD, mailbox_tbl->account_id, mailbox_tbl->mailbox_id,
6450 mailbox_tbl->mailbox_name, mailbox_tbl->mailbox_type))
6451 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6454 if (err_code != NULL)
6457 EM_DEBUG_FUNC_END("ret [%d]", ret);
6461 INTERNAL_FUNC int emstorage_set_all_mailbox_modifiable_yn(char *multi_user_name, int account_id, int modifiable_yn, int transaction, int *err_code)
6463 EM_DEBUG_FUNC_BEGIN("account_id[%d], modifiable_yn[%d], err_code[%p]", account_id, modifiable_yn, err_code);
6465 if (account_id < FIRST_ACCOUNT_ID) {
6467 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
6469 if (err_code != NULL)
6470 *err_code = EMAIL_ERROR_INVALID_PARAM;
6473 int rc, ret = false;
6474 int error = EMAIL_ERROR_NONE;
6475 char sql_query_string[QUERY_SIZE] = {0,};
6476 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6477 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6480 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET modifiable_yn = %d WHERE account_id = %d", modifiable_yn, account_id);
6482 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6483 if (error != EMAIL_ERROR_NONE) {
6484 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6488 rc = sqlite3_changes(local_db_handle);
6490 EM_DEBUG_EXCEPTION("All mailbox_name modifiable_yn set to 0 already");
6496 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6498 if (err_code != NULL)
6500 EM_DEBUG_FUNC_END("ret [%d]", ret);
6506 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)
6508 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);
6510 if (account_id < FIRST_ACCOUNT_ID) {
6511 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
6513 if (err_code != NULL)
6514 *err_code = EMAIL_ERROR_INVALID_PARAM;
6518 int rc, ret = false;
6519 int error = EMAIL_ERROR_NONE;
6520 char sql_query_string[QUERY_SIZE] = {0, };
6521 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6522 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6525 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_box_tbl WHERE account_id = %d ", account_id);
6527 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);
6529 if (input_mailbox_id > 0) { /* 0 means all mailbox */
6530 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);
6533 EM_DEBUG_LOG_SEC("mailbox sql_query_string [%s]", sql_query_string);
6534 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6535 if (error != EMAIL_ERROR_NONE) {
6536 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6540 rc = sqlite3_changes(local_db_handle);
6542 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
6543 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
6550 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6552 if (error == EMAIL_ERROR_NONE) {
6553 if (!emcore_notify_storage_event(NOTI_MAILBOX_DELETE, account_id, input_mailbox_id, NULL, 0))
6554 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6557 if (err_code != NULL)
6560 EM_DEBUG_FUNC_END("ret [%d]", ret);
6564 INTERNAL_FUNC int emstorage_free_mailbox(emstorage_mailbox_tbl_t** mailbox_list, int count, int *err_code)
6566 EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d], err_code[%p]", mailbox_list, count, err_code);
6569 int error = EMAIL_ERROR_NONE;
6572 if (!mailbox_list || !*mailbox_list) {
6573 EM_DEBUG_EXCEPTION(" mailbox_list[%p], count[%d]", mailbox_list, count);
6575 error = EMAIL_ERROR_INVALID_PARAM;
6579 emstorage_mailbox_tbl_t* p = *mailbox_list;
6582 for (; i < count; i++) {
6583 EM_SAFE_FREE(p[i].mailbox_name);
6584 EM_SAFE_FREE(p[i].alias);
6585 EM_SAFE_FREE(p[i].eas_data); /*valgrind*/
6588 EM_SAFE_FREE(p); *mailbox_list = NULL;
6594 if (err_code != NULL)
6597 EM_DEBUG_FUNC_END("ret [%d]", ret);
6601 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)
6603 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);
6605 if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !count) {
6606 EM_DEBUG_EXCEPTION("Invalid params");
6608 if (err_code != NULL)
6609 *err_code = EMAIL_ERROR_INVALID_PARAM;
6613 int rc = -1, ret = false;
6614 int error = EMAIL_ERROR_NONE;
6615 char sql_query_string[QUERY_SIZE] = {0, };
6616 char *replaced_mailbox_name = NULL;
6618 if (strstr(mailbox_name, "'"))
6619 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6621 replaced_mailbox_name = EM_SAFE_STRDUP(mailbox_name);
6623 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6626 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6627 EMSTORAGE_START_READ_TRANSACTION(transaction);
6628 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);
6629 EM_DEBUG_LOG_SEC(">>> SQL [ %s ] ", sql_query_string);
6633 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6634 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6635 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6637 *count = atoi(result[1]);
6638 sqlite3_free_table(result);
6643 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6645 EM_SAFE_FREE(replaced_mailbox_name);
6647 if (err_code != NULL)
6650 EM_DEBUG_FUNC_END("ret [%d]", ret);
6654 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)
6656 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);
6658 if (account_id < FIRST_ACCOUNT_ID || !uid || !exist) {
6659 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], uid[%p], exist[%p]", account_id, mailbox_name , uid, exist);
6661 if (err_code != NULL)
6662 *err_code = EMAIL_ERROR_INVALID_PARAM;
6666 int rc = -1, ret = false;
6667 int error = EMAIL_ERROR_NONE;
6668 char sql_query_string[QUERY_SIZE] = {0, };
6669 char *replaced_mailbox_name = NULL;
6671 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6673 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6674 EMSTORAGE_START_READ_TRANSACTION(transaction);
6677 if (strstr(mailbox_name, "'"))
6678 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6680 replaced_mailbox_name = strdup(mailbox_name);
6682 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);
6684 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);
6687 char **result = NULL;
6689 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6690 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6691 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6693 *exist = atoi(result[1]);
6694 sqlite3_free_table(result);
6704 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6706 EM_SAFE_FREE(replaced_mailbox_name);
6708 if (err_code != NULL)
6711 EM_DEBUG_FUNC_END("ret [%d]", ret);
6715 INTERNAL_FUNC int emstorage_get_downloaded_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code)
6717 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], err_code[%p]", mail_id, mail, err_code);
6719 if (!mail || mail_id <= 0) {
6720 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
6721 if (err_code != NULL)
6722 *err_code = EMAIL_ERROR_INVALID_PARAM;
6726 int rc, ret = false;
6727 int error = EMAIL_ERROR_NONE;
6728 DB_STMT hStmt = NULL;
6729 char sql_query_string[QUERY_SIZE] = {0, };
6731 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6732 EMSTORAGE_START_READ_TRANSACTION(transaction);
6734 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE local_uid = %d", mail_id);
6736 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6737 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6739 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6740 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6743 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6744 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6745 ("sqlite3_step fail:%d", rc));
6747 *mail = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
6748 if (*mail == NULL) {
6749 error = EMAIL_ERROR_OUT_OF_MEMORY;
6750 EM_DEBUG_EXCEPTION("Memory allocation for mail failed.");
6754 memset(*mail, 0x00, sizeof(emstorage_mail_tbl_t));
6756 _get_stmt_field_data_int(hStmt, &((*mail)->account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6757 _get_stmt_field_data_int(hStmt, &((*mail)->mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6758 _get_stmt_field_data_string(hStmt, &((*mail)->server_mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6759 _get_stmt_field_data_int(hStmt, &((*mail)->mail_id), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6760 _get_stmt_field_data_string(hStmt, &((*mail)->server_mail_id), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6761 _get_stmt_field_data_int(hStmt, &((*mail)->mail_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6762 _get_stmt_field_data_char(hStmt, &((*mail)->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6764 (*mail)->server_mail_status = 1;
6770 if (hStmt != NULL) {
6771 rc = sqlite3_finalize(hStmt);
6772 if (rc != SQLITE_OK) {
6773 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6774 error = EMAIL_ERROR_DB_FAILURE;
6778 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6780 if (err_code != NULL)
6783 EM_DEBUG_FUNC_END("ret [%d]", ret);
6787 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)
6789 EM_PROFILE_BEGIN(emStorageGetDownloadList);
6790 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);
6791 if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid || !count) {
6792 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_id[%d], read_mail_uid[%p], count[%p]", account_id, mailbox_id, read_mail_uid, count);
6794 if (err_code != NULL)
6795 *err_code = EMAIL_ERROR_INVALID_PARAM;
6799 int rc, ret = false;
6800 int error = EMAIL_ERROR_NONE;
6802 DB_STMT hStmt = NULL;
6803 char sql_query_string[QUERY_SIZE] = {0, };
6805 emstorage_read_mail_uid_tbl_t* p_data_tbl = NULL;
6808 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6809 EMSTORAGE_START_READ_TRANSACTION(transaction);
6812 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);
6814 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
6816 EM_DEBUG_LOG_SEC(" sql_query_string : %s", sql_query_string);
6820 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6821 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6822 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6823 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6826 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6827 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6828 ("sqlite3_step fail:%d", rc));
6831 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL); */
6832 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL), rc);
6833 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6834 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6836 sqlite3_free_table(result);
6838 EM_DEBUG_LOG("No mail found in mail_read_mail_uid_tbl");
6844 if (!(p_data_tbl = (emstorage_read_mail_uid_tbl_t*)malloc(sizeof(emstorage_read_mail_uid_tbl_t) * *count))) {
6845 EM_DEBUG_EXCEPTION(" malloc failed...");
6846 error = EMAIL_ERROR_OUT_OF_MEMORY;
6850 memset(p_data_tbl, 0x00, sizeof(emstorage_read_mail_uid_tbl_t)*(*count));
6852 for (i = 0; i < *count; ++i) {
6853 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6854 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6855 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6856 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].local_uid), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6857 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].server_uid), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6858 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rfc822_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6859 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6860 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_flagged_field), FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6862 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6863 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6864 ("sqlite3_step fail:%d", rc));
6871 *read_mail_uid = p_data_tbl;
6872 else if (p_data_tbl)
6873 emstorage_free_read_mail_uid(&p_data_tbl, *count, NULL);
6875 if (hStmt != NULL) {
6876 rc = sqlite3_finalize(hStmt);
6877 if (rc != SQLITE_OK) {
6878 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6879 error = EMAIL_ERROR_DB_FAILURE;
6883 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6885 if (err_code != NULL)
6888 EM_PROFILE_END(emStorageGetDownloadList);
6889 EM_DEBUG_FUNC_END("ret [%d]", ret);
6893 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)
6895 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);
6897 if (account_id < FIRST_ACCOUNT_ID || !mail_size) {
6898 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);
6900 if (err_code != NULL)
6901 *err_code = EMAIL_ERROR_INVALID_PARAM;
6905 int rc, ret = false;
6906 int error = EMAIL_ERROR_NONE;
6907 DB_STMT hStmt = NULL;
6908 char sql_query_string[QUERY_SIZE] = {0, };
6909 char *replaced_mailbox_name = NULL;
6911 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6912 EMSTORAGE_START_READ_TRANSACTION(transaction);
6915 if (strstr(mailbox_name, "'"))
6916 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6918 replaced_mailbox_name = strdup(mailbox_name);
6920 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6922 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6923 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6924 "WHERE account_id = %d "
6925 "AND mailbox_id = '%s' "
6926 "AND local_uid = %d "
6927 "AND mailbox_name = '%s' "
6928 "AND server_uid = '%s'",
6929 account_id, mailbox_id, local_uid, replaced_mailbox_name, uid);
6931 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6932 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6933 "WHERE account_id = %d "
6934 "AND mailbox_id = '%s' "
6935 "AND local_uid = %d "
6936 "AND server_uid = '%s'",
6937 account_id, mailbox_id, local_uid, uid);
6941 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6942 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6943 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6946 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6947 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6948 ("sqlite3_step fail:%d", rc));
6950 if (rc == SQLITE_DONE) {
6951 EM_DEBUG_LOG("no matched mail found....");
6952 error = EMAIL_ERROR_MAIL_NOT_FOUND;
6956 _get_stmt_field_data_int(hStmt, mail_size, 0);
6961 EM_SAFE_FREE(replaced_mailbox_name);
6963 if (hStmt != NULL) {
6964 rc = sqlite3_finalize(hStmt);
6965 if (rc != SQLITE_OK) {
6966 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6967 error = EMAIL_ERROR_DB_FAILURE;
6971 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6973 if (err_code != NULL)
6976 EM_DEBUG_FUNC_END("ret [%d]", ret);
6980 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)
6982 EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], transaction[%d], err_code[%p]", read_mail_uid, transaction, err_code);
6984 if (!read_mail_uid) {
6985 EM_DEBUG_EXCEPTION("read_mail_uid[%p]", read_mail_uid);
6986 if (err_code != NULL)
6987 *err_code = EMAIL_ERROR_INVALID_PARAM;
6991 int rc, rc2, ret = false;
6992 int error = EMAIL_ERROR_NONE;
6993 DB_STMT hStmt = NULL;
6994 char sql_query_string[QUERY_SIZE] = {0, };
6996 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6997 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6999 char *sql = "SELECT max(rowid) FROM mail_read_mail_uid_tbl;";
7000 char **result = NULL;
7003 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7004 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7005 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7007 if (NULL == result[1]) rc = 1;
7008 else rc = atoi(result[1])+1;
7009 sqlite3_free_table(result);
7011 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7012 "INSERT INTO mail_read_mail_uid_tbl VALUES "
7013 "(?" /* account_id */
7014 ", ?" /* mailbox_id */
7015 ", ?" /* mailbox_name */
7016 ", ?" /* local_uid */
7017 ", ?" /* server_uid */
7018 ", ?" /* rfc822_size */
7019 ", ?" /* sync_status */
7020 ", ?" /* flags_seen_field */
7021 ", ?" /* flags_flagged_field */
7025 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
7026 if (rc2 != SQLITE_OK) {
7027 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7028 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
7030 error = EMAIL_ERROR_DB_FAILURE;
7034 EM_DEBUG_LOG("account_id[%d] mailbox_id[%d] local_uid [%d]"
7035 "server_uid[%s] rfc822_size[%d] rc[%d]",
7036 read_mail_uid->account_id, read_mail_uid->mailbox_id, read_mail_uid->local_uid,
7037 read_mail_uid->server_uid, read_mail_uid->rfc822_size, rc);
7039 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->account_id);
7040 _bind_stmt_field_data_int(hStmt, LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->mailbox_id);
7041 _bind_stmt_field_data_int(hStmt, LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->local_uid);
7042 _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);
7043 _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);
7044 _bind_stmt_field_data_int(hStmt, RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->rfc822_size);
7045 _bind_stmt_field_data_int(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_seen_field);
7046 _bind_stmt_field_data_int(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_flagged_field);
7047 _bind_stmt_field_data_int(hStmt, IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, rc);
7050 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7051 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
7052 ("sqlite3_step fail:%d", rc));
7053 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7054 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
7060 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7061 if (hStmt != NULL) {
7062 rc = sqlite3_finalize(hStmt);
7063 if (rc != SQLITE_OK) {
7064 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7065 error = EMAIL_ERROR_DB_FAILURE;
7069 if (err_code != NULL)
7072 EM_DEBUG_FUNC_END("ret [%d]", ret);
7076 #ifdef __FEATURE_BODY_SEARCH__
7077 INTERNAL_FUNC int emstorage_add_mail_text(char *multi_user_name, emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
7079 EM_DEBUG_FUNC_BEGIN("mail_text[%p], transaction[%d], err_code[%p]", mail_text, transaction, err_code);
7082 EM_DEBUG_EXCEPTION("mail_text[%p]", mail_text);
7083 if (err_code != NULL)
7084 *err_code = EMAIL_ERROR_INVALID_PARAM;
7088 int rc, rc2, ret = false;
7089 int error = EMAIL_ERROR_NONE;
7090 DB_STMT hStmt = NULL;
7091 char sql_query_string[QUERY_SIZE] = {0, };
7093 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7094 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7096 char *sql = "SELECT max(rowid) FROM mail_text_tbl;";
7097 char **result = NULL;
7099 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7100 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7101 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7102 sqlite3_free_table(result);
7104 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7105 "INSERT INTO mail_text_tbl VALUES "
7111 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
7112 if (rc2 != SQLITE_OK) {
7113 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7114 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc2, sqlite3_errmsg(local_db_handle));
7116 error = EMAIL_ERROR_DB_FAILURE;
7120 EM_DEBUG_LOG("mail_id[%d] account_id[%d] mailbox_id[%d]", mail_text->mail_id,
7121 mail_text->account_id, mail_text->mailbox_id);
7122 EM_DEBUG_LOG_DEV("body_text VALUE [%s] ", mail_text->body_text);
7124 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mail_id);
7125 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->account_id);
7126 _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mailbox_id);
7127 _bind_stmt_field_data_string(hStmt, BODY_TEXT_IDX_IN_MAIL_TEXT_TBL, (char *)mail_text->body_text, 0, -1);
7129 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7130 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
7131 ("sqlite3_step fail:%d", rc));
7132 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7133 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
7138 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7139 if (hStmt != NULL) {
7140 rc = sqlite3_finalize(hStmt);
7141 if (rc != SQLITE_OK) {
7142 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7143 error = EMAIL_ERROR_DB_FAILURE;
7147 if (err_code != NULL)
7150 EM_DEBUG_FUNC_END("ret [%d]", ret);
7155 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)
7157 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);
7159 if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid) {
7160 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);
7162 if (err_code != NULL)
7163 *err_code = EMAIL_ERROR_INVALID_PARAM;
7167 int rc, ret = false;
7168 int error = EMAIL_ERROR_NONE;
7169 DB_STMT hStmt = NULL;
7170 char sql_query_string[QUERY_SIZE] = {0, };
7172 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7174 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7176 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7177 "UPDATE mail_read_mail_uid_tbl SET"
7180 ", mailbox_name = ?"
7184 ", flags_seen_field = ?"
7185 ", flags_flagged_field = ?"
7186 " WHERE account_id = ?"
7187 " AND mailbox_id = ?"
7188 " AND local_uid = ?"
7189 " AND mailbox_name= ?"
7190 " AND server_uid = ?");
7193 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7194 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7195 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7196 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7201 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->account_id);
7202 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->mailbox_id);
7203 _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7204 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->local_uid);
7205 _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->server_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7206 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->rfc822_size);
7207 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_seen_field);
7208 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_flagged_field);
7209 _bind_stmt_field_data_int(hStmt, i++, account_id);
7210 _bind_stmt_field_data_int(hStmt, i++, mailbox_id);
7211 _bind_stmt_field_data_int(hStmt, i++, local_uid);
7212 _bind_stmt_field_data_string(hStmt, i++, (char*)mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7213 _bind_stmt_field_data_string(hStmt, i++, (char*)uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7216 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7217 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7218 ("sqlite3_step fail:%d", rc));
7223 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7224 if (hStmt != NULL) {
7225 rc = sqlite3_finalize(hStmt);
7226 if (rc != SQLITE_OK) {
7227 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7228 error = EMAIL_ERROR_DB_FAILURE;
7232 if (err_code != NULL)
7235 EM_DEBUG_FUNC_END("ret [%d]", ret);
7239 INTERNAL_FUNC int emstorage_remove_downloaded_mail(char *multi_user_name,
7247 EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], mailbox_id[%d], mailbox_name[%s], "
7248 "uid[%s], transaction[%d], err_code[%p]",
7249 account_id, mailbox_id, mailbox_name, uid, transaction, err_code);
7251 if (account_id < FIRST_ACCOUNT_ID) {
7252 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_name[%s], uid[%s]", account_id, mailbox_name, uid);
7254 if (err_code != NULL)
7255 *err_code = EMAIL_ERROR_INVALID_PARAM;
7260 int error = EMAIL_ERROR_NONE;
7261 char sql_query_string[QUERY_SIZE] = {0, };
7262 char *replaced_mailbox_name = NULL;
7263 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7265 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7267 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7268 "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d ", account_id);
7270 if (mailbox_id > 0) {
7271 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7272 sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7273 "AND mailbox_id = %d ", mailbox_id);
7276 if (mailbox_name) { /* NULL means all mailbox_name */
7277 if (strstr(mailbox_name, "'"))
7278 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
7280 replaced_mailbox_name = strdup(mailbox_name);
7282 SNPRINTF(sql_query_string+EM_SAFE_STRLEN(sql_query_string),
7283 sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7284 "AND mailbox_name = '%s' ", replaced_mailbox_name);
7287 if (uid) { /* NULL means all mail */
7288 sqlite3_snprintf(sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7289 sql_query_string + EM_SAFE_STRLEN(sql_query_string), "AND server_uid = '%q' ",
7293 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7294 if (error != EMAIL_ERROR_NONE) {
7295 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7302 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7304 EM_SAFE_FREE(replaced_mailbox_name);
7306 if (err_code != NULL)
7309 EM_DEBUG_FUNC_END("ret [%d]", ret);
7313 INTERNAL_FUNC int emstorage_free_read_mail_uid(emstorage_read_mail_uid_tbl_t** read_mail_uid, int count, int *err_code)
7315 EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], count[%d], err_code[%p]", read_mail_uid, count, err_code);
7318 int error = EMAIL_ERROR_NONE;
7321 if (!read_mail_uid || !*read_mail_uid) {
7322 EM_DEBUG_EXCEPTION(" read_mail_uid[%p], count[%d]", read_mail_uid, count);
7324 error = EMAIL_ERROR_INVALID_PARAM;
7328 emstorage_read_mail_uid_tbl_t* p = *read_mail_uid;
7331 for (i = 0; i < count; i++) {
7332 EM_SAFE_FREE(p[i].mailbox_name);
7333 EM_SAFE_FREE(p[i].server_uid);
7336 EM_SAFE_FREE(p); *read_mail_uid = NULL;
7342 if (err_code != NULL)
7345 EM_DEBUG_FUNC_END("ret [%d]", ret);
7349 INTERNAL_FUNC int emstorage_get_rule_count_by_account_id(char *multi_user_name, int account_id, int *count, int transaction, int *err_code)
7351 EM_DEBUG_FUNC_BEGIN("account_id [%d], count[%p], transaction[%d], err_code[%p]", count, transaction, err_code);
7354 EM_DEBUG_EXCEPTION("count[%p]", count);
7356 if (err_code != NULL)
7357 *err_code = EMAIL_ERROR_INVALID_PARAM;
7361 int rc = -1, ret = false;
7362 int error = EMAIL_ERROR_NONE;
7363 char sql_query_string[QUERY_SIZE] = {0, };
7365 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7366 EMSTORAGE_START_READ_TRANSACTION(transaction);
7368 if (account_id != ALL_ACCOUNT)
7369 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl where account_id = %d", account_id);
7371 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl");
7375 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7376 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7377 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7379 *count = atoi(result[1]);
7380 sqlite3_free_table(result);
7385 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7387 if (err_code != NULL)
7390 EM_DEBUG_FUNC_END("ret [%d]", ret);
7394 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)
7396 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);
7398 if (!select_num || !is_completed || !rule_list) { /* only global rule supported. */
7399 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);
7401 if (err_code != NULL)
7402 *err_code = EMAIL_ERROR_INVALID_PARAM;
7407 int error = EMAIL_ERROR_NONE;
7409 emstorage_rule_tbl_t* p_data_tbl = NULL;
7410 int i = 0, count = 0;
7411 DB_STMT hStmt = NULL;
7412 char sql_query_string[QUERY_SIZE] = {0, };
7416 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7417 EMSTORAGE_START_READ_TRANSACTION(transaction);
7419 if (account_id != ALL_ACCOUNT) {
7421 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d AND type = %d", account_id, type);
7423 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d ORDER BY type", account_id);
7426 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE type = %d", type);
7428 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl ORDER BY type");
7431 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7432 /* EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt); */
7433 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7434 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7437 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7438 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7439 ("sqlite3_step fail:%d", rc));
7442 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL); */
7443 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
7444 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7445 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7447 sqlite3_free_table(result);
7450 EM_DEBUG_LOG_DEV("No matching rule found...");
7452 error = EMAIL_ERROR_FILTER_NOT_FOUND; /*there is no matched rule*/
7457 if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t) * count))) {
7458 EM_DEBUG_EXCEPTION(" malloc failed...");
7460 error = EMAIL_ERROR_OUT_OF_MEMORY;
7464 memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t) * count);
7466 for (i = 0; i < count; i++) {
7467 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7468 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7469 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7470 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].type), TYPE_IDX_IN_MAIL_RULE_TBL);
7471 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7472 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7473 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7474 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7475 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7476 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7478 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7479 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7480 ("sqlite3_step fail:%d", rc));
7487 EM_DEBUG_LOG("[%d] rules found.", count);
7490 *rule_list = p_data_tbl;
7491 *select_num = count;
7492 } else if (p_data_tbl != NULL)
7493 emstorage_free_rule(&p_data_tbl, count, NULL); /* CID FIX */
7495 if (hStmt != NULL) {
7496 rc = sqlite3_finalize(hStmt);
7497 if (rc != SQLITE_OK) {
7498 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7499 error = EMAIL_ERROR_DB_FAILURE;
7503 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7505 if (err_code != NULL)
7508 EM_DEBUG_FUNC_END("ret [%d]", ret);
7512 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)
7514 EM_DEBUG_FUNC_BEGIN("rule_id[%d], rule[%p], transaction[%d], err_code[%p]", rule_id, rule, transaction, err_code);
7515 int error = EMAIL_ERROR_NONE;
7517 DB_STMT hStmt = NULL;
7520 EM_DEBUG_EXCEPTION("Invalid parameter");
7521 error = EMAIL_ERROR_INVALID_PARAM;
7526 EM_DEBUG_EXCEPTION("rule_id[%d], rule[%p]", rule_id, rule);
7527 error = EMAIL_ERROR_INVALID_PARAM;
7531 emstorage_rule_tbl_t* p_data_tbl = NULL;
7534 char sql_query_string[QUERY_SIZE] = {0, };
7535 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7536 EMSTORAGE_START_READ_TRANSACTION(transaction);
7538 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7540 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7541 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7542 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7545 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7546 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7547 ("sqlite3_step fail:%d", rc));
7549 if (rc == SQLITE_DONE) {
7550 EM_DEBUG_EXCEPTION(" no matched rule found...");
7551 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7555 if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t)))) {
7556 EM_DEBUG_EXCEPTION(" malloc failed...");
7557 error = EMAIL_ERROR_OUT_OF_MEMORY;
7561 memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t));
7562 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7563 _get_stmt_field_data_int(hStmt, &(p_data_tbl->rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7564 _get_stmt_field_data_string(hStmt, &(p_data_tbl->filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7565 _get_stmt_field_data_int(hStmt, &(p_data_tbl->type), TYPE_IDX_IN_MAIL_RULE_TBL);
7566 _get_stmt_field_data_string(hStmt, &(p_data_tbl->value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7567 _get_stmt_field_data_string(hStmt, &(p_data_tbl->value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7568 _get_stmt_field_data_int(hStmt, &(p_data_tbl->action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7569 _get_stmt_field_data_int(hStmt, &(p_data_tbl->target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7570 _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7571 _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7580 if (hStmt != NULL) {
7581 rc = sqlite3_finalize(hStmt);
7582 if (rc != SQLITE_OK) {
7583 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7584 error = EMAIL_ERROR_DB_FAILURE;
7588 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7590 if (err_code != NULL)
7593 EM_DEBUG_FUNC_END("ret [%d]", ret);
7597 INTERNAL_FUNC int emstorage_change_rule(char *multi_user_name, int rule_id, emstorage_rule_tbl_t* new_rule, int transaction, int *err_code)
7599 EM_DEBUG_FUNC_BEGIN("rule_id[%d], new_rule[%p], transaction[%d], err_code[%p]", rule_id, new_rule, transaction, err_code);
7601 if (!new_rule) { /* only global rule supported. */
7602 EM_DEBUG_EXCEPTION("rule_id[%d], new_rule[%p]", rule_id, new_rule);
7604 if (err_code != NULL)
7605 *err_code = EMAIL_ERROR_INVALID_PARAM;
7609 int rc, ret = false;
7610 int error = EMAIL_ERROR_NONE;
7612 DB_STMT hStmt = NULL;
7613 char sql_query_string[QUERY_SIZE] = {0, };
7614 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7615 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7617 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7618 "UPDATE mail_rule_tbl SET"
7624 ", target_mailbox_id = ?"
7629 " WHERE rule_id = %d"
7633 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7634 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
7635 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7636 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7640 _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->filter_name, 0, FILTER_NAME_LEN_IN_MAIL_RULE_TBL);
7641 _bind_stmt_field_data_int(hStmt, i++, new_rule->type);
7642 _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7643 _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7644 _bind_stmt_field_data_int(hStmt, i++, new_rule->action_type);
7645 _bind_stmt_field_data_int(hStmt, i++, new_rule->target_mailbox_id);
7646 _bind_stmt_field_data_int(hStmt, i++, new_rule->flag1);
7647 _bind_stmt_field_data_int(hStmt, i++, new_rule->flag2);
7648 _bind_stmt_field_data_int(hStmt, i++, new_rule->account_id);
7649 _bind_stmt_field_data_int(hStmt, i++, rule_id);
7652 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7653 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7654 ("sqlite3_step fail:%d", rc));
7659 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7660 if (hStmt != NULL) {
7661 rc = sqlite3_finalize(hStmt);
7662 if (rc != SQLITE_OK) {
7663 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7664 error = EMAIL_ERROR_DB_FAILURE;
7668 if (err_code != NULL)
7671 EM_DEBUG_FUNC_END("ret [%d]", ret);
7675 INTERNAL_FUNC int emstorage_find_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7677 EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7680 EM_DEBUG_LOG("rule is NULL");
7681 if (err_code != NULL)
7682 *err_code = EMAIL_ERROR_INVALID_PARAM;
7686 DB_STMT hStmt = NULL;
7687 char sql_query_string[QUERY_SIZE] = {0,};
7688 int error = EMAIL_ERROR_NONE;
7692 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7693 EMSTORAGE_START_READ_TRANSACTION(transaction);
7695 switch (rule->action_type) {
7696 case EMAIL_FILTER_MOVE:
7697 if (rule->type == EMAIL_PRIORITY_SENDER) {
7698 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7699 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7700 rule->action_type, rule->type, rule->value2);
7702 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7703 "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);
7706 case EMAIL_FILTER_BLOCK:
7707 if (rule->type == EMAIL_FILTER_FROM)
7708 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7709 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7710 rule->action_type, rule->type, rule->value2);
7711 else if (rule->type == EMAIL_FILTER_SUBJECT)
7712 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7713 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value) = UPPER(\'%q\')",
7714 rule->action_type, rule->type, rule->value);
7715 else if (rule->type == (EMAIL_FILTER_SUBJECT | EMAIL_FILTER_FROM))
7716 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7717 "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\'))",
7718 rule->action_type, EMAIL_FILTER_SUBJECT, rule->value, EMAIL_FILTER_FROM, rule->value2);
7722 EM_DEBUG_EXCEPTION("Invalid parameter : rule->action_type[%d]", rule->action_type);
7723 error = EMAIL_ERROR_INVALID_PARAM;
7728 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7729 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7730 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7732 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7733 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7734 ("sqlite3_step fail:%d", rc));
7736 if (rc == SQLITE_DONE) {
7737 EM_DEBUG_EXCEPTION(" no matched rule found...");
7738 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7745 if (hStmt != NULL) {
7746 rc = sqlite3_finalize(hStmt);
7747 if (rc != SQLITE_OK) {
7748 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7749 error = EMAIL_ERROR_DB_FAILURE;
7753 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7758 EM_DEBUG_FUNC_END("ret [%d]", ret);
7762 INTERNAL_FUNC int emstorage_add_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7764 EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7766 if (!rule) { /* only global rule supported. */
7767 EM_DEBUG_LOG("rule is NULL");
7768 if (err_code != NULL)
7769 *err_code = EMAIL_ERROR_INVALID_PARAM;
7773 int rc, rc_2, ret = false;
7774 int error = EMAIL_ERROR_NONE;
7775 DB_STMT hStmt = NULL;
7776 char sql_query_string[QUERY_SIZE] = {0, };
7778 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7780 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7784 sql = "SELECT max(rowid) FROM mail_rule_tbl;";
7786 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7787 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7788 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7790 if (NULL == result[1])
7793 rc = atoi(result[1])+1;
7795 sqlite3_free_table(result);
7799 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7800 "INSERT INTO mail_rule_tbl VALUES "
7801 "(?" /* account id */
7803 ", ?" /* filter_name */
7807 ", ?" /* action_type */
7808 ", ?" /* target_mailbox_id */
7810 ", ?)"); /* flag2 */
7812 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc_2);
7813 if (rc_2 != SQLITE_OK) {
7814 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc_2, sqlite3_errmsg(local_db_handle));
7815 error = EMAIL_ERROR_DB_FAILURE;
7819 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL, rule->account_id);
7820 _bind_stmt_field_data_int(hStmt, RULE_ID_IDX_IN_MAIL_RULE_TBL, rule->rule_id);
7821 _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);
7822 _bind_stmt_field_data_int(hStmt, TYPE_IDX_IN_MAIL_RULE_TBL, rule->type);
7823 _bind_stmt_field_data_string(hStmt, VALUE_IDX_IN_MAIL_RULE_TBL, (char*)rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7824 _bind_stmt_field_data_string(hStmt, VALUE2_IDX_IN_MAIL_RULE_TBL, (char*)rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7825 _bind_stmt_field_data_int(hStmt, ACTION_TYPE_IDX_IN_MAIL_RULE_TBL, rule->action_type);
7826 _bind_stmt_field_data_int(hStmt, TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL, rule->target_mailbox_id);
7827 _bind_stmt_field_data_int(hStmt, FLAG1_IDX_IN_MAIL_RULE_TBL, rule->flag1);
7828 _bind_stmt_field_data_int(hStmt, FLAG2_IDX_IN_MAIL_RULE_TBL, rule->flag2);
7830 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7831 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7832 ("sqlite3_step fail:%d", rc));
7837 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7838 if (hStmt != NULL) {
7839 rc = sqlite3_finalize(hStmt);
7840 if (rc != SQLITE_OK) {
7841 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7842 error = EMAIL_ERROR_DB_FAILURE;
7846 if (err_code != NULL)
7849 EM_DEBUG_FUNC_END("ret [%d]", ret);
7853 INTERNAL_FUNC int emstorage_delete_rule(char *multi_user_name, int rule_id, int transaction, int *err_code)
7855 EM_DEBUG_FUNC_BEGIN("rule_id[%d], transaction[%d], err_code[%p]", rule_id, transaction, err_code);
7857 if (rule_id <= 0) { /* only global rule supported. */
7858 EM_DEBUG_EXCEPTION("rule_id[%d]", rule_id);
7860 if (err_code != NULL)
7861 *err_code = EMAIL_ERROR_INVALID_PARAM;
7865 int rc, ret = false;
7866 int error = EMAIL_ERROR_NONE;
7867 char sql_query_string[QUERY_SIZE] = {0, };
7868 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7869 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7871 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7872 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7873 if (error != EMAIL_ERROR_NONE) {
7874 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7878 rc = sqlite3_changes(local_db_handle);
7880 EM_DEBUG_EXCEPTION(" no matched rule found...");
7882 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7889 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7891 if (err_code != NULL)
7894 EM_DEBUG_FUNC_END("ret [%d]", ret);
7898 INTERNAL_FUNC int emstorage_free_rule(emstorage_rule_tbl_t** rule_list, int count, int *err_code)
7900 EM_DEBUG_FUNC_BEGIN("rule_list[%p], conut[%d], err_code[%p]", rule_list, count, err_code);
7903 int error = EMAIL_ERROR_NONE;
7906 if (!rule_list || !*rule_list) {
7907 EM_DEBUG_EXCEPTION(" rule_list[%p], conut[%d]", rule_list, count);
7909 error = EMAIL_ERROR_INVALID_PARAM;
7913 emstorage_rule_tbl_t* p = *rule_list;
7916 for (; i < count; i++)
7917 EM_SAFE_FREE(p[i].value);
7919 EM_SAFE_FREE(p); *rule_list = NULL;
7923 if (err_code != NULL)
7926 EM_DEBUG_FUNC_END("ret [%d]", ret);
7930 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)
7932 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);
7934 if (!total && !unseen) {
7935 EM_DEBUG_EXCEPTION(" accoun_id[%d], mailbox_id[%d], total[%p], unseen[%p]", account_id, mailbox_id, total, unseen);
7936 if (err_code != NULL)
7937 *err_code = EMAIL_ERROR_INVALID_PARAM;
7941 int rc = -1, ret = false;
7942 int error = EMAIL_ERROR_NONE;
7943 DB_STMT hStmt = NULL;
7944 char sql_query_string[QUERY_SIZE] = {0, };
7945 char *replaced_mailbox_name = NULL;
7947 memset(&sql_query_string, 0x00, sizeof(sql_query_string));
7948 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7949 EMSTORAGE_START_READ_TRANSACTION(transaction);
7952 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl");
7954 if (account_id != ALL_ACCOUNT) {
7955 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);
7957 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);
7958 } else if (mailbox_id)
7959 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);
7961 #ifdef USE_GET_RECORD_COUNT_API
7964 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7965 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF2; },
7966 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7968 *total = atoi(result[1]);
7969 sqlite3_free_table(result);
7972 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7973 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF2; },
7974 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7976 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7977 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF2; },
7978 ("sqlite3_step fail:%d", rc));
7979 _get_stmt_field_data_int(hStmt, total, 0);
7980 #endif /* USE_GET_RECORD_COUNT_API */
7984 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl WHERE flags_seen_field = 0"); /* fSEEN = 0x01 */
7986 if (account_id != ALL_ACCOUNT) {
7987 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7988 sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND account_id = %d", account_id);
7992 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7993 sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND mailbox_id = %d", mailbox_id);
7995 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7996 sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND mailbox_type NOT IN (3, 5)");
7999 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
8000 EM_DEBUG_DB_EXEC(SQLITE_OK != rc,
8001 { error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
8002 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8004 *unseen = atoi(result[1]);
8005 sqlite3_free_table(result);
8013 #ifndef USE_PREPARED_QUERY_
8014 if (hStmt != NULL) {
8015 rc = sqlite3_finalize(hStmt);
8016 if (rc != SQLITE_OK) {
8017 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8018 error = EMAIL_ERROR_DB_FAILURE;
8023 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8025 EM_SAFE_FREE(replaced_mailbox_name);
8027 if (err_code != NULL)
8030 EM_DEBUG_FUNC_END("ret [%d]", ret);
8034 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)
8036 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
8038 if (mail_id <= 0 || !mail) {
8039 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
8040 if (err_code != NULL)
8041 *err_code = EMAIL_ERROR_INVALID_PARAM;
8046 emstorage_mail_tbl_t* p_data_tbl = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
8048 if (p_data_tbl == NULL) {
8049 EM_DEBUG_EXCEPTION("malloc failed...");
8050 if (err_code != NULL)
8051 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8055 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8056 DB_STMT hStmt = NULL;
8057 char sql_query_string[QUERY_SIZE] = {0, };
8059 int rc, ret = false;
8060 int error = EMAIL_ERROR_NONE;
8062 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8063 EMSTORAGE_START_READ_TRANSACTION(transaction);
8066 case RETRIEVE_SUMMARY:
8067 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8068 "SELECT account_id, "
8071 "server_mail_status, "
8072 "server_mailbox_name, "
8076 "file_path_mime_entity, "
8077 "flags_seen_field, "
8081 "thread_item_count "
8082 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8085 case RETRIEVE_FIELDS_FOR_DELETE:
8086 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8087 "SELECT account_id, "
8089 "server_mail_status, "
8090 "server_mailbox_name, "
8092 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8095 case RETRIEVE_ACCOUNT:
8096 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8097 "SELECT account_id "
8098 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8102 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8103 "SELECT account_id, "
8104 "flags_seen_field, "
8107 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8111 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
8112 error = EMAIL_ERROR_INVALID_PARAM;
8116 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
8118 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8119 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8120 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8123 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8124 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8125 ("sqlite3_step fail:%d", rc));
8127 if (rc == SQLITE_DONE) {
8128 EM_DEBUG_LOG("no matched mail found...");
8129 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8133 case RETRIEVE_SUMMARY:
8134 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8135 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
8136 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
8137 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
8138 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
8139 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8140 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, col_index++);
8141 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, col_index++);
8142 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, col_index++);
8143 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8144 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), col_index++);
8145 _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), col_index++);
8146 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8147 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_item_count), col_index++);
8150 case RETRIEVE_FIELDS_FOR_DELETE:
8151 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8152 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
8153 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
8154 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
8155 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8158 case RETRIEVE_ACCOUNT:
8159 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8163 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8164 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8165 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8166 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
8175 else if (p_data_tbl != NULL)
8176 emstorage_free_mail(&p_data_tbl, 1, NULL);
8178 if (hStmt != NULL) {
8179 rc = sqlite3_finalize(hStmt);
8180 if (rc != SQLITE_OK) {
8181 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8182 error = EMAIL_ERROR_DB_FAILURE;
8187 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8189 if (err_code != NULL)
8192 EM_DEBUG_FUNC_END("ret [%d]", ret);
8196 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)
8198 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);
8201 int error = EMAIL_ERROR_NONE;
8202 int query_string_length = 0;
8203 int i = 0, item_count = 0, rc = -1, field_count, col_index, cur_sql_query_string = 0;
8204 char **result = NULL;
8205 char *sql_query_string = NULL;
8206 emstorage_mail_tbl_t* p_data_tbl = NULL;
8207 sqlite3 *local_db_handle = NULL;
8209 if (number_of_mails <= 0 || !mail_ids) {
8210 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8211 if (err_code != NULL)
8212 *err_code = EMAIL_ERROR_INVALID_PARAM;
8216 p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * number_of_mails);
8218 query_string_length = (sizeof(char) * 8 * number_of_mails) + 512;
8219 sql_query_string = (char*)em_malloc(query_string_length);
8221 if (p_data_tbl == NULL || sql_query_string == NULL) {
8222 EM_DEBUG_EXCEPTION("malloc failed...");
8224 EM_SAFE_FREE(p_data_tbl);
8225 EM_SAFE_FREE(sql_query_string);
8227 if (err_code != NULL)
8228 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8232 local_db_handle = emstorage_get_db_connection(multi_user_name);
8234 EMSTORAGE_START_READ_TRANSACTION(transaction);
8237 case RETRIEVE_SUMMARY:
8238 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8239 "SELECT account_id, "
8242 "server_mail_status, "
8243 "server_mailbox_name, "
8247 "file_path_mime_entity, "
8249 "flags_seen_field, "
8253 "thread_item_count "
8254 "FROM mail_tbl WHERE mail_id in (");
8258 case RETRIEVE_FIELDS_FOR_DELETE:
8259 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8260 "SELECT account_id, "
8262 "server_mail_status, "
8263 "server_mailbox_name, "
8265 "FROM mail_tbl WHERE mail_id in (");
8269 case RETRIEVE_ACCOUNT:
8270 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8271 "SELECT account_id FROM mail_tbl WHERE mail_id in (");
8276 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8277 "SELECT account_id, "
8280 "flags_seen_field, "
8282 "FROM mail_tbl WHERE mail_id in (");
8287 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
8288 error = EMAIL_ERROR_INVALID_PARAM;
8292 for (i = 0; i < number_of_mails; i++)
8293 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_string_length, "%d,", mail_ids[i]);
8294 sql_query_string[EM_SAFE_STRLEN(sql_query_string) - 1] = ')';
8296 EM_DEBUG_LOG_SEC("Query [%s], Length [%zu]", sql_query_string, EM_SAFE_STRLEN(sql_query_string));
8298 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &item_count, 0, NULL), rc);
8299 if (SQLITE_OK != rc && -1 != rc) {
8300 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
8301 error = EMAIL_ERROR_DB_FAILURE;
8304 EM_DEBUG_LOG("item_count [%d]", item_count);
8306 if (number_of_mails != item_count) {
8307 EM_DEBUG_EXCEPTION("Can't find all emails");
8308 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8312 col_index = field_count;
8314 for (i = 0; i < item_count; i++) {
8316 case RETRIEVE_SUMMARY:
8317 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8318 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8319 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8320 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8321 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8322 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8323 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
8324 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
8325 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
8326 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 0, col_index++);
8327 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8328 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
8329 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
8330 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8331 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
8334 case RETRIEVE_FIELDS_FOR_DELETE:
8335 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8336 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8337 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8338 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8339 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8342 case RETRIEVE_ACCOUNT:
8343 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8347 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8348 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8349 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8350 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8351 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8362 emstorage_free_mail(&p_data_tbl, number_of_mails, NULL);
8365 sqlite3_free_table(result);
8367 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8369 EM_SAFE_FREE(sql_query_string);
8371 if (err_code != NULL)
8374 EM_DEBUG_FUNC_END("ret [%d]", ret);
8378 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)
8380 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8382 if (mail_id <= 0 || !mail) {
8383 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
8384 if (err_code != NULL)
8385 *err_code = EMAIL_ERROR_INVALID_PARAM;
8389 int ret = false, error = EMAIL_ERROR_NONE, count;
8390 char conditional_clause[QUERY_SIZE] = {0, };
8391 emstorage_mail_tbl_t* p_data_tbl = NULL;
8393 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8394 EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8396 if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8397 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8406 else if (p_data_tbl != NULL)
8407 emstorage_free_mail(&p_data_tbl, 1, &error);
8409 if (err_code != NULL)
8412 EM_DEBUG_FUNC_END("ret [%d]", ret);
8416 #ifdef __FEATURE_BODY_SEARCH__
8417 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)
8419 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
8421 if (mail_id <= 0 || !mail_text) {
8422 EM_DEBUG_EXCEPTION("mail_id[%d], mail_text[%p]", mail_id, mail_text);
8423 if (err_code != NULL)
8424 *err_code = EMAIL_ERROR_INVALID_PARAM;
8429 int error = EMAIL_ERROR_NONE;
8431 char conditional_clause[QUERY_SIZE] = {0, };
8432 emstorage_mail_text_tbl_t *p_data_tbl = NULL;
8434 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8435 EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8437 if (!emstorage_query_mail_text_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8438 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8446 *mail_text = p_data_tbl;
8447 else if (p_data_tbl != NULL)
8448 emstorage_free_mail_text(&p_data_tbl, 1, &error);
8450 if (err_code != NULL)
8453 EM_DEBUG_FUNC_END("ret [%d]", ret);
8458 INTERNAL_FUNC int emstorage_mail_search_start(char *multi_user_name,
8459 emstorage_search_filter_t *search,
8463 DB_STMT *search_handle,
8468 EM_DEBUG_FUNC_BEGIN("search[%p], account_id[%d], mailbox_id[%d], sorting[%d], "
8469 "search_handle[%p], searched[%p], transaction[%d], err_code[%p]",
8470 search, account_id, mailbox_id, sorting, search_handle,
8471 searched, transaction, err_code);
8473 if (!search_handle || !searched) {
8474 if (err_code != NULL)
8475 *err_code = EMAIL_ERROR_INVALID_PARAM;
8476 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8477 EM_DEBUG_FUNC_END("false");
8481 emstorage_search_filter_t* p = search;
8482 int error = EMAIL_ERROR_NONE;
8483 DB_STMT hStmt = NULL;
8484 char sql_query_string[QUERY_SIZE] = {0, };
8485 int rc, ret = false;
8486 int and = false, mail_count = 0;
8488 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8489 EMSTORAGE_START_READ_TRANSACTION(transaction);
8491 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl");
8493 if (account_id != ALL_ACCOUNT) {
8494 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);
8499 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);
8505 if (!strncmp(p->key_type, "subject", strlen("subject"))) {
8506 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);
8508 } else if (!strncmp(p->key_type, "full_address_from", strlen("full_address_from"))) {
8509 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);
8511 } else if (!strncmp(p->key_type, "full_address_to", strlen("full_address_to"))) {
8512 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);
8514 } else if (!strncmp(p->key_type, "email_address", strlen("email_address"))) {
8515 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);
8523 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");
8525 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
8528 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8529 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8530 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8533 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8534 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8535 ("sqlite3_step fail:%d", rc));
8539 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &mail_count, NULL, NULL), rc);
8540 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
8541 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8543 sqlite3_free_table(result);
8549 *search_handle = hStmt;
8550 *searched = mail_count;
8551 EM_DEBUG_LOG("mail_count [%d]", mail_count);
8553 if (hStmt != NULL) {
8554 rc = sqlite3_finalize(hStmt);
8555 if (rc != SQLITE_OK) {
8556 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8557 error = EMAIL_ERROR_DB_FAILURE;
8561 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8564 if (err_code != NULL)
8567 EM_DEBUG_FUNC_END("ret [%d]", ret);
8571 INTERNAL_FUNC int emstorage_mail_search_result(DB_STMT search_handle, emstorage_mail_field_type_t type, void** data, int transaction, int *err_code)
8573 EM_DEBUG_FUNC_BEGIN("search_handle[%d], type[%d], data[%p], transaction[%d], err_code[%p]", search_handle, type, data, transaction, err_code);
8575 if (search_handle == 0 || !data) {
8576 EM_DEBUG_EXCEPTION("No Search_handle type[%d], data[%p]", type, data);
8578 if (err_code != NULL)
8579 *err_code = EMAIL_ERROR_INVALID_PARAM;
8583 emstorage_mail_tbl_t* p_data_tbl = NULL;
8584 DB_STMT hStmt = search_handle;
8585 int rc, ret = false;
8586 int error = EMAIL_ERROR_NONE;
8590 _get_stmt_field_data_int(hStmt, (int *)data, MAIL_ID_IDX_IN_MAIL_TBL);
8593 case RETRIEVE_ENVELOPE:
8595 if (!(p_data_tbl = em_malloc(sizeof(emstorage_mail_tbl_t)))) {
8596 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
8597 error = EMAIL_ERROR_OUT_OF_MEMORY;
8601 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8602 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8603 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_size), MAIL_SIZE_IDX_IN_MAIL_TBL);
8604 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8605 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_from), 1, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL);
8606 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_to), 1, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL);
8607 _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject), 1, SUBJECT_IDX_IN_MAIL_TBL);
8608 _get_stmt_field_data_int(hStmt, &(p_data_tbl->body_download_status), BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL);
8609 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL);
8610 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8611 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8612 _get_stmt_field_data_time_t(hStmt, &(p_data_tbl->date_time), DATETIME_IDX_IN_MAIL_TBL);
8613 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL);
8614 _get_stmt_field_data_int(hStmt, &(p_data_tbl->DRM_status), DRM_STATUS_IDX_IN_MAIL_TBL);
8615 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->priority), PRIORITY_IDX_IN_MAIL_TBL);
8616 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), SAVE_STATUS_IDX_IN_MAIL_TBL);
8617 _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), LOCK_STATUS_IDX_IN_MAIL_TBL);
8618 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->report_status), REPORT_STATUS_IDX_IN_MAIL_TBL);
8619 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8620 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->meeting_request_status), MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL);
8621 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->message_class), MESSAGE_CLASS_IDX_IN_MAIL_TBL);
8622 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->digest_type), DIGEST_TYPE_IDX_IN_MAIL_TBL);
8623 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->smime_type), SMIME_TYPE_IDX_IN_MAIL_TBL);
8624 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->scheduled_sending_time), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8625 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->remaining_resend_times), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8626 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->tag_id), TAG_ID_IDX_IN_MAIL_TBL);
8627 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->eas_data_length), EAS_DATA_LENGTH_IDX_IN_MAIL_TBL);
8628 _get_stmt_field_data_blob(hStmt, (void**)&(p_data_tbl->eas_data), EAS_DATA_IDX_IN_MAIL_TBL);
8630 if (type == RETRIEVE_ALL) {
8631 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8632 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8633 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_reply), 1, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL);
8634 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_cc), 1, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL);
8635 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_bcc), 1, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL);
8636 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_return), 1, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL);
8637 _get_stmt_field_data_string(hStmt, &(p_data_tbl->message_id), 0, MESSAGE_ID_IDX_IN_MAIL_TBL);
8638 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8639 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8640 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_count), ATTACHMENT_COUNT_IDX_IN_MAIL_TBL);
8641 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8642 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->replied_time), REPLIED_TIME_IDX_IN_MAIL_TBL);
8643 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->forwarded_time), FORWARDED_TIME_IDX_IN_MAIL_TBL);
8644 _get_stmt_field_data_string(hStmt, &(p_data_tbl->default_charset), 0, DEFAULT_CHARSET_IDX_IN_MAIL_TBL);
8647 if (p_data_tbl->body_download_status) {
8650 if (p_data_tbl->file_path_html) {
8651 if (stat(p_data_tbl->file_path_html, &buf) == -1)
8652 p_data_tbl->body_download_status = 0;
8653 } else if (p_data_tbl->file_path_plain) {
8654 if (stat(p_data_tbl->file_path_plain, &buf) == -1)
8655 p_data_tbl->body_download_status = 0;
8657 p_data_tbl->body_download_status = 0;
8660 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8663 case RETRIEVE_SUMMARY:
8664 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
8665 EM_DEBUG_EXCEPTION(" malloc failed...");
8667 error = EMAIL_ERROR_OUT_OF_MEMORY;
8671 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8673 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8674 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8675 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8676 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8677 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8679 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8682 case RETRIEVE_ADDRESS:
8683 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
8684 EM_DEBUG_EXCEPTION(" malloc failed...");
8685 error = EMAIL_ERROR_OUT_OF_MEMORY;
8689 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8690 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8691 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8692 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8693 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8700 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8701 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8702 ("sqlite3_step fail:%d", rc));
8708 if (err_code != NULL)
8711 if (ret == false && p_data_tbl)
8712 emstorage_free_mail(&p_data_tbl, 1, NULL);
8714 EM_DEBUG_FUNC_END("ret [%d]", ret);
8718 INTERNAL_FUNC int emstorage_mail_search_end(DB_STMT search_handle, int transaction, int *err_code)
8720 EM_DEBUG_FUNC_BEGIN("search_handle[%d], transaction[%d], err_code[%p]", search_handle, transaction, err_code);
8722 int error = EMAIL_ERROR_NONE;
8723 int rc, ret = false;
8725 if (search_handle == 0) {
8726 EM_DEBUG_EXCEPTION("No search_handle");
8727 error = EMAIL_ERROR_INVALID_PARAM;
8731 DB_STMT hStmt = search_handle;
8733 rc = sqlite3_finalize(hStmt);
8734 if (rc != SQLITE_OK) {
8735 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8736 error = EMAIL_ERROR_DB_FAILURE;
8742 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8744 if (err_code != NULL)
8747 EM_DEBUG_FUNC_END("ret [%d]", ret);
8751 INTERNAL_FUNC int emstorage_change_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t *mail, int transaction, int *err_code)
8753 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8755 if (mail_id <= 0 || !mail) {
8756 EM_DEBUG_EXCEPTION(" mail_id[%d], mail[%p]", mail_id, mail);
8758 if (err_code != NULL)
8759 *err_code = EMAIL_ERROR_INVALID_PARAM;
8763 DB_STMT hStmt = NULL;
8764 char sql_query_string[QUERY_SIZE] = {0, };
8767 int error = EMAIL_ERROR_NONE;
8769 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8770 char mailbox_id_param_string[10] = {0,};
8772 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
8774 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8775 "UPDATE mail_tbl SET"
8780 ", server_mail_status = ?"
8781 ", server_mailbox_name = ?"
8782 ", server_mail_id = ?"
8783 ", reference_mail_id = ?"
8784 ", full_address_from = ?"
8785 ", full_address_reply = ?" /* 10 */
8786 ", full_address_to = ?"
8787 ", full_address_cc = ?"
8788 ", full_address_bcc = ?"
8789 ", full_address_return = ?"
8791 ", body_download_status = ?"
8792 ", file_path_plain = ?"
8793 ", file_path_html = ?"
8794 ", file_path_mime_entity = ?"
8796 ", flags_seen_field = ?"
8797 ", flags_deleted_field = ?"
8798 ", flags_flagged_field = ?"
8799 ", flags_answered_field = ?"
8800 ", flags_recent_field = ?"
8801 ", flags_draft_field = ?"
8802 ", flags_forwarded_field = ?"
8808 ", report_status = ?"
8809 ", preview_text = ?"
8811 ", scheduled_sending_time = ?"
8812 ", remaining_resend_times = ?"
8814 ", replied_time = ?"
8815 ", forwarded_time = ?"
8816 ", default_charset = ?"
8817 ", eas_data_length = ?"
8819 " WHERE mail_id = %d AND account_id != 0 "
8823 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8824 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8825 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8827 _bind_stmt_field_data_int(hStmt, i++, mail->mail_id);
8828 _bind_stmt_field_data_int(hStmt, i++, mail->account_id);
8829 _bind_stmt_field_data_int(hStmt, i++, mail->mailbox_id);
8830 _bind_stmt_field_data_int(hStmt, i++, mail->mail_size);
8831 _bind_stmt_field_data_int(hStmt, i++, mail->server_mail_status);
8832 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mailbox_name, 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
8833 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mail_id, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
8834 _bind_stmt_field_data_int(hStmt, i++, mail->reference_mail_id);
8835 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
8836 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
8837 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
8838 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
8839 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
8840 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
8841 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8842 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
8843 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8844 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8845 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
8846 _bind_stmt_field_data_int(hStmt, i++, mail->date_time);
8847 _bind_stmt_field_data_char(hStmt, i++, mail->flags_seen_field);
8848 _bind_stmt_field_data_char(hStmt, i++, mail->flags_deleted_field);
8849 _bind_stmt_field_data_char(hStmt, i++, mail->flags_flagged_field);
8850 _bind_stmt_field_data_char(hStmt, i++, mail->flags_answered_field);
8851 _bind_stmt_field_data_char(hStmt, i++, mail->flags_recent_field);
8852 _bind_stmt_field_data_char(hStmt, i++, mail->flags_draft_field);
8853 _bind_stmt_field_data_char(hStmt, i++, mail->flags_forwarded_field);
8854 _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
8855 _bind_stmt_field_data_int(hStmt, i++, mail->priority);
8856 _bind_stmt_field_data_int(hStmt, i++, mail->save_status);
8857 _bind_stmt_field_data_int(hStmt, i++, mail->lock_status);
8858 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->message_id, 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
8859 _bind_stmt_field_data_int(hStmt, i++, mail->report_status);
8860 _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8861 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
8862 _bind_stmt_field_data_int(hStmt, i++, mail->scheduled_sending_time);
8863 _bind_stmt_field_data_int(hStmt, i++, mail->remaining_resend_times);
8864 _bind_stmt_field_data_int(hStmt, i++, mail->tag_id);
8865 _bind_stmt_field_data_int(hStmt, i++, mail->replied_time);
8866 _bind_stmt_field_data_int(hStmt, i++, mail->forwarded_time);
8867 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
8868 _bind_stmt_field_data_int(hStmt, i++, mail->eas_data_length);
8869 _bind_stmt_field_data_blob(hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
8871 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8872 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8873 ("sqlite3_step fail:%d", rc));
8875 rc = sqlite3_changes(local_db_handle);
8877 EM_DEBUG_LOG(" no matched mail found...");
8878 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8885 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
8886 if (hStmt != NULL) {
8887 rc = sqlite3_finalize(hStmt);
8888 if (rc != SQLITE_OK) {
8889 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8890 error = EMAIL_ERROR_DB_FAILURE;
8895 if (error == EMAIL_ERROR_NONE && mail) {
8896 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
8897 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail->mail_id, mailbox_id_param_string, 0))
8898 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventfailed [NOTI_MAIL_UPDATE]");
8901 if (err_code != NULL)
8904 EM_DEBUG_FUNC_END("ret [%d]", ret);
8909 * emstorage_clean_save_status(int save_status, int *err_code) - set the all mail status to the set value
8913 INTERNAL_FUNC int emstorage_clean_save_status(char *multi_user_name, int save_status, int *err_code)
8915 EM_DEBUG_FUNC_BEGIN("save_status[%d], err_code[%p]", save_status, err_code);
8917 EM_IF_NULL_RETURN_VALUE(err_code, false);
8920 int error = EMAIL_ERROR_NONE;
8922 char sql_query_string[QUERY_SIZE] = {0, };
8923 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8925 memset(sql_query_string, 0x00, sizeof(sql_query_string));
8926 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);
8928 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
8929 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
8930 if (error != EMAIL_ERROR_NONE) {
8931 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
8935 rc = sqlite3_changes(local_db_handle);
8937 EM_DEBUG_LOG(" No Matched Mail Exists ");
8938 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8945 if (err_code != NULL)
8948 EM_DEBUG_FUNC_END("ret [%d]", ret);
8952 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)
8954 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);
8956 int error = EMAIL_ERROR_NONE;
8959 int cur_mail_id_string = 0;
8960 int mail_id_string_buffer_length = 0;
8961 int parameter_string_length = 0;
8962 char *sql_query_string = NULL;
8963 char *mail_id_string_buffer = NULL;
8964 char *parameter_string = NULL;
8965 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8966 email_mail_attribute_type target_mail_attribute_type = 0;
8968 if (!mail_ids || !field_name || account_id == 0) {
8969 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8970 if (err_code != NULL)
8971 *err_code = EMAIL_ERROR_INVALID_PARAM;
8975 if ((error = emcore_get_attribute_type_by_mail_field_name(field_name, &target_mail_attribute_type)) != EMAIL_ERROR_NONE) {
8976 EM_DEBUG_EXCEPTION("emstorageemcore_get_attribute_type_by_mail_field_name failed [%d]", error);
8977 if (err_code != NULL)
8982 /* Generating mail id list string */
8983 mail_id_string_buffer_length = MAIL_ID_STRING_LENGTH * mail_ids_count;
8985 mail_id_string_buffer = em_malloc(mail_id_string_buffer_length);
8987 if (!mail_id_string_buffer) {
8988 EM_DEBUG_EXCEPTION("em_mallocfailed");
8989 if (err_code != NULL)
8990 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8994 for (i = 0; i < mail_ids_count; i++)
8995 cur_mail_id_string += SNPRINTF_OFFSET(mail_id_string_buffer, cur_mail_id_string, mail_id_string_buffer_length, "%d,", mail_ids[i]);
8997 if (EM_SAFE_STRLEN(mail_id_string_buffer) > 1)
8998 mail_id_string_buffer[EM_SAFE_STRLEN(mail_id_string_buffer) - 1] = NULL_CHAR;
9000 /* Generating notification parameter string */
9001 parameter_string_length = mail_id_string_buffer_length + EM_SAFE_STRLEN(field_name) + 2;
9002 parameter_string = em_malloc(parameter_string_length);
9004 if (!parameter_string) {
9005 EM_DEBUG_EXCEPTION("em_mallocfailed");
9006 if (err_code != NULL)
9007 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
9008 EM_SAFE_FREE(mail_id_string_buffer);
9012 SNPRINTF(parameter_string, parameter_string_length, "%s%c%s", field_name, 0x01, mail_id_string_buffer);
9013 query_size = EM_SAFE_STRLEN(mail_id_string_buffer) + EM_SAFE_STRLEN(field_name) + 250;
9015 sql_query_string = em_malloc(query_size);
9016 if (sql_query_string == NULL) {
9017 EM_DEBUG_EXCEPTION("em_mallocfailed");
9018 error = EMAIL_ERROR_OUT_OF_MEMORY;
9021 /* Write query string */
9022 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);
9024 EM_DEBUG_LOG_DEV("sql_query_string [%s]", sql_query_string);
9027 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9028 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9029 if (error != EMAIL_ERROR_NONE) {
9030 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9034 if (sqlite3_changes(local_db_handle) == 0)
9035 EM_DEBUG_LOG("no mail matched...");
9040 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9042 if (error == EMAIL_ERROR_NONE && parameter_string) {
9043 if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, target_mail_attribute_type, parameter_string, value))
9044 EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_MAIL_FIELD_UPDATE [%s,%d]", field_name, value);
9047 EM_SAFE_FREE(mail_id_string_buffer);
9048 EM_SAFE_FREE(parameter_string);
9049 EM_SAFE_FREE(sql_query_string);
9052 if (err_code != NULL)
9055 EM_DEBUG_FUNC_END("error [%d]", error);
9059 #ifdef __FEATURE_BODY_SEARCH__
9060 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)
9062 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
9064 if (mail_id <= 0 || !mail_text) {
9065 EM_DEBUG_EXCEPTION(" mail_id[%d], mail_text[%p]", mail_id, mail_text);
9066 if (err_code != NULL)
9067 *err_code = EMAIL_ERROR_INVALID_PARAM;
9072 int error = EMAIL_ERROR_NONE;
9073 DB_STMT hStmt = NULL;
9074 char sql_query_string[QUERY_SIZE] = {0, };
9079 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9080 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9082 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9083 "UPDATE mail_text_tbl SET"
9085 " WHERE mail_id = %d AND account_id != 0"
9087 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9089 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9090 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9091 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9094 _bind_stmt_field_data_string(hStmt, i++, (char *)mail_text->body_text, 0, -1);
9096 if (hStmt != NULL) {
9097 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9098 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9099 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9101 rc = sqlite3_changes(local_db_handle);
9103 EM_DEBUG_LOG(" no matched mail found...");
9104 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9112 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9114 if (hStmt != NULL) {
9115 rc = sqlite3_finalize(hStmt);
9116 if (rc != SQLITE_OK) {
9117 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9118 error = EMAIL_ERROR_DB_FAILURE;
9123 if (err_code != NULL)
9126 EM_DEBUG_FUNC_END("ret [%d]", ret);
9131 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)
9133 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
9136 int error = EMAIL_ERROR_NONE;
9141 DB_STMT hStmt = NULL;
9142 char sql_query_string[QUERY_SIZE] = {0, };
9143 char mailbox_id_param_string[10] = {0,};
9144 sqlite3 *local_db_handle = NULL;
9146 if (mail_id <= 0 || !mail) {
9147 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], mail[%p]", mail_id, type, mail);
9148 if (err_code != NULL)
9149 *err_code = EMAIL_ERROR_INVALID_PARAM;
9153 local_db_handle = emstorage_get_db_connection(multi_user_name);
9155 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9159 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9160 "UPDATE mail_tbl SET"
9161 " body_download_status = ?"
9162 ", file_path_plain = ?"
9163 ", file_path_html = ?"
9164 ", file_path_mime_entity = ?"
9165 ", flags_seen_field = ?"
9166 ", flags_deleted_field = ?"
9167 ", flags_flagged_field = ?"
9168 ", flags_answered_field = ?"
9169 ", flags_recent_field = ?"
9170 ", flags_draft_field = ?"
9171 ", flags_forwarded_field = ?"
9173 ", attachment_count = ?"
9175 ", meeting_request_status = ? "
9176 ", message_class = ? "
9177 ", digest_type = ? "
9179 " WHERE mail_id = %d AND account_id != 0"
9183 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9184 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9185 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9188 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9189 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9190 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9191 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9192 _bind_stmt_field_data_int(hStmt, i++, mail->flags_seen_field);
9193 _bind_stmt_field_data_int(hStmt, i++, mail->flags_deleted_field);
9194 _bind_stmt_field_data_int(hStmt, i++, mail->flags_flagged_field);
9195 _bind_stmt_field_data_int(hStmt, i++, mail->flags_answered_field);
9196 _bind_stmt_field_data_int(hStmt, i++, mail->flags_recent_field);
9197 _bind_stmt_field_data_int(hStmt, i++, mail->flags_draft_field);
9198 _bind_stmt_field_data_int(hStmt, i++, mail->flags_forwarded_field);
9199 _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
9200 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9201 _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9202 _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
9203 _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
9204 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9205 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9208 case UPDATE_MAILBOX: {
9210 emstorage_mailbox_tbl_t *mailbox_tbl;
9212 if ((err = emstorage_get_mailbox_by_id(multi_user_name, mail->mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
9213 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_id failed [%d]", err);
9217 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9218 "UPDATE mail_tbl SET"
9219 " mailbox_id = '%d'"
9220 ",mailbox_type = '%d'"
9221 " WHERE mail_id = %d AND account_id != 0"
9222 , mailbox_tbl->mailbox_id
9223 , mailbox_tbl->mailbox_type
9228 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9229 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9230 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9233 _bind_stmt_field_data_string(hStmt, i++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
9235 emstorage_free_mailbox(&mailbox_tbl, 1, NULL); /*prevent 26251*/
9241 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9242 "UPDATE mail_tbl SET"
9243 " flags_seen_field = %d"
9244 ",flags_deleted_field = %d"
9245 ",flags_flagged_field = %d"
9246 ",flags_answered_field = %d"
9247 ",flags_recent_field = %d"
9248 ",flags_draft_field = %d"
9249 ",flags_forwarded_field = %d"
9250 " WHERE mail_id = %d AND account_id != 0"
9251 , mail->flags_seen_field
9252 , mail->flags_deleted_field
9253 , mail->flags_flagged_field
9254 , mail->flags_answered_field
9255 , mail->flags_recent_field
9256 , mail->flags_draft_field
9257 , mail->flags_forwarded_field
9259 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9262 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9263 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9264 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9268 case UPDATE_EXTRA_FLAG:
9269 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9270 "UPDATE mail_tbl SET"
9272 ", save_status = %d"
9273 ", lock_status = %d"
9274 ", report_status = %d"
9276 " WHERE mail_id = %d AND account_id != 0"
9280 , mail->report_status
9283 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9286 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9287 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9288 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9291 case UPDATE_STICKY_EXTRA_FLAG:
9292 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9293 "UPDATE mail_tbl SET"
9295 " WHERE mail_id = %d AND account_id != 0"
9298 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9301 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9302 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9303 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9307 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9308 "UPDATE mail_tbl SET"
9309 " full_address_from = ?"
9310 ", full_address_reply = ?"
9311 ", full_address_to = ?"
9312 ", full_address_cc = ?"
9313 ", full_address_bcc = ?"
9314 ", full_address_return = ?"
9316 ", file_path_plain = ?"
9318 ", flags_seen_field = ?"
9319 ", flags_deleted_field = ?"
9320 ", flags_flagged_field = ?"
9321 ", flags_answered_field = ?"
9322 ", flags_recent_field = ?"
9323 ", flags_draft_field = ?"
9324 ", flags_forwarded_field = ?"
9328 ", report_status = ?"
9330 ", file_path_html = ?"
9331 ", file_path_mime_entity = ?"
9333 ", preview_text = ?"
9334 ", body_download_status = ?"
9335 ", attachment_count = ?"
9336 ", inline_content_count = ?"
9337 ", meeting_request_status = ?"
9338 ", message_class = ?"
9341 ", scheduled_sending_time = ?"
9342 ", remaining_resend_times = ?"
9344 ", replied_time = ?"
9345 ", forwarded_time = ?"
9346 ", default_charset = ?"
9347 ", eas_data_length = ?"
9349 " WHERE mail_id = %d AND account_id != 0"
9353 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9354 EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
9355 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9356 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9358 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
9359 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
9360 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
9361 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
9362 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
9363 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
9364 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9365 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9366 _bind_stmt_field_data_time_t(hStmt, i++, mail->date_time);
9367 _bind_stmt_field_data_char(hStmt, i++, mail->flags_seen_field);
9368 _bind_stmt_field_data_char(hStmt, i++, mail->flags_deleted_field);
9369 _bind_stmt_field_data_char(hStmt, i++, mail->flags_flagged_field);
9370 _bind_stmt_field_data_char(hStmt, i++, mail->flags_answered_field);
9371 _bind_stmt_field_data_char(hStmt, i++, mail->flags_recent_field);
9372 _bind_stmt_field_data_char(hStmt, i++, mail->flags_draft_field);
9373 _bind_stmt_field_data_char(hStmt, i++, mail->flags_forwarded_field);
9374 _bind_stmt_field_data_int(hStmt, i++, mail->priority);
9375 _bind_stmt_field_data_int(hStmt, i++, mail->save_status);
9376 _bind_stmt_field_data_int(hStmt, i++, mail->lock_status);
9377 _bind_stmt_field_data_int(hStmt, i++, mail->report_status);
9378 _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
9379 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9380 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9381 _bind_stmt_field_data_int(hStmt, i++, mail->mail_size);
9382 _bind_stmt_field_data_nstring(hStmt, i++, (char*)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9383 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9384 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9385 _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
9386 _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
9387 _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
9388 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9389 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9390 _bind_stmt_field_data_int(hStmt, i++, mail->scheduled_sending_time);
9391 _bind_stmt_field_data_int(hStmt, i++, mail->remaining_resend_times);
9392 _bind_stmt_field_data_int(hStmt, i++, mail->tag_id);
9393 _bind_stmt_field_data_int(hStmt, i++, mail->replied_time);
9394 _bind_stmt_field_data_int(hStmt, i++, mail->forwarded_time);
9395 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
9396 _bind_stmt_field_data_int(hStmt, i++, mail->eas_data_length);
9397 _bind_stmt_field_data_blob(hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
9400 case UPDATE_DATETIME: {
9401 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9402 "UPDATE mail_tbl SET"
9403 " date_time = '%ld'"
9404 " WHERE mail_id = %d AND account_id != 0"
9408 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9409 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9410 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9414 case UPDATE_FROM_CONTACT_INFO:
9415 EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_FROM_CONTACT_INFO");
9416 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9417 "UPDATE mail_tbl SET"
9418 " email_address_sender = ?,"
9419 " WHERE mail_id = %d",
9424 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9425 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9426 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9428 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9431 case UPDATE_TO_CONTACT_INFO:
9432 EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_TO_CONTACT_INFO");
9433 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9434 "UPDATE mail_tbl SET"
9435 " email_address_recipient = ?,"
9436 " WHERE mail_id = %d",
9441 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9442 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9443 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9445 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9448 case UPDATE_ALL_CONTACT_INFO:
9449 EM_DEBUG_LOG("emstorage_change_mail_field - mail change type is UPDATE_ALL_CONTACT_INFO");
9450 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9451 "UPDATE mail_tbl SET"
9452 " email_address_sender = ?,"
9453 " email_address_recipient = ?,"
9454 " WHERE mail_id = %d",
9459 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9460 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9461 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9463 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9464 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9468 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
9469 case UPDATE_PARTIAL_BODY_DOWNLOAD:
9471 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9472 "UPDATE mail_tbl SET"
9473 " body_download_status = ?"
9474 ", file_path_plain = ?"
9475 ", file_path_html = ?"
9476 ", file_path_mime_entity = ?"
9477 ", attachment_count = ?"
9478 ", inline_content_count = ?"
9479 ", preview_text = ?"
9482 " WHERE mail_id = %d"
9486 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9487 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9488 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9491 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9492 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9493 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9494 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9495 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9496 _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
9497 _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9498 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9499 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9504 case UPDATE_FILE_PATH:
9505 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9506 "UPDATE mail_tbl SET"
9507 ", file_path_plain = ?"
9508 ", file_path_html = ?"
9509 ", file_path_mime_entity = ?"
9510 " WHERE mail_id = %d"
9514 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9515 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
9516 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9517 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9519 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9520 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9521 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9525 EM_DEBUG_LOG(" type[%d]", type);
9527 error = EMAIL_ERROR_INVALID_PARAM;
9531 if (hStmt != NULL) {
9533 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9534 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
9535 ("sqlite3_step fail:%d", rc));
9536 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9537 ("sqlite3_step fail:%d", rc));
9538 rc = sqlite3_changes(local_db_handle);
9540 EM_DEBUG_LOG(" no matched mail found...");
9541 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9546 if (mail->account_id == 0) {
9547 emstorage_mail_tbl_t* mail_for_account_tbl = NULL;
9548 if (!emstorage_get_mail_field_by_id(multi_user_name, mail_id, RETRIEVE_ACCOUNT, &mail_for_account_tbl, true, &error) || !mail_for_account_tbl) {
9549 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id error [%d]", error);
9552 mail->account_id = mail_for_account_tbl->account_id;
9553 if (mail_for_account_tbl)
9554 emstorage_free_mail(&mail_for_account_tbl, 1, NULL);
9560 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9562 if (hStmt != NULL) {
9563 rc = sqlite3_finalize(hStmt);
9564 if (rc != SQLITE_OK) {
9565 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9566 error = EMAIL_ERROR_DB_FAILURE;
9571 if (error == EMAIL_ERROR_NONE && move_flag != 1 && transaction) {
9572 if (!emstorage_get_mailbox_id_by_mailbox_type(multi_user_name, mail->account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &mailbox_id, false, &error))
9573 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_id_by_mailbox_type error [%d]", error);
9575 if (mail->mailbox_id == mailbox_id) {
9576 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
9577 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, mailbox_id_param_string, type))
9578 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventerror [ NOTI_MAIL_UPDATE ] >>>> ");
9580 /* h.gahlaut@samsung.com: Jan 10, 2011 Publishing noti to refresh outbox when email sending status changes */
9581 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, NULL, type))
9582 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventerror [ NOTI_MAIL_UPDATE ]");
9586 if (err_code != NULL)
9589 EM_DEBUG_FUNC_END("ret [%d]", ret);
9593 INTERNAL_FUNC int emstorage_increase_mail_id(char *multi_user_name, int *mail_id, int transaction, int *err_code)
9595 EM_DEBUG_FUNC_BEGIN("mail_id[%p], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9597 int rc, ret = false;
9598 int error = EMAIL_ERROR_NONE;
9599 int latest_mail_id = 0;
9600 sqlite3 *local_db_handle = NULL;
9601 char *sql = "SELECT MAX(mail_id) FROM mail_tbl;";
9602 char **result = NULL;
9604 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9605 _timedlock_shm_mutex(mapped_for_generating_mail_id, 2);
9606 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9608 ret = vconf_get_int(VCONF_KEY_LATEST_MAIL_ID, &latest_mail_id);
9609 if (ret < 0 || latest_mail_id == 0) {
9610 EM_DEBUG_LOG("vconf_get_int() failed [%d] or latest_mail_id is zero", ret);
9612 local_db_handle = emstorage_get_db_connection(multi_user_name);
9614 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9615 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
9616 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9617 if (NULL == result[1])
9620 rc = atoi(result[1]) + 1;
9622 sqlite3_free_table(result);
9623 latest_mail_id = rc;
9628 ret = vconf_set_int(VCONF_KEY_LATEST_MAIL_ID, latest_mail_id);
9631 *mail_id = latest_mail_id;
9637 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9638 _unlockshm_mutex(mapped_for_generating_mail_id);
9639 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9641 if (err_code != NULL)
9644 EM_DEBUG_FUNC_END("ret [%d]", ret);
9648 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)
9650 EM_PROFILE_BEGIN(profile_emstorage_add_mail);
9651 EM_DEBUG_FUNC_BEGIN("mail_tbl_data[%p], get_id[%d], transaction[%d], err_code[%p]", mail_tbl_data, get_id, transaction, err_code);
9653 if (!mail_tbl_data) {
9654 EM_DEBUG_EXCEPTION("mail_tbl_data[%p], get_id[%d]", mail_tbl_data, get_id);
9655 if (err_code != NULL)
9656 *err_code = EMAIL_ERROR_INVALID_PARAM;
9660 int rc, ret = false;
9661 int error = EMAIL_ERROR_NONE;
9662 char sql_query_string[QUERY_SIZE] = {0, };
9663 DB_STMT hStmt = NULL;
9665 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9667 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9670 /* increase unique id */
9671 char *sql = "SELECT max(rowid) FROM mail_tbl;";
9674 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9675 /* EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
9676 ("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle))); */
9677 if (rc != SQLITE_OK) {
9678 EM_DEBUG_EXCEPTION("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle));
9679 error = EMAIL_ERROR_DB_FAILURE;
9680 sqlite3_free_table(result);
9684 if (NULL == result[1])
9687 rc = atoi(result[1])+1;
9689 sqlite3_free_table(result);
9691 mail_tbl_data->mail_id = rc;
9692 mail_tbl_data->thread_id = rc;
9695 if (mail_tbl_data->date_time == 0)
9696 mail_tbl_data->date_time = time(NULL);
9698 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9699 "INSERT INTO mail_tbl VALUES "
9701 ", ?" /* account_id */
9702 ", ?" /* mailbox_id */
9703 ", ?" /* mailbox_type */
9706 ", ?" /* date_time */
9707 ", ?" /* server_mail_status */
9708 ", ?" /* server_mailbox_name */
9709 ", ?" /* server_mail_id */
9710 ", ?" /* message_id */
9712 ", ?" /* reference_mail_id */
9713 ", ?" /* full_address_from */
9714 ", ?" /* full_address_reply */
9715 ", ?" /* full_address_to */
9716 ", ?" /* full_address_cc */
9718 ", ?" /* full_address_bcc */
9719 ", ?" /* full_address_return */
9720 ", ?" /* email_address_sender */
9721 ", ?" /* email_address_recipient */
9722 ", ?" /* alias_sender */
9724 ", ?" /* alias_recipient */
9725 ", ?" /* body_download_status */
9726 ", ?" /* file_path_plain */
9727 ", ?" /* file_path_html */
9728 ", ?" /* file_path_mime_entity */
9730 ", ?" /* mail_size */
9731 ", ?" /* flags_seen_field */
9732 ", ?" /* flags_deleted_field */
9733 ", ?" /* flags_flagged_field */
9734 ", ?" /* flags_answered_field */
9736 ", ?" /* flags_recent_field */
9737 ", ?" /* flags_draft_field */
9738 ", ?" /* flags_forwarded_field */
9739 ", ?" /* DRM_status */
9740 ", ?" /* priority */
9742 ", ?" /* save_status */
9743 ", ?" /* lock_status */
9744 ", ?" /* report_status */
9745 ", ?" /* attachment_count */
9746 ", ?" /* inline_content_count */
9748 ", ?" /* thread_id */
9749 ", ?" /* thread_item_count */
9750 ", ?" /* preview_text */
9751 ", ?" /* meeting_request_status */
9752 ", ?" /* message_class */
9754 ", ?" /* digest_type */
9755 ", ?" /* smime_type */
9756 ", ?" /* scheduled_sending_time */
9757 ", ?" /* remaining_resend_times */
9760 ", ?" /* replied_time */
9761 ", ?" /* forwarded_time */
9762 ", ?" /* default charset */
9763 ", ?" /* eas_data_length */
9764 ", ?" /* eas_data */
9765 ", ?" /* user_name */
9768 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle,
9770 EM_SAFE_STRLEN(sql_query_string),
9774 if (rc != SQLITE_OK) {
9775 EM_DEBUG_EXCEPTION("sqlite3_prepare error [%d] [%s] SQL(%s) ",
9776 rc, sql_query_string, sqlite3_errmsg(local_db_handle));
9777 error = EMAIL_ERROR_DB_FAILURE;
9781 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mail_id);
9782 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, mail_tbl_data->account_id);
9783 _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_id);
9784 _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_type);
9785 _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9787 _bind_stmt_field_data_int(hStmt, DATETIME_IDX_IN_MAIL_TBL, mail_tbl_data->date_time);
9788 _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->server_mail_status);
9789 _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);
9790 _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);
9791 _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);
9793 _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, mail_tbl_data->reference_mail_id);
9794 _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);
9795 _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);
9796 _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);
9797 _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);
9799 _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);
9800 _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);
9801 _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);
9802 _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);
9803 _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);
9805 _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);
9806 _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->body_download_status);
9807 _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);
9808 _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);
9809 _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);
9811 _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, mail_tbl_data->mail_size);
9812 _bind_stmt_field_data_int(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_seen_field);
9813 _bind_stmt_field_data_int(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_deleted_field);
9814 _bind_stmt_field_data_int(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_flagged_field);
9815 _bind_stmt_field_data_int(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_answered_field);
9817 _bind_stmt_field_data_int(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_recent_field);
9818 _bind_stmt_field_data_int(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_draft_field);
9819 _bind_stmt_field_data_int(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_forwarded_field);
9820 _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->DRM_status);
9821 _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, mail_tbl_data->priority);
9823 _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->save_status);
9824 _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->lock_status);
9825 _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->report_status);
9826 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->attachment_count);
9827 _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->inline_content_count);
9829 _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, mail_tbl_data->thread_id);
9830 _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->thread_item_count);
9831 _bind_stmt_field_data_nstring(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9832 _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->meeting_request_status);
9833 _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, mail_tbl_data->message_class);
9835 _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->digest_type);
9836 _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->smime_type);
9837 _bind_stmt_field_data_int(hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->scheduled_sending_time);
9838 _bind_stmt_field_data_int(hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, mail_tbl_data->remaining_resend_times);
9839 _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, mail_tbl_data->tag_id);
9841 _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->replied_time);
9842 _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->forwarded_time);
9843 _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);
9844 _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, mail_tbl_data->eas_data_length);
9845 _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, (void*)mail_tbl_data->eas_data, mail_tbl_data->eas_data_length);
9846 _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);
9848 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9849 if (rc == SQLITE_FULL) {
9850 EM_DEBUG_EXCEPTION("sqlite3_step error [%d]", rc);
9851 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
9854 if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
9855 EM_DEBUG_EXCEPTION("sqlite3_step error [%d]", rc);
9856 error = EMAIL_ERROR_DB_FAILURE;
9862 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9864 if (hStmt != NULL) {
9865 rc = sqlite3_finalize(hStmt);
9866 if (rc != SQLITE_OK) {
9867 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9868 error = EMAIL_ERROR_DB_FAILURE;
9872 if (err_code != NULL)
9875 EM_PROFILE_END(profile_emstorage_add_mail);
9876 EM_DEBUG_FUNC_END("ret [%d]", ret);
9880 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)
9882 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);
9884 int ret = false, i, cur_conditional_clause = 0;
9885 int error = EMAIL_ERROR_NONE;
9886 int target_account_id;
9887 int conditional_clause_len = 0;
9888 char *sql_query_string = NULL, *conditional_clause = NULL;
9889 emstorage_mailbox_tbl_t *result_mailbox = NULL;
9890 email_mailbox_type_e target_mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED;
9891 char* target_mailbox_name = NULL;
9893 if (!mail_ids || input_mailbox_id <= 0) {
9894 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9895 if (err_code != NULL)
9896 *err_code = EMAIL_ERROR_INVALID_PARAM;
9900 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9902 if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &result_mailbox)) != EMAIL_ERROR_NONE || !result_mailbox) {
9903 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", error);
9904 if (err_code != NULL)
9909 if (result_mailbox->mailbox_name) {
9910 if (strstr(result_mailbox->mailbox_name, "'"))
9911 target_mailbox_name = em_replace_all_string(result_mailbox->mailbox_name, "'", "''");
9913 target_mailbox_name = strdup(result_mailbox->mailbox_name);
9916 target_mailbox_type = result_mailbox->mailbox_type;
9917 target_account_id = result_mailbox->account_id;
9918 emstorage_free_mailbox(&result_mailbox, 1, NULL);
9920 conditional_clause_len = (sizeof(char) * 8 * number_of_mails) + 512;
9921 conditional_clause = em_malloc(conditional_clause_len);
9922 if (conditional_clause == NULL) {
9923 EM_DEBUG_EXCEPTION("em_mallocfailed");
9924 error = EMAIL_ERROR_OUT_OF_MEMORY;
9927 cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE mail_id in (");
9929 for (i = 0; i < number_of_mails; i++)
9930 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
9933 char *last_comma = rindex(conditional_clause, ',');
9934 if (last_comma) *last_comma = ')'; /* replace , with) */
9936 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9938 /* Updating a mail_tbl */
9940 sql_query_string = em_malloc(conditional_clause_len);
9941 if (sql_query_string == NULL) {
9942 EM_DEBUG_EXCEPTION("em_mallocfailed");
9943 error = EMAIL_ERROR_OUT_OF_MEMORY;
9946 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);
9947 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9949 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9950 if (error != EMAIL_ERROR_NONE) {
9951 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9955 /* Updating a mail_attachment_tbl */
9956 memset(sql_query_string, 0x00, conditional_clause_len);
9957 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);
9958 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9959 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9960 if (error != EMAIL_ERROR_NONE) {
9961 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9965 /* Updating a mail_meeting_tbl */
9966 memset(sql_query_string, 0x00, conditional_clause_len);
9967 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);
9968 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9969 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9970 if (error != EMAIL_ERROR_NONE) {
9971 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9975 #ifdef __FEATURE_BODY_SEARCH__
9976 /* Updating mail_text_tbl */
9977 memset(sql_query_string, 0x00, conditional_clause_len);
9978 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);
9979 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9980 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9981 if (error != EMAIL_ERROR_NONE) {
9982 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9987 /* Updating a mail_read_mail_uid_tbl */
9988 memset(conditional_clause, 0x00, conditional_clause_len);
9989 cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE local_uid in (");
9991 for (i = 0; i < number_of_mails; i++)
9992 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
9995 last_comma = rindex(conditional_clause, ',');
9996 if (last_comma) *last_comma = ')'; /* replace , with) */
9998 memset(sql_query_string, 0x00, conditional_clause_len);
9999 SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_read_mail_uid_tbl SET mailbox_name = ? mailbox_id = ? account_id = ? %s", conditional_clause);
10001 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10002 error = emstorage_exec_query_by_prepare_v2_stmt(local_db_handle, sql_query_string, target_mailbox_name, input_mailbox_id, target_account_id);
10003 if (error != EMAIL_ERROR_NONE) {
10004 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2_stmt failed:[%d]", error);
10011 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10013 EM_SAFE_FREE(target_mailbox_name);
10014 EM_SAFE_FREE(conditional_clause);
10015 EM_SAFE_FREE(sql_query_string);
10017 if (err_code != NULL)
10020 EM_DEBUG_FUNC_END("ret [%d]", ret);
10024 INTERNAL_FUNC int emstorage_delete_mail(char *multi_user_name, int mail_id, int from_server, int transaction, int *err_code)
10026 EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
10029 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
10030 if (err_code != NULL)
10031 *err_code = EMAIL_ERROR_INVALID_PARAM;
10036 int error = EMAIL_ERROR_NONE;
10037 char sql_query_string[QUERY_SIZE] = {0, };
10038 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10039 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10041 memset(sql_query_string, 0x00, sizeof(sql_query_string));
10043 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id = %d ", mail_id);
10044 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10045 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10046 if (error != EMAIL_ERROR_NONE) {
10047 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10054 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10056 if (err_code != NULL)
10059 EM_DEBUG_FUNC_END("ret [%d]", ret);
10063 INTERNAL_FUNC int emstorage_delete_multiple_mails(char *multi_user_name, int mail_ids[], int number_of_mails, int transaction, int *err_code)
10065 EM_DEBUG_FUNC_BEGIN("mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", mail_ids, number_of_mails, transaction, err_code);
10067 int ret = false, i, cur_sql_query_string = 0;
10068 int error = EMAIL_ERROR_NONE;
10069 int query_size = 0;
10070 char *sql_query_string = NULL;
10073 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10074 if (err_code != NULL)
10075 *err_code = EMAIL_ERROR_INVALID_PARAM;
10079 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10080 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10082 query_size = (sizeof(char) * 8 * number_of_mails) + 512;
10083 sql_query_string = em_malloc(query_size);
10084 if (sql_query_string == NULL) {
10085 EM_DEBUG_EXCEPTION("em_mallocfailed");
10086 error = EMAIL_ERROR_OUT_OF_MEMORY;
10090 cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_tbl WHERE mail_id in (");
10092 for (i = 0; i < number_of_mails; i++)
10093 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
10095 /* prevent 34414 */
10096 char *last_comma = rindex(sql_query_string, ',');
10097 if (last_comma != NULL) *last_comma = ')'; /* replace , with) */
10099 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10100 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10101 if (error != EMAIL_ERROR_NONE) {
10102 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10106 #ifdef __FEATURE_BODY_SEARCH__
10107 /* delete mail_text from mail_text_tbl */
10108 cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_text_tbl WHERE mail_id in (");
10110 for (i = 0; i < number_of_mails; i++)
10111 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
10113 last_comma = rindex(sql_query_string, ',');
10114 *last_comma = ')'; /* replace , with) */
10116 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10117 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10118 if (error != EMAIL_ERROR_NONE) {
10119 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10127 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10129 EM_SAFE_FREE(sql_query_string);
10130 if (err_code != NULL)
10133 EM_DEBUG_FUNC_END("ret [%d]", ret);
10137 INTERNAL_FUNC int emstorage_delete_mail_by_account(char *multi_user_name, int account_id, int transaction, int *err_code)
10139 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
10141 if (account_id < FIRST_ACCOUNT_ID) {
10142 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
10144 if (err_code != NULL)
10145 *err_code = EMAIL_ERROR_INVALID_PARAM;
10149 int rc, ret = false;
10150 int error = EMAIL_ERROR_NONE;
10151 char sql_query_string[QUERY_SIZE] = {0, };
10153 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10154 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10156 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d", account_id);
10157 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10158 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10159 if (error != EMAIL_ERROR_NONE) {
10160 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10164 rc = sqlite3_changes(local_db_handle);
10166 EM_DEBUG_LOG(" no mail found...");
10167 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10170 /* Delete all mails mail_read_mail_uid_tbl table based on account id */
10171 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
10172 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10173 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10174 if (error != EMAIL_ERROR_NONE) {
10175 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10179 rc = sqlite3_changes(local_db_handle);
10181 EM_DEBUG_LOG("No mail found...");
10182 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10185 #ifdef __FEATURE_BODY_SEARCH__
10186 /* Delete all mail_text in mail_text_tbl table based on account id */
10187 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE account_id = %d", account_id);
10188 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10189 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10190 if (error != EMAIL_ERROR_NONE) {
10191 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10195 rc = sqlite3_changes(local_db_handle);
10197 EM_DEBUG_LOG("No mail found...");
10198 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10205 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10207 if (error == EMAIL_ERROR_NONE) {
10208 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_WITH_ACCOUNT, account_id, 0 , NULL, 0))
10209 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAIL_DELETE_ALL ]");
10212 if (err_code != NULL)
10215 EM_DEBUG_FUNC_END("ret [%d]", ret);
10219 INTERNAL_FUNC int emstorage_delete_mail_by_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox, int transaction, int *err_code)
10221 EM_DEBUG_FUNC_BEGIN("mailbox[%p], transaction[%d], err_code[%p]", mailbox, transaction, err_code);
10223 if (mailbox == NULL) {
10224 EM_DEBUG_EXCEPTION("mailbox [%p]", mailbox);
10225 if (err_code != NULL)
10226 *err_code = EMAIL_ERROR_INVALID_PARAM;
10231 int error = EMAIL_ERROR_NONE;
10232 char sql_query_string[QUERY_SIZE] = {0, };
10233 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10234 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10236 if (strcmp(mailbox->mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
10237 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);
10238 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10239 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10240 if (error != EMAIL_ERROR_NONE) {
10241 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10245 /* Delete Mails from mail_read_mail_uid_tbl */
10246 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);
10247 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10248 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10249 if (error != EMAIL_ERROR_NONE) {
10250 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10254 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);
10255 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10256 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10257 if (error != EMAIL_ERROR_NONE) {
10258 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10262 /* Delete Mails from mail_read_mail_uid_tbl */
10263 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);
10264 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10265 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10266 if (error != EMAIL_ERROR_NONE) {
10267 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10271 #ifdef __FEATURE_BODY_SEARCH__
10272 /* Delete Mails from mail_text_tbl */
10273 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);
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);
10285 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10287 if (error == EMAIL_ERROR_NONE) {
10288 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_ALL, mailbox->account_id, mailbox->mailbox_id , mailbox->mailbox_name, 0))
10289 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventFailed [ NOTI_MAIL_DELETE_ALL ] >>>> ");
10292 if (err_code != NULL)
10295 EM_DEBUG_FUNC_END("ret [%d]", ret);
10299 INTERNAL_FUNC int emstorage_free_mail(emstorage_mail_tbl_t** mail_list, int count, int *err_code)
10301 EM_DEBUG_FUNC_BEGIN("mail_list[%p], count[%d], err_code[%p]", mail_list, count, err_code);
10304 if ((mail_list == NULL) || (*mail_list == NULL)) {
10305 EM_DEBUG_EXCEPTION("mail_ilst[%p], count[%d]", mail_list, count);
10308 *err_code = EMAIL_ERROR_INVALID_PARAM;
10312 emstorage_mail_tbl_t* p = *mail_list;
10315 for (; i < count; i++, p++) {
10316 EM_SAFE_FREE(p->server_mailbox_name);
10317 EM_SAFE_FREE(p->server_mail_id);
10318 EM_SAFE_FREE(p->full_address_from);
10319 EM_SAFE_FREE(p->full_address_reply);
10320 EM_SAFE_FREE(p->full_address_to);
10321 EM_SAFE_FREE(p->full_address_cc);
10322 EM_SAFE_FREE(p->full_address_bcc);
10323 EM_SAFE_FREE(p->full_address_return);
10324 EM_SAFE_FREE(p->subject);
10325 EM_SAFE_FREE(p->file_path_plain);
10326 EM_SAFE_FREE(p->file_path_html);
10327 EM_SAFE_FREE(p->file_path_mime_entity);
10328 EM_SAFE_FREE(p->message_id);
10329 EM_SAFE_FREE(p->email_address_sender);
10330 EM_SAFE_FREE(p->email_address_recipient);
10331 EM_SAFE_FREE(p->preview_text);
10332 EM_SAFE_FREE(p->alias_sender);
10333 EM_SAFE_FREE(p->alias_recipient);
10334 EM_SAFE_FREE(p->default_charset);
10335 EM_SAFE_FREE(p->pgp_password);
10336 EM_SAFE_FREE(p->eas_data);
10337 EM_SAFE_FREE(p->user_name);
10339 EM_SAFE_FREE(*mail_list);
10342 if (err_code != NULL)
10343 *err_code = EMAIL_ERROR_NONE;
10345 EM_DEBUG_FUNC_END();
10349 #ifdef __FEATURE_BODY_SEARCH__
10350 INTERNAL_FUNC void emstorage_free_mail_text(emstorage_mail_text_tbl_t** mail_text_list, int count, int *err_code)
10352 EM_DEBUG_FUNC_BEGIN("mail_text_list[%p], count[%d], err_code[%p]", mail_text_list, count, err_code);
10355 if ((mail_text_list == NULL) || (*mail_text_list == NULL)) {
10356 EM_DEBUG_LOG("Nothing to free: mail_text_list[%p]", mail_text_list);
10360 emstorage_mail_text_tbl_t *p = *mail_text_list;
10363 for (; i < count; i++, p++)
10364 EM_SAFE_FREE(p->body_text);
10366 EM_SAFE_FREE(*mail_text_list);
10369 EM_DEBUG_FUNC_END();
10373 INTERNAL_FUNC int emstorage_get_attachment_count(char *multi_user_name, int mail_id, int *count, int transaction, int *err_code)
10375 EM_DEBUG_FUNC_BEGIN("mail_id[%d], count[%p], transaction[%d], err_code[%p]", mail_id, count, transaction, err_code);
10377 if (mail_id <= 0 || !count) {
10378 EM_DEBUG_EXCEPTION("mail_id[%d], count[%p]", mail_id, count);
10379 if (err_code != NULL)
10380 *err_code = EMAIL_ERROR_INVALID_PARAM;
10384 int rc = -1, ret = false;
10385 int error = EMAIL_ERROR_NONE;
10386 char sql_query_string[QUERY_SIZE] = {0, };
10388 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10389 EMSTORAGE_START_READ_TRANSACTION(transaction);
10391 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
10395 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10396 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10397 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10399 *count = atoi(result[1]);
10400 sqlite3_free_table(result);
10405 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10407 if (err_code != NULL)
10410 EM_DEBUG_FUNC_END("ret [%d]", ret);
10414 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)
10416 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);
10418 if (input_mail_id <= 0 || !output_attachment_list || !output_attachment_count) {
10419 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10420 return EMAIL_ERROR_INVALID_PARAM;
10423 int error = EMAIL_ERROR_NONE;
10426 char **result = NULL;
10427 char sql_query_string[QUERY_SIZE] = {0, };
10428 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10429 DB_STMT hStmt = NULL;
10430 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10432 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
10433 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", input_mail_id);
10434 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10435 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10436 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10438 *output_attachment_count = atoi(result[1]);
10439 sqlite3_free_table(result);
10441 if (*output_attachment_count == 0) {
10442 error = EMAIL_ERROR_NONE;
10446 p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * (*output_attachment_count));
10449 EM_DEBUG_EXCEPTION("em_mallocfailed...");
10450 error = EMAIL_ERROR_OUT_OF_MEMORY;
10454 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE mail_id = %d ORDER BY attachment_id", input_mail_id);
10455 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
10457 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10458 EM_DEBUG_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)));
10460 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10461 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
10463 if (rc == SQLITE_DONE) {
10464 EM_DEBUG_EXCEPTION("no matched attachment found...");
10465 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10468 for (i = 0; i < *output_attachment_count; i++) {
10469 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10470 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10471 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10472 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10473 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10474 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10475 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10476 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10477 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10478 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10479 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10480 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10481 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10482 EM_DEBUG_LOG("attachment[%d].attachment_id : %d", i, p_data_tbl[i].attachment_id);
10483 EM_DEBUG_LOG("attachment_mime_type : %s", p_data_tbl[i].attachment_mime_type);
10484 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10485 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
10490 if (error == EMAIL_ERROR_NONE)
10491 *output_attachment_list = p_data_tbl;
10492 else if (p_data_tbl != NULL)
10493 emstorage_free_attachment(&p_data_tbl, *output_attachment_count, NULL);
10496 rc = sqlite3_finalize(hStmt);
10497 if (rc != SQLITE_OK) {
10498 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10499 error = EMAIL_ERROR_DB_FAILURE;
10500 if (*output_attachment_list)
10501 emstorage_free_attachment(output_attachment_list, *output_attachment_count, NULL); /* prevent */
10505 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
10507 EM_DEBUG_FUNC_END("error [%d]", error);
10511 INTERNAL_FUNC int emstorage_get_attachment(char *multi_user_name, int attachment_id, emstorage_attachment_tbl_t **attachment, int transaction, int *err_code)
10513 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], transaction[%d], err_code[%p]", attachment_id, attachment, transaction, err_code);
10515 if (attachment_id <= 0 || !attachment) {
10516 EM_DEBUG_EXCEPTION("attachment_id[%d], attachment[%p]", attachment_id, attachment);
10517 if (err_code != NULL)
10518 *err_code = EMAIL_ERROR_INVALID_PARAM;
10522 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10523 int rc, ret = false;
10524 int error = EMAIL_ERROR_NONE;
10525 char sql_query_string[QUERY_SIZE] = {0, };
10527 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10528 EMSTORAGE_START_READ_TRANSACTION(transaction);
10530 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
10532 sqlite3_stmt* hStmt = NULL;
10534 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10535 EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
10537 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10538 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10541 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10542 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10543 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10545 if (rc == SQLITE_DONE) {
10546 EM_DEBUG_LOG("no matched attachment found...");
10547 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10551 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10552 EM_DEBUG_EXCEPTION("malloc failed...");
10553 error = EMAIL_ERROR_OUT_OF_MEMORY;
10557 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10558 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10559 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10560 _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10561 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10562 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10563 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10564 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10565 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10566 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10567 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10568 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10569 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10571 #ifdef __ATTACHMENT_OPTI__
10572 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10573 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10580 *attachment = p_data_tbl;
10582 if (hStmt != NULL) {
10583 rc = sqlite3_finalize(hStmt);
10584 if (rc != SQLITE_OK) {
10585 EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10586 error = EMAIL_ERROR_DB_FAILURE;
10590 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10592 if (err_code != NULL)
10595 EM_DEBUG_FUNC_END("ret [%d]", ret);
10599 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)
10601 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);
10603 if (mail_id <= 0 || nth <= 0 || !attachment_tbl) {
10604 EM_DEBUG_EXCEPTION(" mail_id[%d], nth[%d], attachment[%p]", mail_id, nth, attachment_tbl);
10606 if (err_code != NULL)
10607 *err_code = EMAIL_ERROR_INVALID_PARAM;
10611 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10613 int rc, ret = false;
10614 int error = EMAIL_ERROR_NONE;
10615 char sql_query_string[QUERY_SIZE] = {0, };
10617 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10618 EMSTORAGE_START_READ_TRANSACTION(transaction);
10620 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));
10621 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10623 DB_STMT hStmt = NULL;
10625 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10626 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10627 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10630 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10631 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10632 ("sqlite3_step fail:%d", rc));
10634 if (rc == SQLITE_DONE) {
10635 EM_DEBUG_EXCEPTION("no matched attachment found: mail_id[%d] nth[%d]", mail_id, nth);
10636 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10640 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10641 EM_DEBUG_EXCEPTION(" malloc failed...");
10642 error = EMAIL_ERROR_OUT_OF_MEMORY;
10646 p_data_tbl->attachment_id = sqlite3_column_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10647 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10648 p_data_tbl->attachment_name = cpy_str(p);
10649 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10650 p_data_tbl->attachment_path = cpy_str(p);
10651 if ((p = (char *)sqlite3_column_text(hStmt, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10652 p_data_tbl->content_id = cpy_str(p);
10653 p_data_tbl->attachment_size = sqlite3_column_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10654 p_data_tbl->mail_id = sqlite3_column_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10655 p_data_tbl->account_id = sqlite3_column_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10656 p_data_tbl->mailbox_id = sqlite3_column_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10657 p_data_tbl->attachment_save_status = sqlite3_column_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10658 p_data_tbl->attachment_drm_type = sqlite3_column_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10659 p_data_tbl->attachment_drm_method = sqlite3_column_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10660 p_data_tbl->attachment_inline_content_status = sqlite3_column_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10661 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10662 p_data_tbl->attachment_mime_type = cpy_str(p);
10663 #ifdef __ATTACHMENT_OPTI__
10664 p_data_tbl->encoding = sqlite3_column_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10665 if ((p = (char *)sqlite3_column_text(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10666 p_data_tbl->section = cpy_str(p);
10672 *attachment_tbl = p_data_tbl;
10674 if (hStmt != NULL) {
10675 rc = sqlite3_finalize(hStmt);
10676 if (rc != SQLITE_OK) {
10677 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10678 error = EMAIL_ERROR_DB_FAILURE;
10682 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10684 if (err_code != NULL)
10687 EM_DEBUG_FUNC_END("ret [%d]", ret);
10691 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)
10693 EM_DEBUG_FUNC_BEGIN("attachment_path[%p], attachment[%p], transaction[%d], err_code[%p]", attachment_path, attachment, transaction, err_code);
10695 if (attachment_path == NULL || !attachment) {
10696 EM_DEBUG_EXCEPTION("attachment_path[%p], attachment[%p]", attachment_path, attachment);
10697 if (err_code != NULL)
10698 *err_code = EMAIL_ERROR_INVALID_PARAM;
10702 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10703 int rc, ret = false;
10704 int error = EMAIL_ERROR_NONE;
10705 char sql_query_string[QUERY_SIZE] = {0, };
10707 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10708 EMSTORAGE_START_READ_TRANSACTION(transaction);
10710 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_path = '%s'", attachment_path);
10712 sqlite3_stmt* hStmt = NULL;
10714 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10715 EM_DEBUG_LOG_DEV("before sqlite3_prepare hStmt = %p", hStmt);
10717 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10718 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10721 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10722 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10723 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10725 if (rc == SQLITE_DONE) {
10726 EM_DEBUG_LOG("no matched attachment found...");
10727 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10731 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10732 EM_DEBUG_EXCEPTION("malloc failed...");
10733 error = EMAIL_ERROR_OUT_OF_MEMORY;
10737 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10738 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10739 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10740 _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10741 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10742 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10743 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10744 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10745 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10746 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10747 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10748 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10749 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10751 #ifdef __ATTACHMENT_OPTI__
10752 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10753 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10760 *attachment = p_data_tbl;
10762 if (hStmt != NULL) {
10763 rc = sqlite3_finalize(hStmt);
10764 if (rc != SQLITE_OK) {
10765 EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10766 error = EMAIL_ERROR_DB_FAILURE;
10770 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10772 if (err_code != NULL)
10775 EM_DEBUG_FUNC_END("ret [%d]", ret);
10779 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)
10781 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], attachment[%p], transaction[%d], err_code[%p]", mail_id, type, attachment, transaction, err_code);
10783 if (mail_id <= 0 || !attachment) {
10784 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], attachment[%p]", mail_id, type, attachment);
10785 if (err_code != NULL)
10786 *err_code = EMAIL_ERROR_INVALID_PARAM;
10790 int rc, ret = false;
10791 int error = EMAIL_ERROR_NONE;
10792 DB_STMT hStmt = NULL;
10793 char sql_query_string[QUERY_SIZE] = {0, };
10797 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10799 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10802 case UPDATE_MAILBOX:
10803 EM_DEBUG_LOG("UPDATE_MAILBOX");
10804 if (!attachment->mailbox_id) {
10805 EM_DEBUG_EXCEPTION(" attachment->mailbox_id[%d]", attachment->mailbox_id);
10806 error = EMAIL_ERROR_INVALID_PARAM;
10809 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10810 "UPDATE mail_attachment_tbl SET mailbox_id = ? WHERE mail_id = %d", mail_id);
10812 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10813 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10814 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10815 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10817 _bind_stmt_field_data_int(hStmt, i++, attachment->mailbox_id);
10820 case UPDATE_SAVENAME:
10821 EM_DEBUG_LOG("UPDATE_SAVENAME");
10822 if (!attachment->attachment_path) {
10823 EM_DEBUG_EXCEPTION(" attachment->attachment_path[%p]", attachment->attachment_path);
10824 error = EMAIL_ERROR_INVALID_PARAM;
10828 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10829 "UPDATE mail_attachment_tbl SET"
10830 " attachment_size = ?"
10831 ", attachment_save_status = ?"
10832 ", attachment_path = ?"
10833 " WHERE mail_id = %d"
10834 " AND attachment_id = %d"
10835 , attachment->mail_id
10836 , attachment->attachment_id);
10839 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10840 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10841 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10842 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10844 _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_size);
10845 _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_save_status);
10846 _bind_stmt_field_data_string(hStmt, i++, (char *)attachment->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10850 EM_DEBUG_LOG("type[%d]", type);
10851 error = EMAIL_ERROR_INVALID_PARAM;
10854 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10856 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10857 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10858 ("sqlite3_step fail:%d", rc));
10863 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10865 if (hStmt != NULL) {
10866 rc = sqlite3_finalize(hStmt);
10867 if (rc != SQLITE_OK) {
10868 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10869 error = EMAIL_ERROR_DB_FAILURE;
10873 if (err_code != NULL)
10875 EM_DEBUG_FUNC_END("ret [%d]", ret);
10879 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)
10881 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);
10885 int error = EMAIL_ERROR_NONE;
10887 int account_id = 0;
10888 char sql_query_string[QUERY_SIZE] = {0, };
10889 char *replaced_mailbox_name = NULL;
10890 char *replaced_alias = NULL;
10891 sqlite3 *local_db_handle = NULL;
10892 DB_STMT hStmt = NULL;
10893 emstorage_mailbox_tbl_t *old_mailbox_data = NULL;
10895 if (input_mailbox_id <= 0 || !input_new_mailbox_name || !input_new_mailbox_alias) {
10896 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10897 return EMAIL_ERROR_INVALID_PARAM;
10900 if (strstr(input_new_mailbox_name, "'"))
10901 replaced_mailbox_name = em_replace_all_string(input_new_mailbox_name, "'", "''");
10903 replaced_mailbox_name = strdup(input_new_mailbox_name);
10905 if (strstr(input_new_mailbox_alias, "'"))
10906 replaced_alias = em_replace_all_string(input_new_mailbox_alias, "'", "''");
10908 replaced_alias = strdup(input_new_mailbox_alias);
10910 local_db_handle = emstorage_get_db_connection(multi_user_name);
10912 if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &old_mailbox_data)) != EMAIL_ERROR_NONE) {
10913 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", error);
10914 EM_SAFE_FREE(replaced_mailbox_name);
10915 EM_SAFE_FREE(replaced_alias);
10919 if (old_mailbox_data == NULL) {
10920 EM_DEBUG_LOG("old_mailbox_data is NULL");
10921 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
10925 account_id = old_mailbox_data->account_id;
10927 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
10929 if (input_eas_data && input_eas_data_length > 0) {
10930 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10931 "UPDATE mail_box_tbl SET"
10932 " mailbox_name = ?"
10935 ",eas_data_length = ?"
10936 " WHERE mailbox_id = ?");
10938 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10940 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10941 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10943 _bind_stmt_field_data_string(hStmt, field_idx++, input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10944 _bind_stmt_field_data_string(hStmt, field_idx++, input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10945 _bind_stmt_field_data_blob(hStmt, field_idx++, input_eas_data, input_eas_data_length);
10946 _bind_stmt_field_data_int(hStmt, field_idx++, input_eas_data_length);
10947 _bind_stmt_field_data_int(hStmt, field_idx++, input_mailbox_id);
10949 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10950 "UPDATE mail_box_tbl SET"
10951 " mailbox_name = ?"
10953 " WHERE mailbox_id = ?");
10955 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10957 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10958 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10960 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10961 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10962 _bind_stmt_field_data_int(hStmt, field_idx++ , input_mailbox_id);
10966 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10967 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
10968 ("sqlite3_step fail:%d", rc));
10969 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10970 ("sqlite3_step fail:%d", rc));
10972 if (sqlite3_changes(local_db_handle) == 0)
10973 EM_DEBUG_LOG("no mail_meeting_tbl matched...");
10978 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, error);
10979 if (error == EMAIL_ERROR_NONE) {
10980 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME, account_id, input_mailbox_id, input_new_mailbox_name, 0))
10981 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAILBOX_RENAME ] >>>> ");
10983 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME_FAIL, account_id, input_mailbox_id, input_new_mailbox_name, error))
10984 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAILBOX_RENAME_FAIL ] >>>> ");
10987 EM_SAFE_FREE(replaced_mailbox_name);
10988 EM_SAFE_FREE(replaced_alias);
10990 if (old_mailbox_data)
10991 emstorage_free_mailbox(&old_mailbox_data, 1, NULL);
10993 if (hStmt != NULL) {
10994 rc = sqlite3_finalize(hStmt);
10995 if (rc != SQLITE_OK) {
10996 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10997 error = EMAIL_ERROR_DB_FAILURE;
11001 EM_DEBUG_FUNC_END("error [%d]", error);
11005 INTERNAL_FUNC int emstorage_get_new_attachment_no(char *multi_user_name, int *attachment_no, int *err_code)
11007 EM_DEBUG_FUNC_BEGIN("attachment_no [%p], err_code[%p]", attachment_no, err_code);
11008 int rc, ret = false;
11009 int error = EMAIL_ERROR_NONE;
11010 char *sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
11013 if (!attachment_no) {
11014 EM_DEBUG_EXCEPTION("Invalid attachment");
11015 if (err_code != NULL)
11016 *err_code = EMAIL_ERROR_INVALID_PARAM;
11020 *attachment_no = -1;
11022 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11025 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
11026 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
11027 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
11029 if (NULL == result[1])
11032 rc = atoi(result[1])+1;
11034 sqlite3_free_table(result);
11036 *attachment_no = rc;
11037 EM_DEBUG_LOG("attachment_no [%d]", *attachment_no);
11042 if (err_code != NULL)
11045 EM_DEBUG_FUNC_END("ret [%d]", ret);
11049 INTERNAL_FUNC int emstorage_add_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int iscopy, int transaction, int *err_code)
11051 EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], iscopy[%d], transaction[%d], err_code[%p]", attachment_tbl, iscopy, transaction, err_code);
11055 int rc, ret = false;
11056 int error = EMAIL_ERROR_NONE;
11057 char sql_query_string[QUERY_SIZE] = {0, };
11059 DB_STMT hStmt = NULL;
11060 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11062 if (!attachment_tbl) {
11063 EM_DEBUG_EXCEPTION("attachment_tbl[%p], iscopy[%d]", attachment_tbl, iscopy);
11064 if (err_code != NULL)
11065 *err_code = EMAIL_ERROR_INVALID_PARAM;
11069 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11071 sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
11073 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
11074 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
11075 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
11077 if (NULL == result[1]) rc = 1;
11078 else rc = atoi(result[1]) + 1;
11079 sqlite3_free_table(result);
11081 attachment_tbl->attachment_id = rc;
11083 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11084 "INSERT INTO mail_attachment_tbl VALUES "
11085 "(?" /* attachment_id */
11086 ", ?" /* attachment_name */
11087 ", ?" /* attachment_path */
11088 ", ?" /* content_id */
11089 ", ?" /* attachment_size */
11090 ", ?" /* mail_id */
11091 ", ?" /* account_id */
11092 ", ?" /* mailbox_id */
11093 ", ?" /* attachment_save_status */
11094 ", ?" /* attachment_drm_type */
11095 ", ?" /* attachment_drm_method */
11096 ", ?" /* attachment_inline_content_status */
11097 ", ?" /* attachment_mime_type */
11098 #ifdef __ATTACHMENT_OPTI__
11105 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11106 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11107 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11109 _bind_stmt_field_data_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_id);
11110 _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);
11111 _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);
11112 _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);
11113 _bind_stmt_field_data_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_size);
11114 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mail_id);
11115 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->account_id);
11116 _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mailbox_id);
11117 _bind_stmt_field_data_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_save_status);
11118 _bind_stmt_field_data_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_type);
11119 _bind_stmt_field_data_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_method);
11120 _bind_stmt_field_data_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_inline_content_status);
11121 _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);
11122 #ifdef __ATTACHMENT_OPTI__
11123 _bind_stmt_field_data_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->encoding);
11124 _bind_stmt_field_data_string(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->section, 0, ATTACHMENT_LEN_IN_MAIL_ATTACHMENT_TBL);
11128 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11129 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
11130 ("sqlite3_step fail:%d", rc));
11131 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11132 ("sqlite3_step fail:%d", rc));
11134 rc = sqlite3_changes(local_db_handle);
11136 EM_DEBUG_LOG(" no matched mail found...");
11137 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11144 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11146 if (hStmt != NULL) {
11147 rc = sqlite3_finalize(hStmt);
11148 if (rc != SQLITE_OK) {
11149 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11150 error = EMAIL_ERROR_DB_FAILURE;
11154 if (err_code != NULL)
11157 EM_DEBUG_FUNC_END("ret [%d]", ret);
11161 INTERNAL_FUNC int emstorage_update_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int transaction, int *err_code)
11163 EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], transaction[%d], err_code[%p]", attachment_tbl, transaction, err_code);
11165 int rc, ret = false, field_idx = 0;
11166 int error = EMAIL_ERROR_NONE;
11167 DB_STMT hStmt = NULL;
11168 char sql_query_string[QUERY_SIZE] = {0, };
11170 if (!attachment_tbl) {
11171 EM_DEBUG_EXCEPTION(" attachment_tbl[%p] ", attachment_tbl);
11173 *err_code = EMAIL_ERROR_INVALID_PARAM;
11177 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11179 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11181 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11182 "UPDATE mail_attachment_tbl SET "
11183 " attachment_name = ?"
11184 ", attachment_path = ?"
11186 ", attachment_size = ?"
11190 ", attachment_save_status = ?"
11191 ", attachment_drm_type = ?"
11192 ", attachment_drm_method = ?"
11193 ", attachment_inline_content_status = ? "
11194 ", attachment_mime_type = ? "
11195 " WHERE attachment_id = ?;");
11198 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11200 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11201 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11203 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
11204 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
11205 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->content_id, 0, CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL);
11206 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_size);
11207 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->mail_id);
11208 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->account_id);
11209 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->mailbox_id);
11210 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_save_status);
11211 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_drm_type);
11212 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_drm_method);
11213 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_inline_content_status);
11214 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_mime_type, 0, ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL);
11215 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_id);
11218 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11219 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
11220 ("sqlite3_step fail:%d", rc));
11221 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11222 ("sqlite3_step fail:%d", rc));
11224 rc = sqlite3_changes(local_db_handle);
11226 EM_DEBUG_LOG(" no matched mail found...");
11227 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11234 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11235 if (hStmt != NULL) {
11236 rc = sqlite3_finalize(hStmt);
11237 if (rc != SQLITE_OK) {
11238 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11239 error = EMAIL_ERROR_DB_FAILURE;
11243 if (err_code != NULL)
11246 EM_DEBUG_FUNC_END("ret [%d]", ret);
11250 INTERNAL_FUNC int emstorage_delete_attachment_on_db(char *multi_user_name, int attachment_id, int transaction, int *err_code)
11252 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], transaction[%d], err_code[%p]", attachment_id, transaction, err_code);
11254 if (attachment_id < 0) {
11255 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
11256 if (err_code != NULL)
11257 *err_code = EMAIL_ERROR_INVALID_PARAM;
11262 int error = EMAIL_ERROR_NONE;
11263 char sql_query_string[QUERY_SIZE] = {0, };
11264 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11266 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11268 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
11270 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11271 if (error != EMAIL_ERROR_NONE) {
11272 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11279 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11284 EM_DEBUG_FUNC_END("ret [%d]", ret);
11288 INTERNAL_FUNC int emstorage_delete_all_attachments_of_mail(char *multi_user_name, int mail_id, int transaction, int *err_code)
11290 EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
11292 int error = EMAIL_ERROR_NONE;
11293 char sql_query_string[QUERY_SIZE] = {0, };
11294 sqlite3 *local_db_handle = NULL;
11296 if (mail_id <= 0) {
11297 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
11298 if (err_code != NULL)
11299 *err_code = EMAIL_ERROR_INVALID_PARAM;
11303 local_db_handle = emstorage_get_db_connection(multi_user_name);
11305 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11307 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
11308 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11309 if (error != EMAIL_ERROR_NONE) {
11310 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11317 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11322 EM_DEBUG_FUNC_END("ret [%d]", ret);
11326 INTERNAL_FUNC int emstorage_delete_attachment_all_on_db(char *multi_user_name, int account_id, char *mailbox, int transaction, int *err_code)
11328 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox, transaction, err_code);
11330 int error = EMAIL_ERROR_NONE;
11332 char sql_query_string[QUERY_SIZE] = {0, };
11333 char *replaced_mailbox = NULL;
11334 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11336 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11338 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl");
11340 if (account_id != ALL_ACCOUNT) /* '0' means all account */
11341 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);
11343 if (mailbox) /* NULL means all mailbox_name */ {
11344 if (strstr(mailbox, "'"))
11345 replaced_mailbox = em_replace_all_string(mailbox, "'", "''");
11347 replaced_mailbox = strdup(mailbox);
11349 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);
11350 EM_SAFE_FREE(replaced_mailbox); /*prevent 49434*/
11353 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11354 if (error != EMAIL_ERROR_NONE) {
11355 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11362 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11364 if (err_code != NULL)
11367 EM_DEBUG_FUNC_END("ret [%d]", ret);
11371 INTERNAL_FUNC int emstorage_free_attachment(emstorage_attachment_tbl_t** attachment_tbl_list, int count, int *err_code)
11373 EM_DEBUG_FUNC_BEGIN("attachment_tbl_list[%p], count[%d], err_code[%p]", attachment_tbl_list, count, err_code);
11376 if ((attachment_tbl_list == NULL) || (*attachment_tbl_list == NULL)) {
11377 EM_DEBUG_LOG("Nothing to free: attachment_tbl_list[%p], count[%d]", attachment_tbl_list, count);
11378 if (err_code != NULL)
11379 *err_code = EMAIL_ERROR_INVALID_PARAM;
11383 emstorage_attachment_tbl_t* p = *attachment_tbl_list;
11386 for (i = 0; i < count; i++) {
11387 EM_SAFE_FREE(p[i].attachment_name);
11388 EM_SAFE_FREE(p[i].attachment_path);
11389 EM_SAFE_FREE(p[i].content_id);
11390 EM_SAFE_FREE(p[i].attachment_mime_type);
11391 #ifdef __ATTACHMENT_OPTI__
11392 EM_SAFE_FREE(p[i].section);
11397 *attachment_tbl_list = NULL;
11400 if (err_code != NULL)
11401 *err_code = EMAIL_ERROR_NONE;
11402 EM_DEBUG_FUNC_END();
11406 INTERNAL_FUNC int emstorage_begin_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11408 EM_DEBUG_FUNC_BEGIN();
11409 EM_PROFILE_BEGIN(emStorageBeginTransaction);
11412 _timedlock_shm_mutex(mapped_for_db_lock, 2);
11414 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11417 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN immediate;", NULL, NULL, NULL), rc);
11418 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11419 ("SQL(BEGIN) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11421 if (ret == false) {
11422 if (err_code != NULL) *err_code = EMAIL_ERROR_DB_FAILURE;
11425 EM_PROFILE_END(emStorageBeginTransaction);
11426 EM_DEBUG_FUNC_END("ret [%d]", ret);
11430 INTERNAL_FUNC int emstorage_commit_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11432 EM_DEBUG_FUNC_BEGIN();
11436 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11438 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
11439 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; }, ("SQL(END) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11441 if (ret == false && err_code != NULL)
11442 *err_code = EMAIL_ERROR_DB_FAILURE;
11444 _unlockshm_mutex(mapped_for_db_lock);
11446 EM_DEBUG_FUNC_END("ret [%d]", ret);
11450 INTERNAL_FUNC int emstorage_rollback_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11452 EM_DEBUG_FUNC_BEGIN();
11455 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11457 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "ROLLBACK;", NULL, NULL, NULL), rc);
11458 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11459 ("SQL(ROLLBACK) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11461 if (ret == false && err_code != NULL)
11462 *err_code = EMAIL_ERROR_DB_FAILURE;
11464 _unlockshm_mutex(mapped_for_db_lock);
11466 EM_DEBUG_FUNC_END("ret [%d]", ret);
11470 INTERNAL_FUNC int emstorage_is_mailbox_full(char *multi_user_name, int account_id, email_mailbox_t *mailbox, int *result, int *err_code)
11472 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], result[%p], err_code[%p]", account_id, mailbox, result, err_code);
11474 if (account_id < FIRST_ACCOUNT_ID || !mailbox || !result) {
11476 EM_DEBUG_EXCEPTION("Invalid Parameter. accoun_id[%d], mailbox[%p]", account_id, mailbox);
11478 if (err_code != NULL)
11479 *err_code = EMAIL_ERROR_INVALID_PARAM;
11485 int error = EMAIL_ERROR_NONE;
11486 int mail_count = 0;
11488 if (!emstorage_get_mail_count(multi_user_name, account_id, mailbox->mailbox_id, &mail_count, NULL, true, &error)) {
11489 EM_DEBUG_EXCEPTION("emstorage_get_mail_count failed [%d]", error);
11494 EM_DEBUG_LOG("mail_count[%d] mail_slot_size[%d]", mail_count, mailbox->mail_slot_size);
11495 if (mail_count >= mailbox->mail_slot_size)
11506 if (err_code != NULL)
11509 EM_DEBUG_FUNC_END("ret [%d]", ret);
11513 INTERNAL_FUNC int emstorage_clear_mail_data(char *multi_user_name, int transaction, int *err_code)
11515 EM_DEBUG_FUNC_BEGIN("transaction[%d], err_code[%p]", transaction, err_code);
11518 int error = EMAIL_ERROR_NONE;
11519 char sql_query_string[QUERY_SIZE] = {0, };
11520 const email_db_object_t* tables = _g_db_tables;
11521 const email_db_object_t* indexes = _g_db_indexes;
11523 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11524 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11526 if (!emstorage_delete_dir((char *)MAIL_HOME, &error)) {
11527 EM_DEBUG_EXCEPTION(" emstorage_delete_dir failed - %d", error);
11532 if (mkdir(MAIL_HOME, DIRECTORY_PERMISSION) != 0)
11533 EM_DEBUG_EXCEPTION("Fail to create the directory \n");
11534 if (mkdir(MAIL_TEMP, DIRECTORY_PERMISSION) != 0)
11535 EM_DEBUG_EXCEPTION("Fail to create the directory \n");
11536 if (chmod(MAIL_TEMP, 0777) != 0)
11537 EM_DEBUG_EXCEPTION("Fail to change the permission \n");
11539 /* first clear index. */
11540 while (indexes->object_name) {
11541 if (indexes->data_flag) {
11542 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP index %s", indexes->object_name);
11543 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11544 if (error != EMAIL_ERROR_NONE) {
11545 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11552 while (tables->object_name) {
11553 if (tables->data_flag) {
11554 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP table %s", tables->object_name);
11555 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11556 if (error != EMAIL_ERROR_NONE) {
11557 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11567 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11569 if (err_code != NULL)
11572 EM_DEBUG_FUNC_END("ret [%d]", ret);
11575 /*======================= DB File Utils =============================================*/
11576 #include <dirent.h>
11577 #include <sys/types.h>
11578 #define DIR_SEPERATOR "/"
11580 INTERNAL_FUNC char *emstorage_make_directory_path_from_file_path(char *file_name)
11582 EM_DEBUG_FUNC_BEGIN("Filename [ %p ]", file_name);
11583 char delims[] = "/";
11584 char *result = NULL;
11585 gchar **token = NULL;
11587 token = g_strsplit_set(file_name, delims, 1);
11589 if (token && token[0]) {
11590 EM_DEBUG_LOG_SEC(">>>> Directory_name [ %s ]", token[0]);
11591 result = EM_SAFE_STRDUP(token[0]);
11593 EM_DEBUG_LOG(">>>> No Need to create Directory");
11600 INTERNAL_FUNC int emstorage_get_save_name(char *multi_user_name, int account_id, int mail_id, int atch_id,
11601 char *fname, char *move_buf, char *path_buf, int maxlen, int *err_code)
11603 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);
11604 EM_PROFILE_BEGIN(profile_emstorage_get_save_name);
11607 int error = EMAIL_ERROR_NONE;
11608 char *dir_name = NULL;
11609 char create_dir[1024] = {0};
11610 char *temp_file = NULL;
11611 char *prefix_path = NULL;
11612 char *modified_fname = NULL;
11614 if (!move_buf || !path_buf || account_id < FIRST_ACCOUNT_ID || mail_id < 0 || atch_id < 0) {
11615 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);
11616 error = EMAIL_ERROR_INVALID_PARAM;
11620 snprintf(path_buf, 512, "%s", MAIL_HOME);
11621 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, account_id);
11624 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, mail_id);
11627 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, atch_id);
11630 temp_file = EM_SAFE_STRDUP(fname);
11631 if (temp_file && strstr(temp_file, "/"))
11632 dir_name = emstorage_make_directory_path_from_file_path(temp_file);
11636 snprintf(create_dir, sizeof(create_dir), "%s%s%s", path_buf, DIR_SEPERATOR, dir_name);
11637 EM_DEBUG_LOG(">>>>> DIR PATH [%s]", create_dir);
11638 ret = mkdir(create_dir, DIRECTORY_PERMISSION);
11639 EM_DEBUG_LOG("mkdir result = [%d]", ret);
11640 EM_SAFE_FREE(dir_name);
11644 EM_DEBUG_LOG_DEV(">>>>> fname [%s]", fname);
11646 /* Did not allow the slash */
11647 modified_fname = reg_replace_new(fname, "/", "_");
11648 EM_DEBUG_LOG_SEC("modified_fname : [%s]", modified_fname);
11650 if (modified_fname == NULL) modified_fname = g_strdup(fname);
11652 if (EM_SAFE_STRLEN(modified_fname) + EM_SAFE_STRLEN(path_buf) + strlen(DIR_SEPERATOR) > maxlen - 1) {
11653 char *modified_name = NULL;
11654 int remain_len = (maxlen - 1) - EM_SAFE_STRLEN(path_buf) - strlen(DIR_SEPERATOR);
11656 if (remain_len <= 0) {
11657 error = EMAIL_ERROR_MAX_EXCEEDED;
11661 if (remain_len > MAX_FILENAME)
11662 remain_len = MAX_FILENAME;
11664 modified_name = em_shrink_filename(modified_fname, remain_len);
11666 if (!modified_name) {
11667 error = EMAIL_ERROR_MAX_EXCEEDED;
11671 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),
11672 "%s%s", DIR_SEPERATOR, modified_name);
11673 EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11674 EM_SAFE_FREE(modified_name);
11676 if (EM_SAFE_STRLEN(modified_fname) > MAX_FILENAME - 1) {
11677 char *modified_name = NULL;
11679 modified_name = em_shrink_filename(modified_fname, MAX_FILENAME);
11680 if (!modified_name) {
11681 error = EMAIL_ERROR_MAX_EXCEEDED;
11685 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),
11686 "%s%s", DIR_SEPERATOR, modified_name);
11687 EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11688 EM_SAFE_FREE(modified_name);
11690 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),
11691 "%s%s", DIR_SEPERATOR, modified_fname);
11696 EM_DEBUG_LOG_SEC(">>>>> path_buf [%s]", path_buf);
11698 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11699 error = emcore_get_container_path(multi_user_name, &prefix_path);
11700 if (error != EMAIL_ERROR_NONE) {
11701 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11704 snprintf(move_buf, 512, "%s/%s", prefix_path, path_buf);
11705 EM_DEBUG_LOG_SEC("move_buf : [%s]", move_buf);
11707 snprintf(move_buf, 512, "%s", path_buf);
11708 EM_DEBUG_LOG_SEC("move_buf : [%s]", move_buf);
11715 EM_SAFE_FREE(temp_file);
11716 EM_SAFE_FREE(prefix_path);
11717 EM_SAFE_FREE(modified_fname);
11719 if (err_code != NULL)
11722 EM_PROFILE_END(profile_emstorage_get_save_name);
11723 EM_DEBUG_FUNC_END("ret [%d]", ret);
11728 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)
11730 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);
11732 if (!name_buf || account_id < FIRST_ACCOUNT_ID) {
11733 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);
11734 if (err_code != NULL)
11735 *err_code = EMAIL_ERROR_INVALID_PARAM;
11739 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
11742 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, mail_id);
11747 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, atch_id);
11752 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), ".DELE");
11754 EM_DEBUG_FUNC_END();
11759 INTERNAL_FUNC int emstorage_create_dir(char *multi_user_name, int account_id, int mail_id, int atch_id, int *err_code)
11761 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], err_code[%p]", account_id, mail_id, atch_id, err_code);
11762 EM_PROFILE_BEGIN(profile_emcore_save_create_dir);
11764 int error = EMAIL_ERROR_NONE;
11767 char *prefix_path = NULL;
11768 char errno_buf[ERRNO_BUF_SIZE] = {0};
11770 memset(buf, 0x00, sizeof(buf));
11772 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11773 error = emcore_get_container_path(multi_user_name, &prefix_path);
11774 if (error != EMAIL_ERROR_NONE) {
11775 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11779 prefix_path = strdup("");
11782 if (account_id >= FIRST_ACCOUNT_ID) {
11783 SNPRINTF(buf, sizeof(buf), "%s%s%s%s%d", prefix_path,
11789 if (stat(buf, &sbuf) == 0) {
11790 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11791 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11792 error = EMAIL_ERROR_SYSTEM_FAILURE;
11796 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11797 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11798 EM_DEBUG_EXCEPTION("mkdir failed: %s", EM_STRERROR(errno_buf));
11799 error = EMAIL_ERROR_SYSTEM_FAILURE;
11801 error = EMAIL_ERROR_OUT_OF_MEMORY;
11805 if (account_id == EML_FOLDER)
11811 int space_left_in_buffer = sizeof(buf) - EM_SAFE_STRLEN(buf);
11813 if (account_id < FIRST_ACCOUNT_ID) {
11814 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11815 error = EMAIL_ERROR_INVALID_PARAM;
11819 if (space_left_in_buffer + 10 > sizeof(buf)) {
11820 EM_DEBUG_EXCEPTION("Buffer overflowed");
11821 error = EMAIL_ERROR_OUT_OF_MEMORY;
11825 SNPRINTF(buf+EM_SAFE_STRLEN(buf), space_left_in_buffer, "%s%d", DIR_SEPERATOR, mail_id);
11827 if (stat(buf, &sbuf) == 0) {
11828 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11829 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11830 error = EMAIL_ERROR_SYSTEM_FAILURE;
11834 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11835 EM_DEBUG_EXCEPTION("mkdir failed [%s]", buf);
11836 EM_DEBUG_EXCEPTION("mkdir failed [%d][%s]", errno, EM_STRERROR(errno_buf));
11837 error = EMAIL_ERROR_SYSTEM_FAILURE;
11839 error = EMAIL_ERROR_OUT_OF_MEMORY;
11843 if (account_id == EML_FOLDER)
11849 if (account_id < FIRST_ACCOUNT_ID || mail_id <= 0) {
11850 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11851 error = EMAIL_ERROR_INVALID_PARAM;
11855 SNPRINTF(buf+EM_SAFE_STRLEN(buf), sizeof(buf)-(EM_SAFE_STRLEN(buf)+1), "%s%d", DIR_SEPERATOR, atch_id);
11857 if (stat(buf, &sbuf) == 0) {
11858 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11859 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11860 error = EMAIL_ERROR_SYSTEM_FAILURE;
11864 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11865 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11866 error = EMAIL_ERROR_SYSTEM_FAILURE;
11868 error = EMAIL_ERROR_OUT_OF_MEMORY;
11872 if (account_id == EML_FOLDER)
11873 if (chmod(buf, 0777) != 0)
11874 EM_DEBUG_EXCEPTION("Fail to change the permission \n");
11882 EM_SAFE_FREE(prefix_path);
11884 if (err_code != NULL)
11887 EM_PROFILE_END(profile_emcore_save_create_dir);
11888 EM_DEBUG_FUNC_END("ret [%d]", ret);
11892 INTERNAL_FUNC int emstorage_copy_file(char *src_file, char *dst_file, int sync_status, int *err_code)
11894 EM_DEBUG_FUNC_BEGIN_SEC("src_file[%s], dst_file[%s], err_code[%p]", src_file, dst_file, err_code);
11895 EM_DEBUG_LOG("Using the fsync function");
11897 int error = EMAIL_ERROR_NONE;
11898 struct stat st_buf;
11903 char buf[FILE_MAX_BUFFER_SIZE] = {0};
11904 char errno_buf[ERRNO_BUF_SIZE] = {0};
11906 if (!src_file || !dst_file) {
11907 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
11909 error = EMAIL_ERROR_INVALID_PARAM;
11913 if (stat(src_file, &st_buf) < 0) {
11914 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", src_file);
11916 error = EMAIL_ERROR_SYSTEM_FAILURE; /* EMAIL_ERROR_INVALID_PATH; */
11920 error = em_open(src_file, O_RDONLY, 0, &fp_src);
11921 if (error != EMAIL_ERROR_NONE) {
11922 EM_DEBUG_EXCEPTION(">>>> Source Fail em_open %s Failed: %d", src_file, error);
11926 error = em_open(dst_file, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, &fp_dst); /*prevent 24474*/
11927 if (error != EMAIL_ERROR_NONE) {
11928 EM_DEBUG_EXCEPTION(">>>> Destination Fail em_open %s: %d", dst_file, error);
11932 memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
11934 while ((nread = read(fp_src, buf, FILE_MAX_BUFFER_SIZE)) > 0) {
11935 if (nread > 0 && nread <= FILE_MAX_BUFFER_SIZE) {
11936 EM_DEBUG_LOG("Nread Value [%d]", nread);
11938 ssize_t byte_written = 0;
11939 size_t remain_byte = nread;
11943 while (remain_byte > 0 && buf_ptr && errno == 0) {
11944 byte_written = write(fp_dst, buf_ptr, remain_byte);
11946 if (byte_written < 0) {
11947 /* interrupted by a signal */
11948 if (errno == EINTR) {
11953 EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
11954 error = EMAIL_ERROR_UNKNOWN;
11957 EM_DEBUG_LOG("NWRITTEN [%zu]", byte_written);
11958 remain_byte -= byte_written;
11959 buf_ptr += byte_written;
11963 memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
11969 EM_SAFE_CLOSE(fp_src);
11971 if (fp_dst >= 0) { /*prevent 24474*/
11973 EM_DEBUG_LOG("Before fsync");
11979 if (nread < 0 || error == EMAIL_ERROR_UNKNOWN)
11982 if (err_code != NULL)
11984 EM_DEBUG_FUNC_END("ret [%d]", ret);
11987 /* create Directory if user has deleted [deepam.p@samsung.com] */
11988 INTERNAL_FUNC void emstorage_create_dir_if_delete()
11990 EM_DEBUG_FUNC_BEGIN();
11992 if (mkdir(DATA_PATH, DIRECTORY_PERMISSION) != 0)
11993 EM_DEBUG_EXCEPTION("Fail to create the directory \n");
11994 if (mkdir(EMAIL_PATH, DIRECTORY_PERMISSION) != 0)
11995 EM_DEBUG_EXCEPTION("Fail to create the directory \n");
11996 if (mkdir(MAIL_HOME, DIRECTORY_PERMISSION) != 0)
11997 EM_DEBUG_EXCEPTION("Fail to create the directory \n");
11998 if (mkdir(MAIL_TEMP, DIRECTORY_PERMISSION) != 0)
11999 EM_DEBUG_EXCEPTION("Fail to create the directory \n");
12000 if (chmod(MAIL_TEMP, 0777) != 0)
12001 EM_DEBUG_EXCEPTION("Fail to change the permission \n");
12003 EM_DEBUG_FUNC_END();
12006 static int _get_temp_file_name(char **filename, int *err_code)
12008 EM_DEBUG_FUNC_BEGIN("filename[%p], err_code[%p]", filename, err_code);
12011 int error = EMAIL_ERROR_NONE;
12013 if (filename == NULL) {
12014 EM_DEBUG_EXCEPTION(" filename[%p]", filename);
12015 error = EMAIL_ERROR_INVALID_PARAM;
12019 char tempname[512] = {0x00, };
12022 gettimeofday(&tv, NULL);
12024 unsigned int seed = time(NULL);
12025 SNPRINTF(tempname, sizeof(tempname), "%s%c%d", MAIL_TEMP, '/', rand_r(&seed));
12027 char *p = EM_SAFE_STRDUP(tempname);
12029 EM_DEBUG_EXCEPTION(" strdup failed...");
12030 error = EMAIL_ERROR_OUT_OF_MEMORY;
12039 if (err_code != NULL)
12042 EM_DEBUG_FUNC_END("ret [%d]", ret);
12046 INTERNAL_FUNC int emstorage_add_content_type(char *file_path, char *char_set, int *err_code)
12048 EM_DEBUG_FUNC_BEGIN("File path [ %p ] Character Set [ %p ] err_code [ %p]", file_path, char_set, err_code);
12050 EM_IF_NULL_RETURN_VALUE(file_path, false);
12051 EM_IF_NULL_RETURN_VALUE(char_set, false);
12052 EM_IF_NULL_RETURN_VALUE(err_code, false);
12056 struct stat st_buf;
12058 char *low_char_set = NULL;
12059 char *match_str = NULL;
12062 int error = EMAIL_ERROR_NONE;
12063 int data_count_to_written = 0;
12064 char *temp_file_name = NULL;
12066 FILE* fp_src = NULL;
12067 FILE* fp_dest = NULL;
12071 if (stat(file_path, &st_buf) < 0) {
12072 EM_DEBUG_EXCEPTION_SEC(" stat(\"%s\") failed...", file_path);
12073 error = EMAIL_ERROR_SYSTEM_FAILURE;
12077 buf_size = st_buf.st_size;
12078 EM_DEBUG_LOG(">>>> File Size [ %d ] ", buf_size);
12079 buf = (char *)calloc(1, buf_size+1);
12082 EM_DEBUG_LOG(">>> Memory cannot be allocated ");
12086 error = em_fopen(file_path, "rb", &fp_src);
12087 if (error != EMAIL_ERROR_NONE || fp_src == NULL) {
12088 EM_DEBUG_EXCEPTION_SEC(" file_path fopen failed - %s [%d]", file_path, error);
12092 if ((nread = fread(buf, 1, buf_size, fp_src)) > 0) {
12093 if (nread > 0 && nread <= buf_size) {
12094 EM_DEBUG_LOG(">>>> Nread Value [ %d ] ", nread);
12097 * 1.Add check for whether content type is there.
12098 * 2. If not based on the character set, Append it in File
12101 low_char_set = calloc(1, EM_SAFE_STRLEN(char_set) + strlen(" \" /></head>") +1); /*prevent 34359*/
12102 if (low_char_set == NULL) {
12103 EM_DEBUG_EXCEPTION("calloc failed");
12104 error = EMAIL_ERROR_OUT_OF_MEMORY;
12108 strncat(low_char_set, char_set, sizeof(low_char_set)-EM_SAFE_STRLEN(low_char_set)-1);
12109 EM_DEBUG_LOG(">>>> CHAR SET [ %s ] ", low_char_set);
12110 strncat(low_char_set, " \" /></head>", sizeof(low_char_set)-EM_SAFE_STRLEN(low_char_set)-1); /*prevent 34359*/
12111 EM_DEBUG_LOG(">>> CHARSET [ %s ] ", low_char_set);
12114 match_str = strstr(buf, CONTENT_TYPE_DATA);
12116 if (match_str == NULL) {
12117 EM_DEBUG_LOG(">>>>emstorage_add_content_type 3 ");
12118 if (fp_src != NULL) {
12122 data_count_to_written = EM_SAFE_STRLEN(low_char_set)+strlen(CONTENT_DATA)+1; /*prevent 34359*/
12123 buf1 = (char *)calloc(1, data_count_to_written);
12126 strncat(buf1, CONTENT_DATA, sizeof(buf1)-EM_SAFE_STRLEN(buf1)-1); /*prevent 34359*/
12127 EM_DEBUG_LOG(">>>>> BUF 1 [ %s ] ", buf1);
12128 strncat(buf1, low_char_set, sizeof(buf1)-EM_SAFE_STRLEN(buf1)-1);
12129 EM_DEBUG_LOG(">>>> HTML TAG DATA [ %s ] ", buf1);
12131 /* 1. Create a temporary file name */
12132 if (!_get_temp_file_name(&temp_file_name, &err)) {
12133 EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed - %d", err);
12134 if (err_code != NULL) *err_code = err;
12137 EM_DEBUG_LOG_SEC(">>>>>>> TEMP APPEND FILE PATH [ %s ] ", temp_file_name);
12139 /* Open the Temp file in Append mode */
12140 error = em_fopen(temp_file_name, "ab", &fp_dest);
12141 if (error != EMAIL_ERROR_NONE) {
12142 EM_DEBUG_EXCEPTION_SEC(" fopen failed - %s [%d]", temp_file_name, error);
12146 /* 2. write the Latest data */
12147 nwritten = fwrite(buf1, data_count_to_written-1, 1, fp_dest);
12149 if (nwritten > 0) {
12150 EM_DEBUG_LOG(" Latest Data : [%d ] bytes written ", nwritten);
12152 /* 3. Append old data */
12153 nwritten = fwrite(buf, nread-1, 1, fp_dest);
12155 if (nwritten <= 0) {
12156 EM_DEBUG_EXCEPTION(" Error Occured while writing Old data : [%d ] bytes written ", nwritten);
12157 error = EMAIL_ERROR_SYSTEM_FAILURE;
12160 EM_DEBUG_LOG(">>>> OLD data appended [ %d ] ", nwritten);
12162 if (!emstorage_move_file(temp_file_name, file_path, false, &err)) {
12163 EM_DEBUG_EXCEPTION(" emstorage_move_file failed - %d", err);
12169 EM_DEBUG_EXCEPTION(" Error Occured while writing New data : [%d ] bytes written ", nwritten);
12170 error = EMAIL_ERROR_SYSTEM_FAILURE;
12182 EM_SAFE_FREE(buf1);
12183 EM_SAFE_FREE(low_char_set);
12184 EM_SAFE_FREE(temp_file_name);
12186 if (fp_src != NULL) {
12191 if (fp_dest != NULL) {
12199 EM_DEBUG_FUNC_END("ret [%d]", ret);
12204 INTERNAL_FUNC int emstorage_move_file(char *src_file, char *dst_file, int sync_status, int *err_code)
12206 EM_DEBUG_FUNC_BEGIN("src_file[%p], dst_file[%p], err_code[%p]", src_file, dst_file, err_code);
12209 int error = EMAIL_ERROR_NONE;
12210 char errno_buf[ERRNO_BUF_SIZE] = {0};
12212 if (src_file == NULL || dst_file == NULL) {
12213 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
12214 error = EMAIL_ERROR_INVALID_PARAM;
12218 EM_DEBUG_LOG_SEC("src_file[%s], dst_file[%s]", src_file, dst_file);
12220 if (strcmp(src_file, dst_file) != 0) {
12221 EM_DEBUG_LOG("oldpath and newpath are not on the same mounted file system.");
12222 if (!emstorage_copy_file(src_file, dst_file, sync_status, &error)) {
12223 EM_DEBUG_EXCEPTION("emstorage_copy_file failed - %d", error);
12225 struct stat temp_file_stat;
12226 if (stat(src_file, &temp_file_stat) < 0)
12227 EM_DEBUG_EXCEPTION("no src file found [%s] : %s", src_file, EM_STRERROR(errno_buf));
12229 if (stat(dst_file, &temp_file_stat) < 0)
12230 EM_DEBUG_EXCEPTION("no dst file found [%s] : %s", dst_file, EM_STRERROR(errno_buf));
12232 error = EMAIL_ERROR_FILE_NOT_FOUND;
12238 EM_DEBUG_LOG("src[%s] removed", src_file);
12240 EM_DEBUG_LOG("src[%s] = dst[%s]", src_file, dst_file);
12246 if (err_code != NULL)
12249 EM_DEBUG_FUNC_END("ret [%d]", ret);
12253 INTERNAL_FUNC int emstorage_delete_file(char *src_file, int *err_code)
12255 EM_DEBUG_FUNC_BEGIN("src_file[%p], err_code[%p]", src_file, err_code);
12258 int error = EMAIL_ERROR_NONE;
12260 if (src_file == NULL) {
12261 EM_DEBUG_EXCEPTION(" src_file[%p]", src_file);
12263 error = EMAIL_ERROR_INVALID_PARAM;
12267 if (remove(src_file) != 0) {
12268 if (errno != ENOENT) {
12269 EM_DEBUG_EXCEPTION(" remove failed - %d", errno);
12271 error = EMAIL_ERROR_SYSTEM_FAILURE;
12274 EM_DEBUG_EXCEPTION(" no file found...");
12276 error = EMAIL_ERROR_FILE_NOT_FOUND;
12283 if (err_code != NULL)
12286 EM_DEBUG_FUNC_END("ret [%d]", ret);
12291 INTERNAL_FUNC int emstorage_delete_dir(char *src_dir, int *err_code)
12293 EM_DEBUG_FUNC_BEGIN("src_dir[%p], err_code[%p]", src_dir, err_code);
12295 if (src_dir == NULL) {
12296 EM_DEBUG_EXCEPTION("src_dir[%p]", src_dir);
12298 if (err_code != NULL)
12299 *err_code = EMAIL_ERROR_INVALID_PARAM;
12303 int error = EMAIL_ERROR_NONE;
12310 dirp = opendir(src_dir);
12312 if (dirp == NULL) {
12313 if (errno == ENOENT) {
12314 EM_DEBUG_EXCEPTION("directory[%s] does not exist...", src_dir);
12315 if (err_code != NULL)
12316 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12319 EM_DEBUG_EXCEPTION("opendir failed [%s] [%d]", src_dir, errno);
12320 if (err_code != NULL)
12321 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12326 while ((dp = readdir(dirp))) {
12327 if (strncmp(dp->d_name, ".", 1) == 0 || strncmp(dp->d_name, "..", 2) == 0) /* prevent 34360 */
12330 SNPRINTF(buf, sizeof(buf), "%s/%s", src_dir, dp->d_name);
12332 if (lstat(buf, &sbuf) == 0 || stat(buf, &sbuf) == 0) {
12333 /* check directory */
12334 if ((sbuf.st_mode & S_IFMT) == S_IFDIR) { /* directory */
12335 /* recursive call */
12336 if (!emstorage_delete_dir(buf, &error)) {
12338 if (err_code != NULL)
12342 } else { /* file */
12343 if (remove(buf) < 0) {
12344 EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", buf, errno);
12346 if (err_code != NULL)
12347 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12352 EM_DEBUG_EXCEPTION("content does not exist...");
12357 EM_DEBUG_LOG_DEV("remove direcotory [%s]", src_dir);
12359 /* EM_DEBUG_FUNC_BEGIN(); */
12361 if (remove(src_dir) < 0) {
12362 EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", src_dir, errno);
12363 if (err_code != NULL)
12364 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12368 if (err_code != NULL)
12374 /* faizan.h@samsung.com */
12375 INTERNAL_FUNC int emstorage_update_server_uid(char *multi_user_name,
12377 char *old_server_uid,
12378 char *new_server_uid,
12381 EM_DEBUG_FUNC_BEGIN("new_server_uid[%s], old_server_uid[%s]", new_server_uid, old_server_uid);
12383 int error = EMAIL_ERROR_NONE;
12384 int transaction = true;
12385 int temp_strlen = 0;
12386 int and_operation = 0;
12387 char sql_query_string[QUERY_SIZE] = {0, };
12388 char conditional_clause_string[QUERY_SIZE] = {0};
12390 if ((mail_id <= 0 || !old_server_uid) && !new_server_uid) {
12391 EM_DEBUG_EXCEPTION("Invalid parameters");
12393 *err_code = EMAIL_ERROR_INVALID_PARAM;
12397 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12398 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12400 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE ");
12403 temp_strlen = strlen(conditional_clause_string);
12404 SNPRINTF(conditional_clause_string + temp_strlen, sizeof(conditional_clause_string) - temp_strlen,
12405 "mail_id = %d ", mail_id);
12409 if (old_server_uid) {
12410 temp_strlen = strlen(conditional_clause_string);
12411 if (!and_operation) {
12412 sqlite3_snprintf(sizeof(conditional_clause_string) - temp_strlen, conditional_clause_string + temp_strlen,
12413 "server_mail_id = '%q'", old_server_uid);
12415 sqlite3_snprintf(sizeof(conditional_clause_string) - temp_strlen, conditional_clause_string + temp_strlen,
12416 "and server_mail_id = '%q'", old_server_uid);
12420 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
12421 "UPDATE mail_tbl SET server_mail_id = '%q' %s", new_server_uid, conditional_clause_string);
12423 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12424 if (error != EMAIL_ERROR_NONE) {
12425 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12433 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12435 if (err_code != NULL)
12438 EM_DEBUG_FUNC_END("ret [%d]", ret);
12443 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)
12445 EM_DEBUG_FUNC_BEGIN_SEC("mail_id[%d], new_server_uid[%s], mbox_name[%s]", mail_id, new_server_uid, mbox_name);
12448 int error = EMAIL_ERROR_NONE;
12449 char sql_query_string[QUERY_SIZE] = {0, };
12450 int transaction = true;
12452 if (!mail_id || !new_server_uid || !mbox_name) {
12453 EM_DEBUG_EXCEPTION("Invalid parameters");
12454 if (err_code != NULL)
12455 *err_code = EMAIL_ERROR_INVALID_PARAM;
12459 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12460 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12463 SNPRINTF(sql_query_string, sizeof(sql_query_string),
12464 "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);
12466 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12467 if (error != EMAIL_ERROR_NONE) {
12468 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12476 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12478 if (err_code != NULL)
12481 EM_DEBUG_FUNC_END("ret [%d]", ret);
12486 INTERNAL_FUNC int emstorage_update_save_status(char *multi_user_name, int account_id, int *err_code)
12488 EM_DEBUG_FUNC_BEGIN();
12491 int transaction = true;
12492 int error = EMAIL_ERROR_NONE;
12493 char sql_query_string[QUERY_SIZE] = {0,};
12495 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12497 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12499 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12501 if (account_id <= ALL_ACCOUNT)
12502 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);
12504 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);
12506 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12507 if (error != EMAIL_ERROR_NONE) {
12508 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12515 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12517 if (err_code != NULL)
12520 EM_DEBUG_FUNC_END("ret [%d]", ret);
12525 int emstorage_get_unread_mailid(char *multi_user_name, int account_id, int vip_mode, int **mail_ids, int *mail_number, int *err_code)
12527 EM_DEBUG_FUNC_BEGIN();
12529 if ((!mail_ids) || (account_id <= 0 && account_id != -1)) {
12530 EM_DEBUG_EXCEPTION(" mail_id[%p], account_id[%d] ", mail_ids, account_id);
12531 if (err_code != NULL)
12532 *err_code = EMAIL_ERROR_INVALID_PARAM;
12538 int error = EMAIL_ERROR_NONE;
12542 int *p_mail_ids = NULL;
12543 int transaction = false;
12544 char **result = NULL;
12545 char sql_query_string[QUERY_SIZE] = {0, };
12546 char temp_query_string[QUERY_SIZE] = {0,};
12547 char sql_select_query_string[QUERY_SIZE] = {0, };
12549 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12550 EMSTORAGE_START_READ_TRANSACTION(transaction);
12552 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12554 if (account_id == -1) {
12555 SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string),
12556 "SELECT mail_id FROM mail_tbl WHERE flags_seen_field = 0 AND (save_status = %d or save_status = %d)",
12557 EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12559 SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string),
12560 "SELECT mail_id FROM mail_tbl WHERE account_id = %d AND flags_seen_field = 0 AND (save_status = %d or save_status = %d)",
12561 account_id, EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12565 SNPRINTF(temp_query_string, sizeof(temp_query_string),
12566 "%s AND tag_id < 0", sql_select_query_string);
12568 SNPRINTF(temp_query_string, sizeof(temp_query_string), "%s", sql_select_query_string);
12571 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s ORDER BY date_time ASC", temp_query_string);
12573 EM_DEBUG_LOG_SEC("query: [%s]", sql_query_string);
12575 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
12576 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
12577 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12579 EM_DEBUG_LOG("Count : %d", count);
12582 EM_DEBUG_EXCEPTION("no Mails found...");
12584 error = EMAIL_ERROR_MAIL_NOT_FOUND;
12588 p_mail_ids = em_malloc(count * sizeof(int));
12589 if (p_mail_ids == NULL) {
12590 EM_DEBUG_EXCEPTION("em_mallocfailed...");
12591 error = EMAIL_ERROR_OUT_OF_MEMORY;
12597 for (i = 0; i < count; i++)
12598 _get_table_field_data_int(result, &(p_mail_ids[i]), col_index++);
12605 sqlite3_free_table(result);
12607 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12609 // sqlite3_db_release_memory(local_db_handle);
12612 if (mail_ids != NULL)
12613 *mail_ids = p_mail_ids;
12615 if (mail_number != NULL)
12616 *mail_number = count;
12618 EM_SAFE_FREE(p_mail_ids);
12621 if (err_code != NULL)
12624 EM_DEBUG_FUNC_END("ret [%d]", ret);
12628 int setting_system_command(const char *command)
12631 int pid = 0, status = 0;
12632 char *const environ[] = { NULL };
12647 argv[2] = (char *)command;
12650 execve("/bin/sh", argv, environ);
12655 if (waitpid(pid, &status, 0) == -1) {
12656 if (errno != EINTR)
12667 INTERNAL_FUNC int emstorage_mail_get_total_diskspace_usage(unsigned long *total_usage, int transaction, int *err_code)
12669 EM_DEBUG_FUNC_BEGIN("total_usage[%p], transaction[%d], err_code[%p]", total_usage, transaction, err_code);
12671 if (!total_usage) {
12672 EM_DEBUG_EXCEPTION("total_usage[%p]", total_usage);
12674 if (err_code != NULL)
12675 *err_code = EMAIL_ERROR_INVALID_PARAM;
12680 int error = EMAIL_ERROR_NONE;
12681 char syscmd[256] = {0, };
12682 char line[256] = {0, };
12683 char *line_from_file = NULL;
12685 unsigned long total_diskusage = 0;
12687 SNPRINTF(syscmd, sizeof(syscmd), "touch %s", SETTING_MEMORY_TEMP_FILE_PATH);
12688 if (setting_system_command(syscmd) == -1) {
12689 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
12691 error = EMAIL_ERROR_SYSTEM_FAILURE;
12695 SNPRINTF(syscmd, sizeof(syscmd), "du -hsk %s > %s", EMAIL_PATH, SETTING_MEMORY_TEMP_FILE_PATH);
12696 EM_DEBUG_LOG(" cmd : %s", syscmd);
12697 if (setting_system_command(syscmd) == -1) {
12698 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : Setting > Memory] System Command [%s] is failed", syscmd);
12700 error = EMAIL_ERROR_SYSTEM_FAILURE;
12704 error = em_fopen(SETTING_MEMORY_TEMP_FILE_PATH, "r", &fp);
12705 if (error != EMAIL_ERROR_NONE) {
12706 perror(SETTING_MEMORY_TEMP_FILE_PATH);
12710 line_from_file = fgets(line, sizeof(line), fp);
12712 if (line_from_file == NULL) {
12713 EM_DEBUG_EXCEPTION("fgets failed");
12714 error = EMAIL_ERROR_SYSTEM_FAILURE;
12717 total_diskusage = strtoul(line, NULL, 10);
12719 memset(syscmd, 0, sizeof(syscmd));
12720 SNPRINTF(syscmd, sizeof(syscmd), "rm -f %s", SETTING_MEMORY_TEMP_FILE_PATH);
12721 if (setting_system_command(syscmd) == -1) {
12722 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
12723 error = EMAIL_ERROR_SYSTEM_FAILURE;
12727 EM_DEBUG_LOG("[Setting > Memory] @@@@@ Size of Directory [%s] is %ld KB", EMAIL_PATH, total_diskusage);
12732 if (err_code != NULL)
12736 *total_usage = total_diskusage;
12740 if (fp) fclose(fp); /* prevent 32730 */
12742 EM_DEBUG_FUNC_END("ret [%d]", ret);
12745 #define MAILHOME_UTF8 tzplatform_mkpath(TZ_USER_DATA, "email/.email_data/7/348/UTF-8")
12747 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)
12749 DB_STMT hStmt = NULL;
12751 int error = EMAIL_ERROR_NONE;
12753 char sql_query_string[QUERY_SIZE] = {0, };
12755 SNPRINTF(sql_query_string, sizeof(sql_query_string),
12756 "INSERT INTO mail_tbl VALUES "
12758 ", ?" /* account_id */
12759 ", ?" /* mail_size */
12760 ", ?" /* server_mail_status */
12761 ", ?" /* server_mailbox_name */
12762 ", ?" /* server_mail_id */
12763 ", ?" /* reference_mail_id */
12764 ", ?" /* full_address_from */
12765 ", ?" /* full_address_reply */
12766 ", ?" /* full_address_to */
12767 ", ?" /* full_address_cc */
12768 ", ?" /* full_address_bcc */
12769 ", ?" /* full_address_return */
12770 ", ?" /* subject */
12771 ", ?" /* body_download_status */
12772 ", ?" /* file_path_plain */
12773 ", ?" /* file_path_html */
12774 ", ?" /* date_time */
12775 ", ?" /* flags_seen_field */
12776 ", ?" /* flags_deleted_field */
12777 ", ?" /* flags_flagged_field */
12778 ", ?" /* flags_answered_field */
12779 ", ?" /* flags_recent_field */
12780 ", ?" /* flags_draft_field */
12781 ", ?" /* flags_forwarded_field */
12782 ", ?" /* DRM_status */
12783 ", ?" /* priority */
12784 ", ?" /* save_status */
12785 ", ?" /* lock_status */
12786 ", ?" /* message_id */
12787 ", ?" /* report_status */
12788 ", ?" /* email_address_sender */
12789 ", ?" /* email_address_recipient */
12790 ", ?" /* attachment_count */
12791 ", ?" /* inline_content_count */
12792 ", ?" /* preview_text */
12793 ", ?" /* thread_id */
12794 ", ?" /* mailbox_type */
12795 ", ?" /* alias_sender */
12796 ", ?" /* alias_recipient */
12797 ", ?" /* thread_item_count */
12798 ", ?" /* meeting_request_status */
12799 ", ?" /* message_class */
12800 ", ?" /* digest_type */
12801 ", ?" /* smime_type */
12802 ", ?" /* scheduled_sending_time */
12803 ", ?" /* remaining_resend_times */
12805 ", ?" /* replied_time */
12806 ", ?" /* forwarded_time */
12807 ", ?" /* default_charset */
12808 ", ?" /* eas_data_length */
12809 ", ?" /* eas_data */
12812 int transaction = true;
12813 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12815 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12818 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12819 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
12820 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12822 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_id);
12823 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, account_id);
12824 _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, EMAIL_MAILBOX_TYPE_OUTBOX);
12825 _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, "save test - long", 1, SUBJECT_LEN_IN_MAIL_TBL);
12826 _bind_stmt_field_data_string(hStmt, DATETIME_IDX_IN_MAIL_TBL, "20100316052908", 0, DATETIME_LEN_IN_MAIL_TBL);
12827 _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, 0);
12828 _bind_stmt_field_data_string(hStmt, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL, "", 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
12829 _bind_stmt_field_data_string(hStmt, SERVER_MAIL_ID_IDX_IN_MAIL_TBL, "", 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
12830 _bind_stmt_field_data_string(hStmt, MESSAGE_ID_IDX_IN_MAIL_TBL, "", 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
12831 _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, 0);
12832 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL, "<test08@streaming.s3glab.net>", 1, FROM_LEN_IN_MAIL_TBL);
12833 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL, "", 1, REPLY_TO_LEN_IN_MAIL_TBL);
12834 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL, full_address_to, 1, TO_LEN_IN_MAIL_TBL);
12835 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL, full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
12836 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL, full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
12837 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL, "", 1, RETURN_PATH_LEN_IN_MAIL_TBL);
12838 _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);
12839 _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);
12840 _bind_stmt_field_data_string(hStmt, ALIAS_SENDER_IDX_IN_MAIL_TBL, "send_alias", 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12841 _bind_stmt_field_data_string(hStmt, ALIAS_RECIPIENT_IDX_IN_MAIL_TBL, "recipient_alias", 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12842 _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, 1);
12843 _bind_stmt_field_data_string(hStmt, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL, (char *)MAILHOME_UTF8, 0, TEXT_1_LEN_IN_MAIL_TBL);
12844 _bind_stmt_field_data_string(hStmt, FILE_PATH_HTML_IDX_IN_MAIL_TBL, "", 0, TEXT_2_LEN_IN_MAIL_TBL);
12845 _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, 4);
12846 _bind_stmt_field_data_char(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, 0);
12847 _bind_stmt_field_data_char(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, 0);
12848 _bind_stmt_field_data_char(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, 0);
12849 _bind_stmt_field_data_char(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, 0);
12850 _bind_stmt_field_data_char(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, 0);
12851 _bind_stmt_field_data_char(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, 0);
12852 _bind_stmt_field_data_char(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, 0);
12853 _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, 0);
12854 _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, 0);
12855 _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, 0);
12856 _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, 0);
12857 _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, 0);
12858 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12859 _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, 0);
12860 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12861 _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, 0);
12862 _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, 0);
12863 _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, "preview body", 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
12864 _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, 0);
12865 _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, 0);
12866 _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, 0);
12867 _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, 0);
12868 _bind_stmt_field_data_int(hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, 0);
12869 _bind_stmt_field_data_int(hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, 0);
12870 _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, 0);
12871 _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, 0);
12872 _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, 0);
12873 _bind_stmt_field_data_string(hStmt, DEFAULT_CHARSET_IDX_IN_MAIL_TBL, "UTF-8", 0, TEXT_2_LEN_IN_MAIL_TBL);
12874 _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, 0);
12875 _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, NULL, 0);
12877 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12878 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
12879 ("sqlite3_step fail:%d", rc));
12880 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
12881 ("sqlite3_step fail:%d", rc));
12885 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12886 if (hStmt != NULL) {
12887 rc = sqlite3_finalize(hStmt);
12888 if (rc != SQLITE_OK) {
12889 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
12890 error = EMAIL_ERROR_DB_FAILURE;
12894 if (err_code != NULL)
12897 EM_DEBUG_FUNC_END("ret [%d]", ret);
12902 INTERNAL_FUNC int emstorage_get_max_mail_count()
12904 return EMAIL_MAIL_MAX_COUNT;
12907 #define STRIPPED_SUBJECT_BUFFER_SIZE 4086
12909 INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(char *multi_user_name,
12910 emstorage_mail_tbl_t *mail_tbl,
12912 int *result_latest_mail_id_in_thread,
12913 int *thread_item_count)
12915 EM_DEBUG_FUNC_BEGIN("mail_tbl [%p], thread_id [%p], "
12916 "result_latest_mail_id_in_thread [%p], thread_item_count [%p]",
12917 mail_tbl, thread_id, result_latest_mail_id_in_thread, thread_item_count);
12918 EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
12920 int rc = 0, query_size = 0, query_size_account = 0;
12922 int search_thread = false;
12923 int account_id = 0;
12924 int err_code = EMAIL_ERROR_NONE;
12925 int count = 0, result_thread_id = -1, latest_mail_id_in_thread = -1;
12926 time_t latest_date_time = 0;
12927 char *subject = NULL;
12928 char *p_subject = NULL;
12929 char *sql_query_string = NULL, *sql_account = NULL;
12931 int temp_thread_id = -1;
12932 char *sql_format = "SELECT mail_id, thread_id, date_time, subject "
12933 "FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
12934 char *sql_format_account = " AND account_id = %d ";
12935 char *sql_format_order_by = " ORDER BY thread_id, date_time DESC ";
12936 char **result = NULL;
12937 char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
12938 char stripped_subject2[STRIPPED_SUBJECT_BUFFER_SIZE];
12940 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12942 EM_DEBUG_LOG("subject: [%p], mail_id: [%d]", subject, mail_tbl->mail_id);
12944 EM_IF_NULL_RETURN_VALUE(mail_tbl, EMAIL_ERROR_INVALID_PARAM);
12945 EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
12946 EM_IF_NULL_RETURN_VALUE(result_latest_mail_id_in_thread, EMAIL_ERROR_INVALID_PARAM);
12947 EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
12949 account_id = mail_tbl->account_id;
12950 subject = mail_tbl->subject;
12952 EM_DEBUG_LOG_SEC("subject: [%s]", subject);
12954 if (EM_SAFE_STRLEN(subject) == 0 && mail_tbl->mail_id != 0) {
12955 result_thread_id = mail_tbl->mail_id;
12960 if (em_find_pos_stripped_subject_for_thread_view(subject,
12962 STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE) {
12963 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
12964 err_code = EMAIL_ERROR_UNKNOWN;
12965 result_thread_id = -1;
12969 EM_DEBUG_LOG_SEC("stripped_subject: [%s]", stripped_subject);
12971 if (EM_SAFE_STRLEN(stripped_subject) == 0) {
12972 result_thread_id = -1;
12976 EM_DEBUG_LOG_SEC("em_find_pos_stripped_subject_for_thread_view returns[len = %zu] = %s",
12977 EM_SAFE_STRLEN(stripped_subject), stripped_subject);
12979 if (account_id > 0) {
12980 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
12981 sql_account = malloc(query_size_account);
12982 if (sql_account == NULL) {
12983 EM_DEBUG_EXCEPTION("malloc for sql_account is failed %d", query_size_account);
12984 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
12987 snprintf(sql_account, query_size_account, sql_format_account, account_id);
12990 /* prevent 34362 */
12991 query_size = strlen(sql_format) + strlen(stripped_subject)*2 + 50 + query_size_account + strlen(sql_format_order_by); /* + query_size_mailbox; */
12992 sql_query_string = malloc(query_size);
12994 if (sql_query_string == NULL) {
12995 EM_DEBUG_EXCEPTION("malloc for sql is failed %d", query_size);
12996 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
13000 sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mail_tbl->mailbox_id);
13002 if (account_id > 0)
13003 EM_SAFE_STRNCAT(sql_query_string, sql_account, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13005 EM_SAFE_STRNCAT(sql_query_string, sql_format_order_by, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13006 EM_SAFE_STRNCAT(sql_query_string, ";", query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13008 EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
13010 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL),
13013 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13014 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13016 EM_DEBUG_LOG("Result rows count : %d", count);
13019 result_thread_id = -1;
13021 for (i = 0; i < count; i++) {
13022 EM_SAFE_FREE(p_subject);
13024 _get_table_field_data_int(result, &latest_mail_id_in_thread, col_index++);
13025 _get_table_field_data_int(result, &result_thread_id, col_index++);
13026 _get_table_field_data_time_t(result, &latest_date_time, col_index++);
13027 _get_table_field_data_string(result, &p_subject, 0, col_index++);
13029 if (temp_thread_id == result_thread_id)
13032 temp_thread_id = result_thread_id;
13034 if (em_find_pos_stripped_subject_for_thread_view(p_subject,
13036 STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE) {
13037 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
13038 err_code = EMAIL_ERROR_UNKNOWN;
13039 result_thread_id = -1;
13043 if (g_strcmp0(stripped_subject2, stripped_subject) == 0) {
13044 if (latest_date_time < mail_tbl->date_time)
13045 *result_latest_mail_id_in_thread = latest_mail_id_in_thread;
13047 *result_latest_mail_id_in_thread = mail_tbl->mail_id;
13049 search_thread = true;
13052 if (search_thread) {
13053 EM_DEBUG_LOG("latest_mail_id_in_thread [%d], mail_id [%d]",
13054 latest_mail_id_in_thread, mail_tbl->mail_id);
13057 result_thread_id = -1;
13066 *thread_id = result_thread_id;
13067 *thread_item_count = count;
13069 EM_DEBUG_LOG("Result thread id : %d", *thread_id);
13070 EM_DEBUG_LOG("Result count : %d", *thread_item_count);
13071 EM_DEBUG_LOG("err_code : %d", err_code);
13073 EM_SAFE_FREE(sql_account);
13074 EM_SAFE_FREE(sql_query_string);
13075 EM_SAFE_FREE(p_subject);
13077 sqlite3_free_table(result);
13079 EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
13084 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)
13086 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);
13087 EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
13090 int query_size = 0;
13091 int query_size_account = 0;
13092 int err_code = EMAIL_ERROR_NONE;
13094 int result_thread_id = -1;
13095 char *sql_query_string = NULL;
13096 char *sql_account = NULL;
13097 char *sql_format = "SELECT thread_id FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
13098 char *sql_format_account = " AND account_id = %d ";
13099 char *sql_format_order_by = " ORDER BY date_time DESC ";
13100 char **result = NULL;
13101 char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
13102 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13104 EM_IF_NULL_RETURN_VALUE(mail_subject, EMAIL_ERROR_INVALID_PARAM);
13105 EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
13106 EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
13108 EM_DEBUG_LOG_SEC("subject: [%s]", mail_subject);
13110 if (em_find_pos_stripped_subject_for_thread_view(mail_subject, stripped_subject, STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE) {
13111 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
13112 err_code = EMAIL_ERROR_UNKNOWN;
13113 result_thread_id = -1;
13117 if (EM_SAFE_STRLEN(stripped_subject) == 0) {
13118 result_thread_id = -1;
13122 EM_DEBUG_LOG("em_find_pos_stripped_subject_for_thread_view returns[len = %zu] = %s", EM_SAFE_STRLEN(stripped_subject), stripped_subject);
13124 if (account_id > 0) {
13125 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
13126 sql_account = malloc(query_size_account);
13127 if (sql_account == NULL) {
13128 EM_DEBUG_EXCEPTION("malloc for sql_account is failed %d", query_size_account);
13129 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
13132 snprintf(sql_account, query_size_account, sql_format_account, account_id);
13135 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; */
13136 sql_query_string = malloc(query_size);
13138 if (sql_query_string == NULL) {
13139 EM_DEBUG_EXCEPTION("malloc for sql is failed %d", query_size);
13140 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
13144 sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mailbox_id);
13146 if (account_id > 0)
13147 EM_SAFE_STRNCAT(sql_query_string, sql_account, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13148 EM_SAFE_STRNCAT(sql_query_string, sql_format_order_by, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13149 EM_SAFE_STRNCAT(sql_query_string, ";", query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13151 EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
13153 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
13155 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13156 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13158 EM_DEBUG_LOG("Result rows count : %d", count);
13161 result_thread_id = -1;
13163 _get_table_field_data_int(result, &result_thread_id, 1);
13166 *thread_id = result_thread_id;
13167 *thread_item_count = count;
13169 EM_DEBUG_LOG("Result thread id : %d", *thread_id);
13170 EM_DEBUG_LOG("Result count : %d", *thread_item_count);
13171 EM_DEBUG_LOG("err_code : %d", err_code);
13173 EM_SAFE_FREE(sql_account);
13174 EM_SAFE_FREE(sql_query_string);
13176 sqlite3_free_table(result);
13178 EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
13183 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)
13185 EM_DEBUG_FUNC_BEGIN();
13187 int count = 0, ret = false;
13188 int error = EMAIL_ERROR_NONE;
13189 emstorage_mail_tbl_t *p_data_tbl = NULL;
13190 char conditional_clause[QUERY_SIZE] = {0, };
13192 EM_IF_NULL_RETURN_VALUE(mail_tbl, false);
13194 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE thread_id = %d AND thread_item_count > 0", thread_id);
13195 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
13197 if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
13198 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
13203 EM_DEBUG_LOG("thread_id : %d, thread_item_count : %d", p_data_tbl[0].thread_id, p_data_tbl[0].thread_item_count);
13209 *mail_tbl = p_data_tbl;
13210 else if (p_data_tbl != NULL)
13211 emstorage_free_mail(&p_data_tbl, 1, NULL);
13213 if (err_code != NULL)
13216 EM_DEBUG_FUNC_END("ret [%d]", ret);
13220 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)
13222 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]"
13223 , account_id , mailbox_id , search_type , search_value , sorting , sender_list, sender_count, err_code);
13225 if ((!sender_list) || (!sender_count)) {
13226 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
13227 if (err_code != NULL)
13228 *err_code = EMAIL_ERROR_INVALID_PARAM;
13232 int rc = -1, ret = false;
13233 int error = EMAIL_ERROR_NONE;
13235 int i, col_index = 0;
13236 int read_count = 0;
13237 email_sender_list_t *p_sender_list = NULL;
13238 char sql_query_string[QUERY_SIZE] = {0, };
13239 char **result = NULL;
13240 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13242 SNPRINTF(sql_query_string, sizeof(sql_query_string),
13243 "SELECT email_address_sender, alias_sender, COUNT(email_address_sender), SUM(flags_seen_field = 1) "
13248 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);
13249 else /* NULL means all mailbox_name. but except for trash(3), spambox(5), all emails(for GMail, 7) */
13250 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) ");
13253 /* '0' (ALL_ACCOUNT) means all account */
13254 if (account_id > ALL_ACCOUNT)
13255 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);
13257 if (search_value) {
13258 switch (search_type) {
13259 case EMAIL_SEARCH_FILTER_SUBJECT:
13260 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13261 " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\')) ", search_value);
13263 case EMAIL_SEARCH_FILTER_SENDER:
13264 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13265 " AND ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13266 ") ", search_value);
13268 case EMAIL_SEARCH_FILTER_RECIPIENT:
13269 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13270 " AND ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13271 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13272 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13273 ") ", search_value, search_value, search_value);
13275 case EMAIL_SEARCH_FILTER_ALL:
13276 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13277 " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') "
13278 " OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13279 " OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13280 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13281 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13284 ")", search_value, search_value, search_value, search_value, search_value);
13290 /* sorting option is not available now. The order of sender list is ascending order by display name */
13291 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
13292 "GROUP BY email_address_sender "
13293 "ORDER BY UPPER(alias_sender) ");
13295 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
13297 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
13298 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13299 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13301 EM_DEBUG_LOG("Count of Sender [%d]", count);
13303 if (!(p_sender_list = (email_sender_list_t*)em_malloc(sizeof(email_sender_list_t) * count))) {
13304 EM_DEBUG_EXCEPTION("em_mallocfailed...");
13305 error = EMAIL_ERROR_OUT_OF_MEMORY;
13311 EM_DEBUG_LOG(">>>> DATA ASSIGN START >>");
13312 for (i = 0; i < count; i++) {
13313 _get_table_field_data_string(result, &(p_sender_list[i].address), 1, col_index++);
13314 _get_table_field_data_string(result, &(p_sender_list[i].display_name), 1, col_index++);
13315 _get_table_field_data_int(result, &(p_sender_list[i].total_count), col_index++);
13316 _get_table_field_data_int(result, &(read_count), col_index++);
13317 p_sender_list[i].unread_count = p_sender_list[i].total_count - read_count; /* unread count = total - read */
13319 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >>", count);
13321 sqlite3_free_table(result);
13328 *sender_list = p_sender_list;
13329 *sender_count = count;
13330 EM_DEBUG_LOG(">>>> COUNT : %d >>", count);
13334 if (err_code != NULL)
13337 EM_DEBUG_FUNC_END("ret [%d]", ret);
13341 INTERNAL_FUNC int emstorage_free_sender_list(email_sender_list_t **sender_list, int count)
13343 EM_DEBUG_FUNC_BEGIN("sender_list[%p], count[%d]", sender_list, count);
13345 int err = EMAIL_ERROR_NONE;
13348 if (!sender_list || !*sender_list) {
13349 EM_DEBUG_EXCEPTION("sender_list[%p], count[%d]", sender_list, count);
13350 err = EMAIL_ERROR_INVALID_PARAM;
13354 email_sender_list_t* p = *sender_list;
13357 for (; i < count; i++) {
13358 EM_SAFE_FREE(p[i].address);
13359 EM_SAFE_FREE(p[i].display_name);
13363 *sender_list = NULL;
13370 INTERNAL_FUNC int emstorage_free_address_info_list(email_address_info_list_t **address_info_list)
13372 EM_DEBUG_FUNC_BEGIN("address_info_list[%p]", address_info_list);
13374 int err = EMAIL_ERROR_NONE;
13375 email_address_info_t *p_address_info = NULL;
13376 GList *list = NULL;
13377 GList *node = NULL;
13380 if (!address_info_list || !*address_info_list) {
13381 EM_DEBUG_EXCEPTION("address_info_list[%p]", address_info_list);
13382 err = EMAIL_ERROR_INVALID_PARAM;
13386 /* delete GLists */
13387 for (i = EMAIL_ADDRESS_TYPE_FROM; i <= EMAIL_ADDRESS_TYPE_BCC; i++) {
13389 case EMAIL_ADDRESS_TYPE_FROM:
13390 list = (*address_info_list)->from;
13392 case EMAIL_ADDRESS_TYPE_TO:
13393 list = (*address_info_list)->to;
13395 case EMAIL_ADDRESS_TYPE_CC:
13396 list = (*address_info_list)->cc;
13398 case EMAIL_ADDRESS_TYPE_BCC:
13399 list = (*address_info_list)->bcc;
13403 /* delete dynamic-allocated memory for each item */
13404 node = g_list_first(list);
13405 while (node != NULL) {
13406 p_address_info = (email_address_info_t*)node->data;
13407 EM_SAFE_FREE(p_address_info->address);
13408 EM_SAFE_FREE(p_address_info->display_name);
13409 EM_SAFE_FREE(node->data);
13411 node = g_list_next(node);
13416 EM_SAFE_FREE(*address_info_list);
13417 *address_info_list = NULL;
13419 EM_DEBUG_FUNC_END("err [%d]", err);
13422 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
13424 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)
13426 EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
13428 if (!local_activity || !activity_id) {
13429 EM_DEBUG_EXCEPTION("local_activity[%p], transaction[%d], activity_id[%p], err_code[%p]", local_activity, transaction, activity_id, err_code);
13430 if (err_code != NULL)
13431 *err_code = EMAIL_ERROR_INVALID_PARAM;
13437 int error = EMAIL_ERROR_NONE;
13440 char sql_query_string[QUERY_SIZE] = {0, };
13441 DB_STMT hStmt = NULL;
13442 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13444 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13445 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13446 SNPRINTF(sql_query_string, sizeof(sql_query_string),
13447 "INSERT INTO mail_partial_body_activity_tbl VALUES "
13449 "? " /* Account ID */
13450 ",?" /* Local Mail ID */
13451 ",?" /* Server mail ID */
13452 ",?" /* Activity ID */
13453 ",?" /* Activity type*/
13454 ",?" /* Mailbox ID*/
13455 ",?" /* Mailbox name*/
13456 ",?" /* Multi User Name */
13459 char *sql = "SELECT max(rowid) FROM mail_partial_body_activity_tbl;";
13460 char **result = NULL;
13463 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13465 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13466 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13468 if (NULL == result[1]) rc = 1;
13469 else rc = atoi(result[1])+1;
13470 sqlite3_free_table(result);
13473 *activity_id = local_activity->activity_id = rc;
13475 EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [ %d ], MAIL ID [ %d ], ACTIVITY TYPE [ %d ], SERVER MAIL ID [ %lu ]", \
13476 local_activity->activity_id, local_activity->mail_id, local_activity->activity_type, local_activity->server_mail_id);
13478 if (local_activity->mailbox_id)
13479 EM_DEBUG_LOG(" MAILBOX ID [ %d ]", local_activity->mailbox_id);
13482 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13483 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13484 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13487 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
13488 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
13489 _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
13490 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
13491 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
13492 _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
13493 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->mailbox_name, 0, 3999);
13494 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
13497 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13499 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
13500 ("sqlite3_step fail:%d", rc));
13501 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13502 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
13507 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13508 if (hStmt != NULL) {
13509 rc = sqlite3_finalize(hStmt);
13511 if (rc != SQLITE_OK) {
13512 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13513 error = EMAIL_ERROR_DB_FAILURE;
13517 if (err_code != NULL)
13520 EM_DEBUG_FUNC_END("ret [%d]", ret);
13524 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)
13526 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13528 if (account_id < FIRST_ACCOUNT_ID || NULL == mailbox_list || *mailbox_list == NULL || NULL == count) {
13529 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13530 if (err_code != NULL)
13531 *err_code = EMAIL_ERROR_INVALID_PARAM;
13536 int error = EMAIL_ERROR_NONE;
13538 int i = 0, rc = -1;
13539 int *mbox_list = NULL;
13540 DB_STMT hStmt = NULL;
13541 char sql_query_string[QUERY_SIZE] = {0, };
13543 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13544 EMSTORAGE_START_READ_TRANSACTION(transaction);
13546 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13547 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);
13550 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13551 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13552 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13554 *count = atoi(result[1]);
13555 sqlite3_free_table(result);
13558 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
13559 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13563 EM_DEBUG_LOG("Mailbox count = %d", *count);
13565 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13567 /* 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); */
13568 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);
13570 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13573 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13576 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13577 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13580 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13581 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13582 ("sqlite3_step fail:%d", rc));
13584 mbox_list = (int *)em_malloc(sizeof(int) * (*count));
13585 if (NULL == mbox_list) {
13586 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
13587 error = EMAIL_ERROR_OUT_OF_MEMORY;
13591 memset(mbox_list, 0x00, sizeof(int) * (*count));
13593 for (i = 0; i < (*count); i++) {
13594 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
13595 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13596 /* EM_DEBUG_LOG("In emstorage_get_pdb_mailbox_list() loop, After sqlite3_step(), , i = %d, rc = %d.", i, rc); */
13597 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13598 ("sqlite3_step fail:%d", rc));
13599 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
13606 *mailbox_list = mbox_list;
13608 EM_SAFE_FREE(mbox_list);
13610 if (hStmt != NULL) {
13611 rc = sqlite3_finalize(hStmt);
13613 if (rc != SQLITE_OK) {
13614 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13615 error = EMAIL_ERROR_DB_FAILURE;
13619 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13620 if (err_code != NULL)
13622 EM_DEBUG_FUNC_END("ret [%d]", ret);
13626 INTERNAL_FUNC int emstorage_get_pbd_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
13628 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13630 if (NULL == account_list || NULL == count) {
13631 EM_DEBUG_EXCEPTION("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13632 if (err_code != NULL)
13633 *err_code = EMAIL_ERROR_INVALID_PARAM;
13638 int error = EMAIL_ERROR_NONE;
13639 char *sql = "SELECT count(distinct account_id) FROM mail_partial_body_activity_tbl";
13641 int i = 0, rc = -1;
13642 int *result_account_list = NULL;
13643 DB_STMT hStmt = NULL;
13644 char sql_query_string[QUERY_SIZE] = {0, };
13646 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13648 EMSTORAGE_START_READ_TRANSACTION(transaction);
13650 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13651 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13652 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13654 *count = atoi(result[1]);
13655 sqlite3_free_table(result);
13658 EM_DEBUG_EXCEPTION("no account found...");
13659 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13664 EM_DEBUG_LOG("Account count [%d]", *count);
13666 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13668 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_partial_body_activity_tbl");
13670 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13673 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13675 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
13676 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13677 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13680 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13681 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13682 ("sqlite3_step fail:%d", rc));
13684 if (NULL == (result_account_list = (int *)em_malloc(sizeof(int) * (*count)))) {
13685 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
13686 error = EMAIL_ERROR_OUT_OF_MEMORY;
13690 memset(result_account_list, 0x00, sizeof(int) * (*count));
13692 for (i = 0; i < (*count); i++) {
13693 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
13695 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13696 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13697 ("sqlite3_step fail:%d", rc));
13698 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
13705 *account_list = result_account_list;
13707 EM_SAFE_FREE(result_account_list);
13709 if (hStmt != NULL) {
13710 rc = sqlite3_finalize(hStmt);
13712 if (rc != SQLITE_OK) {
13713 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13714 error = EMAIL_ERROR_DB_FAILURE;
13718 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13719 if (err_code != NULL)
13721 EM_DEBUG_FUNC_END("ret [%d]", ret);
13725 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)
13727 EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
13729 if (account_id < FIRST_ACCOUNT_ID || NULL == event_start || 0 == input_mailbox_id || NULL == count) {
13730 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);
13732 if (err_code != NULL)
13733 *err_code = EMAIL_ERROR_INVALID_PARAM;
13740 int error = EMAIL_ERROR_NONE;
13742 DB_STMT hStmt = NULL;
13743 email_event_partial_body_thd* event_list = NULL;
13744 char sql_query_string[QUERY_SIZE] = {0, };
13746 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13748 EMSTORAGE_START_READ_TRANSACTION(transaction);
13750 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13751 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);
13754 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13755 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13756 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13758 *count = atoi(result[1]);
13759 sqlite3_free_table(result);
13761 EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
13764 EM_DEBUG_LOG("No matched activity found in mail_partial_body_activity_tbl");
13765 error = EMAIL_ERROR_MAIL_NOT_FOUND;
13769 EM_DEBUG_LOG("Activity Count = %d", *count);
13771 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13772 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);
13774 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13777 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13779 EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
13780 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13781 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13784 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13785 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13786 ("sqlite3_step fail:%d", rc));
13788 if (!(event_list = (email_event_partial_body_thd*)em_malloc(sizeof(email_event_partial_body_thd) * (*count)))) {
13789 EM_DEBUG_EXCEPTION("Malloc failed");
13791 error = EMAIL_ERROR_OUT_OF_MEMORY;
13794 memset(event_list, 0x00, sizeof(email_event_partial_body_thd) * (*count));
13796 for (i = 0; i < (*count); i++) {
13797 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_IDX_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13798 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13799 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13800 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13801 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_type), ACTIVITY_TYPE_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13802 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13803 _get_stmt_field_data_string(hStmt, &(event_list[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13804 _get_stmt_field_data_string(hStmt, &(event_list[i].multi_user_name), 0, MULTI_USER_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13806 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13807 /* EM_DEBUG_LOG("In emstorage_get_pbd_activity_data() loop, After sqlite3_step(), , i = %d, rc = %d.", i, rc); */
13808 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13809 ("sqlite3_step fail:%d", rc));
13811 event_list[i].event_type = 0;
13818 *event_start = event_list;
13820 for (i = 0; i < (*count); i++)
13821 emcore_free_partial_body_thd_event(event_list, NULL);
13822 EM_SAFE_FREE(event_list); /*prevent 54559*/
13823 *event_start = NULL;
13827 if (hStmt != NULL) {
13828 rc = sqlite3_finalize(hStmt);
13830 if (rc != SQLITE_OK) {
13831 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13832 error = EMAIL_ERROR_DB_FAILURE;
13836 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13837 if (err_code != NULL)
13840 EM_DEBUG_FUNC_END("ret [%d]", ret);
13844 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)
13846 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);
13849 if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
13850 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);
13852 if (err_code != NULL)
13853 *err_code = EMAIL_ERROR_INVALID_PARAM;
13859 int error = EMAIL_ERROR_NONE;
13860 char sql_query_string[QUERY_SIZE] = {0, };
13861 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13863 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13864 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13866 if (activity_id == 0)
13867 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);
13869 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);
13871 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13872 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
13873 if (error != EMAIL_ERROR_NONE) {
13874 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
13878 /* validate activity existence */
13879 rc = sqlite3_changes(local_db_handle);
13881 EM_DEBUG_EXCEPTION("No matching activity found");
13882 error = EMAIL_ERROR_DATA_NOT_FOUND;
13890 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13892 if (err_code != NULL)
13895 EM_DEBUG_FUNC_END("ret [%d]", ret);
13899 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)
13901 EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
13903 if (account_id < FIRST_ACCOUNT_ID || NULL == activity_count || NULL == err_code) {
13904 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
13905 if (err_code != NULL)
13906 *err_code = EMAIL_ERROR_INVALID_PARAM;
13911 int error = EMAIL_ERROR_NONE;
13912 char sql_query_string[QUERY_SIZE] = {0, };
13914 DB_STMT hStmt = NULL;
13916 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13918 EMSTORAGE_START_READ_TRANSACTION(transaction);
13919 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13921 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);
13923 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13926 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13927 EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
13928 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13929 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13932 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13933 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13934 ("sqlite3_step fail:%d", rc));
13936 _get_stmt_field_data_int(hStmt, activity_count, 0);
13938 EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
13944 if (hStmt != NULL) {
13945 rc = sqlite3_finalize(hStmt);
13947 if (rc != SQLITE_OK) {
13948 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13949 error = EMAIL_ERROR_DB_FAILURE;
13953 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13955 if (err_code != NULL)
13958 EM_DEBUG_FUNC_END("ret [%d]", ret);
13962 INTERNAL_FUNC int emstorage_get_pbd_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
13964 EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
13966 if (NULL == activity_count || NULL == err_code) {
13967 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
13968 if (err_code != NULL)
13969 *err_code = EMAIL_ERROR_INVALID_PARAM;
13974 int error = EMAIL_ERROR_NONE;
13975 DB_STMT hStmt = NULL;
13976 char sql_query_string[QUERY_SIZE] = {0, };
13978 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13980 EMSTORAGE_START_READ_TRANSACTION(transaction);
13981 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13983 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl;");
13985 EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
13987 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13988 EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
13989 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13990 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13992 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13993 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13994 ("sqlite3_step fail:%d", rc));
13996 _get_stmt_field_data_int(hStmt, activity_count, 0);
13998 EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
14005 if (hStmt != NULL) {
14006 rc = sqlite3_finalize(hStmt);
14008 if (rc != SQLITE_OK) {
14009 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14010 error = EMAIL_ERROR_DB_FAILURE;
14014 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14015 if (err_code != NULL)
14018 EM_DEBUG_FUNC_END("ret [%d]", ret);
14022 INTERNAL_FUNC int emstorage_delete_full_pbd_activity_data(char *multi_user_name, int account_id, int transaction, int *err_code)
14024 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
14025 if (account_id < FIRST_ACCOUNT_ID) {
14026 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
14027 if (err_code != NULL)
14028 *err_code = EMAIL_ERROR_INVALID_PARAM;
14034 int error = EMAIL_ERROR_NONE;
14035 char sql_query_string[QUERY_SIZE] = {0, };
14037 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14039 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14040 memset(sql_query_string, 0x00, sizeof(sql_query_string));
14041 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d", account_id);
14043 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14044 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14045 if (error != EMAIL_ERROR_NONE) {
14046 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14050 rc = sqlite3_changes(local_db_handle);
14052 EM_DEBUG_EXCEPTION("No matching activities found in mail_partial_body_activity_tbl");
14053 error = EMAIL_ERROR_DATA_NOT_FOUND;
14062 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14064 if (err_code != NULL)
14067 EM_DEBUG_FUNC_END("ret [%d]", ret);
14071 /*Himanshu[h.gahlaut]-> Added below API to update mail_partial_body_activity_tbl
14072 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*/
14074 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)
14076 EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
14078 int rc = -1, ret = false;
14079 int error = EMAIL_ERROR_NONE;
14080 char sql_query_string[QUERY_SIZE] = {0, };
14082 int transaction = true;
14084 if (!old_server_uid || !new_server_uid || !mbox_name) {
14085 EM_DEBUG_EXCEPTION("Invalid parameters");
14086 error = EMAIL_ERROR_INVALID_PARAM;
14090 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14092 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14093 memset(sql_query_string, 0x00, sizeof(sql_query_string));
14094 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14095 "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);
14097 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14098 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14099 if (error != EMAIL_ERROR_NONE) {
14100 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14104 rc = sqlite3_changes(local_db_handle);
14106 EM_DEBUG_LOG("No matching found in mail_partial_body_activity_tbl");
14111 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14113 if (err_code != NULL)
14116 EM_DEBUG_FUNC_END("ret [%d]", ret);
14121 INTERNAL_FUNC int emstorage_create_file(char *data_string, size_t file_size, char *dst_file_name, int *err_code)
14123 EM_DEBUG_FUNC_BEGIN_SEC("file_size[%d] , dst_file_name[%s], err_code[%p]", file_size, dst_file_name, err_code);
14126 int error = EMAIL_ERROR_NONE;
14127 FILE* fp_dst = NULL;
14128 char errno_buf[ERRNO_BUF_SIZE] = {0};
14130 if (!data_string || !dst_file_name) {
14131 EM_DEBUG_LOG("data_string[%p], dst_file_name[%p]", data_string, dst_file_name);
14132 error = EMAIL_ERROR_INVALID_PARAM;
14136 error = em_fopen(dst_file_name, "w", &fp_dst);
14137 if (error != EMAIL_ERROR_NONE) {
14138 EM_DEBUG_EXCEPTION_SEC("em_fopen failed - %s: %d", dst_file_name, error);
14142 if (fwrite(data_string, 1, file_size, fp_dst) == 0) {
14143 EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
14144 error = EMAIL_ERROR_UNKNOWN;
14152 if (fp_dst != NULL)
14155 if (err_code != NULL)
14158 EM_DEBUG_FUNC_END("ret [%d]", ret);
14162 #endif /* __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
14166 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
14167 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)
14169 EM_DEBUG_FUNC_BEGIN();
14172 int error = EMAIL_ERROR_NONE;
14173 char sql_query_string[QUERY_SIZE] = {0, };
14175 int transaction = true;
14177 if (!old_server_uid || !new_server_uid || !mbox_name) {
14178 EM_DEBUG_EXCEPTION("Invalid parameters");
14179 if (err_code != NULL)
14180 *err_code = EMAIL_ERROR_INVALID_PARAM;
14184 EM_DEBUG_LOG_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
14186 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14187 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14190 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14191 "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);
14193 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
14194 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14195 if (error != EMAIL_ERROR_NONE) {
14196 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14200 rc = sqlite3_changes(local_db_handle);
14202 EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
14203 error = EMAIL_ERROR_DATA_NOT_FOUND;
14210 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14212 if (err_code != NULL)
14215 EM_DEBUG_FUNC_END("ret [%d]", ret);
14222 * @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);
14223 * Prepare an array of mail_id and corresponding server mail id.
14225 * @author h.gahlaut@samsung.com
14226 * @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)
14227 * where 88 is the length of fixed keywords including ending null character in the QUERY to be formed
14228 * @param[out] idset Returns the array of mail_id and corresponding server_mail_id sorted by server_mail_ids
14229 * @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
14230 * @param[out] err_code Returns the error code.
14231 * @remarks An Example of Query to be exexuted in this API:
14232 * SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (12, 13, 56, 78);
14233 * @return This function returns true on success or false on failure.
14235 INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *multi_user_name,
14237 email_id_set_t** idset,
14241 EM_DEBUG_FUNC_BEGIN();
14242 EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds);
14244 int error = EMAIL_ERROR_NONE;
14246 email_id_set_t* p_id_set = NULL;
14248 const int buf_size = QUERY_SIZE;
14249 char sql_query_string[QUERY_SIZE] = {0, };
14250 int space_left_in_query_buffer = buf_size;
14253 char *server_mail_id = NULL;
14254 char **result = NULL;
14258 if (NULL == mail_ids || NULL == idset || NULL == id_set_count) {
14259 EM_DEBUG_EXCEPTION("Invalid Parameters mail_ids[%p] idset[%p] id_set_count [%p]",
14260 mail_ids, idset, id_set_count);
14261 if (err_code != NULL)
14262 *err_code = EMAIL_ERROR_INVALID_PARAM;
14266 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14268 SNPRINTF(sql_query_string, space_left_in_query_buffer,
14269 "SELECT local_uid, server_uid FROM mail_read_mail_uid_tbl WHERE local_uid in (%s) ORDER BY server_uid",
14272 EM_DEBUG_LOG_SEC("SQL Query formed [%s] ", sql_query_string);
14274 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14275 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14276 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result);
14277 goto FINISH_OFF; }, ("SQL(%s) sqlite3_get_table fail:%d -%s",
14278 sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14280 EM_DEBUG_LOG(" Count of mails [%d ]", count);
14283 EM_DEBUG_EXCEPTION("Can't find proper mail");
14284 error = EMAIL_ERROR_DATA_NOT_FOUND;
14288 if (NULL == (p_id_set = (email_id_set_t*)em_malloc(sizeof(email_id_set_t) * count))) {
14289 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
14290 error = EMAIL_ERROR_OUT_OF_MEMORY;
14296 EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds_Loop);
14297 EM_DEBUG_LOG(">>>> DATA ASSIGN START");
14298 for (i = 0; i < count; i++) {
14299 _get_table_field_data_int(result, &(p_id_set[i].mail_id), col_index++);
14300 _get_table_field_data_string(result, &server_mail_id, 1, col_index++);
14301 if (server_mail_id) {
14302 p_id_set[i].server_mail_id = strtoul(server_mail_id, NULL, 10);
14303 EM_SAFE_FREE(server_mail_id);
14306 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d]", count);
14307 EM_PROFILE_END(EmStorageGetIdSetFromMailIds_Loop);
14309 sqlite3_free_table(result);
14318 *id_set_count = count;
14319 EM_DEBUG_LOG(" idset[%p] id_set_count [%d]", *idset, *id_set_count);
14321 EM_SAFE_FREE(p_id_set);
14324 if (err_code != NULL)
14327 EM_PROFILE_END(EmStorageGetIdSetFromMailIds);
14329 EM_DEBUG_FUNC_END("ret [%d]", ret);
14337 INTERNAL_FUNC int emstorage_delete_triggers_from_lucene(char *multi_user_name)
14339 EM_DEBUG_FUNC_BEGIN();
14340 int ret = true, transaction = true;
14341 int error = EMAIL_ERROR_NONE;
14342 char sql_query_string[QUERY_SIZE] = {0, };
14343 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14344 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14346 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerDelete;");
14347 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14348 if (error != EMAIL_ERROR_NONE) {
14349 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14353 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerInsert;");
14354 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14355 if (error != EMAIL_ERROR_NONE) {
14356 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14360 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerUpdate;");
14361 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14362 if (error != EMAIL_ERROR_NONE) {
14363 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14370 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14372 EM_DEBUG_FUNC_END("ret [%d]", ret);
14376 INTERNAL_FUNC int emstorage_update_tag_id(char *multi_user_name, int old_filter_id, int new_filter_id, int *err_code)
14378 EM_DEBUG_FUNC_BEGIN("new_filter_id[%d], old_filter_id[%d]", new_filter_id, old_filter_id);
14380 int error = EMAIL_ERROR_NONE;
14381 char sql_query_string[QUERY_SIZE] = {0, };
14382 int transaction = true;
14384 if (old_filter_id < 0 || new_filter_id < 0) {
14385 EM_DEBUG_EXCEPTION("Invalid parameters");
14386 if (err_code != NULL)
14387 *err_code = EMAIL_ERROR_INVALID_PARAM;
14391 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14393 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14395 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14396 "UPDATE mail_tbl SET tag_id=%d WHERE tag_id=%d ", new_filter_id, old_filter_id);
14398 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14399 if (error != EMAIL_ERROR_NONE) {
14400 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14407 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14409 if (err_code != NULL)
14412 EM_DEBUG_FUNC_END("ret [%d]", ret);
14416 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)
14418 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);
14420 if ((account_id < 0) || (dest_mailbox_id < 0) || (!rule)) {
14421 EM_DEBUG_EXCEPTION("Invalid Parameter");
14423 if (err_code != NULL)
14424 *err_code = EMAIL_ERROR_INVALID_PARAM;
14428 int rc = -1, ret = false, error = EMAIL_ERROR_NONE;
14429 int count = 0, col_index = 0, i = 0, where_pararaph_length = 0, *mail_list = NULL;
14430 int tag_id = rule->rule_id;
14431 char **result = NULL, *where_pararaph = NULL;
14432 char sql_query_string[QUERY_SIZE] = {0, };
14433 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14435 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl ");
14437 where_pararaph_length = EM_SAFE_STRLEN(rule->value) + 2 * (EM_SAFE_STRLEN(rule->value2)) + 100;
14438 where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
14439 if (where_pararaph == NULL) {
14440 EM_DEBUG_EXCEPTION("malloc failed for where_pararaph.");
14441 error = EMAIL_ERROR_OUT_OF_MEMORY;
14445 if (account_id != ALL_ACCOUNT)
14446 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (0)", account_id);
14448 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_type NOT in (0)");
14450 if (rule->type & EMAIL_FILTER_SUBJECT) {
14451 if (rule->flag2 == RULE_TYPE_INCLUDES)
14452 sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject like \'%%%q%%\'", rule->value);
14453 else /* RULE_TYPE_EXACTLY */
14454 sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject = \'%q\'", rule->value);
14457 if (rule->type & EMAIL_FILTER_FROM) {
14458 if (rule->flag2 == RULE_TYPE_INCLUDES)
14459 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);
14460 #ifdef __FEATURE_COMPARE_DOMAIN__
14461 else if (rule->flag2 == RULE_TYPE_COMPARE_DOMAIN)
14462 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);
14463 #endif /*__FEATURE_COMPARE_DOMAIN__ */
14464 else /* RULE_TYPE_EXACTLY */
14465 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);
14468 if (rule->type == EMAIL_PRIORITY_SENDER) {
14469 if (rule->flag2 == RULE_TYPE_INCLUDES)
14470 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);
14471 else /* RULE_TYPE_EXACTLY */
14472 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);
14474 tag_id = PRIORITY_SENDER_TAG_ID;
14477 /* prevent 34361 */
14478 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14479 EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14480 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14482 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14483 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14484 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
14485 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14487 EM_DEBUG_LOG("Count of mails [%d]", count);
14490 mail_list = malloc(sizeof(int) * count);
14491 if (mail_list == NULL) {
14492 EM_DEBUG_EXCEPTION("malloc failed for mail_list.");
14493 error = EMAIL_ERROR_OUT_OF_MEMORY;
14499 for (i = 0; i < count; i++)
14500 _get_table_field_data_int(result, &(mail_list[i]), col_index++);
14502 memset(sql_query_string, 0x00, QUERY_SIZE);
14504 switch (rule->action_type) {
14505 case EMAIL_FILTER_MOVE:
14506 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = 0 ");
14508 case EMAIL_FILTER_BLOCK:
14510 EM_DEBUG_LOG("Not support : action_type[%d]", rule->action_type);
14515 switch (rule->action_type) {
14516 case EMAIL_FILTER_MOVE:
14517 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = %d ", tag_id);
14519 case EMAIL_FILTER_BLOCK:
14520 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET mailbox_id = %d, mailbox_type = %d ", dest_mailbox_id, dest_mailbox_type);
14523 EM_DEBUG_LOG("Not support");
14528 /* prevent 34361 */
14529 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14530 EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14532 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14533 if (error != EMAIL_ERROR_NONE) {
14534 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14538 #ifdef __FEATURE_BODY_SEARCH__
14539 /* Updating mail_text_tbl */
14540 if (rule->action_type == EMAIL_FILTER_BLOCK) {
14541 memset(sql_query_string, 0x00, QUERY_SIZE);
14542 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_text_tbl SET mailbox_id = %d ", dest_mailbox_id);
14543 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14544 EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14546 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14547 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14548 if (error != EMAIL_ERROR_NONE) {
14549 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14561 if (filtered_mail_id_list)
14562 *filtered_mail_id_list = mail_list;
14564 if (count_of_mails)
14565 *count_of_mails = count;
14567 EM_SAFE_FREE(mail_list);
14569 sqlite3_free_table(result);
14573 EM_SAFE_FREE(where_pararaph);
14575 if (err_code != NULL)
14578 EM_DEBUG_FUNC_END("ret [%d]", ret);
14582 #define EMAIL_SLOT_UNIT 25
14583 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)
14585 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);
14586 int ret = false, err = EMAIL_ERROR_NONE;
14587 int where_pararaph_length = 0;
14588 char *where_pararaph = NULL;
14589 char sql_query_string[QUERY_SIZE] = {0, };
14591 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14593 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
14595 if (new_slot_size > 0)
14596 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = %d ", new_slot_size);
14597 else if (new_slot_size == 0)
14598 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", EMAIL_SLOT_UNIT);
14600 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", new_slot_size * -1);
14604 where_pararaph_length = 80;
14606 where_pararaph_length = 50;
14608 if (new_slot_size == 0)
14609 where_pararaph_length += 70;
14611 where_pararaph = malloc(sizeof(char) * where_pararaph_length);
14612 if (where_pararaph == NULL) {
14613 EM_DEBUG_EXCEPTION("Memory allocation failed for where_pararaph");
14614 err = EMAIL_ERROR_OUT_OF_MEMORY;
14617 memset(where_pararaph, 0x00, where_pararaph_length);
14619 if (account_id > ALL_ACCOUNT) {
14622 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_id = %d ", mailbox_id);
14624 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d ", account_id);
14627 if (new_slot_size == 0)
14628 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"));
14630 if (strlen(sql_query_string) + EM_SAFE_STRLEN(where_pararaph) < QUERY_SIZE) /* prevent 34363 */
14631 EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14633 EM_DEBUG_EXCEPTION("Query buffer overflowed !!!");
14634 err = EMAIL_ERROR_OUT_OF_MEMORY;
14638 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14639 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14640 if (err != EMAIL_ERROR_NONE) {
14641 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
14648 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
14650 EM_SAFE_FREE(where_pararaph);
14652 if (err_code != NULL)
14655 EM_DEBUG_FUNC_END("ret [%d]", ret);
14659 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)
14661 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);
14663 if (!meeting_req || meeting_req->mail_id <= 0) {
14665 EM_DEBUG_EXCEPTION("mail_id[%d]", meeting_req->mail_id);
14667 if (err_code != NULL)
14668 *err_code = EMAIL_ERROR_INVALID_PARAM;
14675 int error = EMAIL_ERROR_NONE;
14676 DB_STMT hStmt = NULL;
14677 char sql_query_string[QUERY_SIZE] = {0, };
14679 time_t temp_unix_time = 0;
14681 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14682 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14684 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14685 "INSERT INTO mail_meeting_tbl VALUES "
14687 ", ?" /* account_id */
14688 ", ?" /* mailbox_id */
14689 ", ?" /* meeting_response */
14690 ", ?" /* start_time */
14691 ", ?" /* end_time */
14692 ", ?" /* location */
14693 ", ?" /* global_object_id */
14695 ", ?" /* standard_name */
14696 ", ?" /* standard_time_start_date */
14697 ", ?" /* standard_biad */
14698 ", ?" /* daylight_name */
14699 ", ?" /* daylight_time_start_date */
14700 ", ?" /* daylight_bias */
14703 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14704 if (rc != SQLITE_OK) {
14705 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
14706 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
14708 error = EMAIL_ERROR_DB_FAILURE;
14714 EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
14715 EM_DEBUG_LOG_SEC(">>>>> account_id[%d]", account_id);
14716 EM_DEBUG_LOG_SEC(">>>>> mailbox_name[%s]", mailbox_name);
14717 EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
14718 EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
14719 EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
14720 EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
14721 EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
14722 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
14723 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
14724 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
14725 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
14726 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
14727 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
14728 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
14730 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->mail_id);
14731 _bind_stmt_field_data_int(hStmt, col_index++, account_id);
14732 _bind_stmt_field_data_int(hStmt, col_index++, input_mailbox_id);
14733 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
14735 temp_unix_time = timegm(&(meeting_req->start_time));
14736 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14737 temp_unix_time = timegm(&(meeting_req->end_time));
14738 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14740 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->location, 0, LOCATION_LEN_IN_MAIL_MEETING_TBL);
14741 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->global_object_id, 0, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
14743 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
14744 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.standard_name, 0, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
14745 temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
14746 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14747 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
14749 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.daylight_name, 0, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
14750 temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
14751 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14752 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
14755 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14756 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
14757 ("sqlite3_step fail:%d", rc));
14758 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14759 ("sqlite3_step fail:%d", rc));
14764 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14765 if (hStmt != NULL) {
14766 rc = sqlite3_finalize(hStmt);
14767 if (rc != SQLITE_OK) {
14768 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14769 error = EMAIL_ERROR_DB_FAILURE;
14773 if (err_code != NULL)
14776 EM_DEBUG_FUNC_END("ret [%d]", ret);
14780 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)
14782 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);
14789 int err = EMAIL_ERROR_NONE;
14790 char **result = NULL;
14791 char sql_query_string[QUERY_SIZE] = {0, };
14792 email_meeting_request_t* p_temp_meeting_req = NULL;
14793 sqlite3 *local_db_handle = NULL;
14794 time_t temp_unix_time;
14796 if (conditional_clause == NULL || output_meeting_req == NULL || output_result_count == NULL) {
14797 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14798 err = EMAIL_ERROR_INVALID_PARAM;
14802 col_index = _field_count_of_table[CREATE_TABLE_MAIL_MEETING_TBL];
14803 EM_DEBUG_LOG("col_index [%d]", col_index);
14805 local_db_handle = emstorage_get_db_connection(multi_user_name);
14807 EMSTORAGE_START_READ_TRANSACTION(transaction);
14809 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_meeting_tbl %s", conditional_clause);
14811 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14813 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14814 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
14815 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14818 EM_DEBUG_EXCEPTION("No meeting_request found...");
14819 err = EMAIL_ERROR_DATA_NOT_FOUND;
14823 EM_DEBUG_LOG("There are [%d] meeting requests.", count);
14824 if (!(p_temp_meeting_req = (email_meeting_request_t*)em_malloc(sizeof(email_meeting_request_t) * count))) {
14825 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
14826 err = EMAIL_ERROR_OUT_OF_MEMORY;
14830 EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
14832 for (i = 0; i < count; i++) {
14833 _get_table_field_data_int(result, &(p_temp_meeting_req[i].mail_id), col_index++);
14834 _get_table_field_data_int(result, &dummy, col_index++); /* account_id. but why should this field exist in DB table? */
14835 _get_table_field_data_int(result, &dummy, col_index++); /* mailbox_id */
14836 _get_table_field_data_int(result, (int*)&(p_temp_meeting_req[i].meeting_response), col_index++);
14837 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* start time */
14838 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].start_time));
14839 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* end time */
14840 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].end_time));
14841 _get_table_field_data_string(result, &p_temp_meeting_req[i].location, 1, col_index++);
14842 _get_table_field_data_string(result, &p_temp_meeting_req[i].global_object_id, 1, col_index++);
14843 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.offset_from_GMT), col_index++);
14844 _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++);
14845 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14846 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.standard_time_start_date));
14847 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.standard_bias), col_index++);
14848 _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++);
14849 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14850 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.daylight_time_start_date));
14851 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.daylight_bias), col_index++);
14857 sqlite3_free_table(result);
14859 if (err == EMAIL_ERROR_NONE) {
14860 if (p_temp_meeting_req)
14861 *output_meeting_req = p_temp_meeting_req;
14862 *output_result_count = count;
14864 EM_SAFE_FREE(p_temp_meeting_req);
14866 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14868 // sqlite3_db_release_memory(local_db_handle);
14870 EM_DEBUG_FUNC_END("err [%d]", err);
14874 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)
14876 EM_DEBUG_FUNC_BEGIN();
14880 int error = EMAIL_ERROR_NONE;
14881 char conditional_clause[QUERY_SIZE] = {0, };
14883 EM_IF_NULL_RETURN_VALUE(meeting_req, false);
14886 SNPRINTF(conditional_clause, QUERY_SIZE, " WHERE mail_id = %d", mail_id);
14887 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
14889 if ((error = emstorage_query_meeting_request(multi_user_name, conditional_clause, meeting_req, &count, transaction)) != EMAIL_ERROR_NONE) {
14890 EM_DEBUG_EXCEPTION("emstorage_query_meeting_request failed. [%d]", error);
14894 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->mail_id[%d]", (*meeting_req)->mail_id);
14895 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->meeting_response[%d]", (*meeting_req)->meeting_response);
14896 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->start_time[%s]", asctime(&((*meeting_req)->start_time)));
14897 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->end_time[%s]", asctime(&((*meeting_req)->end_time)));
14898 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->location[%s]", (*meeting_req)->location);
14899 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->global_object_id[%s]", (*meeting_req)->global_object_id);
14900 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.offset_from_GMT[%d]", (*meeting_req)->time_zone.offset_from_GMT);
14901 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_name[%s]", (*meeting_req)->time_zone.standard_name);
14902 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.standard_time_start_date)));
14903 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_bias[%d]", (*meeting_req)->time_zone.standard_bias);
14904 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_name[%s]", (*meeting_req)->time_zone.daylight_name);
14905 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.daylight_time_start_date)));
14906 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_bias[%d]", (*meeting_req)->time_zone.daylight_bias);
14911 if (err_code != NULL)
14914 EM_DEBUG_FUNC_END("ret [%d]", ret);
14918 INTERNAL_FUNC int emstorage_update_meeting_request(char *multi_user_name, email_meeting_request_t* meeting_req, int transaction, int *err_code)
14920 EM_DEBUG_FUNC_BEGIN("meeting_req[%p], transaction[%d], err_code[%p]", meeting_req, transaction, err_code);
14923 int error = EMAIL_ERROR_NONE;
14925 DB_STMT hStmt = NULL;
14926 char sql_query_string[QUERY_SIZE] = {0, };
14927 time_t temp_unix_time = 0;
14929 if (!meeting_req) {
14930 EM_DEBUG_EXCEPTION("Invalid Parameter!");
14931 if (err_code != NULL)
14932 *err_code = EMAIL_ERROR_INVALID_PARAM;
14936 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14937 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14939 memset(sql_query_string, 0x00, sizeof(sql_query_string));
14940 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14941 "UPDATE mail_meeting_tbl "
14943 /* " account_id = ?, " // not update here, this can be changed when move or copy */
14944 /* " mailbox_name = ?, " // not update here, this can be changed when move or copy */
14945 " meeting_response = ?, "
14946 " start_time = ?, "
14949 " global_object_id = ?, "
14951 " standard_name = ?, "
14952 " standard_time_start_date = ?, "
14953 " standard_bias = ?, "
14954 " daylight_name = ?, "
14955 " daylight_time_start_date = ?, "
14956 " daylight_bias = ? "
14957 "WHERE mail_id = %d",
14958 meeting_req->mail_id);
14960 EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
14962 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14963 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14964 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14966 EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
14967 EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
14968 EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
14969 EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
14970 EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
14971 EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
14972 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
14973 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
14974 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
14975 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
14976 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
14977 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
14978 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
14982 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
14983 temp_unix_time = timegm(&(meeting_req->start_time));
14984 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14985 temp_unix_time = timegm(&(meeting_req->end_time));
14986 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14987 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->location, 1, LOCATION_LEN_IN_MAIL_MEETING_TBL);
14988 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->global_object_id, 1, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
14989 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
14990 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.standard_name, 1, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
14991 temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
14992 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14993 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
14994 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.daylight_name, 1, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
14995 temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
14996 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14997 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
15000 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15001 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15002 ("sqlite3_step fail:%d", rc));
15007 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15009 if (hStmt != NULL) {
15010 rc = sqlite3_finalize(hStmt);
15011 if (rc != SQLITE_OK) {
15012 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15013 error = EMAIL_ERROR_DB_FAILURE;
15017 if (err_code != NULL)
15020 EM_DEBUG_FUNC_END("ret [%d]", ret);
15024 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)
15026 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);
15028 if (account_id < ALL_ACCOUNT || mail_id < 0) {
15029 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d]", account_id, mail_id);
15031 if (err_code != NULL)
15032 *err_code = EMAIL_ERROR_INVALID_PARAM;
15037 int error = EMAIL_ERROR_NONE;
15039 char sql_query_string[QUERY_SIZE] = {0, };
15041 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15042 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15044 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_meeting_tbl ");
15046 if (account_id != ALL_ACCOUNT) { /* NOT '0' means a specific account. '0' means all account */
15047 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);
15051 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);
15054 if (input_mailbox_id > 0) { /* 0 means all mailbox_id */
15055 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);
15058 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15059 if (error != EMAIL_ERROR_NONE) {
15060 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
15067 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15072 EM_DEBUG_FUNC_END("ret [%d]", ret);
15077 INTERNAL_FUNC void emstorage_free_meeting_request(email_meeting_request_t *meeting_req)
15079 EM_DEBUG_FUNC_BEGIN();
15081 if (!meeting_req) return;
15083 EM_SAFE_FREE(meeting_req->location);
15084 EM_SAFE_FREE(meeting_req->global_object_id);
15086 EM_DEBUG_FUNC_END();
15089 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)
15091 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);
15092 EM_PROFILE_BEGIN(profile_emstorage_get_overflowed_mail_id_list);
15093 char sql_query_string[QUERY_SIZE] = {0, };
15094 char **result = NULL;
15095 int rc = -1, ret = false;
15096 int error = EMAIL_ERROR_NONE;
15097 int counter = 0, col_index = 0;
15098 int result_mail_id_count = 0;
15099 int *result_mail_id_list = NULL;
15101 if (input_mailbox_id <= 0 || !mail_id_list || !mail_id_count || account_id < 1) {
15102 EM_DEBUG_EXCEPTION("Invalid Parameter");
15104 *err_code = EMAIL_ERROR_INVALID_PARAM;
15108 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);
15110 EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
15112 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15113 EMSTORAGE_START_READ_TRANSACTION(transaction);
15115 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_mail_id_count, 0, NULL), rc);
15116 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15117 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15119 if (!result_mail_id_count) {
15120 EM_DEBUG_LOG("No mail found...");
15122 error = EMAIL_ERROR_MAIL_NOT_FOUND;
15126 EM_DEBUG_LOG("There are [%d] overflowed mails in mailbox_id [%d]", result_mail_id_count, input_mailbox_id);
15128 if (!(result_mail_id_list = (int *)malloc(sizeof(int) * result_mail_id_count))) {
15129 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
15130 error = EMAIL_ERROR_OUT_OF_MEMORY;
15131 sqlite3_free_table(result);
15135 memset(result_mail_id_list, 0x00, sizeof(int) * result_mail_id_count);
15139 for (counter = 0; counter < result_mail_id_count; counter++)
15140 _get_table_field_data_int(result, result_mail_id_list + counter, col_index++);
15145 EM_DEBUG_LOG("finish off [%d]", ret);
15148 sqlite3_free_table(result);
15151 *mail_id_list = result_mail_id_list;
15152 *mail_id_count = result_mail_id_count;
15154 EM_SAFE_FREE(result_mail_id_list);
15156 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15158 if (err_code != NULL)
15161 EM_PROFILE_END(profile_emstorage_get_overflowed_mail_id_list);
15162 EM_DEBUG_FUNC_END("ret [%d]", ret);
15166 INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(char *multi_user_name, int mail_id, int *thread_id, int *err_code)
15168 EM_DEBUG_FUNC_BEGIN("mail_id[%d], thread_id[%p], err_code[%p]", mail_id, thread_id, err_code);
15170 int rc = -1, ret = false;
15171 int err = EMAIL_ERROR_NONE;
15172 char sql_query_string[QUERY_SIZE] = {0, };
15174 int result_count = 0;
15176 if (mail_id == 0 || thread_id == NULL) {
15177 EM_DEBUG_EXCEPTION("Invalid Parameter");
15179 *err_code = EMAIL_ERROR_INVALID_PARAM;
15183 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15185 memset(sql_query_string, 0, QUERY_SIZE);
15186 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT thread_id FROM mail_tbl WHERE mail_id = %d", mail_id);
15188 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
15189 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
15190 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15191 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15193 if (!result_count) {
15194 EM_DEBUG_LOG("No mail found...");
15196 err = EMAIL_ERROR_MAIL_NOT_FOUND;
15197 /* sqlite3_free_table(result); */
15201 _get_table_field_data_int(result, thread_id, 1);
15203 sqlite3_free_table(result);
15209 if (err_code != NULL)
15212 EM_DEBUG_FUNC_END("ret [%d]", ret);
15216 INTERNAL_FUNC int emstorage_update_latest_thread_mail(char *multi_user_name,
15221 int *updated_thread_id,
15222 int latest_mail_id,
15223 int thread_item_count,
15228 EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id [%d], thread_id[%d], updated_thread_id[%p], "
15229 "latest_mail_id [%d], thread_item_count[%d], err_code[%p]",
15230 account_id, mailbox_id, thread_id, updated_thread_id,
15231 latest_mail_id, thread_item_count, err_code);
15233 int rc = -1, ret = false;
15234 int err = EMAIL_ERROR_NONE;
15235 char sql_query_string[QUERY_SIZE] = {0, };
15236 char **result = NULL;
15237 int result_count = 0;
15239 if (thread_id == 0) {
15240 EM_DEBUG_EXCEPTION("Invalid Parameter");
15242 *err_code = EMAIL_ERROR_INVALID_PARAM;
15246 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15248 if (thread_item_count == 0 && latest_mail_id == 0) {
15249 memset(sql_query_string, 0, QUERY_SIZE);
15250 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);
15252 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
15253 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
15254 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = 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)));
15256 EM_DEBUG_LOG("result_count[%d]", result_count);
15257 if (result_count == 0) {
15258 EM_DEBUG_LOG("No mail found...");
15261 *err_code = EMAIL_ERROR_MAIL_NOT_FOUND;
15262 sqlite3_free_table(result);
15266 _get_table_field_data_int(result, &latest_mail_id, 2);
15267 _get_table_field_data_int(result, &thread_item_count, 3);
15269 EM_DEBUG_LOG("latest_mail_id[%d]", latest_mail_id);
15270 EM_DEBUG_LOG("thread_item_count[%d]", thread_item_count);
15272 sqlite3_free_table(result);
15275 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15277 if (thread_item_count < 0) {
15278 memset(sql_query_string, 0, QUERY_SIZE);
15279 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);
15280 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15281 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15282 if (err != EMAIL_ERROR_NONE) {
15283 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15286 } else if (thread_id != latest_mail_id) {
15287 /* Initialize the thread id */
15288 memset(sql_query_string, 0, QUERY_SIZE);
15289 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);
15290 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15291 if (err != EMAIL_ERROR_NONE) {
15292 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15296 /* update the thread item count */
15297 memset(sql_query_string, 0, QUERY_SIZE);
15298 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);
15299 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15300 if (err != EMAIL_ERROR_NONE) {
15301 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15305 memset(sql_query_string, 0, QUERY_SIZE);
15306 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);
15307 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15308 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15309 if (err != EMAIL_ERROR_NONE) {
15310 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15317 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15319 if (thread_id != latest_mail_id) {
15320 if (err == EMAIL_ERROR_NONE) {
15321 EM_DEBUG_LOG("noti_type[%d]", noti_type);
15323 if (latest_mail_id > 0 && thread_id > 0 && noti_type > 0) {
15324 char mailbox_id_str[25] = {0,};
15325 snprintf(mailbox_id_str, sizeof(mailbox_id_str), "%d", mailbox_id);
15326 if (!emcore_notify_storage_event(noti_type, thread_id, latest_mail_id, mailbox_id_str, account_id))
15327 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventfailed [NOTI_THREAD_ID_CHANGED] >>>> ");
15329 if (updated_thread_id) *updated_thread_id = latest_mail_id;
15332 } else if (thread_item_count >= 0) {
15333 if (err == EMAIL_ERROR_NONE) {
15334 char parameter_string[500] = {0,};
15335 SNPRINTF(parameter_string, sizeof(parameter_string), "%s%c%d", "thread_item_count", 0x01, latest_mail_id);
15336 if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT, parameter_string, thread_item_count))
15337 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventfailed [NOTI_MAIL_FIELD_UPDATE] >>>> ");
15341 if (err_code != NULL)
15344 EM_DEBUG_FUNC_END("ret [%d]", ret);
15348 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)
15350 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);
15353 int err = EMAIL_ERROR_NONE;
15354 char sql_query_string[QUERY_SIZE] = {0, };
15356 if (thread_id == 0) {
15357 EM_DEBUG_EXCEPTION("Invalid Parameter");
15359 *err_code = EMAIL_ERROR_INVALID_PARAM;
15363 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15365 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15367 memset(sql_query_string, 0, QUERY_SIZE);
15368 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);
15369 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15370 if (err != EMAIL_ERROR_NONE) {
15371 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15378 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15380 if (err_code != NULL)
15383 EM_DEBUG_FUNC_END("ret [%d]", ret);
15387 #ifdef __FEATURE_LOCAL_ACTIVITY__
15389 * emstorage_add_activity - Add Email Local activity during OFFLINE mode
15392 INTERNAL_FUNC int emstorage_add_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15394 EM_DEBUG_FUNC_BEGIN();
15396 EM_DEBUG_LOG(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15398 int rc = -1, ret = false;
15399 int error = EMAIL_ERROR_NONE;
15400 DB_STMT hStmt = NULL;
15401 char sql_query_string[8192] = { 0x00, };
15404 if (!local_activity) {
15405 EM_DEBUG_EXCEPTION(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15406 if (err_code != NULL)
15407 *err_code = EMAIL_ERROR_INVALID_PARAM;
15411 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15413 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15415 memset(sql_query_string, 0x00 , sizeof(sql_query_string));
15416 SNPRINTF(sql_query_string, sizeof(sql_query_string), "INSERT INTO mail_local_activity_tbl VALUES (?, ?, ?, ?, ?, ?, ?)");
15418 EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ] ", local_activity->activity_id);
15419 EM_DEBUG_LOG(">>>>> MAIL ID [ %d ] ", local_activity->mail_id);
15420 EM_DEBUG_LOG(">>>>> ACCOUNT ID [ %d ] ", local_activity->account_id);
15421 EM_DEBUG_LOG(">>>>> ACTIVITY TYPE [ %d ] ", local_activity->activity_type);
15422 EM_DEBUG_LOG_SEC(">>>>> SERVER MAIL ID [ %s ] ", local_activity->server_mailid);
15423 EM_DEBUG_LOG(">>>>> SOURCE MAILBOX [ %s ] ", local_activity->src_mbox);
15424 EM_DEBUG_LOG(">>>>> DEST MAILBOX [ %s ] ", local_activity->dest_mbox);
15426 EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15429 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15430 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15431 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15433 EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15435 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
15436 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
15437 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
15438 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
15439 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->server_mailid, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
15440 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->src_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15441 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->dest_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15444 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15446 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
15447 ("sqlite3_step fail:%d", rc));
15448 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15449 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
15454 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15456 if (hStmt != NULL) {
15457 rc = sqlite3_finalize(hStmt);
15458 if (rc != SQLITE_OK) {
15459 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15460 error = EMAIL_ERROR_DB_FAILURE;
15464 if (err_code != NULL)
15467 EM_DEBUG_FUNC_END("ret [%d]", ret);
15472 * emstorage_get_activity - Get the Local activity Information
15476 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)
15478 EM_DEBUG_FUNC_BEGIN();
15480 int i = 0, count = 0, rc = -1, ret = false;
15481 int error = EMAIL_ERROR_NONE;
15482 emstorage_activity_tbl_t *p_activity_tbl = NULL;
15483 char sql_query_string[1024] = {0x00, };
15484 char **result = NULL;
15487 EM_IF_NULL_RETURN_VALUE(activity_list, false);
15488 EM_IF_NULL_RETURN_VALUE(select_num, false);
15491 if (!select_num || !activity_list || account_id <= 0 || activityid < 0) {
15492 EM_DEBUG_LOG(" select_num[%p], activity_list[%p] account_id [%d] activityid [%d] ", select_num, activity_list, account_id, activityid);
15494 *err_code = EMAIL_ERROR_INVALID_PARAM;
15498 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15500 EMSTORAGE_START_READ_TRANSACTION(transaction);
15502 memset(sql_query_string, 0x00, sizeof(sql_query_string));
15504 if (activityid == ALL_ACTIVITIES) {
15505 SNPRINTF(sql_query_string, sizeof(sql_query_string),
15506 "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d order by activity_id", account_id);
15508 SNPRINTF(sql_query_string, sizeof(sql_query_string),
15509 "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d AND activity_id = %d ", account_id, activityid);
15512 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
15516 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15517 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15518 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15522 if (!(p_activity_tbl = (emstorage_activity_tbl_t*)em_malloc(sizeof(emstorage_activity_tbl_t) * count))) {
15523 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
15524 error = EMAIL_ERROR_OUT_OF_MEMORY;
15529 for (i = 0; i < count; i++) {
15530 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15531 if (result[col_index])
15532 p_activity_tbl[i].activity_id = atoi(result[col_index++]);
15534 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15535 if (result[col_index])
15536 p_activity_tbl[i].account_id = atoi(result[col_index++]);
15538 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15539 if (result[col_index])
15540 p_activity_tbl[i].mail_id = atoi(result[col_index++]);
15542 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15543 if (result[col_index])
15544 p_activity_tbl[i].activity_type = atoi(result[col_index++]);
15547 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15548 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15549 p_activity_tbl[i].server_mailid = EM_SAFE_STRDUP(result[col_index++]);
15553 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15554 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15555 p_activity_tbl[i].src_mbox = EM_SAFE_STRDUP(result[col_index++]);
15559 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15560 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15561 p_activity_tbl[i].dest_mbox = EM_SAFE_STRDUP(result[col_index++]);
15568 sqlite3_free_table(result);
15574 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15577 *activity_list = p_activity_tbl;
15578 *select_num = count;
15579 EM_DEBUG_LOG(">>>> COUNT : %d >> ", count);
15580 } else if (p_activity_tbl != NULL) {
15581 emstorage_free_local_activity(&p_activity_tbl, count, NULL);
15585 if (err_code != NULL)
15588 EM_DEBUG_FUNC_END("ret [%d]", ret);
15593 INTERNAL_FUNC int emstorage_get_next_activity_id(int *activity_id, int *err_code)
15596 EM_DEBUG_FUNC_BEGIN();
15599 int err = EMAIL_ERROR_NONE;
15602 char **result = NULL;
15604 if (NULL == activity_id) {
15605 EM_DEBUG_EXCEPTION(" activity_id[%p]", activity_id);
15607 *err_code = EMAIL_ERROR_INVALID_PARAM;
15611 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15613 /* increase unique id */
15615 sql = "SELECT max(rowid) FROM mail_local_activity_tbl;";
15617 /* 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); */
15618 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15619 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
15621 if (NULL == result[1])
15624 rc = atoi(result[1])+1;
15629 sqlite3_free_table(result);
15638 EM_DEBUG_FUNC_END("ret [%d]", ret);
15643 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)
15646 EM_DEBUG_FUNC_BEGIN();
15648 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);
15650 if (account_id <= 0 || NULL == activity_id_list || NULL == activity_id_count || lowest_activity_type <= 0 || highest_activity_type <= 0) {
15651 if (err_code != NULL)
15652 *err_code = EMAIL_ERROR_INVALID_PARAM;
15657 int error = EMAIL_ERROR_NONE;
15658 int i = 0, rc = -1, count = 0;
15659 char sql_query_string[1024] = {0x00, };
15660 int *activity_ids = NULL;
15662 char **result = NULL;
15663 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15664 EMSTORAGE_START_READ_TRANSACTION(transaction);
15666 memset(sql_query_string, 0x00, sizeof(sql_query_string));
15668 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);
15670 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
15673 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15674 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15675 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15679 EM_DEBUG_LOG(" Activity COUNT : %d ... ", count);
15681 if (NULL == (activity_ids = (int *)em_malloc(sizeof(int) * count))) {
15682 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
15683 error = EMAIL_ERROR_OUT_OF_MEMORY;
15687 for (i = 0; i < count; i++) {
15688 activity_ids[i] = atoi(result[col_index]);
15690 EM_DEBUG_LOG("activity_id %d", activity_ids[i]);
15699 *activity_id_count = count;
15700 *activity_id_list = activity_ids;
15701 } else if (activity_ids != NULL) /* Prevent defect - 216566 */
15702 EM_SAFE_FREE(activity_ids);
15705 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15706 if (err_code != NULL)
15709 EM_DEBUG_FUNC_END("ret [%d]", ret);
15713 INTERNAL_FUNC int emstorage_free_activity_id_list(int *activity_id_list, int *error_code)
15715 EM_DEBUG_FUNC_BEGIN();
15717 int error = EMAIL_ERROR_NONE;
15720 EM_DEBUG_LOG(" activity_id_list [%p]", activity_id_list);
15722 if (NULL == activity_id_list) {
15723 error = EMAIL_ERROR_INVALID_PARAM;
15726 EM_SAFE_FREE(activity_id_list);
15734 if (NULL != error_code)
15735 *error_code = error;
15737 EM_DEBUG_FUNC_END("ret [%d]", ret);
15742 * emstorage_delete_local_activity - Deletes the Local acitivity Generated based on activity_type
15743 * or based on server mail id
15746 INTERNAL_FUNC int emstorage_delete_local_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15748 EM_DEBUG_FUNC_BEGIN();
15751 EM_DEBUG_LOG(" local_activity[%p] ", local_activity);
15753 if (!local_activity) {
15754 EM_DEBUG_EXCEPTION(" local_activity[%p] ", local_activity);
15755 if (err_code != NULL)
15756 *err_code = EMAIL_ERROR_INVALID_PARAM;
15760 int rc = -1, ret = false; /* Prevent_FIX */
15761 int err = EMAIL_ERROR_NONE;
15763 int query_where = 0;
15764 char sql_query_string[8192] = { 0x00, };
15765 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15766 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15768 memset(sql_query_string, 0x00, sizeof(sql_query_string));
15770 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_local_activity_tbl ");
15772 EM_DEBUG_LOG_SEC(">>> Query [ %s ] ", sql_query_string);
15774 if (local_activity->account_id) {
15775 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15776 " WHERE account_id = %d ", local_activity->account_id);
15781 if (local_activity->server_mailid) {
15782 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15783 " %s %s server_mailid = '%s' ", query_where ? "" : "WHERE", query_and ? "AND" : "", local_activity->server_mailid);
15789 if (local_activity->mail_id) {
15790 EM_DEBUG_LOG(">>>> MAIL ID [ %d ] , ACTIVITY TYPE [%d ]", local_activity->mail_id, local_activity->activity_type);
15792 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15793 " %s %s mail_id = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "", local_activity->mail_id);
15800 if (local_activity->activity_type > 0) {
15801 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15802 " %s %s activity_type = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "" , local_activity->activity_type);
15805 if (local_activity->activity_id > 0) {
15806 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15807 " %s %s activity_id = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "" , local_activity->activity_id);
15811 EM_DEBUG_LOG_SEC(">>>>> Query [ %s ] ", sql_query_string);
15812 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15813 if (error != EMAIL_ERROR_NONE) {
15814 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
15818 rc = sqlite3_changes(local_db_handle);
15820 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
15821 err = EMAIL_ERROR_MAILBOX_NOT_FOUND;
15827 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15829 if (hStmt != NULL) {
15830 rc = sqlite3_finalize(hStmt);
15831 if (rc != SQLITE_OK) {
15832 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15833 error = EMAIL_ERROR_DB_FAILURE;
15837 if (err_code != NULL)
15839 EM_DEBUG_FUNC_END("ret [%d]", ret);
15844 * emstorage_free_local_activity - Free the Local Activity data
15846 INTERNAL_FUNC int emstorage_free_local_activity(emstorage_activity_tbl_t **local_activity_list, int count, int *err_code)
15848 EM_DEBUG_FUNC_BEGIN();
15850 EM_DEBUG_LOG(" local_activity_list[%p], count[%d], err_code[%p]", local_activity_list, count, err_code);
15853 int error = EMAIL_ERROR_INVALID_PARAM;
15856 if (!local_activity_list || !*local_activity_list) {
15857 EM_DEBUG_EXCEPTION(" local_activity_list[%p], count[%d]", local_activity_list, count);
15859 error = EMAIL_ERROR_INVALID_PARAM;
15863 emstorage_activity_tbl_t* p = *local_activity_list;
15866 for (; i < count; i++) {
15867 EM_SAFE_FREE(p[i].dest_mbox);
15868 EM_SAFE_FREE(p[i].src_mbox);
15869 EM_SAFE_FREE(p[i].server_mailid);
15873 *local_activity_list = NULL;
15881 if (err_code != NULL)
15884 EM_DEBUG_FUNC_END("ret [%d]", ret);
15888 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
15891 static int _get_key_value_string_for_list_filter_rule(email_list_filter_rule_t *input_list_filter_rule, char **output_key_value_string)
15893 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_key_value_string [%p]", input_list_filter_rule, output_key_value_string);
15895 int ret = EMAIL_ERROR_NONE;
15896 char key_value_string[QUERY_SIZE] = { 0, };
15897 char *temp_key_value_1 = NULL;
15898 char *temp_key_value_2 = NULL;
15900 if (input_list_filter_rule == NULL || output_key_value_string == NULL) {
15901 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15902 return EMAIL_ERROR_INVALID_PARAM;
15905 switch (input_list_filter_rule->target_attribute) {
15906 case EMAIL_MAIL_ATTRIBUTE_MAIL_ID:
15907 case EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID:
15908 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID:
15909 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE:
15910 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_STATUS:
15911 case EMAIL_MAIL_ATTRIBUTE_REFERENCE_MAIL_ID:
15912 case EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS:
15913 case EMAIL_MAIL_ATTRIBUTE_MAIL_SIZE:
15914 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN:
15915 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML:
15916 case EMAIL_MAIL_ATTRIBUTE_FILE_SIZE:
15917 case EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD:
15918 case EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD:
15919 case EMAIL_MAIL_ATTRIBUTE_FLAGS_FLAGGED_FIELD:
15920 case EMAIL_MAIL_ATTRIBUTE_FLAGS_ANSWERED_FIELD:
15921 case EMAIL_MAIL_ATTRIBUTE_FLAGS_RECENT_FIELD:
15922 case EMAIL_MAIL_ATTRIBUTE_FLAGS_DRAFT_FIELD:
15923 case EMAIL_MAIL_ATTRIBUTE_FLAGS_FORWARDED_FIELD:
15924 case EMAIL_MAIL_ATTRIBUTE_DRM_STATUS:
15925 case EMAIL_MAIL_ATTRIBUTE_PRIORITY:
15926 case EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS:
15927 case EMAIL_MAIL_ATTRIBUTE_LOCK_STATUS:
15928 case EMAIL_MAIL_ATTRIBUTE_REPORT_STATUS:
15929 case EMAIL_MAIL_ATTRIBUTE_ATTACHMENT_COUNT:
15930 case EMAIL_MAIL_ATTRIBUTE_INLINE_CONTENT_COUNT:
15931 case EMAIL_MAIL_ATTRIBUTE_THREAD_ID:
15932 case EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT:
15933 case EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS:
15934 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_CLASS:
15935 case EMAIL_MAIL_ATTRIBUTE_DIGEST_TYPE:
15936 case EMAIL_MAIL_ATTRIBUTE_SMIME_TYPE:
15937 case EMAIL_MAIL_ATTRIBUTE_REMAINING_RESEND_TIMES:
15938 case EMAIL_MAIL_ATTRIBUTE_TAG_ID:
15939 case EMAIL_MAIL_ATTRIBUTE_EAS_DATA_LENGTH_TYPE:
15940 SNPRINTF(key_value_string, QUERY_SIZE, "%d", input_list_filter_rule->key_value.integer_type_value);
15943 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
15944 case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
15945 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
15946 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
15947 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
15948 case EMAIL_MAIL_ATTRIBUTE_FROM:
15949 case EMAIL_MAIL_ATTRIBUTE_TO:
15950 case EMAIL_MAIL_ATTRIBUTE_CC:
15951 case EMAIL_MAIL_ATTRIBUTE_BCC:
15952 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
15953 if (input_list_filter_rule->key_value.string_type_value == NULL) {
15954 EM_DEBUG_EXCEPTION("Invalid string_type_value [%p]", input_list_filter_rule->key_value.string_type_value);
15955 ret = EMAIL_ERROR_INVALID_PARAM;
15959 temp_key_value_1 = input_list_filter_rule->key_value.string_type_value;
15961 temp_key_value_2 = em_replace_all_string(temp_key_value_1, "_", "\\_");
15962 temp_key_value_1 = em_replace_all_string(temp_key_value_2, "%", "\\%");
15964 if (input_list_filter_rule->rule_type == EMAIL_LIST_FILTER_RULE_INCLUDE)
15965 SNPRINTF(key_value_string, QUERY_SIZE, "\'%%%s%%\'", temp_key_value_1);
15967 SNPRINTF(key_value_string, QUERY_SIZE, "\'%s\'", temp_key_value_1);
15970 case EMAIL_MAIL_ATTRIBUTE_DATE_TIME:
15971 case EMAIL_MAIL_ATTRIBUTE_SCHEDULED_SENDING_TIME:
15972 case EMAIL_MAIL_ATTRIBUTE_REPLIED_TIME:
15973 case EMAIL_MAIL_ATTRIBUTE_FORWARDED_TIME:
15974 SNPRINTF(key_value_string, QUERY_SIZE, "%d", (int)input_list_filter_rule->key_value.datetime_type_value);
15978 ret = EMAIL_ERROR_INVALID_PARAM;
15979 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
15983 if (ret == EMAIL_ERROR_NONE && EM_SAFE_STRLEN(key_value_string) > 0)
15984 *output_key_value_string = strdup(key_value_string);
15988 EM_SAFE_FREE(temp_key_value_1);
15989 EM_SAFE_FREE(temp_key_value_2);
15991 EM_DEBUG_FUNC_END("ret [%d]", ret);
15995 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15996 static int _get_cert_password_file_name(int index, char *cert_password_file_name)
15998 EM_DEBUG_FUNC_BEGIN("index : [%d]", index);
16000 if (index <= 0 || !cert_password_file_name) {
16001 EM_DEBUG_EXCEPTION("Invalid parameter");
16002 return EMAIL_ERROR_INVALID_PARAM;
16005 sprintf(cert_password_file_name, ".email_cert_%d", index);
16007 EM_DEBUG_FUNC_END();
16008 return EMAIL_ERROR_NONE;
16012 static int _make_filter_rule_string(email_list_filter_rule_t *input_list_filter_rule, char **output_string)
16014 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16016 int ret = EMAIL_ERROR_NONE;
16018 int length_field_name = 0;
16019 int length_value = 0;
16020 char result_rule_string[QUERY_SIZE] = { 0 , };
16021 char *mod_field_name_string = NULL;
16022 char *mod_value_string = NULL;
16023 char *temp_field_name_string = NULL;
16024 char *temp_key_value_string = NULL;
16026 if (input_list_filter_rule == NULL || output_string == NULL) {
16027 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16028 return EMAIL_ERROR_INVALID_PARAM;
16031 temp_field_name_string = emcore_get_mail_field_name_by_attribute_type(input_list_filter_rule->target_attribute);
16033 if (temp_field_name_string == NULL) {
16034 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
16035 return EMAIL_ERROR_INVALID_PARAM;
16038 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) {
16039 EM_DEBUG_EXCEPTION("_get_key_value_string_for_list_filter_rule failed");
16040 return EMAIL_ERROR_INVALID_PARAM;
16043 length_field_name = EM_SAFE_STRLEN(temp_field_name_string);
16044 length_value = EM_SAFE_STRLEN(temp_key_value_string);
16046 switch (input_list_filter_rule->target_attribute) {
16047 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
16048 case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
16049 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
16050 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
16051 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
16052 case EMAIL_MAIL_ATTRIBUTE_FROM:
16053 case EMAIL_MAIL_ATTRIBUTE_TO:
16054 case EMAIL_MAIL_ATTRIBUTE_CC:
16055 case EMAIL_MAIL_ATTRIBUTE_BCC:
16056 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
16064 if (is_alpha == 1 && input_list_filter_rule->case_sensitivity == false) {
16065 length_field_name += strlen("UPPER() ");
16066 length_value += strlen("UPPER() ");
16067 mod_field_name_string = em_malloc(sizeof(char) * length_field_name);
16068 if (mod_field_name_string == NULL) {
16069 EM_DEBUG_EXCEPTION("em_mallocfailed");
16070 EM_SAFE_FREE(temp_field_name_string);
16071 ret = EMAIL_ERROR_OUT_OF_MEMORY;
16075 mod_value_string = em_malloc(sizeof(char) * length_value);
16076 if (mod_value_string == NULL) {
16077 EM_DEBUG_EXCEPTION("em_mallocfailed");
16078 EM_SAFE_FREE(temp_field_name_string);
16079 ret = EMAIL_ERROR_OUT_OF_MEMORY;
16083 SNPRINTF(mod_field_name_string, length_field_name, "UPPER(%s)", temp_field_name_string);
16084 SNPRINTF(mod_value_string, length_value, "UPPER(%s)", temp_key_value_string);
16085 EM_SAFE_FREE(temp_key_value_string);
16087 mod_field_name_string = strdup(temp_field_name_string);
16088 mod_value_string = temp_key_value_string;
16091 switch (input_list_filter_rule->rule_type) {
16092 case EMAIL_LIST_FILTER_RULE_EQUAL:
16093 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = %s ", mod_field_name_string, mod_value_string);
16095 case EMAIL_LIST_FILTER_RULE_NOT_EQUAL:
16096 SNPRINTF(result_rule_string, QUERY_SIZE, "%s != %s ", mod_field_name_string, mod_value_string);
16098 case EMAIL_LIST_FILTER_RULE_LESS_THAN:
16099 SNPRINTF(result_rule_string, QUERY_SIZE, "%s < %s ", mod_field_name_string, mod_value_string);
16101 case EMAIL_LIST_FILTER_RULE_GREATER_THAN:
16102 SNPRINTF(result_rule_string, QUERY_SIZE, "%s > %s ", mod_field_name_string, mod_value_string);
16104 case EMAIL_LIST_FILTER_RULE_LESS_THAN_OR_EQUAL:
16105 SNPRINTF(result_rule_string, QUERY_SIZE, "%s <= %s ", mod_field_name_string, mod_value_string);
16107 case EMAIL_LIST_FILTER_RULE_GREATER_THAN_OR_EQUAL:
16108 SNPRINTF(result_rule_string, QUERY_SIZE, "%s >= %s ", mod_field_name_string, mod_value_string);
16110 case EMAIL_LIST_FILTER_RULE_INCLUDE:
16111 SNPRINTF(result_rule_string, QUERY_SIZE, "%s LIKE %s ", mod_field_name_string, mod_value_string);
16113 case EMAIL_LIST_FILTER_RULE_IN:
16114 SNPRINTF(result_rule_string, QUERY_SIZE, "%s IN (%s) ", mod_field_name_string, mod_value_string);
16116 case EMAIL_LIST_FILTER_RULE_NOT_IN:
16117 SNPRINTF(result_rule_string, QUERY_SIZE, "%s NOT IN (%s) ", mod_field_name_string, mod_value_string);
16120 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16121 ret = EMAIL_ERROR_INVALID_PARAM;
16125 *output_string = strdup(result_rule_string);
16128 EM_SAFE_FREE(mod_field_name_string);
16129 EM_SAFE_FREE(mod_value_string);
16131 EM_DEBUG_FUNC_END("ret [%d]", ret);
16135 static int _make_filter_attach_rule_string(char *multi_user_name, email_list_filter_rule_attach_t *input_list_filter_rule, char **output_string)
16137 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16139 char *field_name_string = NULL;
16140 char key_value_string[QUERY_SIZE] = {0,};
16141 char result_rule_string[QUERY_SIZE] = {0,};
16144 int query_size = 0;
16147 int error = EMAIL_ERROR_NONE;
16148 char **result = NULL;
16149 char sql_query_string[QUERY_SIZE] = {0,};
16150 char *sql_query_string2 = NULL;
16151 sqlite3 *local_db_handle = NULL;
16152 int *mail_ids = NULL;
16154 if (input_list_filter_rule == NULL || output_string == NULL) {
16155 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16156 return EMAIL_ERROR_INVALID_PARAM;
16159 field_name_string = EM_SAFE_STRDUP("attachment_name");
16160 SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
16162 switch (input_list_filter_rule->rule_type) {
16164 case EMAIL_LIST_FILTER_RULE_INCLUDE:
16165 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
16168 case EMAIL_LIST_FILTER_RULE_MATCH:
16169 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
16173 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16174 error = EMAIL_ERROR_INVALID_PARAM;
16178 local_db_handle = emstorage_get_db_connection(multi_user_name);
16180 EMSTORAGE_START_READ_TRANSACTION(true);
16181 SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_attachment_tbl %s", result_rule_string);
16183 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16184 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16185 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16190 EM_DEBUG_LOG("No mail found...");
16191 error = EMAIL_ERROR_MAIL_NOT_FOUND;
16192 *output_string = strdup("mail_id IN () ");
16196 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
16198 if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
16199 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
16200 error = EMAIL_ERROR_OUT_OF_MEMORY;
16204 for (i = 0; i < count; i++) {
16205 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16206 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16209 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >>", count);
16210 sqlite3_free_table(result);
16211 EMSTORAGE_FINISH_READ_TRANSACTION(true);
16213 // sqlite3_db_release_memory(local_db_handle);
16216 query_size = (10 * count) + strlen("mail_id IN () ");
16218 sql_query_string2 = em_malloc(query_size);
16219 if (sql_query_string2 == NULL) {
16220 EM_DEBUG_EXCEPTION("em_mallocfailed");
16221 error = EMAIL_ERROR_OUT_OF_MEMORY;
16225 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN (");
16226 for (i = 0; i < count-1; i++)
16227 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16229 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d) ", mail_ids[count-1]);
16231 *output_string = strdup(sql_query_string2);
16234 EM_SAFE_FREE(mail_ids); /* prevent */
16235 EM_SAFE_FREE(sql_query_string2);
16236 EM_SAFE_FREE(field_name_string);
16237 EM_DEBUG_FUNC_END("error [%d]", error);
16241 static int _make_filter_fts_rule_string(char *multi_user_name, email_list_filter_rule_fts_t *input_list_filter_rule, char **output_string)
16243 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16244 char *field_name_string = NULL;
16245 char key_value_string[QUERY_SIZE] = {0,};
16246 char result_rule_string[QUERY_SIZE] = {0,};
16250 int query_size = 0;
16251 int error = EMAIL_ERROR_NONE;
16252 char **result = NULL;
16253 char sql_query_string[QUERY_SIZE] = {0,};
16254 char *sql_query_string2 = NULL;
16255 sqlite3 *local_db_handle = NULL;
16256 int *mail_ids = NULL;
16258 if (input_list_filter_rule == NULL || output_string == NULL) {
16259 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16260 return EMAIL_ERROR_INVALID_PARAM;
16263 field_name_string = EM_SAFE_STRDUP("body_text");
16264 SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
16266 switch (input_list_filter_rule->rule_type) {
16268 case EMAIL_LIST_FILTER_RULE_INCLUDE:
16269 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
16272 case EMAIL_LIST_FILTER_RULE_MATCH:
16273 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
16277 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16278 error = EMAIL_ERROR_INVALID_PARAM;
16282 local_db_handle = emstorage_get_db_connection(multi_user_name);
16284 EMSTORAGE_START_READ_TRANSACTION(true);
16285 SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_text_tbl %s", result_rule_string);
16287 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16288 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16289 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16294 EM_DEBUG_LOG("No mail found...");
16295 error = EMAIL_ERROR_MAIL_NOT_FOUND;
16296 *output_string = strdup("mail_id IN () ");
16300 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
16303 if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
16304 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
16305 error = EMAIL_ERROR_OUT_OF_MEMORY;
16309 for (i = 0; i < count; i++) {
16310 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16311 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16314 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >>", count);
16315 sqlite3_free_table(result);
16316 EMSTORAGE_FINISH_READ_TRANSACTION(true);
16318 // sqlite3_db_release_memory(local_db_handle);
16321 query_size = (10 * count) + strlen("mail_id IN () ");
16322 sql_query_string2 = em_malloc(query_size);
16323 if (sql_query_string2 == NULL) {
16324 EM_DEBUG_EXCEPTION("em_mallocfailed");
16325 error = EMAIL_ERROR_OUT_OF_MEMORY;
16329 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN (");
16330 for (i = 0; i < count-1; i++)
16331 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16333 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d) ", mail_ids[count-1]);
16335 *output_string = strdup(sql_query_string2);
16338 EM_SAFE_FREE(mail_ids); /* prevent */
16339 EM_SAFE_FREE(sql_query_string2);
16340 EM_SAFE_FREE(field_name_string);
16341 EM_DEBUG_FUNC_END("error [%d]", error);
16345 static int _make_order_rule_string(char *multi_user_name, email_list_sorting_rule_t *input_sorting_rule, char **output_string)
16347 EM_DEBUG_FUNC_BEGIN("input_sorting_rule [%p], output_string [%p]", input_sorting_rule, output_string);
16349 char result_rule_string[QUERY_SIZE] = { 0 , };
16350 int ret = EMAIL_ERROR_NONE;
16352 emstorage_account_tbl_t *account_tbl_array = NULL;
16355 char *result_str = NULL;
16356 char *tmp_str1 = NULL;
16357 char *tmp_str2 = NULL;
16358 char query_per_account[QUERY_SIZE] = { 0 , };
16360 if (input_sorting_rule->force_boolean_check) {
16361 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = 0 ",
16362 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16364 EM_SAFE_STRNCPY(result_rule_string,
16365 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16366 QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16369 switch (input_sorting_rule->sort_order) {
16370 case EMAIL_SORT_ORDER_ASCEND:
16371 EM_SAFE_STRNCAT(result_rule_string, " ASC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16374 case EMAIL_SORT_ORDER_DESCEND:
16375 EM_SAFE_STRNCAT(result_rule_string, " DESC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16378 case EMAIL_SORT_ORDER_NOCASE_ASCEND:
16379 EM_SAFE_STRNCAT(result_rule_string, " COLLATE NOCASE ASC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16382 case EMAIL_SORT_ORDER_NOCASE_DESCEND:
16383 EM_SAFE_STRNCAT(result_rule_string, " COLLATE NOCASE DESC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16386 case EMAIL_SORT_ORDER_TO_CCBCC:
16387 memset(result_rule_string, 0, QUERY_SIZE);
16388 if (input_sorting_rule->key_value.string_type_value)
16389 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16390 " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 ELSE 2 END ",
16391 input_sorting_rule->key_value.string_type_value);
16394 case EMAIL_SORT_ORDER_TO_CC_BCC:
16395 memset(result_rule_string, 0, QUERY_SIZE);
16396 if (input_sorting_rule->key_value.string_type_value)
16397 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16398 " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 WHEN full_address_cc LIKE \'%%%q%%\' THEN 2 ELSE 3 END ",
16399 input_sorting_rule->key_value.string_type_value, input_sorting_rule->key_value.string_type_value);
16402 case EMAIL_SORT_ORDER_TO_CCBCC_ALL:
16403 if (!emstorage_get_account_list(multi_user_name, &count, &account_tbl_array, true, false, NULL)) {
16404 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed");
16409 EM_DEBUG_LOG("No account exist");
16410 ret = EMAIL_ERROR_INVALID_PARAM;
16414 for (i = 0; i < count; i++) {
16415 if (i > 0 && result_str) {
16416 tmp_str2 = result_str;
16417 result_str = g_strconcat(tmp_str2, " OR ", NULL);
16418 EM_SAFE_FREE(tmp_str2);
16421 memset(query_per_account, 0, QUERY_SIZE);
16422 snprintf(query_per_account, QUERY_SIZE,
16423 "(account_id = %d AND full_address_to LIKE \'%%%s%%\')",
16424 account_tbl_array[i].account_id, account_tbl_array[i].user_email_address);
16426 tmp_str1 = result_str;
16428 result_str = g_strconcat(tmp_str1, query_per_account, NULL);
16430 result_str = g_strdup(query_per_account);
16431 EM_SAFE_FREE(tmp_str1);
16434 snprintf(result_rule_string, QUERY_SIZE,
16435 " CASE WHEN %s THEN 1 ELSE 2 END ", result_str);
16437 EM_SAFE_FREE(result_str);
16438 if (account_tbl_array)
16439 emstorage_free_account(&account_tbl_array, count, NULL);
16442 case EMAIL_SORT_ORDER_LOCALIZE_ASCEND:
16443 memset(result_rule_string, 0, QUERY_SIZE);
16444 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16445 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END ASC, %s COLLATE NOCASE ASC ",
16446 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16447 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16450 case EMAIL_SORT_ORDER_LOCALIZE_DESCEND:
16451 memset(result_rule_string, 0, QUERY_SIZE);
16452 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16453 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END DESC, %s COLLATE NOCASE DESC ",
16454 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16455 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16459 EM_DEBUG_EXCEPTION("Invalid sort_order [%d]", input_sorting_rule->sort_order);
16460 ret = EMAIL_ERROR_INVALID_PARAM;
16464 *output_string = strdup(result_rule_string);
16467 EM_DEBUG_FUNC_END("ret [%d]", ret);
16471 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)
16473 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);
16474 int ret = EMAIL_ERROR_NONE;
16476 int string_offset = 0;
16477 int query_size = 0;
16478 int new_query_size = 0;
16479 char *conditional_clause_string = NULL;
16480 char *result_string_for_a_item = NULL;
16482 if ((input_filter_count > 0 && !input_filter_list) || (input_sorting_rule_count > 0 && !input_sorting_rule_list) || output_conditional_clause == NULL) {
16483 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16484 return EMAIL_ERROR_INVALID_PARAM;
16487 conditional_clause_string = em_malloc(QUERY_SIZE);
16488 if (conditional_clause_string == NULL) {
16489 EM_DEBUG_EXCEPTION("Memory is full");
16490 return EMAIL_ERROR_OUT_OF_MEMORY;
16493 if (input_filter_count > 0) {
16494 query_size = QUERY_SIZE;
16495 g_strlcpy(conditional_clause_string, " WHERE ", QUERY_SIZE);
16497 for (i = 0; i < input_filter_count; i++) {
16498 switch (input_filter_list[i].list_filter_item_type) {
16499 case EMAIL_LIST_FILTER_ITEM_RULE:
16500 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE", i);
16501 _make_filter_rule_string(&(input_filter_list[i].list_filter_item.rule), &result_string_for_a_item);
16504 case EMAIL_LIST_FILTER_ITEM_RULE_FTS:
16505 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_FTS", i);
16506 _make_filter_fts_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_fts), &result_string_for_a_item);
16509 case EMAIL_LIST_FILTER_ITEM_RULE_ATTACH:
16510 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_ATTACH", i);
16511 _make_filter_attach_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_attach), &result_string_for_a_item);
16514 case EMAIL_LIST_FILTER_ITEM_OPERATOR:
16515 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_OPERATOR", i);
16516 switch (input_filter_list[i].list_filter_item.operator_type) {
16517 case EMAIL_LIST_FILTER_OPERATOR_AND:
16518 result_string_for_a_item = strdup("AND ");
16520 case EMAIL_LIST_FILTER_OPERATOR_OR:
16521 result_string_for_a_item = strdup("OR ");
16523 case EMAIL_LIST_FILTER_OPERATOR_LEFT_PARENTHESIS:
16524 result_string_for_a_item = strdup(" (");
16526 case EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS:
16527 result_string_for_a_item = strdup(") ");
16533 EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", input_filter_list[i].list_filter_item_type);
16534 ret = EMAIL_ERROR_INVALID_PARAM;
16538 if (result_string_for_a_item == NULL) {
16539 EM_DEBUG_EXCEPTION("result_string_for_a_item is null");
16540 ret = EMAIL_ERROR_INVALID_PARAM;
16544 if (strlen(conditional_clause_string) + EM_SAFE_STRLEN(result_string_for_a_item) >= query_size) { /* prevent 34364 */
16545 EM_DEBUG_LOG("QUERY is too long");
16546 new_query_size = EM_SAFE_STRLEN(result_string_for_a_item) + EM_SAFE_STRLEN(conditional_clause_string) + QUERY_SIZE;
16547 conditional_clause_string = realloc(conditional_clause_string, new_query_size);
16548 if (conditional_clause_string == NULL) {
16549 EM_DEBUG_EXCEPTION("realloc failed");
16550 ret = EMAIL_ERROR_OUT_OF_MEMORY;
16554 query_size = new_query_size;
16557 EM_SAFE_STRNCAT(conditional_clause_string, result_string_for_a_item , QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16558 EM_SAFE_FREE(result_string_for_a_item);
16562 if (input_sorting_rule_count > 0) {
16563 EM_SAFE_STRNCAT(conditional_clause_string, "ORDER BY ", QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16565 for (i = 0; i < input_sorting_rule_count; i++) {
16566 if ((ret = _make_order_rule_string(multi_user_name, &input_sorting_rule_list[i], &result_string_for_a_item)) != EMAIL_ERROR_NONE) {
16567 EM_DEBUG_EXCEPTION("_make_order_rule_string failed. [%d]", ret);
16571 EM_SAFE_STRNCAT(conditional_clause_string, ", " , QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16572 EM_SAFE_STRNCAT(conditional_clause_string, result_string_for_a_item , QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16573 EM_SAFE_FREE(result_string_for_a_item);
16577 if (input_start_index != -1 && input_limit_count != -1) {
16578 string_offset = strlen(conditional_clause_string);
16579 SNPRINTF_OFFSET(conditional_clause_string, string_offset, query_size, " LIMIT %d, %d", input_start_index, input_limit_count);
16582 *output_conditional_clause = strdup(conditional_clause_string);
16585 EM_SAFE_FREE(result_string_for_a_item);
16586 EM_SAFE_FREE(conditional_clause_string);
16588 EM_DEBUG_FUNC_END("ret [%d]", ret);
16592 INTERNAL_FUNC int emstorage_free_list_filter(email_list_filter_t **input_filter_list, int input_filter_count)
16594 EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d]", input_filter_list, input_filter_count);
16595 int err = EMAIL_ERROR_NONE;
16597 email_list_filter_t *temp_filter_list = NULL;
16599 EM_IF_NULL_RETURN_VALUE(input_filter_list, EMAIL_ERROR_INVALID_PARAM);
16601 for (i = 0; i < input_filter_count; i++) {
16602 temp_filter_list = (*input_filter_list) + i;
16603 if (!temp_filter_list)
16606 if (temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE) {
16607 switch (temp_filter_list->list_filter_item.rule.target_attribute) {
16608 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
16609 case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
16610 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
16611 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
16612 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
16613 case EMAIL_MAIL_ATTRIBUTE_FROM:
16614 case EMAIL_MAIL_ATTRIBUTE_TO:
16615 case EMAIL_MAIL_ATTRIBUTE_CC:
16616 case EMAIL_MAIL_ATTRIBUTE_BCC:
16617 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN:
16618 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML:
16619 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
16620 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule.key_value.string_type_value);
16625 } 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) {
16626 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_fts.key_value.string_type_value);
16627 } 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) {
16628 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_attach.key_value.string_type_value);
16632 EM_SAFE_FREE(*input_filter_list);
16634 EM_DEBUG_FUNC_END("err [%d]", err);
16638 /* Tasks --------------------------------------------------------------------------*/
16639 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)
16641 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);
16645 int err = EMAIL_ERROR_NONE;
16647 DB_STMT hStmt = NULL;
16648 char sql_query_string[QUERY_SIZE] = {0, };
16649 sqlite3 *local_db_handle = NULL;
16650 char *sql = "SELECT max(rowid) FROM mail_task_tbl;";
16651 char **result = NULL;
16653 if (input_task_parameter == NULL || output_task_id == NULL) {
16654 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16655 return EMAIL_ERROR_INVALID_PARAM;
16658 local_db_handle = emstorage_get_db_connection(multi_user_name);
16659 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, err);
16661 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
16662 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16663 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
16665 if (NULL == result[1])
16668 task_id = atoi(result[1])+1;
16670 *output_task_id = task_id;
16672 sqlite3_free_table(result);
16675 SNPRINTF(sql_query_string, sizeof(sql_query_string),
16676 "INSERT INTO mail_task_tbl VALUES "
16678 " ? " /* task_id */
16679 " , ? " /* task_type */
16680 " , ? " /* task_status */
16681 " , ? " /* task_priority */
16682 " , ? " /* task_parameter_length */
16683 " , ? " /* task_parameter */
16684 " , ? " /* date_time */
16687 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16688 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16689 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16691 EM_DEBUG_LOG_SEC(">>>> SQL STMT [%s] ", sql_query_string);
16694 _bind_stmt_field_data_int(hStmt, i++, task_id);
16695 _bind_stmt_field_data_int(hStmt, i++, input_task_type);
16696 _bind_stmt_field_data_int(hStmt, i++, EMAIL_TASK_STATUS_WAIT);
16697 _bind_stmt_field_data_int(hStmt, i++, input_task_priority);
16698 _bind_stmt_field_data_int(hStmt, i++, input_task_parameter_length);
16699 _bind_stmt_field_data_blob(hStmt, i++, input_task_parameter, input_task_parameter_length);
16700 _bind_stmt_field_data_int(hStmt, i++, time(NULL));
16702 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16704 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {err = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
16705 ("sqlite3_step fail:%d", rc));
16706 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16707 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
16709 ret = (err == EMAIL_ERROR_NONE);
16712 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, err);
16714 if (hStmt != NULL) {
16715 rc = sqlite3_finalize(hStmt);
16716 if (rc != SQLITE_OK) {
16717 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16718 err = EMAIL_ERROR_DB_FAILURE;
16722 EM_DEBUG_FUNC_END("err [%d]", err);
16726 INTERNAL_FUNC int emstorage_delete_task(char *multi_user_name, int task_id, int transaction)
16728 EM_DEBUG_FUNC_BEGIN("task_id[%d], transaction[%d]", task_id, transaction);
16730 int err = EMAIL_ERROR_NONE;
16731 char sql_query_string[QUERY_SIZE] = {0, };
16733 sqlite3 *local_db_handle = NULL;
16736 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16737 return EMAIL_ERROR_INVALID_PARAM;
16740 local_db_handle = emstorage_get_db_connection(multi_user_name);
16742 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
16744 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_task_tbl WHERE task_id = %d", task_id);
16745 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16746 if (err != EMAIL_ERROR_NONE) {
16747 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16754 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
16756 EM_DEBUG_FUNC_END("err [%d]", err);
16760 INTERNAL_FUNC int emstorage_update_task_status(char *multi_user_name, int task_id, email_task_status_type_t task_status, int transaction)
16762 EM_DEBUG_FUNC_BEGIN("task_id[%d] task_status[%d] transaction[%d]", task_id, task_status, transaction);
16764 int err = EMAIL_ERROR_NONE;
16765 char sql_query_string[QUERY_SIZE] = {0, };
16767 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16768 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
16770 SNPRINTF(sql_query_string, sizeof(sql_query_string),
16771 "UPDATE mail_task_tbl SET"
16772 " task_status = %d"
16773 " WHERE task_id = %d"
16776 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16777 if (err != EMAIL_ERROR_NONE) {
16778 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16785 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
16787 EM_DEBUG_FUNC_END("err [%d]", err);
16791 INTERNAL_FUNC int emstorage_query_task(char *multi_user_name,
16792 const char *input_conditional_clause,
16793 const char *input_ordering_clause,
16794 email_task_t **output_task_list,
16795 int *output_task_count)
16797 EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_ordering_clause [%p], "
16798 "output_task_list[%p], output_task_count[%d]",
16799 input_conditional_clause, input_ordering_clause, output_task_list, output_task_count);
16800 int i = 0, count = 0, rc = -1;
16802 int field_index = 0;
16803 int err = EMAIL_ERROR_NONE;
16804 email_task_t *task_item_from_tbl = NULL;
16805 char sql_query_string[QUERY_SIZE] = {0, };
16806 char *field_list = "task_id, task_type, task_status, task_priority, task_parameter_length, task_parameter ";
16808 sqlite3 *local_db_handle = NULL;
16809 DB_STMT hStmt = NULL;
16811 EM_IF_NULL_RETURN_VALUE(input_conditional_clause, false);
16812 EM_IF_NULL_RETURN_VALUE(output_task_count, false);
16814 local_db_handle = emstorage_get_db_connection(multi_user_name);
16816 SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
16817 "SELECT COUNT(*) FROM mail_task_tbl %s", input_conditional_clause);
16818 EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
16820 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
16821 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16822 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16824 count = atoi(result[1]);
16825 sqlite3_free_table(result);
16827 EM_DEBUG_LOG("count = %d", rc);
16830 EM_DEBUG_EXCEPTION("no task found...");
16831 err = EMAIL_ERROR_TASK_NOT_FOUND;
16835 SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
16836 "SELECT %s FROM mail_task_tbl %s %s", field_list, input_conditional_clause, input_ordering_clause);
16837 EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
16839 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16841 EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
16842 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16843 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16845 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16846 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16847 ("sqlite3_step fail:%d", rc));
16849 if (rc == SQLITE_DONE) {
16850 EM_DEBUG_EXCEPTION("no task found...");
16851 err = EMAIL_ERROR_TASK_NOT_FOUND;
16856 if (!(task_item_from_tbl = (email_task_t*)em_malloc(sizeof(email_task_t) * count))) {
16857 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
16858 err = EMAIL_ERROR_OUT_OF_MEMORY;
16862 for (i = 0; i < count; i++) {
16863 /* get recordset */
16866 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_id), field_index++);
16867 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_type), field_index++);
16868 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_status), field_index++);
16869 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_priority), field_index++);
16870 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_parameter_length), field_index++);
16871 _get_stmt_field_data_blob(hStmt, (void**)&(task_item_from_tbl[i].task_parameter), field_index++);
16873 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16874 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i, rc);
16875 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16876 ("sqlite3_step fail:%d", rc));
16881 if (err == EMAIL_ERROR_NONE && output_task_list) {
16882 *output_task_list = task_item_from_tbl;
16883 *output_task_count = count;
16885 if (task_item_from_tbl) {
16886 for (i = 0; i < count; i++)
16887 EM_SAFE_FREE(task_item_from_tbl[i].task_parameter);
16889 free(task_item_from_tbl);
16893 if (hStmt != NULL) {
16894 rc = sqlite3_finalize(hStmt);
16896 if (rc != SQLITE_OK) {
16897 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16898 err = EMAIL_ERROR_DB_FAILURE;
16902 EM_DEBUG_FUNC_END("err [%d]", err);
16906 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)
16908 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);
16909 int err = EMAIL_ERROR_NONE;
16911 if (message_id == NULL) {
16912 EM_DEBUG_EXCEPTION("Invalid parameter");
16913 err = EMAIL_ERROR_INVALID_PARAM;
16919 int temp_mail_id = 0;
16920 int where_pararaph_length = 0;
16921 char *where_pararaph = NULL;
16922 char sql_query_string[QUERY_SIZE] = {0, };
16923 char **result = NULL;
16924 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16926 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id from mail_tbl ");
16928 where_pararaph_length = EM_SAFE_STRLEN(message_id) + 100;
16929 where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
16930 if (where_pararaph == NULL) {
16931 EM_DEBUG_EXCEPTION("em_mallocfailed");
16932 err = EMAIL_ERROR_OUT_OF_MEMORY;
16936 if (account_id != ALL_ACCOUNT)
16937 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);
16939 sqlite3_snprintf(where_pararaph_length, where_pararaph, "WHERE mailbox_type = %d AND message_id like '%q'", input_mailbox_type, message_id);
16941 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
16942 EM_SAFE_STRNCAT(sql_query_string, where_pararaph , QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
16944 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
16946 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16947 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16948 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16951 EM_DEBUG_LOG("Count of mails [%d]", count);
16954 _get_table_field_data_int(result, &temp_mail_id, 1);
16955 EM_DEBUG_LOG("Searched mail_id [%d]", temp_mail_id);
16957 memset(sql_query_string, 0x00, QUERY_SIZE);
16958 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string, "UPDATE mail_tbl set server_mail_id = '%q'", server_uid);
16960 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE) {
16961 EM_SAFE_STRNCAT(sql_query_string, where_pararaph,
16962 QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
16965 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16966 if (err != EMAIL_ERROR_NONE) {
16967 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16972 err = EMAIL_ERROR_MAIL_NOT_FOUND;
16977 sqlite3_free_table(result);
16981 EM_SAFE_FREE(where_pararaph);
16983 if (mail_id != NULL)
16984 *mail_id = temp_mail_id;
16986 EM_DEBUG_FUNC_END("err : [%d]", err);
16989 /* Tasks --------------------------------------------------------------------------*/
16991 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
16992 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)
16994 EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
16996 if (!local_activity || !activity_id) {
16997 EM_DEBUG_EXCEPTION("local_activity[%p], activity_id[%p]", local_activity, activity_id);
16998 if (err_code != NULL)
16999 *err_code = EMAIL_ERROR_INVALID_PARAM;
17005 int error = EMAIL_ERROR_NONE;
17008 char sql_query_string[QUERY_SIZE] = {0, };
17009 DB_STMT hStmt = NULL;
17011 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17012 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17014 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17015 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17016 "INSERT INTO mail_auto_download_activity_tbl VALUES "
17018 "? " /* Activity ID */
17020 ",?" /* Account ID */
17021 ",?" /* Local Mail ID */
17022 ",?" /* Server mail ID */
17023 ",?" /* Mailbox ID*/
17024 ",?" /* Multi USER NAME */
17027 char *sql = "SELECT max(rowid) FROM mail_auto_download_activity_tbl;";
17028 char **result = NULL;
17031 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
17033 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17034 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
17036 if (NULL == result[1]) rc = 1;
17037 else rc = atoi(result[1])+1;
17038 sqlite3_free_table(result);
17041 *activity_id = local_activity->activity_id = rc;
17043 EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [%d], MAIL ID [%d], SERVER MAIL ID [%lu]",
17044 local_activity->activity_id, local_activity->mail_id, local_activity->server_mail_id);
17046 if (local_activity->mailbox_id)
17047 EM_DEBUG_LOG(" MAILBOX ID [%d]", local_activity->mailbox_id);
17050 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17051 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17052 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17055 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
17056 _bind_stmt_field_data_int(hStmt, i++, local_activity->status);
17057 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
17058 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
17059 _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
17060 _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
17061 _bind_stmt_field_data_string(hStmt, i++, (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
17064 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17066 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
17067 ("sqlite3_step fail:%d", rc));
17068 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17069 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
17074 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17075 if (hStmt != NULL) {
17076 rc = sqlite3_finalize(hStmt);
17078 if (rc != SQLITE_OK) {
17079 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17080 error = EMAIL_ERROR_DB_FAILURE;
17084 if (err_code != NULL)
17087 EM_DEBUG_FUNC_END("ret [%d]", ret);
17092 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)
17094 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);
17096 if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
17097 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);
17099 if (err_code != NULL)
17100 *err_code = EMAIL_ERROR_INVALID_PARAM;
17106 int error = EMAIL_ERROR_NONE;
17107 char sql_query_string[QUERY_SIZE] = {0, };
17108 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17110 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17111 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17113 if (activity_id == 0)
17114 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);
17116 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);
17118 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17119 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17120 if (error != EMAIL_ERROR_NONE) {
17121 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17125 /* validate activity existence */
17126 rc = sqlite3_changes(local_db_handle);
17128 EM_DEBUG_EXCEPTION("No matching activity found");
17129 error = EMAIL_ERROR_DATA_NOT_FOUND;
17137 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17139 if (err_code != NULL)
17142 EM_DEBUG_FUNC_END("ret [%d]", ret);
17146 INTERNAL_FUNC int emstorage_delete_all_auto_download_activity(char *multi_user_name, int account_id, int transaction, int *err_code)
17148 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17150 if (account_id < FIRST_ACCOUNT_ID) {
17151 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17152 if (err_code != NULL)
17153 *err_code = EMAIL_ERROR_INVALID_PARAM;
17159 int error = EMAIL_ERROR_NONE;
17160 char sql_query_string[QUERY_SIZE] = {0, };
17162 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17164 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17165 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17166 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d", account_id);
17168 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17169 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17170 if (error != EMAIL_ERROR_NONE) {
17171 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17175 rc = sqlite3_changes(local_db_handle);
17177 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17178 error = EMAIL_ERROR_DATA_NOT_FOUND;
17186 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17188 if (err_code != NULL)
17191 EM_DEBUG_FUNC_END("ret [%d]", ret);
17196 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)
17198 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17200 if (account_id < FIRST_ACCOUNT_ID || mailbox_id < 0) {
17201 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17202 if (err_code != NULL)
17203 *err_code = EMAIL_ERROR_INVALID_PARAM;
17209 int error = EMAIL_ERROR_NONE;
17210 char sql_query_string[QUERY_SIZE] = {0, };
17212 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17214 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17215 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17216 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);
17218 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17219 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17220 if (error != EMAIL_ERROR_NONE) {
17221 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17225 rc = sqlite3_changes(local_db_handle);
17227 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17228 error = EMAIL_ERROR_DATA_NOT_FOUND;
17236 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17238 if (err_code != NULL)
17241 EM_DEBUG_FUNC_END("ret [%d]", ret);
17246 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)
17248 EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
17250 if (account_id < FIRST_ACCOUNT_ID || !event_start || input_mailbox_id <= 0 || !count) {
17251 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);
17253 if (err_code != NULL)
17254 *err_code = EMAIL_ERROR_INVALID_PARAM;
17261 int error = EMAIL_ERROR_NONE;
17263 DB_STMT hStmt = NULL;
17264 email_event_auto_download *event_list = NULL;
17265 char sql_query_string[QUERY_SIZE] = {0, };
17267 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17269 EMSTORAGE_START_READ_TRANSACTION(transaction);
17271 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17272 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);
17275 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17276 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17277 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17279 *count = atoi(result[1]);
17280 sqlite3_free_table(result);
17282 EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
17285 EM_DEBUG_LOG("No matched activity found in mail_auto_download_activity_tbl");
17286 error = EMAIL_ERROR_MAIL_NOT_FOUND;
17290 EM_DEBUG_LOG("Activity Count = %d", *count);
17292 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17293 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);
17295 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17298 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17300 EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
17301 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17302 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17305 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17306 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17307 ("sqlite3_step fail:%d", rc));
17309 if (!(event_list = (email_event_auto_download *)em_malloc(sizeof(email_event_auto_download)*(*count)))) {
17310 EM_DEBUG_EXCEPTION("Malloc failed");
17312 error = EMAIL_ERROR_OUT_OF_MEMORY;
17316 for (i = 0; i < (*count); i++) {
17317 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17318 _get_stmt_field_data_int(hStmt, &(event_list[i].status), STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17319 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17320 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17321 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17322 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17324 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17325 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17326 ("sqlite3_step fail:%d", rc));
17334 *event_start = event_list;
17336 EM_SAFE_FREE(event_list);
17337 *event_start = NULL;
17341 if (hStmt != NULL) {
17342 rc = sqlite3_finalize(hStmt);
17344 if (rc != SQLITE_OK) {
17345 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17346 error = EMAIL_ERROR_DB_FAILURE;
17350 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17351 if (err_code != NULL)
17354 EM_DEBUG_FUNC_END("ret [%d]", ret);
17359 INTERNAL_FUNC int emstorage_get_auto_download_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
17361 EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
17363 if (!activity_count || !err_code) {
17364 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
17365 if (err_code != NULL)
17366 *err_code = EMAIL_ERROR_INVALID_PARAM;
17372 int error = EMAIL_ERROR_NONE;
17373 DB_STMT hStmt = NULL;
17374 char sql_query_string[QUERY_SIZE] = {0, };
17376 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17378 EMSTORAGE_START_READ_TRANSACTION(transaction);
17379 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17381 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl;");
17383 EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
17386 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17387 EM_DEBUG_LOG_DEV("before sqlite3_prepare hStmt = %p", hStmt);
17388 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17389 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17392 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17393 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17394 ("sqlite3_step fail:%d", rc));
17396 _get_stmt_field_data_int(hStmt, activity_count, 0);
17398 EM_DEBUG_LOG("counts of activities in activity table [%d]", *activity_count);
17404 if (hStmt != NULL) {
17405 rc = sqlite3_finalize(hStmt);
17407 if (rc != SQLITE_OK) {
17408 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17409 error = EMAIL_ERROR_DB_FAILURE;
17413 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17414 if (err_code != NULL)
17417 EM_DEBUG_FUNC_END("ret [%d]", ret);
17422 INTERNAL_FUNC int emstorage_get_auto_download_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
17424 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
17426 if (!account_list || !count) {
17427 EM_DEBUG_EXCEPTION("account_list[%p], count[%p]", account_list, count);
17428 if (err_code != NULL)
17429 *err_code = EMAIL_ERROR_INVALID_PARAM;
17434 int error = EMAIL_ERROR_NONE;
17435 char *sql = "SELECT count(distinct account_id) FROM mail_auto_download_activity_tbl";
17437 int i = 0, rc = -1;
17438 int *result_account_list = NULL;
17439 DB_STMT hStmt = NULL;
17440 char sql_query_string[QUERY_SIZE] = {0, };
17441 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17443 EMSTORAGE_START_READ_TRANSACTION(transaction);
17445 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
17446 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17447 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
17449 *count = atoi(result[1]);
17450 sqlite3_free_table(result);
17453 EM_DEBUG_EXCEPTION("no account found...");
17454 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
17459 EM_DEBUG_LOG("Account count [%d]", *count);
17461 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17463 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_auto_download_activity_tbl");
17465 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17468 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17470 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
17471 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17472 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17475 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17476 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17477 ("sqlite3_step fail:%d", rc));
17479 if (NULL == (result_account_list = (int *)em_malloc(sizeof(int)*(*count)))) {
17480 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
17481 error = EMAIL_ERROR_OUT_OF_MEMORY;
17485 for (i = 0; i < (*count); i++) {
17486 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
17488 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17489 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17490 ("sqlite3_step fail:%d", rc));
17491 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
17499 *account_list = result_account_list;
17501 EM_SAFE_FREE(result_account_list);
17503 if (hStmt != NULL) {
17504 rc = sqlite3_finalize(hStmt);
17506 if (rc != SQLITE_OK) {
17507 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17508 error = EMAIL_ERROR_DB_FAILURE;
17512 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17513 if (err_code != NULL)
17515 EM_DEBUG_FUNC_END("ret [%d]", ret);
17520 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)
17522 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
17524 if (account_id < FIRST_ACCOUNT_ID || !mailbox_list || !count) {
17525 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p]", account_id, mailbox_list, count);
17526 if (err_code != NULL)
17527 *err_code = EMAIL_ERROR_INVALID_PARAM;
17532 int error = EMAIL_ERROR_NONE;
17534 int i = 0, rc = -1;
17535 int *mbox_list = NULL;
17536 DB_STMT hStmt = NULL;
17537 char sql_query_string[QUERY_SIZE] = {0, };
17539 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17541 EMSTORAGE_START_READ_TRANSACTION(transaction);
17543 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17544 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);
17547 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17548 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17549 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17551 *count = atoi(result[1]);
17552 sqlite3_free_table(result);
17555 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
17556 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
17560 EM_DEBUG_LOG("Mailbox count = %d", *count);
17562 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17564 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);
17566 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
17569 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17572 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17573 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17576 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17577 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17578 ("sqlite3_step fail:%d", rc));
17580 mbox_list = (int *)em_malloc(sizeof(int)*(*count)); /* prevent */
17581 if (mbox_list == NULL) {
17582 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
17583 error = EMAIL_ERROR_OUT_OF_MEMORY;
17587 for (i = 0; i < (*count); i++) {
17588 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
17589 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17591 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17592 ("sqlite3_step fail:%d", rc));
17593 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
17601 *mailbox_list = mbox_list;
17603 EM_SAFE_FREE(mbox_list);
17605 if (hStmt != NULL) {
17606 rc = sqlite3_finalize(hStmt);
17608 if (rc != SQLITE_OK) {
17609 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17610 error = EMAIL_ERROR_DB_FAILURE;
17614 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17615 if (err_code != NULL)
17617 EM_DEBUG_FUNC_END("ret [%d]", ret);
17622 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)
17624 EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
17626 if (account_id < FIRST_ACCOUNT_ID || !activity_count || !err_code) {
17627 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
17628 if (err_code != NULL)
17629 *err_code = EMAIL_ERROR_INVALID_PARAM;
17635 int error = EMAIL_ERROR_NONE;
17636 char sql_query_string[QUERY_SIZE] = {0, };
17637 DB_STMT hStmt = NULL;
17639 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17641 EMSTORAGE_START_READ_TRANSACTION(transaction);
17642 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17643 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);
17645 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
17647 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17648 EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
17649 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17650 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17652 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17653 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17654 ("sqlite3_step fail:%d", rc));
17656 _get_stmt_field_data_int(hStmt, activity_count, 0);
17658 EM_DEBUG_LOG("count of activities in activity table [%d]", *activity_count);
17664 if (hStmt != NULL) {
17665 rc = sqlite3_finalize(hStmt);
17667 if (rc != SQLITE_OK) {
17668 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17669 error = EMAIL_ERROR_DB_FAILURE;
17673 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17675 if (err_code != NULL)
17678 EM_DEBUG_FUNC_END("ret [%d]", ret);
17683 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)
17685 EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mailbox_id[%d]", old_server_uid, new_server_uid, mailbox_id);
17687 int rc = -1, ret = false;
17688 int error = EMAIL_ERROR_NONE;
17689 char sql_query_string[QUERY_SIZE] = {0, };
17690 int transaction = true;
17692 if (!old_server_uid || !new_server_uid || (!mailbox_name && mailbox_id < 0)) {
17693 EM_DEBUG_EXCEPTION("Invalid parameters");
17694 error = EMAIL_ERROR_INVALID_PARAM;
17698 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17700 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17701 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17703 if (mailbox_id > 0)
17704 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17705 "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);
17706 else if (mailbox_name)
17707 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17708 "UPDATE mail_auto_download_activity_tbl SET server_mail_id = %s WHERE server_mail_id = %s ", new_server_uid, old_server_uid);
17710 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17711 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17712 if (error != EMAIL_ERROR_NONE) {
17713 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17717 rc = sqlite3_changes(local_db_handle);
17719 EM_DEBUG_LOG("No matching found in mail_auto_download_activity_tbl");
17725 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17727 if (err_code != NULL)
17730 EM_DEBUG_FUNC_END("ret [%d]", ret);
17736 #ifdef __FEATURE_UPDATE_DB_TABLE_SCHEMA__
17741 } email_column_info_t;
17743 static int get_column_information_from_table_callback(void *arg1, int argc, char **argv, char **input_column_name)
17745 EM_DEBUG_FUNC_BEGIN("arg1[%p] argc[%d] argv[%p] column_name[%p]", arg1, argc, argv, input_column_name);
17749 char *column_name = NULL;
17750 char *column_type = NULL;
17751 GList *new_list = *((GList**)arg1);
17752 email_column_info_t *column_info_item = NULL;
17754 for (i = 0; i < argc; ++i) {
17755 /* EM_DEBUG_LOG("%s = %s", input_column_name[i], argv[i]); */
17756 if (EM_SAFE_STRCMP(input_column_name[i], "name") == 0) {
17758 EM_SAFE_FREE(column_name);
17760 column_name = EM_SAFE_STRDUP(argv[i]);
17762 } else if (EM_SAFE_STRCMP(input_column_name[i], "type") == 0) {
17764 EM_SAFE_FREE(column_type);
17766 column_type = EM_SAFE_STRDUP(argv[i]);
17771 EM_DEBUG_LOG("column_name[%s] column_type[%s]", column_name, column_type);
17772 column_info_item = em_malloc(sizeof(email_column_info_t));
17773 if (column_info_item == NULL) {
17774 EM_DEBUG_EXCEPTION("em_mallocfailed");
17778 column_info_item->column_name = EM_SAFE_STRDUP(column_name);
17779 column_info_item->column_type = EM_SAFE_STRDUP(column_type);
17780 new_list = g_list_append(new_list, (gpointer)column_info_item);
17781 *((GList**)arg1) = new_list;
17786 EM_SAFE_FREE(column_name);
17787 EM_SAFE_FREE(column_type);
17789 EM_DEBUG_FUNC_END();
17793 static int emstorage_get_column_information_from_table(char *multi_user_name, const char *input_table_name, GList **output_column_info)
17795 EM_DEBUG_FUNC_BEGIN("input_table_name[%p] output_column_info[%p]", input_table_name, output_column_info);
17796 int err = EMAIL_ERROR_NONE;
17797 int result_from_sqlite = 0;
17798 char *error_message_from_sqlite = NULL;
17799 char sql_query_string[QUERY_SIZE] = {0, };
17800 GList *new_list = NULL;
17801 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17803 SNPRINTF(sql_query_string, QUERY_SIZE, "pragma table_info(%s);", input_table_name);
17805 result_from_sqlite = sqlite3_exec(local_db_handle, sql_query_string, get_column_information_from_table_callback, &new_list, &error_message_from_sqlite);
17807 if (result_from_sqlite != SQLITE_OK)
17808 EM_DEBUG_EXCEPTION("sqlite3_exec returns [%d]", result_from_sqlite);
17810 EM_DEBUG_LOG("new_list[%p] output_column_info[%p]", new_list, output_column_info);
17812 if (new_list && output_column_info) {
17813 EM_DEBUG_LOG("g_list_length[%d]", g_list_length(new_list));
17814 *output_column_info = new_list;
17817 EM_DEBUG_FUNC_END("err [%d]", err);
17821 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)
17823 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);
17824 int error = EMAIL_ERROR_NONE;
17826 sqlite3 *local_db_handle = NULL;
17827 char sql_query_string[QUERY_SIZE] = {0, };
17829 local_db_handle = emstorage_get_db_connection(multi_user_name);
17831 if (input_full_query == NULL || input_source_table_name == NULL || input_new_table_name == NULL) {
17832 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17833 error = EMAIL_ERROR_INVALID_PARAM;
17837 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17838 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17840 EM_DEBUG_LOG("[%s] will be replaced by [%s]", input_source_table_name, input_new_table_name);
17842 EM_SAFE_STRNCPY(sql_query_string, input_full_query[input_query_index], sizeof(sql_query_string)-1); /*prevent 21984*/
17843 reg_replace(sql_query_string, input_source_table_name, input_new_table_name);
17845 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17848 if (error == EMAIL_ERROR_NONE) {
17849 EMSTORAGE_PROTECTED_FUNC_CALL(
17850 sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17852 EMSTORAGE_PROTECTED_FUNC_CALL(
17853 sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17856 EM_DEBUG_FUNC_END("error [%d]", error);
17860 static int emstorage_add_column(char *multi_user_name, char *input_table_name, email_column_info_t *input_new_column)
17862 EM_DEBUG_FUNC_BEGIN("input_table_name[%p] input_new_column[%p]", input_table_name, input_new_column);
17863 int error = EMAIL_ERROR_NONE;
17865 sqlite3 *local_db_handle = NULL;
17866 char sql_query_string[QUERY_SIZE] = {0, };
17868 local_db_handle = emstorage_get_db_connection(multi_user_name);
17870 if (input_table_name == NULL || input_new_column == NULL) {
17871 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17872 error = EMAIL_ERROR_INVALID_PARAM;
17876 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17877 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17878 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);
17879 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17882 if (error == EMAIL_ERROR_NONE) {
17883 EMSTORAGE_PROTECTED_FUNC_CALL(
17884 sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17886 EMSTORAGE_PROTECTED_FUNC_CALL(
17887 sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17890 EM_DEBUG_FUNC_END("error [%d]", error);
17894 static int emstorage_drop_table(char *multi_user_name, char *input_table_name)
17896 EM_DEBUG_FUNC_BEGIN("input_table_name[%p]", input_table_name);
17897 int error = EMAIL_ERROR_NONE;
17899 sqlite3 *local_db_handle = NULL;
17900 char sql_query_string[QUERY_SIZE] = {0, };
17902 local_db_handle = emstorage_get_db_connection(multi_user_name);
17904 if (input_table_name == NULL) {
17905 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17906 error = EMAIL_ERROR_INVALID_PARAM;
17910 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17911 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17912 SNPRINTF(sql_query_string, QUERY_SIZE, "DROP TABLE %s;", input_table_name);
17913 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17916 if (error == EMAIL_ERROR_NONE) {
17917 EMSTORAGE_PROTECTED_FUNC_CALL(
17918 sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17920 EMSTORAGE_PROTECTED_FUNC_CALL(
17921 sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17924 EM_DEBUG_FUNC_END("error [%d]", error);
17928 gint glist_compare_column_name(gconstpointer old_column_info, gconstpointer new_column_info)
17930 EM_DEBUG_FUNC_BEGIN("old_column_info[%p] new_column_info[%p]", old_column_info, new_column_info);
17931 email_column_info_t *left_one = (email_column_info_t*)old_column_info;
17932 email_column_info_t *right_one = (email_column_info_t*)new_column_info;
17934 if (old_column_info == NULL || new_column_info == NULL)
17937 return EM_SAFE_STRCMP((char*)left_one->column_name, (char*)right_one->column_name);
17940 INTERNAL_FUNC int emstorage_update_db_table_schema(char *multi_user_name)
17942 EM_DEBUG_FUNC_BEGIN();
17945 int error = EMAIL_ERROR_NONE;
17947 email_column_info_t *new_column_info = NULL;
17948 email_column_info_t *p_column_info = NULL;
17949 char **create_table_query = NULL;
17950 GList *found_data = NULL;
17951 GList *column_list_of_old_table = NULL;
17952 GList *column_list_of_new_table = NULL;
17953 char table_names[CREATE_TABLE_MAX][2][50] = { { "mail_account_tbl", "mail_account_tbl_new" },
17954 { "mail_box_tbl", "mail_box_tbl_new" },
17955 { "mail_read_mail_uid_tbl", "mail_read_mail_uid_tbl_new" },
17956 { "mail_rule_tbl", "mail_rule_tbl_new" },
17957 { "mail_tbl", "mail_tbl_new" },
17958 { "mail_attachment_tbl", "mail_attachment_tbl_new" },
17959 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
17960 { "mail_partial_body_activity_tbl", "mail_partial_body_activity_tbl_new" },
17964 { "mail_meeting_tbl", "mail_meeting_tbl_new" },
17965 #ifdef __FEATURE_LOCAL_ACTIVITY__
17966 { "mail_local_activity_tbl", "mail_local_activity_tbl_new" },
17970 { "mail_certificate_tbl", "mail_certificate_tbl_new" },
17971 { "mail_task_tbl", "mail_task_tbl_new" },
17972 #ifdef __FEATURE_BODY_SEARCH__
17973 { "mail_text_tbl", "mail_text_tbl_new" },
17978 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
17979 { "mail_auto_download_activity_tbl", "mail_auto_download_activity_tbl_new" }
17986 error = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
17988 if (error != EMAIL_ERROR_NONE) {
17989 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
17993 if (query_len < CREATE_TABLE_MAX) {
17994 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
17995 error = EMAIL_ERROR_SYSTEM_FAILURE;
17999 for (i = CREATE_TABLE_MAIL_ACCOUNT_TBL; i < CREATE_TABLE_MAX; i++) {
18000 EM_DEBUG_LOG("table [%s] new_table [%s]", table_names[i][0], table_names[i][1]);
18001 if (EM_SAFE_STRLEN(table_names[i][0]) && EM_SAFE_STRLEN(table_names[i][1])) {
18002 /* Check existing of _new table */
18003 emstorage_drop_table(multi_user_name, table_names[i][1]);
18004 error = emstorage_create_renamed_table(multi_user_name, create_table_query, i, table_names[i][0], table_names[i][1]);
18005 if (error != EMAIL_ERROR_NONE) {
18006 EM_DEBUG_EXCEPTION("emstorage_create_renamed_table failed [%d]", error);
18010 emstorage_get_column_information_from_table(multi_user_name, table_names[i][0], &column_list_of_old_table);
18011 emstorage_get_column_information_from_table(multi_user_name, table_names[i][1], &column_list_of_new_table);
18013 /* Compare fields and add new field */
18014 for (j = 0; j < g_list_length(column_list_of_new_table); j++) {
18015 new_column_info = (email_column_info_t*)g_list_nth_data(column_list_of_new_table, j);
18016 found_data = g_list_find_custom(column_list_of_old_table, (gconstpointer)new_column_info, glist_compare_column_name);
18017 if (found_data == NULL) {
18019 emstorage_add_column(multi_user_name, table_names[i][0], new_column_info);
18023 emstorage_drop_table(multi_user_name, table_names[i][1]);
18025 EM_DEBUG_LOG("Skipped");
18030 if (create_table_query) {
18032 for (i = 0; i < query_len; i++) {
18033 if (create_table_query[i])
18034 EM_SAFE_FREE(create_table_query[i]);
18036 EM_SAFE_FREE(create_table_query);
18039 found_data = g_list_first(column_list_of_old_table);
18040 while (found_data != NULL) {
18041 p_column_info = (email_column_info_t *)found_data->data;
18042 EM_SAFE_FREE(p_column_info->column_name);
18043 EM_SAFE_FREE(p_column_info->column_type);
18044 EM_SAFE_FREE(p_column_info);
18046 found_data = g_list_next(found_data);
18048 g_list_free(column_list_of_old_table);
18050 found_data = g_list_first(column_list_of_new_table);
18051 while (found_data != NULL) {
18052 p_column_info = (email_column_info_t *)found_data->data;
18053 EM_SAFE_FREE(p_column_info->column_name);
18054 EM_SAFE_FREE(p_column_info->column_type);
18055 EM_SAFE_FREE(p_column_info);
18057 found_data = g_list_next(found_data);
18059 g_list_free(column_list_of_new_table);
18061 EM_DEBUG_FUNC_END("error [%d]", error);
18064 #endif /* __FEATURE_UPDATE_DB_TABLE_SCHEMA__ */