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 [%d] 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 [%d], 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__ */
372 static int emstorage_exec_query_by_prepare_v2(sqlite3 *local_db_handle, char *query_string)
374 EM_DEBUG_FUNC_BEGIN("local_db_handle[%p] query_string[%p]", local_db_handle, query_string);
375 int error = EMAIL_ERROR_NONE;
377 DB_STMT db_statement = NULL;
379 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, query_string, EM_SAFE_STRLEN(query_string), &db_statement, NULL), rc);
380 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
381 ("sqlite3_prepare failed [%d] [%s]", rc, query_string));
383 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(db_statement), rc);
384 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
385 ("sqlite3_step failed [%d] [%s]", rc, query_string));
389 if (db_statement != NULL) {
390 rc = sqlite3_finalize(db_statement);
391 if (rc != SQLITE_OK) {
392 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
393 error = EMAIL_ERROR_DB_FAILURE;
400 INTERNAL_FUNC int emstorage_shm_file_init(const char *shm_file_name)
402 EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
403 char errno_buf[ERRNO_BUF_SIZE] = {0};
405 if (!shm_file_name) {
406 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
407 return EMAIL_ERROR_INVALID_PARAM;
410 int fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); /* note: permission is not working */
412 EM_DEBUG_EXCEPTION("shm_open errno [%d] [%s]", errno, EM_STRERROR(errno_buf));
413 return EMAIL_ERROR_SYSTEM_FAILURE;
417 EM_DEBUG_LOG("** Create SHM FILE **");
418 if (ftruncate(fd, sizeof(mmapped_t)) != 0) {
419 EM_DEBUG_EXCEPTION("ftruncate errno [%d]", errno);
420 return EMAIL_ERROR_SYSTEM_FAILURE;
423 mmapped_t *m = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
424 if (m == MAP_FAILED) {
425 EM_DEBUG_EXCEPTION("mmap errno [%d]", errno);
427 return EMAIL_ERROR_SYSTEM_FAILURE;
432 pthread_mutexattr_t mattr;
433 pthread_mutexattr_init(&mattr);
434 pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
435 pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
436 pthread_mutex_init(&(m->mutex), &mattr);
437 pthread_mutexattr_destroy(&mattr);
439 pthread_mutex_destroy(&(m->mutex));
440 munmap(m, sizeof(mmapped_t));
444 return EMAIL_ERROR_NONE;
447 int emstorage_shm_file_destroy(const char *shm_file_name)
449 EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
450 char errno_buf[ERRNO_BUF_SIZE] = {0};
452 if (!shm_file_name) {
453 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
454 return EMAIL_ERROR_INVALID_PARAM;
457 if (shm_unlink(shm_file_name) != 0)
458 EM_DEBUG_EXCEPTION("shm_unlink failed: %s", EM_STRERROR(errno_buf));
460 return EMAIL_ERROR_NONE;
463 int _initialize_shm_mutex(const char *shm_file_name, int *param_shm_fd, mmapped_t **param_mapped)
465 EM_DEBUG_FUNC_BEGIN("shm_file_name [%p] param_shm_fd [%p], param_mapped [%p]", shm_file_name, param_shm_fd, param_mapped);
466 char errno_buf[ERRNO_BUF_SIZE] = {0};
468 if (!shm_file_name || !param_shm_fd || !param_mapped) {
469 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
470 return EMAIL_ERROR_INVALID_PARAM;
473 if (!(*param_mapped)) {
474 EM_DEBUG_LOG("** mapping begin **");
475 if (!(*param_shm_fd)) { /* open shm_file_name at first. Otherwise, the num of files in /proc/pid/fd will be increasing */
476 *param_shm_fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
477 if ((*param_shm_fd) == -1) {
478 EM_DEBUG_EXCEPTION("FAIL: shm_open(): %s", EM_STRERROR(errno_buf));
479 return EMAIL_ERROR_SYSTEM_FAILURE;
483 fchmod((*param_shm_fd), 0666);
484 EM_DEBUG_LOG("** Create SHM FILE **");
485 if (ftruncate((*param_shm_fd), sizeof(mmapped_t)) != 0) {
486 EM_DEBUG_EXCEPTION("ftruncate errno [%d]", errno);
487 return EMAIL_ERROR_SYSTEM_FAILURE;
490 mmapped_t *tmp = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ|PROT_WRITE, MAP_SHARED, (*param_shm_fd), 0);
491 if (tmp == MAP_FAILED) {
492 EM_DEBUG_EXCEPTION("mmap failed: %s", EM_STRERROR(errno_buf));
493 return EMAIL_ERROR_SYSTEM_FAILURE;
498 pthread_mutexattr_t mattr;
499 pthread_mutexattr_init(&mattr);
500 pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
501 pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
502 pthread_mutex_init(&(tmp->mutex), &mattr);
503 pthread_mutexattr_destroy(&mattr);
509 return EMAIL_ERROR_NONE;
512 int _timedlock_shm_mutex(mmapped_t *param_mapped, int sec)
514 EM_DEBUG_FUNC_BEGIN("param_mapped [%p], sec [%d]", param_mapped, sec);
517 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
518 return EMAIL_ERROR_INVALID_PARAM;
521 struct timespec abs_time;
522 clock_gettime(CLOCK_REALTIME, &abs_time);
523 abs_time.tv_sec += sec;
524 char errno_buf[ERRNO_BUF_SIZE] = {0};
526 int err = pthread_mutex_timedlock(&(param_mapped->mutex), &abs_time);
528 if (err == EOWNERDEAD) {
529 err = pthread_mutex_consistent(&(param_mapped->mutex));
530 EM_DEBUG_EXCEPTION("Previous owner is dead with lock. Fix mutex : %s", EM_STRERROR(errno_buf));
531 } else if (err != 0) {
532 EM_DEBUG_EXCEPTION("ERROR : %s", EM_STRERROR(errno_buf));
537 return EMAIL_ERROR_NONE;
540 void _unlockshm_mutex(mmapped_t *param_mapped)
542 EM_DEBUG_FUNC_BEGIN();
543 pthread_mutex_unlock(&(param_mapped->mutex));
546 /* ------------------------------------------------------------------------------ */
549 static int _open_counter = 0;
551 static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name);
552 static int _read_password_from_secure_storage(char *file_name, char **password);
554 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
555 static int _get_cert_password_file_name(int index, char *cert_password_file_name);
559 const char *object_name;
560 unsigned int data_flag;
563 static const email_db_object_t _g_db_tables[] = {
564 { "mail_read_mail_uid_tbl", 1},
566 { "mail_attachment_tbl", 1},
570 static const email_db_object_t _g_db_indexes[] = {
571 { "mail_read_mail_uid_idx1", 1},
573 { "mail_attachment_idx1", 1},
578 CREATE_TABLE_MAIL_ACCOUNT_TBL,
579 CREATE_TABLE_MAIL_BOX_TBL,
580 CREATE_TABLE_MAIL_READ_MAIL_UID_TBL,
581 CREATE_TABLE_MAIL_RULE_TBL,
582 CREATE_TABLE_MAIL_TBL,
583 CREATE_TABLE_MAIL_ATTACHMENT_TBL,
584 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
585 CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL,
587 CREATE_TABLE_DUMMY_INDEX1,
589 CREATE_TABLE_MAIL_MEETING_TBL,
590 #ifdef __FEATURE_LOCAL_ACTIVITY__
591 CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL,
593 CREATE_TABLE_DUMMY_INDEX2,
595 CREATE_TABLE_MAIL_CERTIFICATE_TBL,
596 CREATE_TABLE_MAIL_TASK_TBL,
597 #ifdef __FEATURE_BODY_SEARCH__
598 CREATE_TABLE_MAIL_TEXT_TBL,
600 CREATE_TABLE_DUMMY_INDEX3,
603 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
604 CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
606 CREATE_TABLE_DUMMY_INDEX4,
610 CREATE_TABLE_MAIL_ACCOUNT_IDX,
611 CREATE_TABLE_MAIL_BOX_IDX,
612 CREATE_TABLE_MAIL_READ_MAIL_UID_IDX,
613 CREATE_TABLE_MAIL_IDX,
614 CREATE_TABLE_MAIL_ATTACHMENT_IDX,
615 CREATE_TABLE_MAIL_MEETING_IDX,
616 CREATE_TABLE_MAIL_TASK_IDX,
617 CREATE_TABLE_MAIL_DATETIME_IDX,
618 CREATE_TABLE_MAIL_THREAD_IDX,
623 DATA1_IDX_IN_MAIL_ACTIVITY_TBL = 0,
624 TRANSTYPE_IDX_IN_MAIL_ACTIVITY_TBL,
625 FLAG_IDX_IN_MAIL_ACTIVITY_TBL,
629 CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL = 0,
630 ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
631 ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
632 ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
633 EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
634 EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
635 EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
636 ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL,
637 EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL,
638 SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL,
639 FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL,
648 ACCOUNT_ID_IDX_IN_MAIL_BOX_TBL = 0,
649 LOCAL_YN_IDX_IN_MAIL_BOX_TBL,
650 MAILBOX_NAME_IDX_IN_MAIL_BOX_TBL,
651 MAILBOX_TYPE_IDX_IN_MAIL_BOX_TBL,
652 ALIAS_IDX_IN_MAIL_BOX_TBL,
653 SYNC_WITH_SERVER_YN_IDX_IN_MAIL_BOX_TBL,
654 MODIFIABLE_YN_IDX_IN_MAIL_BOX_TBL,
655 TOTAL_MAIL_COUNT_ON_SERVER_IDX_IN_MAIL_BOX_TBL,
656 ARCHIVE_IDX_IN_MAIL_BOX_TBL,
657 MAIL_SLOT_SIZE_IDX_IN_MAIL_BOX_TBL,
661 ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL = 0,
662 LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
663 MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL,
664 LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
665 SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
666 RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL,
667 SYNC_STATUS_IDX_IN_MAIL_READ_MAIL_UID_TBL,
668 FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL,
669 FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL,
670 IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, /* unused */
673 #ifdef __FEATURE_BODY_SEARCH__
675 MAIL_ID_IDX_IN_MAIL_TEXT_TBL = 0,
676 ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL,
677 MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL,
678 BODY_TEXT_IDX_IN_MAIL_TEXT_TBL,
679 FIELD_COUNT_OF_MAIL_TEXT_TBL,
684 ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL = 0,
685 RULE_ID_IDX_IN_MAIL_RULE_TBL,
686 FILTER_NAME_IDX_IN_MAIL_RULE_TBL,
687 TYPE_IDX_IN_MAIL_RULE_TBL,
688 VALUE_IDX_IN_MAIL_RULE_TBL,
689 VALUE2_IDX_IN_MAIL_RULE_TBL,
690 ACTION_TYPE_IDX_IN_MAIL_RULE_TBL,
691 TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL,
692 FLAG1_IDX_IN_MAIL_RULE_TBL,
693 FLAG2_IDX_IN_MAIL_RULE_TBL,
697 MAIL_ID_IDX_IN_MAIL_TBL = 0,
698 ACCOUNT_ID_IDX_IN_MAIL_TBL,
699 MAILBOX_ID_IDX_IN_MAIL_TBL,
700 MAILBOX_TYPE_IDX_IN_MAIL_TBL,
701 SUBJECT_IDX_IN_MAIL_TBL,
702 DATETIME_IDX_IN_MAIL_TBL,
703 SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL,
704 SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL,
705 SERVER_MAIL_ID_IDX_IN_MAIL_TBL,
706 MESSAGE_ID_IDX_IN_MAIL_TBL,
707 REFERENCE_ID_IDX_IN_MAIL_TBL,
708 FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL,
709 FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL,
710 FULL_ADDRESS_TO_IDX_IN_MAIL_TBL,
711 FULL_ADDRESS_CC_IDX_IN_MAIL_TBL,
712 FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL,
713 FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL,
714 EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL,
715 EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL,
716 ALIAS_SENDER_IDX_IN_MAIL_TBL,
717 ALIAS_RECIPIENT_IDX_IN_MAIL_TBL,
718 BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL,
719 FILE_PATH_PLAIN_IDX_IN_MAIL_TBL,
720 FILE_PATH_HTML_IDX_IN_MAIL_TBL,
721 FILE_PATH_MIME_ENTITY_IDX_IN_MAIL_TBL,
722 MAIL_SIZE_IDX_IN_MAIL_TBL,
723 FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL,
724 FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL,
725 FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL,
726 FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL,
727 FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL,
728 FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL,
729 FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL,
730 DRM_STATUS_IDX_IN_MAIL_TBL,
731 PRIORITY_IDX_IN_MAIL_TBL,
732 SAVE_STATUS_IDX_IN_MAIL_TBL,
733 LOCK_STATUS_IDX_IN_MAIL_TBL,
734 REPORT_STATUS_IDX_IN_MAIL_TBL,
735 ATTACHMENT_COUNT_IDX_IN_MAIL_TBL,
736 INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL,
737 THREAD_ID_IDX_IN_MAIL_TBL,
738 THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL,
739 PREVIEW_TEXT_IDX_IN_MAIL_TBL,
740 MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL,
741 MESSAGE_CLASS_IDX_IN_MAIL_TBL,
742 DIGEST_TYPE_IDX_IN_MAIL_TBL,
743 SMIME_TYPE_IDX_IN_MAIL_TBL,
744 SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL,
745 REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL,
746 TAG_ID_IDX_IN_MAIL_TBL,
747 REPLIED_TIME_IDX_IN_MAIL_TBL,
748 FORWARDED_TIME_IDX_IN_MAIL_TBL,
749 DEFAULT_CHARSET_IDX_IN_MAIL_TBL,
750 EAS_DATA_LENGTH_IDX_IN_MAIL_TBL,
751 EAS_DATA_IDX_IN_MAIL_TBL,
752 USER_NAME_IDX_IN_MAIL_TBL,
753 FIELD_COUNT_OF_MAIL_TBL, /* End of mail_tbl */
757 ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL = 0,
758 ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL,
759 ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL,
760 CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
761 ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL,
762 MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
763 ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
764 MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
765 ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
766 ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
767 ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL,
768 ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
769 ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
770 #ifdef __ATTACHMENT_OPTI__
771 ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL,
772 SECTION_IDX_IN_MAIL_ATTACHMENT_TBL,
777 IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL = 0,
778 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
779 MAIL_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
780 ACCOUNT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
781 ADDRESS_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
782 ADDRESS_IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL,
784 ADDRESS_IDX_IN_MAIL_CONTACT_SYNC_TBL,
785 CONTACT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
786 STORAGE_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
787 CONTACT_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
788 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
789 DISPLAY_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
790 FLAG1_IDX_IN_MAIL_CONTACT_SYNC_TBL,
794 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
796 ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL = 0,
797 STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
798 ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
799 MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
800 SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
801 MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
802 MULTI_USER_NAME_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
806 /* sowmya.kr 03032010, changes for get list of mails for given addr list */
807 typedef struct _em_mail_id_list {
809 struct _em_mail_id_list *next;
812 static char *g_test_query[] = {
813 /* 1. select mail_account_tbl */
816 " incoming_server_type, "
817 " incoming_server_address, "
818 " user_email_address, "
819 " incoming_server_user_name, "
820 " incoming_server_password, "
822 " incoming_server_port_number, "
823 " incoming_server_secure_connection, "
824 " incoming_server_authentication_method,"
825 " outgoing_server_type, "
826 " outgoing_server_address, "
827 " outgoing_server_port_number, "
828 " outgoing_server_need_authentication, "
829 " outgoing_server_secure_connection, "
830 " outgoing_server_user_name, "
831 " outgoing_server_password, "
836 " keep_mails_on_pop_server_after_download, "
837 " auto_resend_times, "
838 " outgoing_server_size_limit, "
839 " wifi_auto_download, "
841 " incoming_server_requires_apop,"
843 " is_preset_account, "
847 " req_delivery_receipt, "
848 " req_read_receipt, "
852 " display_name_from, "
854 " forward_with_files, "
858 ", add_my_address_to_bcc"
859 ", notification_status "
861 ", display_content_status "
862 ", default_ringtone_status "
863 ", alert_ringtone_path "
872 " FROM mail_account_tbl",
873 /* 2. select mail_box_tbl */
883 " total_mail_count_on_server, "
884 " has_archived_mails, "
888 " FROM mail_box_tbl ",
889 /* 3. select mail_read_mail_uid_tbl */
898 " flags_seen_field, "
900 " FROM mail_read_mail_uid_tbl ",
901 /* 4. select mail_rule_tbl */
910 " target_mailbox_id, "
913 " FROM mail_rule_tbl ",
914 /* 5. select mail_tbl */
923 " server_mail_status, "
924 " server_mailbox_name, "
927 " reference_mail_id, "
928 " full_address_from, "
929 " full_address_reply, "
932 " full_address_bcc, "
933 " full_address_return, "
934 " email_address_sender, "
935 " email_address_recipient, "
938 " body_download_status, "
941 " file_path_mime_entity, "
943 " flags_seen_field ,"
944 " flags_deleted_field ,"
945 " flags_flagged_field ,"
946 " flags_answered_field ,"
947 " flags_recent_field ,"
948 " flags_draft_field ,"
949 " flags_forwarded_field,"
955 " attachment_count, "
956 " inline_content_count, "
958 " thread_item_count, "
960 " meeting_request_status, "
965 /* 6. select mail_attachment_tbl */
974 " attachment_save_status, "
975 " attachment_drm_type, "
976 " attachment_drm_method, "
977 " attachment_inline_content_status, "
978 " attachment_mime_type "
979 " FROM mail_attachment_tbl ",
981 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
991 " FROM mail_partial_body_activity_tbl ",
998 " meeting_response, "
1002 " global_object_id, "
1005 " standard_time_start_date, "
1008 " daylight_time_start_date, "
1010 " FROM mail_meeting_tbl ",
1012 #ifdef __FEATURE_LOCAL_ACTIVITY__
1021 " FROM mail_local_activity_tbl ",
1028 " expiration_year, "
1029 " expiration_month, "
1031 " issue_organization_name, "
1036 " FROM mail_certificate_tbl ",
1042 " task_parameter_length, "
1043 " task_parameter , "
1045 " FROM mail_task_tbl ",
1046 #ifdef __FEATURE_BODY_SEARCH__
1052 " FROM mail_text_tbl ",
1054 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
1062 " multi_user_name, "
1063 " FROM mail_auto_download_activity_tbl ",
1068 int _field_count_of_table[CREATE_TABLE_MAX] = { 0, };
1070 static int _get_table_field_data_char(char **table, char *buf, int index)
1072 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1073 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1077 if (table[index] != NULL) {
1078 *buf = (char)atoi(table[index]);
1082 /* EM_DEBUG_LOG("Empty field. Set as zero"); */
1088 static int _get_table_field_data_int(char **table, int *buf, int index)
1090 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1091 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1095 if (table[index] != NULL) {
1096 *buf = atoi(table[index]);
1100 /* EM_DEBUG_LOG("Empty field. Set as zero"); */
1106 static int _get_table_field_data_time_t(char **table, time_t *buf, int index)
1108 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1109 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1113 if (table[index] != NULL) {
1114 *buf = (time_t)atol(table[index]);
1118 /* EM_DEBUG_LOG("Empty field. Set as zero"); */
1124 static int _get_table_field_data_string(char **table, char **buf, int ucs2, int index)
1128 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1129 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1133 char *pTemp = table[index];
1138 sLen = EM_SAFE_STRLEN(pTemp);
1140 *buf = (char *) em_malloc(sLen + 1);
1142 EM_DEBUG_EXCEPTION("malloc is failed");
1145 strncpy(*buf, pTemp, sLen);
1149 #ifdef _PRINT_STORAGE_LOG_
1151 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", *buf, index);
1153 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1161 static int _get_table_field_data_string_without_allocation(char **table, char *buf, int buffer_size, int ucs2, int index)
1163 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1164 EM_DEBUG_EXCEPTION(" table[%p], buf[%p], index[%d]", table, buf, index);
1168 char *pTemp = table[index];
1173 memset(buf, 0, buffer_size);
1174 strncpy(buf, pTemp, buffer_size - 1);
1176 #ifdef _PRINT_STORAGE_LOG_
1178 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", buf, index);
1180 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1186 static int _get_table_field_data_blob(char **table, void **buffer, int buffer_size, int index)
1188 if ((table == NULL) || (buffer == NULL) || (index < 0)) {
1189 EM_DEBUG_EXCEPTION(" table[%p], buffer[%p], buffer_size [%d], index[%d]", table, buffer, buffer_size, index);
1193 char *temp_buffer = table[index];
1195 if (temp_buffer == NULL)
1198 *buffer = malloc(buffer_size);
1199 if (*buffer == NULL) {
1200 EM_DEBUG_EXCEPTION("allocation failed.");
1203 memset(*buffer, 0, buffer_size);
1204 memcpy(*buffer, temp_buffer, buffer_size);
1206 #ifdef _PRINT_STORAGE_LOG_
1208 EM_DEBUG_LOG("_get_table_field_data_string - buffer[%s], index[%d]", buffer, index);
1210 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1216 static int _get_stmt_field_data_char(DB_STMT hStmt, char *buf, int index)
1218 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1219 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1223 if (sqlite3_column_text(hStmt, index) != NULL) {
1224 *buf = (char)sqlite3_column_int(hStmt, index);
1225 #ifdef _PRINT_STORAGE_LOG_
1226 EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1231 EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1236 static int _get_stmt_field_data_int(DB_STMT hStmt, int *buf, int index)
1238 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1239 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1243 if (sqlite3_column_text(hStmt, index) != NULL) {
1244 *buf = sqlite3_column_int(hStmt, index);
1245 #ifdef _PRINT_STORAGE_LOG_
1246 EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1251 EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1256 static int _get_stmt_field_data_time_t(DB_STMT hStmt, time_t *buf, int index)
1258 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1259 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1263 if (sqlite3_column_text(hStmt, index) != NULL) {
1264 *buf = (time_t)sqlite3_column_int(hStmt, index);
1265 #ifdef _PRINT_STORAGE_LOG_
1266 EM_DEBUG_LOG("_get_stmt_field_data_time_t [%d]", *buf);
1271 EM_DEBUG_LOG("_get_stmt_field_data_time_t fail. index [%d]", index);
1275 static int _get_stmt_field_data_string(DB_STMT hStmt, char **buf, int ucs2, int index)
1277 if (!hStmt || !buf || (index < 0)) { /*prevent 39619*/
1278 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1283 sLen = sqlite3_column_bytes(hStmt, index);
1285 #ifdef _PRINT_STORAGE_LOG_
1286 EM_DEBUG_LOG("_get_stmt_field_data_string sqlite3_column_bytes sLen[%d]", sLen);
1290 *buf = (char *) em_malloc(sLen + 1);
1292 EM_DEBUG_EXCEPTION("em_mallocfailed");
1296 strncpy(*buf, (char *)sqlite3_column_text(hStmt, index), sLen);
1300 #ifdef _PRINT_STORAGE_LOG_
1302 EM_DEBUG_LOG("buf[%s], index[%d]", *buf, index);
1304 EM_DEBUG_LOG("_get_stmt_field_data_string - No string got");
1310 static void _get_stmt_field_data_blob(DB_STMT hStmt, void **buf, int index)
1312 if (!hStmt || !buf || (index < 0)) { /*prevent 39618*/
1313 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1318 sLen = sqlite3_column_bytes(hStmt, index);
1320 #ifdef _PRINT_STORAGE_LOG_
1321 EM_DEBUG_LOG("_get_stmt_field_data_blob sqlite3_column_bytes sLen[%d]", sLen);
1325 *buf = (char *) em_malloc(sLen);
1327 EM_DEBUG_EXCEPTION("em_mallocfailed");
1331 memcpy(*buf, (void *)sqlite3_column_blob(hStmt, index), sLen);
1337 static int _bind_stmt_field_data_char(DB_STMT hStmt, int index, char value)
1339 if ((hStmt == NULL) || (index < 0)) {
1340 EM_DEBUG_EXCEPTION("index[%d]", index);
1344 int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1346 if (ret != SQLITE_OK) {
1347 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1354 static int _bind_stmt_field_data_int(DB_STMT hStmt, int index, int value)
1356 if ((hStmt == NULL) || (index < 0)) {
1357 EM_DEBUG_EXCEPTION("index[%d]", index);
1361 int ret = sqlite3_bind_int(hStmt, index+1, value);
1363 if (ret != SQLITE_OK) {
1364 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1371 static int _bind_stmt_field_data_time_t(DB_STMT hStmt, int index, time_t value)
1373 if ((hStmt == NULL) || (index < 0)) {
1374 EM_DEBUG_EXCEPTION("index[%d]", index);
1378 int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1380 if (ret != SQLITE_OK) {
1381 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1388 static int _bind_stmt_field_data_string(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1390 if ((hStmt == NULL) || (index < 0)) {
1391 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1395 #ifdef _PRINT_STORAGE_LOG_
1396 EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1401 ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1403 ret = sqlite3_bind_text(hStmt, index+1, "", -1, NULL);
1405 if (ret != SQLITE_OK) {
1406 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1412 /* destroy function for sqlite3_bind_text */
1413 void _bind_stmt_free_string(void* arg)
1415 EM_DEBUG_FUNC_BEGIN();
1416 char* p = (char*) arg;
1418 EM_DEBUG_FUNC_END();
1421 static int _bind_stmt_field_data_nstring(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1423 if ((hStmt == NULL) || (index < 0)) {
1424 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1428 #ifdef _PRINT_STORAGE_LOG_
1429 EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1433 if (value != NULL) {
1434 if (strlen(value) <= max_len)
1435 ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1437 char *buf = (char*)em_malloc(sizeof(char) * (max_len));
1439 EM_DEBUG_EXCEPTION("em_mallocfailed");
1442 snprintf(buf, max_len-1, "%s", value);
1443 ret = sqlite3_bind_text(hStmt, index+1, buf, -1, _bind_stmt_free_string);
1446 ret = sqlite3_bind_text(hStmt, index+1, "", -1, NULL);
1448 if (ret != SQLITE_OK) {
1449 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1456 static int _bind_stmt_field_data_blob(DB_STMT hStmt, int index, void *blob, int blob_size)
1458 if ((hStmt == NULL) || (index < 0)) {
1459 EM_DEBUG_EXCEPTION("index[%d], blob_size[%d]", index, blob_size);
1463 #ifdef _PRINT_STORAGE_LOG_
1464 EM_DEBUG_LOG("hStmt = %p, index = %d, blob_size = %d, blob = [%p]", hStmt, index, blob_size, blob);
1469 ret = sqlite3_bind_blob(hStmt, index+1, blob, blob_size, SQLITE_STATIC);
1471 ret = sqlite3_bind_null(hStmt, index+1);
1473 if (ret != SQLITE_OK) {
1474 EM_DEBUG_EXCEPTION("sqlite3_bind_blob fail [%d]", ret);
1481 static int _delete_temp_file(const char *path)
1483 EM_DEBUG_FUNC_BEGIN("path[%p]", path);
1486 struct dirent *entry = NULL;
1488 char buf[1024] = {0x00, };
1490 if ((dp = opendir(path)) == NULL) {
1491 EM_DEBUG_EXCEPTION("opendir(\"%s\") failed...", path);
1495 while ((entry = readdir(dp)) != NULL) {
1496 SNPRINTF(buf, sizeof(buf), "%s/%s", path, entry->d_name);
1501 EM_DEBUG_FUNC_END();
1505 char *cpy_str(char *src)
1510 if (!(p = em_malloc((int)EM_SAFE_STRLEN(src) + 1))) {
1511 EM_DEBUG_EXCEPTION("mailoc failed...");
1514 strncpy(p, src, EM_SAFE_STRLEN(src));
1520 static void _emstorage_close_once(void)
1522 EM_DEBUG_FUNC_BEGIN();
1524 EM_DEBUG_FUNC_END();
1527 INTERNAL_FUNC int emstorage_close(int *err_code)
1529 EM_DEBUG_FUNC_BEGIN();
1532 int error = EMAIL_ERROR_NONE;
1534 if (!emstorage_db_close(NULL, &error))
1536 if (--_open_counter == 0)
1537 _emstorage_close_once();
1541 if (err_code != NULL)
1544 EM_DEBUG_FUNC_END("ret [%d]", ret);
1548 static void *_emstorage_open_once(char *multi_user_name, int *err_code)
1550 EM_DEBUG_FUNC_BEGIN();
1552 int error = EMAIL_ERROR_NONE;
1554 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1555 char buf[MAX_PATH] = {0};
1556 char *prefix_path = NULL;
1558 error = emcore_get_container_path(multi_user_name, &prefix_path);
1559 if (error != EMAIL_ERROR_NONE) {
1560 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
1564 memset(buf, 0x00, sizeof(buf));
1565 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, EMAIL_PATH);
1566 mkdir(buf, DIRECTORY_PERMISSION);
1568 memset(buf, 0x00, sizeof(buf));
1569 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAIL_HOME);
1570 mkdir(buf, DIRECTORY_PERMISSION);
1572 memset(buf, 0x00, sizeof(buf));
1573 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAIL_TEMP);
1574 mkdir(buf, DIRECTORY_PERMISSION);
1576 _delete_temp_file(buf);
1577 EM_SAFE_FREE(prefix_path);
1579 mkdir(DATA_PATH, DIRECTORY_PERMISSION);
1580 mkdir(EMAIL_PATH, DIRECTORY_PERMISSION);
1581 mkdir(MAIL_HOME, DIRECTORY_PERMISSION);
1582 mkdir(MAIL_TEMP, DIRECTORY_PERMISSION);
1584 _delete_temp_file(MAIL_TEMP);
1587 if (!emstorage_create_table(multi_user_name, EMAIL_CREATE_DB_NORMAL, &error)) {
1588 EM_DEBUG_EXCEPTION(" emstorage_create_table failed - %d", error);
1595 if (err_code != NULL)
1601 /* pData : a parameter which is registered when busy handler is registerd */
1602 /* count : retry count */
1603 static int _callback_sqlite_busy_handler(void *pData, int count)
1605 if (10 - count > 0) {
1606 struct timespec time = {
1608 .tv_nsec = (count + 1) * 100 * 1000 * 1000
1610 EM_DEBUG_LOG("Busy handler called!!: PID[%d] / CNT [%d]", getpid(), count);
1611 nanosleep(&time, NULL);
1614 EM_DEBUG_EXCEPTION("Busy handler will be returned SQLITE_BUSY error PID[%d] / CNT[%d]", getpid(), count);
1619 static int _callback_collation_utf7_sort(void *data, int length_text_a, const void *text_a,
1620 int length_text_b, const void *text_b)
1622 EM_DEBUG_FUNC_BEGIN();
1624 char *converted_string_a = NULL;
1625 char *converted_string_b = NULL;
1627 EM_DEBUG_LOG_DEV("text_a : [%s]", text_a);
1628 converted_string_a = emcore_convert_mutf7_to_utf8((char *)text_a);
1629 EM_DEBUG_LOG_DEV("Converted text_a : [%s]", converted_string_a);
1631 EM_DEBUG_LOG_DEV("text_b : [%s]", text_b);
1632 converted_string_b = emcore_convert_mutf7_to_utf8((char *)text_b);
1633 EM_DEBUG_LOG_DEV("Converted text_b : [%s]", converted_string_b);
1635 if (converted_string_a && converted_string_b)
1636 result = strcmp(converted_string_a, converted_string_b);
1638 EM_SAFE_FREE(converted_string_a);
1639 EM_SAFE_FREE(converted_string_b);
1641 EM_DEBUG_FUNC_END();
1645 static int _delete_all_files_and_directories(char *db_file_path, int *err_code)
1647 EM_DEBUG_FUNC_BEGIN();
1649 int error = EMAIL_ERROR_NONE;
1652 if (!emstorage_delete_file(db_file_path, &error)) {
1653 if (error != EMAIL_ERROR_FILE_NOT_FOUND) {
1654 EM_DEBUG_EXCEPTION_SEC("remove failed - %s", EMAIL_SERVICE_DB_FILE_PATH);
1659 if (!emstorage_delete_dir((char *)MAIL_HOME, &error)) {
1660 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed");
1669 EM_DEBUG_FUNC_END();
1673 static int _recovery_from_malformed_db_file(char *db_file_path, int *err_code)
1675 EM_DEBUG_FUNC_BEGIN();
1677 int error = EMAIL_ERROR_NONE;
1680 /* Delete all files and directories */
1681 if (!_delete_all_files_and_directories(db_file_path, &error)) {
1682 EM_DEBUG_EXCEPTION("_delete_all_files_and_directories failed [%d]", error);
1686 /* Delete all accounts on MyAccount */
1688 /* Delete all managed connection to DB */
1689 emstorage_reset_db_handle_list();
1696 EM_DEBUG_FUNC_END();
1700 int _xsystem(const char *argv[])
1708 perror("fork failed");
1711 if (execvp(argv[0], (char *const *)argv) == -1) {
1712 perror("execute init db script");
1722 if (waitpid(pid, &status, 0) == -1) {
1723 perror("waitpid failed");
1727 if (WIFSIGNALED(status)) {
1732 if (!WIFEXITED(status)) {
1733 perror("should not happen");
1737 return WEXITSTATUS(status);
1740 #define SCRIPT_INIT_DB "/usr/bin/email-service_init_db.sh"
1742 INTERNAL_FUNC int emstorage_init_db(char *multi_user_name)
1744 EM_DEBUG_FUNC_BEGIN();
1745 int err = EMAIL_ERROR_NONE;
1746 char *prefix_path = NULL;
1747 char *output_file_path = NULL;
1748 char temp_file_path[MAX_PATH] = {0};
1749 gid_t email_gid = -1;
1750 uid_t email_uid = -1;
1752 struct group buf_group;
1754 struct passwd buf_passwd;
1755 char buf_gr[MAX_GRP_BUF_SIZE];
1756 char buf_pw[MAX_GRP_BUF_SIZE];
1759 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1760 err = emcore_get_container_path(multi_user_name, &prefix_path);
1761 if (err != EMAIL_ERROR_NONE) {
1762 if (err != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1763 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", err);
1768 prefix_path = strdup("");
1771 if (err == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1772 err = emcore_get_canonicalize_path((char *)EMAIL_SERVICE_DB_FILE_PATH, &output_file_path);
1773 if (err != EMAIL_ERROR_NONE) {
1774 EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", err);
1778 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
1780 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
1783 EM_DEBUG_LOG("db file path : [%s]", temp_file_path);
1785 if (!g_file_test(temp_file_path, G_FILE_TEST_EXISTS)) {
1787 const char *argv_script[] = {"/bin/sh", SCRIPT_INIT_DB, NULL};
1788 ret = _xsystem(argv_script);
1791 EM_DEBUG_EXCEPTION("_xsystem failed");
1792 err = EMAIL_ERROR_SYSTEM_FAILURE;
1795 result = getgrnam_r(GID, &buf_group, buf_gr, sizeof(buf_gr), &gr);
1796 if (result == 0 && NULL != gr)
1797 email_gid = gr->gr_gid;
1799 email_gid = GID_DEFAULT;
1802 result = getpwnam_r(UID, &buf_passwd, buf_pw, sizeof(buf_pw), &pw);
1803 if (result == 0 && NULL != pw)
1804 email_uid = pw->pw_uid;
1806 email_uid = UID_DEFAULT;
1809 chmod(EMAIL_SERVICE_DB_FILE_PATH, 0660);
1810 chmod(EMAIL_SERVICE_DB_JOURNAL_FILE_PATH, 0660);
1811 chmod(EMAIL_SQL_PATH, 0660);
1813 chown(EMAIL_SERVICE_DB_FILE_PATH, email_uid, email_gid);
1814 chown(EMAIL_SERVICE_DB_JOURNAL_FILE_PATH, email_uid, email_gid);
1815 chown(EMAIL_SQL_PATH, email_uid, email_gid);
1821 EM_SAFE_FREE(prefix_path);
1822 EM_SAFE_FREE(output_file_path);
1824 EM_DEBUG_FUNC_END();
1828 INTERNAL_FUNC int em_db_open(char *db_file_path, sqlite3 **sqlite_handle, int *err_code)
1830 EM_DEBUG_FUNC_BEGIN();
1832 int error = EMAIL_ERROR_NONE;
1835 EM_DEBUG_LOG_DEV("*sqlite_handle[%p]", *sqlite_handle);
1837 if (*sqlite_handle) { /*prevent 33351*/
1838 EM_DEBUG_LOG_DEV(">>>>> DB Already Opened......");
1839 if (err_code != NULL)
1844 EM_DEBUG_LOG("DB file path : [%s]", db_file_path);
1847 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1848 if (SQLITE_OK != rc) {
1849 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1850 if (SQLITE_PERM == rc || SQLITE_CANTOPEN == rc) {
1851 error = EMAIL_ERROR_PERMISSION_DENIED;
1853 error = EMAIL_ERROR_DB_FAILURE;
1855 sqlite3_close(*sqlite_handle);
1856 *sqlite_handle = NULL;
1858 if (SQLITE_CORRUPT == rc) /* SQLITE_CORRUPT : The database disk image is malformed */ {/* Recovery DB file */
1859 EM_DEBUG_LOG("The database disk image is malformed. Trying to remove and create database disk image and directories");
1860 if (!_recovery_from_malformed_db_file(db_file_path, &error)) {
1861 EM_DEBUG_EXCEPTION("_recovery_from_malformed_db_file failed [%d]", error);
1865 EM_DEBUG_LOG("Open DB again");
1866 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1867 if (SQLITE_OK != rc) {
1868 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1869 if (SQLITE_PERM == rc) {
1870 error = EMAIL_ERROR_PERMISSION_DENIED;
1872 error = EMAIL_ERROR_DB_FAILURE;
1874 sqlite3_close(*sqlite_handle);
1875 *sqlite_handle = NULL;
1876 goto FINISH_OFF; /*prevent 33351*/
1882 /* register busy handler */
1883 EM_DEBUG_LOG_DEV(">>>>> Register DB Handle to busy handler: *sqlite_handle[%p]", *sqlite_handle);
1884 rc = sqlite3_busy_handler(*sqlite_handle, _callback_sqlite_busy_handler, NULL); /* Busy Handler registration, NULL is a parameter which will be passed to handler */
1885 if (SQLITE_OK != rc) {
1886 EM_DEBUG_EXCEPTION("sqlite3_busy_handler fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1887 error = EMAIL_ERROR_DB_FAILURE;
1888 sqlite3_close(*sqlite_handle);
1889 *sqlite_handle = NULL;
1893 /* Register collation callback function */
1894 rc = sqlite3_create_collation(*sqlite_handle, "CONVERTUTF8", SQLITE_UTF8, NULL, _callback_collation_utf7_sort);
1895 if (SQLITE_OK != rc) {
1896 EM_DEBUG_EXCEPTION("sqlite3_create_collation failed : [%d][%s]", rc, sqlite3_errmsg(*sqlite_handle));
1897 error = EMAIL_ERROR_DB_FAILURE;
1898 sqlite3_close(*sqlite_handle);
1899 *sqlite_handle = NULL;
1906 if (err_code != NULL)
1909 EM_DEBUG_FUNC_END("ret [%d]", ret);
1913 INTERNAL_FUNC sqlite3* emstorage_db_open(char *multi_user_name, int *err_code)
1915 EM_DEBUG_FUNC_BEGIN();
1917 sqlite3 *_db_handle = NULL;
1919 int error = EMAIL_ERROR_NONE;
1920 char *prefix_path = NULL;
1922 _db_handle = emstorage_get_db_handle(multi_user_name);
1924 if (_db_handle == NULL) {
1925 char *output_file_path = NULL;
1926 char temp_file_path[MAX_PATH] = {0};
1928 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1929 error = emcore_get_container_path(multi_user_name, &prefix_path);
1930 if (error != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION && error != EMAIL_ERROR_NONE) {
1931 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", error);
1935 prefix_path = strdup("");
1938 if (error == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1939 if ((error = emcore_get_canonicalize_path((char *)EMAIL_SERVICE_DB_FILE_PATH, &output_file_path)) != EMAIL_ERROR_NONE) {
1940 EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", error);
1944 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
1945 EM_SAFE_FREE(output_file_path);
1947 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
1950 if (!em_db_open(temp_file_path, &_db_handle, &error)) {
1951 EM_DEBUG_EXCEPTION("em_db_open failed[%d]", error);
1955 _initialize_shm_mutex(SHM_FILE_FOR_DB_LOCK, &shm_fd_for_db_lock, &mapped_for_db_lock);
1957 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
1958 _initialize_shm_mutex(SHM_FILE_FOR_MAIL_ID_LOCK, &shm_fd_for_generating_mail_id, &mapped_for_generating_mail_id);
1959 #endif /*__FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
1961 emstorage_set_db_handle(multi_user_name, _db_handle);
1963 emstorage_initialize_field_count();
1968 EM_SAFE_FREE(prefix_path);
1970 if (err_code != NULL)
1973 EM_DEBUG_FUNC_END("ret [%p]", _db_handle);
1977 INTERNAL_FUNC int emstorage_db_close(char *multi_user_name, int *err_code)
1979 EM_DEBUG_FUNC_BEGIN();
1980 #ifdef _MULTIPLE_DB_HANDLE
1981 sqlite3 *_db_handle = emstorage_get_db_handle(multi_user_name);
1984 int error = EMAIL_ERROR_NONE;
1988 ret = sqlite3_close(_db_handle);
1989 if (ret != SQLITE_OK) {
1990 EM_DEBUG_EXCEPTION(" sqlite3_close fail - %d", ret);
1991 error = EMAIL_ERROR_DB_FAILURE;
1995 #ifdef _MULTIPLE_DB_HANDLE
1996 emstorage_remove_db_handle();
2004 if (err_code != NULL)
2007 EM_DEBUG_FUNC_END("ret [%d]", ret);
2011 INTERNAL_FUNC int emstorage_open(char *multi_user_name, int *err_code)
2013 EM_DEBUG_FUNC_BEGIN();
2016 int error = EMAIL_ERROR_NONE;
2018 char *prefix_path = NULL;
2019 char buf[MAX_PATH] = {0};
2021 if (EM_SAFE_STRLEN(multi_user_name) <= 0) {
2022 SNPRINTF(buf, sizeof(buf), "%s", DB_PATH);
2024 error = emcore_get_container_path(multi_user_name, &prefix_path);
2025 if (error != EMAIL_ERROR_NONE) {
2026 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
2029 SNPRINTF(buf, sizeof(buf), "%s/%s", prefix_path, DB_PATH);
2032 if (!g_file_test(buf, G_FILE_TEST_EXISTS)) {
2033 retValue = mkdir(buf, DIRECTORY_PERMISSION);
2035 EM_DEBUG_LOG("mkdir return- %d", retValue);
2036 EM_DEBUG_LOG("emstorage_open - before sqlite3_open - pid = %d", getpid());
2039 if (emstorage_db_open(multi_user_name, &error) == NULL) {
2040 EM_DEBUG_EXCEPTION("emstorage_db_open failed[%d]", error);
2044 if (_open_counter++ == 0) {
2045 _emstorage_open_once(multi_user_name, &error);
2052 EM_SAFE_FREE(prefix_path);
2054 if (err_code != NULL)
2057 EM_DEBUG_FUNC_END("ret [%d]", ret);
2061 static int emstorage_get_field_count_from_create_table_query(char *input_create_table_query, int *output_field_count)
2063 EM_DEBUG_FUNC_BEGIN("input_create_table_query[%d], output_field_count[%p]", input_create_table_query, output_field_count);
2064 int err = EMAIL_ERROR_NONE;
2065 int field_count = 0;
2068 if (input_create_table_query == NULL || output_field_count == NULL) {
2069 err = EMAIL_ERROR_INVALID_PARAM;
2073 pos = input_create_table_query;
2077 if (pos == NULL || *pos == (char)0)
2080 pos = strchr(pos, ',');
2083 *output_field_count = field_count;
2085 EM_DEBUG_LOG_DEV("field_count [%d]", field_count);
2089 EM_DEBUG_FUNC_END("err [%d]", err);
2093 INTERNAL_FUNC int emstorage_initialize_field_count()
2095 EM_DEBUG_FUNC_BEGIN();
2096 int err = EMAIL_ERROR_NONE;
2098 char **create_table_query = NULL;
2100 if (_field_count_of_table[CREATE_TABLE_MAIL_ACCOUNT_TBL] != 0) {
2101 err = EMAIL_ERROR_ALREADY_INITIALIZED;
2105 err = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2106 if (err != EMAIL_ERROR_NONE) {
2107 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", err);
2111 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]));
2112 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]));
2113 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]));
2114 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]));
2115 emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_TBL]));
2116 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]));
2117 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]));
2118 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]));
2119 #ifdef __FEATURE_LOCAL_ACTIVITY__
2120 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]));
2121 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2122 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]));
2123 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]));
2124 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]));
2127 if (create_table_query) {
2129 for (i = 0; i < query_len; i++) {
2130 if (create_table_query[i]) {
2131 EM_SAFE_FREE(create_table_query[i]);
2134 EM_SAFE_FREE(create_table_query);
2137 EM_DEBUG_FUNC_END("err [%d]", err);
2141 INTERNAL_FUNC int emstorage_create_table(char *multi_user_name, emstorage_create_db_type_t type, int *err_code)
2143 EM_DEBUG_FUNC_BEGIN();
2145 int error = EMAIL_ERROR_NONE;
2146 int rc = -1, ret = false;
2148 char sql_query_string[QUERY_SIZE] = {0, };
2149 char **create_table_query = NULL;
2151 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2153 error = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2154 if (error != EMAIL_ERROR_NONE) {
2155 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
2159 if (query_len < CREATE_TABLE_MAX) {
2160 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
2161 error = EMAIL_ERROR_SYSTEM_FAILURE;
2165 EM_DEBUG_LOG("local_db_handle = %p.", local_db_handle);
2168 char **result = NULL;
2170 /* 1. create mail_account_tbl */
2171 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_account_tbl';";
2172 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2173 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2174 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2176 EM_DEBUG_LOG("emstorage_create_table - result[1] = %s %c", result[1], result[1]);
2178 if (atoi(result[1]) < 1) {
2179 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2180 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2181 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2183 EM_DEBUG_LOG("CREATE TABLE mail_account_tbl");
2184 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2185 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2186 if (error != EMAIL_ERROR_NONE) {
2187 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2191 /* create mail_account_tbl unique index */
2192 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ACCOUNT_IDX]);
2193 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2194 if (error != EMAIL_ERROR_NONE) {
2195 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2199 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2200 } /* mail_account_tbl */
2201 else if (type == EMAIL_CREATE_DB_CHECK) {
2202 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], NULL, NULL, NULL), rc);
2203 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)));
2206 sqlite3_free_table(result);
2209 /* 2. create mail_box_tbl */
2210 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_box_tbl';";
2212 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2213 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2214 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2216 if (atoi(result[1]) < 1) {
2217 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2218 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2219 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2221 EM_DEBUG_LOG("CREATE TABLE mail_box_tbl");
2223 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_BOX_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2224 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2225 if (error != EMAIL_ERROR_NONE) {
2226 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2230 /* create mail_local_mailbox_tbl unique index */
2231 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_BOX_IDX]);
2232 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2233 if (error != EMAIL_ERROR_NONE) {
2234 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2238 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2239 } /* mail_box_tbl */
2240 else if (type == EMAIL_CREATE_DB_CHECK) {
2241 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_BOX_TBL], NULL, NULL, NULL);
2242 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2243 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_BOX_TBL], rc, sqlite3_errmsg(local_db_handle)));
2245 sqlite3_free_table(result);
2248 /* 3. create mail_read_mail_uid_tbl */
2249 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_read_mail_uid_tbl';";
2250 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2251 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2252 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2255 if (atoi(result[1]) < 1) {
2256 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2257 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2258 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2260 EM_DEBUG_LOG("CREATE TABLE mail_read_mail_uid_tbl");
2262 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2263 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2264 if (error != EMAIL_ERROR_NONE) {
2265 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2269 /* create mail_read_mail_uid_tbl unique index */
2270 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_IDX]);
2271 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2272 if (error != EMAIL_ERROR_NONE) {
2273 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2277 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2278 } /* mail_read_mail_uid_tbl */
2279 else if (type == EMAIL_CREATE_DB_CHECK) {
2280 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], NULL, NULL, NULL), rc);
2281 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2282 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], rc, sqlite3_errmsg(local_db_handle)));
2284 sqlite3_free_table(result);
2287 /* 4. create mail_rule_tbl */
2288 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_rule_tbl';";
2290 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2291 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2292 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2294 if (atoi(result[1]) < 1) {
2295 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2296 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2297 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2299 EM_DEBUG_LOG("CREATE TABLE mail_rule_tbl");
2301 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_RULE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2302 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2303 if (error != EMAIL_ERROR_NONE) {
2304 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2308 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2309 } /* mail_rule_tbl */
2310 else if (type == EMAIL_CREATE_DB_CHECK) {
2311 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_RULE_TBL], NULL, NULL, NULL), rc);
2312 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2313 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_RULE_TBL], rc, sqlite3_errmsg(local_db_handle)));
2315 sqlite3_free_table(result);
2318 /* 5. create mail_tbl */
2319 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_tbl';";
2320 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2321 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2322 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2324 if (atoi(result[1]) < 1) {
2325 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2326 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2327 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2328 EM_DEBUG_LOG("CREATE TABLE mail_tbl");
2330 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2331 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2332 if (error != EMAIL_ERROR_NONE) {
2333 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2337 /* create mail_tbl unique index */
2338 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_IDX]);
2339 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2340 if (error != EMAIL_ERROR_NONE) {
2341 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2345 /* create mail_tbl index for date_time */
2346 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_DATETIME_IDX]);
2347 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2348 if (error != EMAIL_ERROR_NONE) {
2349 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2353 /* create mail_tbl index for thread_item_count */
2354 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_THREAD_IDX]);
2355 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2356 if (error != EMAIL_ERROR_NONE) {
2357 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2361 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2362 /* just one time call */
2363 /* EFTSInitFTSIndex(FTS_EMAIL_IDX); */
2365 else if (type == EMAIL_CREATE_DB_CHECK) {
2366 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TBL], NULL, NULL, NULL), rc);
2367 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2368 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TBL], rc, sqlite3_errmsg(local_db_handle)));
2370 sqlite3_free_table(result);
2373 /* 6. create mail_attachment_tbl */
2374 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_attachment_tbl';";
2375 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2376 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2377 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2379 if (atoi(result[1]) < 1) {
2380 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2381 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2382 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2384 EM_DEBUG_LOG("CREATE TABLE mail_attachment_tbl");
2386 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2387 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2388 if (error != EMAIL_ERROR_NONE) {
2389 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2393 /* create mail_attachment_tbl unique index */
2394 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_IDX]);
2395 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2396 if (error != EMAIL_ERROR_NONE) {
2397 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2401 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2402 } /* mail_attachment_tbl */
2403 else if (type == EMAIL_CREATE_DB_CHECK) {
2404 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], NULL, NULL, NULL), rc);
2405 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2406 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], rc, sqlite3_errmsg(local_db_handle)));
2408 sqlite3_free_table(result);
2411 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
2413 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_partial_body_activity_tbl';";
2414 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2415 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2416 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2418 if (atoi(result[1]) < 1) {
2420 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2421 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2422 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2424 EM_DEBUG_LOG("CREATE TABLE mail_partial_body_activity_tbl");
2426 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2427 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2428 if (error != EMAIL_ERROR_NONE) {
2429 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2433 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2434 } /* mail_rule_tbl */
2435 else if (type == EMAIL_CREATE_DB_CHECK) {
2436 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2437 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2438 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2440 sqlite3_free_table(result);
2443 #endif /* __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
2445 /* create mail_meeting_tbl */
2446 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_meeting_tbl';";
2447 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2448 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2449 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2451 if (atoi(result[1]) < 1) {
2452 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2453 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2454 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2456 EM_DEBUG_LOG("CREATE TABLE mail_meeting_tbl");
2458 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_MEETING_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2459 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2460 if (error != EMAIL_ERROR_NONE) {
2461 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2465 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_MEETING_IDX]);
2466 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2467 if (error != EMAIL_ERROR_NONE) {
2468 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2472 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2473 } /* mail_contact_sync_tbl */
2474 else if (type == EMAIL_CREATE_DB_CHECK) {
2475 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], NULL, NULL, NULL), rc);
2476 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2477 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], rc, sqlite3_errmsg(local_db_handle)));
2479 sqlite3_free_table(result);
2482 #ifdef __FEATURE_LOCAL_ACTIVITY__
2484 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_local_activity_tbl';";
2485 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2486 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2487 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2489 if (atoi(result[1]) < 1) {
2491 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2492 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2493 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2495 EM_DEBUG_LOG(" CREATE TABLE mail_local_activity_tbl");
2497 SNPRINTF(sql_query_string, sizeof(sql_query_string), create_table_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL]);
2498 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2499 if (error != EMAIL_ERROR_NONE) {
2500 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2504 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2505 } /* mail_rule_tbl */
2506 else if (type == EMAIL_CREATE_DB_CHECK) {
2507 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL);
2508 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2509 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2510 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2512 sqlite3_free_table(result);
2514 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2515 /* create mail_certificate_tbl */
2516 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_certificate_tbl';";
2517 /* rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
2518 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2519 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)));
2521 if (atoi(result[1]) < 1) {
2522 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2523 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2525 EM_DEBUG_LOG("CREATE TABLE mail_certificate_tbl");
2527 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2528 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2529 if (error != EMAIL_ERROR_NONE) {
2530 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2534 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2535 } /* mail_contact_sync_tbl */
2536 else if (type == EMAIL_CREATE_DB_CHECK) {
2537 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], NULL, NULL, NULL), rc);
2538 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)));
2541 sqlite3_free_table(result);
2544 /* create mail_task_tbl */
2545 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_task_tbl';";
2546 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2547 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)));
2549 if (atoi(result[1]) < 1) {
2550 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2551 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2553 EM_DEBUG_LOG("CREATE TABLE mail_task_tbl");
2555 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TASK_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2556 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2557 if (error != EMAIL_ERROR_NONE) {
2558 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2562 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_TASK_IDX]);
2563 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2564 if (error != EMAIL_ERROR_NONE) {
2565 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2569 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2570 } /* mail_task_tbl */
2571 else if (type == EMAIL_CREATE_DB_CHECK) {
2572 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TASK_TBL], NULL, NULL, NULL), rc);
2573 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)));
2576 sqlite3_free_table(result);
2579 #ifdef __FEATURE_BODY_SEARCH__
2580 /* create mail_text_tbl */
2581 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_text_tbl';";
2582 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2583 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2585 if (atoi(result[1]) < 1) {
2586 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2587 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2589 EM_DEBUG_LOG("CREATE TABLE mail_text_tbl");
2591 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TEXT_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2592 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2593 if (error != EMAIL_ERROR_NONE) {
2594 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2598 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2599 } /* mail_text_tbl */
2600 else if (type == EMAIL_CREATE_DB_CHECK) {
2601 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TEXT_TBL], NULL, NULL, NULL), rc);
2602 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)));
2605 sqlite3_free_table(result);
2610 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
2611 /* create mail_auto_download_activity_tbl */
2612 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_auto_download_activity_tbl';";
2613 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2614 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)));
2616 if (atoi(result[1]) < 1) {
2617 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2618 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2620 EM_DEBUG_LOG("CREATE TABLE mail_auto_download_activity_tbl");
2622 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], sizeof(sql_query_string)-1);
2623 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2624 if (error != EMAIL_ERROR_NONE) {
2625 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2629 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2630 } /* mail_auto_download_activity_tbl */
2631 else if (type == EMAIL_CREATE_DB_CHECK) {
2632 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2633 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)));
2636 sqlite3_free_table(result);
2645 if (result) sqlite3_free_table(result);
2648 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2650 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
2654 if (create_table_query) {
2656 for (i = 0; i < query_len; i++) {
2657 if (create_table_query[i]) {
2658 EM_SAFE_FREE(create_table_query[i]);
2661 EM_SAFE_FREE(create_table_query);
2664 if (err_code != NULL)
2667 EM_DEBUG_FUNC_END("ret [%d]", ret);
2671 /* Query series --------------------------------------------------------------*/
2672 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)
2674 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);
2677 int error = EMAIL_ERROR_NONE;
2678 DB_STMT hStmt = NULL;
2679 char *sql_query_string = NULL;
2681 sqlite3 *local_db_handle = NULL;
2683 if (!input_conditional_clause || (!output_total_mail_count && !output_unseen_mail_count)) {
2684 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2685 return EMAIL_ERROR_INVALID_PARAM;
2688 query_size = EM_SAFE_STRLEN(input_conditional_clause) + QUERY_SIZE;
2689 sql_query_string = em_malloc(query_size);
2690 if (sql_query_string == NULL) {
2691 EM_DEBUG_EXCEPTION("em_mallocfailed");
2692 error = EMAIL_ERROR_OUT_OF_MEMORY;
2696 local_db_handle = emstorage_get_db_connection(multi_user_name);
2698 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2700 SNPRINTF(sql_query_string, query_size, "SELECT COUNT(*) FROM mail_tbl");
2701 EM_SAFE_STRCAT(sql_query_string, (char*)input_conditional_clause);
2703 if (output_total_mail_count) {
2704 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
2705 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2706 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
2708 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2709 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2710 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
2711 _get_stmt_field_data_int(hStmt, output_total_mail_count, 0);
2714 if (output_unseen_mail_count) {
2715 EM_SAFE_STRCAT(sql_query_string, " AND flags_seen_field = 0 ");
2717 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
2718 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2719 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2721 *output_unseen_mail_count = atoi(result[1]);
2722 sqlite3_free_table(result);
2727 if (hStmt != NULL) {
2728 rc = sqlite3_finalize(hStmt);
2729 if (rc != SQLITE_OK) {
2730 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
2731 error = EMAIL_ERROR_DB_FAILURE;
2735 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2737 EM_SAFE_FREE(sql_query_string);
2739 EM_DEBUG_FUNC_END("error [%d]", error);
2743 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)
2745 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);
2753 int error = EMAIL_ERROR_NONE;
2754 int *result_mail_id_list = NULL;
2755 char **result = NULL;
2756 char *sql_query_string = NULL;
2757 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2759 EM_IF_NULL_RETURN_VALUE(input_conditional_clause, EMAIL_ERROR_INVALID_PARAM);
2760 EM_IF_NULL_RETURN_VALUE(output_mail_id_list, EMAIL_ERROR_INVALID_PARAM);
2761 EM_IF_NULL_RETURN_VALUE(output_mail_id_count, EMAIL_ERROR_INVALID_PARAM);
2763 query_size = strlen(input_conditional_clause) + strlen("SELECT mail_id FROM mail_tbl ") + 10; // 10 is extra space
2764 sql_query_string = em_malloc(query_size);
2765 if (sql_query_string == NULL) {
2766 EM_DEBUG_EXCEPTION("em_mallocfailed");
2767 error = EMAIL_ERROR_OUT_OF_MEMORY;
2771 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2773 /* Composing query */
2774 SNPRINTF_OFFSET(sql_query_string, cur_query, query_size, "SELECT mail_id FROM mail_tbl ");
2775 EM_SAFE_STRCAT(sql_query_string, (char*)input_conditional_clause);
2777 EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
2779 /* Performing query */
2780 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2781 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2782 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2786 /* to get mail list */
2788 EM_DEBUG_LOG("No mail found...");
2789 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2793 EM_DEBUG_LOG("There are [%d] mails.", count);
2795 if (!(result_mail_id_list = (int*)em_malloc(sizeof(int) * count))) {
2796 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
2797 error = EMAIL_ERROR_OUT_OF_MEMORY;
2801 EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2803 for (i = 0; i < count; i++)
2804 _get_table_field_data_int(result, result_mail_id_list + i, col_index++);
2806 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >> ", count);
2808 *output_mail_id_list = result_mail_id_list;
2809 *output_mail_id_count = count;
2814 sqlite3_free_table(result);
2816 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2818 EM_SAFE_FREE(sql_query_string);
2820 if (error != EMAIL_ERROR_NONE)
2821 EM_SAFE_FREE(result_mail_id_list);
2823 EM_DEBUG_FUNC_END("error [%d]", error);
2827 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)
2829 EM_DEBUG_FUNC_BEGIN();
2830 EM_PROFILE_BEGIN(emstorage_query_mail_list_func);
2832 int i = 0, count = 0, rc = -1, to_get_count = (result_mail_list) ? 0 : 1;
2833 int sql_query_string_length = 0;
2834 int local_inline_content_count = 0, local_attachment_count = 0;
2835 int cur_query = 0, base_count = 0, col_index;
2836 int ret = false, error = EMAIL_ERROR_NONE;
2837 char *date_time_string = NULL;
2838 char **result = NULL;
2839 char *field_mail_id = "mail_id";
2840 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 ";
2841 char *select_query_form = "SELECT %s FROM mail_tbl ";
2842 char *target_field = NULL;
2843 char *sql_query_string = NULL;
2844 email_mail_list_item_t *mail_list_item_from_tbl = NULL;
2845 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2847 EM_IF_NULL_RETURN_VALUE(conditional_clause, false);
2848 EM_IF_NULL_RETURN_VALUE(result_count, false);
2850 EMSTORAGE_START_READ_TRANSACTION(transaction);
2853 if (to_get_count) /* count only */
2854 target_field = field_mail_id;
2855 else /* mail list in plain form */
2856 target_field = field_all;
2858 sql_query_string_length = EM_SAFE_STRLEN(select_query_form) + EM_SAFE_STRLEN(target_field) + EM_SAFE_STRLEN(conditional_clause);
2860 if (sql_query_string_length)
2861 sql_query_string = em_malloc(sql_query_string_length);
2863 if (sql_query_string == NULL) {
2864 EM_DEBUG_EXCEPTION("em_mallocfailed...");
2865 error = EMAIL_ERROR_OUT_OF_MEMORY;
2869 cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, sql_query_string_length, select_query_form, target_field);
2871 strncat(sql_query_string, conditional_clause, sql_query_string_length - cur_query);
2873 EM_DEBUG_LOG_DEV("query[%s]", sql_query_string);
2875 /* performing query */
2876 EM_PROFILE_BEGIN(emstorage_query_mail_list_performing_query);
2877 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2878 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2879 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2880 EM_PROFILE_END(emstorage_query_mail_list_performing_query);
2886 for (tmp = field_all; tmp && *(tmp + 1); tmp = index(tmp + 1, ',')) i++ ;
2890 col_index = base_count;
2892 EM_DEBUG_LOG_DEV("base_count [%d]", base_count);
2897 EM_DEBUG_LOG_DEV("No mail found...");
2899 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2902 EM_DEBUG_LOG_DEV("There are [%d] mails.", count);
2904 /* to get mail list */
2906 EM_DEBUG_LOG_DEV("No mail found...");
2908 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2912 EM_DEBUG_LOG_DEV("There are [%d] mails.", count);
2913 if (!(mail_list_item_from_tbl = (email_mail_list_item_t*)em_malloc(sizeof(email_mail_list_item_t) * count))) {
2914 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
2915 error = EMAIL_ERROR_OUT_OF_MEMORY;
2919 EM_PROFILE_BEGIN(emstorage_query_mail_list_loop);
2920 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >> ");
2921 for (i = 0; i < count; i++) {
2922 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_id), col_index++);
2923 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].account_id), col_index++);
2924 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mailbox_id), col_index++);
2925 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].mailbox_type), col_index++);
2926 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].full_address_from, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2927 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_sender, MAX_EMAIL_ADDRESS_LENGTH, 1, col_index++);
2928 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_recipient, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2929 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].subject, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2930 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].body_download_status), col_index++);
2931 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_size), col_index++);
2932 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_seen_field), col_index++);
2933 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_deleted_field), col_index++);
2934 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_flagged_field), col_index++);
2935 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_answered_field), col_index++);
2936 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_recent_field), col_index++);
2937 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_draft_field), col_index++);
2938 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_forwarded_field), col_index++);
2939 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].DRM_status), col_index++);
2940 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].priority), col_index++);
2941 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].save_status), col_index++);
2942 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].lock_status), col_index++);
2943 _get_table_field_data_int(result, &local_attachment_count, col_index++);
2944 _get_table_field_data_int(result, &local_inline_content_count, col_index++);
2945 _get_table_field_data_time_t(result, &(mail_list_item_from_tbl[i].date_time), col_index++);
2946 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].preview_text, MAX_PREVIEW_TEXT_LENGTH, 1, col_index++);
2947 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_id), col_index++);
2948 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_item_count), col_index++);
2949 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].meeting_request_status), col_index++);
2950 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].message_class), col_index++);
2951 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].smime_type), col_index++);
2952 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].scheduled_sending_time), col_index++);
2953 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].remaining_resend_times), col_index++);
2954 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].tag_id), col_index++);
2955 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].eas_data_length), col_index++);
2956 _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++);
2958 mail_list_item_from_tbl[i].attachment_count = (local_attachment_count > 0) ? 1 : 0;
2960 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >> ", count);
2961 EM_PROFILE_END(emstorage_query_mail_list_loop);
2967 EM_DEBUG_LOG("MAIL_COUNT [%d]", count);
2970 sqlite3_free_table(result);
2973 *result_count = count;
2976 if (result_mail_list)
2977 *result_mail_list = mail_list_item_from_tbl;
2978 *result_count = count;
2980 EM_SAFE_FREE(mail_list_item_from_tbl);
2983 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2985 // sqlite3_db_release_memory(local_db_handle);
2987 EM_SAFE_FREE(sql_query_string);
2988 EM_SAFE_FREE(date_time_string);
2990 if (err_code != NULL)
2993 EM_PROFILE_END(emstorage_query_mail_list_func);
2994 EM_DEBUG_FUNC_END("ret [%d]", ret);
2999 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)
3001 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);
3003 if (!conditional_clause || !result_mail_tbl || !result_count) {
3004 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: [%p] [%p] [%p]", conditional_clause, result_mail_tbl, result_mail_tbl);
3005 if (err_code != NULL)
3006 *err_code = EMAIL_ERROR_INVALID_PARAM;
3010 int i, col_index = FIELD_COUNT_OF_MAIL_TBL, rc, ret = false, count;
3011 int error = EMAIL_ERROR_NONE;
3012 char **result = NULL, sql_query_string[QUERY_SIZE] = {0, };
3013 emstorage_mail_tbl_t* p_data_tbl = NULL;
3014 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3016 EMSTORAGE_START_READ_TRANSACTION(transaction);
3018 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl %s", conditional_clause);
3020 EM_DEBUG_LOG_DEV("Query[%s]", sql_query_string);
3022 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3023 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3024 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
3027 EM_DEBUG_LOG("No mail found...");
3029 error = EMAIL_ERROR_MAIL_NOT_FOUND;
3033 EM_DEBUG_LOG("There are [%d] mails.", count);
3034 if (!(p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * count))) {
3035 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
3036 error = EMAIL_ERROR_OUT_OF_MEMORY;
3040 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >> ");
3041 for (i = 0; i < count; i++) {
3042 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
3043 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3044 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3045 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_type), col_index++);
3046 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 1, col_index++);
3047 _get_table_field_data_time_t (result, &(p_data_tbl[i].date_time), col_index++);
3048 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
3049 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
3050 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
3051 _get_table_field_data_string(result, &(p_data_tbl[i].message_id), 0, col_index++);
3052 _get_table_field_data_int(result, &(p_data_tbl[i].reference_mail_id), col_index++);
3053 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_from), 1, col_index++);
3054 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_reply), 1, col_index++);
3055 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_to), 1, col_index++);
3056 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_cc), 1, col_index++);
3057 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_bcc), 1, col_index++);
3058 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_return), 1, col_index++);
3059 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_sender), 1, col_index++);
3060 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_recipient), 1, col_index++);
3061 _get_table_field_data_string(result, &(p_data_tbl[i].alias_sender), 1, col_index++);
3062 _get_table_field_data_string(result, &(p_data_tbl[i].alias_recipient), 1, col_index++);
3063 _get_table_field_data_int(result, &(p_data_tbl[i].body_download_status), col_index++);
3064 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
3065 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
3066 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
3067 _get_table_field_data_int(result, &(p_data_tbl[i].mail_size), col_index++);
3068 _get_table_field_data_char (result, &(p_data_tbl[i].flags_seen_field), col_index++);
3069 _get_table_field_data_char (result, &(p_data_tbl[i].flags_deleted_field), col_index++);
3070 _get_table_field_data_char (result, &(p_data_tbl[i].flags_flagged_field), col_index++);
3071 _get_table_field_data_char (result, &(p_data_tbl[i].flags_answered_field), col_index++);
3072 _get_table_field_data_char (result, &(p_data_tbl[i].flags_recent_field), col_index++);
3073 _get_table_field_data_char (result, &(p_data_tbl[i].flags_draft_field), col_index++);
3074 _get_table_field_data_char (result, &(p_data_tbl[i].flags_forwarded_field), col_index++);
3075 _get_table_field_data_int(result, &(p_data_tbl[i].DRM_status), col_index++);
3076 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].priority), col_index++);
3077 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
3078 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
3079 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].report_status), col_index++);
3080 _get_table_field_data_int(result, &(p_data_tbl[i].attachment_count), col_index++);
3081 _get_table_field_data_int(result, &(p_data_tbl[i].inline_content_count), col_index++);
3082 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
3083 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
3084 _get_table_field_data_string(result, &(p_data_tbl[i].preview_text), 1, col_index++);
3085 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].meeting_request_status), col_index++);
3086 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].message_class), col_index++);
3087 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].digest_type), col_index++);
3088 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].smime_type), col_index++);
3089 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].scheduled_sending_time), col_index++);
3090 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].remaining_resend_times), col_index++);
3091 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].tag_id), col_index++);
3092 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].replied_time), col_index++);
3093 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].forwarded_time), col_index++);
3094 _get_table_field_data_string(result, &(p_data_tbl[i].default_charset), 0, col_index++);
3095 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3096 _get_table_field_data_blob(result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3097 _get_table_field_data_string(result, &(p_data_tbl[i].user_name), 0, col_index++);
3104 sqlite3_free_table(result);
3107 *result_mail_tbl = p_data_tbl;
3108 *result_count = count;
3110 *result_mail_tbl = NULL;
3114 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3116 // sqlite3_db_release_memory(local_db_handle);
3118 if (err_code != NULL)
3121 EM_DEBUG_FUNC_END("ret [%d]", ret);
3125 #ifdef __FEATURE_BODY_SEARCH__
3126 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)
3128 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);
3130 if (!conditional_clause || !result_mail_text_tbl || !result_count) { /*prevent 50930*/
3131 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3132 if (err_code != NULL)
3133 *err_code = EMAIL_ERROR_INVALID_PARAM;
3141 int col_index = FIELD_COUNT_OF_MAIL_TEXT_TBL;
3142 int error = EMAIL_ERROR_NONE;
3143 char **result = NULL;
3144 char sql_query_string[QUERY_SIZE] = {0, };
3145 emstorage_mail_text_tbl_t* p_data_tbl = NULL;
3146 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3148 EMSTORAGE_START_READ_TRANSACTION(transaction);
3150 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_text_tbl %s", conditional_clause);
3152 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
3154 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3155 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3156 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3159 EM_DEBUG_LOG("No mail found...");
3161 error = EMAIL_ERROR_MAIL_NOT_FOUND;
3165 EM_DEBUG_LOG("There are [%d] mails.", count);
3166 if (!(p_data_tbl = (emstorage_mail_text_tbl_t *)em_malloc(sizeof(emstorage_mail_text_tbl_t) * count))) {
3167 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_text_tbl_t failed...");
3168 error = EMAIL_ERROR_OUT_OF_MEMORY;
3172 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
3173 for (i = 0; i < count; i++) {
3174 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
3175 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3176 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3177 _get_table_field_data_string(result, &(p_data_tbl[i].body_text), 0, col_index++);
3184 sqlite3_free_table(result);
3187 *result_mail_text_tbl = p_data_tbl;
3188 *result_count = count;
3190 EM_SAFE_FREE(p_data_tbl);
3192 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3194 // sqlite3_db_release_memory(local_db_handle);
3196 if (err_code != NULL)
3199 EM_DEBUG_FUNC_END("ret [%d]", ret);
3204 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)
3206 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);
3212 int error = EMAIL_ERROR_NONE;
3214 int total_count = 0;
3216 char sql_query_string[QUERY_SIZE] = {0, };
3217 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 ";
3218 emstorage_mailbox_tbl_t* p_data_tbl = NULL;
3219 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3221 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
3223 if (input_get_mail_count == 0) { /* without mail count */
3225 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);
3226 } else { /* with read count and total count */
3228 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);
3231 EM_DEBUG_LOG_DEV("query[%s]", sql_query_string);
3233 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
3234 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3235 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string))
3237 EM_DEBUG_LOG_DEV("result count [%d]", count);
3240 EM_DEBUG_LOG_SEC("Can't find mailbox query[%s]", sql_query_string);
3241 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
3245 if ((p_data_tbl = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t) * count)) == NULL) {
3246 EM_DEBUG_EXCEPTION("em_mallocfailed...");
3247 error = EMAIL_ERROR_OUT_OF_MEMORY;
3251 for (i = 0; i < count; i++) {
3252 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3253 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3254 _get_table_field_data_int(result, &(p_data_tbl[i].local_yn), col_index++);
3255 _get_table_field_data_string(result, &(p_data_tbl[i].mailbox_name), 0, col_index++);
3256 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].mailbox_type), col_index++);
3257 _get_table_field_data_string(result, &(p_data_tbl[i].alias), 0, col_index++);
3258 _get_table_field_data_int(result, &(p_data_tbl[i].deleted_flag), col_index++);
3259 _get_table_field_data_int(result, &(p_data_tbl[i].modifiable_yn), col_index++);
3260 _get_table_field_data_int(result, &(p_data_tbl[i].total_mail_count_on_server), col_index++);
3261 _get_table_field_data_int(result, &(p_data_tbl[i].has_archived_mails), col_index++);
3262 _get_table_field_data_int(result, &(p_data_tbl[i].mail_slot_size), col_index++);
3263 _get_table_field_data_int(result, &(p_data_tbl[i].no_select), col_index++);
3264 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].last_sync_time), col_index++);
3265 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3266 _get_table_field_data_blob(result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3268 if (input_get_mail_count == 1) {
3269 _get_table_field_data_int(result, &(total_count), col_index++);
3270 p_data_tbl[i].total_mail_count_on_local = total_count;
3271 _get_table_field_data_int(result, &(read_count), col_index++);
3272 p_data_tbl[i].unread_count = total_count - read_count; /* return unread count, NOT */
3279 sqlite3_free_table(result);
3281 if (error == EMAIL_ERROR_NONE) {
3282 *output_mailbox_list = p_data_tbl;
3283 *output_mailbox_count = count;
3284 EM_DEBUG_LOG("Mailbox Count [%d]", count);
3287 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
3289 // sqlite3_db_release_memory(local_db_handle);
3291 EM_DEBUG_FUNC_END("error [%d]", error);
3295 /* Query series --------------------------------------------------------------*/
3296 INTERNAL_FUNC int emstorage_check_duplicated_account(char *multi_user_name, email_account_t* account, int transaction, int *err_code)
3298 EM_DEBUG_FUNC_BEGIN();
3299 int rc = -1, ret = false;
3300 int error = EMAIL_ERROR_NONE;
3303 char sql_query_string[QUERY_SIZE] = {0, };
3304 sqlite3 *local_db_handle = NULL;
3306 if (account == NULL || account->user_email_address == NULL || account->incoming_server_user_name == NULL || account->incoming_server_address == NULL ||
3307 account->outgoing_server_user_name == NULL || account->outgoing_server_address == NULL) {
3308 if (err_code != NULL)
3309 *err_code = EMAIL_ERROR_INVALID_PARAM;
3313 local_db_handle = emstorage_get_db_connection(multi_user_name);
3315 EMSTORAGE_START_READ_TRANSACTION(transaction);
3317 SNPRINTF(sql_query_string, sizeof(sql_query_string),
3318 "SELECT COUNT(*) FROM mail_account_tbl "
3320 " user_email_address = '%s' AND "
3321 " incoming_server_user_name = '%s' AND "
3322 " incoming_server_type = %d AND "
3323 " incoming_server_address = '%s' AND "
3324 " outgoing_server_user_name = '%s' AND "
3325 " outgoing_server_type = %d AND "
3326 " outgoing_server_address = '%s'; ",
3327 account->user_email_address,
3328 account->incoming_server_user_name, account->incoming_server_type, account->incoming_server_address,
3329 account->outgoing_server_user_name, account->outgoing_server_type, account->outgoing_server_address
3331 EM_DEBUG_LOG_SEC("Query[%s]", sql_query_string);
3332 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
3333 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3334 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3336 count = atoi(result[1]);
3337 sqlite3_free_table(result);
3339 EM_DEBUG_LOG("Count of Duplicated Account Information: [%d]", count);
3341 if (count == 0) { /* not duplicated account */
3343 EM_DEBUG_LOG_SEC("NOT duplicated account: user_email_address[%s]", account->user_email_address);
3344 } else { /* duplicated account */
3346 EM_DEBUG_LOG_SEC("The same account already exists. Duplicated account: user_email_address[%s]", account->user_email_address);
3347 error = EMAIL_ERROR_ALREADY_EXISTS;
3352 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3354 if (err_code != NULL)
3357 EM_DEBUG_FUNC_END("ret [%d]", ret);
3362 INTERNAL_FUNC int emstorage_get_account_count(char *multi_user_name, int *count, int transaction, int *err_code)
3364 EM_DEBUG_FUNC_BEGIN();
3367 if (err_code != NULL)
3368 *err_code = EMAIL_ERROR_INVALID_PARAM;
3372 int rc = -1, ret = false;
3373 int error = EMAIL_ERROR_NONE;
3375 DB_STMT hStmt = NULL;
3376 char sql_query_string[QUERY_SIZE] = {0, };
3379 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3380 EMSTORAGE_START_READ_TRANSACTION(transaction);
3381 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_account_tbl");
3382 EM_DEBUG_LOG_SEC("SQL STMT [%s]", sql_query_string);
3383 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string,
3384 EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3385 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
3386 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3387 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3389 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3390 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3391 ("sqlite3_step fail:%d", rc));
3393 *count = sqlite3_column_int(hStmt, 0);
3399 if (hStmt != NULL) {
3400 rc = sqlite3_finalize(hStmt);
3402 if (rc != SQLITE_OK) {
3403 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
3404 error = EMAIL_ERROR_DB_FAILURE;
3408 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3410 if (err_code != NULL)
3413 EM_DEBUG_FUNC_END("ret [%d]", ret);
3417 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)
3419 EM_DEBUG_FUNC_BEGIN();
3421 int i = 0, count = 0, rc = -1, ret = false;
3422 int field_index = 0;
3424 int error = EMAIL_ERROR_NONE;
3425 emstorage_account_tbl_t *p_data_tbl = NULL;
3426 char sql_query_string[QUERY_SIZE] = {0, };
3427 char *sql = "SELECT count(*) FROM mail_account_tbl;";
3429 sqlite3 *local_db_handle = NULL;
3430 DB_STMT hStmt = NULL;
3432 if (!select_num || !account_list) {
3433 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, account_list);
3434 if (err_code != NULL)
3435 *err_code = EMAIL_ERROR_INVALID_PARAM;
3439 local_db_handle = emstorage_get_db_connection(multi_user_name);
3441 EMSTORAGE_START_READ_TRANSACTION(transaction);
3443 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
3444 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3445 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
3447 count = atoi(result[1]);
3448 sqlite3_free_table(result);
3450 EM_DEBUG_LOG_DEV("count = %d", rc);
3453 EM_DEBUG_LOG("no account found...");
3454 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3458 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
3459 sql_len = EM_SAFE_STRLEN(sql_query_string);
3461 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
3466 "user_data_length, "
3470 "default_mail_slot_size, "
3473 "user_display_name, "
3474 "user_email_address, "
3475 "reply_to_address, "
3477 "incoming_server_type, "
3478 "incoming_server_address, "
3479 "incoming_server_port_number, "
3480 "incoming_server_user_name, "
3481 "incoming_server_password, "
3482 "incoming_server_secure_connection, "
3483 "incoming_server_authentication_method, "
3485 "keep_mails_on_pop_server_after_download, "
3487 "auto_download_size, "
3492 "outgoing_server_type, "
3493 "outgoing_server_address, "
3494 "outgoing_server_port_number, "
3495 "outgoing_server_user_name, "
3496 "outgoing_server_password, "
3497 "outgoing_server_secure_connection, "
3498 "outgoing_server_need_authentication, "
3499 "outgoing_server_use_same_authenticator, "
3502 "req_delivery_receipt, "
3503 "req_read_receipt, "
3507 "display_name_from, "
3509 "forward_with_files, "
3513 "add_my_address_to_bcc, "
3514 "notification_status, "
3516 "display_content_status, "
3517 "default_ringtone_status, "
3518 "alert_ringtone_path, "
3519 "auto_resend_times, "
3520 "outgoing_server_size_limit, "
3521 "wifi_auto_download, "
3523 "incoming_server_requires_apop, "
3525 "certificate_path, "
3530 sql_len = EM_SAFE_STRLEN(sql_query_string);
3532 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " FROM mail_account_tbl ORDER BY account_id");
3534 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3536 EM_DEBUG_LOG_DEV("After sqlite3_prepare_v2 hStmt = %p", hStmt);
3537 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3538 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3540 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3541 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3542 ("sqlite3_step fail:%d", rc));
3543 if (rc == SQLITE_DONE) {
3544 EM_DEBUG_EXCEPTION("no account found...");
3546 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3552 if (!(p_data_tbl = (emstorage_account_tbl_t*)malloc(sizeof(emstorage_account_tbl_t) * count))) {
3553 EM_DEBUG_EXCEPTION("malloc failed...");
3554 error = EMAIL_ERROR_OUT_OF_MEMORY;
3557 memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * count);
3558 for (i = 0; i < count; i++) {
3561 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_id), field_index++);
3562 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].account_name), 0, field_index++);
3563 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].logo_icon_path), 0, field_index++);
3564 _get_stmt_field_data_blob(hStmt, &(p_data_tbl[i].user_data), field_index++);
3565 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].user_data_length), field_index++);
3566 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_svc_id), field_index++);
3567 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_status), field_index++);
3568 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_disabled), field_index++);
3569 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].default_mail_slot_size), field_index++);
3570 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].roaming_option), field_index++);
3571 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].color_label), field_index++);
3572 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_display_name), 0, field_index++);
3573 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_email_address), 0, field_index++);
3574 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].reply_to_address), 0, field_index++);
3575 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].return_address), 0, field_index++);
3576 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].incoming_server_type), field_index++);
3577 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_address), 0, field_index++);
3578 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_port_number), field_index++);
3579 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_user_name), 0, field_index++);
3580 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_password), 0, field_index++);
3581 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_secure_connection), field_index++);
3582 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].incoming_server_authentication_method), field_index++);
3583 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].retrieval_mode), field_index++);
3584 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].keep_mails_on_pop_server_after_download), field_index++);
3585 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].check_interval), field_index++);
3586 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_download_size), field_index++);
3587 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_interval), field_index++);
3588 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_days), field_index++);
3589 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_start_time), field_index++);
3590 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_end_time), field_index++);
3591 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].outgoing_server_type), field_index++);
3592 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_address), 0, field_index++);
3593 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_port_number), field_index++);
3594 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_user_name), 0, field_index++);
3595 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_password), 0, field_index++);
3596 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_secure_connection), field_index++);
3597 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_need_authentication), field_index++);
3598 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_use_same_authenticator), field_index++);
3599 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.priority), field_index++);
3600 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.keep_local_copy), field_index++);
3601 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_delivery_receipt), field_index++);
3602 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_read_receipt), field_index++);
3603 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.download_limit), field_index++);
3604 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_address), field_index++);
3605 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_subject), field_index++);
3606 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.display_name_from), 0, field_index++);
3607 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.reply_with_body), field_index++);
3608 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.forward_with_files), field_index++);
3609 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_myname_card), field_index++);
3610 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_signature), field_index++);
3611 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.signature), 0, field_index++);
3612 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.add_my_address_to_bcc), field_index++);
3613 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.notification_status), field_index++);
3614 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.vibrate_status), field_index++);
3615 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.display_content_status), field_index++);
3616 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.default_ringtone_status), field_index++);
3617 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.alert_ringtone_path), 0, field_index++);
3618 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_resend_times), field_index++);
3619 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_size_limit), field_index++);
3620 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].wifi_auto_download), field_index++);
3621 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].pop_before_smtp), field_index++);
3622 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_requires_apop), field_index++);
3623 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].smime_type), field_index++);
3624 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].certificate_path), 0, field_index++);
3625 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].cipher_type), field_index++);
3626 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].digest_type), field_index++);
3628 /* EAS passwd is not accessible */
3629 if (with_password == true && p_data_tbl[i].incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
3630 /* get password from the secure storage */
3631 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3632 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3634 EM_SAFE_FREE(p_data_tbl[i].incoming_server_password);
3635 EM_SAFE_FREE(p_data_tbl[i].outgoing_server_password);
3637 /* get password file name */
3638 error = _get_password_file_name(multi_user_name, p_data_tbl[i].account_id,
3639 recv_password_file_name,
3640 send_password_file_name);
3641 if (error != EMAIL_ERROR_NONE) {
3642 EM_DEBUG_EXCEPTION("_get_password_file_name error [%d]", error);
3643 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3647 /* read password from secure storage */
3648 error = _read_password_from_secure_storage(recv_password_file_name,
3649 &(p_data_tbl[i].incoming_server_password));
3651 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s] error [%d]",
3652 recv_password_file_name, error);
3653 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3656 error = _read_password_from_secure_storage(send_password_file_name,
3657 &(p_data_tbl[i].outgoing_server_password));
3659 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s] error [%d]",
3660 send_password_file_name, error);
3661 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3666 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3667 EM_DEBUG_LOG_DEV("after sqlite3_step(), i = %d, rc = %d.", i, rc);
3668 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3669 ("sqlite3_step fail:%d", rc));
3676 *account_list = p_data_tbl;
3677 *select_num = count;
3678 EM_DEBUG_LOG_DEV("COUNT : %d", count);
3679 } else if (p_data_tbl != NULL)
3680 emstorage_free_account(&p_data_tbl, count, NULL);
3682 if (hStmt != NULL) {
3683 rc = sqlite3_finalize(hStmt);
3685 if (rc != SQLITE_OK) {
3686 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
3687 error = EMAIL_ERROR_DB_FAILURE;
3691 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3693 if (err_code != NULL)
3696 EM_DEBUG_FUNC_END("ret [%d]", ret);
3700 INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(char *multi_user_name,
3701 char *server_mail_id,
3703 emstorage_mail_tbl_t **mail,
3707 EM_DEBUG_FUNC_BEGIN_SEC("mailbox_id [%d], server_mail_id[%s], mail[%p], transaction[%d], err_code[%p]",
3708 mailbox_id, server_mail_id, mail, transaction, err_code);
3710 int ret = false, error = EMAIL_ERROR_NONE, result_count;
3711 char conditional_clause[QUERY_SIZE] = {0, };
3712 emstorage_mail_tbl_t* p_data_tbl = NULL;
3714 if (!server_mail_id || !mail) {
3715 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3716 error = EMAIL_ERROR_INVALID_PARAM;
3720 sqlite3_snprintf(sizeof(conditional_clause), conditional_clause, "WHERE server_mail_id = '%q'", server_mail_id);
3722 if (mailbox_id > 0) {
3723 SNPRINTF(conditional_clause + strlen(conditional_clause),
3724 QUERY_SIZE - strlen(conditional_clause),
3725 " AND mailbox_id = %d",
3729 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
3730 if (!emstorage_query_mail_tbl(multi_user_name,
3736 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3747 if (err_code != NULL)
3750 EM_DEBUG_FUNC_END("ret [%d]", ret);
3755 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)
3757 int cur_clause = 0, conditional_clause_count = 0, i = 0;
3759 if (account_id < ALL_ACCOUNT) {
3760 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3761 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3765 if (account_id != ALL_ACCOUNT) {
3766 cur_clause += (conditional_clause_count++ == 0) ?
3767 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE account_id = %d", account_id) :
3768 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND account_id = %d", account_id);
3771 if (mailbox_id > 0) {
3772 cur_clause += (conditional_clause_count++ == 0) ?
3773 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_id = %d", mailbox_id) :
3774 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_id = %d", mailbox_id);
3777 if (thread_id > 0) {
3778 cur_clause += (conditional_clause_count++ == 0) ?
3779 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_id = %d ", thread_id) :
3780 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_id = %d ", thread_id);
3781 } else if (thread_id == EMAIL_LIST_TYPE_THREAD) {
3782 cur_clause += (conditional_clause_count++ == 0) ?
3783 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_item_count > 0") :
3784 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_item_count > 0");
3785 } else if (thread_id == EMAIL_LIST_TYPE_LOCAL) {
3786 cur_clause += (conditional_clause_count++ == 0) ?
3787 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE server_mail_status = 0") :
3788 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND server_mail_status = 0");
3789 } else if (thread_id == EMAIL_LIST_TYPE_UNREAD) {
3790 cur_clause += (conditional_clause_count++ == 0) ?
3791 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_seen_field == 0") :
3792 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_seen_field == 0");
3795 /* EM_DEBUG_LOG("where clause added [%s]", conditional_clause_string); */
3796 if (addr_list && addr_list->address_count > 0) {
3797 if (!addr_list->address_type) {
3798 cur_clause += (conditional_clause_count++ == 0) ?
3799 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE email_address_sender IN(\"%s\"", addr_list->address_list[0]) :
3800 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND email_address_sender IN(\"%s\"", addr_list->address_list[0]);
3802 for (i = 1; i < addr_list->address_count; i++)
3803 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3805 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3807 cur_clause += (conditional_clause_count++ == 0) ?
3808 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]) :
3809 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]);
3811 for (i = 1; i < addr_list->address_count; i++)
3812 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3814 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3818 if (input_except_delete_flagged_mails) {
3819 cur_clause += (conditional_clause_count++ == 0) ?
3820 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_deleted_field = 0") :
3821 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_deleted_field = 0");
3825 switch (search_type) {
3826 case EMAIL_SEARCH_FILTER_SUBJECT:
3827 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3828 " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3830 case EMAIL_SEARCH_FILTER_SENDER:
3831 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3832 " %s ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3833 ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3835 case EMAIL_SEARCH_FILTER_RECIPIENT:
3836 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3837 " %s ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3838 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3839 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3840 ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value);
3842 case EMAIL_SEARCH_FILTER_ALL:
3843 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3844 " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\' "
3845 " OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3846 " OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3847 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3848 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3851 ")", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value, search_value, search_value);
3856 /* EM_DEBUG_LOG("where clause [%s]", conditional_clause_string); */
3857 static char sorting_str[][50] = {
3858 " ORDER BY date_time DESC", /* case EMAIL_SORT_DATETIME_HIGH: */
3859 " ORDER BY date_time ASC", /* case EMAIL_SORT_DATETIME_LOW: */
3860 " ORDER BY full_address_from DESC, date_time DESC", /* case EMAIL_SORT_SENDER_HIGH: */
3861 " ORDER BY full_address_from ASC, date_time DESC", /* case EMAIL_SORT_SENDER_LOW: */
3862 " ORDER BY full_address_to DESC, date_time DESC", /* case EMAIL_SORT_RCPT_HIGH: */
3863 " ORDER BY full_address_to ASC, date_time DESC", /* case EMAIL_SORT_RCPT_LOW: */
3864 " ORDER BY subject DESC, date_time DESC", /* case EMAIL_SORT_SUBJECT_HIGH: */
3865 " ORDER BY subject ASC, date_time DESC", /* case EMAIL_SORT_SUBJECT_LOW: */
3866 " ORDER BY priority DESC, date_time DESC", /* case EMAIL_SORT_PRIORITY_HIGH: */
3867 " ORDER BY priority ASC, date_time DESC", /* case EMAIL_SORT_PRIORITY_LOW: */
3868 " ORDER BY attachment_count DESC, date_time DESC", /* case EMAIL_SORT_ATTACHMENT_HIGH: */
3869 " ORDER BY attachment_count ASC, date_time DESC", /* case EMAIL_SORT_ATTACHMENT_LOW: */
3870 " ORDER BY lock_status DESC, date_time DESC", /* case EMAIL_SORT_FAVORITE_HIGH: */
3871 " ORDER BY lock_status ASC, date_time DESC", /* case EMAIL_SORT_FAVORITE_LOW: */
3872 " ORDER BY mailbox_id DESC, date_time DESC", /* case EMAIL_SORT_MAILBOX_ID_HIGH: */
3873 " ORDER BY mailbox_id ASC, date_time DESC", /* case EMAIL_SORT_MAILBOX_ID_LOW: */
3874 " ORDER BY flags_flagged_field DESC, date_time DESC", /* case EMAIL_SORT_FLAGGED_FLAG_HIGH: */
3875 " ORDER BY flags_flagged_field ASC, date_time DESC", /* case EMAIL_SORT_FLAGGED_FLAG_LOW: */
3876 " ORDER BY flags_seen_field DESC, date_time DESC", /* case EMAIL_SORT_SEEN_FLAG_HIGH: */
3877 " ORDER BY flags_seen_field ASC, date_time DESC" /* case EMAIL_SORT_SEEN_FLAG_LOW: */
3880 if (sorting < EMAIL_SORT_END && sorting >= 0)
3881 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " %s", sorting_str[sorting]);
3883 EM_DEBUG_LOG(" Invalid Sorting order ");
3886 if (start_index != -1 && limit_count != -1)
3887 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " LIMIT %d, %d", start_index, limit_count);
3894 * emstorage_get_mail_list - Get the mail list information.
3898 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)
3900 EM_DEBUG_FUNC_BEGIN();
3901 EM_PROFILE_BEGIN(emstorage_get_mail_list_func);
3903 int ret = false, error = EMAIL_ERROR_NONE;
3904 char conditional_clause_string[QUERY_SIZE] = { 0, };
3906 if (account_id < ALL_ACCOUNT) {
3907 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3908 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3910 EM_IF_NULL_RETURN_VALUE(result_count, false);
3912 _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);
3914 EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
3916 if (!emstorage_query_mail_list(multi_user_name, conditional_clause_string, transaction, mail_list, result_count, &error)) {
3917 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
3924 if (err_code != NULL)
3927 EM_PROFILE_END(emstorage_get_mail_list_func);
3928 EM_DEBUG_FUNC_END("ret [%d]", ret);
3934 * emstorage_get_mails - Get the Mail list information based on mailbox_name name
3938 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)
3940 EM_DEBUG_FUNC_BEGIN();
3941 EM_PROFILE_BEGIN(emStorageGetMails);
3943 int count = 0, ret = false, error = EMAIL_ERROR_NONE;
3944 emstorage_mail_tbl_t *p_data_tbl = NULL;
3945 char conditional_clause_string[QUERY_SIZE] = {0, };
3947 EM_IF_NULL_RETURN_VALUE(mail_list, false);
3948 EM_IF_NULL_RETURN_VALUE(result_count, false);
3950 if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3951 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3952 if (err_code != NULL)
3953 *err_code = EMAIL_ERROR_INVALID_PARAM;
3957 _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);
3959 EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
3961 if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause_string, transaction, &p_data_tbl, &count, &error)) {
3962 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3971 *mail_list = p_data_tbl;
3972 *result_count = count;
3973 EM_DEBUG_LOG("COUNT : %d", count);
3974 } else if (p_data_tbl != NULL)
3975 emstorage_free_mail(&p_data_tbl, count, NULL);
3977 if (err_code != NULL)
3980 EM_PROFILE_END(emStorageGetMails);
3981 EM_DEBUG_FUNC_END("ret [%d]", ret);
3988 * emstorage_get_searched_mail_list - Get the mail list information after filtering
3992 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)
3994 EM_DEBUG_FUNC_BEGIN();
3996 int ret = false, error = EMAIL_ERROR_NONE;
3997 char conditional_clause[QUERY_SIZE] = {0, };
3998 char *temp_search_value = NULL;
3999 char *temp_search_value2 = NULL;
4001 EM_IF_NULL_RETURN_VALUE(mail_list, false);
4002 EM_IF_NULL_RETURN_VALUE(result_count, false);
4004 if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
4005 EM_DEBUG_EXCEPTION("select_num[%p], Mail_list[%p]", result_count, mail_list);
4006 error = EMAIL_ERROR_INVALID_PARAM;
4010 temp_search_value = em_replace_all_string((char*)search_value, "_", "\\_");
4011 temp_search_value2 = em_replace_all_string(temp_search_value, "%", "\\%");
4013 _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);
4015 EM_DEBUG_LOG("conditional_clause[%s]", conditional_clause);
4017 if (!emstorage_query_mail_list(multi_user_name, conditional_clause, transaction, mail_list, result_count, &error)) {
4018 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
4025 EM_DEBUG_LOG("emstorage_get_searched_mail_list finish off");
4027 if (err_code != NULL)
4030 EM_SAFE_FREE(temp_search_value);
4031 EM_SAFE_FREE(temp_search_value2);
4033 EM_DEBUG_FUNC_END("ret [%d]", ret);
4038 static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name)
4040 EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
4042 if (account_id <= 0 || !recv_password_file_name || !send_password_file_name) {
4043 EM_DEBUG_EXCEPTION("Invalid parameter");
4044 return EMAIL_ERROR_INVALID_PARAM;
4047 EM_DEBUG_LOG("MULTI_USER_NAME : [%s]", multi_user_name);
4049 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
4050 snprintf(recv_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_recv_%s", account_id, multi_user_name);
4051 snprintf(send_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_send_%s", account_id, multi_user_name);
4053 snprintf(recv_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_recv", account_id);
4054 snprintf(send_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_send", account_id);
4057 EM_DEBUG_FUNC_END();
4058 return EMAIL_ERROR_NONE;
4061 static int _read_password_from_secure_storage(char *file_name, char **password)
4063 EM_DEBUG_FUNC_BEGIN_SEC("file_name[%s]", file_name);
4065 if (!file_name || !password) {
4066 EM_DEBUG_EXCEPTION("Invalid Parameter");
4067 return EMAIL_ERROR_INVALID_PARAM;
4070 int ret = EMAIL_ERROR_NONE;
4072 ret = emcore_get_password_in_key_manager(file_name, password);
4073 if (ret != EMAIL_ERROR_NONE) {
4074 EM_DEBUG_EXCEPTION("emcore_get_password_in_key_manager failed : [%d]", ret);
4080 EM_DEBUG_FUNC_END("ret [%d]", ret);
4084 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)
4086 EM_DEBUG_FUNC_BEGIN("account_id[%d], pulloption[%d], account[%p], transaction[%d], err_code[%p]", account_id, pulloption, account, transaction, err_code);
4089 EM_DEBUG_EXCEPTION("account_id[%d], account[%p]", account_id, account);
4090 if (err_code != NULL)
4091 *err_code = EMAIL_ERROR_INVALID_PARAM;
4096 int error = EMAIL_ERROR_NONE;
4097 emstorage_account_tbl_t* p_data_tbl = NULL;
4098 DB_STMT hStmt = NULL;
4099 char sql_query_string[QUERY_SIZE] = {0, };
4102 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4103 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4105 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4107 EMSTORAGE_START_READ_TRANSACTION(transaction);
4109 /* Make query string */
4110 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
4111 sql_len = EM_SAFE_STRLEN(sql_query_string);
4113 if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4114 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4115 "incoming_server_type,"
4116 "incoming_server_address,"
4117 "user_email_address,"
4118 "incoming_server_user_name,"
4120 "incoming_server_port_number,"
4121 "incoming_server_secure_connection,"
4122 "incoming_server_authentication_method,"
4123 "outgoing_server_type,"
4124 "outgoing_server_address,"
4125 "outgoing_server_port_number,"
4126 "outgoing_server_need_authentication,"
4127 "outgoing_server_secure_connection,"
4128 "outgoing_server_user_name,"
4129 "user_display_name,"
4133 "keep_mails_on_pop_server_after_download,"
4134 "auto_download_size,"
4139 "outgoing_server_use_same_authenticator,"
4140 "auto_resend_times,"
4141 "outgoing_server_size_limit,"
4142 "wifi_auto_download,"
4144 "incoming_server_requires_apop,"
4151 sql_len = EM_SAFE_STRLEN(sql_query_string);
4154 if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME) {
4155 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " account_name, ");
4156 sql_len = EM_SAFE_STRLEN(sql_query_string);
4159 /* get from secure storage, not from db */
4160 if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4161 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4164 "req_delivery_receipt,"
4169 "display_name_from,"
4171 "forward_with_files,"
4175 "add_my_address_to_bcc,"
4176 "notification_status,"
4178 "display_content_status,"
4179 "default_ringtone_status,"
4180 "alert_ringtone_path,"
4183 "default_mail_slot_size,"
4190 sql_len = EM_SAFE_STRLEN(sql_query_string);
4193 /* dummy value, FROM WHERE clause */
4194 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "0 FROM mail_account_tbl WHERE account_id = %d", account_id);
4197 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
4199 /* execute a sql and count rows */
4200 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4201 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4202 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4204 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4205 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4206 ("sqlite3_step fail:%d", rc));
4208 if (rc == SQLITE_DONE) {
4209 EM_DEBUG_EXCEPTION("no matched account found...");
4210 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4214 /* Assign query result to structure */
4215 if (!(p_data_tbl = (emstorage_account_tbl_t *)malloc(sizeof(emstorage_account_tbl_t) * 1))) {
4216 EM_DEBUG_EXCEPTION("malloc failed...");
4217 error = EMAIL_ERROR_OUT_OF_MEMORY;
4221 memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * 1);
4224 if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4225 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_type), col_index++);
4226 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_address), 0, col_index++);
4227 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_email_address), 0, col_index++);
4228 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_user_name), 0, col_index++);
4229 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->retrieval_mode), col_index++);
4230 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_port_number), col_index++);
4231 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_secure_connection), col_index++);
4232 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_authentication_method), col_index++);
4233 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->outgoing_server_type), col_index++);
4234 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_address), 0, col_index++);
4235 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_port_number), col_index++);
4236 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_need_authentication), col_index++);
4237 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_secure_connection), col_index++);
4238 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_user_name), 0, col_index++);
4239 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_display_name), 0, col_index++);
4240 _get_stmt_field_data_string(hStmt, &(p_data_tbl->reply_to_address), 0, col_index++);
4241 _get_stmt_field_data_string(hStmt, &(p_data_tbl->return_address), 0, col_index++);
4242 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
4243 _get_stmt_field_data_int(hStmt, &(p_data_tbl->keep_mails_on_pop_server_after_download), col_index++);
4244 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_download_size), col_index++);
4245 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_interval), col_index++);
4246 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_days), col_index++);
4247 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_start_time), col_index++);
4248 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_end_time), col_index++);
4249 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_use_same_authenticator), col_index++);
4250 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_resend_times), col_index++);
4251 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_size_limit), col_index++);
4252 _get_stmt_field_data_int(hStmt, &(p_data_tbl->wifi_auto_download), col_index++);
4253 _get_stmt_field_data_int(hStmt, &(p_data_tbl->pop_before_smtp), col_index++);
4254 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_requires_apop), col_index++);
4255 _get_stmt_field_data_string(hStmt, &(p_data_tbl->logo_icon_path), 0, col_index++);
4256 _get_stmt_field_data_blob(hStmt, &p_data_tbl->user_data, col_index++);
4257 _get_stmt_field_data_int(hStmt, &p_data_tbl->user_data_length, col_index++);
4258 _get_stmt_field_data_int(hStmt, &p_data_tbl->color_label, col_index++);
4259 _get_stmt_field_data_int(hStmt, &(p_data_tbl->check_interval), col_index++);
4260 _get_stmt_field_data_int(hStmt, &(p_data_tbl->sync_status), col_index++);
4263 if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME)
4264 _get_stmt_field_data_string(hStmt, &(p_data_tbl->account_name), 0, col_index++);
4266 if (pulloption & EMAIL_ACC_GET_OPT_PASSWORD) {
4267 /* get password file name */
4268 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) {
4269 EM_DEBUG_EXCEPTION("_get_password_file_name failed [%d]", error);
4273 /* read password from secure storage */
4274 if ((error = _read_password_from_secure_storage(recv_password_file_name, &(p_data_tbl->incoming_server_password))) < 0) {
4275 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4279 EM_DEBUG_LOG_DEV("recv_password_file_name[%s], password[%s]", recv_password_file_name, p_data_tbl->incoming_server_password);
4281 if (p_data_tbl->outgoing_server_use_same_authenticator == 0) {
4282 if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl->outgoing_server_password))) < 0) {
4283 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4286 EM_DEBUG_LOG_DEV("send_password_file_name[%s], password[%s]", send_password_file_name, p_data_tbl->outgoing_server_password);
4290 if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4291 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.priority), col_index++);
4292 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.keep_local_copy), col_index++);
4293 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_delivery_receipt), col_index++);
4294 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_read_receipt), col_index++);
4295 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.download_limit), col_index++);
4296 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_address), col_index++);
4297 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_subject), col_index++);
4298 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.display_name_from), 0, col_index++);
4299 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.reply_with_body), col_index++);
4300 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.forward_with_files), col_index++);
4301 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_myname_card), col_index++);
4302 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_signature), col_index++);
4303 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.signature), 0, col_index++);
4304 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.add_my_address_to_bcc), col_index++);
4305 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.notification_status), col_index++);
4306 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.vibrate_status), col_index++);
4307 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.display_content_status), col_index++);
4308 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.default_ringtone_status), col_index++);
4309 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.alert_ringtone_path), 0, col_index++);
4310 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_svc_id), col_index++);
4311 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->sync_disabled), col_index++);
4312 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->default_mail_slot_size), col_index++);
4313 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->roaming_option), col_index++);
4314 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->smime_type), col_index++);
4315 _get_stmt_field_data_string(hStmt, &(p_data_tbl->certificate_path), 0, col_index++);
4316 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->cipher_type), col_index++);
4317 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->digest_type), col_index++);
4324 *account = p_data_tbl;
4327 emstorage_free_account((emstorage_account_tbl_t **)&p_data_tbl, 1, NULL);
4329 if (hStmt != NULL) {
4330 rc = sqlite3_finalize(hStmt);
4331 if (rc != SQLITE_OK) {
4332 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
4333 error = EMAIL_ERROR_DB_FAILURE;
4337 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4339 if (err_code != NULL)
4342 EM_DEBUG_FUNC_END("ret [%d]", ret);
4346 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)
4348 EM_DEBUG_FUNC_BEGIN("account_id[%d], password_length[%p], err_code[%p]", account_id, password_length, err_code);
4350 if (account_id <= 0 || password_length == NULL) {
4351 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4352 if (err_code != NULL)
4353 *err_code = EMAIL_ERROR_INVALID_PARAM;
4358 int error = EMAIL_ERROR_NONE;
4359 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4360 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4361 char *temp_password = NULL;
4364 /* get password file name */
4365 if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4366 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4370 /* read password from secure storage */
4371 if (password_type == EMAIL_GET_INCOMING_PASSWORD_LENGTH) {
4372 if ((error = _read_password_from_secure_storage(recv_password_file_name, &temp_password)) < 0 || !temp_password) {
4373 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
4376 } else if (password_type == EMAIL_GET_OUTGOING_PASSWORD_LENGTH) {
4377 if ((error = _read_password_from_secure_storage(send_password_file_name, &temp_password)) < 0 || !temp_password) {
4378 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
4382 EM_DEBUG_LOG("Invalid password type");
4383 if (err_code != NULL)
4384 *err_code = EMAIL_ERROR_INVALID_PARAM;
4388 *password_length = EM_SAFE_STRLEN(temp_password);
4390 EM_DEBUG_LOG_SEC("recv_password_file_name[%s], *password_length[%d]", recv_password_file_name, *password_length);
4395 EM_SAFE_FREE(temp_password);
4397 if (err_code != NULL)
4400 EM_DEBUG_FUNC_END("ret [%d]", ret);
4404 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)
4406 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);
4408 int err = EMAIL_ERROR_NONE;
4409 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4410 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4412 if (input_incoming_server_password == NULL && input_outgoing_server_password == NULL) {
4413 EM_DEBUG_EXCEPTION_SEC("Invalid param");
4414 err = EMAIL_ERROR_INVALID_PARAM;
4418 /* get password file name */
4419 if ((err = _get_password_file_name(multi_user_name,
4421 recv_password_file_name,
4422 send_password_file_name)) != EMAIL_ERROR_NONE) {
4423 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4427 EM_DEBUG_LOG_SEC("recv_password_file_name [%s] input_incoming_server_password [%s]",
4428 recv_password_file_name, input_incoming_server_password);
4429 EM_DEBUG_LOG_SEC("send_password_file_name [%s] input_outgoing_server_password [%s]",
4430 send_password_file_name, input_outgoing_server_password);
4432 if (input_incoming_server_password) {
4433 err = emcore_remove_password_in_key_manager(recv_password_file_name);
4434 if (err != EMAIL_ERROR_NONE) {
4435 EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", recv_password_file_name);
4439 /* save recv passwords to the secure storage */
4440 err = emcore_add_password_in_key_manager(recv_password_file_name, input_incoming_server_password);
4441 if (err != EMAIL_ERROR_NONE) {
4442 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4447 if (input_outgoing_server_password) {
4448 err = emcore_remove_password_in_key_manager(send_password_file_name);
4449 if (err != EMAIL_ERROR_NONE) {
4450 EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", send_password_file_name);
4454 /* save send passwords to the secure storage */
4455 err = emcore_add_password_in_key_manager(send_password_file_name, input_outgoing_server_password);
4456 if (err != EMAIL_ERROR_NONE) {
4457 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4463 EM_DEBUG_FUNC_END("err [%d]", err);
4467 INTERNAL_FUNC int emstorage_update_account(char *multi_user_name, int account_id, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
4469 EM_DEBUG_FUNC_BEGIN("account_id[%d], account[%p], transaction[%d], err_code[%p]", account_id, account_tbl, transaction, err_code);
4471 if (account_id < FIRST_ACCOUNT_ID || !account_tbl) {
4472 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4473 if (err_code != NULL)
4474 *err_code = EMAIL_ERROR_INVALID_PARAM;
4478 int error = EMAIL_ERROR_NONE;
4479 int rc, ret = false;
4481 DB_STMT hStmt = NULL;
4482 char sql_query_string[QUERY_SIZE] = {0, };
4484 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4486 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4488 SNPRINTF(sql_query_string, sizeof(sql_query_string),
4489 "UPDATE mail_account_tbl SET"
4491 ", logo_icon_path = ?"
4493 ", user_data_length = ?"
4494 ", account_svc_id = ?"
4496 ", sync_disabled = ?"
4497 ", default_mail_slot_size = ?"
4498 ", roaming_option = ?"
4500 ", user_display_name = ?"
4501 ", user_email_address = ?"
4502 ", reply_to_address = ?"
4503 ", return_address = ?"
4504 ", incoming_server_type = ?"
4505 ", incoming_server_address = ?"
4506 ", incoming_server_port_number = ?"
4507 ", incoming_server_user_name = ?"
4508 ", incoming_server_secure_connection = ?"
4509 ", incoming_server_authentication_method = ?"
4510 ", retrieval_mode = ?"
4511 ", keep_mails_on_pop_server_after_download = ?"
4512 ", check_interval = ?"
4513 ", auto_download_size = ?"
4514 ", peak_interval = ?"
4516 ", peak_start_time = ?"
4517 ", peak_end_time = ?"
4518 ", outgoing_server_type = ?"
4519 ", outgoing_server_address = ?"
4520 ", outgoing_server_port_number = ?"
4521 ", outgoing_server_user_name = ?"
4522 ", outgoing_server_secure_connection = ?"
4523 ", outgoing_server_need_authentication = ?"
4524 ", outgoing_server_use_same_authenticator = ?"
4526 ", keep_local_copy = ?"
4527 ", req_delivery_receipt = ?"
4528 ", req_read_receipt = ?"
4529 ", download_limit = ?"
4530 ", block_address = ?"
4531 ", block_subject = ?"
4532 ", display_name_from = ?"
4533 ", reply_with_body = ?"
4534 ", forward_with_files = ?"
4535 ", add_myname_card = ?"
4536 ", add_signature = ?"
4538 ", add_my_address_to_bcc = ?"
4539 ", notification_status = ?"
4540 ", vibrate_status = ?"
4541 ", display_content_status = ?"
4542 ", default_ringtone_status = ?"
4543 ", alert_ringtone_path = ?"
4544 ", auto_resend_times = ?"
4545 ", outgoing_server_size_limit = ?"
4546 ", wifi_auto_download = ?"
4547 ", pop_before_smtp = ?"
4548 ", incoming_server_requires_apop = ?"
4550 ", certificate_path = ?"
4554 " WHERE account_id = ?");
4556 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4557 EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
4558 EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
4559 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4560 ("sqlite3_prepare fail:(%d) %s", rc, sqlite3_errmsg(local_db_handle)));
4564 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4565 _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4566 _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4567 _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4568 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4569 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4570 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4571 _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4572 _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
4573 _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
4574 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4575 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4576 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4577 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4578 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4579 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4580 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
4581 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4582 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
4583 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
4584 _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
4585 _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
4586 _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
4587 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
4588 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
4589 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
4590 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
4591 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
4592 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
4593 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4594 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
4595 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
4596 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
4597 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
4598 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
4599 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
4600 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
4601 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
4602 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
4603 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
4604 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
4605 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
4606 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4607 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
4608 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
4609 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
4610 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
4611 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
4612 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
4613 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
4614 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
4615 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
4616 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
4617 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4618 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
4619 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
4620 _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
4621 _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
4622 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
4623 _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
4624 _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4625 _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
4626 _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
4627 _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4628 _bind_stmt_field_data_int(hStmt, i++, account_id);
4630 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4631 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
4632 ("sqlite3_step fail:%d", rc));
4633 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4634 ("sqlite3_step fail:%d", rc));
4636 /* validate account existence */
4637 rc = sqlite3_changes(local_db_handle);
4639 EM_DEBUG_EXCEPTION("no matched account found...");
4640 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4644 if (account_tbl->incoming_server_password || account_tbl->outgoing_server_password) {
4645 if ((error = emstorage_update_account_password(multi_user_name, account_id, account_tbl->incoming_server_password, account_tbl->outgoing_server_password)) != EMAIL_ERROR_NONE) {
4646 EM_DEBUG_EXCEPTION("emstorage_update_account_password failed [%d]", error);
4654 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4656 if (hStmt != NULL) {
4657 rc = sqlite3_finalize(hStmt);
4658 if (rc != SQLITE_OK) {
4659 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
4660 error = EMAIL_ERROR_DB_FAILURE;
4664 if (error == EMAIL_ERROR_NONE) {
4665 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_tbl->account_id, 0, NULL, 0))
4666 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_ACCOUNT_UPDATE] : Notification Failed >>> ");
4669 if (err_code != NULL)
4672 EM_DEBUG_FUNC_END("ret [%d]", ret);
4676 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)
4678 EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], field_name[%s], value[%d], transaction[%d]", account_id, field_name, value, transaction);
4679 int error = EMAIL_ERROR_NONE;
4681 char sql_query_string[QUERY_SIZE] = {0, };
4682 sqlite3 *local_db_handle = NULL;
4684 if (!account_id || !field_name) {
4685 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4686 return EMAIL_ERROR_INVALID_PARAM;
4689 local_db_handle = emstorage_get_db_connection(multi_user_name);
4691 /* Write query string */
4692 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_account_tbl SET %s = %d WHERE account_id = %d", field_name, value, account_id);
4694 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4697 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4699 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4700 if (error != EMAIL_ERROR_NONE) {
4701 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4705 if (sqlite3_changes(local_db_handle) == 0)
4706 EM_DEBUG_LOG("no mail matched...");
4710 result = (error == EMAIL_ERROR_NONE) ? true : false;
4711 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, error);
4713 if (error == EMAIL_ERROR_NONE) {
4714 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_id, 0, field_name, value))
4715 EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_ACCOUNT_UPDATE [%s,%d]", field_name, value);
4717 EM_DEBUG_FUNC_END("error [%d]", error);
4721 INTERNAL_FUNC int emstorage_get_sync_status_of_account(char *multi_user_name, int account_id, int *result_sync_status, int *err_code)
4723 EM_DEBUG_FUNC_BEGIN("account_id[%d], result_sync_status [%p], err_code[%p]", account_id, result_sync_status, err_code);
4725 if (!result_sync_status) {
4726 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4727 if (err_code != NULL)
4728 *err_code = EMAIL_ERROR_INVALID_PARAM;
4732 int error = EMAIL_ERROR_NONE, rc, ret = false, sync_status, count, i, col_index;
4733 char sql_query_string[QUERY_SIZE] = {0, };
4734 char **result = NULL;
4735 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4738 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl WHERE account_id = %d", account_id);
4740 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl");
4742 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
4743 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
4744 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4747 EM_DEBUG_EXCEPTION("no matched account found...");
4748 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4753 *result_sync_status = 0;
4755 for (i = 0; i < count; i++) {
4756 _get_table_field_data_int(result, &sync_status, col_index++);
4757 *result_sync_status |= sync_status;
4760 EM_DEBUG_LOG("sync_status [%d]", sync_status);
4762 sqlite3_free_table(result);
4769 if (err_code != NULL)
4772 EM_DEBUG_FUNC_END("ret [%d]", ret);
4776 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)
4778 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);
4780 int error = EMAIL_ERROR_NONE, rc, ret = false, set_value = sync_status, result_sync_status;
4781 char sql_query_string[QUERY_SIZE] = {0, };
4782 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4784 if (set_operator != SET_TYPE_SET && account_id) {
4785 if (!emstorage_get_sync_status_of_account(multi_user_name, account_id, &result_sync_status, &error)) {
4786 EM_DEBUG_EXCEPTION("emstorage_get_sync_status_of_account failed [%d]", error);
4787 if (err_code != NULL)
4791 switch (set_operator) {
4792 case SET_TYPE_UNION:
4793 set_value = result_sync_status | set_value;
4795 case SET_TYPE_MINUS:
4796 set_value = result_sync_status & (~set_value);
4799 EM_DEBUG_EXCEPTION("EMAIL_ERROR_NOT_SUPPORTED [%d]", set_operator);
4800 error = EMAIL_ERROR_NOT_SUPPORTED;
4803 EM_DEBUG_LOG("set_value [%d]", set_value);
4806 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4809 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE account_id = %d", set_value, account_id);
4811 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE incoming_server_type <> 5", set_value);
4813 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4815 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4816 if (error != EMAIL_ERROR_NONE) {
4817 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4821 rc = sqlite3_changes(local_db_handle);
4824 EM_DEBUG_EXCEPTION("no matched account found...");
4825 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4832 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4834 if (error == EMAIL_ERROR_NONE) {
4835 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE_SYNC_STATUS, account_id, set_value, NULL, 0))
4836 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_UPDATE_SYNC_STATUS] : Notification failed");
4839 if (err_code != NULL)
4842 EM_DEBUG_FUNC_END("ret [%d]", ret);
4846 INTERNAL_FUNC int emstorage_add_account(char *multi_user_name, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
4848 EM_DEBUG_FUNC_BEGIN("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4851 EM_DEBUG_EXCEPTION("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4852 if (err_code != NULL)
4853 *err_code = EMAIL_ERROR_INVALID_PARAM;
4859 int rc = -1, ret = false;
4860 int error = EMAIL_ERROR_NONE;
4861 DB_STMT hStmt = NULL;
4862 char sql_query_string[QUERY_SIZE] = {0, };
4863 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4864 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4866 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4868 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4870 char *sql = "SELECT rowid FROM mail_account_tbl;";
4871 char **result = NULL;
4873 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, &row_count, NULL, NULL), rc);
4874 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
4875 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
4877 if (NULL == result[1]) rc = 1;
4879 for (i = 1; i <= row_count; i++) {
4880 if (i != atoi(result[i])) {
4887 sqlite3_free_table(result);
4890 if (rc < 0 || rc > EMAIL_ACCOUNT_MAX) {
4891 EM_DEBUG_EXCEPTION("OVERFLOWED THE MAX ACCOUNT");
4892 error = EMAIL_ERROR_ACCOUNT_MAX_COUNT;
4896 account_tbl->account_id = rc;
4898 if ((error = _get_password_file_name(multi_user_name, account_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4899 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4903 EM_DEBUG_LOG(" >>>> ACCOUNT_ID [ %d ] ", account_tbl->account_id);
4904 SNPRINTF(sql_query_string, sizeof(sql_query_string),
4905 "INSERT INTO mail_account_tbl VALUES "
4907 " ? " /* account_id */
4908 " , ? " /* account_name */
4909 " , ? " /* logo_icon_path */
4910 " , ? " /* user_data */
4911 " , ? " /* user_data_length */
4912 " , ? " /* account_svc_id */
4913 " , ? " /* sync_status */
4914 " , ? " /* sync_disabled */
4915 " , ? " /* default_mail_slot_size */
4916 " , ? " /* roaming_option */
4917 " , ? " /* color_label */
4918 " , ? " /* user_display_name */
4919 " , ? " /* user_email_address */
4920 " , ? " /* reply_to_address */
4921 " , ? " /* return_address */
4922 " , ? " /* incoming_server_type */
4923 " , ? " /* incoming_server_address */
4924 " , ? " /* incoming_server_port_number */
4925 " , ? " /* incoming_server_user_name */
4926 " , ? " /* incoming_server_password */
4927 " , ? " /* incoming_server_secure_connection */
4928 " , ? " /* incoming_server_authentication_method */
4929 " , ? " /* retrieval_mode */
4930 " , ? " /* keep_mails_on_pop_server_after_download */
4931 " , ? " /* check_interval */
4932 " , ? " /* auto_download_size */
4933 " , ? " /* peak_interval */
4934 " , ? " /* peak_days */
4935 " , ? " /* peak_start_time */
4936 " , ? " /* peak_end_time */
4937 " , ? " /* outgoing_server_type */
4938 " , ? " /* outgoing_server_address */
4939 " , ? " /* outgoing_server_port_number */
4940 " , ? " /* outgoing_server_user_name */
4941 " , ? " /* outgoing_server_password */
4942 " , ? " /* outgoing_server_secure_connection */
4943 " , ? " /* outgoing_server_need_authentication */
4944 " , ? " /* outgoing_server_use_same_authenticator */
4945 " , ? " /* priority */
4946 " , ? " /* keep_local_copy */
4947 " , ? " /* req_delivery_receipt */
4948 " , ? " /* req_read_receipt */
4949 " , ? " /* download_limit */
4950 " , ? " /* block_address */
4951 " , ? " /* block_subject */
4952 " , ? " /* display_name_from */
4953 " , ? " /* reply_with_body */
4954 " , ? " /* forward_with_files */
4955 " , ? " /* add_myname_card */
4956 " , ? " /* add_signature */
4957 " , ? " /* signature */
4958 " , ? " /* add_my_address_to_bcc */
4959 " , ? " /* auto_resend_times */
4960 " , ? " /* outgoing_server_size_limit */
4961 " , ? " /* wifi_auto_download */
4962 " , ? " /* pop_before_smtp */
4963 " , ? " /* incoming_server_requires_apop */
4964 " , ? " /* smime_type */
4965 " , ? " /* certificate_path */
4966 " , ? " /* cipher_type */
4967 " , ? " /* digest_type */
4968 " , ? " /* notification_status */
4969 " , ? " /* vibrate_status */
4970 " , ? " /* display_content_status */
4971 " , ? " /* default_ringtone_status */
4972 " , ? " /* alert_ringtone_path */
4973 " , ? " /* user_name */
4976 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4977 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4978 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4982 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_id);
4983 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4984 _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4985 _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4986 _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4987 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4988 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4989 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4990 _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4991 _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
4992 _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
4993 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4994 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4995 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4996 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4997 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4998 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4999 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
5000 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
5001 _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
5002 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
5003 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
5004 _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
5005 _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
5006 _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
5007 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
5008 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
5009 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
5010 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
5011 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
5012 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
5013 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5014 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
5015 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
5016 _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
5017 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
5018 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
5019 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
5020 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
5021 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
5022 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
5023 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
5024 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
5025 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
5026 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
5027 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
5028 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
5029 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
5030 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
5031 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
5032 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
5033 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
5034 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
5035 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
5036 _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
5037 _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
5038 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
5039 _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
5040 _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
5041 _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
5042 _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
5043 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
5044 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
5045 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
5046 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
5047 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5048 _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5050 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5052 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
5053 ("sqlite3_step fail:%d", rc));
5054 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5055 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
5058 /* save passwords to the secure storage */
5059 EM_DEBUG_LOG_SEC("save to the secure storage : recv_file[%s], send_file[%s]", recv_password_file_name, send_password_file_name);
5060 error = emcore_add_password_in_key_manager(recv_password_file_name, account_tbl->incoming_server_password);
5061 if (error != EMAIL_ERROR_NONE) {
5062 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
5066 error = emcore_add_password_in_key_manager(send_password_file_name, account_tbl->outgoing_server_password);
5067 if (error != EMAIL_ERROR_NONE) {
5068 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
5075 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5077 if (hStmt != NULL) {
5078 rc = sqlite3_finalize(hStmt);
5079 if (rc != SQLITE_OK) {
5080 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5081 error = EMAIL_ERROR_DB_FAILURE;
5085 if (error == EMAIL_ERROR_NONE) {
5086 if (!emcore_notify_storage_event(NOTI_ACCOUNT_ADD, account_tbl->account_id, 0, NULL, 0))
5087 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_ADD] : Notification failed");
5090 if (err_code != NULL)
5093 EM_DEBUG_FUNC_END("ret [%d]", ret);
5097 INTERNAL_FUNC int emstorage_delete_account(char *multi_user_name, int account_id, int transaction, int *err_code)
5099 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
5101 if (account_id < FIRST_ACCOUNT_ID) {
5102 EM_DEBUG_EXCEPTION(" account_id[%d]", account_id);
5104 if (err_code != NULL)
5105 *err_code = EMAIL_ERROR_INVALID_PARAM;
5109 int rc = -1, ret = false;
5110 int error = EMAIL_ERROR_NONE;
5112 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5114 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5116 /* TODO : delete password files - file names can be obtained from db or a rule that makes a name */
5117 char sql_query_string[QUERY_SIZE] = {0, };
5118 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5119 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5121 /* get password file name */
5122 if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
5123 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
5127 /* delete from db */
5128 memset(sql_query_string, 0x00, sizeof(sql_query_string));
5129 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_account_tbl WHERE account_id = %d", account_id);
5130 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5131 if (error != EMAIL_ERROR_NONE) {
5132 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5136 /* validate account existence */
5137 rc = sqlite3_changes(local_db_handle);
5139 EM_DEBUG_EXCEPTION(" no matched account found...");
5140 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
5144 /* delete from secure storage */
5145 error = emcore_remove_password_in_key_manager(recv_password_file_name);
5146 if (error != EMAIL_ERROR_NONE) {
5147 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5151 error = emcore_remove_password_in_key_manager(send_password_file_name);
5152 if (error != EMAIL_ERROR_NONE) {
5153 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5160 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5162 if (err_code != NULL)
5165 EM_DEBUG_FUNC_END("ret [%d]", ret);
5170 INTERNAL_FUNC int emstorage_free_account(emstorage_account_tbl_t** account_list, int count, int *err_code)
5172 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%d], err_code[%p]", account_list, count, err_code);
5175 int error = EMAIL_ERROR_NONE;
5178 if (!account_list || !*account_list) {
5179 EM_DEBUG_EXCEPTION("account_list[%p], count[%d]", account_list, count);
5180 error = EMAIL_ERROR_INVALID_PARAM;
5184 emstorage_account_tbl_t* p = *account_list;
5187 for (; i < count; i++) {
5188 EM_SAFE_FREE(p[i].account_name);
5189 EM_SAFE_FREE(p[i].incoming_server_address);
5190 EM_SAFE_FREE(p[i].user_email_address);
5191 EM_SAFE_FREE(p[i].user_data);
5192 EM_SAFE_FREE(p[i].incoming_server_user_name);
5193 EM_SAFE_FREE(p[i].incoming_server_password);
5194 EM_SAFE_FREE(p[i].outgoing_server_address);
5195 EM_SAFE_FREE(p[i].outgoing_server_user_name);
5196 EM_SAFE_FREE(p[i].outgoing_server_password);
5197 EM_SAFE_FREE(p[i].user_display_name);
5198 EM_SAFE_FREE(p[i].reply_to_address);
5199 EM_SAFE_FREE(p[i].return_address);
5200 EM_SAFE_FREE(p[i].logo_icon_path);
5201 EM_SAFE_FREE(p[i].options.display_name_from);
5202 EM_SAFE_FREE(p[i].options.signature);
5203 EM_SAFE_FREE(p[i].options.alert_ringtone_path);
5204 EM_SAFE_FREE(p[i].certificate_path);
5205 EM_SAFE_FREE(p[i].user_name);
5209 *account_list = NULL;
5215 if (err_code != NULL)
5218 EM_DEBUG_FUNC_END("ret [%d]", ret);
5222 INTERNAL_FUNC int emstorage_get_mailbox_count(char *multi_user_name, int account_id, int local_yn, int *count, int transaction, int *err_code)
5224 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);
5226 if ((account_id < FIRST_ACCOUNT_ID) || (count == NULL)) {
5227 EM_DEBUG_EXCEPTION(" account_list[%d], local_yn[%d], count[%p]", account_id, local_yn, count);
5229 if (err_code != NULL)
5230 *err_code = EMAIL_ERROR_INVALID_PARAM;
5234 int rc = -1, ret = false;
5235 int error = EMAIL_ERROR_NONE;
5236 char sql_query_string[QUERY_SIZE] = {0, };
5238 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5239 EMSTORAGE_START_READ_TRANSACTION(transaction);
5241 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);
5245 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5246 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
5247 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5249 *count = atoi(result[1]);
5250 sqlite3_free_table(result);
5257 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5259 if (err_code != NULL)
5262 EM_DEBUG_FUNC_END("ret [%d]", ret);
5266 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)
5268 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);
5270 if (!select_num || !mailbox_list) {
5271 EM_DEBUG_EXCEPTION("Invalid parameters");
5273 if (err_code != NULL)
5274 *err_code = EMAIL_ERROR_INVALID_PARAM;
5280 int error = EMAIL_ERROR_NONE;
5281 char conditional_clause_string[QUERY_SIZE] = {0, };
5282 char ordering_clause_string[QUERY_SIZE] = {0, };
5284 if (account_id == ALL_ACCOUNT) {
5285 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5286 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);
5288 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d ", account_id);
5289 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5290 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);
5293 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5295 switch (sort_type) {
5296 case EMAIL_MAILBOX_SORT_BY_NAME_ASC:
5297 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name ASC");
5300 case EMAIL_MAILBOX_SORT_BY_NAME_DSC:
5301 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name DESC");
5304 case EMAIL_MAILBOX_SORT_BY_TYPE_ASC:
5305 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type ASC");
5308 case EMAIL_MAILBOX_SORT_BY_TYPE_DSC:
5309 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type DEC");
5313 EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5315 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5316 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5324 if (err_code != NULL)
5327 EM_DEBUG_FUNC_END("ret [%d]", ret);
5331 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)
5333 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);
5336 int error = EMAIL_ERROR_NONE;
5337 int where_clause_count = 0;
5338 char conditional_clause_string[QUERY_SIZE] = {0, };
5339 char ordering_clause_string[QUERY_SIZE] = {0, };
5341 if (!select_num || !mailbox_list) {
5342 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5343 error = EMAIL_ERROR_INVALID_PARAM;
5347 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL) {
5348 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);
5349 where_clause_count++;
5352 if (account_id > 0) {
5353 if (where_clause_count == 0) {
5354 SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " WHERE ");
5355 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);
5357 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);
5360 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY CASE WHEN MBT.mailbox_name"
5361 " GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\'"
5362 " THEN 2 ELSE 1 END ASC,"
5363 " MBT.mailbox_name COLLATE CONVERTUTF8 ASC ");
5364 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5365 EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5367 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 1, 1, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5368 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5376 if (err_code != NULL)
5379 EM_DEBUG_FUNC_END("ret [%d]", ret);
5383 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)
5385 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);
5388 int error = EMAIL_ERROR_NONE;
5389 char conditional_clause_string[QUERY_SIZE] = {0, };
5391 if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list || !parent_mailbox_name) {
5392 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);
5393 if (err_code != NULL)
5394 *err_code = EMAIL_ERROR_INVALID_PARAM;
5398 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);
5399 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5401 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) {
5402 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5411 if (err_code != NULL)
5414 EM_DEBUG_FUNC_END("ret [%d]", ret);
5418 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)
5420 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);
5421 if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list) {
5422 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5424 if (err_code != NULL)
5425 *err_code = EMAIL_ERROR_INVALID_PARAM;
5431 int error = EMAIL_ERROR_NONE;
5432 char conditional_clause_string[QUERY_SIZE] = {0, };
5434 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND modifiable_yn = 0", account_id);
5435 EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
5437 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) {
5438 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5446 if (err_code != NULL)
5449 EM_DEBUG_FUNC_END("ret [%d]", ret);
5453 INTERNAL_FUNC int emstorage_stamp_last_sync_time_of_mailbox(char *multi_user_name, int input_mailbox_id, int input_transaction)
5455 EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_transaction [%d]", input_mailbox_id, input_transaction);
5457 int result_code = false;
5458 int error = EMAIL_ERROR_NONE;
5459 time_t current_time = 0;
5460 char sql_query_string[QUERY_SIZE] = {0, };
5461 sqlite3 *local_db_handle = NULL;
5463 if (!input_mailbox_id) {
5464 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5465 return EMAIL_ERROR_INVALID_PARAM;
5468 time(¤t_time);
5470 local_db_handle = emstorage_get_db_connection(multi_user_name);
5471 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
5473 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5474 "UPDATE mail_box_tbl SET"
5475 " last_sync_time = %d"
5476 " WHERE mailbox_id = %d"
5478 , input_mailbox_id);
5480 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
5481 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5482 if (error != EMAIL_ERROR_NONE) {
5483 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5489 if (error == EMAIL_ERROR_NONE)
5492 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, result_code, error);
5494 EM_DEBUG_FUNC_END("error [%d]", error);
5498 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)
5500 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);
5501 EM_PROFILE_BEGIN(profile_emstorage_get_mailbox_by_name);
5503 if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !result_mailbox) {
5504 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], mailbox_name[%s], result_mailbox[%p]", account_id, local_yn, mailbox_name, result_mailbox);
5506 if (err_code != NULL)
5507 *err_code = EMAIL_ERROR_INVALID_PARAM;
5512 int error = EMAIL_ERROR_NONE;
5513 int result_count = 0;
5514 char conditional_clause_string[QUERY_SIZE] = {0, };
5516 if (strcmp(mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
5517 if (!(*result_mailbox = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t)))) {
5518 EM_DEBUG_EXCEPTION("malloc failed...");
5519 error = EMAIL_ERROR_OUT_OF_MEMORY;
5523 (*result_mailbox)->mailbox_id = 0;
5524 (*result_mailbox)->account_id = account_id;
5525 (*result_mailbox)->local_yn = 1;
5526 (*result_mailbox)->mailbox_name = EM_SAFE_STRDUP(mailbox_name);
5527 (*result_mailbox)->mailbox_type = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;
5528 (*result_mailbox)->alias = EM_SAFE_STRDUP(mailbox_name);
5529 (*result_mailbox)->deleted_flag = 0;
5530 (*result_mailbox)->modifiable_yn = 1;
5531 (*result_mailbox)->total_mail_count_on_server = 1;
5532 (*result_mailbox)->has_archived_mails = 0;
5533 (*result_mailbox)->mail_slot_size = 0x0FFFFFFF;
5534 (*result_mailbox)->no_select = 0;
5537 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string, "WHERE account_id = %d AND mailbox_name = '%q'", account_id, mailbox_name);
5539 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);
5541 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5543 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5544 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5553 if (err_code != NULL)
5556 EM_PROFILE_END(profile_emstorage_get_mailbox_by_name);
5557 EM_DEBUG_FUNC_END("ret [%d]", ret);
5561 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)
5563 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);
5565 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_USER_DEFINED) || !output_mailbox) {
5566 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_type[%d], output_mailbox[%p]", account_id, mailbox_type, output_mailbox);
5567 if (err_code != NULL)
5568 *err_code = EMAIL_ERROR_INVALID_PARAM;
5573 int error = EMAIL_ERROR_NONE;
5574 int result_count = 0;
5575 emstorage_mailbox_tbl_t *result_mailbox = NULL;
5576 char conditional_clause_string[QUERY_SIZE] = {0,};
5579 SNPRINTF(conditional_clause_string, QUERY_SIZE, "WHERE account_id = %d AND mailbox_type = %d ", account_id, mailbox_type);
5581 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5583 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", true, false, &result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5584 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl error [%d]", error);
5592 *output_mailbox = result_mailbox;
5594 if (err_code != NULL)
5597 EM_DEBUG_FUNC_END("ret [%d]", ret);
5601 INTERNAL_FUNC int emstorage_get_mailbox_by_id(char *multi_user_name, int input_mailbox_id, emstorage_mailbox_tbl_t** output_mailbox)
5603 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], output_mailbox[%p]", input_mailbox_id, output_mailbox);
5605 if (input_mailbox_id <= 0 || !output_mailbox) {
5606 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: input_mailbox_id[%d] output_mailbox[%p]", input_mailbox_id, output_mailbox);
5607 return EMAIL_ERROR_INVALID_PARAM;
5610 int ret = EMAIL_ERROR_NONE;
5611 int result_count = 0;
5612 char conditional_clause_string[QUERY_SIZE] = {0, };
5614 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE MBT.mailbox_id = %d", input_mailbox_id);
5616 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5618 if ((ret = emstorage_query_mailbox_tbl(multi_user_name,
5619 conditional_clause_string,
5620 "", true, false, output_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5621 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", ret);
5627 EM_DEBUG_FUNC_END("ret [%d]", ret);
5631 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)
5633 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);
5635 if (account_id < 0 || !keyword || !result_mailbox) {
5636 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], keyword[%s], result_mailbox[%p]", account_id, keyword, result_mailbox);
5638 if (err_code != NULL)
5639 *err_code = EMAIL_ERROR_INVALID_PARAM;
5644 int error = EMAIL_ERROR_NONE;
5645 char conditional_clause_string[QUERY_SIZE] = {0, };
5647 if (account_id == 0)
5648 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5649 "WHERE alias LIKE \'%%%q%%\'", keyword);
5650 else if (account_id > 0)
5651 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5652 "WHERE account_id = %d AND alias LIKE \'%%%q%%\'", account_id, keyword);
5654 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5656 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, result_count)) != EMAIL_ERROR_NONE) {
5657 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5665 if (err_code != NULL)
5668 EM_DEBUG_FUNC_END("ret [%d]", ret);
5672 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)
5674 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);
5675 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_id) {
5676 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_id[%p]", account_id, mailbox_type, mailbox_id);
5677 if (err_code != NULL)
5678 *err_code = EMAIL_ERROR_INVALID_PARAM;
5682 int rc, ret = false;
5683 int error = EMAIL_ERROR_NONE;
5684 emstorage_account_tbl_t* account = NULL;
5685 DB_STMT hStmt = NULL;
5686 char sql_query_string[QUERY_SIZE] = {0, };
5688 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5689 EMSTORAGE_START_READ_TRANSACTION(transaction);
5691 /* Check whether the account exists. */
5692 if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME, &account, true, &error) || !account) {
5693 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5698 emstorage_free_account(&account, 1, NULL);
5700 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);
5702 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5705 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5707 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5708 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5711 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5712 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5713 ("sqlite3_step fail:%d", rc));
5715 if (rc == SQLITE_DONE) {
5716 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5717 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5721 _get_stmt_field_data_int(hStmt, mailbox_id, 0);
5726 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5727 if (hStmt != NULL) {
5728 rc = sqlite3_finalize(hStmt);
5729 if (rc != SQLITE_OK) {
5730 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5731 error = EMAIL_ERROR_DB_FAILURE;
5735 if (err_code != NULL)
5738 EM_DEBUG_FUNC_END("ret [%d]", ret);
5742 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)
5744 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);
5745 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_name) {
5746 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
5747 if (err_code != NULL)
5748 *err_code = EMAIL_ERROR_INVALID_PARAM;
5752 int rc, ret = false;
5753 int error = EMAIL_ERROR_NONE;
5754 emstorage_account_tbl_t* account = NULL;
5755 DB_STMT hStmt = NULL;
5756 char sql_query_string[QUERY_SIZE] = {0, };
5758 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5759 EMSTORAGE_START_READ_TRANSACTION(transaction);
5761 /* Check whether the account exists. */
5762 if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME, &account, true, &error) || !account) {
5763 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5768 emstorage_free_account(&account, 1, NULL);
5770 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);
5772 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5775 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5777 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5778 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5781 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5782 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5783 ("sqlite3_step fail:%d", rc));
5785 if (rc == SQLITE_DONE) {
5786 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5787 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5791 _get_stmt_field_data_string(hStmt, mailbox_name, 0, 0);
5796 if (hStmt != NULL) {
5797 rc = sqlite3_finalize(hStmt);
5798 if (rc != SQLITE_OK) {
5799 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5800 error = EMAIL_ERROR_DB_FAILURE;
5804 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5806 if (err_code != NULL)
5809 EM_DEBUG_FUNC_END("ret [%d]", ret);
5813 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)
5815 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);
5817 int error = EMAIL_ERROR_NONE;
5818 char sql_query_string[QUERY_SIZE] = {0, };
5819 char *replaced_mailbox_name = NULL;
5822 if (strstr(mailbox_name, "'")) {
5823 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5825 replaced_mailbox_name = strdup(mailbox_name);
5829 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5831 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5833 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5834 "UPDATE mail_box_tbl SET"
5835 " modifiable_yn = %d"
5836 " WHERE account_id = %d"
5837 " AND local_yn = %d"
5838 " AND mailbox_name = '%s'"
5842 , replaced_mailbox_name);
5844 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5845 if (error != EMAIL_ERROR_NONE) {
5846 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5853 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5855 EM_SAFE_FREE(replaced_mailbox_name);
5857 if (err_code != NULL)
5860 EM_DEBUG_FUNC_END("ret [%d]", ret);
5865 INTERNAL_FUNC int emstorage_update_mailbox_total_count(char *multi_user_name,
5867 int input_mailbox_id,
5868 int total_count_on_server,
5872 EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], total_count_on_server[%d], "
5873 "transaction[%d], err_code[%p]",
5874 account_id, input_mailbox_id, total_count_on_server, transaction, err_code);
5877 int error = EMAIL_ERROR_NONE;
5878 char sql_query_string[QUERY_SIZE] = {0, };
5880 if (account_id <= 0 || input_mailbox_id <= 0) {
5881 EM_DEBUG_EXCEPTION("account_id[%d], input_mailbox_id[%d]", account_id, input_mailbox_id);
5882 if (err_code != NULL)
5883 *err_code = EMAIL_ERROR_INVALID_PARAM;
5884 EM_DEBUG_FUNC_END("ret [%d]", ret);
5888 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5890 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5892 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5893 "UPDATE mail_box_tbl SET"
5894 " total_mail_count_on_server = %d"
5895 " WHERE account_id = %d"
5896 " AND mailbox_id = %d"
5897 , total_count_on_server
5899 , input_mailbox_id);
5901 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
5902 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5903 if (error != EMAIL_ERROR_NONE) {
5904 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5912 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5914 if (err_code != NULL)
5917 EM_DEBUG_FUNC_END("ret [%d]", ret);
5922 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)
5924 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);
5926 if (account_id < FIRST_ACCOUNT_ID || input_mailbox_id <= 0 || !result_mailbox) {
5927 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);
5929 if (err_code != NULL)
5930 *err_code = EMAIL_ERROR_INVALID_PARAM;
5931 EM_DEBUG_FUNC_END("ret [%d]", EMAIL_ERROR_INVALID_PARAM);
5935 int rc, ret = false;
5936 int error = EMAIL_ERROR_NONE;
5937 char sql_query_string[QUERY_SIZE] = {0, };
5938 DB_STMT hStmt = NULL;
5941 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5942 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5944 if (local_yn != -1) {
5945 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5946 "UPDATE mail_box_tbl SET"
5948 ", mailbox_name = ?"
5949 ", mailbox_type = ?"
5951 ", deleted_flag = ?"
5952 ", modifiable_yn= ?"
5953 ", mail_slot_size= ?"
5954 ", total_mail_count_on_server = ?"
5955 " WHERE account_id = %d"
5956 " AND local_yn = %d"
5957 " AND mailbox_id = '%d'"
5960 , input_mailbox_id);
5962 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5963 "UPDATE mail_box_tbl SET"
5965 ", mailbox_name = ?"
5966 ", mailbox_type = ?"
5968 ", deleted_flag = ?"
5969 ", modifiable_yn= ?"
5970 ", mail_slot_size= ?"
5971 ", total_mail_count_on_server = ?"
5972 " WHERE account_id = %d"
5973 " AND mailbox_id = '%d'"
5975 , input_mailbox_id);
5980 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5981 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5982 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5984 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_id);
5985 _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);
5986 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_type);
5987 _bind_stmt_field_data_string(hStmt, i++, (char *)result_mailbox->alias ? result_mailbox->alias : "", 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
5988 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->deleted_flag);
5989 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->modifiable_yn);
5990 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mail_slot_size);
5991 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->total_mail_count_on_server);
5994 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5995 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
5996 ("sqlite3_step fail:%d", rc));
5997 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5998 ("sqlite3_step fail:%d", rc));
6003 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6005 if (hStmt != NULL) {
6006 rc = sqlite3_finalize(hStmt);
6007 if (rc != SQLITE_OK) {
6008 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6009 error = EMAIL_ERROR_DB_FAILURE;
6013 if (err_code != NULL)
6016 EM_DEBUG_FUNC_END("ret [%d]", ret);
6020 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)
6022 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);
6024 if (account_id < FIRST_ACCOUNT_ID) {
6025 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
6027 if (err_code != NULL)
6028 *err_code = EMAIL_ERROR_INVALID_PARAM;
6032 int rc, ret = false;
6033 int error = EMAIL_ERROR_NONE;
6034 char sql_query_string[QUERY_SIZE] = {0, };
6036 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6037 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6039 EM_DEBUG_LOG("emstorage_update_mailbox_type");
6041 DB_STMT hStmt_box_tbl = NULL;
6042 DB_STMT hStmt_mail_tbl = NULL;
6045 /* Update mail_box_tbl */
6046 if (local_yn != -1) {
6047 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
6048 "UPDATE mail_box_tbl SET"
6050 " WHERE account_id = %d"
6051 " AND local_yn = %d"
6052 " AND mailbox_id = '%d'"
6055 , input_mailbox_id);
6057 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
6058 "UPDATE mail_box_tbl SET"
6060 " WHERE account_id = %d"
6061 " AND mailbox_id = '%d'"
6063 , input_mailbox_id);
6066 EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
6068 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_box_tbl, NULL), rc);
6070 if (SQLITE_OK != rc) {
6071 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6072 error = EMAIL_ERROR_DB_FAILURE;
6076 _bind_stmt_field_data_int(hStmt_box_tbl, i++, new_mailbox_type);
6079 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_box_tbl), rc);
6081 if (rc == SQLITE_FULL) {
6082 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6083 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
6087 if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
6088 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6089 error = EMAIL_ERROR_DB_FAILURE;
6094 /* Update mail_tbl */
6096 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6097 "UPDATE mail_tbl SET"
6099 " WHERE account_id = %d"
6100 " AND mailbox_id = '%d'"
6102 , input_mailbox_id);
6104 EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
6106 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_mail_tbl, NULL), rc);
6107 if (SQLITE_OK != rc) {
6108 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6109 error = EMAIL_ERROR_DB_FAILURE;
6113 _bind_stmt_field_data_int(hStmt_mail_tbl, i++, new_mailbox_type);
6115 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_mail_tbl), rc);
6116 if (rc == SQLITE_FULL) {
6117 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6118 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
6122 if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
6123 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6124 error = EMAIL_ERROR_DB_FAILURE;
6131 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6133 if (hStmt_box_tbl != NULL) {
6134 rc = sqlite3_finalize(hStmt_box_tbl);
6135 if (rc != SQLITE_OK) {
6136 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6137 error = EMAIL_ERROR_DB_FAILURE;
6141 if (hStmt_mail_tbl != NULL) {
6142 rc = sqlite3_finalize(hStmt_mail_tbl);
6143 if (rc != SQLITE_OK) {
6144 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6145 error = EMAIL_ERROR_DB_FAILURE;
6149 if (err_code != NULL)
6152 EM_DEBUG_FUNC_END("ret [%d]", ret);
6156 INTERNAL_FUNC int emstorage_set_local_mailbox(char *multi_user_name, int input_mailbox_id, int input_is_local_mailbox, int transaction)
6158 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);
6160 int rc, ret = false;
6161 int error = EMAIL_ERROR_NONE;
6162 char sql_query_string[QUERY_SIZE] = {0, };
6164 if (input_mailbox_id < 0) {
6165 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6166 return EMAIL_ERROR_INVALID_PARAM;
6169 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6171 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6173 EM_DEBUG_LOG("emstorage_update_mailbox_type");
6175 DB_STMT hStmt = NULL;
6178 /* Update mail_box_tbl */
6179 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6180 "UPDATE mail_box_tbl SET"
6182 " WHERE mailbox_id = %d"
6183 , input_mailbox_id);
6185 EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
6187 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6188 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6189 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6191 _bind_stmt_field_data_int(hStmt, i++, input_is_local_mailbox);
6193 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6194 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
6195 ("sqlite3_step fail:%d", rc));
6196 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6197 ("sqlite3_step fail:%d", rc));
6199 if (hStmt != NULL) {
6200 rc = sqlite3_finalize(hStmt);
6201 if (rc != SQLITE_OK) {
6202 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6203 error = EMAIL_ERROR_DB_FAILURE;
6211 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6213 if (hStmt != NULL) {
6214 rc = sqlite3_finalize(hStmt);
6215 if (rc != SQLITE_OK) {
6216 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6217 error = EMAIL_ERROR_DB_FAILURE;
6221 EM_DEBUG_FUNC_END("error [%d]", error);
6225 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)
6227 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);
6229 int err = EMAIL_ERROR_NONE;
6231 int cur_mailbox_id_string = 0;
6232 int mailbox_id_string_buffer_length = 0;
6233 char sql_query_string[QUERY_SIZE] = {0, };
6234 char *mailbox_id_string_buffer = NULL;
6235 char *parameter_string = NULL;
6236 sqlite3 *local_db_handle = NULL;
6238 if (input_mailbox_id_array == NULL || input_mailbox_id_count == 0 || input_field_name == NULL) {
6239 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6240 return EMAIL_ERROR_INVALID_PARAM;
6243 local_db_handle = emstorage_get_db_connection(multi_user_name);
6245 /* Generating mail id list string */
6246 mailbox_id_string_buffer_length = MAILBOX_ID_STRING_LENGTH * input_mailbox_id_count;
6248 mailbox_id_string_buffer = em_malloc(mailbox_id_string_buffer_length);
6250 if (!mailbox_id_string_buffer) {
6251 EM_DEBUG_EXCEPTION("em_mallocfailed");
6252 err = EMAIL_ERROR_OUT_OF_MEMORY;
6256 for (i = 0; i < input_mailbox_id_count; i++)
6257 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]);
6259 if (EM_SAFE_STRLEN(mailbox_id_string_buffer) > 1)
6260 mailbox_id_string_buffer[EM_SAFE_STRLEN(mailbox_id_string_buffer) - 1] = NULL_CHAR;
6262 /* Generating notification parameter string */
6263 parameter_string = em_malloc(mailbox_id_string_buffer_length + EM_SAFE_STRLEN(input_field_name) + 2);
6265 if (!parameter_string) {
6266 EM_DEBUG_EXCEPTION("em_mallocfailed");
6267 err = EMAIL_ERROR_OUT_OF_MEMORY;
6271 SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", input_field_name, 0x01, mailbox_id_string_buffer);
6273 /* Write query string */
6274 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);
6276 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
6279 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
6280 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6281 if (err != EMAIL_ERROR_NONE) {
6282 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
6286 if (sqlite3_changes(local_db_handle) == 0)
6287 EM_DEBUG_LOG("no mail matched...");
6292 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, err);
6294 if (err == EMAIL_ERROR_NONE && parameter_string) {
6295 if (!emcore_notify_storage_event(NOTI_MAILBOX_FIELD_UPDATE, input_account_id, 0, parameter_string, input_value))
6296 EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_MAILBOX_FIELD_UPDATE [%s,%d]",
6297 input_field_name, input_value);
6300 EM_SAFE_FREE(mailbox_id_string_buffer);
6301 EM_SAFE_FREE(parameter_string);
6303 EM_DEBUG_FUNC_END("err [%d]", err);
6307 INTERNAL_FUNC int emstorage_add_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox_tbl, int transaction, int *err_code)
6309 EM_DEBUG_FUNC_BEGIN("mailbox_tbl[%p], transaction[%d], err_code[%p]", mailbox_tbl, transaction, err_code);
6312 if (err_code != NULL)
6313 *err_code = EMAIL_ERROR_INVALID_PARAM;
6317 int rc, ret = false;
6318 int error = EMAIL_ERROR_NONE;
6319 DB_STMT hStmt = NULL;
6320 char sql_query_string[QUERY_SIZE] = {0,};
6321 char **result = NULL;
6322 time_t current_time;
6323 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6324 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6326 EM_SAFE_STRCPY(sql_query_string, "SELECT max(rowid) FROM mail_box_tbl;");
6328 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6329 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6330 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6332 time(¤t_time);
6334 if (NULL == result[1])
6337 rc = atoi(result[1]) + 1;
6338 sqlite3_free_table(result);
6340 memset(sql_query_string, 0, sizeof(char) * QUERY_SIZE);
6342 mailbox_tbl->mailbox_id = rc;
6344 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6345 "INSERT INTO mail_box_tbl VALUES "
6346 "(?" /* mailbox_id */
6347 ", ?" /* account_id */
6348 ", ?" /* local_yn */
6349 ", ?" /* mailbox_name */
6350 ", ?" /* mailbox_type */
6352 ", ?" /* deleted_flag */
6353 ", ?" /* modifiable_yn */
6354 ", ?" /* total_mail_count_on_server */
6355 ", ?" /* has_archived_mails */
6356 ", ?" /* mail_slot_size */
6357 ", ?" /* no_select */
6358 ", ?" /* last_sync_time */
6359 ", ?" /* eas_data_length */
6360 ", ?" /* eas_data */
6364 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6365 EM_DEBUG_LOG_DEV("After sqlite3_prepare hStmt = %p", hStmt);
6366 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6367 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6371 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_id);
6372 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->account_id);
6373 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->local_yn);
6374 _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
6375 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_type);
6376 _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->alias, 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
6377 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->deleted_flag);
6378 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->modifiable_yn);
6379 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->total_mail_count_on_server);
6380 _bind_stmt_field_data_int(hStmt, col_index++, 0);
6381 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mail_slot_size);
6382 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->no_select);
6383 _bind_stmt_field_data_int(hStmt, col_index++, current_time);
6384 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->eas_data_length);
6385 _bind_stmt_field_data_blob(hStmt, col_index++, (void*)mailbox_tbl->eas_data, mailbox_tbl->eas_data_length);
6388 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6389 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6390 ("sqlite3_step fail:%dn", rc));
6395 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6396 if (hStmt != NULL) {
6397 rc = sqlite3_finalize(hStmt);
6398 if (rc != SQLITE_OK) {
6399 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6400 error = EMAIL_ERROR_DB_FAILURE;
6404 if (error == EMAIL_ERROR_NONE) {
6405 if (!emcore_notify_storage_event(NOTI_MAILBOX_ADD, mailbox_tbl->account_id, mailbox_tbl->mailbox_id,
6406 mailbox_tbl->mailbox_name, mailbox_tbl->mailbox_type))
6407 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6410 if (err_code != NULL)
6413 EM_DEBUG_FUNC_END("ret [%d]", ret);
6417 INTERNAL_FUNC int emstorage_set_all_mailbox_modifiable_yn(char *multi_user_name, int account_id, int modifiable_yn, int transaction, int *err_code)
6419 EM_DEBUG_FUNC_BEGIN("account_id[%d], modifiable_yn[%d], err_code[%p]", account_id, modifiable_yn, err_code);
6421 if (account_id < FIRST_ACCOUNT_ID) {
6423 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
6425 if (err_code != NULL)
6426 *err_code = EMAIL_ERROR_INVALID_PARAM;
6429 int rc, ret = false;
6430 int error = EMAIL_ERROR_NONE;
6431 char sql_query_string[QUERY_SIZE] = {0,};
6432 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6433 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6436 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET modifiable_yn = %d WHERE account_id = %d", modifiable_yn, account_id);
6438 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6439 if (error != EMAIL_ERROR_NONE) {
6440 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6444 rc = sqlite3_changes(local_db_handle);
6446 EM_DEBUG_EXCEPTION("All mailbox_name modifiable_yn set to 0 already");
6452 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6454 if (err_code != NULL)
6456 EM_DEBUG_FUNC_END("ret [%d]", ret);
6462 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)
6464 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);
6466 if (account_id < FIRST_ACCOUNT_ID) {
6467 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
6469 if (err_code != NULL)
6470 *err_code = EMAIL_ERROR_INVALID_PARAM;
6474 int rc, ret = false;
6475 int error = EMAIL_ERROR_NONE;
6476 char sql_query_string[QUERY_SIZE] = {0, };
6477 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6478 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6481 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_box_tbl WHERE account_id = %d ", account_id);
6483 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);
6485 if (input_mailbox_id > 0) { /* 0 means all mailbox */
6486 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);
6489 EM_DEBUG_LOG_SEC("mailbox sql_query_string [%s]", sql_query_string);
6490 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6491 if (error != EMAIL_ERROR_NONE) {
6492 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6496 rc = sqlite3_changes(local_db_handle);
6498 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
6499 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
6506 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6508 if (error == EMAIL_ERROR_NONE) {
6509 if (!emcore_notify_storage_event(NOTI_MAILBOX_DELETE, account_id, input_mailbox_id, NULL, 0))
6510 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6513 if (err_code != NULL)
6516 EM_DEBUG_FUNC_END("ret [%d]", ret);
6520 INTERNAL_FUNC int emstorage_free_mailbox(emstorage_mailbox_tbl_t** mailbox_list, int count, int *err_code)
6522 EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d], err_code[%p]", mailbox_list, count, err_code);
6525 int error = EMAIL_ERROR_NONE;
6528 if (!mailbox_list || !*mailbox_list) {
6529 EM_DEBUG_EXCEPTION(" mailbox_list[%p], count[%d]", mailbox_list, count);
6531 error = EMAIL_ERROR_INVALID_PARAM;
6535 emstorage_mailbox_tbl_t* p = *mailbox_list;
6538 for (; i < count; i++) {
6539 EM_SAFE_FREE(p[i].mailbox_name);
6540 EM_SAFE_FREE(p[i].alias);
6541 EM_SAFE_FREE(p[i].eas_data); /*valgrind*/
6544 EM_SAFE_FREE(p); *mailbox_list = NULL;
6550 if (err_code != NULL)
6553 EM_DEBUG_FUNC_END("ret [%d]", ret);
6557 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)
6559 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);
6561 if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !count) {
6562 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], count[%p], exist[%p]", account_id, mailbox_name, count);
6564 if (err_code != NULL)
6565 *err_code = EMAIL_ERROR_INVALID_PARAM;
6569 int rc = -1, ret = false;
6570 int error = EMAIL_ERROR_NONE;
6571 char sql_query_string[QUERY_SIZE] = {0, };
6572 char *replaced_mailbox_name = NULL;
6574 if (strstr(mailbox_name, "'")) {
6575 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6577 replaced_mailbox_name = EM_SAFE_STRDUP(mailbox_name);
6580 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6583 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6584 EMSTORAGE_START_READ_TRANSACTION(transaction);
6585 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);
6586 EM_DEBUG_LOG_SEC(">>> SQL [ %s ] ", sql_query_string);
6590 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6591 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6592 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6594 *count = atoi(result[1]);
6595 sqlite3_free_table(result);
6600 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6602 EM_SAFE_FREE(replaced_mailbox_name);
6604 if (err_code != NULL)
6607 EM_DEBUG_FUNC_END("ret [%d]", ret);
6611 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)
6613 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);
6615 if (account_id < FIRST_ACCOUNT_ID || !uid || !exist) {
6616 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], uid[%p], exist[%p]", account_id, mailbox_name , uid, exist);
6618 if (err_code != NULL)
6619 *err_code = EMAIL_ERROR_INVALID_PARAM;
6623 int rc = -1, ret = false;
6624 int error = EMAIL_ERROR_NONE;
6625 char sql_query_string[QUERY_SIZE] = {0, };
6626 char *replaced_mailbox_name = NULL;
6628 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6630 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6631 EMSTORAGE_START_READ_TRANSACTION(transaction);
6634 if (strstr(mailbox_name, "'")) {
6635 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6637 replaced_mailbox_name = strdup(mailbox_name);
6640 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);
6642 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);
6645 char **result = NULL;
6647 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6648 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6649 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6651 *exist = atoi(result[1]);
6652 sqlite3_free_table(result);
6662 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6664 EM_SAFE_FREE(replaced_mailbox_name);
6666 if (err_code != NULL)
6669 EM_DEBUG_FUNC_END("ret [%d]", ret);
6673 INTERNAL_FUNC int emstorage_get_downloaded_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code)
6675 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], err_code[%p]", mail_id, mail, err_code);
6677 if (!mail || mail_id <= 0) {
6678 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
6679 if (err_code != NULL)
6680 *err_code = EMAIL_ERROR_INVALID_PARAM;
6684 int rc, ret = false;
6685 int error = EMAIL_ERROR_NONE;
6686 DB_STMT hStmt = NULL;
6687 char sql_query_string[QUERY_SIZE] = {0, };
6689 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6690 EMSTORAGE_START_READ_TRANSACTION(transaction);
6692 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE local_uid = %d", mail_id);
6694 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6695 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6697 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6698 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6701 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6702 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6703 ("sqlite3_step fail:%d", rc));
6705 *mail = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
6706 if (*mail == NULL) {
6707 error = EMAIL_ERROR_OUT_OF_MEMORY;
6708 EM_DEBUG_EXCEPTION("Memory allocation for mail failed.");
6712 memset(*mail, 0x00, sizeof(emstorage_mail_tbl_t));
6714 _get_stmt_field_data_int(hStmt, &((*mail)->account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6715 _get_stmt_field_data_int(hStmt, &((*mail)->mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6716 _get_stmt_field_data_string(hStmt, &((*mail)->server_mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6717 _get_stmt_field_data_int(hStmt, &((*mail)->mail_id), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6718 _get_stmt_field_data_string(hStmt, &((*mail)->server_mail_id), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6719 _get_stmt_field_data_int(hStmt, &((*mail)->mail_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6720 _get_stmt_field_data_char(hStmt, &((*mail)->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6722 (*mail)->server_mail_status = 1;
6728 if (hStmt != NULL) {
6729 rc = sqlite3_finalize(hStmt);
6730 if (rc != SQLITE_OK) {
6731 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6732 error = EMAIL_ERROR_DB_FAILURE;
6736 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6738 if (err_code != NULL)
6741 EM_DEBUG_FUNC_END("ret [%d]", ret);
6745 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)
6747 EM_PROFILE_BEGIN(emStorageGetDownloadList);
6748 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);
6749 if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid || !count) {
6750 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_id[%s], read_mail_uid[%p], count[%p]", account_id, mailbox_id, read_mail_uid, count);
6752 if (err_code != NULL)
6753 *err_code = EMAIL_ERROR_INVALID_PARAM;
6757 int rc, ret = false;
6758 int error = EMAIL_ERROR_NONE;
6760 DB_STMT hStmt = NULL;
6761 char sql_query_string[QUERY_SIZE] = {0, };
6763 emstorage_read_mail_uid_tbl_t* p_data_tbl = NULL;
6766 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6767 EMSTORAGE_START_READ_TRANSACTION(transaction);
6770 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);
6772 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
6774 EM_DEBUG_LOG_SEC(" sql_query_string : %s", sql_query_string);
6778 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6779 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6780 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6781 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6784 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6785 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6786 ("sqlite3_step fail:%d", rc));
6789 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL); */
6790 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL), rc);
6791 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6792 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6794 sqlite3_free_table(result);
6796 EM_DEBUG_LOG("No mail found in mail_read_mail_uid_tbl");
6802 if (!(p_data_tbl = (emstorage_read_mail_uid_tbl_t*)malloc(sizeof(emstorage_read_mail_uid_tbl_t) * *count))) {
6803 EM_DEBUG_EXCEPTION(" malloc failed...");
6804 error = EMAIL_ERROR_OUT_OF_MEMORY;
6808 memset(p_data_tbl, 0x00, sizeof(emstorage_read_mail_uid_tbl_t)*(*count));
6810 for (i = 0; i < *count; ++i) {
6811 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6812 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6813 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6814 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].local_uid), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6815 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].server_uid), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6816 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rfc822_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6817 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6818 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_flagged_field), FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6820 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6821 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6822 ("sqlite3_step fail:%d", rc));
6829 *read_mail_uid = p_data_tbl;
6830 else if (p_data_tbl)
6831 emstorage_free_read_mail_uid(&p_data_tbl, *count, NULL);
6833 if (hStmt != NULL) {
6834 rc = sqlite3_finalize(hStmt);
6835 if (rc != SQLITE_OK) {
6836 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6837 error = EMAIL_ERROR_DB_FAILURE;
6841 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6843 if (err_code != NULL)
6846 EM_PROFILE_END(emStorageGetDownloadList);
6847 EM_DEBUG_FUNC_END("ret [%d]", ret);
6851 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)
6853 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);
6855 if (account_id < FIRST_ACCOUNT_ID || !mail_size) {
6856 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);
6858 if (err_code != NULL)
6859 *err_code = EMAIL_ERROR_INVALID_PARAM;
6863 int rc, ret = false;
6864 int error = EMAIL_ERROR_NONE;
6865 DB_STMT hStmt = NULL;
6866 char sql_query_string[QUERY_SIZE] = {0, };
6867 char *replaced_mailbox_name = NULL;
6869 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6870 EMSTORAGE_START_READ_TRANSACTION(transaction);
6873 if (strstr(mailbox_name, "'")) {
6874 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6876 replaced_mailbox_name = strdup(mailbox_name);
6879 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6881 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6882 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6883 "WHERE account_id = %d "
6884 "AND mailbox_id = '%s' "
6885 "AND local_uid = %d "
6886 "AND mailbox_name = '%s' "
6887 "AND server_uid = '%s'",
6888 account_id, mailbox_id, local_uid, replaced_mailbox_name, uid);
6890 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6891 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6892 "WHERE account_id = %d "
6893 "AND mailbox_id = '%s' "
6894 "AND local_uid = %d "
6895 "AND server_uid = '%s'",
6896 account_id, mailbox_id, local_uid, uid);
6900 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6901 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6902 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6905 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6906 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6907 ("sqlite3_step fail:%d", rc));
6909 if (rc == SQLITE_DONE) {
6910 EM_DEBUG_LOG("no matched mail found....");
6911 error = EMAIL_ERROR_MAIL_NOT_FOUND;
6915 _get_stmt_field_data_int(hStmt, mail_size, 0);
6920 EM_SAFE_FREE(replaced_mailbox_name);
6922 if (hStmt != NULL) {
6923 rc = sqlite3_finalize(hStmt);
6924 if (rc != SQLITE_OK) {
6925 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6926 error = EMAIL_ERROR_DB_FAILURE;
6930 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6932 if (err_code != NULL)
6935 EM_DEBUG_FUNC_END("ret [%d]", ret);
6939 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)
6941 EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], transaction[%d], err_code[%p]", read_mail_uid, transaction, err_code);
6943 if (!read_mail_uid) {
6944 EM_DEBUG_EXCEPTION("read_mail_uid[%p]", read_mail_uid);
6945 if (err_code != NULL)
6946 *err_code = EMAIL_ERROR_INVALID_PARAM;
6950 int rc, rc2, ret = false;
6951 int error = EMAIL_ERROR_NONE;
6952 DB_STMT hStmt = NULL;
6953 char sql_query_string[QUERY_SIZE] = {0, };
6955 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6956 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6958 char *sql = "SELECT max(rowid) FROM mail_read_mail_uid_tbl;";
6959 char **result = NULL;
6962 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6963 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6964 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6966 if (NULL == result[1]) rc = 1;
6967 else rc = atoi(result[1])+1;
6968 sqlite3_free_table(result);
6970 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6971 "INSERT INTO mail_read_mail_uid_tbl VALUES "
6972 "(?" /* account_id */
6973 ", ?" /* mailbox_id */
6974 ", ?" /* mailbox_name */
6975 ", ?" /* local_uid */
6976 ", ?" /* server_uid */
6977 ", ?" /* rfc822_size */
6978 ", ?" /* sync_status */
6979 ", ?" /* flags_seen_field */
6980 ", ?" /* flags_flagged_field */
6984 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
6985 if (rc2 != SQLITE_OK) {
6986 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6987 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6989 error = EMAIL_ERROR_DB_FAILURE;
6993 EM_DEBUG_LOG("account_id[%d] mailbox_id[%d] local_uid [%d]"
6994 "server_uid[%s] rfc822_size[%d] rc[%d]",
6995 read_mail_uid->account_id, read_mail_uid->mailbox_id, read_mail_uid->local_uid,
6996 read_mail_uid->server_uid, read_mail_uid->rfc822_size, rc);
6998 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->account_id);
6999 _bind_stmt_field_data_int(hStmt, LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->mailbox_id);
7000 _bind_stmt_field_data_int(hStmt, LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->local_uid);
7001 _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);
7002 _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);
7003 _bind_stmt_field_data_int(hStmt, RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->rfc822_size);
7004 _bind_stmt_field_data_int(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_seen_field);
7005 _bind_stmt_field_data_int(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_flagged_field);
7006 _bind_stmt_field_data_int(hStmt, IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, rc);
7009 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7010 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
7011 ("sqlite3_step fail:%d", rc));
7012 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7013 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
7019 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7020 if (hStmt != NULL) {
7021 rc = sqlite3_finalize(hStmt);
7022 if (rc != SQLITE_OK) {
7023 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7024 error = EMAIL_ERROR_DB_FAILURE;
7028 if (err_code != NULL)
7031 EM_DEBUG_FUNC_END("ret [%d]", ret);
7035 #ifdef __FEATURE_BODY_SEARCH__
7036 INTERNAL_FUNC int emstorage_add_mail_text(char *multi_user_name, emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
7038 EM_DEBUG_FUNC_BEGIN("mail_text[%p], transaction[%d], err_code[%p]", mail_text, transaction, err_code);
7041 EM_DEBUG_EXCEPTION("mail_text[%p]", mail_text);
7042 if (err_code != NULL)
7043 *err_code = EMAIL_ERROR_INVALID_PARAM;
7047 int rc, rc2, ret = false;
7048 int error = EMAIL_ERROR_NONE;
7049 DB_STMT hStmt = NULL;
7050 char sql_query_string[QUERY_SIZE] = {0, };
7052 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7053 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7055 char *sql = "SELECT max(rowid) FROM mail_text_tbl;";
7056 char **result = NULL;
7058 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7059 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7060 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7061 sqlite3_free_table(result);
7063 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7064 "INSERT INTO mail_text_tbl VALUES "
7070 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
7071 if (rc2 != SQLITE_OK) {
7072 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7073 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc2, sqlite3_errmsg(local_db_handle));
7075 error = EMAIL_ERROR_DB_FAILURE;
7079 EM_DEBUG_LOG("mail_id[%d] account_id[%d] mailbox_id[%d]", mail_text->mail_id,
7080 mail_text->account_id, mail_text->mailbox_id);
7081 EM_DEBUG_LOG_DEV("body_text VALUE [%s] ", mail_text->body_text);
7083 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mail_id);
7084 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->account_id);
7085 _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mailbox_id);
7086 _bind_stmt_field_data_string(hStmt, BODY_TEXT_IDX_IN_MAIL_TEXT_TBL, (char *)mail_text->body_text, 0, -1);
7088 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7089 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
7090 ("sqlite3_step fail:%d", rc));
7091 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7092 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
7097 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7098 if (hStmt != NULL) {
7099 rc = sqlite3_finalize(hStmt);
7100 if (rc != SQLITE_OK) {
7101 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7102 error = EMAIL_ERROR_DB_FAILURE;
7106 if (err_code != NULL)
7109 EM_DEBUG_FUNC_END("ret [%d]", ret);
7114 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)
7116 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);
7118 if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid) {
7119 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);
7121 if (err_code != NULL)
7122 *err_code = EMAIL_ERROR_INVALID_PARAM;
7126 int rc, ret = false;
7127 int error = EMAIL_ERROR_NONE;
7128 DB_STMT hStmt = NULL;
7129 char sql_query_string[QUERY_SIZE] = {0, };
7131 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7133 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7135 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7136 "UPDATE mail_read_mail_uid_tbl SET"
7139 ", mailbox_name = ?"
7143 ", flags_seen_field = ?"
7144 ", flags_flagged_field = ?"
7145 " WHERE account_id = ?"
7146 " AND mailbox_id = ?"
7147 " AND local_uid = ?"
7148 " AND mailbox_name= ?"
7149 " AND server_uid = ?");
7152 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7153 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7154 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7155 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7160 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->account_id);
7161 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->mailbox_id);
7162 _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7163 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->local_uid);
7164 _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->server_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7165 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->rfc822_size);
7166 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_seen_field);
7167 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_flagged_field);
7168 _bind_stmt_field_data_int(hStmt, i++, account_id);
7169 _bind_stmt_field_data_int(hStmt, i++, mailbox_id);
7170 _bind_stmt_field_data_int(hStmt, i++, local_uid);
7171 _bind_stmt_field_data_string(hStmt, i++, (char*)mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7172 _bind_stmt_field_data_string(hStmt, i++, (char*)uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7175 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7176 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7177 ("sqlite3_step fail:%d", rc));
7182 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7183 if (hStmt != NULL) {
7184 rc = sqlite3_finalize(hStmt);
7185 if (rc != SQLITE_OK) {
7186 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7187 error = EMAIL_ERROR_DB_FAILURE;
7191 if (err_code != NULL)
7194 EM_DEBUG_FUNC_END("ret [%d]", ret);
7198 INTERNAL_FUNC int emstorage_remove_downloaded_mail(char *multi_user_name,
7206 EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], mailbox_id[%d], mailbox_name[%s], "
7207 "uid[%s], transaction[%d], err_code[%p]",
7208 account_id, mailbox_id, mailbox_name, uid, transaction, err_code);
7210 if (account_id < FIRST_ACCOUNT_ID) {
7211 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_name[%s], uid[%s]", account_id, mailbox_name, uid);
7213 if (err_code != NULL)
7214 *err_code = EMAIL_ERROR_INVALID_PARAM;
7219 int error = EMAIL_ERROR_NONE;
7220 char sql_query_string[QUERY_SIZE] = {0, };
7221 char *replaced_mailbox_name = NULL;
7222 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7224 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7226 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7227 "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d ", account_id);
7229 if (mailbox_id > 0) {
7230 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7231 sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7232 "AND mailbox_id = %d ", mailbox_id);
7235 if (mailbox_name) { /* NULL means all mailbox_name */
7236 if (strstr(mailbox_name, "'")) {
7237 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
7239 replaced_mailbox_name = strdup(mailbox_name);
7242 SNPRINTF(sql_query_string+EM_SAFE_STRLEN(sql_query_string),
7243 sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7244 "AND mailbox_name = '%s' ", replaced_mailbox_name);
7247 if (uid) { /* NULL means all mail */
7248 sqlite3_snprintf(sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7249 sql_query_string + EM_SAFE_STRLEN(sql_query_string), "AND server_uid = '%q' ",
7253 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7254 if (error != EMAIL_ERROR_NONE) {
7255 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7262 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7264 EM_SAFE_FREE(replaced_mailbox_name);
7266 if (err_code != NULL)
7269 EM_DEBUG_FUNC_END("ret [%d]", ret);
7273 INTERNAL_FUNC int emstorage_free_read_mail_uid(emstorage_read_mail_uid_tbl_t** read_mail_uid, int count, int *err_code)
7275 EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], count[%d], err_code[%p]", read_mail_uid, count, err_code);
7278 int error = EMAIL_ERROR_NONE;
7281 if (!read_mail_uid || !*read_mail_uid) {
7282 EM_DEBUG_EXCEPTION(" read_mail_uid[%p], count[%d]", read_mail_uid, count);
7284 error = EMAIL_ERROR_INVALID_PARAM;
7288 emstorage_read_mail_uid_tbl_t* p = *read_mail_uid;
7291 for (i = 0; i < count; i++) {
7292 EM_SAFE_FREE(p[i].mailbox_name);
7293 EM_SAFE_FREE(p[i].server_uid);
7296 EM_SAFE_FREE(p); *read_mail_uid = NULL;
7302 if (err_code != NULL)
7305 EM_DEBUG_FUNC_END("ret [%d]", ret);
7309 INTERNAL_FUNC int emstorage_get_rule_count_by_account_id(char *multi_user_name, int account_id, int *count, int transaction, int *err_code)
7311 EM_DEBUG_FUNC_BEGIN("account_id [%d], count[%p], transaction[%d], err_code[%p]", count, transaction, err_code);
7314 EM_DEBUG_EXCEPTION("count[%p]", count);
7316 if (err_code != NULL)
7317 *err_code = EMAIL_ERROR_INVALID_PARAM;
7321 int rc = -1, ret = false;
7322 int error = EMAIL_ERROR_NONE;
7323 char sql_query_string[QUERY_SIZE] = {0, };
7325 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7326 EMSTORAGE_START_READ_TRANSACTION(transaction);
7328 if (account_id != ALL_ACCOUNT)
7329 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl where account_id = %d", account_id);
7331 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl");
7335 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7336 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7337 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7339 *count = atoi(result[1]);
7340 sqlite3_free_table(result);
7345 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7347 if (err_code != NULL)
7350 EM_DEBUG_FUNC_END("ret [%d]", ret);
7354 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)
7356 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);
7358 if (!select_num || !is_completed || !rule_list) { /* only global rule supported. */
7359 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);
7361 if (err_code != NULL)
7362 *err_code = EMAIL_ERROR_INVALID_PARAM;
7367 int error = EMAIL_ERROR_NONE;
7369 emstorage_rule_tbl_t* p_data_tbl = NULL;
7370 int i = 0, count = 0;
7371 DB_STMT hStmt = NULL;
7372 char sql_query_string[QUERY_SIZE] = {0, };
7376 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7377 EMSTORAGE_START_READ_TRANSACTION(transaction);
7379 if (account_id != ALL_ACCOUNT) {
7381 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d AND type = %d", account_id, type);
7383 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d ORDER BY type", account_id);
7386 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE type = %d", type);
7388 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl ORDER BY type");
7391 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7392 /* EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt); */
7393 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7394 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7397 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7398 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7399 ("sqlite3_step fail:%d", rc));
7402 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL); */
7403 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
7404 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7405 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7407 sqlite3_free_table(result);
7410 EM_DEBUG_LOG_DEV("No matching rule found...");
7412 error = EMAIL_ERROR_FILTER_NOT_FOUND; /*there is no matched rule*/
7417 if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t) * count))) {
7418 EM_DEBUG_EXCEPTION(" malloc failed...");
7420 error = EMAIL_ERROR_OUT_OF_MEMORY;
7424 memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t) * count);
7426 for (i = 0; i < count; i++) {
7427 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7428 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7429 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7430 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].type), TYPE_IDX_IN_MAIL_RULE_TBL);
7431 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7432 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7433 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7434 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7435 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7436 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7438 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7439 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7440 ("sqlite3_step fail:%d", rc));
7447 EM_DEBUG_LOG("[%d] rules found.", count);
7450 *rule_list = p_data_tbl;
7451 *select_num = count;
7452 } else if (p_data_tbl != NULL)
7453 emstorage_free_rule(&p_data_tbl, count, NULL); /* CID FIX */
7455 if (hStmt != NULL) {
7456 rc = sqlite3_finalize(hStmt);
7457 if (rc != SQLITE_OK) {
7458 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7459 error = EMAIL_ERROR_DB_FAILURE;
7463 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7465 if (err_code != NULL)
7468 EM_DEBUG_FUNC_END("ret [%d]", ret);
7472 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)
7474 EM_DEBUG_FUNC_BEGIN("rule_id[%d], rule[%p], transaction[%d], err_code[%p]", rule_id, rule, transaction, err_code);
7475 int error = EMAIL_ERROR_NONE;
7477 DB_STMT hStmt = NULL;
7480 EM_DEBUG_EXCEPTION("Invalid parameter");
7481 error = EMAIL_ERROR_INVALID_PARAM;
7486 EM_DEBUG_EXCEPTION("rule_id[%d], rule[%p]", rule_id, rule);
7487 error = EMAIL_ERROR_INVALID_PARAM;
7491 emstorage_rule_tbl_t* p_data_tbl = NULL;
7494 char sql_query_string[QUERY_SIZE] = {0, };
7495 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7496 EMSTORAGE_START_READ_TRANSACTION(transaction);
7498 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7500 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7501 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7502 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7505 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7506 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7507 ("sqlite3_step fail:%d", rc));
7509 if (rc == SQLITE_DONE) {
7510 EM_DEBUG_EXCEPTION(" no matched rule found...");
7511 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7515 if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t)))) {
7516 EM_DEBUG_EXCEPTION(" malloc failed...");
7517 error = EMAIL_ERROR_OUT_OF_MEMORY;
7521 memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t));
7522 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7523 _get_stmt_field_data_int(hStmt, &(p_data_tbl->rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7524 _get_stmt_field_data_string(hStmt, &(p_data_tbl->filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7525 _get_stmt_field_data_int(hStmt, &(p_data_tbl->type), TYPE_IDX_IN_MAIL_RULE_TBL);
7526 _get_stmt_field_data_string(hStmt, &(p_data_tbl->value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7527 _get_stmt_field_data_string(hStmt, &(p_data_tbl->value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7528 _get_stmt_field_data_int(hStmt, &(p_data_tbl->action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7529 _get_stmt_field_data_int(hStmt, &(p_data_tbl->target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7530 _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7531 _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7540 if (hStmt != NULL) {
7541 rc = sqlite3_finalize(hStmt);
7542 if (rc != SQLITE_OK) {
7543 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7544 error = EMAIL_ERROR_DB_FAILURE;
7548 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7550 if (err_code != NULL)
7553 EM_DEBUG_FUNC_END("ret [%d]", ret);
7557 INTERNAL_FUNC int emstorage_change_rule(char *multi_user_name, int rule_id, emstorage_rule_tbl_t* new_rule, int transaction, int *err_code)
7559 EM_DEBUG_FUNC_BEGIN("rule_id[%d], new_rule[%p], transaction[%d], err_code[%p]", rule_id, new_rule, transaction, err_code);
7561 if (!new_rule) { /* only global rule supported. */
7562 EM_DEBUG_EXCEPTION("rule_id[%d], new_rule[%p]", rule_id, new_rule);
7564 if (err_code != NULL)
7565 *err_code = EMAIL_ERROR_INVALID_PARAM;
7569 int rc, ret = false;
7570 int error = EMAIL_ERROR_NONE;
7572 DB_STMT hStmt = NULL;
7573 char sql_query_string[QUERY_SIZE] = {0, };
7574 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7575 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7577 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7578 "UPDATE mail_rule_tbl SET"
7584 ", target_mailbox_id = ?"
7589 " WHERE rule_id = %d"
7593 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7594 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
7595 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7596 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7600 _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->filter_name, 0, FILTER_NAME_LEN_IN_MAIL_RULE_TBL);
7601 _bind_stmt_field_data_int(hStmt, i++, new_rule->type);
7602 _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7603 _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7604 _bind_stmt_field_data_int(hStmt, i++, new_rule->action_type);
7605 _bind_stmt_field_data_int(hStmt, i++, new_rule->target_mailbox_id);
7606 _bind_stmt_field_data_int(hStmt, i++, new_rule->flag1);
7607 _bind_stmt_field_data_int(hStmt, i++, new_rule->flag2);
7608 _bind_stmt_field_data_int(hStmt, i++, new_rule->account_id);
7609 _bind_stmt_field_data_int(hStmt, i++, rule_id);
7612 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7613 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7614 ("sqlite3_step fail:%d", rc));
7619 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7620 if (hStmt != NULL) {
7621 rc = sqlite3_finalize(hStmt);
7622 if (rc != SQLITE_OK) {
7623 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7624 error = EMAIL_ERROR_DB_FAILURE;
7628 if (err_code != NULL)
7631 EM_DEBUG_FUNC_END("ret [%d]", ret);
7635 INTERNAL_FUNC int emstorage_find_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7637 EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7640 EM_DEBUG_LOG("rule is NULL");
7641 if (err_code != NULL)
7642 *err_code = EMAIL_ERROR_INVALID_PARAM;
7646 DB_STMT hStmt = NULL;
7647 char sql_query_string[QUERY_SIZE] = {0,};
7648 int error = EMAIL_ERROR_NONE;
7652 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7653 EMSTORAGE_START_READ_TRANSACTION(transaction);
7655 switch (rule->action_type) {
7656 case EMAIL_FILTER_MOVE:
7657 if (rule->type == EMAIL_PRIORITY_SENDER) {
7658 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7659 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7660 rule->action_type, rule->type, rule->value2);
7662 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7663 "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);
7666 case EMAIL_FILTER_BLOCK:
7667 if (rule->type == EMAIL_FILTER_FROM)
7668 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7669 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7670 rule->action_type, rule->type, rule->value2);
7671 else if (rule->type == EMAIL_FILTER_SUBJECT)
7672 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7673 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value) = UPPER(\'%q\')",
7674 rule->action_type, rule->type, rule->value);
7675 else if (rule->type == (EMAIL_FILTER_SUBJECT | EMAIL_FILTER_FROM))
7676 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7677 "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\'))",
7678 rule->action_type, EMAIL_FILTER_SUBJECT, rule->value, EMAIL_FILTER_FROM, rule->value2);
7682 EM_DEBUG_EXCEPTION("Invalid parameter : rule->action_type[%d]", rule->action_type);
7683 error = EMAIL_ERROR_INVALID_PARAM;
7688 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7689 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7690 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7692 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7693 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7694 ("sqlite3_step fail:%d", rc));
7696 if (rc == SQLITE_DONE) {
7697 EM_DEBUG_EXCEPTION(" no matched rule found...");
7698 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7705 if (hStmt != NULL) {
7706 rc = sqlite3_finalize(hStmt);
7707 if (rc != SQLITE_OK) {
7708 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7709 error = EMAIL_ERROR_DB_FAILURE;
7713 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7718 EM_DEBUG_FUNC_END("ret [%d]", ret);
7722 INTERNAL_FUNC int emstorage_add_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7724 EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7726 if (!rule) { /* only global rule supported. */
7727 EM_DEBUG_LOG("rule is NULL");
7728 if (err_code != NULL)
7729 *err_code = EMAIL_ERROR_INVALID_PARAM;
7733 int rc, rc_2, ret = false;
7734 int error = EMAIL_ERROR_NONE;
7735 DB_STMT hStmt = NULL;
7736 char sql_query_string[QUERY_SIZE] = {0, };
7738 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7740 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7744 sql = "SELECT max(rowid) FROM mail_rule_tbl;";
7746 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7747 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7748 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7750 if (NULL == result[1])
7753 rc = atoi(result[1])+1;
7755 sqlite3_free_table(result);
7759 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7760 "INSERT INTO mail_rule_tbl VALUES "
7761 "(?" /* account id */
7763 ", ?" /* filter_name */
7767 ", ?" /* action_type */
7768 ", ?" /* target_mailbox_id */
7770 ", ?)"); /* flag2 */
7772 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc_2);
7773 if (rc_2 != SQLITE_OK) {
7774 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc_2, sqlite3_errmsg(local_db_handle));
7775 error = EMAIL_ERROR_DB_FAILURE;
7779 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL, rule->account_id);
7780 _bind_stmt_field_data_int(hStmt, RULE_ID_IDX_IN_MAIL_RULE_TBL, rule->rule_id);
7781 _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);
7782 _bind_stmt_field_data_int(hStmt, TYPE_IDX_IN_MAIL_RULE_TBL, rule->type);
7783 _bind_stmt_field_data_string(hStmt, VALUE_IDX_IN_MAIL_RULE_TBL, (char*)rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7784 _bind_stmt_field_data_string(hStmt, VALUE2_IDX_IN_MAIL_RULE_TBL, (char*)rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7785 _bind_stmt_field_data_int(hStmt, ACTION_TYPE_IDX_IN_MAIL_RULE_TBL, rule->action_type);
7786 _bind_stmt_field_data_int(hStmt, TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL, rule->target_mailbox_id);
7787 _bind_stmt_field_data_int(hStmt, FLAG1_IDX_IN_MAIL_RULE_TBL, rule->flag1);
7788 _bind_stmt_field_data_int(hStmt, FLAG2_IDX_IN_MAIL_RULE_TBL, rule->flag2);
7790 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7791 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7792 ("sqlite3_step fail:%d", rc));
7797 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7798 if (hStmt != NULL) {
7799 rc = sqlite3_finalize(hStmt);
7800 if (rc != SQLITE_OK) {
7801 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7802 error = EMAIL_ERROR_DB_FAILURE;
7806 if (err_code != NULL)
7809 EM_DEBUG_FUNC_END("ret [%d]", ret);
7813 INTERNAL_FUNC int emstorage_delete_rule(char *multi_user_name, int rule_id, int transaction, int *err_code)
7815 EM_DEBUG_FUNC_BEGIN("rule_id[%d], transaction[%d], err_code[%p]", rule_id, transaction, err_code);
7817 if (rule_id <= 0) { /* only global rule supported. */
7818 EM_DEBUG_EXCEPTION("rule_id[%d]", rule_id);
7820 if (err_code != NULL)
7821 *err_code = EMAIL_ERROR_INVALID_PARAM;
7825 int rc, ret = false;
7826 int error = EMAIL_ERROR_NONE;
7827 char sql_query_string[QUERY_SIZE] = {0, };
7828 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7829 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7831 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7832 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7833 if (error != EMAIL_ERROR_NONE) {
7834 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7838 rc = sqlite3_changes(local_db_handle);
7840 EM_DEBUG_EXCEPTION(" no matched rule found...");
7842 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7849 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7851 if (err_code != NULL)
7854 EM_DEBUG_FUNC_END("ret [%d]", ret);
7858 INTERNAL_FUNC int emstorage_free_rule(emstorage_rule_tbl_t** rule_list, int count, int *err_code)
7860 EM_DEBUG_FUNC_BEGIN("rule_list[%p], conut[%d], err_code[%p]", rule_list, count, err_code);
7863 int error = EMAIL_ERROR_NONE;
7866 if (!rule_list || !*rule_list) {
7867 EM_DEBUG_EXCEPTION(" rule_list[%p], conut[%d]", rule_list, count);
7869 error = EMAIL_ERROR_INVALID_PARAM;
7873 emstorage_rule_tbl_t* p = *rule_list;
7876 for (; i < count; i++) {
7877 EM_SAFE_FREE(p[i].value);
7880 EM_SAFE_FREE(p); *rule_list = NULL;
7884 if (err_code != NULL)
7887 EM_DEBUG_FUNC_END("ret [%d]", ret);
7891 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)
7893 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);
7895 if (!total && !unseen) {
7896 EM_DEBUG_EXCEPTION(" accoun_id[%d], mailbox_id[%d], total[%p], unseen[%p]", account_id, mailbox_id, total, unseen);
7897 if (err_code != NULL)
7898 *err_code = EMAIL_ERROR_INVALID_PARAM;
7902 int rc = -1, ret = false;
7903 int error = EMAIL_ERROR_NONE;
7904 DB_STMT hStmt = NULL;
7905 char sql_query_string[QUERY_SIZE] = {0, };
7906 char *replaced_mailbox_name = NULL;
7908 memset(&sql_query_string, 0x00, sizeof(sql_query_string));
7909 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7910 EMSTORAGE_START_READ_TRANSACTION(transaction);
7913 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl");
7915 if (account_id != ALL_ACCOUNT) {
7916 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);
7918 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);
7919 } else if (mailbox_id)
7920 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);
7922 #ifdef USE_GET_RECORD_COUNT_API
7925 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7926 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF2; },
7927 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7929 *total = atoi(result[1]);
7930 sqlite3_free_table(result);
7933 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7934 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF2; },
7935 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7937 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7938 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF2; },
7939 ("sqlite3_step fail:%d", rc));
7940 _get_stmt_field_data_int(hStmt, total, 0);
7941 #endif /* USE_GET_RECORD_COUNT_API */
7945 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl WHERE flags_seen_field = 0"); /* fSEEN = 0x01 */
7947 if (account_id != ALL_ACCOUNT)
7948 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND account_id = %d", account_id);
7951 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);
7953 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND mailbox_type NOT IN (3, 5)");
7956 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7957 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7958 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7960 *unseen = atoi(result[1]);
7961 sqlite3_free_table(result);
7969 #ifndef USE_PREPARED_QUERY_
7970 if (hStmt != NULL) {
7971 rc = sqlite3_finalize(hStmt);
7972 if (rc != SQLITE_OK) {
7973 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7974 error = EMAIL_ERROR_DB_FAILURE;
7979 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7981 EM_SAFE_FREE(replaced_mailbox_name);
7983 if (err_code != NULL)
7986 EM_DEBUG_FUNC_END("ret [%d]", ret);
7990 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)
7992 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
7994 if (mail_id <= 0 || !mail) {
7995 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
7996 if (err_code != NULL)
7997 *err_code = EMAIL_ERROR_INVALID_PARAM;
8002 emstorage_mail_tbl_t* p_data_tbl = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
8004 if (p_data_tbl == NULL) {
8005 EM_DEBUG_EXCEPTION("malloc failed...");
8006 if (err_code != NULL)
8007 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8011 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8012 DB_STMT hStmt = NULL;
8013 char sql_query_string[QUERY_SIZE] = {0, };
8015 int rc, ret = false;
8016 int error = EMAIL_ERROR_NONE;
8018 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8019 EMSTORAGE_START_READ_TRANSACTION(transaction);
8022 case RETRIEVE_SUMMARY:
8023 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8024 "SELECT account_id, "
8027 "server_mail_status, "
8028 "server_mailbox_name, "
8032 "file_path_mime_entity, "
8033 "flags_seen_field, "
8037 "thread_item_count "
8038 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8041 case RETRIEVE_FIELDS_FOR_DELETE:
8042 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8043 "SELECT account_id, "
8045 "server_mail_status, "
8046 "server_mailbox_name, "
8048 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8051 case RETRIEVE_ACCOUNT:
8052 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8053 "SELECT account_id "
8054 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8058 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8059 "SELECT account_id, "
8060 "flags_seen_field, "
8063 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8067 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
8068 error = EMAIL_ERROR_INVALID_PARAM;
8072 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
8074 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8075 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8076 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8079 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8080 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8081 ("sqlite3_step fail:%d", rc));
8083 if (rc == SQLITE_DONE) {
8084 EM_DEBUG_LOG("no matched mail found...");
8085 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8089 case RETRIEVE_SUMMARY:
8090 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8091 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
8092 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
8093 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
8094 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
8095 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8096 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, col_index++);
8097 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, col_index++);
8098 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, col_index++);
8099 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8100 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), col_index++);
8101 _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), col_index++);
8102 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8103 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_item_count), col_index++);
8106 case RETRIEVE_FIELDS_FOR_DELETE:
8107 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8108 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
8109 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
8110 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
8111 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8114 case RETRIEVE_ACCOUNT:
8115 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8119 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8120 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8121 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8122 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
8131 else if (p_data_tbl != NULL)
8132 emstorage_free_mail(&p_data_tbl, 1, NULL);
8134 if (hStmt != NULL) {
8135 rc = sqlite3_finalize(hStmt);
8136 if (rc != SQLITE_OK) {
8137 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8138 error = EMAIL_ERROR_DB_FAILURE;
8143 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8145 if (err_code != NULL)
8148 EM_DEBUG_FUNC_END("ret [%d]", ret);
8152 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)
8154 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);
8157 int error = EMAIL_ERROR_NONE;
8158 int query_string_length = 0;
8159 int i = 0, item_count = 0, rc = -1, field_count, col_index, cur_sql_query_string = 0;
8160 char **result = NULL;
8161 char *sql_query_string = NULL;
8162 emstorage_mail_tbl_t* p_data_tbl = NULL;
8163 sqlite3 *local_db_handle = NULL;
8165 if (number_of_mails <= 0 || !mail_ids) {
8166 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8167 if (err_code != NULL)
8168 *err_code = EMAIL_ERROR_INVALID_PARAM;
8172 p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * number_of_mails);
8174 query_string_length = (sizeof(char) * 8 * number_of_mails) + 512;
8175 sql_query_string = (char*)em_malloc(query_string_length);
8177 if (p_data_tbl == NULL || sql_query_string == NULL) {
8178 EM_DEBUG_EXCEPTION("malloc failed...");
8180 EM_SAFE_FREE(p_data_tbl);
8181 EM_SAFE_FREE(sql_query_string);
8183 if (err_code != NULL)
8184 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8188 local_db_handle = emstorage_get_db_connection(multi_user_name);
8190 EMSTORAGE_START_READ_TRANSACTION(transaction);
8193 case RETRIEVE_SUMMARY:
8194 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8195 "SELECT account_id, "
8198 "server_mail_status, "
8199 "server_mailbox_name, "
8203 "file_path_mime_entity, "
8205 "flags_seen_field, "
8209 "thread_item_count "
8210 "FROM mail_tbl WHERE mail_id in (");
8214 case RETRIEVE_FIELDS_FOR_DELETE:
8215 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8216 "SELECT account_id, "
8218 "server_mail_status, "
8219 "server_mailbox_name, "
8221 "FROM mail_tbl WHERE mail_id in (");
8225 case RETRIEVE_ACCOUNT:
8226 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8227 "SELECT account_id FROM mail_tbl WHERE mail_id in (");
8232 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8233 "SELECT account_id, "
8236 "flags_seen_field, "
8238 "FROM mail_tbl WHERE mail_id in (");
8243 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
8244 error = EMAIL_ERROR_INVALID_PARAM;
8248 for (i = 0; i < number_of_mails; i++)
8249 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_string_length, "%d,", mail_ids[i]);
8250 sql_query_string[EM_SAFE_STRLEN(sql_query_string) - 1] = ')';
8252 EM_DEBUG_LOG_SEC("Query [%s], Length [%d]", sql_query_string, EM_SAFE_STRLEN(sql_query_string));
8254 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &item_count, 0, NULL), rc);
8255 if (SQLITE_OK != rc && -1 != rc) {
8256 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
8257 error = EMAIL_ERROR_DB_FAILURE;
8261 if (rc == SQLITE_DONE) {
8262 EM_DEBUG_LOG("no matched mail found...");
8263 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8267 EM_DEBUG_LOG("item_count [%d]", item_count);
8269 if (number_of_mails != item_count) {
8270 EM_DEBUG_EXCEPTION("Can't find all emails");
8271 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8275 col_index = field_count;
8277 for (i = 0; i < item_count; i++) {
8279 case RETRIEVE_SUMMARY:
8280 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8281 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8282 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8283 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8284 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8285 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8286 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
8287 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
8288 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
8289 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 0, col_index++);
8290 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8291 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
8292 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
8293 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8294 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
8297 case RETRIEVE_FIELDS_FOR_DELETE:
8298 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8299 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8300 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8301 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8302 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8305 case RETRIEVE_ACCOUNT:
8306 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8310 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8311 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8312 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8313 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8314 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8325 emstorage_free_mail(&p_data_tbl, number_of_mails, NULL);
8328 sqlite3_free_table(result);
8330 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8332 EM_SAFE_FREE(sql_query_string);
8334 if (err_code != NULL)
8337 EM_DEBUG_FUNC_END("ret [%d]", ret);
8341 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)
8343 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8345 if (mail_id <= 0 || !mail) {
8346 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
8347 if (err_code != NULL)
8348 *err_code = EMAIL_ERROR_INVALID_PARAM;
8352 int ret = false, error = EMAIL_ERROR_NONE, count;
8353 char conditional_clause[QUERY_SIZE] = {0, };
8354 emstorage_mail_tbl_t* p_data_tbl = NULL;
8356 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8357 EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8359 if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8360 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8369 else if (p_data_tbl != NULL)
8370 emstorage_free_mail(&p_data_tbl, 1, &error);
8372 if (err_code != NULL)
8375 EM_DEBUG_FUNC_END("ret [%d]", ret);
8379 #ifdef __FEATURE_BODY_SEARCH__
8380 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)
8382 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
8384 if (mail_id <= 0 || !mail_text) {
8385 EM_DEBUG_EXCEPTION("mail_id[%d], mail_text[%p]", mail_id, mail_text);
8386 if (err_code != NULL)
8387 *err_code = EMAIL_ERROR_INVALID_PARAM;
8392 int error = EMAIL_ERROR_NONE;
8394 char conditional_clause[QUERY_SIZE] = {0, };
8395 emstorage_mail_text_tbl_t *p_data_tbl = NULL;
8397 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8398 EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8400 if (!emstorage_query_mail_text_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8401 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8409 *mail_text = p_data_tbl;
8410 else if (p_data_tbl != NULL)
8411 emstorage_free_mail_text(&p_data_tbl, 1, &error);
8413 if (err_code != NULL)
8416 EM_DEBUG_FUNC_END("ret [%d]", ret);
8421 INTERNAL_FUNC int emstorage_mail_search_start(char *multi_user_name,
8422 emstorage_search_filter_t *search,
8426 DB_STMT *search_handle,
8431 EM_DEBUG_FUNC_BEGIN("search[%p], account_id[%d], mailbox_id[%d], sorting[%d], "
8432 "search_handle[%p], searched[%p], transaction[%d], err_code[%p]",
8433 search, account_id, mailbox_id, sorting, search_handle,
8434 searched, transaction, err_code);
8436 if (!search_handle || !searched) {
8437 if (err_code != NULL)
8438 *err_code = EMAIL_ERROR_INVALID_PARAM;
8439 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8440 EM_DEBUG_FUNC_END("false");
8444 emstorage_search_filter_t* p = search;
8445 int error = EMAIL_ERROR_NONE;
8446 DB_STMT hStmt = NULL;
8447 char sql_query_string[QUERY_SIZE] = {0, };
8448 int rc, ret = false;
8449 int and = false, mail_count = 0;
8451 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8452 EMSTORAGE_START_READ_TRANSACTION(transaction);
8454 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl");
8456 if (account_id != ALL_ACCOUNT) {
8457 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);
8462 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);
8468 if (!strncmp(p->key_type, "subject", strlen("subject"))) {
8469 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);
8471 } else if (!strncmp(p->key_type, "full_address_from", strlen("full_address_from"))) {
8472 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);
8474 } else if (!strncmp(p->key_type, "full_address_to", strlen("full_address_to"))) {
8475 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);
8477 } else if (!strncmp(p->key_type, "email_address", strlen("email_address"))) {
8478 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);
8486 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");
8488 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
8491 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8492 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8493 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8496 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8497 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8498 ("sqlite3_step fail:%d", rc));
8502 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &mail_count, NULL, NULL), rc);
8503 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
8504 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8506 sqlite3_free_table(result);
8512 *search_handle = hStmt;
8513 *searched = mail_count;
8514 EM_DEBUG_LOG("mail_count [%d]", mail_count);
8516 if (hStmt != NULL) {
8517 rc = sqlite3_finalize(hStmt);
8518 if (rc != SQLITE_OK) {
8519 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8520 error = EMAIL_ERROR_DB_FAILURE;
8524 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8527 if (err_code != NULL)
8530 EM_DEBUG_FUNC_END("ret [%d]", ret);
8534 INTERNAL_FUNC int emstorage_mail_search_result(DB_STMT search_handle, emstorage_mail_field_type_t type, void** data, int transaction, int *err_code)
8536 EM_DEBUG_FUNC_BEGIN("search_handle[%d], type[%d], data[%p], transaction[%d], err_code[%p]", search_handle, type, data, transaction, err_code);
8538 if (search_handle == 0 || !data) {
8539 EM_DEBUG_EXCEPTION(" search_handle[%d], type[%d], data[%p]", search_handle, type, data);
8541 if (err_code != NULL)
8542 *err_code = EMAIL_ERROR_INVALID_PARAM;
8546 emstorage_mail_tbl_t* p_data_tbl = NULL;
8547 DB_STMT hStmt = search_handle;
8548 int rc, ret = false;
8549 int error = EMAIL_ERROR_NONE;
8553 _get_stmt_field_data_int(hStmt, (int *)data, MAIL_ID_IDX_IN_MAIL_TBL);
8556 case RETRIEVE_ENVELOPE:
8558 if (!(p_data_tbl = em_malloc(sizeof(emstorage_mail_tbl_t)))) {
8559 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
8560 error = EMAIL_ERROR_OUT_OF_MEMORY;
8564 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8565 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8566 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_size), MAIL_SIZE_IDX_IN_MAIL_TBL);
8567 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8568 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_from), 1, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL);
8569 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_to), 1, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL);
8570 _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject), 1, SUBJECT_IDX_IN_MAIL_TBL);
8571 _get_stmt_field_data_int(hStmt, &(p_data_tbl->body_download_status), BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL);
8572 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL);
8573 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8574 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8575 _get_stmt_field_data_time_t(hStmt, &(p_data_tbl->date_time), DATETIME_IDX_IN_MAIL_TBL);
8576 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL);
8577 _get_stmt_field_data_int(hStmt, &(p_data_tbl->DRM_status), DRM_STATUS_IDX_IN_MAIL_TBL);
8578 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->priority), PRIORITY_IDX_IN_MAIL_TBL);
8579 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), SAVE_STATUS_IDX_IN_MAIL_TBL);
8580 _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), LOCK_STATUS_IDX_IN_MAIL_TBL);
8581 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->report_status), REPORT_STATUS_IDX_IN_MAIL_TBL);
8582 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8583 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->meeting_request_status), MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL);
8584 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->message_class), MESSAGE_CLASS_IDX_IN_MAIL_TBL);
8585 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->digest_type), DIGEST_TYPE_IDX_IN_MAIL_TBL);
8586 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->smime_type), SMIME_TYPE_IDX_IN_MAIL_TBL);
8587 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->scheduled_sending_time), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8588 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->remaining_resend_times), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8589 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->tag_id), TAG_ID_IDX_IN_MAIL_TBL);
8590 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->eas_data_length), EAS_DATA_LENGTH_IDX_IN_MAIL_TBL);
8591 _get_stmt_field_data_blob(hStmt, (void**)&(p_data_tbl->eas_data), EAS_DATA_IDX_IN_MAIL_TBL);
8593 if (type == RETRIEVE_ALL) {
8594 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8595 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8596 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_reply), 1, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL);
8597 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_cc), 1, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL);
8598 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_bcc), 1, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL);
8599 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_return), 1, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL);
8600 _get_stmt_field_data_string(hStmt, &(p_data_tbl->message_id), 0, MESSAGE_ID_IDX_IN_MAIL_TBL);
8601 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8602 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8603 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_count), ATTACHMENT_COUNT_IDX_IN_MAIL_TBL);
8604 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8605 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->replied_time), REPLIED_TIME_IDX_IN_MAIL_TBL);
8606 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->forwarded_time), FORWARDED_TIME_IDX_IN_MAIL_TBL);
8607 _get_stmt_field_data_string(hStmt, &(p_data_tbl->default_charset), 0, DEFAULT_CHARSET_IDX_IN_MAIL_TBL);
8610 if (p_data_tbl->body_download_status) {
8613 if (p_data_tbl->file_path_html) {
8614 if (stat(p_data_tbl->file_path_html, &buf) == -1)
8615 p_data_tbl->body_download_status = 0;
8616 } else if (p_data_tbl->file_path_plain) {
8617 if (stat(p_data_tbl->file_path_plain, &buf) == -1)
8618 p_data_tbl->body_download_status = 0;
8620 p_data_tbl->body_download_status = 0;
8623 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8626 case RETRIEVE_SUMMARY:
8627 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
8628 EM_DEBUG_EXCEPTION(" malloc failed...");
8630 error = EMAIL_ERROR_OUT_OF_MEMORY;
8634 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8636 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8637 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8638 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8639 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8640 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8642 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8645 case RETRIEVE_ADDRESS:
8646 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
8647 EM_DEBUG_EXCEPTION(" malloc failed...");
8648 error = EMAIL_ERROR_OUT_OF_MEMORY;
8652 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8653 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8654 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8655 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8656 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8663 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8664 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8665 ("sqlite3_step fail:%d", rc));
8671 if (err_code != NULL)
8674 if (ret == false && p_data_tbl)
8675 emstorage_free_mail(&p_data_tbl, 1, NULL);
8677 EM_DEBUG_FUNC_END("ret [%d]", ret);
8681 INTERNAL_FUNC int emstorage_mail_search_end(DB_STMT search_handle, int transaction, int *err_code)
8683 EM_DEBUG_FUNC_BEGIN("search_handle[%d], transaction[%d], err_code[%p]", search_handle, transaction, err_code);
8685 int error = EMAIL_ERROR_NONE;
8686 int rc, ret = false;
8688 if (search_handle == 0) {
8689 EM_DEBUG_EXCEPTION(" search_handle[%d]", search_handle);
8690 error = EMAIL_ERROR_INVALID_PARAM;
8694 DB_STMT hStmt = search_handle;
8696 rc = sqlite3_finalize(hStmt);
8697 if (rc != SQLITE_OK) {
8698 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8699 error = EMAIL_ERROR_DB_FAILURE;
8705 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8707 if (err_code != NULL)
8710 EM_DEBUG_FUNC_END("ret [%d]", ret);
8714 INTERNAL_FUNC int emstorage_change_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t *mail, int transaction, int *err_code)
8716 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8718 if (mail_id <= 0 || !mail) {
8719 EM_DEBUG_EXCEPTION(" mail_id[%d], mail[%p]", mail_id, mail);
8721 if (err_code != NULL)
8722 *err_code = EMAIL_ERROR_INVALID_PARAM;
8726 DB_STMT hStmt = NULL;
8727 char sql_query_string[QUERY_SIZE] = {0, };
8730 int error = EMAIL_ERROR_NONE;
8732 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8733 char mailbox_id_param_string[10] = {0,};
8735 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
8737 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8738 "UPDATE mail_tbl SET"
8743 ", server_mail_status = ?"
8744 ", server_mailbox_name = ?"
8745 ", server_mail_id = ?"
8746 ", reference_mail_id = ?"
8747 ", full_address_from = ?"
8748 ", full_address_reply = ?" /* 10 */
8749 ", full_address_to = ?"
8750 ", full_address_cc = ?"
8751 ", full_address_bcc = ?"
8752 ", full_address_return = ?"
8754 ", body_download_status = ?"
8755 ", file_path_plain = ?"
8756 ", file_path_html = ?"
8757 ", file_path_mime_entity = ?"
8759 ", flags_seen_field = ?"
8760 ", flags_deleted_field = ?"
8761 ", flags_flagged_field = ?"
8762 ", flags_answered_field = ?"
8763 ", flags_recent_field = ?"
8764 ", flags_draft_field = ?"
8765 ", flags_forwarded_field = ?"
8771 ", report_status = ?"
8772 ", preview_text = ?"
8774 ", scheduled_sending_time = ?"
8775 ", remaining_resend_times = ?"
8777 ", replied_time = ?"
8778 ", forwarded_time = ?"
8779 ", default_charset = ?"
8780 ", eas_data_length = ?"
8782 " WHERE mail_id = %d AND account_id != 0 "
8786 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8787 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8788 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8790 _bind_stmt_field_data_int(hStmt, i++, mail->mail_id);
8791 _bind_stmt_field_data_int(hStmt, i++, mail->account_id);
8792 _bind_stmt_field_data_int(hStmt, i++, mail->mailbox_id);
8793 _bind_stmt_field_data_int(hStmt, i++, mail->mail_size);
8794 _bind_stmt_field_data_int(hStmt, i++, mail->server_mail_status);
8795 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mailbox_name, 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
8796 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mail_id, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
8797 _bind_stmt_field_data_int(hStmt, i++, mail->reference_mail_id);
8798 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
8799 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
8800 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
8801 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
8802 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
8803 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
8804 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8805 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
8806 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8807 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8808 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
8809 _bind_stmt_field_data_int(hStmt, i++, mail->date_time);
8810 _bind_stmt_field_data_char(hStmt, i++, mail->flags_seen_field);
8811 _bind_stmt_field_data_char(hStmt, i++, mail->flags_deleted_field);
8812 _bind_stmt_field_data_char(hStmt, i++, mail->flags_flagged_field);
8813 _bind_stmt_field_data_char(hStmt, i++, mail->flags_answered_field);
8814 _bind_stmt_field_data_char(hStmt, i++, mail->flags_recent_field);
8815 _bind_stmt_field_data_char(hStmt, i++, mail->flags_draft_field);
8816 _bind_stmt_field_data_char(hStmt, i++, mail->flags_forwarded_field);
8817 _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
8818 _bind_stmt_field_data_int(hStmt, i++, mail->priority);
8819 _bind_stmt_field_data_int(hStmt, i++, mail->save_status);
8820 _bind_stmt_field_data_int(hStmt, i++, mail->lock_status);
8821 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->message_id, 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
8822 _bind_stmt_field_data_int(hStmt, i++, mail->report_status);
8823 _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8824 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
8825 _bind_stmt_field_data_int(hStmt, i++, mail->scheduled_sending_time);
8826 _bind_stmt_field_data_int(hStmt, i++, mail->remaining_resend_times);
8827 _bind_stmt_field_data_int(hStmt, i++, mail->tag_id);
8828 _bind_stmt_field_data_int(hStmt, i++, mail->replied_time);
8829 _bind_stmt_field_data_int(hStmt, i++, mail->forwarded_time);
8830 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
8831 _bind_stmt_field_data_int(hStmt, i++, mail->eas_data_length);
8832 _bind_stmt_field_data_blob(hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
8834 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8835 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8836 ("sqlite3_step fail:%d", rc));
8838 rc = sqlite3_changes(local_db_handle);
8840 EM_DEBUG_LOG(" no matched mail found...");
8841 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8848 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
8849 if (hStmt != NULL) {
8850 rc = sqlite3_finalize(hStmt);
8851 if (rc != SQLITE_OK) {
8852 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8853 error = EMAIL_ERROR_DB_FAILURE;
8858 if (error == EMAIL_ERROR_NONE && mail) {
8859 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
8860 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail->mail_id, mailbox_id_param_string, 0))
8861 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventfailed [NOTI_MAIL_UPDATE]");
8864 if (err_code != NULL)
8867 EM_DEBUG_FUNC_END("ret [%d]", ret);
8872 * emstorage_clean_save_status(int save_status, int *err_code) - set the all mail status to the set value
8876 INTERNAL_FUNC int emstorage_clean_save_status(char *multi_user_name, int save_status, int *err_code)
8878 EM_DEBUG_FUNC_BEGIN("save_status[%d], err_code[%p]", save_status, err_code);
8880 EM_IF_NULL_RETURN_VALUE(err_code, false);
8883 int error = EMAIL_ERROR_NONE;
8885 char sql_query_string[QUERY_SIZE] = {0, };
8886 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8888 memset(sql_query_string, 0x00, sizeof(sql_query_string));
8889 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);
8891 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
8892 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
8893 if (error != EMAIL_ERROR_NONE) {
8894 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
8898 rc = sqlite3_changes(local_db_handle);
8900 EM_DEBUG_LOG(" No Matched Mail Exists ");
8901 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8908 if (err_code != NULL)
8911 EM_DEBUG_FUNC_END("ret [%d]", ret);
8915 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)
8917 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);
8919 int error = EMAIL_ERROR_NONE;
8922 int cur_mail_id_string = 0;
8923 int mail_id_string_buffer_length = 0;
8924 int parameter_string_length = 0;
8925 char *sql_query_string = NULL;
8926 char *mail_id_string_buffer = NULL;
8927 char *parameter_string = NULL;
8928 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8929 email_mail_attribute_type target_mail_attribute_type = 0;
8931 if (!mail_ids || !field_name || account_id == 0) {
8932 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8933 if (err_code != NULL)
8934 *err_code = EMAIL_ERROR_INVALID_PARAM;
8938 if ((error = emcore_get_attribute_type_by_mail_field_name(field_name, &target_mail_attribute_type)) != EMAIL_ERROR_NONE) {
8939 EM_DEBUG_EXCEPTION("emstorageemcore_get_attribute_type_by_mail_field_name failed [%d]", error);
8940 if (err_code != NULL)
8945 /* Generating mail id list string */
8946 mail_id_string_buffer_length = MAIL_ID_STRING_LENGTH * mail_ids_count;
8948 mail_id_string_buffer = em_malloc(mail_id_string_buffer_length);
8950 if (!mail_id_string_buffer) {
8951 EM_DEBUG_EXCEPTION("em_mallocfailed");
8952 if (err_code != NULL)
8953 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8957 for (i = 0; i < mail_ids_count; i++)
8958 cur_mail_id_string += SNPRINTF_OFFSET(mail_id_string_buffer, cur_mail_id_string, mail_id_string_buffer_length, "%d,", mail_ids[i]);
8960 if (EM_SAFE_STRLEN(mail_id_string_buffer) > 1)
8961 mail_id_string_buffer[EM_SAFE_STRLEN(mail_id_string_buffer) - 1] = NULL_CHAR;
8963 /* Generating notification parameter string */
8964 parameter_string_length = mail_id_string_buffer_length + EM_SAFE_STRLEN(field_name) + 2;
8965 parameter_string = em_malloc(parameter_string_length);
8967 if (!parameter_string) {
8968 EM_DEBUG_EXCEPTION("em_mallocfailed");
8969 if (err_code != NULL)
8970 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8971 EM_SAFE_FREE(mail_id_string_buffer);
8975 SNPRINTF(parameter_string, parameter_string_length, "%s%c%s", field_name, 0x01, mail_id_string_buffer);
8976 query_size = EM_SAFE_STRLEN(mail_id_string_buffer) + EM_SAFE_STRLEN(field_name) + 250;
8978 sql_query_string = em_malloc(query_size);
8979 if (sql_query_string == NULL) {
8980 EM_DEBUG_EXCEPTION("em_mallocfailed");
8981 error = EMAIL_ERROR_OUT_OF_MEMORY;
8984 /* Write query string */
8985 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);
8987 EM_DEBUG_LOG_DEV("sql_query_string [%s]", sql_query_string);
8990 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
8991 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
8992 if (error != EMAIL_ERROR_NONE) {
8993 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
8997 if (sqlite3_changes(local_db_handle) == 0)
8998 EM_DEBUG_LOG("no mail matched...");
9003 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9005 if (error == EMAIL_ERROR_NONE && parameter_string) {
9006 if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, target_mail_attribute_type, parameter_string, value))
9007 EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_MAIL_FIELD_UPDATE [%s,%d]", field_name, value);
9010 EM_SAFE_FREE(mail_id_string_buffer);
9011 EM_SAFE_FREE(parameter_string);
9012 EM_SAFE_FREE(sql_query_string);
9015 if (err_code != NULL)
9018 EM_DEBUG_FUNC_END("error [%d]", error);
9022 #ifdef __FEATURE_BODY_SEARCH__
9023 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)
9025 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
9027 if (mail_id <= 0 || !mail_text) {
9028 EM_DEBUG_EXCEPTION(" mail_id[%d], mail_text[%p]", mail_id, mail_text);
9029 if (err_code != NULL)
9030 *err_code = EMAIL_ERROR_INVALID_PARAM;
9035 int error = EMAIL_ERROR_NONE;
9036 DB_STMT hStmt = NULL;
9037 char sql_query_string[QUERY_SIZE] = {0, };
9042 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9043 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9045 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9046 "UPDATE mail_text_tbl SET"
9048 " WHERE mail_id = %d AND account_id != 0"
9050 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9052 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9053 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9054 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9057 _bind_stmt_field_data_string(hStmt, i++, (char *)mail_text->body_text, 0, -1);
9059 if (hStmt != NULL) {
9060 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9061 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9062 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9064 rc = sqlite3_changes(local_db_handle);
9066 EM_DEBUG_LOG(" no matched mail found...");
9067 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9075 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9077 if (hStmt != NULL) {
9078 rc = sqlite3_finalize(hStmt);
9079 if (rc != SQLITE_OK) {
9080 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9081 error = EMAIL_ERROR_DB_FAILURE;
9086 if (err_code != NULL)
9089 EM_DEBUG_FUNC_END("ret [%d]", ret);
9094 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)
9096 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
9099 int error = EMAIL_ERROR_NONE;
9104 DB_STMT hStmt = NULL;
9105 char sql_query_string[QUERY_SIZE] = {0, };
9106 char mailbox_id_param_string[10] = {0,};
9107 sqlite3 *local_db_handle = NULL;
9109 if (mail_id <= 0 || !mail) {
9110 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], mail[%p]", mail_id, type, mail);
9111 if (err_code != NULL)
9112 *err_code = EMAIL_ERROR_INVALID_PARAM;
9116 local_db_handle = emstorage_get_db_connection(multi_user_name);
9118 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9122 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9123 "UPDATE mail_tbl SET"
9124 " body_download_status = ?"
9125 ", file_path_plain = ?"
9126 ", file_path_html = ?"
9127 ", file_path_mime_entity = ?"
9128 ", flags_seen_field = ?"
9129 ", flags_deleted_field = ?"
9130 ", flags_flagged_field = ?"
9131 ", flags_answered_field = ?"
9132 ", flags_recent_field = ?"
9133 ", flags_draft_field = ?"
9134 ", flags_forwarded_field = ?"
9136 ", attachment_count = ?"
9138 ", meeting_request_status = ? "
9139 ", message_class = ? "
9140 ", digest_type = ? "
9142 " WHERE mail_id = %d AND account_id != 0"
9146 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9147 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9148 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9151 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9152 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9153 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9154 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9155 _bind_stmt_field_data_int(hStmt, i++, mail->flags_seen_field);
9156 _bind_stmt_field_data_int(hStmt, i++, mail->flags_deleted_field);
9157 _bind_stmt_field_data_int(hStmt, i++, mail->flags_flagged_field);
9158 _bind_stmt_field_data_int(hStmt, i++, mail->flags_answered_field);
9159 _bind_stmt_field_data_int(hStmt, i++, mail->flags_recent_field);
9160 _bind_stmt_field_data_int(hStmt, i++, mail->flags_draft_field);
9161 _bind_stmt_field_data_int(hStmt, i++, mail->flags_forwarded_field);
9162 _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
9163 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9164 _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9165 _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
9166 _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
9167 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9168 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9171 case UPDATE_MAILBOX: {
9173 emstorage_mailbox_tbl_t *mailbox_tbl;
9175 if ((err = emstorage_get_mailbox_by_id(multi_user_name, mail->mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
9176 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_id failed [%d]", err);
9180 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9181 "UPDATE mail_tbl SET"
9182 " mailbox_id = '%d'"
9183 ",mailbox_type = '%d'"
9184 " WHERE mail_id = %d AND account_id != 0"
9185 , mailbox_tbl->mailbox_id
9186 , mailbox_tbl->mailbox_type
9191 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9192 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9193 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9196 _bind_stmt_field_data_string(hStmt, i++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
9198 emstorage_free_mailbox(&mailbox_tbl, 1, NULL); /*prevent 26251*/
9204 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9205 "UPDATE mail_tbl SET"
9206 " flags_seen_field = %d"
9207 ",flags_deleted_field = %d"
9208 ",flags_flagged_field = %d"
9209 ",flags_answered_field = %d"
9210 ",flags_recent_field = %d"
9211 ",flags_draft_field = %d"
9212 ",flags_forwarded_field = %d"
9213 " WHERE mail_id = %d AND account_id != 0"
9214 , mail->flags_seen_field
9215 , mail->flags_deleted_field
9216 , mail->flags_flagged_field
9217 , mail->flags_answered_field
9218 , mail->flags_recent_field
9219 , mail->flags_draft_field
9220 , mail->flags_forwarded_field
9222 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9225 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9226 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9227 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9231 case UPDATE_EXTRA_FLAG:
9232 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9233 "UPDATE mail_tbl SET"
9235 ", save_status = %d"
9236 ", lock_status = %d"
9237 ", report_status = %d"
9239 " WHERE mail_id = %d AND account_id != 0"
9243 , mail->report_status
9246 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9249 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9250 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9251 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9254 case UPDATE_STICKY_EXTRA_FLAG:
9255 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9256 "UPDATE mail_tbl SET"
9258 " WHERE mail_id = %d AND account_id != 0"
9261 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9264 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9265 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9266 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9270 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9271 "UPDATE mail_tbl SET"
9272 " full_address_from = ?"
9273 ", full_address_reply = ?"
9274 ", full_address_to = ?"
9275 ", full_address_cc = ?"
9276 ", full_address_bcc = ?"
9277 ", full_address_return = ?"
9279 ", file_path_plain = ?"
9281 ", flags_seen_field = ?"
9282 ", flags_deleted_field = ?"
9283 ", flags_flagged_field = ?"
9284 ", flags_answered_field = ?"
9285 ", flags_recent_field = ?"
9286 ", flags_draft_field = ?"
9287 ", flags_forwarded_field = ?"
9291 ", report_status = ?"
9293 ", file_path_html = ?"
9294 ", file_path_mime_entity = ?"
9296 ", preview_text = ?"
9297 ", body_download_status = ?"
9298 ", attachment_count = ?"
9299 ", inline_content_count = ?"
9300 ", meeting_request_status = ?"
9301 ", message_class = ?"
9304 ", scheduled_sending_time = ?"
9305 ", remaining_resend_times = ?"
9307 ", replied_time = ?"
9308 ", forwarded_time = ?"
9309 ", default_charset = ?"
9310 ", eas_data_length = ?"
9312 " WHERE mail_id = %d AND account_id != 0"
9316 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9317 EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
9318 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9319 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9321 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
9322 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
9323 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
9324 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
9325 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
9326 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
9327 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9328 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9329 _bind_stmt_field_data_time_t(hStmt, i++, mail->date_time);
9330 _bind_stmt_field_data_char(hStmt, i++, mail->flags_seen_field);
9331 _bind_stmt_field_data_char(hStmt, i++, mail->flags_deleted_field);
9332 _bind_stmt_field_data_char(hStmt, i++, mail->flags_flagged_field);
9333 _bind_stmt_field_data_char(hStmt, i++, mail->flags_answered_field);
9334 _bind_stmt_field_data_char(hStmt, i++, mail->flags_recent_field);
9335 _bind_stmt_field_data_char(hStmt, i++, mail->flags_draft_field);
9336 _bind_stmt_field_data_char(hStmt, i++, mail->flags_forwarded_field);
9337 _bind_stmt_field_data_int(hStmt, i++, mail->priority);
9338 _bind_stmt_field_data_int(hStmt, i++, mail->save_status);
9339 _bind_stmt_field_data_int(hStmt, i++, mail->lock_status);
9340 _bind_stmt_field_data_int(hStmt, i++, mail->report_status);
9341 _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
9342 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9343 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9344 _bind_stmt_field_data_int(hStmt, i++, mail->mail_size);
9345 _bind_stmt_field_data_nstring(hStmt, i++, (char*)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9346 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9347 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9348 _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
9349 _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
9350 _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
9351 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9352 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9353 _bind_stmt_field_data_int(hStmt, i++, mail->scheduled_sending_time);
9354 _bind_stmt_field_data_int(hStmt, i++, mail->remaining_resend_times);
9355 _bind_stmt_field_data_int(hStmt, i++, mail->tag_id);
9356 _bind_stmt_field_data_int(hStmt, i++, mail->replied_time);
9357 _bind_stmt_field_data_int(hStmt, i++, mail->forwarded_time);
9358 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
9359 _bind_stmt_field_data_int(hStmt, i++, mail->eas_data_length);
9360 _bind_stmt_field_data_blob(hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
9363 case UPDATE_DATETIME: {
9364 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9365 "UPDATE mail_tbl SET"
9366 " date_time = '%ld'"
9367 " WHERE mail_id = %d AND account_id != 0"
9371 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9372 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9373 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9377 case UPDATE_FROM_CONTACT_INFO:
9378 EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_FROM_CONTACT_INFO");
9379 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9380 "UPDATE mail_tbl SET"
9381 " email_address_sender = ?,"
9382 " WHERE mail_id = %d",
9387 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9388 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9389 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9391 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9394 case UPDATE_TO_CONTACT_INFO:
9395 EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_TO_CONTACT_INFO");
9396 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9397 "UPDATE mail_tbl SET"
9398 " email_address_recipient = ?,"
9399 " WHERE mail_id = %d",
9404 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9405 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9406 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9408 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9411 case UPDATE_ALL_CONTACT_INFO:
9412 EM_DEBUG_LOG("emstorage_change_mail_field - mail change type is UPDATE_ALL_CONTACT_INFO");
9413 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9414 "UPDATE mail_tbl SET"
9415 " email_address_sender = ?,"
9416 " email_address_recipient = ?,"
9417 " WHERE mail_id = %d",
9422 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9423 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9424 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9426 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9427 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9431 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
9432 case UPDATE_PARTIAL_BODY_DOWNLOAD:
9434 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9435 "UPDATE mail_tbl SET"
9436 " body_download_status = ?"
9437 ", file_path_plain = ?"
9438 ", file_path_html = ?"
9439 ", file_path_mime_entity = ?"
9440 ", attachment_count = ?"
9441 ", inline_content_count = ?"
9442 ", preview_text = ?"
9445 " WHERE mail_id = %d"
9449 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9450 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9451 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9454 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9455 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9456 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9457 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9458 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9459 _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
9460 _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9461 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9462 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9467 case UPDATE_FILE_PATH:
9468 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9469 "UPDATE mail_tbl SET"
9470 ", file_path_plain = ?"
9471 ", file_path_html = ?"
9472 ", file_path_mime_entity = ?"
9473 " WHERE mail_id = %d"
9477 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9478 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
9479 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9480 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9482 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9483 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9484 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9488 EM_DEBUG_LOG(" type[%d]", type);
9490 error = EMAIL_ERROR_INVALID_PARAM;
9494 if (hStmt != NULL) {
9496 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9497 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
9498 ("sqlite3_step fail:%d", rc));
9499 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9500 ("sqlite3_step fail:%d", rc));
9501 rc = sqlite3_changes(local_db_handle);
9503 EM_DEBUG_LOG(" no matched mail found...");
9504 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9509 if (mail->account_id == 0) {
9510 emstorage_mail_tbl_t* mail_for_account_tbl = NULL;
9511 if (!emstorage_get_mail_field_by_id(multi_user_name, mail_id, RETRIEVE_ACCOUNT, &mail_for_account_tbl, true, &error) || !mail_for_account_tbl) {
9512 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id error [%d]", error);
9515 mail->account_id = mail_for_account_tbl->account_id;
9516 if (mail_for_account_tbl)
9517 emstorage_free_mail(&mail_for_account_tbl, 1, NULL);
9523 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9525 if (hStmt != NULL) {
9526 rc = sqlite3_finalize(hStmt);
9527 if (rc != SQLITE_OK) {
9528 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9529 error = EMAIL_ERROR_DB_FAILURE;
9534 if (error == EMAIL_ERROR_NONE && move_flag != 1 && transaction) {
9535 if (!emstorage_get_mailbox_id_by_mailbox_type(multi_user_name, mail->account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &mailbox_id, false, &error))
9536 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_id_by_mailbox_type error [%d]", error);
9538 if (mail->mailbox_id == mailbox_id) {
9539 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
9540 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, mailbox_id_param_string, type))
9541 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventerror [ NOTI_MAIL_UPDATE ] >>>> ");
9543 /* h.gahlaut@samsung.com: Jan 10, 2011 Publishing noti to refresh outbox when email sending status changes */
9544 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, NULL, type))
9545 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventerror [ NOTI_MAIL_UPDATE ]");
9549 if (err_code != NULL)
9552 EM_DEBUG_FUNC_END("ret [%d]", ret);
9556 INTERNAL_FUNC int emstorage_increase_mail_id(char *multi_user_name, int *mail_id, int transaction, int *err_code)
9558 EM_DEBUG_FUNC_BEGIN("mail_id[%p], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9560 int rc, ret = false;
9561 int error = EMAIL_ERROR_NONE;
9562 int latest_mail_id = 0;
9563 sqlite3 *local_db_handle = NULL;
9564 char *sql = "SELECT MAX(mail_id) FROM mail_tbl;";
9565 char **result = NULL;
9567 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9568 _timedlock_shm_mutex(mapped_for_generating_mail_id, 2);
9569 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9571 ret = vconf_get_int(VCONF_KEY_LATEST_MAIL_ID, &latest_mail_id);
9572 if (ret < 0 || latest_mail_id == 0) {
9573 EM_DEBUG_LOG("vconf_get_int() failed [%d] or latest_mail_id is zero", ret);
9575 local_db_handle = emstorage_get_db_connection(multi_user_name);
9577 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9578 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
9579 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9580 if (NULL == result[1])
9583 rc = atoi(result[1]) + 1;
9585 sqlite3_free_table(result);
9586 latest_mail_id = rc;
9591 ret = vconf_set_int(VCONF_KEY_LATEST_MAIL_ID, latest_mail_id);
9594 *mail_id = latest_mail_id;
9596 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9597 _unlockshm_mutex(mapped_for_generating_mail_id);
9598 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9604 if (err_code != NULL)
9607 EM_DEBUG_FUNC_END("ret [%d]", ret);
9611 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)
9613 EM_PROFILE_BEGIN(profile_emstorage_add_mail);
9614 EM_DEBUG_FUNC_BEGIN("mail_tbl_data[%p], get_id[%d], transaction[%d], err_code[%p]", mail_tbl_data, get_id, transaction, err_code);
9616 if (!mail_tbl_data) {
9617 EM_DEBUG_EXCEPTION("mail_tbl_data[%p], get_id[%d]", mail_tbl_data, get_id);
9618 if (err_code != NULL)
9619 *err_code = EMAIL_ERROR_INVALID_PARAM;
9623 int rc, ret = false;
9624 int error = EMAIL_ERROR_NONE;
9625 char sql_query_string[QUERY_SIZE] = {0, };
9626 DB_STMT hStmt = NULL;
9628 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9630 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9633 /* increase unique id */
9634 char *sql = "SELECT max(rowid) FROM mail_tbl;";
9637 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9638 /* EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
9639 ("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle))); */
9640 if (rc != SQLITE_OK) {
9641 EM_DEBUG_EXCEPTION("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle));
9642 error = EMAIL_ERROR_DB_FAILURE;
9643 sqlite3_free_table(result);
9647 if (NULL == result[1])
9650 rc = atoi(result[1])+1;
9652 sqlite3_free_table(result);
9654 mail_tbl_data->mail_id = rc;
9655 mail_tbl_data->thread_id = rc;
9658 if (mail_tbl_data->date_time == 0)
9659 mail_tbl_data->date_time = time(NULL);
9661 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9662 "INSERT INTO mail_tbl VALUES "
9664 ", ?" /* account_id */
9665 ", ?" /* mailbox_id */
9666 ", ?" /* mailbox_type */
9669 ", ?" /* date_time */
9670 ", ?" /* server_mail_status */
9671 ", ?" /* server_mailbox_name */
9672 ", ?" /* server_mail_id */
9673 ", ?" /* message_id */
9675 ", ?" /* reference_mail_id */
9676 ", ?" /* full_address_from */
9677 ", ?" /* full_address_reply */
9678 ", ?" /* full_address_to */
9679 ", ?" /* full_address_cc */
9681 ", ?" /* full_address_bcc */
9682 ", ?" /* full_address_return */
9683 ", ?" /* email_address_sender */
9684 ", ?" /* email_address_recipient */
9685 ", ?" /* alias_sender */
9687 ", ?" /* alias_recipient */
9688 ", ?" /* body_download_status */
9689 ", ?" /* file_path_plain */
9690 ", ?" /* file_path_html */
9691 ", ?" /* file_path_mime_entity */
9693 ", ?" /* mail_size */
9694 ", ?" /* flags_seen_field */
9695 ", ?" /* flags_deleted_field */
9696 ", ?" /* flags_flagged_field */
9697 ", ?" /* flags_answered_field */
9699 ", ?" /* flags_recent_field */
9700 ", ?" /* flags_draft_field */
9701 ", ?" /* flags_forwarded_field */
9702 ", ?" /* DRM_status */
9703 ", ?" /* priority */
9705 ", ?" /* save_status */
9706 ", ?" /* lock_status */
9707 ", ?" /* report_status */
9708 ", ?" /* attachment_count */
9709 ", ?" /* inline_content_count */
9711 ", ?" /* thread_id */
9712 ", ?" /* thread_item_count */
9713 ", ?" /* preview_text */
9714 ", ?" /* meeting_request_status */
9715 ", ?" /* message_class */
9717 ", ?" /* digest_type */
9718 ", ?" /* smime_type */
9719 ", ?" /* scheduled_sending_time */
9720 ", ?" /* remaining_resend_times */
9723 ", ?" /* replied_time */
9724 ", ?" /* forwarded_time */
9725 ", ?" /* default charset */
9726 ", ?" /* eas_data_length */
9727 ", ?" /* eas_data */
9728 ", ?" /* user_name */
9731 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle,
9733 EM_SAFE_STRLEN(sql_query_string),
9737 if (rc != SQLITE_OK) {
9738 EM_DEBUG_EXCEPTION("sqlite3_prepare error [%d] [%s] SQL(%s) ",
9739 rc, sql_query_string, sqlite3_errmsg(local_db_handle));
9740 error = EMAIL_ERROR_DB_FAILURE;
9744 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mail_id);
9745 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, mail_tbl_data->account_id);
9746 _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_id);
9747 _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_type);
9748 _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9750 _bind_stmt_field_data_int(hStmt, DATETIME_IDX_IN_MAIL_TBL, mail_tbl_data->date_time);
9751 _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->server_mail_status);
9752 _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);
9753 _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);
9754 _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);
9756 _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, mail_tbl_data->reference_mail_id);
9757 _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);
9758 _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);
9759 _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);
9760 _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);
9762 _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);
9763 _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);
9764 _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);
9765 _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);
9766 _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);
9768 _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);
9769 _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->body_download_status);
9770 _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);
9771 _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);
9772 _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);
9774 _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, mail_tbl_data->mail_size);
9775 _bind_stmt_field_data_int(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_seen_field);
9776 _bind_stmt_field_data_int(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_deleted_field);
9777 _bind_stmt_field_data_int(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_flagged_field);
9778 _bind_stmt_field_data_int(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_answered_field);
9780 _bind_stmt_field_data_int(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_recent_field);
9781 _bind_stmt_field_data_int(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_draft_field);
9782 _bind_stmt_field_data_int(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_forwarded_field);
9783 _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->DRM_status);
9784 _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, mail_tbl_data->priority);
9786 _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->save_status);
9787 _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->lock_status);
9788 _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->report_status);
9789 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->attachment_count);
9790 _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->inline_content_count);
9792 _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, mail_tbl_data->thread_id);
9793 _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->thread_item_count);
9794 _bind_stmt_field_data_nstring(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9795 _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->meeting_request_status);
9796 _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, mail_tbl_data->message_class);
9798 _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->digest_type);
9799 _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->smime_type);
9800 _bind_stmt_field_data_int(hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->scheduled_sending_time);
9801 _bind_stmt_field_data_int(hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, mail_tbl_data->remaining_resend_times);
9802 _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, mail_tbl_data->tag_id);
9804 _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->replied_time);
9805 _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->forwarded_time);
9806 _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);
9807 _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, mail_tbl_data->eas_data_length);
9808 _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, (void*)mail_tbl_data->eas_data, mail_tbl_data->eas_data_length);
9809 _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);
9811 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9812 if (rc == SQLITE_FULL) {
9813 EM_DEBUG_EXCEPTION("sqlite3_step error [%d]", rc);
9814 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
9817 if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
9818 EM_DEBUG_EXCEPTION("sqlite3_step error [%d]", rc);
9819 error = EMAIL_ERROR_DB_FAILURE;
9825 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9827 if (hStmt != NULL) {
9828 rc = sqlite3_finalize(hStmt);
9829 if (rc != SQLITE_OK) {
9830 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9831 error = EMAIL_ERROR_DB_FAILURE;
9835 if (err_code != NULL)
9838 EM_PROFILE_END(profile_emstorage_add_mail);
9839 EM_DEBUG_FUNC_END("ret [%d]", ret);
9843 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)
9845 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);
9847 int ret = false, i, cur_conditional_clause = 0;
9848 int error = EMAIL_ERROR_NONE;
9849 int target_account_id;
9850 int conditional_clause_len = 0;
9851 char *sql_query_string = NULL, *conditional_clause = NULL;
9852 emstorage_mailbox_tbl_t *result_mailbox = NULL;
9853 email_mailbox_type_e target_mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED;
9854 char* target_mailbox_name = NULL;
9856 if (!mail_ids || input_mailbox_id <= 0) {
9857 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9858 if (err_code != NULL)
9859 *err_code = EMAIL_ERROR_INVALID_PARAM;
9863 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9865 if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &result_mailbox)) != EMAIL_ERROR_NONE || !result_mailbox) {
9866 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", error);
9867 if (err_code != NULL)
9872 if (result_mailbox->mailbox_name) {
9873 if (strstr(result_mailbox->mailbox_name, "'")) {
9874 target_mailbox_name = em_replace_all_string(result_mailbox->mailbox_name, "'", "''");
9876 target_mailbox_name = strdup(result_mailbox->mailbox_name);
9880 target_mailbox_type = result_mailbox->mailbox_type;
9881 target_account_id = result_mailbox->account_id;
9882 emstorage_free_mailbox(&result_mailbox, 1, NULL);
9884 conditional_clause_len = (sizeof(char) * 8 * number_of_mails) + 512;
9885 conditional_clause = em_malloc(conditional_clause_len);
9886 if (conditional_clause == NULL) {
9887 EM_DEBUG_EXCEPTION("em_mallocfailed");
9888 error = EMAIL_ERROR_OUT_OF_MEMORY;
9891 cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE mail_id in (");
9893 for (i = 0; i < number_of_mails; i++)
9894 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
9897 char *last_comma = rindex(conditional_clause, ',');
9898 if (last_comma) *last_comma = ')'; /* replace , with) */
9900 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9902 /* Updating a mail_tbl */
9904 sql_query_string = em_malloc(conditional_clause_len);
9905 if (sql_query_string == NULL) {
9906 EM_DEBUG_EXCEPTION("em_mallocfailed");
9907 error = EMAIL_ERROR_OUT_OF_MEMORY;
9910 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);
9911 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9913 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9914 if (error != EMAIL_ERROR_NONE) {
9915 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9919 /* Updating a mail_attachment_tbl */
9920 memset(sql_query_string, 0x00, conditional_clause_len);
9921 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);
9922 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9923 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9924 if (error != EMAIL_ERROR_NONE) {
9925 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9929 /* Updating a mail_meeting_tbl */
9930 memset(sql_query_string, 0x00, conditional_clause_len);
9931 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);
9932 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9933 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9934 if (error != EMAIL_ERROR_NONE) {
9935 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9939 #ifdef __FEATURE_BODY_SEARCH__
9940 /* Updating mail_text_tbl */
9941 memset(sql_query_string, 0x00, conditional_clause_len);
9942 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);
9943 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9944 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9945 if (error != EMAIL_ERROR_NONE) {
9946 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9951 /* Updating a mail_read_mail_uid_tbl */
9952 memset(conditional_clause, 0x00, conditional_clause_len);
9953 cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE local_uid in (");
9955 for (i = 0; i < number_of_mails; i++)
9956 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
9959 last_comma = rindex(conditional_clause, ',');
9960 if (last_comma) *last_comma = ')'; /* replace , with) */
9962 memset(sql_query_string, 0x00, conditional_clause_len);
9963 SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_read_mail_uid_tbl SET mailbox_name = '%s', mailbox_id = %d, account_id = %d %s", target_mailbox_name, input_mailbox_id, target_account_id, conditional_clause);
9964 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9965 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9966 if (error != EMAIL_ERROR_NONE) {
9967 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9974 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9976 EM_SAFE_FREE(target_mailbox_name);
9977 EM_SAFE_FREE(conditional_clause);
9978 EM_SAFE_FREE(sql_query_string);
9980 if (err_code != NULL)
9983 EM_DEBUG_FUNC_END("ret [%d]", ret);
9987 INTERNAL_FUNC int emstorage_delete_mail(char *multi_user_name, int mail_id, int from_server, int transaction, int *err_code)
9989 EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9992 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
9993 if (err_code != NULL)
9994 *err_code = EMAIL_ERROR_INVALID_PARAM;
9999 int error = EMAIL_ERROR_NONE;
10000 char sql_query_string[QUERY_SIZE] = {0, };
10001 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10002 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10004 memset(sql_query_string, 0x00, sizeof(sql_query_string));
10006 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id = %d ", mail_id);
10007 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10008 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10009 if (error != EMAIL_ERROR_NONE) {
10010 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10017 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10019 if (err_code != NULL)
10022 EM_DEBUG_FUNC_END("ret [%d]", ret);
10026 INTERNAL_FUNC int emstorage_delete_multiple_mails(char *multi_user_name, int mail_ids[], int number_of_mails, int transaction, int *err_code)
10028 EM_DEBUG_FUNC_BEGIN("mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", mail_ids, number_of_mails, transaction, err_code);
10030 int ret = false, i, cur_sql_query_string = 0;
10031 int error = EMAIL_ERROR_NONE;
10032 int query_size = 0;
10033 char *sql_query_string = NULL;
10036 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10037 if (err_code != NULL)
10038 *err_code = EMAIL_ERROR_INVALID_PARAM;
10042 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10043 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10045 query_size = (sizeof(char) * 8 * number_of_mails) + 512;
10046 sql_query_string = em_malloc(query_size);
10047 if (sql_query_string == NULL) {
10048 EM_DEBUG_EXCEPTION("em_mallocfailed");
10049 error = EMAIL_ERROR_OUT_OF_MEMORY;
10053 cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_tbl WHERE mail_id in (");
10055 for (i = 0; i < number_of_mails; i++)
10056 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
10058 /* prevent 34414 */
10059 char *last_comma = rindex(sql_query_string, ',');
10060 if (last_comma != NULL) *last_comma = ')'; /* replace , with) */
10062 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10063 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10064 if (error != EMAIL_ERROR_NONE) {
10065 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10069 #ifdef __FEATURE_BODY_SEARCH__
10070 /* delete mail_text from mail_text_tbl */
10071 cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_text_tbl WHERE mail_id in (");
10073 for (i = 0; i < number_of_mails; i++)
10074 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
10076 last_comma = rindex(sql_query_string, ',');
10077 *last_comma = ')'; /* replace , with) */
10079 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10080 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10081 if (error != EMAIL_ERROR_NONE) {
10082 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10090 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10092 EM_SAFE_FREE(sql_query_string);
10093 if (err_code != NULL)
10096 EM_DEBUG_FUNC_END("ret [%d]", ret);
10100 INTERNAL_FUNC int emstorage_delete_mail_by_account(char *multi_user_name, int account_id, int transaction, int *err_code)
10102 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
10104 if (account_id < FIRST_ACCOUNT_ID) {
10105 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
10107 if (err_code != NULL)
10108 *err_code = EMAIL_ERROR_INVALID_PARAM;
10112 int rc, ret = false;
10113 int error = EMAIL_ERROR_NONE;
10114 char sql_query_string[QUERY_SIZE] = {0, };
10116 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10117 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10119 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d", account_id);
10120 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10121 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10122 if (error != EMAIL_ERROR_NONE) {
10123 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10127 rc = sqlite3_changes(local_db_handle);
10129 EM_DEBUG_LOG(" no mail found...");
10130 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10133 /* Delete all mails mail_read_mail_uid_tbl table based on account id */
10134 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
10135 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10136 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10137 if (error != EMAIL_ERROR_NONE) {
10138 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10142 rc = sqlite3_changes(local_db_handle);
10144 EM_DEBUG_LOG("No mail found...");
10145 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10148 #ifdef __FEATURE_BODY_SEARCH__
10149 /* Delete all mail_text in mail_text_tbl table based on account id */
10150 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE account_id = %d", account_id);
10151 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10152 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10153 if (error != EMAIL_ERROR_NONE) {
10154 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10158 rc = sqlite3_changes(local_db_handle);
10160 EM_DEBUG_LOG("No mail found...");
10161 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10168 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10170 if (error == EMAIL_ERROR_NONE) {
10171 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_WITH_ACCOUNT, account_id, 0 , NULL, 0))
10172 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAIL_DELETE_ALL ]");
10175 if (err_code != NULL)
10178 EM_DEBUG_FUNC_END("ret [%d]", ret);
10182 INTERNAL_FUNC int emstorage_delete_mail_by_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox, int transaction, int *err_code)
10184 EM_DEBUG_FUNC_BEGIN("mailbox[%p], transaction[%d], err_code[%p]", mailbox, transaction, err_code);
10186 if (mailbox == NULL) {
10187 EM_DEBUG_EXCEPTION("mailbox [%p]", mailbox);
10188 if (err_code != NULL)
10189 *err_code = EMAIL_ERROR_INVALID_PARAM;
10194 int error = EMAIL_ERROR_NONE;
10195 char sql_query_string[QUERY_SIZE] = {0, };
10196 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10197 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10199 if (strcmp(mailbox->mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
10200 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);
10201 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10202 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10203 if (error != EMAIL_ERROR_NONE) {
10204 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10208 /* Delete Mails from mail_read_mail_uid_tbl */
10209 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);
10210 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10211 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10212 if (error != EMAIL_ERROR_NONE) {
10213 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10217 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);
10218 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10219 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10220 if (error != EMAIL_ERROR_NONE) {
10221 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10225 /* Delete Mails from mail_read_mail_uid_tbl */
10226 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);
10227 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10228 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10229 if (error != EMAIL_ERROR_NONE) {
10230 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10234 #ifdef __FEATURE_BODY_SEARCH__
10235 /* Delete Mails from mail_text_tbl */
10236 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);
10237 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10238 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10239 if (error != EMAIL_ERROR_NONE) {
10240 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10248 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10250 if (error == EMAIL_ERROR_NONE) {
10251 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_ALL, mailbox->account_id, mailbox->mailbox_id , mailbox->mailbox_name, 0))
10252 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventFailed [ NOTI_MAIL_DELETE_ALL ] >>>> ");
10255 if (err_code != NULL)
10258 EM_DEBUG_FUNC_END("ret [%d]", ret);
10262 INTERNAL_FUNC int emstorage_free_mail(emstorage_mail_tbl_t** mail_list, int count, int *err_code)
10264 EM_DEBUG_FUNC_BEGIN("mail_list[%p], count[%d], err_code[%p]", mail_list, count, err_code);
10267 if ((mail_list == NULL) || (*mail_list == NULL)) {
10268 EM_DEBUG_EXCEPTION("mail_ilst[%p], count[%d]", mail_list, count);
10271 *err_code = EMAIL_ERROR_INVALID_PARAM;
10275 emstorage_mail_tbl_t* p = *mail_list;
10278 for (; i < count; i++, p++) {
10279 EM_SAFE_FREE(p->server_mailbox_name);
10280 EM_SAFE_FREE(p->server_mail_id);
10281 EM_SAFE_FREE(p->full_address_from);
10282 EM_SAFE_FREE(p->full_address_reply);
10283 EM_SAFE_FREE(p->full_address_to);
10284 EM_SAFE_FREE(p->full_address_cc);
10285 EM_SAFE_FREE(p->full_address_bcc);
10286 EM_SAFE_FREE(p->full_address_return);
10287 EM_SAFE_FREE(p->subject);
10288 EM_SAFE_FREE(p->file_path_plain);
10289 EM_SAFE_FREE(p->file_path_html);
10290 EM_SAFE_FREE(p->file_path_mime_entity);
10291 EM_SAFE_FREE(p->message_id);
10292 EM_SAFE_FREE(p->email_address_sender);
10293 EM_SAFE_FREE(p->email_address_recipient);
10294 EM_SAFE_FREE(p->preview_text);
10295 EM_SAFE_FREE(p->alias_sender);
10296 EM_SAFE_FREE(p->alias_recipient);
10297 EM_SAFE_FREE(p->default_charset);
10298 EM_SAFE_FREE(p->pgp_password);
10299 EM_SAFE_FREE(p->eas_data);
10300 EM_SAFE_FREE(p->user_name);
10302 EM_SAFE_FREE(*mail_list);
10305 if (err_code != NULL)
10306 *err_code = EMAIL_ERROR_NONE;
10308 EM_DEBUG_FUNC_END();
10312 #ifdef __FEATURE_BODY_SEARCH__
10313 INTERNAL_FUNC void emstorage_free_mail_text(emstorage_mail_text_tbl_t** mail_text_list, int count, int *err_code)
10315 EM_DEBUG_FUNC_BEGIN("mail_text_list[%p], count[%d], err_code[%p]", mail_text_list, count, err_code);
10318 if ((mail_text_list == NULL) || (*mail_text_list == NULL)) {
10319 EM_DEBUG_LOG("Nothing to free: mail_text_list[%p]", mail_text_list);
10323 emstorage_mail_text_tbl_t *p = *mail_text_list;
10326 for (; i < count; i++, p++) {
10327 EM_SAFE_FREE(p->body_text);
10329 EM_SAFE_FREE(*mail_text_list);
10332 EM_DEBUG_FUNC_END();
10336 INTERNAL_FUNC int emstorage_get_attachment_count(char *multi_user_name, int mail_id, int *count, int transaction, int *err_code)
10338 EM_DEBUG_FUNC_BEGIN("mail_id[%d], count[%p], transaction[%d], err_code[%p]", mail_id, count, transaction, err_code);
10340 if (mail_id <= 0 || !count) {
10341 EM_DEBUG_EXCEPTION("mail_id[%d], count[%p]", mail_id, count);
10342 if (err_code != NULL)
10343 *err_code = EMAIL_ERROR_INVALID_PARAM;
10347 int rc = -1, ret = false;
10348 int error = EMAIL_ERROR_NONE;
10349 char sql_query_string[QUERY_SIZE] = {0, };
10351 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10352 EMSTORAGE_START_READ_TRANSACTION(transaction);
10354 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
10358 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10359 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10360 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10362 *count = atoi(result[1]);
10363 sqlite3_free_table(result);
10368 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10370 if (err_code != NULL)
10373 EM_DEBUG_FUNC_END("ret [%d]", ret);
10377 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)
10379 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);
10381 if (input_mail_id <= 0 || !output_attachment_list || !output_attachment_count) {
10382 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10383 return EMAIL_ERROR_INVALID_PARAM;
10386 int error = EMAIL_ERROR_NONE;
10389 char **result = NULL;
10390 char sql_query_string[QUERY_SIZE] = {0, };
10391 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10392 DB_STMT hStmt = NULL;
10393 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10395 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
10396 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", input_mail_id);
10397 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10398 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10399 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10401 *output_attachment_count = atoi(result[1]);
10402 sqlite3_free_table(result);
10404 if (*output_attachment_count == 0) {
10405 error = EMAIL_ERROR_NONE;
10409 p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * (*output_attachment_count));
10412 EM_DEBUG_EXCEPTION("em_mallocfailed...");
10413 error = EMAIL_ERROR_OUT_OF_MEMORY;
10417 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE mail_id = %d ORDER BY attachment_id", input_mail_id);
10418 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
10420 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10421 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)));
10423 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10424 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
10426 if (rc == SQLITE_DONE) {
10427 EM_DEBUG_EXCEPTION("no matched attachment found...");
10428 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10431 for (i = 0; i < *output_attachment_count; i++) {
10432 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10433 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10434 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10435 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10436 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10437 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10438 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10439 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10440 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10441 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10442 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10443 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10444 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10445 EM_DEBUG_LOG("attachment[%d].attachment_id : %d", i, p_data_tbl[i].attachment_id);
10446 EM_DEBUG_LOG("attachment_mime_type : %s", p_data_tbl[i].attachment_mime_type);
10447 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10448 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
10453 if (error == EMAIL_ERROR_NONE)
10454 *output_attachment_list = p_data_tbl;
10455 else if (p_data_tbl != NULL)
10456 emstorage_free_attachment(&p_data_tbl, *output_attachment_count, NULL);
10459 rc = sqlite3_finalize(hStmt);
10460 if (rc != SQLITE_OK) {
10461 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10462 error = EMAIL_ERROR_DB_FAILURE;
10463 if (*output_attachment_list)
10464 emstorage_free_attachment(output_attachment_list, *output_attachment_count, NULL); /* prevent */
10468 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
10470 EM_DEBUG_FUNC_END("error [%d]", error);
10474 INTERNAL_FUNC int emstorage_get_attachment(char *multi_user_name, int attachment_id, emstorage_attachment_tbl_t **attachment, int transaction, int *err_code)
10476 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], transaction[%d], err_code[%p]", attachment_id, attachment, transaction, err_code);
10478 if (attachment_id <= 0 || !attachment) {
10479 EM_DEBUG_EXCEPTION("attachment_id[%d], attachment[%p]", attachment_id, attachment);
10480 if (err_code != NULL)
10481 *err_code = EMAIL_ERROR_INVALID_PARAM;
10485 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10486 int rc, ret = false;
10487 int error = EMAIL_ERROR_NONE;
10488 char sql_query_string[QUERY_SIZE] = {0, };
10490 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10491 EMSTORAGE_START_READ_TRANSACTION(transaction);
10493 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
10495 sqlite3_stmt* hStmt = NULL;
10497 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10498 EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
10500 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10501 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10504 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10505 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10506 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10508 if (rc == SQLITE_DONE) {
10509 EM_DEBUG_LOG("no matched attachment found...");
10510 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10514 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10515 EM_DEBUG_EXCEPTION("malloc failed...");
10516 error = EMAIL_ERROR_OUT_OF_MEMORY;
10520 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10521 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10522 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10523 _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10524 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10525 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10526 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10527 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10528 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10529 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10530 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10531 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10532 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10534 #ifdef __ATTACHMENT_OPTI__
10535 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10536 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10543 *attachment = p_data_tbl;
10545 if (hStmt != NULL) {
10546 rc = sqlite3_finalize(hStmt);
10547 if (rc != SQLITE_OK) {
10548 EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10549 error = EMAIL_ERROR_DB_FAILURE;
10553 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10555 if (err_code != NULL)
10558 EM_DEBUG_FUNC_END("ret [%d]", ret);
10562 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)
10564 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);
10566 if (mail_id <= 0 || nth <= 0 || !attachment_tbl) {
10567 EM_DEBUG_EXCEPTION(" mail_id[%d], nth[%d], attachment[%p]", mail_id, nth, attachment_tbl);
10569 if (err_code != NULL)
10570 *err_code = EMAIL_ERROR_INVALID_PARAM;
10574 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10576 int rc, ret = false;
10577 int error = EMAIL_ERROR_NONE;
10578 char sql_query_string[QUERY_SIZE] = {0, };
10580 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10581 EMSTORAGE_START_READ_TRANSACTION(transaction);
10583 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));
10584 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10586 DB_STMT hStmt = NULL;
10588 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10589 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10590 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10593 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10594 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10595 ("sqlite3_step fail:%d", rc));
10597 if (rc == SQLITE_DONE) {
10598 EM_DEBUG_EXCEPTION("no matched attachment found: mail_id[%d] nth[%d]", mail_id, nth);
10599 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10603 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10604 EM_DEBUG_EXCEPTION(" malloc failed...");
10605 error = EMAIL_ERROR_OUT_OF_MEMORY;
10609 p_data_tbl->attachment_id = sqlite3_column_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10610 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10611 p_data_tbl->attachment_name = cpy_str(p);
10612 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10613 p_data_tbl->attachment_path = cpy_str(p);
10614 if ((p = (char *)sqlite3_column_text(hStmt, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10615 p_data_tbl->content_id = cpy_str(p);
10616 p_data_tbl->attachment_size = sqlite3_column_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10617 p_data_tbl->mail_id = sqlite3_column_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10618 p_data_tbl->account_id = sqlite3_column_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10619 p_data_tbl->mailbox_id = sqlite3_column_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10620 p_data_tbl->attachment_save_status = sqlite3_column_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10621 p_data_tbl->attachment_drm_type = sqlite3_column_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10622 p_data_tbl->attachment_drm_method = sqlite3_column_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10623 p_data_tbl->attachment_inline_content_status = sqlite3_column_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10624 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10625 p_data_tbl->attachment_mime_type = cpy_str(p);
10626 #ifdef __ATTACHMENT_OPTI__
10627 p_data_tbl->encoding = sqlite3_column_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10628 if ((p = (char *)sqlite3_column_text(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10629 p_data_tbl->section = cpy_str(p);
10635 *attachment_tbl = p_data_tbl;
10637 if (hStmt != NULL) {
10638 rc = sqlite3_finalize(hStmt);
10639 if (rc != SQLITE_OK) {
10640 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10641 error = EMAIL_ERROR_DB_FAILURE;
10645 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10647 if (err_code != NULL)
10650 EM_DEBUG_FUNC_END("ret [%d]", ret);
10654 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)
10656 EM_DEBUG_FUNC_BEGIN("attachment_path[%p], attachment[%p], transaction[%d], err_code[%p]", attachment_path, attachment, transaction, err_code);
10658 if (attachment_path == NULL || !attachment) {
10659 EM_DEBUG_EXCEPTION("attachment_path[%p], attachment[%p]", attachment_path, attachment);
10660 if (err_code != NULL)
10661 *err_code = EMAIL_ERROR_INVALID_PARAM;
10665 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10666 int rc, ret = false;
10667 int error = EMAIL_ERROR_NONE;
10668 char sql_query_string[QUERY_SIZE] = {0, };
10670 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10671 EMSTORAGE_START_READ_TRANSACTION(transaction);
10673 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_path = '%s'", attachment_path);
10675 sqlite3_stmt* hStmt = NULL;
10677 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10678 EM_DEBUG_LOG_DEV("before sqlite3_prepare hStmt = %p", hStmt);
10680 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10681 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10684 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10685 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10686 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10688 if (rc == SQLITE_DONE) {
10689 EM_DEBUG_LOG("no matched attachment found...");
10690 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10694 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10695 EM_DEBUG_EXCEPTION("malloc failed...");
10696 error = EMAIL_ERROR_OUT_OF_MEMORY;
10700 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10701 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10702 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10703 _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10704 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10705 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10706 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10707 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10708 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10709 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10710 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10711 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10712 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10714 #ifdef __ATTACHMENT_OPTI__
10715 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10716 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10723 *attachment = p_data_tbl;
10725 if (hStmt != NULL) {
10726 rc = sqlite3_finalize(hStmt);
10727 if (rc != SQLITE_OK) {
10728 EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10729 error = EMAIL_ERROR_DB_FAILURE;
10733 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10735 if (err_code != NULL)
10738 EM_DEBUG_FUNC_END("ret [%d]", ret);
10742 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)
10744 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], attachment[%p], transaction[%d], err_code[%p]", mail_id, type, attachment, transaction, err_code);
10746 if (mail_id <= 0 || !attachment) {
10747 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], attachment[%p]", mail_id, type, attachment);
10748 if (err_code != NULL)
10749 *err_code = EMAIL_ERROR_INVALID_PARAM;
10753 int rc, ret = false;
10754 int error = EMAIL_ERROR_NONE;
10755 DB_STMT hStmt = NULL;
10756 char sql_query_string[QUERY_SIZE] = {0, };
10760 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10762 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10765 case UPDATE_MAILBOX:
10766 EM_DEBUG_LOG("UPDATE_MAILBOX");
10767 if (!attachment->mailbox_id) {
10768 EM_DEBUG_EXCEPTION(" attachment->mailbox_id[%d]", attachment->mailbox_id);
10769 error = EMAIL_ERROR_INVALID_PARAM;
10772 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10773 "UPDATE mail_attachment_tbl SET mailbox_id = ? WHERE mail_id = %d", mail_id);
10775 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10776 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10777 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10778 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10780 _bind_stmt_field_data_int(hStmt, i++, attachment->mailbox_id);
10783 case UPDATE_SAVENAME:
10784 EM_DEBUG_LOG("UPDATE_SAVENAME");
10785 if (!attachment->attachment_path) {
10786 EM_DEBUG_EXCEPTION(" attachment->attachment_path[%p]", attachment->attachment_path);
10787 error = EMAIL_ERROR_INVALID_PARAM;
10791 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10792 "UPDATE mail_attachment_tbl SET"
10793 " attachment_size = ?"
10794 ", attachment_save_status = ?"
10795 ", attachment_path = ?"
10796 " WHERE mail_id = %d"
10797 " AND attachment_id = %d"
10798 , attachment->mail_id
10799 , attachment->attachment_id);
10802 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10803 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10804 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10805 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10807 _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_size);
10808 _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_save_status);
10809 _bind_stmt_field_data_string(hStmt, i++, (char *)attachment->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10813 EM_DEBUG_LOG("type[%d]", type);
10814 error = EMAIL_ERROR_INVALID_PARAM;
10817 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10819 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10820 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10821 ("sqlite3_step fail:%d", rc));
10826 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10828 if (hStmt != NULL) {
10829 rc = sqlite3_finalize(hStmt);
10830 if (rc != SQLITE_OK) {
10831 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10832 error = EMAIL_ERROR_DB_FAILURE;
10836 if (err_code != NULL)
10838 EM_DEBUG_FUNC_END("ret [%d]", ret);
10842 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)
10844 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);
10848 int error = EMAIL_ERROR_NONE;
10850 int account_id = 0;
10851 char sql_query_string[QUERY_SIZE] = {0, };
10852 char *replaced_mailbox_name = NULL;
10853 char *replaced_alias = NULL;
10854 sqlite3 *local_db_handle = NULL;
10855 DB_STMT hStmt = NULL;
10856 emstorage_mailbox_tbl_t *old_mailbox_data = NULL;
10858 if (input_mailbox_id <= 0 || !input_new_mailbox_name || !input_new_mailbox_alias) {
10859 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10860 return EMAIL_ERROR_INVALID_PARAM;
10863 if (strstr(input_new_mailbox_name, "'")) {
10864 replaced_mailbox_name = em_replace_all_string(input_new_mailbox_name, "'", "''");
10866 replaced_mailbox_name = strdup(input_new_mailbox_name);
10869 if (strstr(input_new_mailbox_alias, "'")) {
10870 replaced_alias = em_replace_all_string(input_new_mailbox_alias, "'", "''");
10872 replaced_alias = strdup(input_new_mailbox_alias);
10875 local_db_handle = emstorage_get_db_connection(multi_user_name);
10877 if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &old_mailbox_data)) != EMAIL_ERROR_NONE) {
10878 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", error);
10879 EM_SAFE_FREE(replaced_mailbox_name);
10880 EM_SAFE_FREE(replaced_alias);
10884 if (old_mailbox_data == NULL) {
10885 EM_DEBUG_LOG("old_mailbox_data is NULL");
10886 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
10890 account_id = old_mailbox_data->account_id;
10892 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
10894 if (input_eas_data && input_eas_data_length > 0) {
10895 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10896 "UPDATE mail_box_tbl SET"
10897 " mailbox_name = ?"
10900 ",eas_data_length = ?"
10901 " WHERE mailbox_id = ?");
10903 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10905 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10906 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10908 _bind_stmt_field_data_string(hStmt, field_idx++, input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10909 _bind_stmt_field_data_string(hStmt, field_idx++, input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10910 _bind_stmt_field_data_blob(hStmt, field_idx++, input_eas_data, input_eas_data_length);
10911 _bind_stmt_field_data_int(hStmt, field_idx++, input_eas_data_length);
10912 _bind_stmt_field_data_int(hStmt, field_idx++, input_mailbox_id);
10914 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10915 "UPDATE mail_box_tbl SET"
10916 " mailbox_name = ?"
10918 " WHERE mailbox_id = ?");
10920 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10922 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10923 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10925 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10926 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10927 _bind_stmt_field_data_int(hStmt, field_idx++ , input_mailbox_id);
10931 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10932 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
10933 ("sqlite3_step fail:%d", rc));
10934 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10935 ("sqlite3_step fail:%d", rc));
10937 if (sqlite3_changes(local_db_handle) == 0)
10938 EM_DEBUG_LOG("no mail_meeting_tbl matched...");
10943 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, error);
10944 if (error == EMAIL_ERROR_NONE) {
10945 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME, account_id, input_mailbox_id, input_new_mailbox_name, 0))
10946 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAILBOX_RENAME ] >>>> ");
10948 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME_FAIL, account_id, input_mailbox_id, input_new_mailbox_name, error))
10949 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAILBOX_RENAME_FAIL ] >>>> ");
10952 EM_SAFE_FREE(replaced_mailbox_name);
10953 EM_SAFE_FREE(replaced_alias);
10955 if (old_mailbox_data)
10956 emstorage_free_mailbox(&old_mailbox_data, 1, NULL);
10958 if (hStmt != NULL) {
10959 rc = sqlite3_finalize(hStmt);
10960 if (rc != SQLITE_OK) {
10961 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10962 error = EMAIL_ERROR_DB_FAILURE;
10966 EM_DEBUG_FUNC_END("error [%d]", error);
10970 INTERNAL_FUNC int emstorage_get_new_attachment_no(char *multi_user_name, int *attachment_no, int *err_code)
10972 EM_DEBUG_FUNC_BEGIN("attachment_no [%p], err_code[%p]", attachment_no, err_code);
10973 int rc, ret = false;
10974 int error = EMAIL_ERROR_NONE;
10975 char *sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
10978 if (!attachment_no) {
10979 EM_DEBUG_EXCEPTION("Invalid attachment");
10980 if (err_code != NULL)
10981 *err_code = EMAIL_ERROR_INVALID_PARAM;
10985 *attachment_no = -1;
10987 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10990 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
10991 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10992 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
10994 if (NULL == result[1])
10997 rc = atoi(result[1])+1;
10999 sqlite3_free_table(result);
11001 *attachment_no = rc;
11002 EM_DEBUG_LOG("attachment_no [%d]", *attachment_no);
11007 if (err_code != NULL)
11010 EM_DEBUG_FUNC_END("ret [%d]", ret);
11014 INTERNAL_FUNC int emstorage_add_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int iscopy, int transaction, int *err_code)
11016 EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], iscopy[%d], transaction[%d], err_code[%p]", attachment_tbl, iscopy, transaction, err_code);
11020 int rc, ret = false;
11021 int error = EMAIL_ERROR_NONE;
11022 char sql_query_string[QUERY_SIZE] = {0, };
11024 DB_STMT hStmt = NULL;
11025 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11027 if (!attachment_tbl) {
11028 EM_DEBUG_EXCEPTION("attachment_tbl[%p], iscopy[%d]", attachment_tbl, iscopy);
11029 if (err_code != NULL)
11030 *err_code = EMAIL_ERROR_INVALID_PARAM;
11034 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11036 sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
11038 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
11039 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
11040 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
11042 if (NULL == result[1]) rc = 1;
11043 else rc = atoi(result[1]) + 1;
11044 sqlite3_free_table(result);
11046 attachment_tbl->attachment_id = rc;
11048 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11049 "INSERT INTO mail_attachment_tbl VALUES "
11050 "(?" /* attachment_id */
11051 ", ?" /* attachment_name */
11052 ", ?" /* attachment_path */
11053 ", ?" /* content_id */
11054 ", ?" /* attachment_size */
11055 ", ?" /* mail_id */
11056 ", ?" /* account_id */
11057 ", ?" /* mailbox_id */
11058 ", ?" /* attachment_save_status */
11059 ", ?" /* attachment_drm_type */
11060 ", ?" /* attachment_drm_method */
11061 ", ?" /* attachment_inline_content_status */
11062 ", ?" /* attachment_mime_type */
11063 #ifdef __ATTACHMENT_OPTI__
11070 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11071 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11072 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11074 _bind_stmt_field_data_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_id);
11075 _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);
11076 _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);
11077 _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);
11078 _bind_stmt_field_data_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_size);
11079 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mail_id);
11080 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->account_id);
11081 _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mailbox_id);
11082 _bind_stmt_field_data_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_save_status);
11083 _bind_stmt_field_data_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_type);
11084 _bind_stmt_field_data_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_method);
11085 _bind_stmt_field_data_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_inline_content_status);
11086 _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);
11087 #ifdef __ATTACHMENT_OPTI__
11088 _bind_stmt_field_data_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->encoding);
11089 _bind_stmt_field_data_string(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->section, 0, ATTACHMENT_LEN_IN_MAIL_ATTACHMENT_TBL);
11093 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11094 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
11095 ("sqlite3_step fail:%d", rc));
11096 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11097 ("sqlite3_step fail:%d", rc));
11099 rc = sqlite3_changes(local_db_handle);
11101 EM_DEBUG_LOG(" no matched mail found...");
11102 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11109 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11111 if (hStmt != NULL) {
11112 rc = sqlite3_finalize(hStmt);
11113 if (rc != SQLITE_OK) {
11114 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11115 error = EMAIL_ERROR_DB_FAILURE;
11119 if (err_code != NULL)
11122 EM_DEBUG_FUNC_END("ret [%d]", ret);
11126 INTERNAL_FUNC int emstorage_update_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int transaction, int *err_code)
11128 EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], transaction[%d], err_code[%p]", attachment_tbl, transaction, err_code);
11130 int rc, ret = false, field_idx = 0;
11131 int error = EMAIL_ERROR_NONE;
11132 DB_STMT hStmt = NULL;
11133 char sql_query_string[QUERY_SIZE] = {0, };
11135 if (!attachment_tbl) {
11136 EM_DEBUG_EXCEPTION(" attachment_tbl[%p] ", attachment_tbl);
11138 *err_code = EMAIL_ERROR_INVALID_PARAM;
11142 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11144 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11146 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11147 "UPDATE mail_attachment_tbl SET "
11148 " attachment_name = ?"
11149 ", attachment_path = ?"
11151 ", attachment_size = ?"
11155 ", attachment_save_status = ?"
11156 ", attachment_drm_type = ?"
11157 ", attachment_drm_method = ?"
11158 ", attachment_inline_content_status = ? "
11159 ", attachment_mime_type = ? "
11160 " WHERE attachment_id = ?;");
11163 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11165 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11166 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11168 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
11169 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
11170 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->content_id, 0, CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL);
11171 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_size);
11172 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->mail_id);
11173 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->account_id);
11174 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->mailbox_id);
11175 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_save_status);
11176 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_drm_type);
11177 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_drm_method);
11178 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_inline_content_status);
11179 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_mime_type, 0, ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL);
11180 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_id);
11183 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11184 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
11185 ("sqlite3_step fail:%d", rc));
11186 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11187 ("sqlite3_step fail:%d", rc));
11189 rc = sqlite3_changes(local_db_handle);
11191 EM_DEBUG_LOG(" no matched mail found...");
11192 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11199 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11200 if (hStmt != NULL) {
11201 rc = sqlite3_finalize(hStmt);
11202 if (rc != SQLITE_OK) {
11203 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11204 error = EMAIL_ERROR_DB_FAILURE;
11208 if (err_code != NULL)
11211 EM_DEBUG_FUNC_END("ret [%d]", ret);
11215 INTERNAL_FUNC int emstorage_delete_attachment_on_db(char *multi_user_name, int attachment_id, int transaction, int *err_code)
11217 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], transaction[%d], err_code[%p]", attachment_id, transaction, err_code);
11219 if (attachment_id < 0) {
11220 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
11221 if (err_code != NULL)
11222 *err_code = EMAIL_ERROR_INVALID_PARAM;
11227 int error = EMAIL_ERROR_NONE;
11228 char sql_query_string[QUERY_SIZE] = {0, };
11229 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11231 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11233 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
11235 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11236 if (error != EMAIL_ERROR_NONE) {
11237 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11244 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11249 EM_DEBUG_FUNC_END("ret [%d]", ret);
11253 INTERNAL_FUNC int emstorage_delete_all_attachments_of_mail(char *multi_user_name, int mail_id, int transaction, int *err_code)
11255 EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
11257 int error = EMAIL_ERROR_NONE;
11258 char sql_query_string[QUERY_SIZE] = {0, };
11259 sqlite3 *local_db_handle = NULL;
11261 if (mail_id <= 0) {
11262 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
11263 if (err_code != NULL)
11264 *err_code = EMAIL_ERROR_INVALID_PARAM;
11268 local_db_handle = emstorage_get_db_connection(multi_user_name);
11270 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11272 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
11273 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11274 if (error != EMAIL_ERROR_NONE) {
11275 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11282 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11287 EM_DEBUG_FUNC_END("ret [%d]", ret);
11291 INTERNAL_FUNC int emstorage_delete_attachment_all_on_db(char *multi_user_name, int account_id, char *mailbox, int transaction, int *err_code)
11293 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox, transaction, err_code);
11295 int error = EMAIL_ERROR_NONE;
11297 char sql_query_string[QUERY_SIZE] = {0, };
11298 char *replaced_mailbox = NULL;
11299 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11301 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11303 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl");
11305 if (account_id != ALL_ACCOUNT) /* '0' means all account */
11306 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);
11308 if (mailbox) /* NULL means all mailbox_name */ {
11309 if (strstr(mailbox, "'")) {
11310 replaced_mailbox = em_replace_all_string(mailbox, "'", "''");
11312 replaced_mailbox = strdup(mailbox);
11315 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);
11316 EM_SAFE_FREE(replaced_mailbox); /*prevent 49434*/
11319 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11320 if (error != EMAIL_ERROR_NONE) {
11321 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11328 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11330 if (err_code != NULL)
11333 EM_DEBUG_FUNC_END("ret [%d]", ret);
11337 INTERNAL_FUNC int emstorage_free_attachment(emstorage_attachment_tbl_t** attachment_tbl_list, int count, int *err_code)
11339 EM_DEBUG_FUNC_BEGIN("attachment_tbl_list[%p], count[%d], err_code[%p]", attachment_tbl_list, count, err_code);
11342 if ((attachment_tbl_list == NULL) || (*attachment_tbl_list == NULL)) {
11343 EM_DEBUG_LOG("Nothing to free: attachment_tbl_list[%p], count[%d]", attachment_tbl_list, count);
11344 if (err_code != NULL)
11345 *err_code = EMAIL_ERROR_INVALID_PARAM;
11349 emstorage_attachment_tbl_t* p = *attachment_tbl_list;
11352 for (i = 0; i < count; i++) {
11353 EM_SAFE_FREE(p[i].attachment_name);
11354 EM_SAFE_FREE(p[i].attachment_path);
11355 EM_SAFE_FREE(p[i].content_id);
11356 EM_SAFE_FREE(p[i].attachment_mime_type);
11357 #ifdef __ATTACHMENT_OPTI__
11358 EM_SAFE_FREE(p[i].section);
11363 *attachment_tbl_list = NULL;
11366 if (err_code != NULL)
11367 *err_code = EMAIL_ERROR_NONE;
11368 EM_DEBUG_FUNC_END();
11372 INTERNAL_FUNC int emstorage_begin_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11374 EM_DEBUG_FUNC_BEGIN();
11375 EM_PROFILE_BEGIN(emStorageBeginTransaction);
11378 _timedlock_shm_mutex(mapped_for_db_lock, 2);
11380 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11383 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN immediate;", NULL, NULL, NULL), rc);
11384 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11385 ("SQL(BEGIN) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11387 if (ret == false) {
11388 if (err_code != NULL) *err_code = EMAIL_ERROR_DB_FAILURE;
11391 EM_PROFILE_END(emStorageBeginTransaction);
11392 EM_DEBUG_FUNC_END("ret [%d]", ret);
11396 INTERNAL_FUNC int emstorage_commit_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11398 EM_DEBUG_FUNC_BEGIN();
11402 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11404 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
11405 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; }, ("SQL(END) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11407 if (ret == false && err_code != NULL)
11408 *err_code = EMAIL_ERROR_DB_FAILURE;
11410 _unlockshm_mutex(mapped_for_db_lock);
11412 EM_DEBUG_FUNC_END("ret [%d]", ret);
11416 INTERNAL_FUNC int emstorage_rollback_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11418 EM_DEBUG_FUNC_BEGIN();
11421 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11423 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "ROLLBACK;", NULL, NULL, NULL), rc);
11424 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11425 ("SQL(ROLLBACK) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11427 if (ret == false && err_code != NULL)
11428 *err_code = EMAIL_ERROR_DB_FAILURE;
11430 _unlockshm_mutex(mapped_for_db_lock);
11432 EM_DEBUG_FUNC_END("ret [%d]", ret);
11436 INTERNAL_FUNC int emstorage_is_mailbox_full(char *multi_user_name, int account_id, email_mailbox_t *mailbox, int *result, int *err_code)
11438 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], result[%p], err_code[%p]", account_id, mailbox, result, err_code);
11440 if (account_id < FIRST_ACCOUNT_ID || !mailbox || !result) {
11442 EM_DEBUG_EXCEPTION("Invalid Parameter. accoun_id[%d], mailbox[%p]", account_id, mailbox);
11444 if (err_code != NULL)
11445 *err_code = EMAIL_ERROR_INVALID_PARAM;
11451 int error = EMAIL_ERROR_NONE;
11452 int mail_count = 0;
11454 if (!emstorage_get_mail_count(multi_user_name, account_id, mailbox->mailbox_id, &mail_count, NULL, true, &error)) {
11455 EM_DEBUG_EXCEPTION("emstorage_get_mail_count failed [%d]", error);
11460 EM_DEBUG_LOG("mail_count[%d] mail_slot_size[%d]", mail_count, mailbox->mail_slot_size);
11461 if (mail_count >= mailbox->mail_slot_size)
11472 if (err_code != NULL)
11475 EM_DEBUG_FUNC_END("ret [%d]", ret);
11479 INTERNAL_FUNC int emstorage_clear_mail_data(char *multi_user_name, int transaction, int *err_code)
11481 EM_DEBUG_FUNC_BEGIN("transaction[%d], err_code[%p]", transaction, err_code);
11484 int error = EMAIL_ERROR_NONE;
11485 char sql_query_string[QUERY_SIZE] = {0, };
11486 const email_db_object_t* tables = _g_db_tables;
11487 const email_db_object_t* indexes = _g_db_indexes;
11489 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11490 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11492 if (!emstorage_delete_dir((char *)MAIL_HOME, &error)) {
11493 EM_DEBUG_EXCEPTION(" emstorage_delete_dir failed - %d", error);
11498 mkdir(MAIL_HOME, DIRECTORY_PERMISSION);
11499 mkdir(MAIL_TEMP, DIRECTORY_PERMISSION);
11500 chmod(MAIL_TEMP, 0777);
11502 /* first clear index. */
11503 while (indexes->object_name) {
11504 if (indexes->data_flag) {
11505 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP index %s", indexes->object_name);
11506 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11507 if (error != EMAIL_ERROR_NONE) {
11508 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11515 while (tables->object_name) {
11516 if (tables->data_flag) {
11517 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP table %s", tables->object_name);
11518 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11519 if (error != EMAIL_ERROR_NONE) {
11520 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11530 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11532 if (err_code != NULL)
11535 EM_DEBUG_FUNC_END("ret [%d]", ret);
11538 /*======================= DB File Utils =============================================*/
11539 #include <dirent.h>
11540 #include <sys/types.h>
11541 #define DIR_SEPERATOR "/"
11543 INTERNAL_FUNC char *emstorage_make_directory_path_from_file_path(char *file_name)
11545 EM_DEBUG_FUNC_BEGIN("Filename [ %p ]", file_name);
11546 char delims[] = "/";
11547 char *result = NULL;
11548 gchar **token = NULL;
11550 token = g_strsplit_set(file_name, delims, 1);
11552 if (token && token[0]) {
11553 EM_DEBUG_LOG_SEC(">>>> Directory_name [ %s ]", token[0]);
11554 result = EM_SAFE_STRDUP(token[0]);
11556 EM_DEBUG_LOG(">>>> No Need to create Directory");
11563 INTERNAL_FUNC int emstorage_get_save_name(char *multi_user_name, int account_id, int mail_id, int atch_id,
11564 char *fname, char *move_buf, char *path_buf, int maxlen, int *err_code)
11566 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);
11567 EM_PROFILE_BEGIN(profile_emstorage_get_save_name);
11570 int error = EMAIL_ERROR_NONE;
11571 char *dir_name = NULL;
11572 char create_dir[1024] = {0};
11573 char *temp_file = NULL;
11574 char *prefix_path = NULL;
11575 char *modified_fname = NULL;
11577 if (!move_buf || !path_buf || account_id < FIRST_ACCOUNT_ID || mail_id < 0 || atch_id < 0) {
11578 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);
11579 error = EMAIL_ERROR_INVALID_PARAM;
11583 snprintf(path_buf, 512, "%s", MAIL_HOME);
11584 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, account_id);
11587 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, mail_id);
11590 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, atch_id);
11593 temp_file = EM_SAFE_STRDUP(fname);
11594 if (temp_file && strstr(temp_file, "/")) {
11595 dir_name = emstorage_make_directory_path_from_file_path(temp_file);
11600 snprintf(create_dir, sizeof(create_dir), "%s%s%s", path_buf, DIR_SEPERATOR, dir_name);
11601 EM_DEBUG_LOG(">>>>> DIR PATH [%s]", create_dir);
11602 mkdir(create_dir, DIRECTORY_PERMISSION);
11603 EM_SAFE_FREE(dir_name);
11607 EM_DEBUG_LOG_DEV(">>>>> fname [%s]", fname);
11609 /* Did not allow the slash */
11610 modified_fname = reg_replace_new(fname, "/", "_");
11611 EM_DEBUG_LOG_SEC("modified_fname : [%s]", modified_fname);
11613 if (modified_fname == NULL) modified_fname = g_strdup(fname);
11615 if (EM_SAFE_STRLEN(modified_fname) + EM_SAFE_STRLEN(path_buf) + strlen(DIR_SEPERATOR) > maxlen - 1) {
11616 char *modified_name = NULL;
11617 int remain_len = (maxlen - 1) - EM_SAFE_STRLEN(path_buf) - strlen(DIR_SEPERATOR);
11619 if (remain_len <= 0) {
11620 error = EMAIL_ERROR_MAX_EXCEEDED;
11624 if (remain_len > MAX_FILENAME) {
11625 remain_len = MAX_FILENAME;
11628 modified_name = em_shrink_filename(modified_fname, remain_len);
11630 if (!modified_name) {
11631 error = EMAIL_ERROR_MAX_EXCEEDED;
11635 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),"%s%s", DIR_SEPERATOR, modified_name);
11636 EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11637 EM_SAFE_FREE(modified_name);
11639 if (EM_SAFE_STRLEN(modified_fname) > MAX_FILENAME - 1) {
11640 char *modified_name = NULL;
11642 modified_name = em_shrink_filename(modified_fname, MAX_FILENAME);
11643 if (!modified_name) {
11644 error = EMAIL_ERROR_MAX_EXCEEDED;
11648 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),"%s%s", DIR_SEPERATOR, modified_name);
11649 EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11650 EM_SAFE_FREE(modified_name);
11652 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),"%s%s", DIR_SEPERATOR, modified_fname);
11657 EM_DEBUG_LOG_SEC(">>>>> path_buf [%s]", path_buf);
11659 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11660 error = emcore_get_container_path(multi_user_name, &prefix_path);
11661 if (error != EMAIL_ERROR_NONE) {
11662 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11665 snprintf(move_buf, 512, "%s/%s", prefix_path, path_buf);
11666 EM_DEBUG_LOG_SEC("move_buf : [%s]", move_buf);
11668 snprintf(move_buf, 512, "%s", path_buf);
11669 EM_DEBUG_LOG_SEC("move_buf : [%s]", move_buf);
11676 EM_SAFE_FREE(temp_file);
11677 EM_SAFE_FREE(prefix_path);
11678 EM_SAFE_FREE(modified_fname);
11680 if (err_code != NULL)
11683 EM_PROFILE_END(profile_emstorage_get_save_name);
11684 EM_DEBUG_FUNC_END("ret [%d]", ret);
11689 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)
11691 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);
11693 if (!name_buf || account_id < FIRST_ACCOUNT_ID) {
11694 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);
11695 if (err_code != NULL)
11696 *err_code = EMAIL_ERROR_INVALID_PARAM;
11700 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
11703 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, mail_id);
11708 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, atch_id);
11713 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), ".DELE");
11715 EM_DEBUG_FUNC_END();
11720 INTERNAL_FUNC int emstorage_create_dir(char *multi_user_name, int account_id, int mail_id, int atch_id, int *err_code)
11722 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], err_code[%p]", account_id, mail_id, atch_id, err_code);
11723 EM_PROFILE_BEGIN(profile_emcore_save_create_dir);
11725 int error = EMAIL_ERROR_NONE;
11728 char *prefix_path = NULL;
11729 char errno_buf[ERRNO_BUF_SIZE] = {0};
11731 memset(buf, 0x00, sizeof(buf));
11733 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11734 error = emcore_get_container_path(multi_user_name, &prefix_path);
11735 if (error != EMAIL_ERROR_NONE) {
11736 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11740 prefix_path = strdup("");
11743 if (account_id >= FIRST_ACCOUNT_ID) {
11744 SNPRINTF(buf, sizeof(buf), "%s%s%s%s%d", prefix_path,
11750 if (stat(buf, &sbuf) == 0) {
11751 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11752 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11753 error = EMAIL_ERROR_SYSTEM_FAILURE;
11757 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11758 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11759 EM_DEBUG_EXCEPTION("mkdir failed: %s", EM_STRERROR(errno_buf));
11760 error = EMAIL_ERROR_SYSTEM_FAILURE;
11762 error = EMAIL_ERROR_OUT_OF_MEMORY;
11766 if (account_id == EML_FOLDER) {
11773 int space_left_in_buffer = sizeof(buf) - EM_SAFE_STRLEN(buf);
11775 if (account_id < FIRST_ACCOUNT_ID) {
11776 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11777 error = EMAIL_ERROR_INVALID_PARAM;
11781 if (space_left_in_buffer + 10 > sizeof(buf)) {
11782 EM_DEBUG_EXCEPTION("Buffer overflowed");
11783 error = EMAIL_ERROR_OUT_OF_MEMORY;
11787 SNPRINTF(buf+EM_SAFE_STRLEN(buf), space_left_in_buffer, "%s%d", DIR_SEPERATOR, mail_id);
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 [%d][%s]", errno, EM_STRERROR(errno_buf));
11799 error = EMAIL_ERROR_SYSTEM_FAILURE;
11801 error = EMAIL_ERROR_OUT_OF_MEMORY;
11805 if (account_id == EML_FOLDER) {
11812 if (account_id < FIRST_ACCOUNT_ID || mail_id <= 0) {
11813 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11814 error = EMAIL_ERROR_INVALID_PARAM;
11818 SNPRINTF(buf+EM_SAFE_STRLEN(buf), sizeof(buf)-(EM_SAFE_STRLEN(buf)+1), "%s%d", DIR_SEPERATOR, atch_id);
11820 if (stat(buf, &sbuf) == 0) {
11821 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11822 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11823 error = EMAIL_ERROR_SYSTEM_FAILURE;
11827 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11828 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11829 error = EMAIL_ERROR_SYSTEM_FAILURE;
11831 error = EMAIL_ERROR_OUT_OF_MEMORY;
11835 if (account_id == EML_FOLDER) {
11845 EM_SAFE_FREE(prefix_path);
11847 if (err_code != NULL)
11850 EM_PROFILE_END(profile_emcore_save_create_dir);
11851 EM_DEBUG_FUNC_END("ret [%d]", ret);
11855 INTERNAL_FUNC int emstorage_copy_file(char *src_file, char *dst_file, int sync_status, int *err_code)
11857 EM_DEBUG_FUNC_BEGIN_SEC("src_file[%s], dst_file[%s], err_code[%p]", src_file, dst_file, err_code);
11858 EM_DEBUG_LOG("Using the fsync function");
11860 int error = EMAIL_ERROR_NONE;
11861 struct stat st_buf;
11866 char buf[FILE_MAX_BUFFER_SIZE] = {0};
11867 char errno_buf[ERRNO_BUF_SIZE] = {0};
11869 if (!src_file || !dst_file) {
11870 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
11872 error = EMAIL_ERROR_INVALID_PARAM;
11876 if (stat(src_file, &st_buf) < 0) {
11877 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", src_file);
11879 error = EMAIL_ERROR_SYSTEM_FAILURE; /* EMAIL_ERROR_INVALID_PATH; */
11883 error = em_open(src_file, O_RDONLY, 0, &fp_src);
11884 if (error != EMAIL_ERROR_NONE) {
11885 EM_DEBUG_EXCEPTION(">>>> Source Fail em_open %s Failed: %d", src_file, error);
11889 error = em_open(dst_file, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, &fp_dst); /*prevent 24474*/
11890 if (error != EMAIL_ERROR_NONE) {
11891 EM_DEBUG_EXCEPTION(">>>> Destination Fail em_open %s: %d", dst_file, error);
11895 memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
11897 while ((nread = read(fp_src, buf, FILE_MAX_BUFFER_SIZE)) > 0) {
11898 if (nread > 0 && nread <= FILE_MAX_BUFFER_SIZE) {
11899 EM_DEBUG_LOG("Nread Value [%d]", nread);
11901 ssize_t byte_written = 0;
11902 size_t remain_byte = nread;
11906 while (remain_byte > 0 && buf_ptr && errno == 0) {
11907 byte_written = write(fp_dst, buf_ptr, remain_byte);
11909 if (byte_written < 0) {
11910 /* interrupted by a signal */
11911 if (errno == EINTR) {
11916 EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
11917 error = EMAIL_ERROR_UNKNOWN;
11920 EM_DEBUG_LOG("NWRITTEN [%d]", byte_written);
11921 remain_byte -= byte_written;
11922 buf_ptr += byte_written;
11926 memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
11932 EM_SAFE_CLOSE(fp_src);
11934 if (fp_dst >= 0) { /*prevent 24474*/
11936 EM_DEBUG_LOG("Before fsync");
11942 if (nread < 0 || error == EMAIL_ERROR_UNKNOWN)
11945 if (err_code != NULL)
11947 EM_DEBUG_FUNC_END("ret [%d]", ret);
11950 /* create Directory if user has deleted [deepam.p@samsung.com] */
11951 INTERNAL_FUNC void emstorage_create_dir_if_delete()
11953 EM_DEBUG_FUNC_BEGIN();
11955 mkdir(DATA_PATH, DIRECTORY_PERMISSION);
11956 mkdir(EMAIL_PATH, DIRECTORY_PERMISSION);
11957 mkdir(MAIL_HOME, DIRECTORY_PERMISSION);
11958 mkdir(MAIL_TEMP, DIRECTORY_PERMISSION);
11959 chmod(MAIL_TEMP, 0777);
11961 EM_DEBUG_FUNC_END();
11964 static int _get_temp_file_name(char **filename, int *err_code)
11966 EM_DEBUG_FUNC_BEGIN("filename[%p], err_code[%p]", filename, err_code);
11969 int error = EMAIL_ERROR_NONE;
11971 if (filename == NULL) {
11972 EM_DEBUG_EXCEPTION(" filename[%p]", filename);
11973 error = EMAIL_ERROR_INVALID_PARAM;
11977 char tempname[512] = {0x00, };
11980 gettimeofday(&tv, NULL);
11982 unsigned int seed = time(NULL);
11983 SNPRINTF(tempname, sizeof(tempname), "%s%c%d", MAIL_TEMP, '/', rand_r(&seed));
11985 char *p = EM_SAFE_STRDUP(tempname);
11987 EM_DEBUG_EXCEPTION(" strdup failed...");
11988 error = EMAIL_ERROR_OUT_OF_MEMORY;
11997 if (err_code != NULL)
12000 EM_DEBUG_FUNC_END("ret [%d]", ret);
12004 INTERNAL_FUNC int emstorage_add_content_type(char *file_path, char *char_set, int *err_code)
12006 EM_DEBUG_FUNC_BEGIN("File path [ %p ] Character Set [ %p ] err_code [ %p]", file_path, char_set, err_code);
12008 EM_IF_NULL_RETURN_VALUE(file_path, false);
12009 EM_IF_NULL_RETURN_VALUE(char_set, false);
12010 EM_IF_NULL_RETURN_VALUE(err_code, false);
12014 struct stat st_buf;
12016 char *low_char_set = NULL;
12017 char *match_str = NULL;
12020 int error = EMAIL_ERROR_NONE;
12021 int data_count_to_written = 0;
12022 char *temp_file_name = NULL;
12024 FILE* fp_src = NULL;
12025 FILE* fp_dest = NULL;
12029 if (stat(file_path, &st_buf) < 0) {
12030 EM_DEBUG_EXCEPTION_SEC(" stat(\"%s\") failed...", file_path);
12031 error = EMAIL_ERROR_SYSTEM_FAILURE;
12035 buf_size = st_buf.st_size;
12036 EM_DEBUG_LOG(">>>> File Size [ %d ] ", buf_size);
12037 buf = (char *)calloc(1, buf_size+1);
12040 EM_DEBUG_LOG(">>> Memory cannot be allocated ");
12044 error = em_fopen(file_path, "rb", &fp_src);
12045 if (error != EMAIL_ERROR_NONE || fp_src == NULL) {
12046 EM_DEBUG_EXCEPTION_SEC(" file_path fopen failed - %s [%d]", file_path, error);
12050 if ((nread = fread(buf, 1, buf_size, fp_src)) > 0) {
12051 if (nread > 0 && nread <= buf_size) {
12052 EM_DEBUG_LOG(">>>> Nread Value [ %d ] ", nread);
12055 * 1.Add check for whether content type is there.
12056 * 2. If not based on the character set, Append it in File
12059 low_char_set = calloc(1, EM_SAFE_STRLEN(char_set) + strlen(" \" /></head>") +1); /*prevent 34359*/
12060 if (low_char_set == NULL) {
12061 EM_DEBUG_EXCEPTION("calloc failed");
12062 error = EMAIL_ERROR_OUT_OF_MEMORY;
12066 strncat(low_char_set, char_set, EM_SAFE_STRLEN(char_set));
12067 EM_DEBUG_LOG(">>>> CHAR SET [ %s ] ", low_char_set);
12068 strncat(low_char_set, " \" /></head>", strlen(" \" /></head>")); /*prevent 34359*/
12069 EM_DEBUG_LOG(">>> CHARSET [ %s ] ", low_char_set);
12070 match_str = strstr(buf, CONTENT_TYPE_DATA);
12072 if (match_str == NULL) {
12073 EM_DEBUG_LOG(">>>>emstorage_add_content_type 3 ");
12074 if (fp_src != NULL) {
12078 data_count_to_written = EM_SAFE_STRLEN(low_char_set)+strlen(CONTENT_DATA)+1; /*prevent 34359*/
12079 buf1 = (char *)calloc(1, data_count_to_written);
12082 strncat(buf1, CONTENT_DATA, strlen(CONTENT_DATA)); /*prevent 34359*/
12083 EM_DEBUG_LOG(">>>>> BUF 1 [ %s ] ", buf1);
12084 strncat(buf1, low_char_set, EM_SAFE_STRLEN(low_char_set));
12085 EM_DEBUG_LOG(">>>> HTML TAG DATA [ %s ] ", buf1);
12087 /* 1. Create a temporary file name */
12088 if (!_get_temp_file_name(&temp_file_name, &err)) {
12089 EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed - %d", err);
12090 if (err_code != NULL) *err_code = err;
12093 EM_DEBUG_LOG_SEC(">>>>>>> TEMP APPEND FILE PATH [ %s ] ", temp_file_name);
12095 /* Open the Temp file in Append mode */
12096 error = em_fopen(temp_file_name, "ab", &fp_dest);
12097 if (error != EMAIL_ERROR_NONE) {
12098 EM_DEBUG_EXCEPTION_SEC(" fopen failed - %s [%d]", temp_file_name, error);
12102 /* 2. write the Latest data */
12103 nwritten = fwrite(buf1, data_count_to_written-1, 1, fp_dest);
12105 if (nwritten > 0) {
12106 EM_DEBUG_LOG(" Latest Data : [%d ] bytes written ", nwritten);
12108 /* 3. Append old data */
12109 nwritten = fwrite(buf, nread-1, 1, fp_dest);
12111 if (nwritten <= 0) {
12112 EM_DEBUG_EXCEPTION(" Error Occured while writing Old data : [%d ] bytes written ", nwritten);
12113 error = EMAIL_ERROR_SYSTEM_FAILURE;
12116 EM_DEBUG_LOG(">>>> OLD data appended [ %d ] ", nwritten);
12118 if (!emstorage_move_file(temp_file_name, file_path, false, &err)) {
12119 EM_DEBUG_EXCEPTION(" emstorage_move_file failed - %d", err);
12125 EM_DEBUG_EXCEPTION(" Error Occured while writing New data : [%d ] bytes written ", nwritten);
12126 error = EMAIL_ERROR_SYSTEM_FAILURE;
12138 EM_SAFE_FREE(buf1);
12139 EM_SAFE_FREE(low_char_set);
12140 EM_SAFE_FREE(temp_file_name);
12142 if (fp_src != NULL) {
12147 if (fp_dest != NULL) {
12155 EM_DEBUG_FUNC_END("ret [%d]", ret);
12160 INTERNAL_FUNC int emstorage_move_file(char *src_file, char *dst_file, int sync_status, int *err_code)
12162 EM_DEBUG_FUNC_BEGIN("src_file[%p], dst_file[%p], err_code[%p]", src_file, dst_file, err_code);
12165 int error = EMAIL_ERROR_NONE;
12166 char errno_buf[ERRNO_BUF_SIZE] = {0};
12168 if (src_file == NULL || dst_file == NULL) {
12169 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
12170 error = EMAIL_ERROR_INVALID_PARAM;
12174 EM_DEBUG_LOG_SEC("src_file[%s], dst_file[%s]", src_file, dst_file);
12176 if (strcmp(src_file, dst_file) != 0) {
12177 EM_DEBUG_LOG("oldpath and newpath are not on the same mounted file system.");
12178 if (!emstorage_copy_file(src_file, dst_file, sync_status, &error)) {
12179 EM_DEBUG_EXCEPTION("emstorage_copy_file failed - %d", error);
12181 struct stat temp_file_stat;
12182 if (stat(src_file, &temp_file_stat) < 0) {
12183 EM_DEBUG_EXCEPTION("no src file found [%s] : %s", src_file, EM_STRERROR(errno_buf));
12185 if (stat(dst_file, &temp_file_stat) < 0)
12186 EM_DEBUG_EXCEPTION("no dst file found [%s] : %s", dst_file, EM_STRERROR(errno_buf));
12188 error = EMAIL_ERROR_FILE_NOT_FOUND;
12194 EM_DEBUG_LOG("src[%s] removed", src_file);
12196 EM_DEBUG_LOG("src[%s] = dst[%s]", src_file, dst_file);
12202 if (err_code != NULL)
12205 EM_DEBUG_FUNC_END("ret [%d]", ret);
12209 INTERNAL_FUNC int emstorage_delete_file(char *src_file, int *err_code)
12211 EM_DEBUG_FUNC_BEGIN("src_file[%p], err_code[%p]", src_file, err_code);
12214 int error = EMAIL_ERROR_NONE;
12216 if (src_file == NULL) {
12217 EM_DEBUG_EXCEPTION(" src_file[%p]", src_file);
12219 error = EMAIL_ERROR_INVALID_PARAM;
12223 if (remove(src_file) != 0) {
12224 if (errno != ENOENT) {
12225 EM_DEBUG_EXCEPTION(" remove failed - %d", errno);
12227 error = EMAIL_ERROR_SYSTEM_FAILURE;
12230 EM_DEBUG_EXCEPTION(" no file found...");
12232 error = EMAIL_ERROR_FILE_NOT_FOUND;
12239 if (err_code != NULL)
12242 EM_DEBUG_FUNC_END("ret [%d]", ret);
12247 INTERNAL_FUNC int emstorage_delete_dir(char *src_dir, int *err_code)
12249 EM_DEBUG_FUNC_BEGIN("src_dir[%p], err_code[%p]", src_dir, err_code);
12251 if (src_dir == NULL) {
12252 EM_DEBUG_EXCEPTION("src_dir[%p]", src_dir);
12254 if (err_code != NULL)
12255 *err_code = EMAIL_ERROR_INVALID_PARAM;
12259 int error = EMAIL_ERROR_NONE;
12266 dirp = opendir(src_dir);
12268 if (dirp == NULL) {
12269 if (errno == ENOENT) {
12270 EM_DEBUG_EXCEPTION("directory[%s] does not exist...", src_dir);
12271 if (err_code != NULL)
12272 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12275 EM_DEBUG_EXCEPTION("opendir failed [%s] [%d]", src_dir, errno);
12276 if (err_code != NULL)
12277 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12282 while ((dp = readdir(dirp))) {
12283 if (strncmp(dp->d_name, ".", 1) == 0 || strncmp(dp->d_name, "..", 2) == 0) /* prevent 34360 */
12286 SNPRINTF(buf, sizeof(buf), "%s/%s", src_dir, dp->d_name);
12288 if (lstat(buf, &sbuf) == 0 || stat(buf, &sbuf) == 0) {
12289 /* check directory */
12290 if ((sbuf.st_mode & S_IFMT) == S_IFDIR) { /* directory */
12291 /* recursive call */
12292 if (!emstorage_delete_dir(buf, &error)) {
12294 if (err_code != NULL)
12298 } else { /* file */
12299 if (remove(buf) < 0) {
12300 EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", buf, errno);
12302 if (err_code != NULL)
12303 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12308 EM_DEBUG_EXCEPTION("content does not exist...");
12313 EM_DEBUG_LOG_DEV("remove direcotory [%s]", src_dir);
12315 /* EM_DEBUG_FUNC_BEGIN(); */
12317 if (remove(src_dir) < 0) {
12318 EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", src_dir, errno);
12319 if (err_code != NULL)
12320 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12324 if (err_code != NULL)
12330 /* faizan.h@samsung.com */
12331 INTERNAL_FUNC int emstorage_update_server_uid(char *multi_user_name,
12333 char *old_server_uid,
12334 char *new_server_uid,
12337 EM_DEBUG_FUNC_BEGIN("new_server_uid[%s], old_server_uid[%s]", new_server_uid, old_server_uid);
12339 int error = EMAIL_ERROR_NONE;
12340 int transaction = true;
12341 int temp_strlen = 0;
12342 int and_operation = 0;
12343 char sql_query_string[QUERY_SIZE] = {0, };
12344 char conditional_clause_string[QUERY_SIZE] = {0};
12346 if ((mail_id <= 0 || !old_server_uid) && !new_server_uid) {
12347 EM_DEBUG_EXCEPTION("Invalid parameters");
12349 *err_code = EMAIL_ERROR_INVALID_PARAM;
12353 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12354 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12356 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE ");
12359 temp_strlen = strlen(conditional_clause_string);
12360 SNPRINTF(conditional_clause_string + temp_strlen, sizeof(conditional_clause_string) - temp_strlen,
12361 "mail_id = %d ", mail_id);
12365 if (old_server_uid) {
12366 temp_strlen = strlen(conditional_clause_string);
12367 if (!and_operation) {
12368 sqlite3_snprintf(sizeof(conditional_clause_string) - temp_strlen, conditional_clause_string + temp_strlen,
12369 "server_mail_id = '%q'", old_server_uid);
12371 sqlite3_snprintf(sizeof(conditional_clause_string) - temp_strlen, conditional_clause_string + temp_strlen,
12372 "and server_mail_id = '%q'", old_server_uid);
12376 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
12377 "UPDATE mail_tbl SET server_mail_id = '%q' %s", new_server_uid, conditional_clause_string);
12379 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12380 if (error != EMAIL_ERROR_NONE) {
12381 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12389 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12391 if (err_code != NULL)
12394 EM_DEBUG_FUNC_END("ret [%d]", ret);
12399 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)
12401 EM_DEBUG_FUNC_BEGIN_SEC("mail_id[%d], new_server_uid[%s], mbox_name[%s]", mail_id, new_server_uid, mbox_name);
12404 int error = EMAIL_ERROR_NONE;
12405 char sql_query_string[QUERY_SIZE] = {0, };
12406 int transaction = true;
12408 if (!mail_id || !new_server_uid || !mbox_name) {
12409 EM_DEBUG_EXCEPTION("Invalid parameters");
12410 if (err_code != NULL)
12411 *err_code = EMAIL_ERROR_INVALID_PARAM;
12415 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12416 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12419 SNPRINTF(sql_query_string, sizeof(sql_query_string),
12420 "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);
12422 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12423 if (error != EMAIL_ERROR_NONE) {
12424 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12432 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12434 if (err_code != NULL)
12437 EM_DEBUG_FUNC_END("ret [%d]", ret);
12442 INTERNAL_FUNC int emstorage_update_save_status(char *multi_user_name, int account_id, int *err_code)
12444 EM_DEBUG_FUNC_BEGIN();
12447 int transaction = true;
12448 int error = EMAIL_ERROR_NONE;
12449 char sql_query_string[QUERY_SIZE] = {0,};
12451 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12453 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12455 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12457 if (account_id <= ALL_ACCOUNT)
12458 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);
12460 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);
12462 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12463 if (error != EMAIL_ERROR_NONE) {
12464 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12471 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12473 if (err_code != NULL)
12476 EM_DEBUG_FUNC_END("ret [%d]", ret);
12481 int emstorage_get_unread_mailid(char *multi_user_name, int account_id, int vip_mode, int **mail_ids, int *mail_number, int *err_code)
12483 EM_DEBUG_FUNC_BEGIN();
12485 if ((!mail_ids) || (account_id <= 0 && account_id != -1)) {
12486 EM_DEBUG_EXCEPTION(" mail_id[%p], account_id[%d] ", mail_ids, account_id);
12487 if (err_code != NULL)
12488 *err_code = EMAIL_ERROR_INVALID_PARAM;
12494 int error = EMAIL_ERROR_NONE;
12498 int *p_mail_ids = NULL;
12499 int transaction = false;
12500 char **result = NULL;
12501 char sql_query_string[QUERY_SIZE] = {0, };
12502 char temp_query_string[QUERY_SIZE] = {0,};
12503 char sql_select_query_string[QUERY_SIZE] = {0, };
12505 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12506 EMSTORAGE_START_READ_TRANSACTION(transaction);
12508 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12510 if (account_id == -1)
12511 SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string), "SELECT mail_id FROM mail_tbl WHERE flags_seen_field = 0 AND (save_status = %d or save_status = %d)", EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12513 SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string), "SELECT mail_id FROM mail_tbl WHERE account_id = %d AND flags_seen_field = 0 AND (save_status = %d or save_status = %d)", account_id, EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12516 SNPRINTF(temp_query_string, sizeof(temp_query_string), "%s AND tag_id < 0", sql_select_query_string);
12518 SNPRINTF(temp_query_string, sizeof(temp_query_string), "%s", sql_select_query_string);
12521 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s ORDER BY date_time ASC", temp_query_string);
12523 EM_DEBUG_LOG_SEC("query: [%s]", sql_query_string);
12525 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
12526 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
12527 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12529 EM_DEBUG_LOG("Count : %d", count);
12532 EM_DEBUG_EXCEPTION("no Mails found...");
12534 error = EMAIL_ERROR_MAIL_NOT_FOUND;
12538 p_mail_ids = em_malloc(count * sizeof(int));
12539 if (p_mail_ids == NULL) {
12540 EM_DEBUG_EXCEPTION("em_mallocfailed...");
12541 error = EMAIL_ERROR_OUT_OF_MEMORY;
12547 for (i = 0; i < count; i++) {
12548 _get_table_field_data_int(result, &(p_mail_ids[i]), col_index++);
12556 sqlite3_free_table(result);
12558 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12560 // sqlite3_db_release_memory(local_db_handle);
12563 if (mail_ids != NULL)
12564 *mail_ids = p_mail_ids;
12566 if (mail_number != NULL)
12567 *mail_number = count;
12569 EM_SAFE_FREE(p_mail_ids);
12572 if (err_code != NULL)
12575 EM_DEBUG_FUNC_END("ret [%d]", ret);
12579 int setting_system_command(const char *command)
12582 int pid = 0, status = 0;
12583 char *const environ[] = { NULL };
12598 argv[2] = (char *)command;
12601 execve("/bin/sh", argv, environ);
12606 if (waitpid(pid, &status, 0) == -1) {
12607 if (errno != EINTR)
12618 INTERNAL_FUNC int emstorage_mail_get_total_diskspace_usage(unsigned long *total_usage, int transaction, int *err_code)
12620 EM_DEBUG_FUNC_BEGIN("total_usage[%p], transaction[%d], err_code[%p]", total_usage, transaction, err_code);
12622 if (!total_usage) {
12623 EM_DEBUG_EXCEPTION("total_usage[%p]", total_usage);
12625 if (err_code != NULL)
12626 *err_code = EMAIL_ERROR_INVALID_PARAM;
12631 int error = EMAIL_ERROR_NONE;
12632 char syscmd[256] = {0, };
12633 char line[256] = {0, };
12634 char *line_from_file = NULL;
12636 unsigned long total_diskusage = 0;
12638 SNPRINTF(syscmd, sizeof(syscmd), "touch %s", SETTING_MEMORY_TEMP_FILE_PATH);
12639 if (setting_system_command(syscmd) == -1) {
12640 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
12642 error = EMAIL_ERROR_SYSTEM_FAILURE;
12646 SNPRINTF(syscmd, sizeof(syscmd), "du -hsk %s > %s", EMAIL_PATH, SETTING_MEMORY_TEMP_FILE_PATH);
12647 EM_DEBUG_LOG(" cmd : %s", syscmd);
12648 if (setting_system_command(syscmd) == -1) {
12649 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : Setting > Memory] System Command [%s] is failed", syscmd);
12651 error = EMAIL_ERROR_SYSTEM_FAILURE;
12655 error = em_fopen(SETTING_MEMORY_TEMP_FILE_PATH, "r", &fp);
12656 if (error != EMAIL_ERROR_NONE) {
12657 perror(SETTING_MEMORY_TEMP_FILE_PATH);
12661 line_from_file = fgets(line, sizeof(line), fp);
12663 if (line_from_file == NULL) {
12664 EM_DEBUG_EXCEPTION("fgets failed");
12665 error = EMAIL_ERROR_SYSTEM_FAILURE;
12668 total_diskusage = strtoul(line, NULL, 10);
12670 memset(syscmd, 0, sizeof(syscmd));
12671 SNPRINTF(syscmd, sizeof(syscmd), "rm -f %s", SETTING_MEMORY_TEMP_FILE_PATH);
12672 if (setting_system_command(syscmd) == -1) {
12673 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
12674 error = EMAIL_ERROR_SYSTEM_FAILURE;
12678 EM_DEBUG_LOG("[Setting > Memory] @@@@@ Size of Directory [%s] is %ld KB", EMAIL_PATH, total_diskusage);
12683 if (err_code != NULL)
12687 *total_usage = total_diskusage;
12691 if (fp) fclose(fp); /* prevent 32730 */
12693 EM_DEBUG_FUNC_END("ret [%d]", ret);
12696 #define MAILHOME_UTF8 tzplatform_mkpath(TZ_USER_DATA, "email/.email_data/7/348/UTF-8")
12698 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)
12700 DB_STMT hStmt = NULL;
12702 int error = EMAIL_ERROR_NONE;
12704 char sql_query_string[QUERY_SIZE] = {0, };
12706 SNPRINTF(sql_query_string, sizeof(sql_query_string),
12707 "INSERT INTO mail_tbl VALUES "
12709 ", ?" /* account_id */
12710 ", ?" /* mail_size */
12711 ", ?" /* server_mail_status */
12712 ", ?" /* server_mailbox_name */
12713 ", ?" /* server_mail_id */
12714 ", ?" /* reference_mail_id */
12715 ", ?" /* full_address_from */
12716 ", ?" /* full_address_reply */
12717 ", ?" /* full_address_to */
12718 ", ?" /* full_address_cc */
12719 ", ?" /* full_address_bcc */
12720 ", ?" /* full_address_return */
12721 ", ?" /* subject */
12722 ", ?" /* body_download_status */
12723 ", ?" /* file_path_plain */
12724 ", ?" /* file_path_html */
12725 ", ?" /* date_time */
12726 ", ?" /* flags_seen_field */
12727 ", ?" /* flags_deleted_field */
12728 ", ?" /* flags_flagged_field */
12729 ", ?" /* flags_answered_field */
12730 ", ?" /* flags_recent_field */
12731 ", ?" /* flags_draft_field */
12732 ", ?" /* flags_forwarded_field */
12733 ", ?" /* DRM_status */
12734 ", ?" /* priority */
12735 ", ?" /* save_status */
12736 ", ?" /* lock_status */
12737 ", ?" /* message_id */
12738 ", ?" /* report_status */
12739 ", ?" /* email_address_sender */
12740 ", ?" /* email_address_recipient */
12741 ", ?" /* attachment_count */
12742 ", ?" /* inline_content_count */
12743 ", ?" /* preview_text */
12744 ", ?" /* thread_id */
12745 ", ?" /* mailbox_type */
12746 ", ?" /* alias_sender */
12747 ", ?" /* alias_recipient */
12748 ", ?" /* thread_item_count */
12749 ", ?" /* meeting_request_status */
12750 ", ?" /* message_class */
12751 ", ?" /* digest_type */
12752 ", ?" /* smime_type */
12753 ", ?" /* scheduled_sending_time */
12754 ", ?" /* remaining_resend_times */
12756 ", ?" /* replied_time */
12757 ", ?" /* forwarded_time */
12758 ", ?" /* default_charset */
12759 ", ?" /* eas_data_length */
12760 ", ?" /* eas_data */
12763 int transaction = true;
12764 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12766 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12769 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12770 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
12771 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12773 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_id);
12774 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, account_id);
12775 _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, EMAIL_MAILBOX_TYPE_OUTBOX);
12776 _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, "save test - long", 1, SUBJECT_LEN_IN_MAIL_TBL);
12777 _bind_stmt_field_data_string(hStmt, DATETIME_IDX_IN_MAIL_TBL, "20100316052908", 0, DATETIME_LEN_IN_MAIL_TBL);
12778 _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, 0);
12779 _bind_stmt_field_data_string(hStmt, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL, "", 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
12780 _bind_stmt_field_data_string(hStmt, SERVER_MAIL_ID_IDX_IN_MAIL_TBL, "", 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
12781 _bind_stmt_field_data_string(hStmt, MESSAGE_ID_IDX_IN_MAIL_TBL, "", 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
12782 _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, 0);
12783 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL, "<test08@streaming.s3glab.net>", 1, FROM_LEN_IN_MAIL_TBL);
12784 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL, "", 1, REPLY_TO_LEN_IN_MAIL_TBL);
12785 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL, full_address_to, 1, TO_LEN_IN_MAIL_TBL);
12786 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL, full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
12787 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL, full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
12788 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL, "", 1, RETURN_PATH_LEN_IN_MAIL_TBL);
12789 _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);
12790 _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);
12791 _bind_stmt_field_data_string(hStmt, ALIAS_SENDER_IDX_IN_MAIL_TBL, "send_alias", 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12792 _bind_stmt_field_data_string(hStmt, ALIAS_RECIPIENT_IDX_IN_MAIL_TBL, "recipient_alias", 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12793 _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, 1);
12794 _bind_stmt_field_data_string(hStmt, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL, (char *)MAILHOME_UTF8, 0, TEXT_1_LEN_IN_MAIL_TBL);
12795 _bind_stmt_field_data_string(hStmt, FILE_PATH_HTML_IDX_IN_MAIL_TBL, "", 0, TEXT_2_LEN_IN_MAIL_TBL);
12796 _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, 4);
12797 _bind_stmt_field_data_char(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, 0);
12798 _bind_stmt_field_data_char(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, 0);
12799 _bind_stmt_field_data_char(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, 0);
12800 _bind_stmt_field_data_char(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, 0);
12801 _bind_stmt_field_data_char(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, 0);
12802 _bind_stmt_field_data_char(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, 0);
12803 _bind_stmt_field_data_char(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, 0);
12804 _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, 0);
12805 _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, 0);
12806 _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, 0);
12807 _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, 0);
12808 _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, 0);
12809 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12810 _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, 0);
12811 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12812 _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, 0);
12813 _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, 0);
12814 _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, "preview body", 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
12815 _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, 0);
12816 _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, 0);
12817 _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, 0);
12818 _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, 0);
12819 _bind_stmt_field_data_int(hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, 0);
12820 _bind_stmt_field_data_int(hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, 0);
12821 _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, 0);
12822 _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, 0);
12823 _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, 0);
12824 _bind_stmt_field_data_string(hStmt, DEFAULT_CHARSET_IDX_IN_MAIL_TBL, "UTF-8", 0, TEXT_2_LEN_IN_MAIL_TBL);
12825 _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, 0);
12826 _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, NULL, 0);
12828 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12829 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
12830 ("sqlite3_step fail:%d", rc));
12831 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
12832 ("sqlite3_step fail:%d", rc));
12836 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12837 if (hStmt != NULL) {
12838 rc = sqlite3_finalize(hStmt);
12839 if (rc != SQLITE_OK) {
12840 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
12841 error = EMAIL_ERROR_DB_FAILURE;
12845 if (err_code != NULL)
12848 EM_DEBUG_FUNC_END("ret [%d]", ret);
12853 INTERNAL_FUNC int emstorage_get_max_mail_count()
12855 return EMAIL_MAIL_MAX_COUNT;
12858 #define STRIPPED_SUBJECT_BUFFER_SIZE 4086
12860 INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(char *multi_user_name,
12861 emstorage_mail_tbl_t *mail_tbl,
12863 int *result_latest_mail_id_in_thread,
12864 int *thread_item_count)
12866 EM_DEBUG_FUNC_BEGIN("mail_tbl [%p], thread_id [%p], "
12867 "result_latest_mail_id_in_thread [%p], thread_item_count [%p]",
12868 mail_tbl, thread_id, result_latest_mail_id_in_thread, thread_item_count);
12869 EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
12871 int rc = 0, query_size = 0, query_size_account = 0;
12873 int search_thread = false;
12874 int account_id = 0;
12875 int err_code = EMAIL_ERROR_NONE;
12876 int count = 0, result_thread_id = -1, latest_mail_id_in_thread = -1;
12877 time_t latest_date_time = 0;
12878 char *subject = NULL;
12879 char *p_subject = NULL;
12880 char *sql_query_string = NULL, *sql_account = NULL;
12882 int temp_thread_id = -1;
12883 char *sql_format = "SELECT mail_id, thread_id, date_time, subject "
12884 "FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
12885 char *sql_format_account = " AND account_id = %d ";
12886 char *sql_format_order_by = " ORDER BY thread_id, date_time DESC ";
12887 char **result = NULL;
12888 char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
12889 char stripped_subject2[STRIPPED_SUBJECT_BUFFER_SIZE];
12891 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12893 EM_DEBUG_LOG("subject: [%p], mail_id: [%d]", subject, mail_tbl->mail_id);
12895 EM_IF_NULL_RETURN_VALUE(mail_tbl, EMAIL_ERROR_INVALID_PARAM);
12896 EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
12897 EM_IF_NULL_RETURN_VALUE(result_latest_mail_id_in_thread, EMAIL_ERROR_INVALID_PARAM);
12898 EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
12900 account_id = mail_tbl->account_id;
12901 subject = mail_tbl->subject;
12903 EM_DEBUG_LOG_SEC("subject: [%s]", subject);
12905 if (EM_SAFE_STRLEN(subject) == 0 && mail_tbl->mail_id != 0) {
12906 result_thread_id = mail_tbl->mail_id;
12911 if (em_find_pos_stripped_subject_for_thread_view(subject,
12913 STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE) {
12914 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
12915 err_code = EMAIL_ERROR_UNKNOWN;
12916 result_thread_id = -1;
12920 EM_DEBUG_LOG_SEC("stripped_subject: [%s]", stripped_subject);
12922 if (EM_SAFE_STRLEN(stripped_subject) == 0) {
12923 result_thread_id = -1;
12927 EM_DEBUG_LOG_SEC("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s",
12928 EM_SAFE_STRLEN(stripped_subject), stripped_subject);
12930 if (account_id > 0) {
12931 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
12932 sql_account = malloc(query_size_account);
12933 if (sql_account == NULL) {
12934 EM_DEBUG_EXCEPTION("malloc for sql_account is failed %d", query_size_account);
12935 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
12938 snprintf(sql_account, query_size_account, sql_format_account, account_id);
12941 /* prevent 34362 */
12942 query_size = strlen(sql_format) + strlen(stripped_subject)*2 + 50 + query_size_account + strlen(sql_format_order_by); /* + query_size_mailbox; */
12943 sql_query_string = malloc(query_size);
12945 if (sql_query_string == NULL) {
12946 EM_DEBUG_EXCEPTION("malloc for sql is failed %d", query_size);
12947 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
12951 sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mail_tbl->mailbox_id);
12953 if (account_id > 0)
12954 strcat(sql_query_string, sql_account);
12956 strcat(sql_query_string, sql_format_order_by);
12957 strcat(sql_query_string, ";");
12959 EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
12961 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL),
12964 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
12965 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12967 EM_DEBUG_LOG("Result rows count : %d", count);
12970 result_thread_id = -1;
12972 for (i = 0; i < count; i++) {
12973 EM_SAFE_FREE(p_subject);
12975 _get_table_field_data_int(result, &latest_mail_id_in_thread, col_index++);
12976 _get_table_field_data_int(result, &result_thread_id, col_index++);
12977 _get_table_field_data_time_t(result, &latest_date_time, col_index++);
12978 _get_table_field_data_string(result, &p_subject, 0, col_index++);
12980 if (temp_thread_id == result_thread_id)
12983 temp_thread_id = result_thread_id;
12985 if (em_find_pos_stripped_subject_for_thread_view(p_subject,
12987 STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE) {
12988 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
12989 err_code = EMAIL_ERROR_UNKNOWN;
12990 result_thread_id = -1;
12994 if (g_strcmp0(stripped_subject2, stripped_subject) == 0) {
12995 if (latest_date_time < mail_tbl->date_time)
12996 *result_latest_mail_id_in_thread = latest_mail_id_in_thread;
12998 *result_latest_mail_id_in_thread = mail_tbl->mail_id;
13000 search_thread = true;
13004 if (search_thread) {
13005 EM_DEBUG_LOG("latest_mail_id_in_thread [%d], mail_id [%d]",
13006 latest_mail_id_in_thread, mail_tbl->mail_id);
13008 result_thread_id = -1;
13017 *thread_id = result_thread_id;
13018 *thread_item_count = count;
13020 EM_DEBUG_LOG("Result thread id : %d", *thread_id);
13021 EM_DEBUG_LOG("Result count : %d", *thread_item_count);
13022 EM_DEBUG_LOG("err_code : %d", err_code);
13024 EM_SAFE_FREE(sql_account);
13025 EM_SAFE_FREE(sql_query_string);
13026 EM_SAFE_FREE(p_subject);
13028 sqlite3_free_table(result);
13030 EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
13035 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)
13037 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);
13038 EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
13041 int query_size = 0;
13042 int query_size_account = 0;
13043 int err_code = EMAIL_ERROR_NONE;
13045 int result_thread_id = -1;
13046 char *sql_query_string = NULL;
13047 char *sql_account = NULL;
13048 char *sql_format = "SELECT thread_id FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
13049 char *sql_format_account = " AND account_id = %d ";
13050 char *sql_format_order_by = " ORDER BY date_time DESC ";
13051 char **result = NULL;
13052 char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
13053 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13055 EM_IF_NULL_RETURN_VALUE(mail_subject, EMAIL_ERROR_INVALID_PARAM);
13056 EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
13057 EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
13059 EM_DEBUG_LOG_SEC("subject: [%s]", mail_subject);
13061 if (em_find_pos_stripped_subject_for_thread_view(mail_subject, stripped_subject, STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE) {
13062 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
13063 err_code = EMAIL_ERROR_UNKNOWN;
13064 result_thread_id = -1;
13068 if (EM_SAFE_STRLEN(stripped_subject) == 0) {
13069 result_thread_id = -1;
13073 EM_DEBUG_LOG("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s", EM_SAFE_STRLEN(stripped_subject), stripped_subject);
13075 if (account_id > 0) {
13076 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
13077 sql_account = malloc(query_size_account);
13078 if (sql_account == NULL) {
13079 EM_DEBUG_EXCEPTION("malloc for sql_account is failed %d", query_size_account);
13080 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
13083 snprintf(sql_account, query_size_account, sql_format_account, account_id);
13086 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; */
13087 sql_query_string = malloc(query_size);
13089 if (sql_query_string == NULL) {
13090 EM_DEBUG_EXCEPTION("malloc for sql is failed %d", query_size);
13091 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
13095 sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mailbox_id);
13097 if (account_id > 0)
13098 strcat(sql_query_string, sql_account);
13100 strcat(sql_query_string, sql_format_order_by);
13101 strcat(sql_query_string, ";");
13103 EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
13105 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
13107 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13108 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13110 EM_DEBUG_LOG("Result rows count : %d", count);
13113 result_thread_id = -1;
13115 _get_table_field_data_int(result, &result_thread_id, 1);
13119 *thread_id = result_thread_id;
13120 *thread_item_count = count;
13122 EM_DEBUG_LOG("Result thread id : %d", *thread_id);
13123 EM_DEBUG_LOG("Result count : %d", *thread_item_count);
13124 EM_DEBUG_LOG("err_code : %d", err_code);
13126 EM_SAFE_FREE(sql_account);
13127 EM_SAFE_FREE(sql_query_string);
13129 sqlite3_free_table(result);
13131 EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
13136 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)
13138 EM_DEBUG_FUNC_BEGIN();
13140 int count = 0, ret = false;
13141 int error = EMAIL_ERROR_NONE;
13142 emstorage_mail_tbl_t *p_data_tbl = NULL;
13143 char conditional_clause[QUERY_SIZE] = {0, };
13145 EM_IF_NULL_RETURN_VALUE(mail_tbl, false);
13147 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE thread_id = %d AND thread_item_count > 0", thread_id);
13148 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
13150 if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
13151 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
13156 EM_DEBUG_LOG("thread_id : %d, thread_item_count : %d", p_data_tbl[0].thread_id, p_data_tbl[0].thread_item_count);
13162 *mail_tbl = p_data_tbl;
13163 else if (p_data_tbl != NULL)
13164 emstorage_free_mail(&p_data_tbl, 1, NULL);
13166 if (err_code != NULL)
13169 EM_DEBUG_FUNC_END("ret [%d]", ret);
13173 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)
13175 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]"
13176 , account_id , mailbox_id , search_type , search_value , sorting , sender_list, sender_count, err_code);
13178 if ((!sender_list) || (!sender_count)) {
13179 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
13180 if (err_code != NULL)
13181 *err_code = EMAIL_ERROR_INVALID_PARAM;
13185 int rc = -1, ret = false;
13186 int error = EMAIL_ERROR_NONE;
13188 int i, col_index = 0;
13189 int read_count = 0;
13190 email_sender_list_t *p_sender_list = NULL;
13191 char sql_query_string[QUERY_SIZE] = {0, };
13192 char **result = NULL;
13193 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13195 SNPRINTF(sql_query_string, sizeof(sql_query_string),
13196 "SELECT email_address_sender, alias_sender, COUNT(email_address_sender), SUM(flags_seen_field = 1) "
13201 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);
13202 else /* NULL means all mailbox_name. but except for trash(3), spambox(5), all emails(for GMail, 7) */
13203 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) ");
13206 /* '0' (ALL_ACCOUNT) means all account */
13207 if (account_id > ALL_ACCOUNT)
13208 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);
13210 if (search_value) {
13211 switch (search_type) {
13212 case EMAIL_SEARCH_FILTER_SUBJECT:
13213 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13214 " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\')) ", search_value);
13216 case EMAIL_SEARCH_FILTER_SENDER:
13217 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13218 " AND ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13219 ") ", search_value);
13221 case EMAIL_SEARCH_FILTER_RECIPIENT:
13222 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13223 " AND ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13224 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13225 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13226 ") ", search_value, search_value, search_value);
13228 case EMAIL_SEARCH_FILTER_ALL:
13229 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13230 " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') "
13231 " OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13232 " OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13233 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13234 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13237 ")", search_value, search_value, search_value, search_value, search_value);
13243 /* sorting option is not available now. The order of sender list is ascending order by display name */
13244 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
13245 "GROUP BY email_address_sender "
13246 "ORDER BY UPPER(alias_sender) ");
13248 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
13250 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
13251 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13252 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13254 EM_DEBUG_LOG("Count of Sender [%d]", count);
13256 if (!(p_sender_list = (email_sender_list_t*)em_malloc(sizeof(email_sender_list_t) * count))) {
13257 EM_DEBUG_EXCEPTION("em_mallocfailed...");
13258 error = EMAIL_ERROR_OUT_OF_MEMORY;
13264 EM_DEBUG_LOG(">>>> DATA ASSIGN START >>");
13265 for (i = 0; i < count; i++) {
13266 _get_table_field_data_string(result, &(p_sender_list[i].address), 1, col_index++);
13267 _get_table_field_data_string(result, &(p_sender_list[i].display_name), 1, col_index++);
13268 _get_table_field_data_int(result, &(p_sender_list[i].total_count), col_index++);
13269 _get_table_field_data_int(result, &(read_count), col_index++);
13270 p_sender_list[i].unread_count = p_sender_list[i].total_count - read_count; /* unread count = total - read */
13272 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >>", count);
13274 sqlite3_free_table(result);
13281 *sender_list = p_sender_list;
13282 *sender_count = count;
13283 EM_DEBUG_LOG(">>>> COUNT : %d >>", count);
13287 if (err_code != NULL)
13290 EM_DEBUG_FUNC_END("ret [%d]", ret);
13294 INTERNAL_FUNC int emstorage_free_sender_list(email_sender_list_t **sender_list, int count)
13296 EM_DEBUG_FUNC_BEGIN("sender_list[%p], count[%d]", sender_list, count);
13298 int err = EMAIL_ERROR_NONE;
13301 if (!sender_list || !*sender_list) {
13302 EM_DEBUG_EXCEPTION("sender_list[%p], count[%d]", sender_list, count);
13303 err = EMAIL_ERROR_INVALID_PARAM;
13307 email_sender_list_t* p = *sender_list;
13310 for (; i < count; i++) {
13311 EM_SAFE_FREE(p[i].address);
13312 EM_SAFE_FREE(p[i].display_name);
13316 *sender_list = NULL;
13323 INTERNAL_FUNC int emstorage_free_address_info_list(email_address_info_list_t **address_info_list)
13325 EM_DEBUG_FUNC_BEGIN("address_info_list[%p]", address_info_list);
13327 int err = EMAIL_ERROR_NONE;
13328 email_address_info_t *p_address_info = NULL;
13329 GList *list = NULL;
13330 GList *node = NULL;
13333 if (!address_info_list || !*address_info_list) {
13334 EM_DEBUG_EXCEPTION("address_info_list[%p]", address_info_list);
13335 err = EMAIL_ERROR_INVALID_PARAM;
13339 /* delete GLists */
13340 for (i = EMAIL_ADDRESS_TYPE_FROM; i <= EMAIL_ADDRESS_TYPE_BCC; i++) {
13342 case EMAIL_ADDRESS_TYPE_FROM:
13343 list = (*address_info_list)->from;
13345 case EMAIL_ADDRESS_TYPE_TO:
13346 list = (*address_info_list)->to;
13348 case EMAIL_ADDRESS_TYPE_CC:
13349 list = (*address_info_list)->cc;
13351 case EMAIL_ADDRESS_TYPE_BCC:
13352 list = (*address_info_list)->bcc;
13356 /* delete dynamic-allocated memory for each item */
13357 node = g_list_first(list);
13358 while (node != NULL) {
13359 p_address_info = (email_address_info_t*)node->data;
13360 EM_SAFE_FREE(p_address_info->address);
13361 EM_SAFE_FREE(p_address_info->display_name);
13362 EM_SAFE_FREE(node->data);
13364 node = g_list_next(node);
13369 EM_SAFE_FREE(*address_info_list);
13370 *address_info_list = NULL;
13372 EM_DEBUG_FUNC_END("err [%d]", err);
13375 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
13377 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)
13379 EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
13381 if (!local_activity || !activity_id) {
13382 EM_DEBUG_EXCEPTION("local_activity[%p], transaction[%d], activity_id[%p], err_code[%p]", local_activity, activity_id, transaction, err_code);
13383 if (err_code != NULL)
13384 *err_code = EMAIL_ERROR_INVALID_PARAM;
13390 int error = EMAIL_ERROR_NONE;
13393 char sql_query_string[QUERY_SIZE] = {0, };
13394 DB_STMT hStmt = NULL;
13395 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13397 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13398 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13399 SNPRINTF(sql_query_string, sizeof(sql_query_string),
13400 "INSERT INTO mail_partial_body_activity_tbl VALUES "
13402 "? " /* Account ID */
13403 ",?" /* Local Mail ID */
13404 ",?" /* Server mail ID */
13405 ",?" /* Activity ID */
13406 ",?" /* Activity type*/
13407 ",?" /* Mailbox ID*/
13408 ",?" /* Mailbox name*/
13409 ",?" /* Multi User Name */
13412 char *sql = "SELECT max(rowid) FROM mail_partial_body_activity_tbl;";
13413 char **result = NULL;
13416 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13418 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13419 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13421 if (NULL == result[1]) rc = 1;
13422 else rc = atoi(result[1])+1;
13423 sqlite3_free_table(result);
13426 *activity_id = local_activity->activity_id = rc;
13428 EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [ %d ], MAIL ID [ %d ], ACTIVITY TYPE [ %d ], SERVER MAIL ID [ %lu ]", \
13429 local_activity->activity_id, local_activity->mail_id, local_activity->activity_type, local_activity->server_mail_id);
13431 if (local_activity->mailbox_id)
13432 EM_DEBUG_LOG(" MAILBOX ID [ %d ]", local_activity->mailbox_id);
13435 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13436 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13437 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13440 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
13441 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
13442 _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
13443 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
13444 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
13445 _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
13446 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->mailbox_name, 0, 3999);
13447 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
13450 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13452 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
13453 ("sqlite3_step fail:%d", rc));
13454 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13455 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
13460 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13461 if (hStmt != NULL) {
13462 rc = sqlite3_finalize(hStmt);
13464 if (rc != SQLITE_OK) {
13465 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13466 error = EMAIL_ERROR_DB_FAILURE;
13470 if (err_code != NULL)
13473 EM_DEBUG_FUNC_END("ret [%d]", ret);
13477 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)
13479 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13481 if (account_id < FIRST_ACCOUNT_ID || NULL == mailbox_list || *mailbox_list == NULL || NULL == count) {
13482 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13483 if (err_code != NULL)
13484 *err_code = EMAIL_ERROR_INVALID_PARAM;
13489 int error = EMAIL_ERROR_NONE;
13491 int i = 0, rc = -1;
13492 int *mbox_list = NULL;
13493 DB_STMT hStmt = NULL;
13494 char sql_query_string[QUERY_SIZE] = {0, };
13496 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13497 EMSTORAGE_START_READ_TRANSACTION(transaction);
13499 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13500 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);
13503 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13504 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13505 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13507 *count = atoi(result[1]);
13508 sqlite3_free_table(result);
13511 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
13512 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13516 EM_DEBUG_LOG("Mailbox count = %d", *count);
13518 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13520 /* 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); */
13521 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);
13523 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13526 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13529 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13530 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13533 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13534 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13535 ("sqlite3_step fail:%d", rc));
13537 mbox_list = (int *)em_malloc(sizeof(int) * (*count));
13538 if (NULL == mbox_list) {
13539 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
13540 error = EMAIL_ERROR_OUT_OF_MEMORY;
13544 memset(mbox_list, 0x00, sizeof(int) * (*count));
13546 for (i = 0; i < (*count); i++) {
13547 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
13548 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13549 /* EM_DEBUG_LOG("In emstorage_get_pdb_mailbox_list() loop, After sqlite3_step(), , i = %d, rc = %d.", i, rc); */
13550 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13551 ("sqlite3_step fail:%d", rc));
13552 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
13559 *mailbox_list = mbox_list;
13561 EM_SAFE_FREE(mbox_list);
13563 if (hStmt != NULL) {
13564 rc = sqlite3_finalize(hStmt);
13566 if (rc != SQLITE_OK) {
13567 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13568 error = EMAIL_ERROR_DB_FAILURE;
13572 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13573 if (err_code != NULL)
13575 EM_DEBUG_FUNC_END("ret [%d]", ret);
13579 INTERNAL_FUNC int emstorage_get_pbd_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
13581 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13583 if (NULL == account_list || NULL == count) {
13584 EM_DEBUG_EXCEPTION("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13585 if (err_code != NULL)
13586 *err_code = EMAIL_ERROR_INVALID_PARAM;
13591 int error = EMAIL_ERROR_NONE;
13592 char *sql = "SELECT count(distinct account_id) FROM mail_partial_body_activity_tbl";
13594 int i = 0, rc = -1;
13595 int *result_account_list = NULL;
13596 DB_STMT hStmt = NULL;
13597 char sql_query_string[QUERY_SIZE] = {0, };
13599 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13601 EMSTORAGE_START_READ_TRANSACTION(transaction);
13603 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13604 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13605 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13607 *count = atoi(result[1]);
13608 sqlite3_free_table(result);
13611 EM_DEBUG_EXCEPTION("no account found...");
13612 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13617 EM_DEBUG_LOG("Account count [%d]", *count);
13619 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13621 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_partial_body_activity_tbl");
13623 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13626 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13628 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
13629 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13630 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13633 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13634 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13635 ("sqlite3_step fail:%d", rc));
13637 if (NULL == (result_account_list = (int *)em_malloc(sizeof(int) * (*count)))) {
13638 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
13639 error = EMAIL_ERROR_OUT_OF_MEMORY;
13643 memset(result_account_list, 0x00, sizeof(int) * (*count));
13645 for (i = 0; i < (*count); i++) {
13646 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
13648 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13649 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13650 ("sqlite3_step fail:%d", rc));
13651 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
13658 *account_list = result_account_list;
13660 EM_SAFE_FREE(result_account_list);
13662 if (hStmt != NULL) {
13663 rc = sqlite3_finalize(hStmt);
13665 if (rc != SQLITE_OK) {
13666 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13667 error = EMAIL_ERROR_DB_FAILURE;
13671 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13672 if (err_code != NULL)
13674 EM_DEBUG_FUNC_END("ret [%d]", ret);
13678 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)
13680 EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
13682 if (account_id < FIRST_ACCOUNT_ID || NULL == event_start || 0 == input_mailbox_id || NULL == count) {
13683 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);
13685 if (err_code != NULL)
13686 *err_code = EMAIL_ERROR_INVALID_PARAM;
13693 int error = EMAIL_ERROR_NONE;
13695 DB_STMT hStmt = NULL;
13696 email_event_partial_body_thd* event_list = NULL;
13697 char sql_query_string[QUERY_SIZE] = {0, };
13699 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13701 EMSTORAGE_START_READ_TRANSACTION(transaction);
13703 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13704 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);
13707 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13708 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13709 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13711 *count = atoi(result[1]);
13712 sqlite3_free_table(result);
13714 EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
13717 EM_DEBUG_LOG("No matched activity found in mail_partial_body_activity_tbl");
13718 error = EMAIL_ERROR_MAIL_NOT_FOUND;
13722 EM_DEBUG_LOG("Activity Count = %d", *count);
13724 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13725 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);
13727 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13730 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13732 EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
13733 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13734 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13737 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13738 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13739 ("sqlite3_step fail:%d", rc));
13741 if (!(event_list = (email_event_partial_body_thd*)em_malloc(sizeof(email_event_partial_body_thd) * (*count)))) {
13742 EM_DEBUG_EXCEPTION("Malloc failed");
13744 error = EMAIL_ERROR_OUT_OF_MEMORY;
13747 memset(event_list, 0x00, sizeof(email_event_partial_body_thd) * (*count));
13749 for (i = 0; i < (*count); i++) {
13750 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_IDX_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13751 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13752 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13753 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13754 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_type), ACTIVITY_TYPE_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13755 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13756 _get_stmt_field_data_string(hStmt, &(event_list[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13757 _get_stmt_field_data_string(hStmt, &(event_list[i].multi_user_name), 0, MULTI_USER_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13759 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13760 /* EM_DEBUG_LOG("In emstorage_get_pbd_activity_data() loop, After sqlite3_step(), , i = %d, rc = %d.", i, rc); */
13761 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13762 ("sqlite3_step fail:%d", rc));
13764 event_list[i].event_type = 0;
13771 *event_start = event_list;
13773 for (i = 0; i < (*count); i++)
13774 emcore_free_partial_body_thd_event(event_list, NULL);
13775 EM_SAFE_FREE(event_list); /*prevent 54559*/
13776 *event_start = NULL;
13780 if (hStmt != NULL) {
13781 rc = sqlite3_finalize(hStmt);
13783 if (rc != SQLITE_OK) {
13784 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13785 error = EMAIL_ERROR_DB_FAILURE;
13789 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13790 if (err_code != NULL)
13793 EM_DEBUG_FUNC_END("ret [%d]", ret);
13797 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)
13799 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);
13802 if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
13803 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);
13805 if (err_code != NULL)
13806 *err_code = EMAIL_ERROR_INVALID_PARAM;
13812 int error = EMAIL_ERROR_NONE;
13813 char sql_query_string[QUERY_SIZE] = {0, };
13814 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13816 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13817 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13819 if (activity_id == 0)
13820 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);
13822 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);
13824 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13825 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
13826 if (error != EMAIL_ERROR_NONE) {
13827 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
13831 /* validate activity existence */
13832 rc = sqlite3_changes(local_db_handle);
13834 EM_DEBUG_EXCEPTION("No matching activity found");
13835 error = EMAIL_ERROR_DATA_NOT_FOUND;
13843 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13845 if (err_code != NULL)
13848 EM_DEBUG_FUNC_END("ret [%d]", ret);
13852 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)
13854 EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
13856 if (account_id < FIRST_ACCOUNT_ID || NULL == activity_count || NULL == err_code) {
13857 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
13858 if (err_code != NULL)
13859 *err_code = EMAIL_ERROR_INVALID_PARAM;
13864 int error = EMAIL_ERROR_NONE;
13865 char sql_query_string[QUERY_SIZE] = {0, };
13867 DB_STMT hStmt = NULL;
13869 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13871 EMSTORAGE_START_READ_TRANSACTION(transaction);
13872 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13874 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);
13876 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13879 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13880 EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
13881 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13882 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13885 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13886 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13887 ("sqlite3_step fail:%d", rc));
13889 _get_stmt_field_data_int(hStmt, activity_count, 0);
13891 EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
13897 if (hStmt != NULL) {
13898 rc = sqlite3_finalize(hStmt);
13900 if (rc != SQLITE_OK) {
13901 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13902 error = EMAIL_ERROR_DB_FAILURE;
13906 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13908 if (err_code != NULL)
13911 EM_DEBUG_FUNC_END("ret [%d]", ret);
13915 INTERNAL_FUNC int emstorage_get_pbd_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
13917 EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
13919 if (NULL == activity_count || NULL == err_code) {
13920 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
13921 if (err_code != NULL)
13922 *err_code = EMAIL_ERROR_INVALID_PARAM;
13927 int error = EMAIL_ERROR_NONE;
13928 DB_STMT hStmt = NULL;
13929 char sql_query_string[QUERY_SIZE] = {0, };
13931 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13933 EMSTORAGE_START_READ_TRANSACTION(transaction);
13934 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13936 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl;");
13938 EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
13940 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13941 EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
13942 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13943 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13945 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13946 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13947 ("sqlite3_step fail:%d", rc));
13949 _get_stmt_field_data_int(hStmt, activity_count, 0);
13951 EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
13958 if (hStmt != NULL) {
13959 rc = sqlite3_finalize(hStmt);
13961 if (rc != SQLITE_OK) {
13962 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13963 error = EMAIL_ERROR_DB_FAILURE;
13967 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13968 if (err_code != NULL)
13971 EM_DEBUG_FUNC_END("ret [%d]", ret);
13975 INTERNAL_FUNC int emstorage_delete_full_pbd_activity_data(char *multi_user_name, int account_id, int transaction, int *err_code)
13977 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
13978 if (account_id < FIRST_ACCOUNT_ID) {
13979 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
13980 if (err_code != NULL)
13981 *err_code = EMAIL_ERROR_INVALID_PARAM;
13987 int error = EMAIL_ERROR_NONE;
13988 char sql_query_string[QUERY_SIZE] = {0, };
13990 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13992 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13993 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13994 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d", account_id);
13996 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13997 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
13998 if (error != EMAIL_ERROR_NONE) {
13999 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14003 rc = sqlite3_changes(local_db_handle);
14005 EM_DEBUG_EXCEPTION("No matching activities found in mail_partial_body_activity_tbl");
14006 error = EMAIL_ERROR_DATA_NOT_FOUND;
14015 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14017 if (err_code != NULL)
14020 EM_DEBUG_FUNC_END("ret [%d]", ret);
14024 /*Himanshu[h.gahlaut]-> Added below API to update mail_partial_body_activity_tbl
14025 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*/
14027 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)
14029 EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
14031 int rc = -1, ret = false;
14032 int error = EMAIL_ERROR_NONE;
14033 char sql_query_string[QUERY_SIZE] = {0, };
14035 int transaction = true;
14037 if (!old_server_uid || !new_server_uid || !mbox_name) {
14038 EM_DEBUG_EXCEPTION("Invalid parameters");
14039 error = EMAIL_ERROR_INVALID_PARAM;
14043 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14045 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14046 memset(sql_query_string, 0x00, sizeof(sql_query_string));
14047 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14048 "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);
14050 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14051 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14052 if (error != EMAIL_ERROR_NONE) {
14053 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14057 rc = sqlite3_changes(local_db_handle);
14059 EM_DEBUG_LOG("No matching found in mail_partial_body_activity_tbl");
14065 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14067 if (err_code != NULL)
14070 EM_DEBUG_FUNC_END("ret [%d]", ret);
14075 INTERNAL_FUNC int emstorage_create_file(char *data_string, size_t file_size, char *dst_file_name, int *err_code)
14077 EM_DEBUG_FUNC_BEGIN_SEC("file_size[%d] , dst_file_name[%s], err_code[%p]", file_size, dst_file_name, err_code);
14080 int error = EMAIL_ERROR_NONE;
14081 FILE* fp_dst = NULL;
14082 char errno_buf[ERRNO_BUF_SIZE] = {0};
14084 if (!data_string || !dst_file_name) {
14085 EM_DEBUG_LOG("data_string[%p], dst_file_name[%p]", data_string, dst_file_name);
14086 error = EMAIL_ERROR_INVALID_PARAM;
14090 error = em_fopen(dst_file_name, "w", &fp_dst);
14091 if (error != EMAIL_ERROR_NONE) {
14092 EM_DEBUG_EXCEPTION_SEC("em_fopen failed - %s: %d", dst_file_name, error);
14096 if (fwrite(data_string, 1, file_size, fp_dst) == 0) {
14097 EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
14098 error = EMAIL_ERROR_UNKNOWN;
14106 if (fp_dst != NULL)
14109 if (err_code != NULL)
14112 EM_DEBUG_FUNC_END("ret [%d]", ret);
14116 #endif /* __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
14120 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
14121 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)
14123 EM_DEBUG_FUNC_BEGIN();
14126 int error = EMAIL_ERROR_NONE;
14127 char sql_query_string[QUERY_SIZE] = {0, };
14129 int transaction = true;
14131 if (!old_server_uid || !new_server_uid || !mbox_name) {
14132 EM_DEBUG_EXCEPTION("Invalid parameters");
14133 if (err_code != NULL)
14134 *err_code = EMAIL_ERROR_INVALID_PARAM;
14138 EM_DEBUG_LOG_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
14140 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14141 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14144 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14145 "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);
14147 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
14148 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14149 if (error != EMAIL_ERROR_NONE) {
14150 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14154 rc = sqlite3_changes(local_db_handle);
14156 EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
14157 error = EMAIL_ERROR_DATA_NOT_FOUND;
14164 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14166 if (err_code != NULL)
14169 EM_DEBUG_FUNC_END("ret [%d]", ret);
14176 * @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);
14177 * Prepare an array of mail_id and corresponding server mail id.
14179 *@author h.gahlaut@samsung.com
14180 * @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)
14181 * where 88 is the length of fixed keywords including ending null character in the QUERY to be formed
14182 * @param[out] idset Returns the array of mail_id and corresponding server_mail_id sorted by server_mail_ids
14183 * @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
14184 * @param[out] err_code Returns the error code.
14185 * @remarks An Example of Query to be exexuted in this API:
14186 * SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (12, 13, 56, 78);
14187 * @return This function returns true on success or false on failure.
14189 INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *multi_user_name,
14191 email_id_set_t** idset,
14195 EM_DEBUG_FUNC_BEGIN();
14196 EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds);
14198 int error = EMAIL_ERROR_NONE;
14200 email_id_set_t* p_id_set = NULL;
14202 const int buf_size = QUERY_SIZE;
14203 char sql_query_string[QUERY_SIZE] = {0, };
14204 int space_left_in_query_buffer = buf_size;
14207 char *server_mail_id = NULL;
14208 char **result = NULL;
14212 if (NULL == mail_ids || NULL == idset || NULL == id_set_count) {
14213 EM_DEBUG_EXCEPTION("Invalid Parameters mail_ids[%p] idset[%p] id_set_count [%p]",
14214 mail_ids, idset, id_set_count);
14215 if (err_code != NULL)
14216 *err_code = EMAIL_ERROR_INVALID_PARAM;
14220 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14222 SNPRINTF(sql_query_string, space_left_in_query_buffer,
14223 "SELECT local_uid, server_uid FROM mail_read_mail_uid_tbl WHERE local_uid in (%s) ORDER BY server_uid",
14226 EM_DEBUG_LOG_SEC("SQL Query formed [%s] ", sql_query_string);
14228 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14229 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14230 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result);
14231 goto FINISH_OFF; }, ("SQL(%s) sqlite3_get_table fail:%d -%s",
14232 sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14234 EM_DEBUG_LOG(" Count of mails [%d ]", count);
14237 EM_DEBUG_EXCEPTION("Can't find proper mail");
14238 error = EMAIL_ERROR_DATA_NOT_FOUND;
14242 if (NULL == (p_id_set = (email_id_set_t*)em_malloc(sizeof(email_id_set_t) * count))) {
14243 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
14244 error = EMAIL_ERROR_OUT_OF_MEMORY;
14250 EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds_Loop);
14251 EM_DEBUG_LOG(">>>> DATA ASSIGN START");
14252 for (i = 0; i < count; i++) {
14253 _get_table_field_data_int(result, &(p_id_set[i].mail_id), col_index++);
14254 _get_table_field_data_string(result, &server_mail_id, 1, col_index++);
14255 if (server_mail_id) {
14256 p_id_set[i].server_mail_id = strtoul(server_mail_id, NULL, 10);
14257 EM_SAFE_FREE(server_mail_id);
14260 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d]", count);
14261 EM_PROFILE_END(EmStorageGetIdSetFromMailIds_Loop);
14263 sqlite3_free_table(result);
14272 *id_set_count = count;
14273 EM_DEBUG_LOG(" idset[%p] id_set_count [%d]", *idset, *id_set_count);
14275 EM_SAFE_FREE(p_id_set);
14278 if (err_code != NULL)
14281 EM_PROFILE_END(EmStorageGetIdSetFromMailIds);
14283 EM_DEBUG_FUNC_END("ret [%d]", ret);
14291 INTERNAL_FUNC int emstorage_delete_triggers_from_lucene(char *multi_user_name)
14293 EM_DEBUG_FUNC_BEGIN();
14294 int ret = true, transaction = true;
14295 int error = EMAIL_ERROR_NONE;
14296 char sql_query_string[QUERY_SIZE] = {0, };
14297 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14298 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14300 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerDelete;");
14301 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14302 if (error != EMAIL_ERROR_NONE) {
14303 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14307 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerInsert;");
14308 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14309 if (error != EMAIL_ERROR_NONE) {
14310 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14314 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerUpdate;");
14315 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14316 if (error != EMAIL_ERROR_NONE) {
14317 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14324 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14326 EM_DEBUG_FUNC_END("ret [%d]", ret);
14330 INTERNAL_FUNC int emstorage_update_tag_id(char *multi_user_name, int old_filter_id, int new_filter_id, int *err_code)
14332 EM_DEBUG_FUNC_BEGIN("new_filter_id[%d], old_filter_id[%d]", new_filter_id, old_filter_id);
14334 int error = EMAIL_ERROR_NONE;
14335 char sql_query_string[QUERY_SIZE] = {0, };
14336 int transaction = true;
14338 if (old_filter_id < 0 || new_filter_id < 0) {
14339 EM_DEBUG_EXCEPTION("Invalid parameters");
14340 if (err_code != NULL)
14341 *err_code = EMAIL_ERROR_INVALID_PARAM;
14345 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14347 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14349 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14350 "UPDATE mail_tbl SET tag_id=%d WHERE tag_id=%d ", new_filter_id, old_filter_id);
14352 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14353 if (error != EMAIL_ERROR_NONE) {
14354 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14361 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14363 if (err_code != NULL)
14366 EM_DEBUG_FUNC_END("ret [%d]", ret);
14370 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)
14372 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);
14374 if ((account_id < 0) || (dest_mailbox_id < 0) || (!rule)) {
14375 EM_DEBUG_EXCEPTION("Invalid Parameter");
14377 if (err_code != NULL)
14378 *err_code = EMAIL_ERROR_INVALID_PARAM;
14382 int rc = -1, ret = false, error = EMAIL_ERROR_NONE;
14383 int count = 0, col_index = 0, i = 0, where_pararaph_length = 0, *mail_list = NULL;
14384 int tag_id = rule->rule_id;
14385 char **result = NULL, *where_pararaph = NULL;
14386 char sql_query_string[QUERY_SIZE] = {0, };
14387 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14389 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl ");
14391 where_pararaph_length = EM_SAFE_STRLEN(rule->value) + 2 * (EM_SAFE_STRLEN(rule->value2)) + 100;
14392 where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
14393 if (where_pararaph == NULL) {
14394 EM_DEBUG_EXCEPTION("malloc failed for where_pararaph.");
14395 error = EMAIL_ERROR_OUT_OF_MEMORY;
14399 if (account_id != ALL_ACCOUNT)
14400 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (0)", account_id);
14402 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_type NOT in (0)");
14404 if (rule->type & EMAIL_FILTER_SUBJECT) {
14405 if (rule->flag2 == RULE_TYPE_INCLUDES)
14406 sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject like \'%%%q%%\'", rule->value);
14407 else /* RULE_TYPE_EXACTLY */
14408 sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject = \'%q\'", rule->value);
14411 if (rule->type & EMAIL_FILTER_FROM) {
14412 if (rule->flag2 == RULE_TYPE_INCLUDES)
14413 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);
14414 #ifdef __FEATURE_COMPARE_DOMAIN__
14415 else if (rule->flag2 == RULE_TYPE_COMPARE_DOMAIN)
14416 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);
14417 #endif /*__FEATURE_COMPARE_DOMAIN__ */
14418 else /* RULE_TYPE_EXACTLY */
14419 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);
14422 if (rule->type == EMAIL_PRIORITY_SENDER) {
14423 if (rule->flag2 == RULE_TYPE_INCLUDES)
14424 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);
14425 else /* RULE_TYPE_EXACTLY */
14426 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);
14428 tag_id = PRIORITY_SENDER_TAG_ID;
14431 /* prevent 34361 */
14432 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14433 strcat(sql_query_string, where_pararaph);
14435 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14437 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14438 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14439 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
14440 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14442 EM_DEBUG_LOG("Count of mails [%d]", count);
14445 mail_list = malloc(sizeof(int) * count);
14446 if (mail_list == NULL) {
14447 EM_DEBUG_EXCEPTION("malloc failed for mail_list.");
14448 error = EMAIL_ERROR_OUT_OF_MEMORY;
14454 for (i = 0; i < count; i++)
14455 _get_table_field_data_int(result, &(mail_list[i]), col_index++);
14457 memset(sql_query_string, 0x00, QUERY_SIZE);
14459 switch (rule->action_type) {
14460 case EMAIL_FILTER_MOVE:
14461 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = 0 ");
14463 case EMAIL_FILTER_BLOCK:
14465 EM_DEBUG_LOG("Not support : action_type[%d]", rule->action_type);
14470 switch (rule->action_type) {
14471 case EMAIL_FILTER_MOVE:
14472 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = %d ", tag_id);
14474 case EMAIL_FILTER_BLOCK:
14475 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET mailbox_id = %d, mailbox_type = %d ", dest_mailbox_id, dest_mailbox_type);
14478 EM_DEBUG_LOG("Not support");
14483 /* prevent 34361 */
14484 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14485 strcat(sql_query_string, where_pararaph);
14487 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14488 if (error != EMAIL_ERROR_NONE) {
14489 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14493 #ifdef __FEATURE_BODY_SEARCH__
14494 /* Updating mail_text_tbl */
14495 if (rule->action_type == EMAIL_FILTER_BLOCK) {
14496 memset(sql_query_string, 0x00, QUERY_SIZE);
14497 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_text_tbl SET mailbox_id = %d ", dest_mailbox_id);
14498 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14499 strcat(sql_query_string, where_pararaph);
14501 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14502 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14503 if (error != EMAIL_ERROR_NONE) {
14504 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14516 if (filtered_mail_id_list)
14517 *filtered_mail_id_list = mail_list;
14519 if (count_of_mails)
14520 *count_of_mails = count;
14522 EM_SAFE_FREE(mail_list);
14524 sqlite3_free_table(result);
14528 EM_SAFE_FREE(where_pararaph);
14530 if (err_code != NULL)
14533 EM_DEBUG_FUNC_END("ret [%d]", ret);
14537 #define EMAIL_SLOT_UNIT 25
14538 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)
14540 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);
14541 int ret = false, err = EMAIL_ERROR_NONE;
14542 int where_pararaph_length = 0;
14543 char *where_pararaph = NULL;
14544 char sql_query_string[QUERY_SIZE] = {0, };
14546 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14548 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
14550 if (new_slot_size > 0)
14551 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = %d ", new_slot_size);
14552 else if (new_slot_size == 0)
14553 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", EMAIL_SLOT_UNIT);
14555 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", new_slot_size * -1);
14559 where_pararaph_length = 80;
14561 where_pararaph_length = 50;
14563 if (new_slot_size == 0)
14564 where_pararaph_length += 70;
14566 where_pararaph = malloc(sizeof(char) * where_pararaph_length);
14567 if (where_pararaph == NULL) {
14568 EM_DEBUG_EXCEPTION("Memory allocation failed for where_pararaph");
14569 err = EMAIL_ERROR_OUT_OF_MEMORY;
14572 memset(where_pararaph, 0x00, where_pararaph_length);
14574 if (account_id > ALL_ACCOUNT) {
14577 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_id = %d ", mailbox_id);
14579 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d ", account_id);
14582 if (new_slot_size == 0)
14583 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"));
14585 if (strlen(sql_query_string) + EM_SAFE_STRLEN(where_pararaph) < QUERY_SIZE) /* prevent 34363 */
14586 strcat(sql_query_string, where_pararaph);
14588 EM_DEBUG_EXCEPTION("Query buffer overflowed !!!");
14589 err = EMAIL_ERROR_OUT_OF_MEMORY;
14593 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14594 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14595 if (err != EMAIL_ERROR_NONE) {
14596 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
14603 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
14605 EM_SAFE_FREE(where_pararaph);
14607 if (err_code != NULL)
14610 EM_DEBUG_FUNC_END("ret [%d]", ret);
14614 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)
14616 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);
14618 if (!meeting_req || meeting_req->mail_id <= 0) {
14620 EM_DEBUG_EXCEPTION("mail_id[%]d", meeting_req->mail_id);
14622 if (err_code != NULL)
14623 *err_code = EMAIL_ERROR_INVALID_PARAM;
14630 int error = EMAIL_ERROR_NONE;
14631 DB_STMT hStmt = NULL;
14632 char sql_query_string[QUERY_SIZE] = {0, };
14634 time_t temp_unix_time = 0;
14636 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14637 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14639 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14640 "INSERT INTO mail_meeting_tbl VALUES "
14642 ", ?" /* account_id */
14643 ", ?" /* mailbox_id */
14644 ", ?" /* meeting_response */
14645 ", ?" /* start_time */
14646 ", ?" /* end_time */
14647 ", ?" /* location */
14648 ", ?" /* global_object_id */
14650 ", ?" /* standard_name */
14651 ", ?" /* standard_time_start_date */
14652 ", ?" /* standard_biad */
14653 ", ?" /* daylight_name */
14654 ", ?" /* daylight_time_start_date */
14655 ", ?" /* daylight_bias */
14658 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14659 if (rc != SQLITE_OK) {
14660 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
14661 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
14663 error = EMAIL_ERROR_DB_FAILURE;
14669 EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
14670 EM_DEBUG_LOG_SEC(">>>>> account_id[%d]", account_id);
14671 EM_DEBUG_LOG_SEC(">>>>> mailbox_name[%s]", mailbox_name);
14672 EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
14673 EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
14674 EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
14675 EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
14676 EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
14677 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
14678 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
14679 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
14680 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
14681 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
14682 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
14683 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
14685 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->mail_id);
14686 _bind_stmt_field_data_int(hStmt, col_index++, account_id);
14687 _bind_stmt_field_data_int(hStmt, col_index++, input_mailbox_id);
14688 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
14690 temp_unix_time = timegm(&(meeting_req->start_time));
14691 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14692 temp_unix_time = timegm(&(meeting_req->end_time));
14693 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14695 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->location, 0, LOCATION_LEN_IN_MAIL_MEETING_TBL);
14696 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->global_object_id, 0, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
14698 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
14699 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.standard_name, 0, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
14700 temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
14701 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14702 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
14704 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.daylight_name, 0, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
14705 temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
14706 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14707 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
14710 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14711 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
14712 ("sqlite3_step fail:%d", rc));
14713 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14714 ("sqlite3_step fail:%d", rc));
14719 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14720 if (hStmt != NULL) {
14721 rc = sqlite3_finalize(hStmt);
14722 if (rc != SQLITE_OK) {
14723 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14724 error = EMAIL_ERROR_DB_FAILURE;
14728 if (err_code != NULL)
14731 EM_DEBUG_FUNC_END("ret [%d]", ret);
14735 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)
14737 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);
14744 int err = EMAIL_ERROR_NONE;
14745 char **result = NULL;
14746 char sql_query_string[QUERY_SIZE] = {0, };
14747 email_meeting_request_t* p_temp_meeting_req = NULL;
14748 sqlite3 *local_db_handle = NULL;
14749 time_t temp_unix_time;
14751 if (conditional_clause == NULL || output_meeting_req == NULL || output_result_count == NULL) {
14752 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14753 err = EMAIL_ERROR_INVALID_PARAM;
14757 col_index = _field_count_of_table[CREATE_TABLE_MAIL_MEETING_TBL];
14758 EM_DEBUG_LOG("col_index [%d]", col_index);
14760 local_db_handle = emstorage_get_db_connection(multi_user_name);
14762 EMSTORAGE_START_READ_TRANSACTION(transaction);
14764 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_meeting_tbl %s", conditional_clause);
14766 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14768 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14769 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
14770 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14773 EM_DEBUG_EXCEPTION("No meeting_request found...");
14774 err = EMAIL_ERROR_DATA_NOT_FOUND;
14778 EM_DEBUG_LOG("There are [%d] meeting requests.", count);
14779 if (!(p_temp_meeting_req = (email_meeting_request_t*)em_malloc(sizeof(email_meeting_request_t) * count))) {
14780 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
14781 err = EMAIL_ERROR_OUT_OF_MEMORY;
14785 EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
14787 for (i = 0; i < count; i++) {
14788 _get_table_field_data_int(result, &(p_temp_meeting_req[i].mail_id), col_index++);
14789 _get_table_field_data_int(result, &dummy, col_index++); /* account_id. but why should this field exist in DB table? */
14790 _get_table_field_data_int(result, &dummy, col_index++); /* mailbox_id */
14791 _get_table_field_data_int(result, (int*)&(p_temp_meeting_req[i].meeting_response), col_index++);
14792 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* start time */
14793 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].start_time));
14794 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* end time */
14795 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].end_time));
14796 _get_table_field_data_string(result, &p_temp_meeting_req[i].location, 1, col_index++);
14797 _get_table_field_data_string(result, &p_temp_meeting_req[i].global_object_id, 1, col_index++);
14798 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.offset_from_GMT), col_index++);
14799 _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++);
14800 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14801 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.standard_time_start_date));
14802 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.standard_bias), col_index++);
14803 _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++);
14804 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14805 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.daylight_time_start_date));
14806 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.daylight_bias), col_index++);
14812 sqlite3_free_table(result);
14814 if (err == EMAIL_ERROR_NONE) {
14815 if (p_temp_meeting_req)
14816 *output_meeting_req = p_temp_meeting_req;
14817 *output_result_count = count;
14819 EM_SAFE_FREE(p_temp_meeting_req);
14821 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14823 // sqlite3_db_release_memory(local_db_handle);
14825 EM_DEBUG_FUNC_END("err [%d]", err);
14829 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)
14831 EM_DEBUG_FUNC_BEGIN();
14835 int error = EMAIL_ERROR_NONE;
14836 char conditional_clause[QUERY_SIZE] = {0, };
14838 EM_IF_NULL_RETURN_VALUE(meeting_req, false);
14841 SNPRINTF(conditional_clause, QUERY_SIZE, " WHERE mail_id = %d", mail_id);
14842 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
14844 if ((error = emstorage_query_meeting_request(multi_user_name, conditional_clause, meeting_req, &count, transaction)) != EMAIL_ERROR_NONE) {
14845 EM_DEBUG_EXCEPTION("emstorage_query_meeting_request failed. [%d]", error);
14849 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->mail_id[%d]", (*meeting_req)->mail_id);
14850 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->meeting_response[%d]", (*meeting_req)->meeting_response);
14851 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->start_time[%s]", asctime(&((*meeting_req)->start_time)));
14852 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->end_time[%s]", asctime(&((*meeting_req)->end_time)));
14853 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->location[%s]", (*meeting_req)->location);
14854 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->global_object_id[%s]", (*meeting_req)->global_object_id);
14855 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.offset_from_GMT[%d]", (*meeting_req)->time_zone.offset_from_GMT);
14856 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_name[%s]", (*meeting_req)->time_zone.standard_name);
14857 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.standard_time_start_date)));
14858 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_bias[%d]", (*meeting_req)->time_zone.standard_bias);
14859 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_name[%s]", (*meeting_req)->time_zone.daylight_name);
14860 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.daylight_time_start_date)));
14861 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_bias[%d]", (*meeting_req)->time_zone.daylight_bias);
14866 if (err_code != NULL)
14869 EM_DEBUG_FUNC_END("ret [%d]", ret);
14873 INTERNAL_FUNC int emstorage_update_meeting_request(char *multi_user_name, email_meeting_request_t* meeting_req, int transaction, int *err_code)
14875 EM_DEBUG_FUNC_BEGIN("meeting_req[%p], transaction[%d], err_code[%p]", meeting_req, transaction, err_code);
14878 int error = EMAIL_ERROR_NONE;
14880 DB_STMT hStmt = NULL;
14881 char sql_query_string[QUERY_SIZE] = {0, };
14882 time_t temp_unix_time = 0;
14884 if (!meeting_req) {
14885 EM_DEBUG_EXCEPTION("Invalid Parameter!");
14886 if (err_code != NULL)
14887 *err_code = EMAIL_ERROR_INVALID_PARAM;
14891 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14892 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14894 memset(sql_query_string, 0x00, sizeof(sql_query_string));
14895 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14896 "UPDATE mail_meeting_tbl "
14898 /* " account_id = ?, " // not update here, this can be changed when move or copy */
14899 /* " mailbox_name = ?, " // not update here, this can be changed when move or copy */
14900 " meeting_response = ?, "
14901 " start_time = ?, "
14904 " global_object_id = ?, "
14906 " standard_name = ?, "
14907 " standard_time_start_date = ?, "
14908 " standard_bias = ?, "
14909 " daylight_name = ?, "
14910 " daylight_time_start_date = ?, "
14911 " daylight_bias = ? "
14912 "WHERE mail_id = %d",
14913 meeting_req->mail_id);
14915 EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
14917 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14918 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14919 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14921 EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
14922 EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
14923 EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
14924 EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
14925 EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
14926 EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
14927 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
14928 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
14929 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
14930 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
14931 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
14932 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
14933 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
14937 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
14938 temp_unix_time = timegm(&(meeting_req->start_time));
14939 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14940 temp_unix_time = timegm(&(meeting_req->end_time));
14941 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14942 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->location, 1, LOCATION_LEN_IN_MAIL_MEETING_TBL);
14943 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->global_object_id, 1, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
14944 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
14945 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.standard_name, 1, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
14946 temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
14947 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14948 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
14949 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.daylight_name, 1, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
14950 temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
14951 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14952 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
14955 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14956 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14957 ("sqlite3_step fail:%d", rc));
14962 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14964 if (hStmt != NULL) {
14965 rc = sqlite3_finalize(hStmt);
14966 if (rc != SQLITE_OK) {
14967 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14968 error = EMAIL_ERROR_DB_FAILURE;
14972 if (err_code != NULL)
14975 EM_DEBUG_FUNC_END("ret [%d]", ret);
14979 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)
14981 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);
14983 if (account_id < ALL_ACCOUNT || mail_id < 0) {
14984 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d]", account_id, mail_id);
14986 if (err_code != NULL)
14987 *err_code = EMAIL_ERROR_INVALID_PARAM;
14992 int error = EMAIL_ERROR_NONE;
14994 char sql_query_string[QUERY_SIZE] = {0, };
14996 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14997 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14999 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_meeting_tbl ");
15001 if (account_id != ALL_ACCOUNT) { /* NOT '0' means a specific account. '0' means all account */
15002 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);
15006 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);
15009 if (input_mailbox_id > 0) { /* 0 means all mailbox_id */
15010 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);
15013 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15014 if (error != EMAIL_ERROR_NONE) {
15015 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
15022 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15027 EM_DEBUG_FUNC_END("ret [%d]", ret);
15032 INTERNAL_FUNC void emstorage_free_meeting_request(email_meeting_request_t *meeting_req)
15034 EM_DEBUG_FUNC_BEGIN();
15036 if (!meeting_req) return;
15038 EM_SAFE_FREE(meeting_req->location);
15039 EM_SAFE_FREE(meeting_req->global_object_id);
15041 EM_DEBUG_FUNC_END();
15044 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)
15046 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);
15047 EM_PROFILE_BEGIN(profile_emstorage_get_overflowed_mail_id_list);
15048 char sql_query_string[QUERY_SIZE] = {0, };
15049 char **result = NULL;
15050 int rc = -1, ret = false;
15051 int error = EMAIL_ERROR_NONE;
15052 int counter = 0, col_index = 0;
15053 int result_mail_id_count = 0;
15054 int *result_mail_id_list = NULL;
15056 if (input_mailbox_id <= 0 || !mail_id_list || !mail_id_count || account_id < 1) {
15057 EM_DEBUG_EXCEPTION("Invalid Parameter");
15059 *err_code = EMAIL_ERROR_INVALID_PARAM;
15063 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);
15065 EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
15067 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15068 EMSTORAGE_START_READ_TRANSACTION(transaction);
15070 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_mail_id_count, 0, NULL), rc);
15071 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15072 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15074 if (!result_mail_id_count) {
15075 EM_DEBUG_LOG("No mail found...");
15077 error = EMAIL_ERROR_MAIL_NOT_FOUND;
15081 EM_DEBUG_LOG("There are [%d] overflowed mails in mailbox_id [%d]", result_mail_id_count, input_mailbox_id);
15083 if (!(result_mail_id_list = (int *)malloc(sizeof(int) * result_mail_id_count))) {
15084 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
15085 error = EMAIL_ERROR_OUT_OF_MEMORY;
15086 sqlite3_free_table(result);
15090 memset(result_mail_id_list, 0x00, sizeof(int) * result_mail_id_count);
15094 for (counter = 0; counter < result_mail_id_count; counter++)
15095 _get_table_field_data_int(result, result_mail_id_list + counter, col_index++);
15100 EM_DEBUG_LOG("finish off [%d]", ret);
15103 sqlite3_free_table(result);
15106 *mail_id_list = result_mail_id_list;
15107 *mail_id_count = result_mail_id_count;
15109 EM_SAFE_FREE(result_mail_id_list);
15111 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15113 if (err_code != NULL)
15116 EM_PROFILE_END(profile_emstorage_get_overflowed_mail_id_list);
15117 EM_DEBUG_FUNC_END("ret [%d]", ret);
15121 INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(char *multi_user_name, int mail_id, int *thread_id, int *err_code)
15123 EM_DEBUG_FUNC_BEGIN("mail_id[%d], thread_id[%p], err_code[%p]", mail_id, thread_id, err_code);
15125 int rc = -1, ret = false;
15126 int err = EMAIL_ERROR_NONE;
15127 char sql_query_string[QUERY_SIZE] = {0, };
15129 int result_count = 0;
15131 if (mail_id == 0 || thread_id == NULL) {
15132 EM_DEBUG_EXCEPTION("Invalid Parameter");
15134 *err_code = EMAIL_ERROR_INVALID_PARAM;
15138 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15140 memset(sql_query_string, 0, QUERY_SIZE);
15141 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT thread_id FROM mail_tbl WHERE mail_id = %d", mail_id);
15143 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
15144 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
15145 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15146 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15148 if (!result_count) {
15149 EM_DEBUG_LOG("No mail found...");
15151 err = EMAIL_ERROR_MAIL_NOT_FOUND;
15152 /* sqlite3_free_table(result); */
15156 _get_table_field_data_int(result, thread_id, 1);
15158 sqlite3_free_table(result);
15164 if (err_code != NULL)
15167 EM_DEBUG_FUNC_END("ret [%d]", ret);
15171 INTERNAL_FUNC int emstorage_update_latest_thread_mail(char *multi_user_name,
15176 int *updated_thread_id,
15177 int latest_mail_id,
15178 int thread_item_count,
15183 EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id [%d], thread_id[%d], updated_thread_id[%p], "
15184 "latest_mail_id [%d], thread_item_count[%d], err_code[%p]",
15185 account_id, mailbox_id, thread_id, updated_thread_id,
15186 latest_mail_id, thread_item_count, err_code);
15188 int rc = -1, ret = false;
15189 int err = EMAIL_ERROR_NONE;
15190 char sql_query_string[QUERY_SIZE] = {0, };
15191 char **result = NULL;
15192 int result_count = 0;
15194 if (thread_id == 0) {
15195 EM_DEBUG_EXCEPTION("Invalid Parameter");
15197 *err_code = EMAIL_ERROR_INVALID_PARAM;
15201 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15203 if (thread_item_count == 0 && latest_mail_id == 0) {
15204 memset(sql_query_string, 0, QUERY_SIZE);
15205 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);
15207 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
15208 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
15209 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15210 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15211 EM_DEBUG_LOG("result_count[%d]", result_count);
15212 if (result_count == 0) {
15213 EM_DEBUG_LOG("No mail found...");
15216 *err_code = EMAIL_ERROR_MAIL_NOT_FOUND;
15217 sqlite3_free_table(result);
15221 _get_table_field_data_int(result, &latest_mail_id, 2);
15222 _get_table_field_data_int(result, &thread_item_count, 3);
15224 EM_DEBUG_LOG("latest_mail_id[%d]", latest_mail_id);
15225 EM_DEBUG_LOG("thread_item_count[%d]", thread_item_count);
15227 sqlite3_free_table(result);
15230 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15232 if (thread_item_count < 0) {
15233 memset(sql_query_string, 0, QUERY_SIZE);
15234 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);
15235 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15236 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15237 if (err != EMAIL_ERROR_NONE) {
15238 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15241 } else if (thread_id != latest_mail_id) {
15242 /* Initialize the thread id */
15243 memset(sql_query_string, 0, QUERY_SIZE);
15244 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);
15245 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15246 if (err != EMAIL_ERROR_NONE) {
15247 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15251 /* update the thread item count */
15252 memset(sql_query_string, 0, QUERY_SIZE);
15253 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);
15254 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15255 if (err != EMAIL_ERROR_NONE) {
15256 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15260 memset(sql_query_string, 0, QUERY_SIZE);
15261 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);
15262 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15263 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15264 if (err != EMAIL_ERROR_NONE) {
15265 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15272 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15274 if (thread_id != latest_mail_id) {
15275 if (err == EMAIL_ERROR_NONE) {
15276 EM_DEBUG_LOG("noti_type[%d]", noti_type);
15278 if (latest_mail_id > 0 && thread_id > 0 && noti_type > 0) {
15279 char mailbox_id_str[25] = {0,};
15280 snprintf(mailbox_id_str, sizeof(mailbox_id_str), "%d", mailbox_id);
15281 if (!emcore_notify_storage_event(noti_type, thread_id, latest_mail_id, mailbox_id_str, account_id))
15282 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventfailed [NOTI_THREAD_ID_CHANGED] >>>> ");
15284 if (updated_thread_id) *updated_thread_id = latest_mail_id;
15287 } else if (thread_item_count >= 0) {
15288 if (err == EMAIL_ERROR_NONE) {
15289 char parameter_string[500] = {0,};
15290 SNPRINTF(parameter_string, sizeof(parameter_string), "%s%c%d", "thread_item_count", 0x01, latest_mail_id);
15291 if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT, parameter_string, thread_item_count))
15292 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventfailed [NOTI_MAIL_FIELD_UPDATE] >>>> ");
15296 if (err_code != NULL)
15299 EM_DEBUG_FUNC_END("ret [%d]", ret);
15303 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)
15305 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);
15308 int err = EMAIL_ERROR_NONE;
15309 char sql_query_string[QUERY_SIZE] = {0, };
15311 if (thread_id == 0) {
15312 EM_DEBUG_EXCEPTION("Invalid Parameter");
15314 *err_code = EMAIL_ERROR_INVALID_PARAM;
15318 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15320 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15322 memset(sql_query_string, 0, QUERY_SIZE);
15323 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);
15324 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15325 if (err != EMAIL_ERROR_NONE) {
15326 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15333 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15335 if (err_code != NULL)
15338 EM_DEBUG_FUNC_END("ret [%d]", ret);
15342 #ifdef __FEATURE_LOCAL_ACTIVITY__
15344 * emstorage_add_activity - Add Email Local activity during OFFLINE mode
15347 INTERNAL_FUNC int emstorage_add_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15349 EM_DEBUG_FUNC_BEGIN();
15351 EM_DEBUG_LOG(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15353 int rc = -1, ret = false;
15354 int error = EMAIL_ERROR_NONE;
15355 DB_STMT hStmt = NULL;
15356 char sql_query_string[8192] = { 0x00, };
15359 if (!local_activity) {
15360 EM_DEBUG_EXCEPTION(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15361 if (err_code != NULL)
15362 *err_code = EMAIL_ERROR_INVALID_PARAM;
15366 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15368 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15370 memset(sql_query_string, 0x00 , sizeof(sql_query_string));
15371 SNPRINTF(sql_query_string, sizeof(sql_query_string), "INSERT INTO mail_local_activity_tbl VALUES (?, ?, ?, ?, ?, ?, ?)");
15373 EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ] ", local_activity->activity_id);
15374 EM_DEBUG_LOG(">>>>> MAIL ID [ %d ] ", local_activity->mail_id);
15375 EM_DEBUG_LOG(">>>>> ACCOUNT ID [ %d ] ", local_activity->account_id);
15376 EM_DEBUG_LOG(">>>>> ACTIVITY TYPE [ %d ] ", local_activity->activity_type);
15377 EM_DEBUG_LOG_SEC(">>>>> SERVER MAIL ID [ %s ] ", local_activity->server_mailid);
15378 EM_DEBUG_LOG(">>>>> SOURCE MAILBOX [ %s ] ", local_activity->src_mbox);
15379 EM_DEBUG_LOG(">>>>> DEST MAILBOX [ %s ] ", local_activity->dest_mbox);
15381 EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15384 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15385 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15386 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15388 EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15390 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
15391 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
15392 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
15393 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
15394 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->server_mailid, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
15395 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->src_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15396 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->dest_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15399 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15401 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
15402 ("sqlite3_step fail:%d", rc));
15403 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15404 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
15409 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15411 if (hStmt != NULL) {
15412 rc = sqlite3_finalize(hStmt);
15413 if (rc != SQLITE_OK) {
15414 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15415 error = EMAIL_ERROR_DB_FAILURE;
15419 if (err_code != NULL)
15422 EM_DEBUG_FUNC_END("ret [%d]", ret);
15427 * emstorage_get_activity - Get the Local activity Information
15431 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)
15433 EM_DEBUG_FUNC_BEGIN();
15435 int i = 0, count = 0, rc = -1, ret = false;
15436 int error = EMAIL_ERROR_NONE;
15437 emstorage_activity_tbl_t *p_activity_tbl = NULL;
15438 char sql_query_string[1024] = {0x00, };
15439 char **result = NULL;
15442 EM_IF_NULL_RETURN_VALUE(activity_list, false);
15443 EM_IF_NULL_RETURN_VALUE(select_num, false);
15446 if (!select_num || !activity_list || account_id <= 0 || activityid < 0) {
15447 EM_DEBUG_LOG(" select_num[%p], activity_list[%p] account_id [%d] activityid [%d] ", select_num, activity_list, account_id, activityid);
15449 *err_code = EMAIL_ERROR_INVALID_PARAM;
15453 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15455 EMSTORAGE_START_READ_TRANSACTION(transaction);
15457 memset(sql_query_string, 0x00, sizeof(sql_query_string));
15459 if (activityid == ALL_ACTIVITIES) {
15460 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d order by activity_id", account_id);
15462 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d AND activity_id = %d ", account_id, activityid);
15465 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
15469 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15470 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15471 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15475 if (!(p_activity_tbl = (emstorage_activity_tbl_t*)em_malloc(sizeof(emstorage_activity_tbl_t) * count))) {
15476 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
15477 error = EMAIL_ERROR_OUT_OF_MEMORY;
15482 for (i = 0; i < count; i++) {
15483 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15484 if (result[col_index])
15485 p_activity_tbl[i].activity_id = atoi(result[col_index++]);
15487 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15488 if (result[col_index])
15489 p_activity_tbl[i].account_id = atoi(result[col_index++]);
15491 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15492 if (result[col_index])
15493 p_activity_tbl[i].mail_id = atoi(result[col_index++]);
15495 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15496 if (result[col_index])
15497 p_activity_tbl[i].activity_type = atoi(result[col_index++]);
15500 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15501 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15502 p_activity_tbl[i].server_mailid = EM_SAFE_STRDUP(result[col_index++]);
15506 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15507 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15508 p_activity_tbl[i].src_mbox = EM_SAFE_STRDUP(result[col_index++]);
15512 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15513 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15514 p_activity_tbl[i].dest_mbox = EM_SAFE_STRDUP(result[col_index++]);
15521 sqlite3_free_table(result);
15527 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15530 *activity_list = p_activity_tbl;
15531 *select_num = count;
15532 EM_DEBUG_LOG(">>>> COUNT : %d >> ", count);
15533 } else if (p_activity_tbl != NULL) {
15534 emstorage_free_local_activity(&p_activity_tbl, count, NULL);
15538 if (err_code != NULL)
15541 EM_DEBUG_FUNC_END("ret [%d]", ret);
15546 INTERNAL_FUNC int emstorage_get_next_activity_id(int *activity_id, int *err_code)
15549 EM_DEBUG_FUNC_BEGIN();
15552 int err = EMAIL_ERROR_NONE;
15555 char **result = NULL;
15557 if (NULL == activity_id) {
15558 EM_DEBUG_EXCEPTION(" activity_id[%p]", activity_id);
15560 *err_code = EMAIL_ERROR_INVALID_PARAM;
15564 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15566 /* increase unique id */
15568 sql = "SELECT max(rowid) FROM mail_local_activity_tbl;";
15570 /* 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); */
15571 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15572 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
15574 if (NULL == result[1])
15577 rc = atoi(result[1])+1;
15582 sqlite3_free_table(result);
15591 EM_DEBUG_FUNC_END("ret [%d]", ret);
15596 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)
15599 EM_DEBUG_FUNC_BEGIN();
15601 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);
15603 if (account_id <= 0 || NULL == activity_id_list || NULL == activity_id_count || lowest_activity_type <= 0 || highest_activity_type <= 0) {
15604 if (err_code != NULL)
15605 *err_code = EMAIL_ERROR_INVALID_PARAM;
15610 int error = EMAIL_ERROR_NONE;
15611 int i = 0, rc = -1, count = 0;
15612 char sql_query_string[1024] = {0x00, };
15613 int *activity_ids = NULL;
15615 char **result = NULL;
15616 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15617 EMSTORAGE_START_READ_TRANSACTION(transaction);
15619 memset(sql_query_string, 0x00, sizeof(sql_query_string));
15621 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);
15623 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
15626 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15627 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15628 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15632 EM_DEBUG_LOG(" Activity COUNT : %d ... ", count);
15634 if (NULL == (activity_ids = (int *)em_malloc(sizeof(int) * count))) {
15635 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
15636 error = EMAIL_ERROR_OUT_OF_MEMORY;
15640 for (i = 0; i < count; i++) {
15641 activity_ids[i] = atoi(result[col_index]);
15643 EM_DEBUG_LOG("activity_id %d", activity_ids[i]);
15652 *activity_id_count = count;
15653 *activity_id_list = activity_ids;
15654 } else if (activity_ids != NULL) /* Prevent defect - 216566 */
15655 EM_SAFE_FREE(activity_ids);
15658 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15659 if (err_code != NULL) {
15663 EM_DEBUG_FUNC_END("ret [%d]", ret);
15667 INTERNAL_FUNC int emstorage_free_activity_id_list(int *activity_id_list, int *error_code)
15669 EM_DEBUG_FUNC_BEGIN();
15671 int error = EMAIL_ERROR_NONE;
15674 EM_DEBUG_LOG(" activity_id_list [%p]", activity_id_list);
15676 if (NULL == activity_id_list) {
15677 error = EMAIL_ERROR_INVALID_PARAM;
15680 EM_SAFE_FREE(activity_id_list);
15688 if (NULL != error_code) {
15689 *error_code = error;
15692 EM_DEBUG_FUNC_END("ret [%d]", ret);
15697 * emstorage_delete_local_activity - Deletes the Local acitivity Generated based on activity_type
15698 * or based on server mail id
15701 INTERNAL_FUNC int emstorage_delete_local_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15703 EM_DEBUG_FUNC_BEGIN();
15706 EM_DEBUG_LOG(" local_activity[%p] ", local_activity);
15708 if (!local_activity) {
15709 EM_DEBUG_EXCEPTION(" local_activity[%p] ", local_activity);
15710 if (err_code != NULL)
15711 *err_code = EMAIL_ERROR_INVALID_PARAM;
15715 int rc = -1, ret = false; /* Prevent_FIX */
15716 int err = EMAIL_ERROR_NONE;
15718 int query_where = 0;
15719 char sql_query_string[8192] = { 0x00, };
15720 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15721 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15723 memset(sql_query_string, 0x00, sizeof(sql_query_string));
15725 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_local_activity_tbl ");
15727 EM_DEBUG_LOG_SEC(">>> Query [ %s ] ", sql_query_string);
15729 if (local_activity->account_id) {
15730 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15731 " WHERE account_id = %d ", local_activity->account_id);
15736 if (local_activity->server_mailid) {
15737 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15738 " %s %s server_mailid = '%s' ", query_where ? "" : "WHERE", query_and ? "AND" : "", local_activity->server_mailid);
15744 if (local_activity->mail_id) {
15745 EM_DEBUG_LOG(">>>> MAIL ID [ %d ] , ACTIVITY TYPE [%d ]", local_activity->mail_id, local_activity->activity_type);
15747 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15748 " %s %s mail_id = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "", local_activity->mail_id);
15755 if (local_activity->activity_type > 0) {
15756 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15757 " %s %s activity_type = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "" , local_activity->activity_type);
15760 if (local_activity->activity_id > 0) {
15761 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15762 " %s %s activity_id = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "" , local_activity->activity_id);
15766 EM_DEBUG_LOG_SEC(">>>>> Query [ %s ] ", sql_query_string);
15767 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15768 if (error != EMAIL_ERROR_NONE) {
15769 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
15773 rc = sqlite3_changes(local_db_handle);
15775 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
15776 err = EMAIL_ERROR_MAILBOX_NOT_FOUND;
15782 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15784 if (hStmt != NULL) {
15785 rc = sqlite3_finalize(hStmt);
15786 if (rc != SQLITE_OK) {
15787 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15788 error = EMAIL_ERROR_DB_FAILURE;
15792 if (err_code != NULL)
15794 EM_DEBUG_FUNC_END("ret [%d]", ret);
15799 * emstorage_free_local_activity - Free the Local Activity data
15801 INTERNAL_FUNC int emstorage_free_local_activity(emstorage_activity_tbl_t **local_activity_list, int count, int *err_code)
15803 EM_DEBUG_FUNC_BEGIN();
15805 EM_DEBUG_LOG(" local_activity_list[%p], count[%d], err_code[%p]", local_activity_list, count, err_code);
15808 int error = EMAIL_ERROR_INVALID_PARAM;
15811 if (!local_activity_list || !*local_activity_list) {
15812 EM_DEBUG_EXCEPTION(" local_activity_list[%p], count[%d]", local_activity_list, count);
15814 error = EMAIL_ERROR_INVALID_PARAM;
15818 emstorage_activity_tbl_t* p = *local_activity_list;
15821 for (; i < count; i++) {
15822 EM_SAFE_FREE(p[i].dest_mbox);
15823 EM_SAFE_FREE(p[i].src_mbox);
15824 EM_SAFE_FREE(p[i].server_mailid);
15828 *local_activity_list = NULL;
15836 if (err_code != NULL)
15839 EM_DEBUG_FUNC_END("ret [%d]", ret);
15843 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
15846 static int _get_key_value_string_for_list_filter_rule(email_list_filter_rule_t *input_list_filter_rule, char **output_key_value_string)
15848 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_key_value_string [%p]", input_list_filter_rule, output_key_value_string);
15850 int ret = EMAIL_ERROR_NONE;
15851 char key_value_string[QUERY_SIZE] = { 0, };
15852 char *temp_key_value_1 = NULL;
15853 char *temp_key_value_2 = NULL;
15855 if (input_list_filter_rule == NULL || output_key_value_string == NULL) {
15856 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15857 return EMAIL_ERROR_INVALID_PARAM;
15860 switch (input_list_filter_rule->target_attribute) {
15861 case EMAIL_MAIL_ATTRIBUTE_MAIL_ID:
15862 case EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID:
15863 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID:
15864 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE:
15865 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_STATUS:
15866 case EMAIL_MAIL_ATTRIBUTE_REFERENCE_MAIL_ID:
15867 case EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS:
15868 case EMAIL_MAIL_ATTRIBUTE_MAIL_SIZE:
15869 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN:
15870 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML:
15871 case EMAIL_MAIL_ATTRIBUTE_FILE_SIZE:
15872 case EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD:
15873 case EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD:
15874 case EMAIL_MAIL_ATTRIBUTE_FLAGS_FLAGGED_FIELD:
15875 case EMAIL_MAIL_ATTRIBUTE_FLAGS_ANSWERED_FIELD:
15876 case EMAIL_MAIL_ATTRIBUTE_FLAGS_RECENT_FIELD:
15877 case EMAIL_MAIL_ATTRIBUTE_FLAGS_DRAFT_FIELD:
15878 case EMAIL_MAIL_ATTRIBUTE_FLAGS_FORWARDED_FIELD:
15879 case EMAIL_MAIL_ATTRIBUTE_DRM_STATUS:
15880 case EMAIL_MAIL_ATTRIBUTE_PRIORITY:
15881 case EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS:
15882 case EMAIL_MAIL_ATTRIBUTE_LOCK_STATUS:
15883 case EMAIL_MAIL_ATTRIBUTE_REPORT_STATUS:
15884 case EMAIL_MAIL_ATTRIBUTE_ATTACHMENT_COUNT:
15885 case EMAIL_MAIL_ATTRIBUTE_INLINE_CONTENT_COUNT:
15886 case EMAIL_MAIL_ATTRIBUTE_THREAD_ID:
15887 case EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT:
15888 case EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS:
15889 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_CLASS:
15890 case EMAIL_MAIL_ATTRIBUTE_DIGEST_TYPE:
15891 case EMAIL_MAIL_ATTRIBUTE_SMIME_TYPE:
15892 case EMAIL_MAIL_ATTRIBUTE_REMAINING_RESEND_TIMES:
15893 case EMAIL_MAIL_ATTRIBUTE_TAG_ID:
15894 case EMAIL_MAIL_ATTRIBUTE_EAS_DATA_LENGTH_TYPE:
15895 SNPRINTF(key_value_string, QUERY_SIZE, "%d", input_list_filter_rule->key_value.integer_type_value);
15898 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
15899 case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
15900 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
15901 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
15902 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
15903 case EMAIL_MAIL_ATTRIBUTE_FROM:
15904 case EMAIL_MAIL_ATTRIBUTE_TO:
15905 case EMAIL_MAIL_ATTRIBUTE_CC:
15906 case EMAIL_MAIL_ATTRIBUTE_BCC:
15907 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
15908 if (input_list_filter_rule->key_value.string_type_value == NULL) {
15909 EM_DEBUG_EXCEPTION("Invalid string_type_value [%p]", input_list_filter_rule->key_value.string_type_value);
15910 ret = EMAIL_ERROR_INVALID_PARAM;
15914 temp_key_value_1 = input_list_filter_rule->key_value.string_type_value;
15916 temp_key_value_2 = em_replace_all_string(temp_key_value_1, "_", "\\_");
15917 temp_key_value_1 = em_replace_all_string(temp_key_value_2, "%", "\\%");
15919 if (input_list_filter_rule->rule_type == EMAIL_LIST_FILTER_RULE_INCLUDE)
15920 SNPRINTF(key_value_string, QUERY_SIZE, "\'%%%s%%\'", temp_key_value_1);
15922 SNPRINTF(key_value_string, QUERY_SIZE, "\'%s\'", temp_key_value_1);
15925 case EMAIL_MAIL_ATTRIBUTE_DATE_TIME:
15926 case EMAIL_MAIL_ATTRIBUTE_SCHEDULED_SENDING_TIME:
15927 case EMAIL_MAIL_ATTRIBUTE_REPLIED_TIME:
15928 case EMAIL_MAIL_ATTRIBUTE_FORWARDED_TIME:
15929 SNPRINTF(key_value_string, QUERY_SIZE, "%d", (int)input_list_filter_rule->key_value.datetime_type_value);
15933 ret = EMAIL_ERROR_INVALID_PARAM;
15934 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
15938 if (ret == EMAIL_ERROR_NONE && EM_SAFE_STRLEN(key_value_string) > 0) {
15939 *output_key_value_string = strdup(key_value_string);
15944 EM_SAFE_FREE(temp_key_value_1);
15945 EM_SAFE_FREE(temp_key_value_2);
15947 EM_DEBUG_FUNC_END("ret [%d]", ret);
15951 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15952 static int _get_cert_password_file_name(int index, char *cert_password_file_name)
15954 EM_DEBUG_FUNC_BEGIN("index : [%d]", index);
15956 if (index <= 0 || !cert_password_file_name) {
15957 EM_DEBUG_EXCEPTION("Invalid parameter");
15958 return EMAIL_ERROR_INVALID_PARAM;
15961 sprintf(cert_password_file_name, ".email_cert_%d", index);
15963 EM_DEBUG_FUNC_END();
15964 return EMAIL_ERROR_NONE;
15968 static int _make_filter_rule_string(email_list_filter_rule_t *input_list_filter_rule, char **output_string)
15970 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
15972 int ret = EMAIL_ERROR_NONE;
15974 int length_field_name = 0;
15975 int length_value = 0;
15976 char result_rule_string[QUERY_SIZE] = { 0 , };
15977 char *mod_field_name_string = NULL;
15978 char *mod_value_string = NULL;
15979 char *temp_field_name_string = NULL;
15980 char *temp_key_value_string = NULL;
15982 if (input_list_filter_rule == NULL || output_string == NULL) {
15983 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15984 return EMAIL_ERROR_INVALID_PARAM;
15987 temp_field_name_string = emcore_get_mail_field_name_by_attribute_type(input_list_filter_rule->target_attribute);
15989 if (temp_field_name_string == NULL) {
15990 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
15991 return EMAIL_ERROR_INVALID_PARAM;
15994 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) {
15995 EM_DEBUG_EXCEPTION("_get_key_value_string_for_list_filter_rule failed");
15996 return EMAIL_ERROR_INVALID_PARAM;
15999 length_field_name = EM_SAFE_STRLEN(temp_field_name_string);
16000 length_value = EM_SAFE_STRLEN(temp_key_value_string);
16002 switch (input_list_filter_rule->target_attribute) {
16003 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
16004 case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
16005 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
16006 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
16007 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
16008 case EMAIL_MAIL_ATTRIBUTE_FROM:
16009 case EMAIL_MAIL_ATTRIBUTE_TO:
16010 case EMAIL_MAIL_ATTRIBUTE_CC:
16011 case EMAIL_MAIL_ATTRIBUTE_BCC:
16012 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
16020 if (is_alpha == 1 && input_list_filter_rule->case_sensitivity == false) {
16021 length_field_name += strlen("UPPER() ");
16022 length_value += strlen("UPPER() ");
16023 mod_field_name_string = em_malloc(sizeof(char) * length_field_name);
16024 if (mod_field_name_string == NULL) {
16025 EM_DEBUG_EXCEPTION("em_mallocfailed");
16026 EM_SAFE_FREE(temp_field_name_string);
16027 ret = EMAIL_ERROR_OUT_OF_MEMORY;
16031 mod_value_string = em_malloc(sizeof(char) * length_value);
16032 if (mod_value_string == NULL) {
16033 EM_DEBUG_EXCEPTION("em_mallocfailed");
16034 EM_SAFE_FREE(temp_field_name_string);
16035 ret = EMAIL_ERROR_OUT_OF_MEMORY;
16039 SNPRINTF(mod_field_name_string, length_field_name, "UPPER(%s)", temp_field_name_string);
16040 SNPRINTF(mod_value_string, length_value, "UPPER(%s)", temp_key_value_string);
16041 EM_SAFE_FREE(temp_key_value_string);
16043 mod_field_name_string = strdup(temp_field_name_string);
16044 mod_value_string = temp_key_value_string;
16047 switch (input_list_filter_rule->rule_type) {
16048 case EMAIL_LIST_FILTER_RULE_EQUAL:
16049 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = %s ", mod_field_name_string, mod_value_string);
16051 case EMAIL_LIST_FILTER_RULE_NOT_EQUAL:
16052 SNPRINTF(result_rule_string, QUERY_SIZE, "%s != %s ", mod_field_name_string, mod_value_string);
16054 case EMAIL_LIST_FILTER_RULE_LESS_THAN:
16055 SNPRINTF(result_rule_string, QUERY_SIZE, "%s < %s ", mod_field_name_string, mod_value_string);
16057 case EMAIL_LIST_FILTER_RULE_GREATER_THAN:
16058 SNPRINTF(result_rule_string, QUERY_SIZE, "%s > %s ", mod_field_name_string, mod_value_string);
16060 case EMAIL_LIST_FILTER_RULE_LESS_THAN_OR_EQUAL:
16061 SNPRINTF(result_rule_string, QUERY_SIZE, "%s <= %s ", mod_field_name_string, mod_value_string);
16063 case EMAIL_LIST_FILTER_RULE_GREATER_THAN_OR_EQUAL:
16064 SNPRINTF(result_rule_string, QUERY_SIZE, "%s >= %s ", mod_field_name_string, mod_value_string);
16066 case EMAIL_LIST_FILTER_RULE_INCLUDE:
16067 SNPRINTF(result_rule_string, QUERY_SIZE, "%s LIKE %s ", mod_field_name_string, mod_value_string);
16069 case EMAIL_LIST_FILTER_RULE_IN:
16070 SNPRINTF(result_rule_string, QUERY_SIZE, "%s IN (%s) ", mod_field_name_string, mod_value_string);
16072 case EMAIL_LIST_FILTER_RULE_NOT_IN:
16073 SNPRINTF(result_rule_string, QUERY_SIZE, "%s NOT IN (%s) ", mod_field_name_string, mod_value_string);
16076 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16077 ret = EMAIL_ERROR_INVALID_PARAM;
16081 *output_string = strdup(result_rule_string);
16084 EM_SAFE_FREE(mod_field_name_string);
16085 EM_SAFE_FREE(mod_value_string);
16087 EM_DEBUG_FUNC_END("ret [%d]", ret);
16091 static int _make_filter_attach_rule_string(char *multi_user_name, email_list_filter_rule_attach_t *input_list_filter_rule, char **output_string)
16093 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16095 char *field_name_string = NULL;
16096 char key_value_string[QUERY_SIZE] = {0,};
16097 char result_rule_string[QUERY_SIZE] = {0,};
16100 int query_size = 0;
16103 int error = EMAIL_ERROR_NONE;
16104 char **result = NULL;
16105 char sql_query_string[QUERY_SIZE] = {0,};
16106 char *sql_query_string2 = NULL;
16107 sqlite3 *local_db_handle = NULL;
16108 int *mail_ids = NULL;
16110 if (input_list_filter_rule == NULL || output_string == NULL) {
16111 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16112 return EMAIL_ERROR_INVALID_PARAM;
16115 field_name_string = EM_SAFE_STRDUP("attachment_name");
16116 SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
16118 switch (input_list_filter_rule->rule_type) {
16120 case EMAIL_LIST_FILTER_RULE_INCLUDE:
16121 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
16124 case EMAIL_LIST_FILTER_RULE_MATCH:
16125 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
16129 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16130 error = EMAIL_ERROR_INVALID_PARAM;
16134 local_db_handle = emstorage_get_db_connection(multi_user_name);
16136 EMSTORAGE_START_READ_TRANSACTION(true);
16137 SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_attachment_tbl %s", result_rule_string);
16139 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16140 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16141 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16146 EM_DEBUG_LOG("No mail found...");
16147 error = EMAIL_ERROR_MAIL_NOT_FOUND;
16148 *output_string = strdup("mail_id IN () ");
16152 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
16154 if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
16155 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
16156 error = EMAIL_ERROR_OUT_OF_MEMORY;
16160 for (i = 0; i < count; i++) {
16161 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16162 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16165 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >>", count);
16166 sqlite3_free_table(result);
16167 EMSTORAGE_FINISH_READ_TRANSACTION(true);
16169 // sqlite3_db_release_memory(local_db_handle);
16172 query_size = (10 * count) + strlen("mail_id IN () ");
16174 sql_query_string2 = em_malloc(query_size);
16175 if (sql_query_string2 == NULL) {
16176 EM_DEBUG_EXCEPTION("em_mallocfailed");
16177 error = EMAIL_ERROR_OUT_OF_MEMORY;
16181 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN (");
16182 for (i = 0; i < count-1; i++) {
16183 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16185 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d) ", mail_ids[count-1]);
16187 *output_string = strdup(sql_query_string2);
16190 EM_SAFE_FREE(mail_ids); /* prevent */
16191 EM_SAFE_FREE(sql_query_string2);
16192 EM_SAFE_FREE(field_name_string);
16193 EM_DEBUG_FUNC_END("error [%d]", error);
16197 static int _make_filter_fts_rule_string(char *multi_user_name, email_list_filter_rule_fts_t *input_list_filter_rule, char **output_string)
16199 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16200 char *field_name_string = NULL;
16201 char key_value_string[QUERY_SIZE] = {0,};
16202 char result_rule_string[QUERY_SIZE] = {0,};
16206 int query_size = 0;
16207 int error = EMAIL_ERROR_NONE;
16208 char **result = NULL;
16209 char sql_query_string[QUERY_SIZE] = {0,};
16210 char *sql_query_string2 = NULL;
16211 sqlite3 *local_db_handle = NULL;
16212 int *mail_ids = NULL;
16214 if (input_list_filter_rule == NULL || output_string == NULL) {
16215 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16216 return EMAIL_ERROR_INVALID_PARAM;
16219 field_name_string = EM_SAFE_STRDUP("body_text");
16220 SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
16222 switch (input_list_filter_rule->rule_type) {
16224 case EMAIL_LIST_FILTER_RULE_INCLUDE:
16225 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
16228 case EMAIL_LIST_FILTER_RULE_MATCH:
16229 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
16233 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16234 error = EMAIL_ERROR_INVALID_PARAM;
16238 local_db_handle = emstorage_get_db_connection(multi_user_name);
16240 EMSTORAGE_START_READ_TRANSACTION(true);
16241 SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_text_tbl %s", result_rule_string);
16243 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16244 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16245 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16250 EM_DEBUG_LOG("No mail found...");
16251 error = EMAIL_ERROR_MAIL_NOT_FOUND;
16252 *output_string = strdup("mail_id IN () ");
16256 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
16259 if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
16260 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
16261 error = EMAIL_ERROR_OUT_OF_MEMORY;
16265 for (i = 0; i < count; i++) {
16266 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16267 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16270 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >>", count);
16271 sqlite3_free_table(result);
16272 EMSTORAGE_FINISH_READ_TRANSACTION(true);
16274 // sqlite3_db_release_memory(local_db_handle);
16277 query_size = (10 * count) + strlen("mail_id IN () ");
16278 sql_query_string2 = em_malloc(query_size);
16279 if (sql_query_string2 == NULL) {
16280 EM_DEBUG_EXCEPTION("em_mallocfailed");
16281 error = EMAIL_ERROR_OUT_OF_MEMORY;
16285 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN (");
16286 for (i = 0; i < count-1; i++) {
16287 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16289 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d) ", mail_ids[count-1]);
16291 *output_string = strdup(sql_query_string2);
16294 EM_SAFE_FREE(mail_ids); /* prevent */
16295 EM_SAFE_FREE(sql_query_string2);
16296 EM_SAFE_FREE(field_name_string);
16297 EM_DEBUG_FUNC_END("error [%d]", error);
16301 static int _make_order_rule_string(char *multi_user_name, email_list_sorting_rule_t *input_sorting_rule, char **output_string)
16303 EM_DEBUG_FUNC_BEGIN("input_sorting_rule [%p], output_string [%p]", input_sorting_rule, output_string);
16305 char result_rule_string[QUERY_SIZE] = { 0 , };
16306 int ret = EMAIL_ERROR_NONE;
16308 emstorage_account_tbl_t *account_tbl_array = NULL;
16311 char *result_str = NULL;
16312 char *tmp_str1 = NULL;
16313 char *tmp_str2 = NULL;
16314 char query_per_account[QUERY_SIZE] = { 0 , };
16316 if (input_sorting_rule->force_boolean_check) {
16317 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = 0 ", emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16319 EM_SAFE_STRCPY(result_rule_string, emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16321 switch (input_sorting_rule->sort_order) {
16322 case EMAIL_SORT_ORDER_ASCEND:
16323 EM_SAFE_STRCAT(result_rule_string, " ASC ");
16326 case EMAIL_SORT_ORDER_DESCEND:
16327 EM_SAFE_STRCAT(result_rule_string, " DESC ");
16330 case EMAIL_SORT_ORDER_NOCASE_ASCEND:
16331 EM_SAFE_STRCAT(result_rule_string, " COLLATE NOCASE ASC ");
16334 case EMAIL_SORT_ORDER_NOCASE_DESCEND:
16335 EM_SAFE_STRCAT(result_rule_string, " COLLATE NOCASE DESC ");
16338 case EMAIL_SORT_ORDER_TO_CCBCC:
16339 memset(result_rule_string, 0, QUERY_SIZE);
16340 if (input_sorting_rule->key_value.string_type_value)
16341 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16342 " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 ELSE 2 END ",
16343 input_sorting_rule->key_value.string_type_value);
16346 case EMAIL_SORT_ORDER_TO_CC_BCC:
16347 memset(result_rule_string, 0, QUERY_SIZE);
16348 if (input_sorting_rule->key_value.string_type_value)
16349 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16350 " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 WHEN full_address_cc LIKE \'%%%q%%\' THEN 2 ELSE 3 END ",
16351 input_sorting_rule->key_value.string_type_value, input_sorting_rule->key_value.string_type_value);
16354 case EMAIL_SORT_ORDER_TO_CCBCC_ALL:
16355 if (!emstorage_get_account_list(multi_user_name, &count, &account_tbl_array, true, false, NULL)) {
16356 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed");
16361 EM_DEBUG_LOG("No account exist");
16362 ret = EMAIL_ERROR_INVALID_PARAM;
16366 for (i = 0; i < count; i++) {
16367 if (i > 0 && result_str) {
16368 tmp_str2 = result_str;
16369 result_str = g_strconcat(tmp_str2, " OR ", NULL);
16370 EM_SAFE_FREE(tmp_str2);
16373 memset(query_per_account, 0, QUERY_SIZE);
16374 snprintf(query_per_account, QUERY_SIZE,
16375 "(account_id = %d AND full_address_to LIKE \'%%%s%%\')",
16376 account_tbl_array[i].account_id, account_tbl_array[i].user_email_address);
16378 tmp_str1 = result_str;
16380 result_str = g_strconcat(tmp_str1, query_per_account, NULL);
16382 result_str = g_strdup(query_per_account);
16383 EM_SAFE_FREE(tmp_str1);
16386 snprintf(result_rule_string, QUERY_SIZE,
16387 " CASE WHEN %s THEN 1 ELSE 2 END ", result_str);
16389 EM_SAFE_FREE(result_str);
16390 if (account_tbl_array)
16391 emstorage_free_account(&account_tbl_array, count, NULL);
16394 case EMAIL_SORT_ORDER_LOCALIZE_ASCEND:
16395 memset(result_rule_string, 0, QUERY_SIZE);
16396 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16397 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END ASC, %s COLLATE NOCASE ASC ",
16398 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16399 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16402 case EMAIL_SORT_ORDER_LOCALIZE_DESCEND:
16403 memset(result_rule_string, 0, QUERY_SIZE);
16404 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16405 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END DESC, %s COLLATE NOCASE DESC ",
16406 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16407 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16411 EM_DEBUG_EXCEPTION("Invalid sort_order [%d]", input_sorting_rule->sort_order);
16412 ret = EMAIL_ERROR_INVALID_PARAM;
16416 *output_string = strdup(result_rule_string);
16419 EM_DEBUG_FUNC_END("ret [%d]", ret);
16423 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)
16425 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);
16426 int ret = EMAIL_ERROR_NONE;
16428 int string_offset = 0;
16429 int query_size = 0;
16430 int new_query_size = 0;
16431 char *conditional_clause_string = NULL;
16432 char *result_string_for_a_item = NULL;
16434 if ((input_filter_count > 0 && !input_filter_list) || (input_sorting_rule_count > 0 && !input_sorting_rule_list) || output_conditional_clause == NULL) {
16435 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16436 return EMAIL_ERROR_INVALID_PARAM;
16439 conditional_clause_string = em_malloc(QUERY_SIZE);
16440 if (conditional_clause_string == NULL) {
16441 EM_DEBUG_EXCEPTION("Memory is full");
16442 return EMAIL_ERROR_OUT_OF_MEMORY;
16445 if (input_filter_count > 0) {
16446 query_size = QUERY_SIZE;
16447 g_strlcpy(conditional_clause_string, " WHERE ", QUERY_SIZE);
16449 for (i = 0; i < input_filter_count; i++) {
16450 switch (input_filter_list[i].list_filter_item_type) {
16451 case EMAIL_LIST_FILTER_ITEM_RULE:
16452 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE", i);
16453 _make_filter_rule_string(&(input_filter_list[i].list_filter_item.rule), &result_string_for_a_item);
16456 case EMAIL_LIST_FILTER_ITEM_RULE_FTS:
16457 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_FTS", i);
16458 _make_filter_fts_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_fts), &result_string_for_a_item);
16461 case EMAIL_LIST_FILTER_ITEM_RULE_ATTACH:
16462 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_ATTACH", i);
16463 _make_filter_attach_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_attach), &result_string_for_a_item);
16466 case EMAIL_LIST_FILTER_ITEM_OPERATOR:
16467 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_OPERATOR", i);
16468 switch (input_filter_list[i].list_filter_item.operator_type) {
16469 case EMAIL_LIST_FILTER_OPERATOR_AND:
16470 result_string_for_a_item = strdup("AND ");
16472 case EMAIL_LIST_FILTER_OPERATOR_OR:
16473 result_string_for_a_item = strdup("OR ");
16475 case EMAIL_LIST_FILTER_OPERATOR_LEFT_PARENTHESIS:
16476 result_string_for_a_item = strdup(" (");
16478 case EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS:
16479 result_string_for_a_item = strdup(") ");
16485 EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", input_filter_list[i].list_filter_item_type);
16486 ret = EMAIL_ERROR_INVALID_PARAM;
16490 if (result_string_for_a_item == NULL) {
16491 EM_DEBUG_EXCEPTION("result_string_for_a_item is null");
16492 ret = EMAIL_ERROR_INVALID_PARAM;
16496 if (strlen(conditional_clause_string) + EM_SAFE_STRLEN(result_string_for_a_item) >= query_size) { /* prevent 34364 */
16497 EM_DEBUG_LOG("QUERY is too long");
16498 new_query_size = EM_SAFE_STRLEN(result_string_for_a_item) + EM_SAFE_STRLEN(conditional_clause_string) + QUERY_SIZE;
16499 conditional_clause_string = realloc(conditional_clause_string, new_query_size);
16500 if (conditional_clause_string == NULL) {
16501 EM_DEBUG_EXCEPTION("realloc failed");
16502 ret = EMAIL_ERROR_OUT_OF_MEMORY;
16506 query_size = new_query_size;
16509 strcat(conditional_clause_string, result_string_for_a_item);
16510 EM_SAFE_FREE(result_string_for_a_item);
16514 if (input_sorting_rule_count > 0) {
16515 strcat(conditional_clause_string, "ORDER BY ");
16517 for (i = 0; i < input_sorting_rule_count; i++) {
16518 if ((ret = _make_order_rule_string(multi_user_name, &input_sorting_rule_list[i], &result_string_for_a_item)) != EMAIL_ERROR_NONE) {
16519 EM_DEBUG_EXCEPTION("_make_order_rule_string failed. [%d]", ret);
16523 strcat(conditional_clause_string, ", ");
16524 strcat(conditional_clause_string, result_string_for_a_item);
16525 EM_SAFE_FREE(result_string_for_a_item);
16529 if (input_start_index != -1 && input_limit_count != -1) {
16530 string_offset = strlen(conditional_clause_string);
16531 SNPRINTF_OFFSET(conditional_clause_string, string_offset, query_size, " LIMIT %d, %d", input_start_index, input_limit_count);
16534 *output_conditional_clause = strdup(conditional_clause_string);
16537 EM_SAFE_FREE(result_string_for_a_item);
16538 EM_SAFE_FREE(conditional_clause_string);
16540 EM_DEBUG_FUNC_END("ret [%d]", ret);
16544 INTERNAL_FUNC int emstorage_free_list_filter(email_list_filter_t **input_filter_list, int input_filter_count)
16546 EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d]", input_filter_list, input_filter_count);
16547 int err = EMAIL_ERROR_NONE;
16549 email_list_filter_t *temp_filter_list = NULL;
16551 EM_IF_NULL_RETURN_VALUE(input_filter_list, EMAIL_ERROR_INVALID_PARAM);
16553 for (i = 0; i < input_filter_count; i++) {
16554 temp_filter_list = (*input_filter_list) + i;
16555 if (!temp_filter_list) {
16559 if (temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE) {
16560 switch (temp_filter_list->list_filter_item.rule.target_attribute) {
16561 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
16562 case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
16563 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
16564 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
16565 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
16566 case EMAIL_MAIL_ATTRIBUTE_FROM:
16567 case EMAIL_MAIL_ATTRIBUTE_TO:
16568 case EMAIL_MAIL_ATTRIBUTE_CC:
16569 case EMAIL_MAIL_ATTRIBUTE_BCC:
16570 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN:
16571 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML:
16572 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
16573 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule.key_value.string_type_value);
16578 } 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) {
16579 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_fts.key_value.string_type_value);
16580 } 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) {
16581 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_attach.key_value.string_type_value);
16585 EM_SAFE_FREE(*input_filter_list);
16587 EM_DEBUG_FUNC_END("err [%d]", err);
16591 /* Tasks --------------------------------------------------------------------------*/
16592 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)
16594 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);
16598 int err = EMAIL_ERROR_NONE;
16600 DB_STMT hStmt = NULL;
16601 char sql_query_string[QUERY_SIZE] = {0, };
16602 sqlite3 *local_db_handle = NULL;
16603 char *sql = "SELECT max(rowid) FROM mail_task_tbl;";
16604 char **result = NULL;
16606 if (input_task_parameter == NULL || output_task_id == NULL) {
16607 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16608 return EMAIL_ERROR_INVALID_PARAM;
16611 local_db_handle = emstorage_get_db_connection(multi_user_name);
16612 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, err);
16614 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
16615 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16616 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
16618 if (NULL == result[1])
16621 task_id = atoi(result[1])+1;
16623 *output_task_id = task_id;
16625 sqlite3_free_table(result);
16628 SNPRINTF(sql_query_string, sizeof(sql_query_string),
16629 "INSERT INTO mail_task_tbl VALUES "
16631 " ? " /* task_id */
16632 " , ? " /* task_type */
16633 " , ? " /* task_status */
16634 " , ? " /* task_priority */
16635 " , ? " /* task_parameter_length */
16636 " , ? " /* task_parameter */
16637 " , ? " /* date_time */
16640 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16641 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16642 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16644 EM_DEBUG_LOG_SEC(">>>> SQL STMT [%s] ", sql_query_string);
16647 _bind_stmt_field_data_int(hStmt, i++, task_id);
16648 _bind_stmt_field_data_int(hStmt, i++, input_task_type);
16649 _bind_stmt_field_data_int(hStmt, i++, EMAIL_TASK_STATUS_WAIT);
16650 _bind_stmt_field_data_int(hStmt, i++, input_task_priority);
16651 _bind_stmt_field_data_int(hStmt, i++, input_task_parameter_length);
16652 _bind_stmt_field_data_blob(hStmt, i++, input_task_parameter, input_task_parameter_length);
16653 _bind_stmt_field_data_int(hStmt, i++, time(NULL));
16655 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16657 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {err = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
16658 ("sqlite3_step fail:%d", rc));
16659 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16660 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
16662 ret = (err == EMAIL_ERROR_NONE);
16665 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, err);
16667 if (hStmt != NULL) {
16668 rc = sqlite3_finalize(hStmt);
16669 if (rc != SQLITE_OK) {
16670 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16671 err = EMAIL_ERROR_DB_FAILURE;
16675 EM_DEBUG_FUNC_END("err [%d]", err);
16679 INTERNAL_FUNC int emstorage_delete_task(char *multi_user_name, int task_id, int transaction)
16681 EM_DEBUG_FUNC_BEGIN("task_id[%d], transaction[%d]", task_id, transaction);
16683 int err = EMAIL_ERROR_NONE;
16684 char sql_query_string[QUERY_SIZE] = {0, };
16686 sqlite3 *local_db_handle = NULL;
16689 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16690 return EMAIL_ERROR_INVALID_PARAM;
16693 local_db_handle = emstorage_get_db_connection(multi_user_name);
16695 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
16697 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_task_tbl WHERE task_id = %d", task_id);
16698 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16699 if (err != EMAIL_ERROR_NONE) {
16700 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16707 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
16709 EM_DEBUG_FUNC_END("err [%d]", err);
16713 INTERNAL_FUNC int emstorage_update_task_status(char *multi_user_name, int task_id, email_task_status_type_t task_status, int transaction)
16715 EM_DEBUG_FUNC_BEGIN("task_id[%d] task_status[%d] transaction[%d]", task_id, task_status, transaction);
16717 int err = EMAIL_ERROR_NONE;
16718 char sql_query_string[QUERY_SIZE] = {0, };
16720 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16721 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
16723 SNPRINTF(sql_query_string, sizeof(sql_query_string),
16724 "UPDATE mail_task_tbl SET"
16725 " task_status = %d"
16726 " WHERE task_id = %d"
16729 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16730 if (err != EMAIL_ERROR_NONE) {
16731 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16738 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
16740 EM_DEBUG_FUNC_END("err [%d]", err);
16744 INTERNAL_FUNC int emstorage_query_task(char *multi_user_name,
16745 const char *input_conditional_clause,
16746 const char *input_ordering_clause,
16747 email_task_t **output_task_list,
16748 int *output_task_count)
16750 EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_ordering_clause [%p], "
16751 "output_task_list[%p], output_task_count[%d]",
16752 input_conditional_clause, input_ordering_clause, output_task_list, output_task_count);
16753 int i = 0, count = 0, rc = -1;
16755 int field_index = 0;
16756 int err = EMAIL_ERROR_NONE;
16757 email_task_t *task_item_from_tbl = NULL;
16758 char sql_query_string[QUERY_SIZE] = {0, };
16759 char *field_list = "task_id, task_type, task_status, task_priority, task_parameter_length, task_parameter ";
16761 sqlite3 *local_db_handle = NULL;
16762 DB_STMT hStmt = NULL;
16764 EM_IF_NULL_RETURN_VALUE(input_conditional_clause, false);
16765 EM_IF_NULL_RETURN_VALUE(output_task_count, false);
16767 local_db_handle = emstorage_get_db_connection(multi_user_name);
16769 SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
16770 "SELECT COUNT(*) FROM mail_task_tbl %s", input_conditional_clause);
16771 EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
16773 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
16774 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16775 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16777 count = atoi(result[1]);
16778 sqlite3_free_table(result);
16780 EM_DEBUG_LOG("count = %d", rc);
16783 EM_DEBUG_EXCEPTION("no task found...");
16784 err = EMAIL_ERROR_TASK_NOT_FOUND;
16788 SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
16789 "SELECT %s FROM mail_task_tbl %s %s", field_list, input_conditional_clause, input_ordering_clause);
16790 EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
16792 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16794 EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
16795 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16796 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16798 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16799 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16800 ("sqlite3_step fail:%d", rc));
16802 if (rc == SQLITE_DONE) {
16803 EM_DEBUG_EXCEPTION("no task found...");
16804 err = EMAIL_ERROR_TASK_NOT_FOUND;
16809 if (!(task_item_from_tbl = (email_task_t*)em_malloc(sizeof(email_task_t) * count))) {
16810 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
16811 err = EMAIL_ERROR_OUT_OF_MEMORY;
16815 for (i = 0; i < count; i++) {
16816 /* get recordset */
16819 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_id), field_index++);
16820 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_type), field_index++);
16821 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_status), field_index++);
16822 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_priority), field_index++);
16823 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_parameter_length), field_index++);
16824 _get_stmt_field_data_blob(hStmt, (void**)&(task_item_from_tbl[i].task_parameter), field_index++);
16826 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16827 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i, rc);
16828 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16829 ("sqlite3_step fail:%d", rc));
16834 if (err == EMAIL_ERROR_NONE) {
16835 if (output_task_list)
16836 *output_task_list = task_item_from_tbl;
16837 *output_task_count = count;
16839 if (task_item_from_tbl) {
16840 for (i = 0; i < count; i++)
16841 EM_SAFE_FREE(task_item_from_tbl[i].task_parameter);
16843 free(task_item_from_tbl);
16847 if (hStmt != NULL) {
16848 rc = sqlite3_finalize(hStmt);
16850 if (rc != SQLITE_OK) {
16851 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16852 err = EMAIL_ERROR_DB_FAILURE;
16856 EM_DEBUG_FUNC_END("err [%d]", err);
16860 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)
16862 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);
16863 int err = EMAIL_ERROR_NONE;
16865 if (message_id == NULL) {
16866 EM_DEBUG_EXCEPTION("Invalid parameter");
16867 err = EMAIL_ERROR_INVALID_PARAM;
16873 int temp_mail_id = 0;
16874 int where_pararaph_length = 0;
16875 char *where_pararaph = NULL;
16876 char sql_query_string[QUERY_SIZE] = {0, };
16877 char **result = NULL;
16878 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16880 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id from mail_tbl ");
16882 where_pararaph_length = EM_SAFE_STRLEN(message_id) + 100;
16883 where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
16884 if (where_pararaph == NULL) {
16885 EM_DEBUG_EXCEPTION("em_mallocfailed");
16886 err = EMAIL_ERROR_OUT_OF_MEMORY;
16890 if (account_id != ALL_ACCOUNT)
16891 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);
16893 sqlite3_snprintf(where_pararaph_length, where_pararaph, "WHERE mailbox_type = %d AND message_id like '%q'", input_mailbox_type, message_id);
16895 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
16896 strcat(sql_query_string, where_pararaph);
16898 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
16900 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16901 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16902 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16905 EM_DEBUG_LOG("Count of mails [%d]", count);
16908 _get_table_field_data_int(result, &temp_mail_id, 1);
16909 EM_DEBUG_LOG("Searched mail_id [%d]", temp_mail_id);
16911 memset(sql_query_string, 0x00, QUERY_SIZE);
16912 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string, "UPDATE mail_tbl set server_mail_id = '%q'", server_uid);
16914 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
16915 strcat(sql_query_string, where_pararaph);
16917 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16918 if (err != EMAIL_ERROR_NONE) {
16919 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16924 err = EMAIL_ERROR_MAIL_NOT_FOUND;
16929 sqlite3_free_table(result);
16933 EM_SAFE_FREE(where_pararaph);
16935 if (mail_id != NULL)
16936 *mail_id = temp_mail_id;
16938 EM_DEBUG_FUNC_END("err : [%d]", err);
16941 /* Tasks --------------------------------------------------------------------------*/
16943 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
16944 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)
16946 EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
16948 if (!local_activity || !activity_id) {
16949 EM_DEBUG_EXCEPTION("local_activity[%p], activity_id[%p]", local_activity, activity_id);
16950 if (err_code != NULL)
16951 *err_code = EMAIL_ERROR_INVALID_PARAM;
16957 int error = EMAIL_ERROR_NONE;
16960 char sql_query_string[QUERY_SIZE] = {0, };
16961 DB_STMT hStmt = NULL;
16963 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16964 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
16966 memset(sql_query_string, 0x00, sizeof(sql_query_string));
16967 SNPRINTF(sql_query_string, sizeof(sql_query_string),
16968 "INSERT INTO mail_auto_download_activity_tbl VALUES "
16970 "? " /* Activity ID */
16972 ",?" /* Account ID */
16973 ",?" /* Local Mail ID */
16974 ",?" /* Server mail ID */
16975 ",?" /* Mailbox ID*/
16976 ",?" /* Multi USER NAME */
16979 char *sql = "SELECT max(rowid) FROM mail_auto_download_activity_tbl;";
16980 char **result = NULL;
16983 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
16985 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16986 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
16988 if (NULL == result[1]) rc = 1;
16989 else rc = atoi(result[1])+1;
16990 sqlite3_free_table(result);
16993 *activity_id = local_activity->activity_id = rc;
16995 EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [%d], MAIL ID [%d], SERVER MAIL ID [%lu]",
16996 local_activity->activity_id, local_activity->mail_id, local_activity->server_mail_id);
16998 if (local_activity->mailbox_id)
16999 EM_DEBUG_LOG(" MAILBOX ID [%d]", local_activity->mailbox_id);
17002 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17003 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17004 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17007 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
17008 _bind_stmt_field_data_int(hStmt, i++, local_activity->status);
17009 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
17010 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
17011 _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
17012 _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
17013 _bind_stmt_field_data_string(hStmt, i++, (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
17016 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17018 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
17019 ("sqlite3_step fail:%d", rc));
17020 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17021 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
17026 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17027 if (hStmt != NULL) {
17028 rc = sqlite3_finalize(hStmt);
17030 if (rc != SQLITE_OK) {
17031 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17032 error = EMAIL_ERROR_DB_FAILURE;
17036 if (err_code != NULL)
17039 EM_DEBUG_FUNC_END("ret [%d]", ret);
17044 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)
17046 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);
17048 if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
17049 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);
17051 if (err_code != NULL)
17052 *err_code = EMAIL_ERROR_INVALID_PARAM;
17058 int error = EMAIL_ERROR_NONE;
17059 char sql_query_string[QUERY_SIZE] = {0, };
17060 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17062 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17063 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17065 if (activity_id == 0)
17066 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);
17068 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);
17070 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17071 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17072 if (error != EMAIL_ERROR_NONE) {
17073 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17077 /* validate activity existence */
17078 rc = sqlite3_changes(local_db_handle);
17080 EM_DEBUG_EXCEPTION("No matching activity found");
17081 error = EMAIL_ERROR_DATA_NOT_FOUND;
17089 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17091 if (err_code != NULL)
17094 EM_DEBUG_FUNC_END("ret [%d]", ret);
17098 INTERNAL_FUNC int emstorage_delete_all_auto_download_activity(char *multi_user_name, int account_id, int transaction, int *err_code)
17100 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17102 if (account_id < FIRST_ACCOUNT_ID) {
17103 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17104 if (err_code != NULL)
17105 *err_code = EMAIL_ERROR_INVALID_PARAM;
17111 int error = EMAIL_ERROR_NONE;
17112 char sql_query_string[QUERY_SIZE] = {0, };
17114 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17116 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17117 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17118 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d", account_id);
17120 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17121 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17122 if (error != EMAIL_ERROR_NONE) {
17123 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17127 rc = sqlite3_changes(local_db_handle);
17129 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17130 error = EMAIL_ERROR_DATA_NOT_FOUND;
17138 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17140 if (err_code != NULL)
17143 EM_DEBUG_FUNC_END("ret [%d]", ret);
17148 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)
17150 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17152 if (account_id < FIRST_ACCOUNT_ID || mailbox_id < 0) {
17153 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17154 if (err_code != NULL)
17155 *err_code = EMAIL_ERROR_INVALID_PARAM;
17161 int error = EMAIL_ERROR_NONE;
17162 char sql_query_string[QUERY_SIZE] = {0, };
17164 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17166 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17167 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17168 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);
17170 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17171 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17172 if (error != EMAIL_ERROR_NONE) {
17173 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17177 rc = sqlite3_changes(local_db_handle);
17179 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17180 error = EMAIL_ERROR_DATA_NOT_FOUND;
17188 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17190 if (err_code != NULL)
17193 EM_DEBUG_FUNC_END("ret [%d]", ret);
17198 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)
17200 EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
17202 if (account_id < FIRST_ACCOUNT_ID || !event_start || input_mailbox_id <= 0 || !count) {
17203 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);
17205 if (err_code != NULL)
17206 *err_code = EMAIL_ERROR_INVALID_PARAM;
17213 int error = EMAIL_ERROR_NONE;
17215 DB_STMT hStmt = NULL;
17216 email_event_auto_download *event_list = NULL;
17217 char sql_query_string[QUERY_SIZE] = {0, };
17219 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17221 EMSTORAGE_START_READ_TRANSACTION(transaction);
17223 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17224 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);
17227 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17228 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17229 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17231 *count = atoi(result[1]);
17232 sqlite3_free_table(result);
17234 EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
17237 EM_DEBUG_LOG("No matched activity found in mail_auto_download_activity_tbl");
17238 error = EMAIL_ERROR_MAIL_NOT_FOUND;
17242 EM_DEBUG_LOG("Activity Count = %d", *count);
17244 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17245 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);
17247 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17250 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17252 EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
17253 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17254 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17257 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17258 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17259 ("sqlite3_step fail:%d", rc));
17261 if (!(event_list = (email_event_auto_download *)em_malloc(sizeof(email_event_auto_download)*(*count)))) {
17262 EM_DEBUG_EXCEPTION("Malloc failed");
17264 error = EMAIL_ERROR_OUT_OF_MEMORY;
17268 for (i = 0; i < (*count); i++) {
17269 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17270 _get_stmt_field_data_int(hStmt, &(event_list[i].status), STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17271 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17272 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17273 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17274 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17276 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17277 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17278 ("sqlite3_step fail:%d", rc));
17286 *event_start = event_list;
17288 EM_SAFE_FREE(event_list);
17289 *event_start = NULL;
17293 if (hStmt != NULL) {
17294 rc = sqlite3_finalize(hStmt);
17296 if (rc != SQLITE_OK) {
17297 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17298 error = EMAIL_ERROR_DB_FAILURE;
17302 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17303 if (err_code != NULL)
17306 EM_DEBUG_FUNC_END("ret [%d]", ret);
17311 INTERNAL_FUNC int emstorage_get_auto_download_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
17313 EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
17315 if (!activity_count || !err_code) {
17316 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
17317 if (err_code != NULL)
17318 *err_code = EMAIL_ERROR_INVALID_PARAM;
17324 int error = EMAIL_ERROR_NONE;
17325 DB_STMT hStmt = NULL;
17326 char sql_query_string[QUERY_SIZE] = {0, };
17328 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17330 EMSTORAGE_START_READ_TRANSACTION(transaction);
17331 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17333 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl;");
17335 EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
17338 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17339 EM_DEBUG_LOG_DEV("before sqlite3_prepare hStmt = %p", hStmt);
17340 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17341 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17344 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17345 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17346 ("sqlite3_step fail:%d", rc));
17348 _get_stmt_field_data_int(hStmt, activity_count, 0);
17350 EM_DEBUG_LOG("counts of activities in activity table [%d]", *activity_count);
17356 if (hStmt != NULL) {
17357 rc = sqlite3_finalize(hStmt);
17359 if (rc != SQLITE_OK) {
17360 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17361 error = EMAIL_ERROR_DB_FAILURE;
17365 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17366 if (err_code != NULL)
17369 EM_DEBUG_FUNC_END("ret [%d]", ret);
17374 INTERNAL_FUNC int emstorage_get_auto_download_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
17376 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
17378 if (!account_list || !count) {
17379 EM_DEBUG_EXCEPTION("account_list[%p], count[%p]", account_list, count);
17380 if (err_code != NULL)
17381 *err_code = EMAIL_ERROR_INVALID_PARAM;
17386 int error = EMAIL_ERROR_NONE;
17387 char *sql = "SELECT count(distinct account_id) FROM mail_auto_download_activity_tbl";
17389 int i = 0, rc = -1;
17390 int *result_account_list = NULL;
17391 DB_STMT hStmt = NULL;
17392 char sql_query_string[QUERY_SIZE] = {0, };
17393 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17395 EMSTORAGE_START_READ_TRANSACTION(transaction);
17397 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
17398 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17399 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
17401 *count = atoi(result[1]);
17402 sqlite3_free_table(result);
17405 EM_DEBUG_EXCEPTION("no account found...");
17406 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
17411 EM_DEBUG_LOG("Account count [%d]", *count);
17413 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17415 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_auto_download_activity_tbl");
17417 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17420 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17422 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
17423 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17424 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17427 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17428 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17429 ("sqlite3_step fail:%d", rc));
17431 if (NULL == (result_account_list = (int *)em_malloc(sizeof(int)*(*count)))) {
17432 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
17433 error = EMAIL_ERROR_OUT_OF_MEMORY;
17437 for (i = 0; i < (*count); i++) {
17438 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
17440 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17441 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17442 ("sqlite3_step fail:%d", rc));
17443 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
17451 *account_list = result_account_list;
17453 EM_SAFE_FREE(result_account_list);
17455 if (hStmt != NULL) {
17456 rc = sqlite3_finalize(hStmt);
17458 if (rc != SQLITE_OK) {
17459 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17460 error = EMAIL_ERROR_DB_FAILURE;
17464 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17465 if (err_code != NULL)
17467 EM_DEBUG_FUNC_END("ret [%d]", ret);
17472 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)
17474 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
17476 if (account_id < FIRST_ACCOUNT_ID || !mailbox_list || !count) {
17477 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p]", account_id, mailbox_list, count);
17478 if (err_code != NULL)
17479 *err_code = EMAIL_ERROR_INVALID_PARAM;
17484 int error = EMAIL_ERROR_NONE;
17486 int i = 0, rc = -1;
17487 int *mbox_list = NULL;
17488 DB_STMT hStmt = NULL;
17489 char sql_query_string[QUERY_SIZE] = {0, };
17491 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17493 EMSTORAGE_START_READ_TRANSACTION(transaction);
17495 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17496 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);
17499 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17500 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17501 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17503 *count = atoi(result[1]);
17504 sqlite3_free_table(result);
17507 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
17508 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
17512 EM_DEBUG_LOG("Mailbox count = %d", *count);
17514 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17516 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);
17518 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
17521 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17524 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17525 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17528 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17529 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17530 ("sqlite3_step fail:%d", rc));
17532 mbox_list = (int *)em_malloc(sizeof(int)*(*count)); /* prevent */
17533 if (mbox_list == NULL) {
17534 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
17535 error = EMAIL_ERROR_OUT_OF_MEMORY;
17539 for (i = 0; i < (*count); i++) {
17540 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
17541 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17543 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17544 ("sqlite3_step fail:%d", rc));
17545 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
17553 *mailbox_list = mbox_list;
17555 EM_SAFE_FREE(mbox_list);
17557 if (hStmt != NULL) {
17558 rc = sqlite3_finalize(hStmt);
17560 if (rc != SQLITE_OK) {
17561 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17562 error = EMAIL_ERROR_DB_FAILURE;
17566 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17567 if (err_code != NULL)
17569 EM_DEBUG_FUNC_END("ret [%d]", ret);
17574 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)
17576 EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
17578 if (account_id < FIRST_ACCOUNT_ID || !activity_count || !err_code) {
17579 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
17580 if (err_code != NULL)
17581 *err_code = EMAIL_ERROR_INVALID_PARAM;
17587 int error = EMAIL_ERROR_NONE;
17588 char sql_query_string[QUERY_SIZE] = {0, };
17589 DB_STMT hStmt = NULL;
17591 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17593 EMSTORAGE_START_READ_TRANSACTION(transaction);
17594 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17595 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);
17597 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
17599 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17600 EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
17601 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17602 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17604 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17605 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17606 ("sqlite3_step fail:%d", rc));
17608 _get_stmt_field_data_int(hStmt, activity_count, 0);
17610 EM_DEBUG_LOG("count of activities in activity table [%d]", *activity_count);
17616 if (hStmt != NULL) {
17617 rc = sqlite3_finalize(hStmt);
17619 if (rc != SQLITE_OK) {
17620 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17621 error = EMAIL_ERROR_DB_FAILURE;
17625 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17627 if (err_code != NULL)
17630 EM_DEBUG_FUNC_END("ret [%d]", ret);
17635 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)
17637 EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mailbox_id[%d]", old_server_uid, new_server_uid, mailbox_id);
17639 int rc = -1, ret = false;
17640 int error = EMAIL_ERROR_NONE;
17641 char sql_query_string[QUERY_SIZE] = {0, };
17642 int transaction = true;
17644 if (!old_server_uid || !new_server_uid || (!mailbox_name && mailbox_id < 0)) {
17645 EM_DEBUG_EXCEPTION("Invalid parameters");
17646 error = EMAIL_ERROR_INVALID_PARAM;
17650 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17652 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17653 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17655 if (mailbox_id > 0)
17656 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17657 "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);
17658 else if (mailbox_name)
17659 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17660 "UPDATE mail_auto_download_activity_tbl SET server_mail_id = %s WHERE server_mail_id = %s ", new_server_uid, old_server_uid);
17662 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17663 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17664 if (error != EMAIL_ERROR_NONE) {
17665 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17669 rc = sqlite3_changes(local_db_handle);
17671 EM_DEBUG_LOG("No matching found in mail_auto_download_activity_tbl");
17678 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17680 if (err_code != NULL)
17683 EM_DEBUG_FUNC_END("ret [%d]", ret);
17689 #ifdef __FEATURE_UPDATE_DB_TABLE_SCHEMA__
17694 } email_column_info_t;
17696 static int get_column_information_from_table_callback(void *arg1, int argc, char **argv, char **input_column_name)
17698 EM_DEBUG_FUNC_BEGIN("arg1[%p] argc[%d] argv[%p] column_name[%p]", arg1, argc, argv, input_column_name);
17702 char *column_name = NULL;
17703 char *column_type = NULL;
17704 GList *new_list = *((GList**)arg1);
17705 email_column_info_t *column_info_item = NULL;
17707 for (i = 0; i < argc; ++i) {
17708 /* EM_DEBUG_LOG("%s = %s", input_column_name[i], argv[i]); */
17709 if (EM_SAFE_STRCMP(input_column_name[i], "name") == 0) {
17711 EM_SAFE_FREE(column_name);
17713 column_name = EM_SAFE_STRDUP(argv[i]);
17715 } else if (EM_SAFE_STRCMP(input_column_name[i], "type") == 0) {
17717 EM_SAFE_FREE(column_type);
17719 column_type = EM_SAFE_STRDUP(argv[i]);
17724 EM_DEBUG_LOG("column_name[%s] column_type[%s]", column_name, column_type);
17725 column_info_item = em_malloc(sizeof(email_column_info_t));
17726 if (column_info_item == NULL) {
17727 EM_DEBUG_EXCEPTION("em_mallocfailed");
17731 column_info_item->column_name = EM_SAFE_STRDUP(column_name);
17732 column_info_item->column_type = EM_SAFE_STRDUP(column_type);
17733 new_list = g_list_append(new_list, (gpointer)column_info_item);
17734 *((GList**)arg1) = new_list;
17739 EM_SAFE_FREE(column_name);
17740 EM_SAFE_FREE(column_type);
17742 EM_DEBUG_FUNC_END();
17746 static int emstorage_get_column_information_from_table(char *multi_user_name, const char *input_table_name, GList **output_column_info)
17748 EM_DEBUG_FUNC_BEGIN("input_table_name[%p] output_column_info[%p]", input_table_name, output_column_info);
17749 int err = EMAIL_ERROR_NONE;
17750 int result_from_sqlite = 0;
17751 char *error_message_from_sqlite = NULL;
17752 char sql_query_string[QUERY_SIZE] = {0, };
17753 GList *new_list = NULL;
17754 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17756 SNPRINTF(sql_query_string, QUERY_SIZE, "pragma table_info(%s);", input_table_name);
17758 result_from_sqlite = sqlite3_exec(local_db_handle, sql_query_string, get_column_information_from_table_callback, &new_list, &error_message_from_sqlite);
17760 if (result_from_sqlite != SQLITE_OK)
17761 EM_DEBUG_EXCEPTION("sqlite3_exec returns [%d]", result_from_sqlite);
17763 EM_DEBUG_LOG("new_list[%p] output_column_info[%p]", new_list, output_column_info);
17765 if (new_list && output_column_info) {
17766 EM_DEBUG_LOG("g_list_length[%d]", g_list_length(new_list));
17767 *output_column_info = new_list;
17770 EM_DEBUG_FUNC_END("err [%d]", err);
17774 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)
17776 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);
17777 int error = EMAIL_ERROR_NONE;
17779 sqlite3 *local_db_handle = NULL;
17780 char sql_query_string[QUERY_SIZE] = {0, };
17782 if (input_full_query == NULL || input_source_table_name == NULL || input_new_table_name == NULL) {
17783 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17784 error = EMAIL_ERROR_INVALID_PARAM;
17788 local_db_handle = emstorage_get_db_connection(multi_user_name);
17790 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17791 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17793 EM_DEBUG_LOG("[%s] will be replaced by [%s]", input_source_table_name, input_new_table_name);
17795 EM_SAFE_STRNCPY(sql_query_string, input_full_query[input_query_index], sizeof(sql_query_string)-1); /*prevent 21984*/
17796 reg_replace(sql_query_string, input_source_table_name, input_new_table_name);
17798 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17801 if (error == EMAIL_ERROR_NONE) {
17802 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17804 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17807 EM_DEBUG_FUNC_END("error [%d]", error);
17811 static int emstorage_add_column(char *multi_user_name, char *input_table_name, email_column_info_t *input_new_column)
17813 EM_DEBUG_FUNC_BEGIN("input_table_name[%p] input_new_column[%p]", input_table_name, input_new_column);
17814 int error = EMAIL_ERROR_NONE;
17816 sqlite3 *local_db_handle = NULL;
17817 char sql_query_string[QUERY_SIZE] = {0, };
17819 if (input_table_name == NULL || input_new_column == NULL) {
17820 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17821 error = EMAIL_ERROR_INVALID_PARAM;
17825 local_db_handle = emstorage_get_db_connection(multi_user_name);
17827 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17828 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17829 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);
17830 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17833 if (error == EMAIL_ERROR_NONE) {
17834 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17836 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17839 EM_DEBUG_FUNC_END("error [%d]", error);
17843 static int emstorage_drop_table(char *multi_user_name, char *input_table_name)
17845 EM_DEBUG_FUNC_BEGIN("input_table_name[%p]", input_table_name);
17846 int error = EMAIL_ERROR_NONE;
17848 sqlite3 *local_db_handle = NULL;
17849 char sql_query_string[QUERY_SIZE] = {0, };
17851 if (input_table_name == NULL) {
17852 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17853 error = EMAIL_ERROR_INVALID_PARAM;
17857 local_db_handle = emstorage_get_db_connection(multi_user_name);
17859 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17860 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17861 SNPRINTF(sql_query_string, QUERY_SIZE, "DROP TABLE %s;", input_table_name);
17862 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17865 if (error == EMAIL_ERROR_NONE) {
17866 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17868 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17871 EM_DEBUG_FUNC_END("error [%d]", error);
17875 gint glist_compare_column_name(gconstpointer old_column_info, gconstpointer new_column_info)
17877 EM_DEBUG_FUNC_BEGIN("old_column_info[%p] new_column_info[%p]", old_column_info, new_column_info);
17878 email_column_info_t *left_one = (email_column_info_t*)old_column_info;
17879 email_column_info_t *right_one = (email_column_info_t*)new_column_info;
17881 if (old_column_info == NULL || new_column_info == NULL)
17884 return EM_SAFE_STRCMP((char*)left_one->column_name, (char*)right_one->column_name);
17887 INTERNAL_FUNC int emstorage_update_db_table_schema(char *multi_user_name)
17889 EM_DEBUG_FUNC_BEGIN();
17892 int error = EMAIL_ERROR_NONE;
17894 email_column_info_t *new_column_info = NULL;
17895 email_column_info_t *p_column_info = NULL;
17896 char **create_table_query = NULL;
17897 GList *found_data = NULL;
17898 GList *column_list_of_old_table = NULL;
17899 GList *column_list_of_new_table = NULL;
17900 char table_names[CREATE_TABLE_MAX][2][50] = { { "mail_account_tbl", "mail_account_tbl_new" },
17901 { "mail_box_tbl", "mail_box_tbl_new" },
17902 { "mail_read_mail_uid_tbl", "mail_read_mail_uid_tbl_new" },
17903 { "mail_rule_tbl", "mail_rule_tbl_new" },
17904 { "mail_tbl", "mail_tbl_new" },
17905 { "mail_attachment_tbl", "mail_attachment_tbl_new" },
17906 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
17907 { "mail_partial_body_activity_tbl", "mail_partial_body_activity_tbl_new" },
17911 { "mail_meeting_tbl", "mail_meeting_tbl_new" },
17912 #ifdef __FEATURE_LOCAL_ACTIVITY__
17913 { "mail_local_activity_tbl", "mail_local_activity_tbl_new" },
17917 { "mail_certificate_tbl", "mail_certificate_tbl_new" },
17918 { "mail_task_tbl", "mail_task_tbl_new" },
17919 #ifdef __FEATURE_BODY_SEARCH__
17920 { "mail_text_tbl", "mail_text_tbl_new" },
17925 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
17926 { "mail_auto_download_activity_tbl", "mail_auto_download_activity_tbl_new" }
17933 error = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
17935 if (error != EMAIL_ERROR_NONE) {
17936 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
17940 if (query_len < CREATE_TABLE_MAX) {
17941 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
17942 error = EMAIL_ERROR_SYSTEM_FAILURE;
17946 for (i = CREATE_TABLE_MAIL_ACCOUNT_TBL; i < CREATE_TABLE_MAX; i++) {
17947 EM_DEBUG_LOG("table [%s] new_table [%s]", table_names[i][0], table_names[i][1]);
17948 if (EM_SAFE_STRLEN(table_names[i][0]) && EM_SAFE_STRLEN(table_names[i][1])) {
17949 /* Check existing of _new table */
17950 emstorage_drop_table(multi_user_name, table_names[i][1]);
17951 error = emstorage_create_renamed_table(multi_user_name, create_table_query, i, table_names[i][0], table_names[i][1]);
17952 if (error != EMAIL_ERROR_NONE) {
17953 EM_DEBUG_EXCEPTION("emstorage_create_renamed_table failed [%d]", error);
17957 emstorage_get_column_information_from_table(multi_user_name, table_names[i][0], &column_list_of_old_table);
17958 emstorage_get_column_information_from_table(multi_user_name, table_names[i][1], &column_list_of_new_table);
17960 /* Compare fields and add new field */
17961 for (j = 0; j < g_list_length(column_list_of_new_table); j++) {
17962 new_column_info = (email_column_info_t*)g_list_nth_data(column_list_of_new_table, j);
17963 found_data = g_list_find_custom(column_list_of_old_table, (gconstpointer)new_column_info, glist_compare_column_name);
17964 if (found_data == NULL) {
17966 emstorage_add_column(multi_user_name, table_names[i][0], new_column_info);
17970 emstorage_drop_table(multi_user_name, table_names[i][1]);
17972 EM_DEBUG_LOG("Skipped");
17976 if (create_table_query) {
17978 for (i = 0; i < query_len; i++) {
17979 if (create_table_query[i]) {
17980 EM_SAFE_FREE(create_table_query[i]);
17983 EM_SAFE_FREE(create_table_query);
17986 found_data = g_list_first(column_list_of_old_table);
17987 while (found_data != NULL) {
17988 p_column_info = (email_column_info_t *)found_data->data;
17989 EM_SAFE_FREE(p_column_info->column_name);
17990 EM_SAFE_FREE(p_column_info->column_type);
17991 EM_SAFE_FREE(p_column_info);
17993 found_data = g_list_next(found_data);
17995 g_list_free(column_list_of_old_table);
17997 found_data = g_list_first(column_list_of_new_table);
17998 while (found_data != NULL) {
17999 p_column_info = (email_column_info_t *)found_data->data;
18000 EM_SAFE_FREE(p_column_info->column_name);
18001 EM_SAFE_FREE(p_column_info->column_type);
18002 EM_SAFE_FREE(p_column_info);
18004 found_data = g_list_next(found_data);
18006 g_list_free(column_list_of_new_table);
18008 EM_DEBUG_FUNC_END("error [%d]", error);
18011 #endif /* __FEATURE_UPDATE_DB_TABLE_SCHEMA__ */