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+1);
1146 (*buf)[sLen] = '\0';
1150 #ifdef _PRINT_STORAGE_LOG_
1152 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", *buf, index);
1154 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1162 static int _get_table_field_data_string_without_allocation(char **table, char *buf, int buffer_size, int ucs2, int index)
1164 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1165 EM_DEBUG_EXCEPTION(" table[%p], buf[%p], index[%d]", table, buf, index);
1169 char *pTemp = table[index];
1174 memset(buf, 0, buffer_size);
1175 strncpy(buf, pTemp, buffer_size - 1);
1177 #ifdef _PRINT_STORAGE_LOG_
1179 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", buf, index);
1181 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1187 static int _get_table_field_data_blob(char **table, void **buffer, int buffer_size, int index)
1189 if ((table == NULL) || (buffer == NULL) || (index < 0)) {
1190 EM_DEBUG_EXCEPTION(" table[%p], buffer[%p], buffer_size [%d], index[%d]", table, buffer, buffer_size, index);
1194 char *temp_buffer = table[index];
1196 if (temp_buffer == NULL)
1199 *buffer = malloc(buffer_size);
1200 if (*buffer == NULL) {
1201 EM_DEBUG_EXCEPTION("allocation failed.");
1204 memset(*buffer, 0, buffer_size);
1205 memcpy(*buffer, temp_buffer, buffer_size);
1207 #ifdef _PRINT_STORAGE_LOG_
1209 EM_DEBUG_LOG("_get_table_field_data_string - buffer[%s], index[%d]", buffer, index);
1211 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1217 static int _get_stmt_field_data_char(DB_STMT hStmt, char *buf, int index)
1219 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1220 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1224 if (sqlite3_column_text(hStmt, index) != NULL) {
1225 *buf = (char)sqlite3_column_int(hStmt, index);
1226 #ifdef _PRINT_STORAGE_LOG_
1227 EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1232 EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1237 static int _get_stmt_field_data_int(DB_STMT hStmt, int *buf, int index)
1239 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1240 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1244 if (sqlite3_column_text(hStmt, index) != NULL) {
1245 *buf = sqlite3_column_int(hStmt, index);
1246 #ifdef _PRINT_STORAGE_LOG_
1247 EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1252 EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1257 static int _get_stmt_field_data_time_t(DB_STMT hStmt, time_t *buf, int index)
1259 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1260 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1264 if (sqlite3_column_text(hStmt, index) != NULL) {
1265 *buf = (time_t)sqlite3_column_int(hStmt, index);
1266 #ifdef _PRINT_STORAGE_LOG_
1267 EM_DEBUG_LOG("_get_stmt_field_data_time_t [%d]", *buf);
1272 EM_DEBUG_LOG("_get_stmt_field_data_time_t fail. index [%d]", index);
1276 static int _get_stmt_field_data_string(DB_STMT hStmt, char **buf, int ucs2, int index)
1278 if (!hStmt || !buf || (index < 0)) { /*prevent 39619*/
1279 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1284 sLen = sqlite3_column_bytes(hStmt, index);
1286 #ifdef _PRINT_STORAGE_LOG_
1287 EM_DEBUG_LOG("_get_stmt_field_data_string sqlite3_column_bytes sLen[%d]", sLen);
1291 *buf = (char *) em_malloc(sLen + 1);
1293 EM_DEBUG_EXCEPTION("em_mallocfailed");
1297 strncpy(*buf, (char *)sqlite3_column_text(hStmt, index), sLen);
1301 #ifdef _PRINT_STORAGE_LOG_
1303 EM_DEBUG_LOG("buf[%s], index[%d]", *buf, index);
1305 EM_DEBUG_LOG("_get_stmt_field_data_string - No string got");
1311 static void _get_stmt_field_data_blob(DB_STMT hStmt, void **buf, int index)
1313 if (!hStmt || !buf || (index < 0)) { /*prevent 39618*/
1314 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1319 sLen = sqlite3_column_bytes(hStmt, index);
1321 #ifdef _PRINT_STORAGE_LOG_
1322 EM_DEBUG_LOG("_get_stmt_field_data_blob sqlite3_column_bytes sLen[%d]", sLen);
1326 *buf = (char *) em_malloc(sLen);
1328 EM_DEBUG_EXCEPTION("em_mallocfailed");
1332 memcpy(*buf, (void *)sqlite3_column_blob(hStmt, index), sLen);
1338 static int _bind_stmt_field_data_char(DB_STMT hStmt, int index, char value)
1340 if ((hStmt == NULL) || (index < 0)) {
1341 EM_DEBUG_EXCEPTION("index[%d]", index);
1345 int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1347 if (ret != SQLITE_OK) {
1348 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1355 static int _bind_stmt_field_data_int(DB_STMT hStmt, int index, int value)
1357 if ((hStmt == NULL) || (index < 0)) {
1358 EM_DEBUG_EXCEPTION("index[%d]", index);
1362 int ret = sqlite3_bind_int(hStmt, index+1, value);
1364 if (ret != SQLITE_OK) {
1365 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1372 static int _bind_stmt_field_data_time_t(DB_STMT hStmt, int index, time_t value)
1374 if ((hStmt == NULL) || (index < 0)) {
1375 EM_DEBUG_EXCEPTION("index[%d]", index);
1379 int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1381 if (ret != SQLITE_OK) {
1382 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1389 static int _bind_stmt_field_data_string(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1391 if ((hStmt == NULL) || (index < 0)) {
1392 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1396 #ifdef _PRINT_STORAGE_LOG_
1397 EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1402 ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1404 ret = sqlite3_bind_text(hStmt, index+1, "", -1, SQLITE_STATIC);
1406 if (ret != SQLITE_OK) {
1407 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1413 static int _bind_stmt_field_data_nstring(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1415 if ((hStmt == NULL) || (index < 0)) {
1416 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1420 #ifdef _PRINT_STORAGE_LOG_
1421 EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1425 if (value != NULL) {
1426 if (strlen(value) <= max_len)
1427 ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1429 char *buf = (char*)em_malloc(sizeof(char) * (max_len));
1431 EM_DEBUG_EXCEPTION("em_mallocfailed");
1434 snprintf(buf, max_len-1, "%s", value);
1435 ret = sqlite3_bind_text(hStmt, index+1, buf, -1, SQLITE_TRANSIENT);
1439 ret = sqlite3_bind_text(hStmt, index+1, "", -1, SQLITE_STATIC);
1441 if (ret != SQLITE_OK) {
1442 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1449 static int _bind_stmt_field_data_blob(DB_STMT hStmt, int index, void *blob, int blob_size)
1451 if ((hStmt == NULL) || (index < 0)) {
1452 EM_DEBUG_EXCEPTION("index[%d], blob_size[%d]", index, blob_size);
1456 #ifdef _PRINT_STORAGE_LOG_
1457 EM_DEBUG_LOG("hStmt = %p, index = %d, blob_size = %d, blob = [%p]", hStmt, index, blob_size, blob);
1462 ret = sqlite3_bind_blob(hStmt, index+1, blob, blob_size, SQLITE_STATIC);
1464 ret = sqlite3_bind_null(hStmt, index+1);
1466 if (ret != SQLITE_OK) {
1467 EM_DEBUG_EXCEPTION("sqlite3_bind_blob fail [%d]", ret);
1474 static int _delete_temp_file(const char *path)
1476 EM_DEBUG_FUNC_BEGIN("path[%p]", path);
1479 struct dirent *entry = NULL;
1481 char buf[1024] = {0x00, };
1483 if ((dp = opendir(path)) == NULL) {
1484 EM_DEBUG_EXCEPTION("opendir(\"%s\") failed...", path);
1488 while ((entry = readdir(dp)) != NULL) {
1489 SNPRINTF(buf, sizeof(buf), "%s/%s", path, entry->d_name);
1494 EM_DEBUG_FUNC_END();
1498 char *cpy_str(char *src)
1503 if (!(p = em_malloc((int)EM_SAFE_STRLEN(src) + 1))) {
1504 EM_DEBUG_EXCEPTION("mailoc failed...");
1507 strncpy(p, src, EM_SAFE_STRLEN(src)+1);
1513 static void _emstorage_close_once(void)
1515 EM_DEBUG_FUNC_BEGIN();
1517 EM_DEBUG_FUNC_END();
1520 INTERNAL_FUNC int emstorage_close(int *err_code)
1522 EM_DEBUG_FUNC_BEGIN();
1525 int error = EMAIL_ERROR_NONE;
1527 if (!emstorage_db_close(NULL, &error))
1529 if (--_open_counter == 0)
1530 _emstorage_close_once();
1534 if (err_code != NULL)
1537 EM_DEBUG_FUNC_END("ret [%d]", ret);
1541 static void *_emstorage_open_once(char *multi_user_name, int *err_code)
1543 EM_DEBUG_FUNC_BEGIN();
1545 int error = EMAIL_ERROR_NONE;
1547 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1548 char buf[MAX_PATH] = {0};
1549 char *prefix_path = NULL;
1551 error = emcore_get_container_path(multi_user_name, &prefix_path);
1552 if (error != EMAIL_ERROR_NONE) {
1553 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
1557 memset(buf, 0x00, sizeof(buf));
1558 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, EMAIL_PATH);
1559 mkdir(buf, DIRECTORY_PERMISSION);
1561 memset(buf, 0x00, sizeof(buf));
1562 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAIL_HOME);
1563 mkdir(buf, DIRECTORY_PERMISSION);
1565 memset(buf, 0x00, sizeof(buf));
1566 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAIL_TEMP);
1567 mkdir(buf, DIRECTORY_PERMISSION);
1569 _delete_temp_file(buf);
1570 EM_SAFE_FREE(prefix_path);
1572 mkdir(DATA_PATH, DIRECTORY_PERMISSION);
1573 mkdir(EMAIL_PATH, DIRECTORY_PERMISSION);
1574 mkdir(MAIL_HOME, DIRECTORY_PERMISSION);
1575 mkdir(MAIL_TEMP, DIRECTORY_PERMISSION);
1577 _delete_temp_file(MAIL_TEMP);
1580 if (!emstorage_create_table(multi_user_name, EMAIL_CREATE_DB_NORMAL, &error)) {
1581 EM_DEBUG_EXCEPTION(" emstorage_create_table failed - %d", error);
1588 if (err_code != NULL)
1594 /* pData : a parameter which is registered when busy handler is registerd */
1595 /* count : retry count */
1596 static int _callback_sqlite_busy_handler(void *pData, int count)
1598 if (10 - count > 0) {
1599 struct timespec time = {
1601 .tv_nsec = (count + 1) * 100 * 1000 * 1000
1603 EM_DEBUG_LOG("Busy handler called!!: PID[%d] / CNT [%d]", getpid(), count);
1604 nanosleep(&time, NULL);
1607 EM_DEBUG_EXCEPTION("Busy handler will be returned SQLITE_BUSY error PID[%d] / CNT[%d]", getpid(), count);
1612 static int _callback_collation_utf7_sort(void *data, int length_text_a, const void *text_a,
1613 int length_text_b, const void *text_b)
1615 EM_DEBUG_FUNC_BEGIN();
1617 char *converted_string_a = NULL;
1618 char *converted_string_b = NULL;
1620 EM_DEBUG_LOG_DEV("text_a : [%s]", text_a);
1621 converted_string_a = emcore_convert_mutf7_to_utf8((char *)text_a);
1622 EM_DEBUG_LOG_DEV("Converted text_a : [%s]", converted_string_a);
1624 EM_DEBUG_LOG_DEV("text_b : [%s]", text_b);
1625 converted_string_b = emcore_convert_mutf7_to_utf8((char *)text_b);
1626 EM_DEBUG_LOG_DEV("Converted text_b : [%s]", converted_string_b);
1628 if (converted_string_a && converted_string_b)
1629 result = strcmp(converted_string_a, converted_string_b);
1631 EM_SAFE_FREE(converted_string_a);
1632 EM_SAFE_FREE(converted_string_b);
1634 EM_DEBUG_FUNC_END();
1638 static int _delete_all_files_and_directories(char *db_file_path, int *err_code)
1640 EM_DEBUG_FUNC_BEGIN();
1642 int error = EMAIL_ERROR_NONE;
1645 if (!emstorage_delete_file(db_file_path, &error)) {
1646 if (error != EMAIL_ERROR_FILE_NOT_FOUND) {
1647 EM_DEBUG_EXCEPTION_SEC("remove failed - %s", EMAIL_SERVICE_DB_FILE_PATH);
1652 if (!emstorage_delete_dir((char *)MAIL_HOME, &error)) {
1653 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed");
1662 EM_DEBUG_FUNC_END();
1666 static int _recovery_from_malformed_db_file(char *db_file_path, int *err_code)
1668 EM_DEBUG_FUNC_BEGIN();
1670 int error = EMAIL_ERROR_NONE;
1673 /* Delete all files and directories */
1674 if (!_delete_all_files_and_directories(db_file_path, &error)) {
1675 EM_DEBUG_EXCEPTION("_delete_all_files_and_directories failed [%d]", error);
1679 /* Delete all accounts on MyAccount */
1681 /* Delete all managed connection to DB */
1682 emstorage_reset_db_handle_list();
1689 EM_DEBUG_FUNC_END();
1693 int _xsystem(const char *argv[])
1701 perror("fork failed");
1704 if (execvp(argv[0], (char *const *)argv) == -1) {
1705 perror("execute init db script");
1714 if (waitpid(pid, &status, 0) == -1) {
1715 perror("waitpid failed");
1719 if (WIFSIGNALED(status)) {
1724 if (!WIFEXITED(status)) {
1725 perror("should not happen");
1729 return WEXITSTATUS(status);
1732 #define SCRIPT_INIT_DB "/usr/bin/email-service_init_db.sh"
1734 INTERNAL_FUNC int emstorage_init_db(char *multi_user_name)
1736 EM_DEBUG_FUNC_BEGIN();
1737 int err = EMAIL_ERROR_NONE;
1738 char *prefix_path = NULL;
1739 char *output_file_path = NULL;
1740 char temp_file_path[MAX_PATH] = {0};
1741 gid_t email_gid = -1;
1742 uid_t email_uid = -1;
1744 struct group buf_group;
1746 struct passwd buf_passwd;
1747 char buf_gr[MAX_GRP_BUF_SIZE];
1748 char buf_pw[MAX_GRP_BUF_SIZE];
1751 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1752 err = emcore_get_container_path(multi_user_name, &prefix_path);
1753 if (err != EMAIL_ERROR_NONE) {
1754 if (err != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1755 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", err);
1760 prefix_path = strdup("");
1763 if (err == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1764 err = emcore_get_canonicalize_path((char *)EMAIL_SERVICE_DB_FILE_PATH, &output_file_path);
1765 if (err != EMAIL_ERROR_NONE) {
1766 EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", err);
1770 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
1772 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
1775 EM_DEBUG_LOG("db file path : [%s]", temp_file_path);
1777 if (!g_file_test(temp_file_path, G_FILE_TEST_EXISTS)) {
1779 const char *argv_script[] = {"/bin/sh", SCRIPT_INIT_DB, NULL};
1780 ret = _xsystem(argv_script);
1783 EM_DEBUG_EXCEPTION("_xsystem failed");
1784 err = EMAIL_ERROR_SYSTEM_FAILURE;
1787 result = getgrnam_r(GID, &buf_group, buf_gr, sizeof(buf_gr), &gr);
1788 if (result == 0 && NULL != gr)
1789 email_gid = gr->gr_gid;
1791 email_gid = GID_DEFAULT;
1794 result = getpwnam_r(UID, &buf_passwd, buf_pw, sizeof(buf_pw), &pw);
1795 if (result == 0 && NULL != pw)
1796 email_uid = pw->pw_uid;
1798 email_uid = UID_DEFAULT;
1801 chmod(EMAIL_SERVICE_DB_FILE_PATH, 0660);
1802 chmod(EMAIL_SERVICE_DB_JOURNAL_FILE_PATH, 0660);
1803 chmod(EMAIL_SQL_PATH, 0660);
1805 ret = chown(EMAIL_SERVICE_DB_FILE_PATH, email_uid, email_gid);
1807 EM_DEBUG_EXCEPTION("chown() Fail[errno=%d]", errno);
1808 ret = chown(EMAIL_SERVICE_DB_JOURNAL_FILE_PATH, email_uid, email_gid);
1810 EM_DEBUG_EXCEPTION("chown() Fail[errno=%d]", errno);
1811 ret = chown(EMAIL_SQL_PATH, email_uid, email_gid);
1813 EM_DEBUG_EXCEPTION("chown() Fail[errno=%d]", errno);
1819 EM_SAFE_FREE(prefix_path);
1820 EM_SAFE_FREE(output_file_path);
1822 EM_DEBUG_FUNC_END();
1826 INTERNAL_FUNC int em_db_open(char *db_file_path, sqlite3 **sqlite_handle, int *err_code)
1828 EM_DEBUG_FUNC_BEGIN();
1830 int error = EMAIL_ERROR_NONE;
1833 EM_DEBUG_LOG_DEV("*sqlite_handle[%p]", *sqlite_handle);
1835 if (*sqlite_handle) { /*prevent 33351*/
1836 EM_DEBUG_LOG_DEV(">>>>> DB Already Opened......");
1837 if (err_code != NULL)
1842 EM_DEBUG_LOG("DB file path : [%s]", db_file_path);
1845 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1846 if (SQLITE_OK != rc) {
1847 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1848 if (SQLITE_PERM == rc || SQLITE_CANTOPEN == rc)
1849 error = EMAIL_ERROR_PERMISSION_DENIED;
1851 error = EMAIL_ERROR_DB_FAILURE;
1853 sqlite3_close(*sqlite_handle);
1854 *sqlite_handle = NULL;
1856 if (SQLITE_CORRUPT == rc) /* SQLITE_CORRUPT : The database disk image is malformed */ {/* Recovery DB file */
1857 EM_DEBUG_LOG("The database disk image is malformed. Trying to remove and create database disk image and directories");
1858 if (!_recovery_from_malformed_db_file(db_file_path, &error)) {
1859 EM_DEBUG_EXCEPTION("_recovery_from_malformed_db_file failed [%d]", error);
1863 EM_DEBUG_LOG("Open DB again");
1864 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1865 if (SQLITE_OK != rc) {
1866 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1867 if (SQLITE_PERM == rc)
1868 error = EMAIL_ERROR_PERMISSION_DENIED;
1870 error = EMAIL_ERROR_DB_FAILURE;
1872 sqlite3_close(*sqlite_handle);
1873 *sqlite_handle = NULL;
1874 goto FINISH_OFF; /*prevent 33351*/
1880 /* register busy handler */
1881 EM_DEBUG_LOG_DEV(">>>>> Register DB Handle to busy handler: *sqlite_handle[%p]", *sqlite_handle);
1882 rc = sqlite3_busy_handler(*sqlite_handle, _callback_sqlite_busy_handler, NULL); /* Busy Handler registration, NULL is a parameter which will be passed to handler */
1883 if (SQLITE_OK != rc) {
1884 EM_DEBUG_EXCEPTION("sqlite3_busy_handler fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1885 error = EMAIL_ERROR_DB_FAILURE;
1886 sqlite3_close(*sqlite_handle);
1887 *sqlite_handle = NULL;
1891 /* Register collation callback function */
1892 rc = sqlite3_create_collation(*sqlite_handle, "CONVERTUTF8", SQLITE_UTF8, NULL, _callback_collation_utf7_sort);
1893 if (SQLITE_OK != rc) {
1894 EM_DEBUG_EXCEPTION("sqlite3_create_collation failed : [%d][%s]", rc, sqlite3_errmsg(*sqlite_handle));
1895 error = EMAIL_ERROR_DB_FAILURE;
1896 sqlite3_close(*sqlite_handle);
1897 *sqlite_handle = NULL;
1904 if (err_code != NULL)
1907 EM_DEBUG_FUNC_END("ret [%d]", ret);
1911 INTERNAL_FUNC sqlite3* emstorage_db_open(char *multi_user_name, int *err_code)
1913 EM_DEBUG_FUNC_BEGIN();
1915 sqlite3 *_db_handle = NULL;
1917 int error = EMAIL_ERROR_NONE;
1918 char *prefix_path = NULL;
1920 _db_handle = emstorage_get_db_handle(multi_user_name);
1922 if (_db_handle == NULL) {
1923 char *output_file_path = NULL;
1924 char temp_file_path[MAX_PATH] = {0};
1926 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1927 error = emcore_get_container_path(multi_user_name, &prefix_path);
1928 if (error != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION && error != EMAIL_ERROR_NONE) {
1929 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", error);
1933 prefix_path = strdup("");
1936 if (error == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1937 if ((error = emcore_get_canonicalize_path((char *)EMAIL_SERVICE_DB_FILE_PATH, &output_file_path)) != EMAIL_ERROR_NONE) {
1938 EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", error);
1942 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
1943 EM_SAFE_FREE(output_file_path);
1945 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
1948 if (!em_db_open(temp_file_path, &_db_handle, &error)) {
1949 EM_DEBUG_EXCEPTION("em_db_open failed[%d]", error);
1953 _initialize_shm_mutex(SHM_FILE_FOR_DB_LOCK, &shm_fd_for_db_lock, &mapped_for_db_lock);
1955 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
1956 _initialize_shm_mutex(SHM_FILE_FOR_MAIL_ID_LOCK, &shm_fd_for_generating_mail_id, &mapped_for_generating_mail_id);
1957 #endif /*__FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
1959 emstorage_set_db_handle(multi_user_name, _db_handle);
1961 emstorage_initialize_field_count();
1966 EM_SAFE_FREE(prefix_path);
1968 if (err_code != NULL)
1971 EM_DEBUG_FUNC_END("ret [%p]", _db_handle);
1975 INTERNAL_FUNC int emstorage_db_close(char *multi_user_name, int *err_code)
1977 EM_DEBUG_FUNC_BEGIN();
1978 #ifdef _MULTIPLE_DB_HANDLE
1979 sqlite3 *_db_handle = emstorage_get_db_handle(multi_user_name);
1982 int error = EMAIL_ERROR_NONE;
1986 ret = sqlite3_close(_db_handle);
1987 if (ret != SQLITE_OK) {
1988 EM_DEBUG_EXCEPTION(" sqlite3_close fail - %d", ret);
1989 error = EMAIL_ERROR_DB_FAILURE;
1993 #ifdef _MULTIPLE_DB_HANDLE
1994 emstorage_remove_db_handle();
2002 if (err_code != NULL)
2005 EM_DEBUG_FUNC_END("ret [%d]", ret);
2009 INTERNAL_FUNC int emstorage_open(char *multi_user_name, int *err_code)
2011 EM_DEBUG_FUNC_BEGIN();
2014 int error = EMAIL_ERROR_NONE;
2016 char *prefix_path = NULL;
2017 char buf[MAX_PATH] = {0};
2019 if (EM_SAFE_STRLEN(multi_user_name) <= 0) {
2020 SNPRINTF(buf, sizeof(buf), "%s", DB_PATH);
2022 error = emcore_get_container_path(multi_user_name, &prefix_path);
2023 if (error != EMAIL_ERROR_NONE) {
2024 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
2027 SNPRINTF(buf, sizeof(buf), "%s/%s", prefix_path, DB_PATH);
2030 if (!g_file_test(buf, G_FILE_TEST_EXISTS)) {
2031 retValue = mkdir(buf, DIRECTORY_PERMISSION);
2033 EM_DEBUG_LOG("mkdir return- %d", retValue);
2034 EM_DEBUG_LOG("emstorage_open - before sqlite3_open - pid = %d", getpid());
2037 if (emstorage_db_open(multi_user_name, &error) == NULL) {
2038 EM_DEBUG_EXCEPTION("emstorage_db_open failed[%d]", error);
2042 if (_open_counter++ == 0)
2043 _emstorage_open_once(multi_user_name, &error);
2049 EM_SAFE_FREE(prefix_path);
2051 if (err_code != NULL)
2054 EM_DEBUG_FUNC_END("ret [%d]", ret);
2058 static int emstorage_get_field_count_from_create_table_query(char *input_create_table_query, int *output_field_count)
2060 EM_DEBUG_FUNC_BEGIN("input_create_table_query[%d], output_field_count[%p]", input_create_table_query, output_field_count);
2061 int err = EMAIL_ERROR_NONE;
2062 int field_count = 0;
2065 if (input_create_table_query == NULL || output_field_count == NULL) {
2066 err = EMAIL_ERROR_INVALID_PARAM;
2070 pos = input_create_table_query;
2074 if (pos == NULL || *pos == (char)0)
2077 pos = strchr(pos, ',');
2080 *output_field_count = field_count;
2082 EM_DEBUG_LOG_DEV("field_count [%d]", field_count);
2086 EM_DEBUG_FUNC_END("err [%d]", err);
2090 INTERNAL_FUNC int emstorage_initialize_field_count()
2092 EM_DEBUG_FUNC_BEGIN();
2093 int err = EMAIL_ERROR_NONE;
2095 char **create_table_query = NULL;
2097 if (_field_count_of_table[CREATE_TABLE_MAIL_ACCOUNT_TBL] != 0) {
2098 err = EMAIL_ERROR_ALREADY_INITIALIZED;
2102 err = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2103 if (err != EMAIL_ERROR_NONE) {
2104 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", err);
2108 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]));
2109 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]));
2110 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]));
2111 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]));
2112 emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_TBL]));
2113 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]));
2114 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]));
2115 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]));
2116 #ifdef __FEATURE_LOCAL_ACTIVITY__
2117 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]));
2118 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2119 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]));
2120 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]));
2121 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]));
2124 if (create_table_query) {
2126 for (i = 0; i < query_len; i++) {
2127 if (create_table_query[i])
2128 EM_SAFE_FREE(create_table_query[i]);
2130 EM_SAFE_FREE(create_table_query);
2133 EM_DEBUG_FUNC_END("err [%d]", err);
2137 INTERNAL_FUNC int emstorage_create_table(char *multi_user_name, emstorage_create_db_type_t type, int *err_code)
2139 EM_DEBUG_FUNC_BEGIN();
2141 int error = EMAIL_ERROR_NONE;
2142 int rc = -1, ret = false;
2144 char sql_query_string[QUERY_SIZE] = {0, };
2145 char **create_table_query = NULL;
2147 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2149 error = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2150 if (error != EMAIL_ERROR_NONE) {
2151 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
2155 if (query_len < CREATE_TABLE_MAX) {
2156 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
2157 error = EMAIL_ERROR_SYSTEM_FAILURE;
2161 EM_DEBUG_LOG("local_db_handle = %p.", local_db_handle);
2164 char **result = NULL;
2166 /* 1. create mail_account_tbl */
2167 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_account_tbl';";
2168 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2169 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2170 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2172 EM_DEBUG_LOG("emstorage_create_table - result[1] = %s %c", result[1], result[1]);
2174 if (atoi(result[1]) < 1) {
2175 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2176 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2177 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2179 EM_DEBUG_LOG("CREATE TABLE mail_account_tbl");
2180 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2181 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2182 if (error != EMAIL_ERROR_NONE) {
2183 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2187 /* create mail_account_tbl unique index */
2188 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ACCOUNT_IDX]);
2189 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2190 if (error != EMAIL_ERROR_NONE) {
2191 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2195 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2196 } /* mail_account_tbl */
2197 else if (type == EMAIL_CREATE_DB_CHECK) {
2198 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], NULL, NULL, NULL), rc);
2199 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)));
2202 sqlite3_free_table(result);
2205 /* 2. create mail_box_tbl */
2206 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_box_tbl';";
2208 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2209 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2210 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2212 if (atoi(result[1]) < 1) {
2213 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2214 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2215 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2217 EM_DEBUG_LOG("CREATE TABLE mail_box_tbl");
2219 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_BOX_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2220 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2221 if (error != EMAIL_ERROR_NONE) {
2222 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2226 /* create mail_local_mailbox_tbl unique index */
2227 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_BOX_IDX]);
2228 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2229 if (error != EMAIL_ERROR_NONE) {
2230 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2234 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2235 } /* mail_box_tbl */
2236 else if (type == EMAIL_CREATE_DB_CHECK) {
2237 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_BOX_TBL], NULL, NULL, NULL);
2238 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2239 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_BOX_TBL], rc, sqlite3_errmsg(local_db_handle)));
2241 sqlite3_free_table(result);
2244 /* 3. create mail_read_mail_uid_tbl */
2245 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_read_mail_uid_tbl';";
2246 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2247 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2248 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2251 if (atoi(result[1]) < 1) {
2252 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2253 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2254 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2256 EM_DEBUG_LOG("CREATE TABLE mail_read_mail_uid_tbl");
2258 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2259 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2260 if (error != EMAIL_ERROR_NONE) {
2261 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2265 /* create mail_read_mail_uid_tbl unique index */
2266 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_IDX]);
2267 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2268 if (error != EMAIL_ERROR_NONE) {
2269 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2273 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2274 } /* mail_read_mail_uid_tbl */
2275 else if (type == EMAIL_CREATE_DB_CHECK) {
2276 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], NULL, NULL, NULL), rc);
2277 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2278 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], rc, sqlite3_errmsg(local_db_handle)));
2280 sqlite3_free_table(result);
2283 /* 4. create mail_rule_tbl */
2284 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_rule_tbl';";
2286 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2287 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2288 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2290 if (atoi(result[1]) < 1) {
2291 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2292 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2293 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2295 EM_DEBUG_LOG("CREATE TABLE mail_rule_tbl");
2297 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_RULE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2298 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2299 if (error != EMAIL_ERROR_NONE) {
2300 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2304 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2305 } /* mail_rule_tbl */
2306 else if (type == EMAIL_CREATE_DB_CHECK) {
2307 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_RULE_TBL], NULL, NULL, NULL), rc);
2308 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2309 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_RULE_TBL], rc, sqlite3_errmsg(local_db_handle)));
2311 sqlite3_free_table(result);
2314 /* 5. create mail_tbl */
2315 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_tbl';";
2316 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2317 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2318 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2320 if (atoi(result[1]) < 1) {
2321 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2322 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2323 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2324 EM_DEBUG_LOG("CREATE TABLE mail_tbl");
2326 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2327 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2328 if (error != EMAIL_ERROR_NONE) {
2329 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2333 /* create mail_tbl unique index */
2334 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_IDX]);
2335 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2336 if (error != EMAIL_ERROR_NONE) {
2337 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2341 /* create mail_tbl index for date_time */
2342 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_DATETIME_IDX]);
2343 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2344 if (error != EMAIL_ERROR_NONE) {
2345 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2349 /* create mail_tbl index for thread_item_count */
2350 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_THREAD_IDX]);
2351 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2352 if (error != EMAIL_ERROR_NONE) {
2353 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2357 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2358 /* just one time call */
2359 /* EFTSInitFTSIndex(FTS_EMAIL_IDX); */
2361 else if (type == EMAIL_CREATE_DB_CHECK) {
2362 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TBL], NULL, NULL, NULL), rc);
2363 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2364 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TBL], rc, sqlite3_errmsg(local_db_handle)));
2366 sqlite3_free_table(result);
2369 /* 6. create mail_attachment_tbl */
2370 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_attachment_tbl';";
2371 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2372 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2373 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2375 if (atoi(result[1]) < 1) {
2376 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2377 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2378 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2380 EM_DEBUG_LOG("CREATE TABLE mail_attachment_tbl");
2382 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2383 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2384 if (error != EMAIL_ERROR_NONE) {
2385 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2389 /* create mail_attachment_tbl unique index */
2390 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_IDX]);
2391 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2392 if (error != EMAIL_ERROR_NONE) {
2393 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2397 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2398 } /* mail_attachment_tbl */
2399 else if (type == EMAIL_CREATE_DB_CHECK) {
2400 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], NULL, NULL, NULL), rc);
2401 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2402 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], rc, sqlite3_errmsg(local_db_handle)));
2404 sqlite3_free_table(result);
2407 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
2409 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_partial_body_activity_tbl';";
2410 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2411 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2412 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2414 if (atoi(result[1]) < 1) {
2416 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2417 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2418 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2420 EM_DEBUG_LOG("CREATE TABLE mail_partial_body_activity_tbl");
2422 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2423 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2424 if (error != EMAIL_ERROR_NONE) {
2425 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2429 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2430 } /* mail_rule_tbl */
2431 else if (type == EMAIL_CREATE_DB_CHECK) {
2432 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2433 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2434 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2436 sqlite3_free_table(result);
2439 #endif /* __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
2441 /* create mail_meeting_tbl */
2442 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_meeting_tbl';";
2443 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2444 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2445 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2447 if (atoi(result[1]) < 1) {
2448 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2449 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2450 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2452 EM_DEBUG_LOG("CREATE TABLE mail_meeting_tbl");
2454 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_MEETING_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2455 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2456 if (error != EMAIL_ERROR_NONE) {
2457 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2461 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_MEETING_IDX]);
2462 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2463 if (error != EMAIL_ERROR_NONE) {
2464 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2468 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2469 } /* mail_contact_sync_tbl */
2470 else if (type == EMAIL_CREATE_DB_CHECK) {
2471 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], NULL, NULL, NULL), rc);
2472 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2473 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], rc, sqlite3_errmsg(local_db_handle)));
2475 sqlite3_free_table(result);
2478 #ifdef __FEATURE_LOCAL_ACTIVITY__
2480 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_local_activity_tbl';";
2481 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2482 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2483 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2485 if (atoi(result[1]) < 1) {
2487 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2488 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2489 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2491 EM_DEBUG_LOG(" CREATE TABLE mail_local_activity_tbl");
2493 SNPRINTF(sql_query_string, sizeof(sql_query_string), create_table_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL]);
2494 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2495 if (error != EMAIL_ERROR_NONE) {
2496 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2500 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2501 } /* mail_rule_tbl */
2502 else if (type == EMAIL_CREATE_DB_CHECK) {
2503 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL);
2504 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2505 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2506 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2508 sqlite3_free_table(result);
2510 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2511 /* create mail_certificate_tbl */
2512 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_certificate_tbl';";
2513 /* rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
2514 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2515 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)));
2517 if (atoi(result[1]) < 1) {
2518 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2519 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2521 EM_DEBUG_LOG("CREATE TABLE mail_certificate_tbl");
2523 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2524 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2525 if (error != EMAIL_ERROR_NONE) {
2526 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2530 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2531 } /* mail_contact_sync_tbl */
2532 else if (type == EMAIL_CREATE_DB_CHECK) {
2533 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], NULL, NULL, NULL), rc);
2534 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], rc, sqlite3_errmsg(local_db_handle)));
2537 sqlite3_free_table(result);
2540 /* create mail_task_tbl */
2541 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_task_tbl';";
2542 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2543 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)));
2545 if (atoi(result[1]) < 1) {
2546 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2547 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2549 EM_DEBUG_LOG("CREATE TABLE mail_task_tbl");
2551 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TASK_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2552 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2553 if (error != EMAIL_ERROR_NONE) {
2554 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2558 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_TASK_IDX]);
2559 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2560 if (error != EMAIL_ERROR_NONE) {
2561 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2565 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2566 } /* mail_task_tbl */
2567 else if (type == EMAIL_CREATE_DB_CHECK) {
2568 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TASK_TBL], NULL, NULL, NULL), rc);
2569 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)));
2572 sqlite3_free_table(result);
2575 #ifdef __FEATURE_BODY_SEARCH__
2576 /* create mail_text_tbl */
2577 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_text_tbl';";
2578 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2579 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)));
2581 if (atoi(result[1]) < 1) {
2582 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2583 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2585 EM_DEBUG_LOG("CREATE TABLE mail_text_tbl");
2587 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TEXT_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2588 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2589 if (error != EMAIL_ERROR_NONE) {
2590 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2594 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2595 } /* mail_text_tbl */
2596 else if (type == EMAIL_CREATE_DB_CHECK) {
2597 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TEXT_TBL], NULL, NULL, NULL), rc);
2598 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)));
2601 sqlite3_free_table(result);
2606 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
2607 /* create mail_auto_download_activity_tbl */
2608 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_auto_download_activity_tbl';";
2609 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2610 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)));
2612 if (atoi(result[1]) < 1) {
2613 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2614 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2616 EM_DEBUG_LOG("CREATE TABLE mail_auto_download_activity_tbl");
2618 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], sizeof(sql_query_string)-1);
2619 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2620 if (error != EMAIL_ERROR_NONE) {
2621 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2625 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2626 } /* mail_auto_download_activity_tbl */
2627 else if (type == EMAIL_CREATE_DB_CHECK) {
2628 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2629 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)));
2632 sqlite3_free_table(result);
2641 if (result) sqlite3_free_table(result);
2644 EMSTORAGE_PROTECTED_FUNC_CALL(
2645 sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2647 EMSTORAGE_PROTECTED_FUNC_CALL(
2648 sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
2652 if (create_table_query) {
2654 for (i = 0; i < query_len; i++) {
2655 if (create_table_query[i])
2656 EM_SAFE_FREE(create_table_query[i]);
2658 EM_SAFE_FREE(create_table_query);
2661 if (err_code != NULL)
2664 EM_DEBUG_FUNC_END("ret [%d]", ret);
2668 /* Query series --------------------------------------------------------------*/
2669 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)
2671 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);
2674 int error = EMAIL_ERROR_NONE;
2675 DB_STMT hStmt = NULL;
2676 char *sql_query_string = NULL;
2678 sqlite3 *local_db_handle = NULL;
2680 if (!input_conditional_clause || (!output_total_mail_count && !output_unseen_mail_count)) {
2681 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2682 return EMAIL_ERROR_INVALID_PARAM;
2685 query_size = EM_SAFE_STRLEN(input_conditional_clause) + QUERY_SIZE;
2686 sql_query_string = em_malloc(query_size);
2687 if (sql_query_string == NULL) {
2688 EM_DEBUG_EXCEPTION("em_mallocfailed");
2689 error = EMAIL_ERROR_OUT_OF_MEMORY;
2693 local_db_handle = emstorage_get_db_connection(multi_user_name);
2695 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2697 SNPRINTF(sql_query_string, query_size, "SELECT COUNT(*) FROM mail_tbl");
2698 EM_SAFE_STRNCAT(sql_query_string, (char*)input_conditional_clause, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
2700 if (output_total_mail_count) {
2701 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
2702 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2703 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
2705 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2706 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2707 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
2708 _get_stmt_field_data_int(hStmt, output_total_mail_count, 0);
2711 if (output_unseen_mail_count) {
2712 EM_SAFE_STRNCAT(sql_query_string, " AND flags_seen_field = 0 ", query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
2714 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
2715 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2716 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2718 *output_unseen_mail_count = atoi(result[1]);
2719 sqlite3_free_table(result);
2724 if (hStmt != NULL) {
2725 rc = sqlite3_finalize(hStmt);
2726 if (rc != SQLITE_OK) {
2727 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
2728 error = EMAIL_ERROR_DB_FAILURE;
2732 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2734 EM_SAFE_FREE(sql_query_string);
2736 EM_DEBUG_FUNC_END("error [%d]", error);
2740 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)
2742 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);
2750 int error = EMAIL_ERROR_NONE;
2751 int *result_mail_id_list = NULL;
2752 char **result = NULL;
2753 char *sql_query_string = NULL;
2754 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2756 EM_IF_NULL_RETURN_VALUE(input_conditional_clause, EMAIL_ERROR_INVALID_PARAM);
2757 EM_IF_NULL_RETURN_VALUE(output_mail_id_list, EMAIL_ERROR_INVALID_PARAM);
2758 EM_IF_NULL_RETURN_VALUE(output_mail_id_count, EMAIL_ERROR_INVALID_PARAM);
2760 query_size = strlen(input_conditional_clause) + strlen("SELECT mail_id FROM mail_tbl ") + 10; // 10 is extra space
2761 sql_query_string = em_malloc(query_size);
2762 if (sql_query_string == NULL) {
2763 EM_DEBUG_EXCEPTION("em_mallocfailed");
2764 error = EMAIL_ERROR_OUT_OF_MEMORY;
2768 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2770 /* Composing query */
2771 SNPRINTF_OFFSET(sql_query_string, cur_query, query_size, "SELECT mail_id FROM mail_tbl ");
2772 EM_SAFE_STRNCAT(sql_query_string, (char*)input_conditional_clause, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
2774 EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
2776 /* Performing query */
2777 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2778 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2779 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2783 /* to get mail list */
2785 EM_DEBUG_LOG("No mail found...");
2786 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2790 EM_DEBUG_LOG("There are [%d] mails.", count);
2792 if (!(result_mail_id_list = (int*)em_malloc(sizeof(int) * count))) {
2793 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
2794 error = EMAIL_ERROR_OUT_OF_MEMORY;
2798 EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2800 for (i = 0; i < count; i++)
2801 _get_table_field_data_int(result, result_mail_id_list + i, col_index++);
2803 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >> ", count);
2805 *output_mail_id_list = result_mail_id_list;
2806 *output_mail_id_count = count;
2811 sqlite3_free_table(result);
2813 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2815 EM_SAFE_FREE(sql_query_string);
2817 if (error != EMAIL_ERROR_NONE)
2818 EM_SAFE_FREE(result_mail_id_list);
2820 EM_DEBUG_FUNC_END("error [%d]", error);
2824 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)
2826 EM_DEBUG_FUNC_BEGIN();
2827 EM_PROFILE_BEGIN(emstorage_query_mail_list_func);
2829 int i = 0, count = 0, rc = -1, to_get_count = (result_mail_list) ? 0 : 1;
2830 int sql_query_string_length = 0;
2831 int local_inline_content_count = 0, local_attachment_count = 0;
2832 int cur_query = 0, base_count = 0, col_index;
2833 int ret = false, error = EMAIL_ERROR_NONE;
2834 char *date_time_string = NULL;
2835 char **result = NULL;
2836 char *field_mail_id = "mail_id";
2837 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 ";
2838 char *select_query_form = "SELECT %s FROM mail_tbl ";
2839 char *target_field = NULL;
2840 char *sql_query_string = NULL;
2841 email_mail_list_item_t *mail_list_item_from_tbl = NULL;
2842 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2844 EM_IF_NULL_RETURN_VALUE(conditional_clause, false);
2845 EM_IF_NULL_RETURN_VALUE(result_count, false);
2847 EMSTORAGE_START_READ_TRANSACTION(transaction);
2850 if (to_get_count) /* count only */
2851 target_field = field_mail_id;
2852 else /* mail list in plain form */
2853 target_field = field_all;
2855 sql_query_string_length = EM_SAFE_STRLEN(select_query_form) + EM_SAFE_STRLEN(target_field) + EM_SAFE_STRLEN(conditional_clause);
2857 if (sql_query_string_length)
2858 sql_query_string = em_malloc(sql_query_string_length);
2860 if (sql_query_string == NULL) {
2861 EM_DEBUG_EXCEPTION("em_mallocfailed...");
2862 error = EMAIL_ERROR_OUT_OF_MEMORY;
2866 cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, sql_query_string_length, select_query_form, target_field);
2868 strncat(sql_query_string, conditional_clause, sql_query_string_length - cur_query);
2870 EM_DEBUG_LOG_DEV("query[%s]", sql_query_string);
2872 /* performing query */
2873 EM_PROFILE_BEGIN(emstorage_query_mail_list_performing_query);
2874 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2875 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2876 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2877 EM_PROFILE_END(emstorage_query_mail_list_performing_query);
2883 for (tmp = field_all; tmp && *(tmp + 1); tmp = index(tmp + 1, ',')) i++ ;
2887 col_index = base_count;
2889 EM_DEBUG_LOG_DEV("base_count [%d]", base_count);
2894 EM_DEBUG_LOG_DEV("No mail found...");
2896 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2899 EM_DEBUG_LOG_DEV("There are [%d] mails.", count);
2901 /* to get mail list */
2903 EM_DEBUG_LOG_DEV("No mail found...");
2905 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2909 EM_DEBUG_LOG_DEV("There are [%d] mails.", count);
2910 if (!(mail_list_item_from_tbl = (email_mail_list_item_t*)em_malloc(sizeof(email_mail_list_item_t) * count))) {
2911 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
2912 error = EMAIL_ERROR_OUT_OF_MEMORY;
2916 EM_PROFILE_BEGIN(emstorage_query_mail_list_loop);
2917 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >> ");
2918 for (i = 0; i < count; i++) {
2919 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_id), col_index++);
2920 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].account_id), col_index++);
2921 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mailbox_id), col_index++);
2922 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].mailbox_type), col_index++);
2923 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].full_address_from, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2924 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_sender, MAX_EMAIL_ADDRESS_LENGTH, 1, col_index++);
2925 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_recipient, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2926 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].subject, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2927 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].body_download_status), col_index++);
2928 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_size), col_index++);
2929 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_seen_field), col_index++);
2930 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_deleted_field), col_index++);
2931 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_flagged_field), col_index++);
2932 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_answered_field), col_index++);
2933 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_recent_field), col_index++);
2934 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_draft_field), col_index++);
2935 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_forwarded_field), col_index++);
2936 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].DRM_status), col_index++);
2937 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].priority), col_index++);
2938 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].save_status), col_index++);
2939 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].lock_status), col_index++);
2940 _get_table_field_data_int(result, &local_attachment_count, col_index++);
2941 _get_table_field_data_int(result, &local_inline_content_count, col_index++);
2942 _get_table_field_data_time_t(result, &(mail_list_item_from_tbl[i].date_time), col_index++);
2943 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].preview_text, MAX_PREVIEW_TEXT_LENGTH, 1, col_index++);
2944 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_id), col_index++);
2945 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_item_count), col_index++);
2946 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].meeting_request_status), col_index++);
2947 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].message_class), col_index++);
2948 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].smime_type), col_index++);
2949 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].scheduled_sending_time), col_index++);
2950 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].remaining_resend_times), col_index++);
2951 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].tag_id), col_index++);
2952 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].eas_data_length), col_index++);
2953 _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++);
2955 mail_list_item_from_tbl[i].attachment_count = (local_attachment_count > 0) ? 1 : 0;
2957 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >> ", count);
2958 EM_PROFILE_END(emstorage_query_mail_list_loop);
2964 EM_DEBUG_LOG("MAIL_COUNT [%d]", count);
2967 sqlite3_free_table(result);
2970 *result_count = count;
2973 if (result_mail_list)
2974 *result_mail_list = mail_list_item_from_tbl;
2975 *result_count = count;
2977 EM_SAFE_FREE(mail_list_item_from_tbl);
2980 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2982 // sqlite3_db_release_memory(local_db_handle);
2984 EM_SAFE_FREE(sql_query_string);
2985 EM_SAFE_FREE(date_time_string);
2987 if (err_code != NULL)
2990 EM_PROFILE_END(emstorage_query_mail_list_func);
2991 EM_DEBUG_FUNC_END("ret [%d]", ret);
2996 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)
2998 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);
3000 if (!conditional_clause || !result_mail_tbl || !result_count) {
3001 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: [%p] [%p] [%p]", conditional_clause, result_mail_tbl, result_mail_tbl);
3002 if (err_code != NULL)
3003 *err_code = EMAIL_ERROR_INVALID_PARAM;
3007 int i, col_index = FIELD_COUNT_OF_MAIL_TBL, rc, ret = false, count;
3008 int error = EMAIL_ERROR_NONE;
3009 char **result = NULL, sql_query_string[QUERY_SIZE] = {0, };
3010 emstorage_mail_tbl_t* p_data_tbl = NULL;
3011 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3013 EMSTORAGE_START_READ_TRANSACTION(transaction);
3015 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl %s", conditional_clause);
3017 EM_DEBUG_LOG_DEV("Query[%s]", sql_query_string);
3019 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3020 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3021 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
3024 EM_DEBUG_LOG("No mail found...");
3026 error = EMAIL_ERROR_MAIL_NOT_FOUND;
3030 EM_DEBUG_LOG("There are [%d] mails.", count);
3031 if (!(p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * count))) {
3032 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
3033 error = EMAIL_ERROR_OUT_OF_MEMORY;
3037 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >> ");
3038 for (i = 0; i < count; i++) {
3039 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
3040 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3041 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3042 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_type), col_index++);
3043 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 1, col_index++);
3044 _get_table_field_data_time_t (result, &(p_data_tbl[i].date_time), col_index++);
3045 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
3046 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
3047 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
3048 _get_table_field_data_string(result, &(p_data_tbl[i].message_id), 0, col_index++);
3049 _get_table_field_data_int(result, &(p_data_tbl[i].reference_mail_id), col_index++);
3050 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_from), 1, col_index++);
3051 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_reply), 1, col_index++);
3052 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_to), 1, col_index++);
3053 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_cc), 1, col_index++);
3054 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_bcc), 1, col_index++);
3055 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_return), 1, col_index++);
3056 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_sender), 1, col_index++);
3057 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_recipient), 1, col_index++);
3058 _get_table_field_data_string(result, &(p_data_tbl[i].alias_sender), 1, col_index++);
3059 _get_table_field_data_string(result, &(p_data_tbl[i].alias_recipient), 1, col_index++);
3060 _get_table_field_data_int(result, &(p_data_tbl[i].body_download_status), col_index++);
3061 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
3062 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
3063 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
3064 _get_table_field_data_int(result, &(p_data_tbl[i].mail_size), col_index++);
3065 _get_table_field_data_char (result, &(p_data_tbl[i].flags_seen_field), col_index++);
3066 _get_table_field_data_char (result, &(p_data_tbl[i].flags_deleted_field), col_index++);
3067 _get_table_field_data_char (result, &(p_data_tbl[i].flags_flagged_field), col_index++);
3068 _get_table_field_data_char (result, &(p_data_tbl[i].flags_answered_field), col_index++);
3069 _get_table_field_data_char (result, &(p_data_tbl[i].flags_recent_field), col_index++);
3070 _get_table_field_data_char (result, &(p_data_tbl[i].flags_draft_field), col_index++);
3071 _get_table_field_data_char (result, &(p_data_tbl[i].flags_forwarded_field), col_index++);
3072 _get_table_field_data_int(result, &(p_data_tbl[i].DRM_status), col_index++);
3073 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].priority), col_index++);
3074 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
3075 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
3076 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].report_status), col_index++);
3077 _get_table_field_data_int(result, &(p_data_tbl[i].attachment_count), col_index++);
3078 _get_table_field_data_int(result, &(p_data_tbl[i].inline_content_count), col_index++);
3079 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
3080 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
3081 _get_table_field_data_string(result, &(p_data_tbl[i].preview_text), 1, col_index++);
3082 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].meeting_request_status), col_index++);
3083 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].message_class), col_index++);
3084 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].digest_type), col_index++);
3085 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].smime_type), col_index++);
3086 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].scheduled_sending_time), col_index++);
3087 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].remaining_resend_times), col_index++);
3088 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].tag_id), col_index++);
3089 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].replied_time), col_index++);
3090 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].forwarded_time), col_index++);
3091 _get_table_field_data_string(result, &(p_data_tbl[i].default_charset), 0, col_index++);
3092 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3093 _get_table_field_data_blob(result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3094 _get_table_field_data_string(result, &(p_data_tbl[i].user_name), 0, col_index++);
3101 sqlite3_free_table(result);
3104 *result_mail_tbl = p_data_tbl;
3105 *result_count = count;
3107 *result_mail_tbl = NULL;
3111 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3113 // sqlite3_db_release_memory(local_db_handle);
3115 if (err_code != NULL)
3118 EM_DEBUG_FUNC_END("ret [%d]", ret);
3122 #ifdef __FEATURE_BODY_SEARCH__
3123 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)
3125 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);
3127 if (!conditional_clause || !result_mail_text_tbl || !result_count) { /*prevent 50930*/
3128 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3129 if (err_code != NULL)
3130 *err_code = EMAIL_ERROR_INVALID_PARAM;
3138 int col_index = FIELD_COUNT_OF_MAIL_TEXT_TBL;
3139 int error = EMAIL_ERROR_NONE;
3140 char **result = NULL;
3141 char sql_query_string[QUERY_SIZE] = {0, };
3142 emstorage_mail_text_tbl_t* p_data_tbl = NULL;
3143 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3145 EMSTORAGE_START_READ_TRANSACTION(transaction);
3147 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_text_tbl %s", conditional_clause);
3149 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
3151 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3152 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3153 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3156 EM_DEBUG_LOG("No mail found...");
3158 error = EMAIL_ERROR_MAIL_NOT_FOUND;
3162 EM_DEBUG_LOG("There are [%d] mails.", count);
3163 if (!(p_data_tbl = (emstorage_mail_text_tbl_t *)em_malloc(sizeof(emstorage_mail_text_tbl_t) * count))) {
3164 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_text_tbl_t failed...");
3165 error = EMAIL_ERROR_OUT_OF_MEMORY;
3169 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
3170 for (i = 0; i < count; i++) {
3171 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
3172 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3173 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3174 _get_table_field_data_string(result, &(p_data_tbl[i].body_text), 0, col_index++);
3181 sqlite3_free_table(result);
3184 *result_mail_text_tbl = p_data_tbl;
3185 *result_count = count;
3187 EM_SAFE_FREE(p_data_tbl);
3189 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3191 // sqlite3_db_release_memory(local_db_handle);
3193 if (err_code != NULL)
3196 EM_DEBUG_FUNC_END("ret [%d]", ret);
3201 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)
3203 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);
3209 int error = EMAIL_ERROR_NONE;
3211 int total_count = 0;
3213 char sql_query_string[QUERY_SIZE] = {0, };
3214 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 ";
3215 emstorage_mailbox_tbl_t* p_data_tbl = NULL;
3216 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3218 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
3220 if (input_get_mail_count == 0) { /* without mail count */
3222 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);
3223 } else { /* with read count and total count */
3225 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);
3228 EM_DEBUG_LOG_DEV("query[%s]", sql_query_string);
3230 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
3231 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3232 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string))
3234 EM_DEBUG_LOG_DEV("result count [%d]", count);
3237 EM_DEBUG_LOG_SEC("Can't find mailbox query[%s]", sql_query_string);
3238 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
3242 if ((p_data_tbl = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t) * count)) == NULL) {
3243 EM_DEBUG_EXCEPTION("em_mallocfailed...");
3244 error = EMAIL_ERROR_OUT_OF_MEMORY;
3248 for (i = 0; i < count; i++) {
3249 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3250 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3251 _get_table_field_data_int(result, &(p_data_tbl[i].local_yn), col_index++);
3252 _get_table_field_data_string(result, &(p_data_tbl[i].mailbox_name), 0, col_index++);
3253 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].mailbox_type), col_index++);
3254 _get_table_field_data_string(result, &(p_data_tbl[i].alias), 0, col_index++);
3255 _get_table_field_data_int(result, &(p_data_tbl[i].deleted_flag), col_index++);
3256 _get_table_field_data_int(result, &(p_data_tbl[i].modifiable_yn), col_index++);
3257 _get_table_field_data_int(result, &(p_data_tbl[i].total_mail_count_on_server), col_index++);
3258 _get_table_field_data_int(result, &(p_data_tbl[i].has_archived_mails), col_index++);
3259 _get_table_field_data_int(result, &(p_data_tbl[i].mail_slot_size), col_index++);
3260 _get_table_field_data_int(result, &(p_data_tbl[i].no_select), col_index++);
3261 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].last_sync_time), col_index++);
3262 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3263 _get_table_field_data_blob(result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3265 if (input_get_mail_count == 1) {
3266 _get_table_field_data_int(result, &(total_count), col_index++);
3267 p_data_tbl[i].total_mail_count_on_local = total_count;
3268 _get_table_field_data_int(result, &(read_count), col_index++);
3269 p_data_tbl[i].unread_count = total_count - read_count; /* return unread count, NOT */
3276 sqlite3_free_table(result);
3278 if (error == EMAIL_ERROR_NONE) {
3279 *output_mailbox_list = p_data_tbl;
3280 *output_mailbox_count = count;
3281 EM_DEBUG_LOG("Mailbox Count [%d]", count);
3284 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
3286 // sqlite3_db_release_memory(local_db_handle);
3288 EM_DEBUG_FUNC_END("error [%d]", error);
3292 /* Query series --------------------------------------------------------------*/
3293 INTERNAL_FUNC int emstorage_check_duplicated_account(char *multi_user_name, email_account_t* account, int transaction, int *err_code)
3295 EM_DEBUG_FUNC_BEGIN();
3296 int rc = -1, ret = false;
3297 int error = EMAIL_ERROR_NONE;
3300 char sql_query_string[QUERY_SIZE] = {0, };
3301 sqlite3 *local_db_handle = NULL;
3303 if (account == NULL || account->user_email_address == NULL || account->incoming_server_user_name == NULL || account->incoming_server_address == NULL ||
3304 account->outgoing_server_user_name == NULL || account->outgoing_server_address == NULL) {
3305 if (err_code != NULL)
3306 *err_code = EMAIL_ERROR_INVALID_PARAM;
3310 local_db_handle = emstorage_get_db_connection(multi_user_name);
3312 EMSTORAGE_START_READ_TRANSACTION(transaction);
3314 SNPRINTF(sql_query_string, sizeof(sql_query_string),
3315 "SELECT COUNT(*) FROM mail_account_tbl "
3317 " user_email_address = '%s' AND "
3318 " incoming_server_user_name = '%s' AND "
3319 " incoming_server_type = %d AND "
3320 " incoming_server_address = '%s' AND "
3321 " outgoing_server_user_name = '%s' AND "
3322 " outgoing_server_type = %d AND "
3323 " outgoing_server_address = '%s'; ",
3324 account->user_email_address,
3325 account->incoming_server_user_name, account->incoming_server_type, account->incoming_server_address,
3326 account->outgoing_server_user_name, account->outgoing_server_type, account->outgoing_server_address
3328 EM_DEBUG_LOG_SEC("Query[%s]", sql_query_string);
3329 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
3330 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3331 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3333 count = atoi(result[1]);
3334 sqlite3_free_table(result);
3336 EM_DEBUG_LOG("Count of Duplicated Account Information: [%d]", count);
3338 if (count == 0) { /* not duplicated account */
3340 EM_DEBUG_LOG_SEC("NOT duplicated account: user_email_address[%s]", account->user_email_address);
3341 } else { /* duplicated account */
3343 EM_DEBUG_LOG_SEC("The same account already exists. Duplicated account: user_email_address[%s]", account->user_email_address);
3344 error = EMAIL_ERROR_ALREADY_EXISTS;
3349 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3351 if (err_code != NULL)
3354 EM_DEBUG_FUNC_END("ret [%d]", ret);
3359 INTERNAL_FUNC int emstorage_get_account_count(char *multi_user_name, int *count, int transaction, int *err_code)
3361 EM_DEBUG_FUNC_BEGIN();
3364 if (err_code != NULL)
3365 *err_code = EMAIL_ERROR_INVALID_PARAM;
3369 int rc = -1, ret = false;
3370 int error = EMAIL_ERROR_NONE;
3372 DB_STMT hStmt = NULL;
3373 char sql_query_string[QUERY_SIZE] = {0, };
3376 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3377 EMSTORAGE_START_READ_TRANSACTION(transaction);
3378 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_account_tbl");
3379 EM_DEBUG_LOG_SEC("SQL STMT [%s]", sql_query_string);
3380 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string,
3381 EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3382 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
3383 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3384 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3386 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3387 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3388 ("sqlite3_step fail:%d", rc));
3390 *count = sqlite3_column_int(hStmt, 0);
3396 if (hStmt != NULL) {
3397 rc = sqlite3_finalize(hStmt);
3399 if (rc != SQLITE_OK) {
3400 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
3401 error = EMAIL_ERROR_DB_FAILURE;
3405 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3407 if (err_code != NULL)
3410 EM_DEBUG_FUNC_END("ret [%d]", ret);
3414 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)
3416 EM_DEBUG_FUNC_BEGIN();
3418 int i = 0, count = 0, rc = -1, ret = false;
3419 int field_index = 0;
3421 int error = EMAIL_ERROR_NONE;
3422 emstorage_account_tbl_t *p_data_tbl = NULL;
3423 char sql_query_string[QUERY_SIZE] = {0, };
3424 char *sql = "SELECT count(*) FROM mail_account_tbl;";
3426 sqlite3 *local_db_handle = NULL;
3427 DB_STMT hStmt = NULL;
3429 if (!select_num || !account_list) {
3430 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, account_list);
3431 if (err_code != NULL)
3432 *err_code = EMAIL_ERROR_INVALID_PARAM;
3436 local_db_handle = emstorage_get_db_connection(multi_user_name);
3438 EMSTORAGE_START_READ_TRANSACTION(transaction);
3440 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
3441 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3442 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
3444 count = atoi(result[1]);
3445 sqlite3_free_table(result);
3447 EM_DEBUG_LOG_DEV("count = %d", rc);
3450 EM_DEBUG_LOG("no account found...");
3451 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3455 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
3456 sql_len = EM_SAFE_STRLEN(sql_query_string);
3458 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
3463 "user_data_length, "
3467 "default_mail_slot_size, "
3470 "user_display_name, "
3471 "user_email_address, "
3472 "reply_to_address, "
3474 "incoming_server_type, "
3475 "incoming_server_address, "
3476 "incoming_server_port_number, "
3477 "incoming_server_user_name, "
3478 "incoming_server_password, "
3479 "incoming_server_secure_connection, "
3480 "incoming_server_authentication_method, "
3482 "keep_mails_on_pop_server_after_download, "
3484 "auto_download_size, "
3489 "outgoing_server_type, "
3490 "outgoing_server_address, "
3491 "outgoing_server_port_number, "
3492 "outgoing_server_user_name, "
3493 "outgoing_server_password, "
3494 "outgoing_server_secure_connection, "
3495 "outgoing_server_need_authentication, "
3496 "outgoing_server_use_same_authenticator, "
3499 "req_delivery_receipt, "
3500 "req_read_receipt, "
3504 "display_name_from, "
3506 "forward_with_files, "
3510 "add_my_address_to_bcc, "
3511 "notification_status, "
3513 "display_content_status, "
3514 "default_ringtone_status, "
3515 "alert_ringtone_path, "
3516 "auto_resend_times, "
3517 "outgoing_server_size_limit, "
3518 "wifi_auto_download, "
3520 "incoming_server_requires_apop, "
3522 "certificate_path, "
3527 sql_len = EM_SAFE_STRLEN(sql_query_string);
3529 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " FROM mail_account_tbl ORDER BY account_id");
3531 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3533 EM_DEBUG_LOG_DEV("After sqlite3_prepare_v2 hStmt = %p", hStmt);
3534 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3535 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3537 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3538 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3539 ("sqlite3_step fail:%d", rc));
3540 if (rc == SQLITE_DONE) {
3541 EM_DEBUG_EXCEPTION("no account found...");
3543 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3549 if (!(p_data_tbl = (emstorage_account_tbl_t*)malloc(sizeof(emstorage_account_tbl_t) * count))) {
3550 EM_DEBUG_EXCEPTION("malloc failed...");
3551 error = EMAIL_ERROR_OUT_OF_MEMORY;
3554 memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * count);
3555 for (i = 0; i < count; i++) {
3558 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_id), field_index++);
3559 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].account_name), 0, field_index++);
3560 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].logo_icon_path), 0, field_index++);
3561 _get_stmt_field_data_blob(hStmt, &(p_data_tbl[i].user_data), field_index++);
3562 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].user_data_length), field_index++);
3563 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_svc_id), field_index++);
3564 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_status), field_index++);
3565 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_disabled), field_index++);
3566 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].default_mail_slot_size), field_index++);
3567 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].roaming_option), field_index++);
3568 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].color_label), field_index++);
3569 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_display_name), 0, field_index++);
3570 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_email_address), 0, field_index++);
3571 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].reply_to_address), 0, field_index++);
3572 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].return_address), 0, field_index++);
3573 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].incoming_server_type), field_index++);
3574 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_address), 0, field_index++);
3575 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_port_number), field_index++);
3576 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_user_name), 0, field_index++);
3577 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_password), 0, field_index++);
3578 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_secure_connection), field_index++);
3579 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].incoming_server_authentication_method), field_index++);
3580 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].retrieval_mode), field_index++);
3581 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].keep_mails_on_pop_server_after_download), field_index++);
3582 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].check_interval), field_index++);
3583 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_download_size), field_index++);
3584 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_interval), field_index++);
3585 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_days), field_index++);
3586 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_start_time), field_index++);
3587 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_end_time), field_index++);
3588 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].outgoing_server_type), field_index++);
3589 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_address), 0, field_index++);
3590 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_port_number), field_index++);
3591 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_user_name), 0, field_index++);
3592 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_password), 0, field_index++);
3593 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_secure_connection), field_index++);
3594 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_need_authentication), field_index++);
3595 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_use_same_authenticator), field_index++);
3596 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.priority), field_index++);
3597 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.keep_local_copy), field_index++);
3598 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_delivery_receipt), field_index++);
3599 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_read_receipt), field_index++);
3600 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.download_limit), field_index++);
3601 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_address), field_index++);
3602 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_subject), field_index++);
3603 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.display_name_from), 0, field_index++);
3604 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.reply_with_body), field_index++);
3605 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.forward_with_files), field_index++);
3606 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_myname_card), field_index++);
3607 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_signature), field_index++);
3608 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.signature), 0, field_index++);
3609 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.add_my_address_to_bcc), field_index++);
3610 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.notification_status), field_index++);
3611 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.vibrate_status), field_index++);
3612 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.display_content_status), field_index++);
3613 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.default_ringtone_status), field_index++);
3614 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.alert_ringtone_path), 0, field_index++);
3615 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_resend_times), field_index++);
3616 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_size_limit), field_index++);
3617 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].wifi_auto_download), field_index++);
3618 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].pop_before_smtp), field_index++);
3619 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_requires_apop), field_index++);
3620 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].smime_type), field_index++);
3621 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].certificate_path), 0, field_index++);
3622 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].cipher_type), field_index++);
3623 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].digest_type), field_index++);
3625 /* EAS passwd is not accessible */
3626 if (with_password == true && p_data_tbl[i].incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
3627 /* get password from the secure storage */
3628 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3629 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3631 EM_SAFE_FREE(p_data_tbl[i].incoming_server_password);
3632 EM_SAFE_FREE(p_data_tbl[i].outgoing_server_password);
3634 /* get password file name */
3635 error = _get_password_file_name(multi_user_name, p_data_tbl[i].account_id,
3636 recv_password_file_name,
3637 send_password_file_name);
3638 if (error != EMAIL_ERROR_NONE) {
3639 EM_DEBUG_EXCEPTION("_get_password_file_name error [%d]", error);
3640 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3644 /* read password from secure storage */
3645 error = _read_password_from_secure_storage(recv_password_file_name,
3646 &(p_data_tbl[i].incoming_server_password));
3648 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s] error [%d]",
3649 recv_password_file_name, error);
3650 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3653 error = _read_password_from_secure_storage(send_password_file_name,
3654 &(p_data_tbl[i].outgoing_server_password));
3656 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s] error [%d]",
3657 send_password_file_name, error);
3658 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3663 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3664 EM_DEBUG_LOG_DEV("after sqlite3_step(), i = %d, rc = %d.", i, rc);
3665 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3666 ("sqlite3_step fail:%d", rc));
3673 *account_list = p_data_tbl;
3674 *select_num = count;
3675 EM_DEBUG_LOG_DEV("COUNT : %d", count);
3676 } else if (p_data_tbl != NULL)
3677 emstorage_free_account(&p_data_tbl, count, NULL);
3679 if (hStmt != NULL) {
3680 rc = sqlite3_finalize(hStmt);
3682 if (rc != SQLITE_OK) {
3683 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
3684 error = EMAIL_ERROR_DB_FAILURE;
3688 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3690 if (err_code != NULL)
3693 EM_DEBUG_FUNC_END("ret [%d]", ret);
3697 INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(char *multi_user_name,
3698 char *server_mail_id,
3700 emstorage_mail_tbl_t **mail,
3704 EM_DEBUG_FUNC_BEGIN_SEC("mailbox_id [%d], server_mail_id[%s], mail[%p], transaction[%d], err_code[%p]",
3705 mailbox_id, server_mail_id, mail, transaction, err_code);
3707 int ret = false, error = EMAIL_ERROR_NONE, result_count;
3708 char conditional_clause[QUERY_SIZE] = {0, };
3709 emstorage_mail_tbl_t* p_data_tbl = NULL;
3711 if (!server_mail_id || !mail) {
3712 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3713 error = EMAIL_ERROR_INVALID_PARAM;
3717 sqlite3_snprintf(sizeof(conditional_clause), conditional_clause, "WHERE server_mail_id = '%q'", server_mail_id);
3719 if (mailbox_id > 0) {
3720 SNPRINTF(conditional_clause + strlen(conditional_clause),
3721 QUERY_SIZE - strlen(conditional_clause),
3722 " AND mailbox_id = %d",
3726 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
3727 if (!emstorage_query_mail_tbl(multi_user_name,
3733 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3744 if (err_code != NULL)
3747 EM_DEBUG_FUNC_END("ret [%d]", ret);
3752 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)
3754 int cur_clause = 0, conditional_clause_count = 0, i = 0;
3756 if (account_id < ALL_ACCOUNT) {
3757 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3758 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3762 if (account_id != ALL_ACCOUNT) {
3763 cur_clause += (conditional_clause_count++ == 0) ?
3764 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE account_id = %d", account_id) :
3765 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND account_id = %d", account_id);
3768 if (mailbox_id > 0) {
3769 cur_clause += (conditional_clause_count++ == 0) ?
3770 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_id = %d", mailbox_id) :
3771 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_id = %d", mailbox_id);
3774 if (thread_id > 0) {
3775 cur_clause += (conditional_clause_count++ == 0) ?
3776 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_id = %d ", thread_id) :
3777 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_id = %d ", thread_id);
3778 } else if (thread_id == EMAIL_LIST_TYPE_THREAD) {
3779 cur_clause += (conditional_clause_count++ == 0) ?
3780 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_item_count > 0") :
3781 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_item_count > 0");
3782 } else if (thread_id == EMAIL_LIST_TYPE_LOCAL) {
3783 cur_clause += (conditional_clause_count++ == 0) ?
3784 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE server_mail_status = 0") :
3785 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND server_mail_status = 0");
3786 } else if (thread_id == EMAIL_LIST_TYPE_UNREAD) {
3787 cur_clause += (conditional_clause_count++ == 0) ?
3788 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_seen_field == 0") :
3789 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_seen_field == 0");
3792 /* EM_DEBUG_LOG("where clause added [%s]", conditional_clause_string); */
3793 if (addr_list && addr_list->address_count > 0) {
3794 if (!addr_list->address_type) {
3795 cur_clause += (conditional_clause_count++ == 0) ?
3796 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE email_address_sender IN(\"%s\"", addr_list->address_list[0]) :
3797 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND email_address_sender IN(\"%s\"", addr_list->address_list[0]);
3799 for (i = 1; i < addr_list->address_count; i++)
3800 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3802 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3804 cur_clause += (conditional_clause_count++ == 0) ?
3805 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]) :
3806 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]);
3808 for (i = 1; i < addr_list->address_count; i++)
3809 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3811 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3815 if (input_except_delete_flagged_mails) {
3816 cur_clause += (conditional_clause_count++ == 0) ?
3817 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_deleted_field = 0") :
3818 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_deleted_field = 0");
3822 switch (search_type) {
3823 case EMAIL_SEARCH_FILTER_SUBJECT:
3824 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3825 " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3827 case EMAIL_SEARCH_FILTER_SENDER:
3828 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3829 " %s ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3830 ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3832 case EMAIL_SEARCH_FILTER_RECIPIENT:
3833 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3834 " %s ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3835 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3836 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3837 ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value);
3839 case EMAIL_SEARCH_FILTER_ALL:
3840 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3841 " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\' "
3842 " OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3843 " OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3844 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3845 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3848 ")", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value, search_value, search_value);
3853 /* EM_DEBUG_LOG("where clause [%s]", conditional_clause_string); */
3854 static char sorting_str[][50] = {
3855 " ORDER BY date_time DESC", /* case EMAIL_SORT_DATETIME_HIGH: */
3856 " ORDER BY date_time ASC", /* case EMAIL_SORT_DATETIME_LOW: */
3857 " ORDER BY full_address_from DESC, date_time DESC", /* case EMAIL_SORT_SENDER_HIGH: */
3858 " ORDER BY full_address_from ASC, date_time DESC", /* case EMAIL_SORT_SENDER_LOW: */
3859 " ORDER BY full_address_to DESC, date_time DESC", /* case EMAIL_SORT_RCPT_HIGH: */
3860 " ORDER BY full_address_to ASC, date_time DESC", /* case EMAIL_SORT_RCPT_LOW: */
3861 " ORDER BY subject DESC, date_time DESC", /* case EMAIL_SORT_SUBJECT_HIGH: */
3862 " ORDER BY subject ASC, date_time DESC", /* case EMAIL_SORT_SUBJECT_LOW: */
3863 " ORDER BY priority DESC, date_time DESC", /* case EMAIL_SORT_PRIORITY_HIGH: */
3864 " ORDER BY priority ASC, date_time DESC", /* case EMAIL_SORT_PRIORITY_LOW: */
3865 " ORDER BY attachment_count DESC, date_time DESC", /* case EMAIL_SORT_ATTACHMENT_HIGH: */
3866 " ORDER BY attachment_count ASC, date_time DESC", /* case EMAIL_SORT_ATTACHMENT_LOW: */
3867 " ORDER BY lock_status DESC, date_time DESC", /* case EMAIL_SORT_FAVORITE_HIGH: */
3868 " ORDER BY lock_status ASC, date_time DESC", /* case EMAIL_SORT_FAVORITE_LOW: */
3869 " ORDER BY mailbox_id DESC, date_time DESC", /* case EMAIL_SORT_MAILBOX_ID_HIGH: */
3870 " ORDER BY mailbox_id ASC, date_time DESC", /* case EMAIL_SORT_MAILBOX_ID_LOW: */
3871 " ORDER BY flags_flagged_field DESC, date_time DESC", /* case EMAIL_SORT_FLAGGED_FLAG_HIGH: */
3872 " ORDER BY flags_flagged_field ASC, date_time DESC", /* case EMAIL_SORT_FLAGGED_FLAG_LOW: */
3873 " ORDER BY flags_seen_field DESC, date_time DESC", /* case EMAIL_SORT_SEEN_FLAG_HIGH: */
3874 " ORDER BY flags_seen_field ASC, date_time DESC" /* case EMAIL_SORT_SEEN_FLAG_LOW: */
3877 if (sorting < EMAIL_SORT_END)
3878 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " %s", sorting_str[sorting]);
3880 EM_DEBUG_LOG(" Invalid Sorting order ");
3883 if (start_index != -1 && limit_count != -1)
3884 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " LIMIT %d, %d", start_index, limit_count);
3891 * emstorage_get_mail_list - Get the mail list information.
3895 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)
3897 EM_DEBUG_FUNC_BEGIN();
3898 EM_PROFILE_BEGIN(emstorage_get_mail_list_func);
3900 int ret = false, error = EMAIL_ERROR_NONE;
3901 char conditional_clause_string[QUERY_SIZE] = { 0, };
3903 if (account_id < ALL_ACCOUNT) {
3904 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3905 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3907 EM_IF_NULL_RETURN_VALUE(result_count, false);
3909 _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);
3911 EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
3913 if (!emstorage_query_mail_list(multi_user_name, conditional_clause_string, transaction, mail_list, result_count, &error)) {
3914 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
3921 if (err_code != NULL)
3924 EM_PROFILE_END(emstorage_get_mail_list_func);
3925 EM_DEBUG_FUNC_END("ret [%d]", ret);
3931 * emstorage_get_mails - Get the Mail list information based on mailbox_name name
3935 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)
3937 EM_DEBUG_FUNC_BEGIN();
3938 EM_PROFILE_BEGIN(emStorageGetMails);
3940 int count = 0, ret = false, error = EMAIL_ERROR_NONE;
3941 emstorage_mail_tbl_t *p_data_tbl = NULL;
3942 char conditional_clause_string[QUERY_SIZE] = {0, };
3944 EM_IF_NULL_RETURN_VALUE(mail_list, false);
3945 EM_IF_NULL_RETURN_VALUE(result_count, false);
3947 if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3948 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3949 if (err_code != NULL)
3950 *err_code = EMAIL_ERROR_INVALID_PARAM;
3954 _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);
3956 EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
3958 if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause_string, transaction, &p_data_tbl, &count, &error)) {
3959 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3968 *mail_list = p_data_tbl;
3969 *result_count = count;
3970 EM_DEBUG_LOG("COUNT : %d", count);
3971 } else if (p_data_tbl != NULL)
3972 emstorage_free_mail(&p_data_tbl, count, NULL);
3974 if (err_code != NULL)
3977 EM_PROFILE_END(emStorageGetMails);
3978 EM_DEBUG_FUNC_END("ret [%d]", ret);
3985 * emstorage_get_searched_mail_list - Get the mail list information after filtering
3989 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)
3991 EM_DEBUG_FUNC_BEGIN();
3993 int ret = false, error = EMAIL_ERROR_NONE;
3994 char conditional_clause[QUERY_SIZE] = {0, };
3995 char *temp_search_value = NULL;
3996 char *temp_search_value2 = NULL;
3998 EM_IF_NULL_RETURN_VALUE(mail_list, false);
3999 EM_IF_NULL_RETURN_VALUE(result_count, false);
4001 if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
4002 EM_DEBUG_EXCEPTION("select_num[%p], Mail_list[%p]", result_count, mail_list);
4003 error = EMAIL_ERROR_INVALID_PARAM;
4007 temp_search_value = em_replace_all_string((char*)search_value, "_", "\\_");
4008 temp_search_value2 = em_replace_all_string(temp_search_value, "%", "\\%");
4010 _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);
4012 EM_DEBUG_LOG("conditional_clause[%s]", conditional_clause);
4014 if (!emstorage_query_mail_list(multi_user_name, conditional_clause, transaction, mail_list, result_count, &error)) {
4015 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
4022 EM_DEBUG_LOG("emstorage_get_searched_mail_list finish off");
4024 if (err_code != NULL)
4027 EM_SAFE_FREE(temp_search_value);
4028 EM_SAFE_FREE(temp_search_value2);
4030 EM_DEBUG_FUNC_END("ret [%d]", ret);
4035 static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name)
4037 EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
4039 if (account_id <= 0 || !recv_password_file_name || !send_password_file_name) {
4040 EM_DEBUG_EXCEPTION("Invalid parameter");
4041 return EMAIL_ERROR_INVALID_PARAM;
4044 EM_DEBUG_LOG("MULTI_USER_NAME : [%s]", multi_user_name);
4046 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
4047 snprintf(recv_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_recv_%s", account_id, multi_user_name);
4048 snprintf(send_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_send_%s", account_id, multi_user_name);
4050 snprintf(recv_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_recv", account_id);
4051 snprintf(send_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_send", account_id);
4054 EM_DEBUG_FUNC_END();
4055 return EMAIL_ERROR_NONE;
4058 static int _read_password_from_secure_storage(char *file_name, char **password)
4060 EM_DEBUG_FUNC_BEGIN_SEC("file_name[%s]", file_name);
4062 if (!file_name || !password) {
4063 EM_DEBUG_EXCEPTION("Invalid Parameter");
4064 return EMAIL_ERROR_INVALID_PARAM;
4067 int ret = EMAIL_ERROR_NONE;
4069 ret = emcore_get_password_in_key_manager(file_name, password);
4070 if (ret != EMAIL_ERROR_NONE) {
4071 EM_DEBUG_EXCEPTION("emcore_get_password_in_key_manager failed : [%d]", ret);
4077 EM_DEBUG_FUNC_END("ret [%d]", ret);
4081 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)
4083 EM_DEBUG_FUNC_BEGIN("account_id[%d], pulloption[%d], account[%p], transaction[%d], err_code[%p]", account_id, pulloption, account, transaction, err_code);
4086 EM_DEBUG_EXCEPTION("account_id[%d], account[%p]", account_id, account);
4087 if (err_code != NULL)
4088 *err_code = EMAIL_ERROR_INVALID_PARAM;
4093 int error = EMAIL_ERROR_NONE;
4094 emstorage_account_tbl_t* p_data_tbl = NULL;
4095 DB_STMT hStmt = NULL;
4096 char sql_query_string[QUERY_SIZE] = {0, };
4099 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4100 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4102 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4104 EMSTORAGE_START_READ_TRANSACTION(transaction);
4106 /* Make query string */
4107 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
4108 sql_len = EM_SAFE_STRLEN(sql_query_string);
4110 if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4111 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4112 "incoming_server_type,"
4113 "incoming_server_address,"
4114 "user_email_address,"
4115 "incoming_server_user_name,"
4117 "incoming_server_port_number,"
4118 "incoming_server_secure_connection,"
4119 "incoming_server_authentication_method,"
4120 "outgoing_server_type,"
4121 "outgoing_server_address,"
4122 "outgoing_server_port_number,"
4123 "outgoing_server_need_authentication,"
4124 "outgoing_server_secure_connection,"
4125 "outgoing_server_user_name,"
4126 "user_display_name,"
4130 "keep_mails_on_pop_server_after_download,"
4131 "auto_download_size,"
4136 "outgoing_server_use_same_authenticator,"
4137 "auto_resend_times,"
4138 "outgoing_server_size_limit,"
4139 "wifi_auto_download,"
4141 "incoming_server_requires_apop,"
4148 sql_len = EM_SAFE_STRLEN(sql_query_string);
4151 if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME) {
4152 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " account_name, ");
4153 sql_len = EM_SAFE_STRLEN(sql_query_string);
4156 /* get from secure storage, not from db */
4157 if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4158 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4161 "req_delivery_receipt,"
4166 "display_name_from,"
4168 "forward_with_files,"
4172 "add_my_address_to_bcc,"
4173 "notification_status,"
4175 "display_content_status,"
4176 "default_ringtone_status,"
4177 "alert_ringtone_path,"
4180 "default_mail_slot_size,"
4187 sql_len = EM_SAFE_STRLEN(sql_query_string);
4190 /* dummy value, FROM WHERE clause */
4191 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "0 FROM mail_account_tbl WHERE account_id = %d", account_id);
4194 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
4196 /* execute a sql and count rows */
4197 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4198 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4199 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4201 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4202 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4203 ("sqlite3_step fail:%d", rc));
4205 if (rc == SQLITE_DONE) {
4206 EM_DEBUG_EXCEPTION("no matched account found...");
4207 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4211 /* Assign query result to structure */
4212 if (!(p_data_tbl = (emstorage_account_tbl_t *)malloc(sizeof(emstorage_account_tbl_t) * 1))) {
4213 EM_DEBUG_EXCEPTION("malloc failed...");
4214 error = EMAIL_ERROR_OUT_OF_MEMORY;
4218 memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * 1);
4221 if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4222 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_type), col_index++);
4223 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_address), 0, col_index++);
4224 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_email_address), 0, col_index++);
4225 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_user_name), 0, col_index++);
4226 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->retrieval_mode), col_index++);
4227 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_port_number), col_index++);
4228 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_secure_connection), col_index++);
4229 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_authentication_method), col_index++);
4230 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->outgoing_server_type), col_index++);
4231 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_address), 0, col_index++);
4232 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_port_number), col_index++);
4233 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_need_authentication), col_index++);
4234 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_secure_connection), col_index++);
4235 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_user_name), 0, col_index++);
4236 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_display_name), 0, col_index++);
4237 _get_stmt_field_data_string(hStmt, &(p_data_tbl->reply_to_address), 0, col_index++);
4238 _get_stmt_field_data_string(hStmt, &(p_data_tbl->return_address), 0, col_index++);
4239 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
4240 _get_stmt_field_data_int(hStmt, &(p_data_tbl->keep_mails_on_pop_server_after_download), col_index++);
4241 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_download_size), col_index++);
4242 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_interval), col_index++);
4243 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_days), col_index++);
4244 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_start_time), col_index++);
4245 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_end_time), col_index++);
4246 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_use_same_authenticator), col_index++);
4247 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_resend_times), col_index++);
4248 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_size_limit), col_index++);
4249 _get_stmt_field_data_int(hStmt, &(p_data_tbl->wifi_auto_download), col_index++);
4250 _get_stmt_field_data_int(hStmt, &(p_data_tbl->pop_before_smtp), col_index++);
4251 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_requires_apop), col_index++);
4252 _get_stmt_field_data_string(hStmt, &(p_data_tbl->logo_icon_path), 0, col_index++);
4253 _get_stmt_field_data_blob(hStmt, &p_data_tbl->user_data, col_index++);
4254 _get_stmt_field_data_int(hStmt, &p_data_tbl->user_data_length, col_index++);
4255 _get_stmt_field_data_int(hStmt, &p_data_tbl->color_label, col_index++);
4256 _get_stmt_field_data_int(hStmt, &(p_data_tbl->check_interval), col_index++);
4257 _get_stmt_field_data_int(hStmt, &(p_data_tbl->sync_status), col_index++);
4260 if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME)
4261 _get_stmt_field_data_string(hStmt, &(p_data_tbl->account_name), 0, col_index++);
4263 if (pulloption & EMAIL_ACC_GET_OPT_PASSWORD) {
4264 /* get password file name */
4265 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) {
4266 EM_DEBUG_EXCEPTION("_get_password_file_name failed [%d]", error);
4270 /* read password from secure storage */
4271 if ((error = _read_password_from_secure_storage(recv_password_file_name, &(p_data_tbl->incoming_server_password))) < 0) {
4272 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4276 EM_DEBUG_LOG_DEV("recv_password_file_name[%s], password[%s]", recv_password_file_name, p_data_tbl->incoming_server_password);
4278 if (p_data_tbl->outgoing_server_use_same_authenticator == 0) {
4279 if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl->outgoing_server_password))) < 0) {
4280 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4283 EM_DEBUG_LOG_DEV("send_password_file_name[%s], password[%s]", send_password_file_name, p_data_tbl->outgoing_server_password);
4287 if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4288 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.priority), col_index++);
4289 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.keep_local_copy), col_index++);
4290 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_delivery_receipt), col_index++);
4291 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_read_receipt), col_index++);
4292 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.download_limit), col_index++);
4293 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_address), col_index++);
4294 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_subject), col_index++);
4295 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.display_name_from), 0, col_index++);
4296 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.reply_with_body), col_index++);
4297 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.forward_with_files), col_index++);
4298 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_myname_card), col_index++);
4299 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_signature), col_index++);
4300 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.signature), 0, col_index++);
4301 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.add_my_address_to_bcc), col_index++);
4302 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.notification_status), col_index++);
4303 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.vibrate_status), col_index++);
4304 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.display_content_status), col_index++);
4305 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.default_ringtone_status), col_index++);
4306 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.alert_ringtone_path), 0, col_index++);
4307 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_svc_id), col_index++);
4308 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->sync_disabled), col_index++);
4309 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->default_mail_slot_size), col_index++);
4310 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->roaming_option), col_index++);
4311 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->smime_type), col_index++);
4312 _get_stmt_field_data_string(hStmt, &(p_data_tbl->certificate_path), 0, col_index++);
4313 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->cipher_type), col_index++);
4314 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->digest_type), col_index++);
4321 *account = p_data_tbl;
4324 emstorage_free_account((emstorage_account_tbl_t **)&p_data_tbl, 1, NULL);
4326 if (hStmt != NULL) {
4327 rc = sqlite3_finalize(hStmt);
4328 if (rc != SQLITE_OK) {
4329 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
4330 error = EMAIL_ERROR_DB_FAILURE;
4334 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4336 if (err_code != NULL)
4339 EM_DEBUG_FUNC_END("ret [%d]", ret);
4343 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)
4345 EM_DEBUG_FUNC_BEGIN("account_id[%d], password_length[%p], err_code[%p]", account_id, password_length, err_code);
4347 if (account_id <= 0 || password_length == NULL) {
4348 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4349 if (err_code != NULL)
4350 *err_code = EMAIL_ERROR_INVALID_PARAM;
4355 int error = EMAIL_ERROR_NONE;
4356 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4357 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4358 char *temp_password = NULL;
4361 /* get password file name */
4362 if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4363 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4367 /* read password from secure storage */
4368 if (password_type == EMAIL_GET_INCOMING_PASSWORD_LENGTH) {
4369 if ((error = _read_password_from_secure_storage(recv_password_file_name, &temp_password)) < 0 || !temp_password) {
4370 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
4373 } else if (password_type == EMAIL_GET_OUTGOING_PASSWORD_LENGTH) {
4374 if ((error = _read_password_from_secure_storage(send_password_file_name, &temp_password)) < 0 || !temp_password) {
4375 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
4379 EM_DEBUG_LOG("Invalid password type");
4380 if (err_code != NULL)
4381 *err_code = EMAIL_ERROR_INVALID_PARAM;
4385 *password_length = EM_SAFE_STRLEN(temp_password);
4387 EM_DEBUG_LOG_SEC("recv_password_file_name[%s], *password_length[%d]", recv_password_file_name, *password_length);
4392 EM_SAFE_FREE(temp_password);
4394 if (err_code != NULL)
4397 EM_DEBUG_FUNC_END("ret [%d]", ret);
4401 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)
4403 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);
4405 int err = EMAIL_ERROR_NONE;
4406 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4407 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4409 if (input_incoming_server_password == NULL && input_outgoing_server_password == NULL) {
4410 EM_DEBUG_EXCEPTION_SEC("Invalid param");
4411 err = EMAIL_ERROR_INVALID_PARAM;
4415 /* get password file name */
4416 if ((err = _get_password_file_name(multi_user_name,
4418 recv_password_file_name,
4419 send_password_file_name)) != EMAIL_ERROR_NONE) {
4420 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4424 EM_DEBUG_LOG_SEC("recv_password_file_name [%s] input_incoming_server_password [%s]",
4425 recv_password_file_name, input_incoming_server_password);
4426 EM_DEBUG_LOG_SEC("send_password_file_name [%s] input_outgoing_server_password [%s]",
4427 send_password_file_name, input_outgoing_server_password);
4429 if (input_incoming_server_password) {
4430 err = emcore_remove_password_in_key_manager(recv_password_file_name);
4431 if (err != EMAIL_ERROR_NONE) {
4432 EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", recv_password_file_name);
4436 /* save recv passwords to the secure storage */
4437 err = emcore_add_password_in_key_manager(recv_password_file_name, input_incoming_server_password);
4438 if (err != EMAIL_ERROR_NONE) {
4439 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4444 if (input_outgoing_server_password) {
4445 err = emcore_remove_password_in_key_manager(send_password_file_name);
4446 if (err != EMAIL_ERROR_NONE) {
4447 EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", send_password_file_name);
4451 /* save send passwords to the secure storage */
4452 err = emcore_add_password_in_key_manager(send_password_file_name, input_outgoing_server_password);
4453 if (err != EMAIL_ERROR_NONE) {
4454 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4460 EM_DEBUG_FUNC_END("err [%d]", err);
4464 INTERNAL_FUNC int emstorage_update_account(char *multi_user_name, int account_id, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
4466 EM_DEBUG_FUNC_BEGIN("account_id[%d], account[%p], transaction[%d], err_code[%p]", account_id, account_tbl, transaction, err_code);
4468 if (account_id < FIRST_ACCOUNT_ID || !account_tbl) {
4469 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4470 if (err_code != NULL)
4471 *err_code = EMAIL_ERROR_INVALID_PARAM;
4475 int error = EMAIL_ERROR_NONE;
4476 int rc, ret = false;
4478 DB_STMT hStmt = NULL;
4479 char sql_query_string[QUERY_SIZE] = {0, };
4481 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4483 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4485 SNPRINTF(sql_query_string, sizeof(sql_query_string),
4486 "UPDATE mail_account_tbl SET"
4488 ", logo_icon_path = ?"
4490 ", user_data_length = ?"
4491 ", account_svc_id = ?"
4493 ", sync_disabled = ?"
4494 ", default_mail_slot_size = ?"
4495 ", roaming_option = ?"
4497 ", user_display_name = ?"
4498 ", user_email_address = ?"
4499 ", reply_to_address = ?"
4500 ", return_address = ?"
4501 ", incoming_server_type = ?"
4502 ", incoming_server_address = ?"
4503 ", incoming_server_port_number = ?"
4504 ", incoming_server_user_name = ?"
4505 ", incoming_server_secure_connection = ?"
4506 ", incoming_server_authentication_method = ?"
4507 ", retrieval_mode = ?"
4508 ", keep_mails_on_pop_server_after_download = ?"
4509 ", check_interval = ?"
4510 ", auto_download_size = ?"
4511 ", peak_interval = ?"
4513 ", peak_start_time = ?"
4514 ", peak_end_time = ?"
4515 ", outgoing_server_type = ?"
4516 ", outgoing_server_address = ?"
4517 ", outgoing_server_port_number = ?"
4518 ", outgoing_server_user_name = ?"
4519 ", outgoing_server_secure_connection = ?"
4520 ", outgoing_server_need_authentication = ?"
4521 ", outgoing_server_use_same_authenticator = ?"
4523 ", keep_local_copy = ?"
4524 ", req_delivery_receipt = ?"
4525 ", req_read_receipt = ?"
4526 ", download_limit = ?"
4527 ", block_address = ?"
4528 ", block_subject = ?"
4529 ", display_name_from = ?"
4530 ", reply_with_body = ?"
4531 ", forward_with_files = ?"
4532 ", add_myname_card = ?"
4533 ", add_signature = ?"
4535 ", add_my_address_to_bcc = ?"
4536 ", notification_status = ?"
4537 ", vibrate_status = ?"
4538 ", display_content_status = ?"
4539 ", default_ringtone_status = ?"
4540 ", alert_ringtone_path = ?"
4541 ", auto_resend_times = ?"
4542 ", outgoing_server_size_limit = ?"
4543 ", wifi_auto_download = ?"
4544 ", pop_before_smtp = ?"
4545 ", incoming_server_requires_apop = ?"
4547 ", certificate_path = ?"
4551 " WHERE account_id = ?");
4553 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4554 EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
4555 EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
4556 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4557 ("sqlite3_prepare fail:(%d) %s", rc, sqlite3_errmsg(local_db_handle)));
4561 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4562 _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4563 _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4564 _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4565 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4566 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4567 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4568 _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4569 _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
4570 _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
4571 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4572 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4573 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4574 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4575 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4576 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4577 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
4578 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4579 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
4580 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
4581 _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
4582 _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
4583 _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
4584 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
4585 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
4586 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
4587 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
4588 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
4589 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
4590 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4591 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
4592 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
4593 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
4594 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
4595 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
4596 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
4597 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
4598 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
4599 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
4600 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
4601 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
4602 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
4603 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4604 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
4605 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
4606 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
4607 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
4608 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
4609 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
4610 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
4611 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
4612 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
4613 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
4614 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4615 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
4616 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
4617 _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
4618 _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
4619 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
4620 _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
4621 _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4622 _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
4623 _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
4624 _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4625 _bind_stmt_field_data_int(hStmt, i++, account_id);
4627 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4628 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
4629 ("sqlite3_step fail:%d", rc));
4630 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4631 ("sqlite3_step fail:%d", rc));
4633 /* validate account existence */
4634 rc = sqlite3_changes(local_db_handle);
4636 EM_DEBUG_EXCEPTION("no matched account found...");
4637 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4641 if (account_tbl->incoming_server_password || account_tbl->outgoing_server_password) {
4642 if ((error = emstorage_update_account_password(multi_user_name, account_id, account_tbl->incoming_server_password, account_tbl->outgoing_server_password)) != EMAIL_ERROR_NONE) {
4643 EM_DEBUG_EXCEPTION("emstorage_update_account_password failed [%d]", error);
4651 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4653 if (hStmt != NULL) {
4654 rc = sqlite3_finalize(hStmt);
4655 if (rc != SQLITE_OK) {
4656 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
4657 error = EMAIL_ERROR_DB_FAILURE;
4661 if (error == EMAIL_ERROR_NONE) {
4662 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_tbl->account_id, 0, NULL, 0))
4663 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_ACCOUNT_UPDATE] : Notification Failed >>> ");
4666 if (err_code != NULL)
4669 EM_DEBUG_FUNC_END("ret [%d]", ret);
4673 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)
4675 EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], field_name[%s], value[%d], transaction[%d]", account_id, field_name, value, transaction);
4676 int error = EMAIL_ERROR_NONE;
4678 char sql_query_string[QUERY_SIZE] = {0, };
4679 sqlite3 *local_db_handle = NULL;
4681 if (!account_id || !field_name) {
4682 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4683 return EMAIL_ERROR_INVALID_PARAM;
4686 local_db_handle = emstorage_get_db_connection(multi_user_name);
4688 /* Write query string */
4689 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_account_tbl SET %s = %d WHERE account_id = %d", field_name, value, account_id);
4691 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4694 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4696 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4697 if (error != EMAIL_ERROR_NONE) {
4698 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4702 if (sqlite3_changes(local_db_handle) == 0)
4703 EM_DEBUG_LOG("no mail matched...");
4707 result = (error == EMAIL_ERROR_NONE) ? true : false;
4708 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, error);
4710 if (error == EMAIL_ERROR_NONE) {
4711 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_id, 0, field_name, value))
4712 EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_ACCOUNT_UPDATE [%s,%d]", field_name, value);
4714 EM_DEBUG_FUNC_END("error [%d]", error);
4718 INTERNAL_FUNC int emstorage_get_sync_status_of_account(char *multi_user_name, int account_id, int *result_sync_status, int *err_code)
4720 EM_DEBUG_FUNC_BEGIN("account_id[%d], result_sync_status [%p], err_code[%p]", account_id, result_sync_status, err_code);
4722 if (!result_sync_status) {
4723 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4724 if (err_code != NULL)
4725 *err_code = EMAIL_ERROR_INVALID_PARAM;
4729 int error = EMAIL_ERROR_NONE, rc, ret = false, sync_status = 0, count, i, col_index;
4730 char sql_query_string[QUERY_SIZE] = {0, };
4731 char **result = NULL;
4732 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4735 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl WHERE account_id = %d", account_id);
4737 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl");
4739 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
4740 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
4741 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4744 EM_DEBUG_EXCEPTION("no matched account found...");
4745 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4750 *result_sync_status = 0;
4752 for (i = 0; i < count; i++) {
4753 _get_table_field_data_int(result, &sync_status, col_index++);
4754 *result_sync_status |= sync_status;
4757 EM_DEBUG_LOG("sync_status [%d]", sync_status);
4759 sqlite3_free_table(result);
4766 if (err_code != NULL)
4769 EM_DEBUG_FUNC_END("ret [%d]", ret);
4773 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)
4775 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);
4777 int error = EMAIL_ERROR_NONE, rc, ret = false, set_value = sync_status, result_sync_status;
4778 char sql_query_string[QUERY_SIZE] = {0, };
4779 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4781 if (set_operator != SET_TYPE_SET && account_id) {
4782 if (!emstorage_get_sync_status_of_account(multi_user_name, account_id, &result_sync_status, &error)) {
4783 EM_DEBUG_EXCEPTION("emstorage_get_sync_status_of_account failed [%d]", error);
4784 if (err_code != NULL)
4788 switch (set_operator) {
4789 case SET_TYPE_UNION:
4790 set_value = result_sync_status | set_value;
4792 case SET_TYPE_MINUS:
4793 set_value = result_sync_status & (~set_value);
4796 EM_DEBUG_EXCEPTION("EMAIL_ERROR_NOT_SUPPORTED [%d]", set_operator);
4797 error = EMAIL_ERROR_NOT_SUPPORTED;
4800 EM_DEBUG_LOG("set_value [%d]", set_value);
4803 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4806 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE account_id = %d", set_value, account_id);
4808 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE incoming_server_type <> 5", set_value);
4810 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4812 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4813 if (error != EMAIL_ERROR_NONE) {
4814 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4818 rc = sqlite3_changes(local_db_handle);
4821 EM_DEBUG_EXCEPTION("no matched account found...");
4822 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4829 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4831 if (error == EMAIL_ERROR_NONE) {
4832 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE_SYNC_STATUS, account_id, set_value, NULL, 0))
4833 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_UPDATE_SYNC_STATUS] : Notification failed");
4836 if (err_code != NULL)
4839 EM_DEBUG_FUNC_END("ret [%d]", ret);
4843 INTERNAL_FUNC int emstorage_add_account(char *multi_user_name, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
4845 EM_DEBUG_FUNC_BEGIN("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4848 EM_DEBUG_EXCEPTION("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4849 if (err_code != NULL)
4850 *err_code = EMAIL_ERROR_INVALID_PARAM;
4856 int rc = -1, ret = false;
4857 int error = EMAIL_ERROR_NONE;
4858 DB_STMT hStmt = NULL;
4859 char sql_query_string[QUERY_SIZE] = {0, };
4860 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4861 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4863 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4865 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4867 char *sql = "SELECT rowid FROM mail_account_tbl;";
4868 char **result = NULL;
4870 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, &row_count, NULL, NULL), rc);
4871 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
4872 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
4874 if (NULL == result[1]) rc = 1;
4876 for (i = 1; i <= row_count; i++) {
4877 if (i != atoi(result[i]))
4883 sqlite3_free_table(result);
4886 if (rc < 0 || rc > EMAIL_ACCOUNT_MAX) {
4887 EM_DEBUG_EXCEPTION("OVERFLOWED THE MAX ACCOUNT");
4888 error = EMAIL_ERROR_ACCOUNT_MAX_COUNT;
4892 account_tbl->account_id = rc;
4894 if ((error = _get_password_file_name(multi_user_name, account_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4895 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4899 EM_DEBUG_LOG(" >>>> ACCOUNT_ID [ %d ] ", account_tbl->account_id);
4900 SNPRINTF(sql_query_string, sizeof(sql_query_string),
4901 "INSERT INTO mail_account_tbl VALUES "
4903 " ? " /* account_id */
4904 " , ? " /* account_name */
4905 " , ? " /* logo_icon_path */
4906 " , ? " /* user_data */
4907 " , ? " /* user_data_length */
4908 " , ? " /* account_svc_id */
4909 " , ? " /* sync_status */
4910 " , ? " /* sync_disabled */
4911 " , ? " /* default_mail_slot_size */
4912 " , ? " /* roaming_option */
4913 " , ? " /* color_label */
4914 " , ? " /* user_display_name */
4915 " , ? " /* user_email_address */
4916 " , ? " /* reply_to_address */
4917 " , ? " /* return_address */
4918 " , ? " /* incoming_server_type */
4919 " , ? " /* incoming_server_address */
4920 " , ? " /* incoming_server_port_number */
4921 " , ? " /* incoming_server_user_name */
4922 " , ? " /* incoming_server_password */
4923 " , ? " /* incoming_server_secure_connection */
4924 " , ? " /* incoming_server_authentication_method */
4925 " , ? " /* retrieval_mode */
4926 " , ? " /* keep_mails_on_pop_server_after_download */
4927 " , ? " /* check_interval */
4928 " , ? " /* auto_download_size */
4929 " , ? " /* peak_interval */
4930 " , ? " /* peak_days */
4931 " , ? " /* peak_start_time */
4932 " , ? " /* peak_end_time */
4933 " , ? " /* outgoing_server_type */
4934 " , ? " /* outgoing_server_address */
4935 " , ? " /* outgoing_server_port_number */
4936 " , ? " /* outgoing_server_user_name */
4937 " , ? " /* outgoing_server_password */
4938 " , ? " /* outgoing_server_secure_connection */
4939 " , ? " /* outgoing_server_need_authentication */
4940 " , ? " /* outgoing_server_use_same_authenticator */
4941 " , ? " /* priority */
4942 " , ? " /* keep_local_copy */
4943 " , ? " /* req_delivery_receipt */
4944 " , ? " /* req_read_receipt */
4945 " , ? " /* download_limit */
4946 " , ? " /* block_address */
4947 " , ? " /* block_subject */
4948 " , ? " /* display_name_from */
4949 " , ? " /* reply_with_body */
4950 " , ? " /* forward_with_files */
4951 " , ? " /* add_myname_card */
4952 " , ? " /* add_signature */
4953 " , ? " /* signature */
4954 " , ? " /* add_my_address_to_bcc */
4955 " , ? " /* auto_resend_times */
4956 " , ? " /* outgoing_server_size_limit */
4957 " , ? " /* wifi_auto_download */
4958 " , ? " /* pop_before_smtp */
4959 " , ? " /* incoming_server_requires_apop */
4960 " , ? " /* smime_type */
4961 " , ? " /* certificate_path */
4962 " , ? " /* cipher_type */
4963 " , ? " /* digest_type */
4964 " , ? " /* notification_status */
4965 " , ? " /* vibrate_status */
4966 " , ? " /* display_content_status */
4967 " , ? " /* default_ringtone_status */
4968 " , ? " /* alert_ringtone_path */
4969 " , ? " /* user_name */
4972 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4973 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4974 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4978 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_id);
4979 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4980 _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4981 _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4982 _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4983 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4984 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4985 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4986 _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4987 _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
4988 _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
4989 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4990 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4991 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4992 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4993 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4994 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4995 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
4996 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4997 _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
4998 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
4999 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
5000 _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
5001 _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
5002 _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
5003 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
5004 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
5005 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
5006 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
5007 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
5008 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
5009 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5010 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
5011 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
5012 _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
5013 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
5014 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
5015 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
5016 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
5017 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
5018 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
5019 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
5020 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
5021 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
5022 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
5023 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
5024 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
5025 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
5026 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
5027 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
5028 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
5029 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
5030 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
5031 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
5032 _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
5033 _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
5034 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
5035 _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
5036 _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
5037 _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
5038 _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
5039 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
5040 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
5041 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
5042 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
5043 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5044 _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5046 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5048 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
5049 ("sqlite3_step fail:%d", rc));
5050 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5051 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
5054 /* save passwords to the secure storage */
5055 EM_DEBUG_LOG_SEC("save to the secure storage : recv_file[%s], send_file[%s]", recv_password_file_name, send_password_file_name);
5056 error = emcore_add_password_in_key_manager(recv_password_file_name, account_tbl->incoming_server_password);
5057 if (error != EMAIL_ERROR_NONE) {
5058 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
5062 error = emcore_add_password_in_key_manager(send_password_file_name, account_tbl->outgoing_server_password);
5063 if (error != EMAIL_ERROR_NONE) {
5064 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
5071 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5073 if (hStmt != NULL) {
5074 rc = sqlite3_finalize(hStmt);
5075 if (rc != SQLITE_OK) {
5076 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5077 error = EMAIL_ERROR_DB_FAILURE;
5081 if (error == EMAIL_ERROR_NONE) {
5082 if (!emcore_notify_storage_event(NOTI_ACCOUNT_ADD, account_tbl->account_id, 0, NULL, 0))
5083 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_ADD] : Notification failed");
5086 if (err_code != NULL)
5089 EM_DEBUG_FUNC_END("ret [%d]", ret);
5093 INTERNAL_FUNC int emstorage_delete_account(char *multi_user_name, int account_id, int transaction, int *err_code)
5095 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
5097 if (account_id < FIRST_ACCOUNT_ID) {
5098 EM_DEBUG_EXCEPTION(" account_id[%d]", account_id);
5100 if (err_code != NULL)
5101 *err_code = EMAIL_ERROR_INVALID_PARAM;
5105 int rc = -1, ret = false;
5106 int error = EMAIL_ERROR_NONE;
5108 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5110 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5112 /* TODO : delete password files - file names can be obtained from db or a rule that makes a name */
5113 char sql_query_string[QUERY_SIZE] = {0, };
5114 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5115 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5117 /* get password file name */
5118 if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
5119 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
5123 /* delete from db */
5124 memset(sql_query_string, 0x00, sizeof(sql_query_string));
5125 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_account_tbl WHERE account_id = %d", account_id);
5126 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5127 if (error != EMAIL_ERROR_NONE) {
5128 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5132 /* validate account existence */
5133 rc = sqlite3_changes(local_db_handle);
5135 EM_DEBUG_EXCEPTION(" no matched account found...");
5136 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
5140 /* delete from secure storage */
5141 error = emcore_remove_password_in_key_manager(recv_password_file_name);
5142 if (error != EMAIL_ERROR_NONE) {
5143 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5147 error = emcore_remove_password_in_key_manager(send_password_file_name);
5148 if (error != EMAIL_ERROR_NONE) {
5149 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5156 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5158 if (err_code != NULL)
5161 EM_DEBUG_FUNC_END("ret [%d]", ret);
5166 INTERNAL_FUNC int emstorage_free_account(emstorage_account_tbl_t** account_list, int count, int *err_code)
5168 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%d], err_code[%p]", account_list, count, err_code);
5171 int error = EMAIL_ERROR_NONE;
5174 if (!account_list || !*account_list) {
5175 EM_DEBUG_EXCEPTION("account_list[%p], count[%d]", account_list, count);
5176 error = EMAIL_ERROR_INVALID_PARAM;
5180 emstorage_account_tbl_t* p = *account_list;
5183 for (; i < count; i++) {
5184 EM_SAFE_FREE(p[i].account_name);
5185 EM_SAFE_FREE(p[i].incoming_server_address);
5186 EM_SAFE_FREE(p[i].user_email_address);
5187 EM_SAFE_FREE(p[i].user_data);
5188 EM_SAFE_FREE(p[i].incoming_server_user_name);
5189 EM_SAFE_FREE(p[i].incoming_server_password);
5190 EM_SAFE_FREE(p[i].outgoing_server_address);
5191 EM_SAFE_FREE(p[i].outgoing_server_user_name);
5192 EM_SAFE_FREE(p[i].outgoing_server_password);
5193 EM_SAFE_FREE(p[i].user_display_name);
5194 EM_SAFE_FREE(p[i].reply_to_address);
5195 EM_SAFE_FREE(p[i].return_address);
5196 EM_SAFE_FREE(p[i].logo_icon_path);
5197 EM_SAFE_FREE(p[i].options.display_name_from);
5198 EM_SAFE_FREE(p[i].options.signature);
5199 EM_SAFE_FREE(p[i].options.alert_ringtone_path);
5200 EM_SAFE_FREE(p[i].certificate_path);
5201 EM_SAFE_FREE(p[i].user_name);
5205 *account_list = NULL;
5211 if (err_code != NULL)
5214 EM_DEBUG_FUNC_END("ret [%d]", ret);
5218 INTERNAL_FUNC int emstorage_get_mailbox_count(char *multi_user_name, int account_id, int local_yn, int *count, int transaction, int *err_code)
5220 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);
5222 if ((account_id < FIRST_ACCOUNT_ID) || (count == NULL)) {
5223 EM_DEBUG_EXCEPTION(" account_list[%d], local_yn[%d], count[%p]", account_id, local_yn, count);
5225 if (err_code != NULL)
5226 *err_code = EMAIL_ERROR_INVALID_PARAM;
5230 int rc = -1, ret = false;
5231 int error = EMAIL_ERROR_NONE;
5232 char sql_query_string[QUERY_SIZE] = {0, };
5234 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5235 EMSTORAGE_START_READ_TRANSACTION(transaction);
5237 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);
5241 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5242 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
5243 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5245 *count = atoi(result[1]);
5246 sqlite3_free_table(result);
5253 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5255 if (err_code != NULL)
5258 EM_DEBUG_FUNC_END("ret [%d]", ret);
5262 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)
5264 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);
5266 if (!select_num || !mailbox_list) {
5267 EM_DEBUG_EXCEPTION("Invalid parameters");
5269 if (err_code != NULL)
5270 *err_code = EMAIL_ERROR_INVALID_PARAM;
5276 int error = EMAIL_ERROR_NONE;
5277 char conditional_clause_string[QUERY_SIZE] = {0, };
5278 char ordering_clause_string[QUERY_SIZE] = {0, };
5280 if (account_id == ALL_ACCOUNT) {
5281 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5282 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);
5284 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d ", account_id);
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), " AND local_yn = %d ", local_yn);
5289 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5291 switch (sort_type) {
5292 case EMAIL_MAILBOX_SORT_BY_NAME_ASC:
5293 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name ASC");
5296 case EMAIL_MAILBOX_SORT_BY_NAME_DSC:
5297 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name DESC");
5300 case EMAIL_MAILBOX_SORT_BY_TYPE_ASC:
5301 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type ASC");
5304 case EMAIL_MAILBOX_SORT_BY_TYPE_DSC:
5305 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type DEC");
5309 EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5311 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5312 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5320 if (err_code != NULL)
5323 EM_DEBUG_FUNC_END("ret [%d]", ret);
5327 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)
5329 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);
5332 int error = EMAIL_ERROR_NONE;
5333 int where_clause_count = 0;
5334 char conditional_clause_string[QUERY_SIZE] = {0, };
5335 char ordering_clause_string[QUERY_SIZE] = {0, };
5337 if (!select_num || !mailbox_list) {
5338 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5339 error = EMAIL_ERROR_INVALID_PARAM;
5343 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL) {
5344 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);
5345 where_clause_count++;
5348 if (account_id > 0) {
5349 if (where_clause_count == 0) {
5350 SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " WHERE ");
5351 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);
5353 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);
5356 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY CASE WHEN MBT.mailbox_name"
5357 " GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\'"
5358 " THEN 2 ELSE 1 END ASC,"
5359 " MBT.mailbox_name COLLATE CONVERTUTF8 ASC ");
5360 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5361 EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5363 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 1, 1, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5364 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5372 if (err_code != NULL)
5375 EM_DEBUG_FUNC_END("ret [%d]", ret);
5379 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)
5381 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);
5384 int error = EMAIL_ERROR_NONE;
5385 char conditional_clause_string[QUERY_SIZE] = {0, };
5387 if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list || !parent_mailbox_name) {
5388 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);
5389 if (err_code != NULL)
5390 *err_code = EMAIL_ERROR_INVALID_PARAM;
5394 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);
5395 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5397 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, " ORDER BY mailbox_name DESC ", 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5398 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5407 if (err_code != NULL)
5410 EM_DEBUG_FUNC_END("ret [%d]", ret);
5414 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)
5416 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);
5417 if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list) {
5418 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5420 if (err_code != NULL)
5421 *err_code = EMAIL_ERROR_INVALID_PARAM;
5427 int error = EMAIL_ERROR_NONE;
5428 char conditional_clause_string[QUERY_SIZE] = {0, };
5430 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND modifiable_yn = 0", account_id);
5431 EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
5433 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) {
5434 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5442 if (err_code != NULL)
5445 EM_DEBUG_FUNC_END("ret [%d]", ret);
5449 INTERNAL_FUNC int emstorage_stamp_last_sync_time_of_mailbox(char *multi_user_name, int input_mailbox_id, int input_transaction)
5451 EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_transaction [%d]", input_mailbox_id, input_transaction);
5453 int result_code = false;
5454 int error = EMAIL_ERROR_NONE;
5455 time_t current_time = 0;
5456 char sql_query_string[QUERY_SIZE] = {0, };
5457 sqlite3 *local_db_handle = NULL;
5459 if (!input_mailbox_id) {
5460 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5461 return EMAIL_ERROR_INVALID_PARAM;
5464 time(¤t_time);
5466 local_db_handle = emstorage_get_db_connection(multi_user_name);
5467 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
5469 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5470 "UPDATE mail_box_tbl SET"
5471 " last_sync_time = %d"
5472 " WHERE mailbox_id = %d"
5474 , input_mailbox_id);
5476 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
5477 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5478 if (error != EMAIL_ERROR_NONE) {
5479 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5485 if (error == EMAIL_ERROR_NONE)
5488 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, result_code, error);
5490 EM_DEBUG_FUNC_END("error [%d]", error);
5494 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)
5496 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);
5497 EM_PROFILE_BEGIN(profile_emstorage_get_mailbox_by_name);
5499 if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !result_mailbox) {
5500 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], mailbox_name[%s], result_mailbox[%p]", account_id, local_yn, mailbox_name, result_mailbox);
5502 if (err_code != NULL)
5503 *err_code = EMAIL_ERROR_INVALID_PARAM;
5508 int error = EMAIL_ERROR_NONE;
5509 int result_count = 0;
5510 char conditional_clause_string[QUERY_SIZE] = {0, };
5512 if (strcmp(mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
5513 if (!(*result_mailbox = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t)))) {
5514 EM_DEBUG_EXCEPTION("malloc failed...");
5515 error = EMAIL_ERROR_OUT_OF_MEMORY;
5519 (*result_mailbox)->mailbox_id = 0;
5520 (*result_mailbox)->account_id = account_id;
5521 (*result_mailbox)->local_yn = 1;
5522 (*result_mailbox)->mailbox_name = EM_SAFE_STRDUP(mailbox_name);
5523 (*result_mailbox)->mailbox_type = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;
5524 (*result_mailbox)->alias = EM_SAFE_STRDUP(mailbox_name);
5525 (*result_mailbox)->deleted_flag = 0;
5526 (*result_mailbox)->modifiable_yn = 1;
5527 (*result_mailbox)->total_mail_count_on_server = 1;
5528 (*result_mailbox)->has_archived_mails = 0;
5529 (*result_mailbox)->mail_slot_size = 0x0FFFFFFF;
5530 (*result_mailbox)->no_select = 0;
5533 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string, "WHERE account_id = %d AND mailbox_name = '%q'", account_id, mailbox_name);
5535 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);
5537 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5539 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5540 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5549 if (err_code != NULL)
5552 EM_PROFILE_END(profile_emstorage_get_mailbox_by_name);
5553 EM_DEBUG_FUNC_END("ret [%d]", ret);
5557 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)
5559 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);
5561 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_USER_DEFINED) || !output_mailbox) {
5562 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_type[%d], output_mailbox[%p]", account_id, mailbox_type, output_mailbox);
5563 if (err_code != NULL)
5564 *err_code = EMAIL_ERROR_INVALID_PARAM;
5569 int error = EMAIL_ERROR_NONE;
5570 int result_count = 0;
5571 emstorage_mailbox_tbl_t *result_mailbox = NULL;
5572 char conditional_clause_string[QUERY_SIZE] = {0,};
5575 SNPRINTF(conditional_clause_string, QUERY_SIZE, "WHERE account_id = %d AND mailbox_type = %d ", account_id, mailbox_type);
5577 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5579 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", true, false, &result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5580 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl error [%d]", error);
5588 *output_mailbox = result_mailbox;
5590 if (err_code != NULL)
5593 EM_DEBUG_FUNC_END("ret [%d]", ret);
5597 INTERNAL_FUNC int emstorage_get_mailbox_by_id(char *multi_user_name, int input_mailbox_id, emstorage_mailbox_tbl_t** output_mailbox)
5599 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], output_mailbox[%p]", input_mailbox_id, output_mailbox);
5601 if (input_mailbox_id <= 0 || !output_mailbox) {
5602 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: input_mailbox_id[%d] output_mailbox[%p]", input_mailbox_id, output_mailbox);
5603 return EMAIL_ERROR_INVALID_PARAM;
5606 int ret = EMAIL_ERROR_NONE;
5607 int result_count = 0;
5608 char conditional_clause_string[QUERY_SIZE] = {0, };
5610 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE MBT.mailbox_id = %d", input_mailbox_id);
5612 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5614 if ((ret = emstorage_query_mailbox_tbl(multi_user_name,
5615 conditional_clause_string,
5616 "", true, false, output_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5617 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", ret);
5623 EM_DEBUG_FUNC_END("ret [%d]", ret);
5627 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)
5629 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);
5631 if (account_id < 0 || !keyword || !result_mailbox) {
5632 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], keyword[%s], result_mailbox[%p]", account_id, keyword, result_mailbox);
5634 if (err_code != NULL)
5635 *err_code = EMAIL_ERROR_INVALID_PARAM;
5640 int error = EMAIL_ERROR_NONE;
5641 char conditional_clause_string[QUERY_SIZE] = {0, };
5643 if (account_id == 0)
5644 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5645 "WHERE alias LIKE \'%%%q%%\'", keyword);
5646 else if (account_id > 0)
5647 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5648 "WHERE account_id = %d AND alias LIKE \'%%%q%%\'", account_id, keyword);
5650 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5652 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, result_count)) != EMAIL_ERROR_NONE) {
5653 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5661 if (err_code != NULL)
5664 EM_DEBUG_FUNC_END("ret [%d]", ret);
5668 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)
5670 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);
5671 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_id) {
5672 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_id[%p]", account_id, mailbox_type, mailbox_id);
5673 if (err_code != NULL)
5674 *err_code = EMAIL_ERROR_INVALID_PARAM;
5678 int rc, ret = false;
5679 int error = EMAIL_ERROR_NONE;
5680 emstorage_account_tbl_t* account = NULL;
5681 DB_STMT hStmt = NULL;
5682 char sql_query_string[QUERY_SIZE] = {0, };
5684 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5685 EMSTORAGE_START_READ_TRANSACTION(transaction);
5687 /* Check whether the account exists. */
5688 if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME, &account, true, &error) || !account) {
5689 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5694 emstorage_free_account(&account, 1, NULL);
5696 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);
5698 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5701 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5703 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5704 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5707 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5708 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5709 ("sqlite3_step fail:%d", rc));
5711 if (rc == SQLITE_DONE) {
5712 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5713 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5717 _get_stmt_field_data_int(hStmt, mailbox_id, 0);
5722 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5723 if (hStmt != NULL) {
5724 rc = sqlite3_finalize(hStmt);
5725 if (rc != SQLITE_OK) {
5726 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5727 error = EMAIL_ERROR_DB_FAILURE;
5731 if (err_code != NULL)
5734 EM_DEBUG_FUNC_END("ret [%d]", ret);
5738 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)
5740 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);
5741 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_name) {
5742 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
5743 if (err_code != NULL)
5744 *err_code = EMAIL_ERROR_INVALID_PARAM;
5748 int rc, ret = false;
5749 int error = EMAIL_ERROR_NONE;
5750 emstorage_account_tbl_t* account = NULL;
5751 DB_STMT hStmt = NULL;
5752 char sql_query_string[QUERY_SIZE] = {0, };
5754 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5755 EMSTORAGE_START_READ_TRANSACTION(transaction);
5757 /* Check whether the account exists. */
5758 if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME, &account, true, &error) || !account) {
5759 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5764 emstorage_free_account(&account, 1, NULL);
5766 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);
5768 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5771 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5773 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5774 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5777 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5778 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5779 ("sqlite3_step fail:%d", rc));
5781 if (rc == SQLITE_DONE) {
5782 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5783 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5787 _get_stmt_field_data_string(hStmt, mailbox_name, 0, 0);
5792 if (hStmt != NULL) {
5793 rc = sqlite3_finalize(hStmt);
5794 if (rc != SQLITE_OK) {
5795 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5796 error = EMAIL_ERROR_DB_FAILURE;
5800 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5802 if (err_code != NULL)
5805 EM_DEBUG_FUNC_END("ret [%d]", ret);
5809 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)
5811 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);
5813 int error = EMAIL_ERROR_NONE;
5814 char sql_query_string[QUERY_SIZE] = {0, };
5815 char *replaced_mailbox_name = NULL;
5818 if (strstr(mailbox_name, "'"))
5819 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5821 replaced_mailbox_name = strdup(mailbox_name);
5824 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5826 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5828 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5829 "UPDATE mail_box_tbl SET"
5830 " modifiable_yn = %d"
5831 " WHERE account_id = %d"
5832 " AND local_yn = %d"
5833 " AND mailbox_name = '%s'"
5837 , replaced_mailbox_name);
5839 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5840 if (error != EMAIL_ERROR_NONE) {
5841 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5848 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5850 EM_SAFE_FREE(replaced_mailbox_name);
5852 if (err_code != NULL)
5855 EM_DEBUG_FUNC_END("ret [%d]", ret);
5860 INTERNAL_FUNC int emstorage_update_mailbox_total_count(char *multi_user_name,
5862 int input_mailbox_id,
5863 int total_count_on_server,
5867 EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], total_count_on_server[%d], "
5868 "transaction[%d], err_code[%p]",
5869 account_id, input_mailbox_id, total_count_on_server, transaction, err_code);
5872 int error = EMAIL_ERROR_NONE;
5873 char sql_query_string[QUERY_SIZE] = {0, };
5875 if (account_id <= 0 || input_mailbox_id <= 0) {
5876 EM_DEBUG_EXCEPTION("account_id[%d], input_mailbox_id[%d]", account_id, input_mailbox_id);
5877 if (err_code != NULL)
5878 *err_code = EMAIL_ERROR_INVALID_PARAM;
5879 EM_DEBUG_FUNC_END("ret [%d]", ret);
5883 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5885 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5887 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5888 "UPDATE mail_box_tbl SET"
5889 " total_mail_count_on_server = %d"
5890 " WHERE account_id = %d"
5891 " AND mailbox_id = %d"
5892 , total_count_on_server
5894 , input_mailbox_id);
5896 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
5897 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5898 if (error != EMAIL_ERROR_NONE) {
5899 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5907 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5909 if (err_code != NULL)
5912 EM_DEBUG_FUNC_END("ret [%d]", ret);
5917 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)
5919 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);
5921 if (account_id < FIRST_ACCOUNT_ID || input_mailbox_id <= 0 || !result_mailbox) {
5922 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);
5924 if (err_code != NULL)
5925 *err_code = EMAIL_ERROR_INVALID_PARAM;
5926 EM_DEBUG_FUNC_END("ret [%d]", EMAIL_ERROR_INVALID_PARAM);
5930 int rc, ret = false;
5931 int error = EMAIL_ERROR_NONE;
5932 char sql_query_string[QUERY_SIZE] = {0, };
5933 DB_STMT hStmt = NULL;
5936 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5937 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5939 if (local_yn != -1) {
5940 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5941 "UPDATE mail_box_tbl SET"
5943 ", mailbox_name = ?"
5944 ", mailbox_type = ?"
5946 ", deleted_flag = ?"
5947 ", modifiable_yn= ?"
5948 ", mail_slot_size= ?"
5949 ", total_mail_count_on_server = ?"
5950 " WHERE account_id = %d"
5951 " AND local_yn = %d"
5952 " AND mailbox_id = '%d'"
5955 , input_mailbox_id);
5957 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5958 "UPDATE mail_box_tbl SET"
5960 ", mailbox_name = ?"
5961 ", mailbox_type = ?"
5963 ", deleted_flag = ?"
5964 ", modifiable_yn= ?"
5965 ", mail_slot_size= ?"
5966 ", total_mail_count_on_server = ?"
5967 " WHERE account_id = %d"
5968 " AND mailbox_id = '%d'"
5970 , input_mailbox_id);
5975 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5976 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5977 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5979 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_id);
5980 _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);
5981 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_type);
5982 _bind_stmt_field_data_string(hStmt, i++, (char *)result_mailbox->alias ? result_mailbox->alias : "", 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
5983 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->deleted_flag);
5984 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->modifiable_yn);
5985 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mail_slot_size);
5986 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->total_mail_count_on_server);
5989 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5990 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
5991 ("sqlite3_step fail:%d", rc));
5992 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5993 ("sqlite3_step fail:%d", rc));
5998 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6000 if (hStmt != NULL) {
6001 rc = sqlite3_finalize(hStmt);
6002 if (rc != SQLITE_OK) {
6003 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6004 error = EMAIL_ERROR_DB_FAILURE;
6008 if (err_code != NULL)
6011 EM_DEBUG_FUNC_END("ret [%d]", ret);
6015 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)
6017 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);
6019 if (account_id < FIRST_ACCOUNT_ID) {
6020 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
6022 if (err_code != NULL)
6023 *err_code = EMAIL_ERROR_INVALID_PARAM;
6027 int rc, ret = false;
6028 int error = EMAIL_ERROR_NONE;
6029 char sql_query_string[QUERY_SIZE] = {0, };
6031 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6032 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6034 EM_DEBUG_LOG("emstorage_update_mailbox_type");
6036 DB_STMT hStmt_box_tbl = NULL;
6037 DB_STMT hStmt_mail_tbl = NULL;
6040 /* Update mail_box_tbl */
6041 if (local_yn != -1) {
6042 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
6043 "UPDATE mail_box_tbl SET"
6045 " WHERE account_id = %d"
6046 " AND local_yn = %d"
6047 " AND mailbox_id = '%d'"
6050 , input_mailbox_id);
6052 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
6053 "UPDATE mail_box_tbl SET"
6055 " WHERE account_id = %d"
6056 " AND mailbox_id = '%d'"
6058 , input_mailbox_id);
6061 EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
6063 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_box_tbl, NULL), rc);
6065 if (SQLITE_OK != rc) {
6066 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6067 error = EMAIL_ERROR_DB_FAILURE;
6071 _bind_stmt_field_data_int(hStmt_box_tbl, i++, new_mailbox_type);
6074 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_box_tbl), rc);
6076 if (rc == SQLITE_FULL) {
6077 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6078 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
6082 if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
6083 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6084 error = EMAIL_ERROR_DB_FAILURE;
6089 /* Update mail_tbl */
6091 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6092 "UPDATE mail_tbl SET"
6094 " WHERE account_id = %d"
6095 " AND mailbox_id = '%d'"
6097 , input_mailbox_id);
6099 EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
6101 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_mail_tbl, NULL), rc);
6102 if (SQLITE_OK != rc) {
6103 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6104 error = EMAIL_ERROR_DB_FAILURE;
6108 _bind_stmt_field_data_int(hStmt_mail_tbl, i++, new_mailbox_type);
6110 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_mail_tbl), rc);
6111 if (rc == SQLITE_FULL) {
6112 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6113 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
6117 if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
6118 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6119 error = EMAIL_ERROR_DB_FAILURE;
6126 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6128 if (hStmt_box_tbl != NULL) {
6129 rc = sqlite3_finalize(hStmt_box_tbl);
6130 if (rc != SQLITE_OK) {
6131 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6132 error = EMAIL_ERROR_DB_FAILURE;
6136 if (hStmt_mail_tbl != NULL) {
6137 rc = sqlite3_finalize(hStmt_mail_tbl);
6138 if (rc != SQLITE_OK) {
6139 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6140 error = EMAIL_ERROR_DB_FAILURE;
6144 if (err_code != NULL)
6147 EM_DEBUG_FUNC_END("ret [%d]", ret);
6151 INTERNAL_FUNC int emstorage_set_local_mailbox(char *multi_user_name, int input_mailbox_id, int input_is_local_mailbox, int transaction)
6153 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);
6155 int rc, ret = false;
6156 int error = EMAIL_ERROR_NONE;
6157 char sql_query_string[QUERY_SIZE] = {0, };
6159 if (input_mailbox_id < 0) {
6160 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6161 return EMAIL_ERROR_INVALID_PARAM;
6164 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6166 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6168 EM_DEBUG_LOG("emstorage_update_mailbox_type");
6170 DB_STMT hStmt = NULL;
6173 /* Update mail_box_tbl */
6174 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6175 "UPDATE mail_box_tbl SET"
6177 " WHERE mailbox_id = %d"
6178 , input_mailbox_id);
6180 EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
6182 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6183 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6184 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6186 _bind_stmt_field_data_int(hStmt, i++, input_is_local_mailbox);
6188 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6189 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
6190 ("sqlite3_step fail:%d", rc));
6191 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6192 ("sqlite3_step fail:%d", rc));
6194 if (hStmt != NULL) {
6195 rc = sqlite3_finalize(hStmt);
6196 if (rc != SQLITE_OK) {
6197 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6198 error = EMAIL_ERROR_DB_FAILURE;
6206 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6208 if (hStmt != NULL) {
6209 rc = sqlite3_finalize(hStmt);
6210 if (rc != SQLITE_OK) {
6211 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6212 error = EMAIL_ERROR_DB_FAILURE;
6216 EM_DEBUG_FUNC_END("error [%d]", error);
6220 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)
6222 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);
6224 int err = EMAIL_ERROR_NONE;
6226 int cur_mailbox_id_string = 0;
6227 int mailbox_id_string_buffer_length = 0;
6228 char sql_query_string[QUERY_SIZE] = {0, };
6229 char *mailbox_id_string_buffer = NULL;
6230 char *parameter_string = NULL;
6231 sqlite3 *local_db_handle = NULL;
6233 if (input_mailbox_id_array == NULL || input_mailbox_id_count == 0 || input_field_name == NULL) {
6234 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6235 return EMAIL_ERROR_INVALID_PARAM;
6238 local_db_handle = emstorage_get_db_connection(multi_user_name);
6240 /* Generating mail id list string */
6241 mailbox_id_string_buffer_length = MAILBOX_ID_STRING_LENGTH * input_mailbox_id_count;
6243 mailbox_id_string_buffer = em_malloc(mailbox_id_string_buffer_length);
6245 if (!mailbox_id_string_buffer) {
6246 EM_DEBUG_EXCEPTION("em_mallocfailed");
6247 err = EMAIL_ERROR_OUT_OF_MEMORY;
6251 for (i = 0; i < input_mailbox_id_count; i++)
6252 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]);
6254 if (EM_SAFE_STRLEN(mailbox_id_string_buffer) > 1)
6255 mailbox_id_string_buffer[EM_SAFE_STRLEN(mailbox_id_string_buffer) - 1] = NULL_CHAR;
6257 /* Generating notification parameter string */
6258 parameter_string = em_malloc(mailbox_id_string_buffer_length + EM_SAFE_STRLEN(input_field_name) + 2);
6260 if (!parameter_string) {
6261 EM_DEBUG_EXCEPTION("em_mallocfailed");
6262 err = EMAIL_ERROR_OUT_OF_MEMORY;
6266 SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", input_field_name, 0x01, mailbox_id_string_buffer);
6268 /* Write query string */
6269 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);
6271 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
6274 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
6275 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6276 if (err != EMAIL_ERROR_NONE) {
6277 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
6281 if (sqlite3_changes(local_db_handle) == 0)
6282 EM_DEBUG_LOG("no mail matched...");
6287 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, err);
6289 if (err == EMAIL_ERROR_NONE && parameter_string) {
6290 if (!emcore_notify_storage_event(NOTI_MAILBOX_FIELD_UPDATE, input_account_id, 0, parameter_string, input_value))
6291 EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_MAILBOX_FIELD_UPDATE [%s,%d]",
6292 input_field_name, input_value);
6295 EM_SAFE_FREE(mailbox_id_string_buffer);
6296 EM_SAFE_FREE(parameter_string);
6298 EM_DEBUG_FUNC_END("err [%d]", err);
6302 INTERNAL_FUNC int emstorage_add_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox_tbl, int transaction, int *err_code)
6304 EM_DEBUG_FUNC_BEGIN("mailbox_tbl[%p], transaction[%d], err_code[%p]", mailbox_tbl, transaction, err_code);
6307 if (err_code != NULL)
6308 *err_code = EMAIL_ERROR_INVALID_PARAM;
6312 int rc, ret = false;
6313 int error = EMAIL_ERROR_NONE;
6314 DB_STMT hStmt = NULL;
6315 char sql_query_string[QUERY_SIZE] = {0,};
6316 char **result = NULL;
6317 time_t current_time;
6318 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6319 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6321 EM_SAFE_STRNCPY(sql_query_string, "SELECT max(rowid) FROM mail_box_tbl;", sizeof(sql_query_string));
6323 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6324 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6325 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6327 time(¤t_time);
6329 if (NULL == result[1])
6332 rc = atoi(result[1]) + 1;
6333 sqlite3_free_table(result);
6335 memset(sql_query_string, 0, sizeof(char) * QUERY_SIZE);
6337 mailbox_tbl->mailbox_id = rc;
6339 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6340 "INSERT INTO mail_box_tbl VALUES "
6341 "(?" /* mailbox_id */
6342 ", ?" /* account_id */
6343 ", ?" /* local_yn */
6344 ", ?" /* mailbox_name */
6345 ", ?" /* mailbox_type */
6347 ", ?" /* deleted_flag */
6348 ", ?" /* modifiable_yn */
6349 ", ?" /* total_mail_count_on_server */
6350 ", ?" /* has_archived_mails */
6351 ", ?" /* mail_slot_size */
6352 ", ?" /* no_select */
6353 ", ?" /* last_sync_time */
6354 ", ?" /* eas_data_length */
6355 ", ?" /* eas_data */
6359 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6360 EM_DEBUG_LOG_DEV("After sqlite3_prepare hStmt = %p", hStmt);
6361 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6362 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6366 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_id);
6367 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->account_id);
6368 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->local_yn);
6369 _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
6370 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_type);
6371 _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->alias, 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
6372 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->deleted_flag);
6373 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->modifiable_yn);
6374 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->total_mail_count_on_server);
6375 _bind_stmt_field_data_int(hStmt, col_index++, 0);
6376 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mail_slot_size);
6377 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->no_select);
6378 _bind_stmt_field_data_int(hStmt, col_index++, current_time);
6379 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->eas_data_length);
6380 _bind_stmt_field_data_blob(hStmt, col_index++, (void*)mailbox_tbl->eas_data, mailbox_tbl->eas_data_length);
6383 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6384 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6385 ("sqlite3_step fail:%dn", rc));
6390 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6391 if (hStmt != NULL) {
6392 rc = sqlite3_finalize(hStmt);
6393 if (rc != SQLITE_OK) {
6394 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6395 error = EMAIL_ERROR_DB_FAILURE;
6399 if (error == EMAIL_ERROR_NONE) {
6400 if (!emcore_notify_storage_event(NOTI_MAILBOX_ADD, mailbox_tbl->account_id, mailbox_tbl->mailbox_id,
6401 mailbox_tbl->mailbox_name, mailbox_tbl->mailbox_type))
6402 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6405 if (err_code != NULL)
6408 EM_DEBUG_FUNC_END("ret [%d]", ret);
6412 INTERNAL_FUNC int emstorage_set_all_mailbox_modifiable_yn(char *multi_user_name, int account_id, int modifiable_yn, int transaction, int *err_code)
6414 EM_DEBUG_FUNC_BEGIN("account_id[%d], modifiable_yn[%d], err_code[%p]", account_id, modifiable_yn, err_code);
6416 if (account_id < FIRST_ACCOUNT_ID) {
6418 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
6420 if (err_code != NULL)
6421 *err_code = EMAIL_ERROR_INVALID_PARAM;
6424 int rc, ret = false;
6425 int error = EMAIL_ERROR_NONE;
6426 char sql_query_string[QUERY_SIZE] = {0,};
6427 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6428 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6431 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET modifiable_yn = %d WHERE account_id = %d", modifiable_yn, account_id);
6433 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6434 if (error != EMAIL_ERROR_NONE) {
6435 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6439 rc = sqlite3_changes(local_db_handle);
6441 EM_DEBUG_EXCEPTION("All mailbox_name modifiable_yn set to 0 already");
6447 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6449 if (err_code != NULL)
6451 EM_DEBUG_FUNC_END("ret [%d]", ret);
6457 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)
6459 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);
6461 if (account_id < FIRST_ACCOUNT_ID) {
6462 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
6464 if (err_code != NULL)
6465 *err_code = EMAIL_ERROR_INVALID_PARAM;
6469 int rc, ret = false;
6470 int error = EMAIL_ERROR_NONE;
6471 char sql_query_string[QUERY_SIZE] = {0, };
6472 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6473 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6476 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_box_tbl WHERE account_id = %d ", account_id);
6478 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);
6480 if (input_mailbox_id > 0) { /* 0 means all mailbox */
6481 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);
6484 EM_DEBUG_LOG_SEC("mailbox sql_query_string [%s]", sql_query_string);
6485 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6486 if (error != EMAIL_ERROR_NONE) {
6487 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6491 rc = sqlite3_changes(local_db_handle);
6493 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
6494 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
6501 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6503 if (error == EMAIL_ERROR_NONE) {
6504 if (!emcore_notify_storage_event(NOTI_MAILBOX_DELETE, account_id, input_mailbox_id, NULL, 0))
6505 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6508 if (err_code != NULL)
6511 EM_DEBUG_FUNC_END("ret [%d]", ret);
6515 INTERNAL_FUNC int emstorage_free_mailbox(emstorage_mailbox_tbl_t** mailbox_list, int count, int *err_code)
6517 EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d], err_code[%p]", mailbox_list, count, err_code);
6520 int error = EMAIL_ERROR_NONE;
6523 if (!mailbox_list || !*mailbox_list) {
6524 EM_DEBUG_EXCEPTION(" mailbox_list[%p], count[%d]", mailbox_list, count);
6526 error = EMAIL_ERROR_INVALID_PARAM;
6530 emstorage_mailbox_tbl_t* p = *mailbox_list;
6533 for (; i < count; i++) {
6534 EM_SAFE_FREE(p[i].mailbox_name);
6535 EM_SAFE_FREE(p[i].alias);
6536 EM_SAFE_FREE(p[i].eas_data); /*valgrind*/
6539 EM_SAFE_FREE(p); *mailbox_list = NULL;
6545 if (err_code != NULL)
6548 EM_DEBUG_FUNC_END("ret [%d]", ret);
6552 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)
6554 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);
6556 if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !count) {
6557 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], count[%p], exist[%p]", account_id, mailbox_name, count);
6559 if (err_code != NULL)
6560 *err_code = EMAIL_ERROR_INVALID_PARAM;
6564 int rc = -1, ret = false;
6565 int error = EMAIL_ERROR_NONE;
6566 char sql_query_string[QUERY_SIZE] = {0, };
6567 char *replaced_mailbox_name = NULL;
6569 if (strstr(mailbox_name, "'"))
6570 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6572 replaced_mailbox_name = EM_SAFE_STRDUP(mailbox_name);
6574 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6577 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6578 EMSTORAGE_START_READ_TRANSACTION(transaction);
6579 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);
6580 EM_DEBUG_LOG_SEC(">>> SQL [ %s ] ", sql_query_string);
6584 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6585 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6586 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6588 *count = atoi(result[1]);
6589 sqlite3_free_table(result);
6594 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6596 EM_SAFE_FREE(replaced_mailbox_name);
6598 if (err_code != NULL)
6601 EM_DEBUG_FUNC_END("ret [%d]", ret);
6605 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)
6607 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);
6609 if (account_id < FIRST_ACCOUNT_ID || !uid || !exist) {
6610 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], uid[%p], exist[%p]", account_id, mailbox_name , uid, exist);
6612 if (err_code != NULL)
6613 *err_code = EMAIL_ERROR_INVALID_PARAM;
6617 int rc = -1, ret = false;
6618 int error = EMAIL_ERROR_NONE;
6619 char sql_query_string[QUERY_SIZE] = {0, };
6620 char *replaced_mailbox_name = NULL;
6622 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6624 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6625 EMSTORAGE_START_READ_TRANSACTION(transaction);
6628 if (strstr(mailbox_name, "'"))
6629 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6631 replaced_mailbox_name = strdup(mailbox_name);
6633 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);
6635 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);
6638 char **result = NULL;
6640 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6641 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6642 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6644 *exist = atoi(result[1]);
6645 sqlite3_free_table(result);
6655 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6657 EM_SAFE_FREE(replaced_mailbox_name);
6659 if (err_code != NULL)
6662 EM_DEBUG_FUNC_END("ret [%d]", ret);
6666 INTERNAL_FUNC int emstorage_get_downloaded_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code)
6668 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], err_code[%p]", mail_id, mail, err_code);
6670 if (!mail || mail_id <= 0) {
6671 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
6672 if (err_code != NULL)
6673 *err_code = EMAIL_ERROR_INVALID_PARAM;
6677 int rc, ret = false;
6678 int error = EMAIL_ERROR_NONE;
6679 DB_STMT hStmt = NULL;
6680 char sql_query_string[QUERY_SIZE] = {0, };
6682 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6683 EMSTORAGE_START_READ_TRANSACTION(transaction);
6685 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE local_uid = %d", mail_id);
6687 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6688 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6690 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6691 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6694 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6695 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6696 ("sqlite3_step fail:%d", rc));
6698 *mail = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
6699 if (*mail == NULL) {
6700 error = EMAIL_ERROR_OUT_OF_MEMORY;
6701 EM_DEBUG_EXCEPTION("Memory allocation for mail failed.");
6705 memset(*mail, 0x00, sizeof(emstorage_mail_tbl_t));
6707 _get_stmt_field_data_int(hStmt, &((*mail)->account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6708 _get_stmt_field_data_int(hStmt, &((*mail)->mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6709 _get_stmt_field_data_string(hStmt, &((*mail)->server_mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6710 _get_stmt_field_data_int(hStmt, &((*mail)->mail_id), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6711 _get_stmt_field_data_string(hStmt, &((*mail)->server_mail_id), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6712 _get_stmt_field_data_int(hStmt, &((*mail)->mail_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6713 _get_stmt_field_data_char(hStmt, &((*mail)->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6715 (*mail)->server_mail_status = 1;
6721 if (hStmt != NULL) {
6722 rc = sqlite3_finalize(hStmt);
6723 if (rc != SQLITE_OK) {
6724 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6725 error = EMAIL_ERROR_DB_FAILURE;
6729 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6731 if (err_code != NULL)
6734 EM_DEBUG_FUNC_END("ret [%d]", ret);
6738 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)
6740 EM_PROFILE_BEGIN(emStorageGetDownloadList);
6741 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);
6742 if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid || !count) {
6743 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_id[%s], read_mail_uid[%p], count[%p]", account_id, mailbox_id, read_mail_uid, count);
6745 if (err_code != NULL)
6746 *err_code = EMAIL_ERROR_INVALID_PARAM;
6750 int rc, ret = false;
6751 int error = EMAIL_ERROR_NONE;
6753 DB_STMT hStmt = NULL;
6754 char sql_query_string[QUERY_SIZE] = {0, };
6756 emstorage_read_mail_uid_tbl_t* p_data_tbl = NULL;
6759 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6760 EMSTORAGE_START_READ_TRANSACTION(transaction);
6763 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);
6765 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
6767 EM_DEBUG_LOG_SEC(" sql_query_string : %s", sql_query_string);
6771 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6772 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6773 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6774 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6777 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6778 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6779 ("sqlite3_step fail:%d", rc));
6782 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL); */
6783 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL), rc);
6784 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6785 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6787 sqlite3_free_table(result);
6789 EM_DEBUG_LOG("No mail found in mail_read_mail_uid_tbl");
6795 if (!(p_data_tbl = (emstorage_read_mail_uid_tbl_t*)malloc(sizeof(emstorage_read_mail_uid_tbl_t) * *count))) {
6796 EM_DEBUG_EXCEPTION(" malloc failed...");
6797 error = EMAIL_ERROR_OUT_OF_MEMORY;
6801 memset(p_data_tbl, 0x00, sizeof(emstorage_read_mail_uid_tbl_t)*(*count));
6803 for (i = 0; i < *count; ++i) {
6804 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6805 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6806 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6807 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].local_uid), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6808 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].server_uid), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6809 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rfc822_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6810 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6811 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_flagged_field), FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6813 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6814 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6815 ("sqlite3_step fail:%d", rc));
6822 *read_mail_uid = p_data_tbl;
6823 else if (p_data_tbl)
6824 emstorage_free_read_mail_uid(&p_data_tbl, *count, NULL);
6826 if (hStmt != NULL) {
6827 rc = sqlite3_finalize(hStmt);
6828 if (rc != SQLITE_OK) {
6829 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6830 error = EMAIL_ERROR_DB_FAILURE;
6834 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6836 if (err_code != NULL)
6839 EM_PROFILE_END(emStorageGetDownloadList);
6840 EM_DEBUG_FUNC_END("ret [%d]", ret);
6844 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)
6846 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);
6848 if (account_id < FIRST_ACCOUNT_ID || !mail_size) {
6849 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);
6851 if (err_code != NULL)
6852 *err_code = EMAIL_ERROR_INVALID_PARAM;
6856 int rc, ret = false;
6857 int error = EMAIL_ERROR_NONE;
6858 DB_STMT hStmt = NULL;
6859 char sql_query_string[QUERY_SIZE] = {0, };
6860 char *replaced_mailbox_name = NULL;
6862 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6863 EMSTORAGE_START_READ_TRANSACTION(transaction);
6866 if (strstr(mailbox_name, "'"))
6867 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6869 replaced_mailbox_name = strdup(mailbox_name);
6871 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6873 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6874 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6875 "WHERE account_id = %d "
6876 "AND mailbox_id = '%s' "
6877 "AND local_uid = %d "
6878 "AND mailbox_name = '%s' "
6879 "AND server_uid = '%s'",
6880 account_id, mailbox_id, local_uid, replaced_mailbox_name, uid);
6882 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6883 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6884 "WHERE account_id = %d "
6885 "AND mailbox_id = '%s' "
6886 "AND local_uid = %d "
6887 "AND server_uid = '%s'",
6888 account_id, mailbox_id, local_uid, uid);
6892 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6893 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6894 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6897 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6898 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6899 ("sqlite3_step fail:%d", rc));
6901 if (rc == SQLITE_DONE) {
6902 EM_DEBUG_LOG("no matched mail found....");
6903 error = EMAIL_ERROR_MAIL_NOT_FOUND;
6907 _get_stmt_field_data_int(hStmt, mail_size, 0);
6912 EM_SAFE_FREE(replaced_mailbox_name);
6914 if (hStmt != NULL) {
6915 rc = sqlite3_finalize(hStmt);
6916 if (rc != SQLITE_OK) {
6917 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6918 error = EMAIL_ERROR_DB_FAILURE;
6922 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6924 if (err_code != NULL)
6927 EM_DEBUG_FUNC_END("ret [%d]", ret);
6931 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)
6933 EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], transaction[%d], err_code[%p]", read_mail_uid, transaction, err_code);
6935 if (!read_mail_uid) {
6936 EM_DEBUG_EXCEPTION("read_mail_uid[%p]", read_mail_uid);
6937 if (err_code != NULL)
6938 *err_code = EMAIL_ERROR_INVALID_PARAM;
6942 int rc, rc2, ret = false;
6943 int error = EMAIL_ERROR_NONE;
6944 DB_STMT hStmt = NULL;
6945 char sql_query_string[QUERY_SIZE] = {0, };
6947 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6948 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6950 char *sql = "SELECT max(rowid) FROM mail_read_mail_uid_tbl;";
6951 char **result = NULL;
6954 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6955 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6956 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6958 if (NULL == result[1]) rc = 1;
6959 else rc = atoi(result[1])+1;
6960 sqlite3_free_table(result);
6962 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6963 "INSERT INTO mail_read_mail_uid_tbl VALUES "
6964 "(?" /* account_id */
6965 ", ?" /* mailbox_id */
6966 ", ?" /* mailbox_name */
6967 ", ?" /* local_uid */
6968 ", ?" /* server_uid */
6969 ", ?" /* rfc822_size */
6970 ", ?" /* sync_status */
6971 ", ?" /* flags_seen_field */
6972 ", ?" /* flags_flagged_field */
6976 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
6977 if (rc2 != SQLITE_OK) {
6978 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6979 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6981 error = EMAIL_ERROR_DB_FAILURE;
6985 EM_DEBUG_LOG("account_id[%d] mailbox_id[%d] local_uid [%d]"
6986 "server_uid[%s] rfc822_size[%d] rc[%d]",
6987 read_mail_uid->account_id, read_mail_uid->mailbox_id, read_mail_uid->local_uid,
6988 read_mail_uid->server_uid, read_mail_uid->rfc822_size, rc);
6990 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->account_id);
6991 _bind_stmt_field_data_int(hStmt, LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->mailbox_id);
6992 _bind_stmt_field_data_int(hStmt, LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->local_uid);
6993 _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);
6994 _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);
6995 _bind_stmt_field_data_int(hStmt, RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->rfc822_size);
6996 _bind_stmt_field_data_int(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_seen_field);
6997 _bind_stmt_field_data_int(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_flagged_field);
6998 _bind_stmt_field_data_int(hStmt, IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, rc);
7001 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7002 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
7003 ("sqlite3_step fail:%d", rc));
7004 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7005 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
7011 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7012 if (hStmt != NULL) {
7013 rc = sqlite3_finalize(hStmt);
7014 if (rc != SQLITE_OK) {
7015 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7016 error = EMAIL_ERROR_DB_FAILURE;
7020 if (err_code != NULL)
7023 EM_DEBUG_FUNC_END("ret [%d]", ret);
7027 #ifdef __FEATURE_BODY_SEARCH__
7028 INTERNAL_FUNC int emstorage_add_mail_text(char *multi_user_name, emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
7030 EM_DEBUG_FUNC_BEGIN("mail_text[%p], transaction[%d], err_code[%p]", mail_text, transaction, err_code);
7033 EM_DEBUG_EXCEPTION("mail_text[%p]", mail_text);
7034 if (err_code != NULL)
7035 *err_code = EMAIL_ERROR_INVALID_PARAM;
7039 int rc, rc2, ret = false;
7040 int error = EMAIL_ERROR_NONE;
7041 DB_STMT hStmt = NULL;
7042 char sql_query_string[QUERY_SIZE] = {0, };
7044 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7045 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7047 char *sql = "SELECT max(rowid) FROM mail_text_tbl;";
7048 char **result = NULL;
7050 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7051 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7052 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7053 sqlite3_free_table(result);
7055 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7056 "INSERT INTO mail_text_tbl VALUES "
7062 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
7063 if (rc2 != SQLITE_OK) {
7064 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7065 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc2, sqlite3_errmsg(local_db_handle));
7067 error = EMAIL_ERROR_DB_FAILURE;
7071 EM_DEBUG_LOG("mail_id[%d] account_id[%d] mailbox_id[%d]", mail_text->mail_id,
7072 mail_text->account_id, mail_text->mailbox_id);
7073 EM_DEBUG_LOG_DEV("body_text VALUE [%s] ", mail_text->body_text);
7075 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mail_id);
7076 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->account_id);
7077 _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mailbox_id);
7078 _bind_stmt_field_data_string(hStmt, BODY_TEXT_IDX_IN_MAIL_TEXT_TBL, (char *)mail_text->body_text, 0, -1);
7080 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7081 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
7082 ("sqlite3_step fail:%d", rc));
7083 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7084 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
7089 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7090 if (hStmt != NULL) {
7091 rc = sqlite3_finalize(hStmt);
7092 if (rc != SQLITE_OK) {
7093 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7094 error = EMAIL_ERROR_DB_FAILURE;
7098 if (err_code != NULL)
7101 EM_DEBUG_FUNC_END("ret [%d]", ret);
7106 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)
7108 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);
7110 if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid) {
7111 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);
7113 if (err_code != NULL)
7114 *err_code = EMAIL_ERROR_INVALID_PARAM;
7118 int rc, ret = false;
7119 int error = EMAIL_ERROR_NONE;
7120 DB_STMT hStmt = NULL;
7121 char sql_query_string[QUERY_SIZE] = {0, };
7123 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7125 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7127 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7128 "UPDATE mail_read_mail_uid_tbl SET"
7131 ", mailbox_name = ?"
7135 ", flags_seen_field = ?"
7136 ", flags_flagged_field = ?"
7137 " WHERE account_id = ?"
7138 " AND mailbox_id = ?"
7139 " AND local_uid = ?"
7140 " AND mailbox_name= ?"
7141 " AND server_uid = ?");
7144 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7145 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7146 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7147 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7152 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->account_id);
7153 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->mailbox_id);
7154 _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7155 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->local_uid);
7156 _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->server_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7157 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->rfc822_size);
7158 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_seen_field);
7159 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_flagged_field);
7160 _bind_stmt_field_data_int(hStmt, i++, account_id);
7161 _bind_stmt_field_data_int(hStmt, i++, mailbox_id);
7162 _bind_stmt_field_data_int(hStmt, i++, local_uid);
7163 _bind_stmt_field_data_string(hStmt, i++, (char*)mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7164 _bind_stmt_field_data_string(hStmt, i++, (char*)uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7167 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7168 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7169 ("sqlite3_step fail:%d", rc));
7174 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7175 if (hStmt != NULL) {
7176 rc = sqlite3_finalize(hStmt);
7177 if (rc != SQLITE_OK) {
7178 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7179 error = EMAIL_ERROR_DB_FAILURE;
7183 if (err_code != NULL)
7186 EM_DEBUG_FUNC_END("ret [%d]", ret);
7190 INTERNAL_FUNC int emstorage_remove_downloaded_mail(char *multi_user_name,
7198 EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], mailbox_id[%d], mailbox_name[%s], "
7199 "uid[%s], transaction[%d], err_code[%p]",
7200 account_id, mailbox_id, mailbox_name, uid, transaction, err_code);
7202 if (account_id < FIRST_ACCOUNT_ID) {
7203 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_name[%s], uid[%s]", account_id, mailbox_name, uid);
7205 if (err_code != NULL)
7206 *err_code = EMAIL_ERROR_INVALID_PARAM;
7211 int error = EMAIL_ERROR_NONE;
7212 char sql_query_string[QUERY_SIZE] = {0, };
7213 char *replaced_mailbox_name = NULL;
7214 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7216 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7218 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7219 "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d ", account_id);
7221 if (mailbox_id > 0) {
7222 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7223 sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7224 "AND mailbox_id = %d ", mailbox_id);
7227 if (mailbox_name) { /* NULL means all mailbox_name */
7228 if (strstr(mailbox_name, "'"))
7229 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
7231 replaced_mailbox_name = strdup(mailbox_name);
7233 SNPRINTF(sql_query_string+EM_SAFE_STRLEN(sql_query_string),
7234 sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7235 "AND mailbox_name = '%s' ", replaced_mailbox_name);
7238 if (uid) { /* NULL means all mail */
7239 sqlite3_snprintf(sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7240 sql_query_string + EM_SAFE_STRLEN(sql_query_string), "AND server_uid = '%q' ",
7244 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7245 if (error != EMAIL_ERROR_NONE) {
7246 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7253 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7255 EM_SAFE_FREE(replaced_mailbox_name);
7257 if (err_code != NULL)
7260 EM_DEBUG_FUNC_END("ret [%d]", ret);
7264 INTERNAL_FUNC int emstorage_free_read_mail_uid(emstorage_read_mail_uid_tbl_t** read_mail_uid, int count, int *err_code)
7266 EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], count[%d], err_code[%p]", read_mail_uid, count, err_code);
7269 int error = EMAIL_ERROR_NONE;
7272 if (!read_mail_uid || !*read_mail_uid) {
7273 EM_DEBUG_EXCEPTION(" read_mail_uid[%p], count[%d]", read_mail_uid, count);
7275 error = EMAIL_ERROR_INVALID_PARAM;
7279 emstorage_read_mail_uid_tbl_t* p = *read_mail_uid;
7282 for (i = 0; i < count; i++) {
7283 EM_SAFE_FREE(p[i].mailbox_name);
7284 EM_SAFE_FREE(p[i].server_uid);
7287 EM_SAFE_FREE(p); *read_mail_uid = NULL;
7293 if (err_code != NULL)
7296 EM_DEBUG_FUNC_END("ret [%d]", ret);
7300 INTERNAL_FUNC int emstorage_get_rule_count_by_account_id(char *multi_user_name, int account_id, int *count, int transaction, int *err_code)
7302 EM_DEBUG_FUNC_BEGIN("account_id [%d], count[%p], transaction[%d], err_code[%p]", count, transaction, err_code);
7305 EM_DEBUG_EXCEPTION("count[%p]", count);
7307 if (err_code != NULL)
7308 *err_code = EMAIL_ERROR_INVALID_PARAM;
7312 int rc = -1, ret = false;
7313 int error = EMAIL_ERROR_NONE;
7314 char sql_query_string[QUERY_SIZE] = {0, };
7316 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7317 EMSTORAGE_START_READ_TRANSACTION(transaction);
7319 if (account_id != ALL_ACCOUNT)
7320 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl where account_id = %d", account_id);
7322 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl");
7326 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7327 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7328 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7330 *count = atoi(result[1]);
7331 sqlite3_free_table(result);
7336 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7338 if (err_code != NULL)
7341 EM_DEBUG_FUNC_END("ret [%d]", ret);
7345 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)
7347 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);
7349 if (!select_num || !is_completed || !rule_list) { /* only global rule supported. */
7350 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);
7352 if (err_code != NULL)
7353 *err_code = EMAIL_ERROR_INVALID_PARAM;
7358 int error = EMAIL_ERROR_NONE;
7360 emstorage_rule_tbl_t* p_data_tbl = NULL;
7361 int i = 0, count = 0;
7362 DB_STMT hStmt = NULL;
7363 char sql_query_string[QUERY_SIZE] = {0, };
7367 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7368 EMSTORAGE_START_READ_TRANSACTION(transaction);
7370 if (account_id != ALL_ACCOUNT) {
7372 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d AND type = %d", account_id, type);
7374 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d ORDER BY type", account_id);
7377 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE type = %d", type);
7379 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl ORDER BY type");
7382 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7383 /* EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt); */
7384 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7385 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7388 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7389 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7390 ("sqlite3_step fail:%d", rc));
7393 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL); */
7394 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
7395 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7396 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7398 sqlite3_free_table(result);
7401 EM_DEBUG_LOG_DEV("No matching rule found...");
7403 error = EMAIL_ERROR_FILTER_NOT_FOUND; /*there is no matched rule*/
7408 if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t) * count))) {
7409 EM_DEBUG_EXCEPTION(" malloc failed...");
7411 error = EMAIL_ERROR_OUT_OF_MEMORY;
7415 memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t) * count);
7417 for (i = 0; i < count; i++) {
7418 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7419 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7420 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7421 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].type), TYPE_IDX_IN_MAIL_RULE_TBL);
7422 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7423 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7424 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7425 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7426 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7427 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7429 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7430 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7431 ("sqlite3_step fail:%d", rc));
7438 EM_DEBUG_LOG("[%d] rules found.", count);
7441 *rule_list = p_data_tbl;
7442 *select_num = count;
7443 } else if (p_data_tbl != NULL)
7444 emstorage_free_rule(&p_data_tbl, count, NULL); /* CID FIX */
7446 if (hStmt != NULL) {
7447 rc = sqlite3_finalize(hStmt);
7448 if (rc != SQLITE_OK) {
7449 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7450 error = EMAIL_ERROR_DB_FAILURE;
7454 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7456 if (err_code != NULL)
7459 EM_DEBUG_FUNC_END("ret [%d]", ret);
7463 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)
7465 EM_DEBUG_FUNC_BEGIN("rule_id[%d], rule[%p], transaction[%d], err_code[%p]", rule_id, rule, transaction, err_code);
7466 int error = EMAIL_ERROR_NONE;
7468 DB_STMT hStmt = NULL;
7471 EM_DEBUG_EXCEPTION("Invalid parameter");
7472 error = EMAIL_ERROR_INVALID_PARAM;
7477 EM_DEBUG_EXCEPTION("rule_id[%d], rule[%p]", rule_id, rule);
7478 error = EMAIL_ERROR_INVALID_PARAM;
7482 emstorage_rule_tbl_t* p_data_tbl = NULL;
7485 char sql_query_string[QUERY_SIZE] = {0, };
7486 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7487 EMSTORAGE_START_READ_TRANSACTION(transaction);
7489 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7491 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7492 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7493 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7496 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7497 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7498 ("sqlite3_step fail:%d", rc));
7500 if (rc == SQLITE_DONE) {
7501 EM_DEBUG_EXCEPTION(" no matched rule found...");
7502 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7506 if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t)))) {
7507 EM_DEBUG_EXCEPTION(" malloc failed...");
7508 error = EMAIL_ERROR_OUT_OF_MEMORY;
7512 memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t));
7513 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7514 _get_stmt_field_data_int(hStmt, &(p_data_tbl->rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7515 _get_stmt_field_data_string(hStmt, &(p_data_tbl->filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7516 _get_stmt_field_data_int(hStmt, &(p_data_tbl->type), TYPE_IDX_IN_MAIL_RULE_TBL);
7517 _get_stmt_field_data_string(hStmt, &(p_data_tbl->value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7518 _get_stmt_field_data_string(hStmt, &(p_data_tbl->value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7519 _get_stmt_field_data_int(hStmt, &(p_data_tbl->action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7520 _get_stmt_field_data_int(hStmt, &(p_data_tbl->target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7521 _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7522 _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7531 if (hStmt != NULL) {
7532 rc = sqlite3_finalize(hStmt);
7533 if (rc != SQLITE_OK) {
7534 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7535 error = EMAIL_ERROR_DB_FAILURE;
7539 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7541 if (err_code != NULL)
7544 EM_DEBUG_FUNC_END("ret [%d]", ret);
7548 INTERNAL_FUNC int emstorage_change_rule(char *multi_user_name, int rule_id, emstorage_rule_tbl_t* new_rule, int transaction, int *err_code)
7550 EM_DEBUG_FUNC_BEGIN("rule_id[%d], new_rule[%p], transaction[%d], err_code[%p]", rule_id, new_rule, transaction, err_code);
7552 if (!new_rule) { /* only global rule supported. */
7553 EM_DEBUG_EXCEPTION("rule_id[%d], new_rule[%p]", rule_id, new_rule);
7555 if (err_code != NULL)
7556 *err_code = EMAIL_ERROR_INVALID_PARAM;
7560 int rc, ret = false;
7561 int error = EMAIL_ERROR_NONE;
7563 DB_STMT hStmt = NULL;
7564 char sql_query_string[QUERY_SIZE] = {0, };
7565 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7566 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7568 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7569 "UPDATE mail_rule_tbl SET"
7575 ", target_mailbox_id = ?"
7580 " WHERE rule_id = %d"
7584 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7585 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
7586 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7587 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7591 _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->filter_name, 0, FILTER_NAME_LEN_IN_MAIL_RULE_TBL);
7592 _bind_stmt_field_data_int(hStmt, i++, new_rule->type);
7593 _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7594 _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7595 _bind_stmt_field_data_int(hStmt, i++, new_rule->action_type);
7596 _bind_stmt_field_data_int(hStmt, i++, new_rule->target_mailbox_id);
7597 _bind_stmt_field_data_int(hStmt, i++, new_rule->flag1);
7598 _bind_stmt_field_data_int(hStmt, i++, new_rule->flag2);
7599 _bind_stmt_field_data_int(hStmt, i++, new_rule->account_id);
7600 _bind_stmt_field_data_int(hStmt, i++, rule_id);
7603 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7604 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7605 ("sqlite3_step fail:%d", rc));
7610 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7611 if (hStmt != NULL) {
7612 rc = sqlite3_finalize(hStmt);
7613 if (rc != SQLITE_OK) {
7614 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7615 error = EMAIL_ERROR_DB_FAILURE;
7619 if (err_code != NULL)
7622 EM_DEBUG_FUNC_END("ret [%d]", ret);
7626 INTERNAL_FUNC int emstorage_find_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7628 EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7631 EM_DEBUG_LOG("rule is NULL");
7632 if (err_code != NULL)
7633 *err_code = EMAIL_ERROR_INVALID_PARAM;
7637 DB_STMT hStmt = NULL;
7638 char sql_query_string[QUERY_SIZE] = {0,};
7639 int error = EMAIL_ERROR_NONE;
7643 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7644 EMSTORAGE_START_READ_TRANSACTION(transaction);
7646 switch (rule->action_type) {
7647 case EMAIL_FILTER_MOVE:
7648 if (rule->type == EMAIL_PRIORITY_SENDER) {
7649 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7650 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7651 rule->action_type, rule->type, rule->value2);
7653 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7654 "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);
7657 case EMAIL_FILTER_BLOCK:
7658 if (rule->type == EMAIL_FILTER_FROM)
7659 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7660 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7661 rule->action_type, rule->type, rule->value2);
7662 else if (rule->type == EMAIL_FILTER_SUBJECT)
7663 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7664 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value) = UPPER(\'%q\')",
7665 rule->action_type, rule->type, rule->value);
7666 else if (rule->type == (EMAIL_FILTER_SUBJECT | EMAIL_FILTER_FROM))
7667 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7668 "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\'))",
7669 rule->action_type, EMAIL_FILTER_SUBJECT, rule->value, EMAIL_FILTER_FROM, rule->value2);
7673 EM_DEBUG_EXCEPTION("Invalid parameter : rule->action_type[%d]", rule->action_type);
7674 error = EMAIL_ERROR_INVALID_PARAM;
7679 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7680 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7681 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7683 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7684 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7685 ("sqlite3_step fail:%d", rc));
7687 if (rc == SQLITE_DONE) {
7688 EM_DEBUG_EXCEPTION(" no matched rule found...");
7689 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7696 if (hStmt != NULL) {
7697 rc = sqlite3_finalize(hStmt);
7698 if (rc != SQLITE_OK) {
7699 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7700 error = EMAIL_ERROR_DB_FAILURE;
7704 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7709 EM_DEBUG_FUNC_END("ret [%d]", ret);
7713 INTERNAL_FUNC int emstorage_add_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7715 EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7717 if (!rule) { /* only global rule supported. */
7718 EM_DEBUG_LOG("rule is NULL");
7719 if (err_code != NULL)
7720 *err_code = EMAIL_ERROR_INVALID_PARAM;
7724 int rc, rc_2, ret = false;
7725 int error = EMAIL_ERROR_NONE;
7726 DB_STMT hStmt = NULL;
7727 char sql_query_string[QUERY_SIZE] = {0, };
7729 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7731 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7735 sql = "SELECT max(rowid) FROM mail_rule_tbl;";
7737 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7738 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7739 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7741 if (NULL == result[1])
7744 rc = atoi(result[1])+1;
7746 sqlite3_free_table(result);
7750 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7751 "INSERT INTO mail_rule_tbl VALUES "
7752 "(?" /* account id */
7754 ", ?" /* filter_name */
7758 ", ?" /* action_type */
7759 ", ?" /* target_mailbox_id */
7761 ", ?)"); /* flag2 */
7763 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc_2);
7764 if (rc_2 != SQLITE_OK) {
7765 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc_2, sqlite3_errmsg(local_db_handle));
7766 error = EMAIL_ERROR_DB_FAILURE;
7770 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL, rule->account_id);
7771 _bind_stmt_field_data_int(hStmt, RULE_ID_IDX_IN_MAIL_RULE_TBL, rule->rule_id);
7772 _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);
7773 _bind_stmt_field_data_int(hStmt, TYPE_IDX_IN_MAIL_RULE_TBL, rule->type);
7774 _bind_stmt_field_data_string(hStmt, VALUE_IDX_IN_MAIL_RULE_TBL, (char*)rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7775 _bind_stmt_field_data_string(hStmt, VALUE2_IDX_IN_MAIL_RULE_TBL, (char*)rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7776 _bind_stmt_field_data_int(hStmt, ACTION_TYPE_IDX_IN_MAIL_RULE_TBL, rule->action_type);
7777 _bind_stmt_field_data_int(hStmt, TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL, rule->target_mailbox_id);
7778 _bind_stmt_field_data_int(hStmt, FLAG1_IDX_IN_MAIL_RULE_TBL, rule->flag1);
7779 _bind_stmt_field_data_int(hStmt, FLAG2_IDX_IN_MAIL_RULE_TBL, rule->flag2);
7781 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7782 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7783 ("sqlite3_step fail:%d", rc));
7788 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7789 if (hStmt != NULL) {
7790 rc = sqlite3_finalize(hStmt);
7791 if (rc != SQLITE_OK) {
7792 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7793 error = EMAIL_ERROR_DB_FAILURE;
7797 if (err_code != NULL)
7800 EM_DEBUG_FUNC_END("ret [%d]", ret);
7804 INTERNAL_FUNC int emstorage_delete_rule(char *multi_user_name, int rule_id, int transaction, int *err_code)
7806 EM_DEBUG_FUNC_BEGIN("rule_id[%d], transaction[%d], err_code[%p]", rule_id, transaction, err_code);
7808 if (rule_id <= 0) { /* only global rule supported. */
7809 EM_DEBUG_EXCEPTION("rule_id[%d]", rule_id);
7811 if (err_code != NULL)
7812 *err_code = EMAIL_ERROR_INVALID_PARAM;
7816 int rc, ret = false;
7817 int error = EMAIL_ERROR_NONE;
7818 char sql_query_string[QUERY_SIZE] = {0, };
7819 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7820 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7822 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7823 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7824 if (error != EMAIL_ERROR_NONE) {
7825 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7829 rc = sqlite3_changes(local_db_handle);
7831 EM_DEBUG_EXCEPTION(" no matched rule found...");
7833 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7840 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7842 if (err_code != NULL)
7845 EM_DEBUG_FUNC_END("ret [%d]", ret);
7849 INTERNAL_FUNC int emstorage_free_rule(emstorage_rule_tbl_t** rule_list, int count, int *err_code)
7851 EM_DEBUG_FUNC_BEGIN("rule_list[%p], conut[%d], err_code[%p]", rule_list, count, err_code);
7854 int error = EMAIL_ERROR_NONE;
7857 if (!rule_list || !*rule_list) {
7858 EM_DEBUG_EXCEPTION(" rule_list[%p], conut[%d]", rule_list, count);
7860 error = EMAIL_ERROR_INVALID_PARAM;
7864 emstorage_rule_tbl_t* p = *rule_list;
7867 for (; i < count; i++)
7868 EM_SAFE_FREE(p[i].value);
7870 EM_SAFE_FREE(p); *rule_list = NULL;
7874 if (err_code != NULL)
7877 EM_DEBUG_FUNC_END("ret [%d]", ret);
7881 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)
7883 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);
7885 if (!total && !unseen) {
7886 EM_DEBUG_EXCEPTION(" accoun_id[%d], mailbox_id[%d], total[%p], unseen[%p]", account_id, mailbox_id, total, unseen);
7887 if (err_code != NULL)
7888 *err_code = EMAIL_ERROR_INVALID_PARAM;
7892 int rc = -1, ret = false;
7893 int error = EMAIL_ERROR_NONE;
7894 DB_STMT hStmt = NULL;
7895 char sql_query_string[QUERY_SIZE] = {0, };
7896 char *replaced_mailbox_name = NULL;
7898 memset(&sql_query_string, 0x00, sizeof(sql_query_string));
7899 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7900 EMSTORAGE_START_READ_TRANSACTION(transaction);
7903 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl");
7905 if (account_id != ALL_ACCOUNT) {
7906 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);
7908 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);
7909 } else if (mailbox_id)
7910 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);
7912 #ifdef USE_GET_RECORD_COUNT_API
7915 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7916 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF2; },
7917 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7919 *total = atoi(result[1]);
7920 sqlite3_free_table(result);
7923 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7924 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF2; },
7925 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7927 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7928 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF2; },
7929 ("sqlite3_step fail:%d", rc));
7930 _get_stmt_field_data_int(hStmt, total, 0);
7931 #endif /* USE_GET_RECORD_COUNT_API */
7935 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl WHERE flags_seen_field = 0"); /* fSEEN = 0x01 */
7937 if (account_id != ALL_ACCOUNT) {
7938 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7939 sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND account_id = %d", account_id);
7943 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7944 sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND mailbox_id = %d", mailbox_id);
7946 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7947 sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND mailbox_type NOT IN (3, 5)");
7950 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7951 EM_DEBUG_DB_EXEC(SQLITE_OK != rc,
7952 { error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7953 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7955 *unseen = atoi(result[1]);
7956 sqlite3_free_table(result);
7964 #ifndef USE_PREPARED_QUERY_
7965 if (hStmt != NULL) {
7966 rc = sqlite3_finalize(hStmt);
7967 if (rc != SQLITE_OK) {
7968 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7969 error = EMAIL_ERROR_DB_FAILURE;
7974 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7976 EM_SAFE_FREE(replaced_mailbox_name);
7978 if (err_code != NULL)
7981 EM_DEBUG_FUNC_END("ret [%d]", ret);
7985 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)
7987 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
7989 if (mail_id <= 0 || !mail) {
7990 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
7991 if (err_code != NULL)
7992 *err_code = EMAIL_ERROR_INVALID_PARAM;
7997 emstorage_mail_tbl_t* p_data_tbl = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
7999 if (p_data_tbl == NULL) {
8000 EM_DEBUG_EXCEPTION("malloc failed...");
8001 if (err_code != NULL)
8002 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8006 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8007 DB_STMT hStmt = NULL;
8008 char sql_query_string[QUERY_SIZE] = {0, };
8010 int rc, ret = false;
8011 int error = EMAIL_ERROR_NONE;
8013 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8014 EMSTORAGE_START_READ_TRANSACTION(transaction);
8017 case RETRIEVE_SUMMARY:
8018 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8019 "SELECT account_id, "
8022 "server_mail_status, "
8023 "server_mailbox_name, "
8027 "file_path_mime_entity, "
8028 "flags_seen_field, "
8032 "thread_item_count "
8033 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8036 case RETRIEVE_FIELDS_FOR_DELETE:
8037 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8038 "SELECT account_id, "
8040 "server_mail_status, "
8041 "server_mailbox_name, "
8043 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8046 case RETRIEVE_ACCOUNT:
8047 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8048 "SELECT account_id "
8049 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8053 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8054 "SELECT account_id, "
8055 "flags_seen_field, "
8058 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8062 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
8063 error = EMAIL_ERROR_INVALID_PARAM;
8067 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
8069 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8070 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8071 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8074 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8075 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8076 ("sqlite3_step fail:%d", rc));
8078 if (rc == SQLITE_DONE) {
8079 EM_DEBUG_LOG("no matched mail found...");
8080 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8084 case RETRIEVE_SUMMARY:
8085 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8086 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
8087 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
8088 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
8089 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
8090 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8091 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, col_index++);
8092 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, col_index++);
8093 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, col_index++);
8094 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8095 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), col_index++);
8096 _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), col_index++);
8097 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8098 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_item_count), col_index++);
8101 case RETRIEVE_FIELDS_FOR_DELETE:
8102 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8103 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
8104 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
8105 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
8106 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8109 case RETRIEVE_ACCOUNT:
8110 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8114 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8115 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8116 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8117 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
8126 else if (p_data_tbl != NULL)
8127 emstorage_free_mail(&p_data_tbl, 1, NULL);
8129 if (hStmt != NULL) {
8130 rc = sqlite3_finalize(hStmt);
8131 if (rc != SQLITE_OK) {
8132 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8133 error = EMAIL_ERROR_DB_FAILURE;
8138 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8140 if (err_code != NULL)
8143 EM_DEBUG_FUNC_END("ret [%d]", ret);
8147 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)
8149 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);
8152 int error = EMAIL_ERROR_NONE;
8153 int query_string_length = 0;
8154 int i = 0, item_count = 0, rc = -1, field_count, col_index, cur_sql_query_string = 0;
8155 char **result = NULL;
8156 char *sql_query_string = NULL;
8157 emstorage_mail_tbl_t* p_data_tbl = NULL;
8158 sqlite3 *local_db_handle = NULL;
8160 if (number_of_mails <= 0 || !mail_ids) {
8161 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8162 if (err_code != NULL)
8163 *err_code = EMAIL_ERROR_INVALID_PARAM;
8167 p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * number_of_mails);
8169 query_string_length = (sizeof(char) * 8 * number_of_mails) + 512;
8170 sql_query_string = (char*)em_malloc(query_string_length);
8172 if (p_data_tbl == NULL || sql_query_string == NULL) {
8173 EM_DEBUG_EXCEPTION("malloc failed...");
8175 EM_SAFE_FREE(p_data_tbl);
8176 EM_SAFE_FREE(sql_query_string);
8178 if (err_code != NULL)
8179 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8183 local_db_handle = emstorage_get_db_connection(multi_user_name);
8185 EMSTORAGE_START_READ_TRANSACTION(transaction);
8188 case RETRIEVE_SUMMARY:
8189 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8190 "SELECT account_id, "
8193 "server_mail_status, "
8194 "server_mailbox_name, "
8198 "file_path_mime_entity, "
8200 "flags_seen_field, "
8204 "thread_item_count "
8205 "FROM mail_tbl WHERE mail_id in (");
8209 case RETRIEVE_FIELDS_FOR_DELETE:
8210 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8211 "SELECT account_id, "
8213 "server_mail_status, "
8214 "server_mailbox_name, "
8216 "FROM mail_tbl WHERE mail_id in (");
8220 case RETRIEVE_ACCOUNT:
8221 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8222 "SELECT account_id FROM mail_tbl WHERE mail_id in (");
8227 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8228 "SELECT account_id, "
8231 "flags_seen_field, "
8233 "FROM mail_tbl WHERE mail_id in (");
8238 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
8239 error = EMAIL_ERROR_INVALID_PARAM;
8243 for (i = 0; i < number_of_mails; i++)
8244 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_string_length, "%d,", mail_ids[i]);
8245 sql_query_string[EM_SAFE_STRLEN(sql_query_string) - 1] = ')';
8247 EM_DEBUG_LOG_SEC("Query [%s], Length [%d]", sql_query_string, EM_SAFE_STRLEN(sql_query_string));
8249 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &item_count, 0, NULL), rc);
8250 if (SQLITE_OK != rc && -1 != rc) {
8251 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
8252 error = EMAIL_ERROR_DB_FAILURE;
8255 EM_DEBUG_LOG("item_count [%d]", item_count);
8257 if (number_of_mails != item_count) {
8258 EM_DEBUG_EXCEPTION("Can't find all emails");
8259 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8263 col_index = field_count;
8265 for (i = 0; i < item_count; i++) {
8267 case RETRIEVE_SUMMARY:
8268 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8269 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8270 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8271 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8272 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8273 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8274 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
8275 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
8276 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
8277 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 0, col_index++);
8278 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8279 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
8280 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
8281 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8282 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
8285 case RETRIEVE_FIELDS_FOR_DELETE:
8286 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8287 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8288 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8289 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8290 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8293 case RETRIEVE_ACCOUNT:
8294 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
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].mailbox_id), col_index++);
8301 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8302 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8313 emstorage_free_mail(&p_data_tbl, number_of_mails, NULL);
8316 sqlite3_free_table(result);
8318 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8320 EM_SAFE_FREE(sql_query_string);
8322 if (err_code != NULL)
8325 EM_DEBUG_FUNC_END("ret [%d]", ret);
8329 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)
8331 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8333 if (mail_id <= 0 || !mail) {
8334 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
8335 if (err_code != NULL)
8336 *err_code = EMAIL_ERROR_INVALID_PARAM;
8340 int ret = false, error = EMAIL_ERROR_NONE, count;
8341 char conditional_clause[QUERY_SIZE] = {0, };
8342 emstorage_mail_tbl_t* p_data_tbl = NULL;
8344 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8345 EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8347 if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8348 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8357 else if (p_data_tbl != NULL)
8358 emstorage_free_mail(&p_data_tbl, 1, &error);
8360 if (err_code != NULL)
8363 EM_DEBUG_FUNC_END("ret [%d]", ret);
8367 #ifdef __FEATURE_BODY_SEARCH__
8368 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)
8370 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
8372 if (mail_id <= 0 || !mail_text) {
8373 EM_DEBUG_EXCEPTION("mail_id[%d], mail_text[%p]", mail_id, mail_text);
8374 if (err_code != NULL)
8375 *err_code = EMAIL_ERROR_INVALID_PARAM;
8380 int error = EMAIL_ERROR_NONE;
8382 char conditional_clause[QUERY_SIZE] = {0, };
8383 emstorage_mail_text_tbl_t *p_data_tbl = NULL;
8385 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8386 EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8388 if (!emstorage_query_mail_text_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8389 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8397 *mail_text = p_data_tbl;
8398 else if (p_data_tbl != NULL)
8399 emstorage_free_mail_text(&p_data_tbl, 1, &error);
8401 if (err_code != NULL)
8404 EM_DEBUG_FUNC_END("ret [%d]", ret);
8409 INTERNAL_FUNC int emstorage_mail_search_start(char *multi_user_name,
8410 emstorage_search_filter_t *search,
8414 DB_STMT *search_handle,
8419 EM_DEBUG_FUNC_BEGIN("search[%p], account_id[%d], mailbox_id[%d], sorting[%d], "
8420 "search_handle[%p], searched[%p], transaction[%d], err_code[%p]",
8421 search, account_id, mailbox_id, sorting, search_handle,
8422 searched, transaction, err_code);
8424 if (!search_handle || !searched) {
8425 if (err_code != NULL)
8426 *err_code = EMAIL_ERROR_INVALID_PARAM;
8427 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8428 EM_DEBUG_FUNC_END("false");
8432 emstorage_search_filter_t* p = search;
8433 int error = EMAIL_ERROR_NONE;
8434 DB_STMT hStmt = NULL;
8435 char sql_query_string[QUERY_SIZE] = {0, };
8436 int rc, ret = false;
8437 int and = false, mail_count = 0;
8439 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8440 EMSTORAGE_START_READ_TRANSACTION(transaction);
8442 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl");
8444 if (account_id != ALL_ACCOUNT) {
8445 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);
8450 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);
8456 if (!strncmp(p->key_type, "subject", strlen("subject"))) {
8457 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);
8459 } else if (!strncmp(p->key_type, "full_address_from", strlen("full_address_from"))) {
8460 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);
8462 } else if (!strncmp(p->key_type, "full_address_to", strlen("full_address_to"))) {
8463 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);
8465 } else if (!strncmp(p->key_type, "email_address", strlen("email_address"))) {
8466 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);
8474 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");
8476 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
8479 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8480 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8481 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8484 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8485 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8486 ("sqlite3_step fail:%d", rc));
8490 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &mail_count, NULL, NULL), rc);
8491 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
8492 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8494 sqlite3_free_table(result);
8500 *search_handle = hStmt;
8501 *searched = mail_count;
8502 EM_DEBUG_LOG("mail_count [%d]", mail_count);
8504 if (hStmt != NULL) {
8505 rc = sqlite3_finalize(hStmt);
8506 if (rc != SQLITE_OK) {
8507 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8508 error = EMAIL_ERROR_DB_FAILURE;
8512 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8515 if (err_code != NULL)
8518 EM_DEBUG_FUNC_END("ret [%d]", ret);
8522 INTERNAL_FUNC int emstorage_mail_search_result(DB_STMT search_handle, emstorage_mail_field_type_t type, void** data, int transaction, int *err_code)
8524 EM_DEBUG_FUNC_BEGIN("search_handle[%d], type[%d], data[%p], transaction[%d], err_code[%p]", search_handle, type, data, transaction, err_code);
8526 if (search_handle == 0 || !data) {
8527 EM_DEBUG_EXCEPTION(" search_handle[%d], type[%d], data[%p]", search_handle, type, data);
8529 if (err_code != NULL)
8530 *err_code = EMAIL_ERROR_INVALID_PARAM;
8534 emstorage_mail_tbl_t* p_data_tbl = NULL;
8535 DB_STMT hStmt = search_handle;
8536 int rc, ret = false;
8537 int error = EMAIL_ERROR_NONE;
8541 _get_stmt_field_data_int(hStmt, (int *)data, MAIL_ID_IDX_IN_MAIL_TBL);
8544 case RETRIEVE_ENVELOPE:
8546 if (!(p_data_tbl = em_malloc(sizeof(emstorage_mail_tbl_t)))) {
8547 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
8548 error = EMAIL_ERROR_OUT_OF_MEMORY;
8552 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8553 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8554 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_size), MAIL_SIZE_IDX_IN_MAIL_TBL);
8555 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8556 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_from), 1, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL);
8557 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_to), 1, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL);
8558 _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject), 1, SUBJECT_IDX_IN_MAIL_TBL);
8559 _get_stmt_field_data_int(hStmt, &(p_data_tbl->body_download_status), BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL);
8560 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL);
8561 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8562 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8563 _get_stmt_field_data_time_t(hStmt, &(p_data_tbl->date_time), DATETIME_IDX_IN_MAIL_TBL);
8564 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL);
8565 _get_stmt_field_data_int(hStmt, &(p_data_tbl->DRM_status), DRM_STATUS_IDX_IN_MAIL_TBL);
8566 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->priority), PRIORITY_IDX_IN_MAIL_TBL);
8567 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), SAVE_STATUS_IDX_IN_MAIL_TBL);
8568 _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), LOCK_STATUS_IDX_IN_MAIL_TBL);
8569 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->report_status), REPORT_STATUS_IDX_IN_MAIL_TBL);
8570 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8571 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->meeting_request_status), MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL);
8572 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->message_class), MESSAGE_CLASS_IDX_IN_MAIL_TBL);
8573 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->digest_type), DIGEST_TYPE_IDX_IN_MAIL_TBL);
8574 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->smime_type), SMIME_TYPE_IDX_IN_MAIL_TBL);
8575 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->scheduled_sending_time), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8576 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->remaining_resend_times), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8577 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->tag_id), TAG_ID_IDX_IN_MAIL_TBL);
8578 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->eas_data_length), EAS_DATA_LENGTH_IDX_IN_MAIL_TBL);
8579 _get_stmt_field_data_blob(hStmt, (void**)&(p_data_tbl->eas_data), EAS_DATA_IDX_IN_MAIL_TBL);
8581 if (type == RETRIEVE_ALL) {
8582 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8583 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8584 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_reply), 1, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL);
8585 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_cc), 1, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL);
8586 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_bcc), 1, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL);
8587 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_return), 1, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL);
8588 _get_stmt_field_data_string(hStmt, &(p_data_tbl->message_id), 0, MESSAGE_ID_IDX_IN_MAIL_TBL);
8589 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8590 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8591 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_count), ATTACHMENT_COUNT_IDX_IN_MAIL_TBL);
8592 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8593 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->replied_time), REPLIED_TIME_IDX_IN_MAIL_TBL);
8594 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->forwarded_time), FORWARDED_TIME_IDX_IN_MAIL_TBL);
8595 _get_stmt_field_data_string(hStmt, &(p_data_tbl->default_charset), 0, DEFAULT_CHARSET_IDX_IN_MAIL_TBL);
8598 if (p_data_tbl->body_download_status) {
8601 if (p_data_tbl->file_path_html) {
8602 if (stat(p_data_tbl->file_path_html, &buf) == -1)
8603 p_data_tbl->body_download_status = 0;
8604 } else if (p_data_tbl->file_path_plain) {
8605 if (stat(p_data_tbl->file_path_plain, &buf) == -1)
8606 p_data_tbl->body_download_status = 0;
8608 p_data_tbl->body_download_status = 0;
8611 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8614 case RETRIEVE_SUMMARY:
8615 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
8616 EM_DEBUG_EXCEPTION(" malloc failed...");
8618 error = EMAIL_ERROR_OUT_OF_MEMORY;
8622 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8624 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8625 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8626 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8627 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8628 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8630 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8633 case RETRIEVE_ADDRESS:
8634 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
8635 EM_DEBUG_EXCEPTION(" malloc failed...");
8636 error = EMAIL_ERROR_OUT_OF_MEMORY;
8640 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8641 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8642 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8643 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8644 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8651 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8652 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8653 ("sqlite3_step fail:%d", rc));
8659 if (err_code != NULL)
8662 if (ret == false && p_data_tbl)
8663 emstorage_free_mail(&p_data_tbl, 1, NULL);
8665 EM_DEBUG_FUNC_END("ret [%d]", ret);
8669 INTERNAL_FUNC int emstorage_mail_search_end(DB_STMT search_handle, int transaction, int *err_code)
8671 EM_DEBUG_FUNC_BEGIN("search_handle[%d], transaction[%d], err_code[%p]", search_handle, transaction, err_code);
8673 int error = EMAIL_ERROR_NONE;
8674 int rc, ret = false;
8676 if (search_handle == 0) {
8677 EM_DEBUG_EXCEPTION(" search_handle[%d]", search_handle);
8678 error = EMAIL_ERROR_INVALID_PARAM;
8682 DB_STMT hStmt = search_handle;
8684 rc = sqlite3_finalize(hStmt);
8685 if (rc != SQLITE_OK) {
8686 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8687 error = EMAIL_ERROR_DB_FAILURE;
8693 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8695 if (err_code != NULL)
8698 EM_DEBUG_FUNC_END("ret [%d]", ret);
8702 INTERNAL_FUNC int emstorage_change_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t *mail, int transaction, int *err_code)
8704 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8706 if (mail_id <= 0 || !mail) {
8707 EM_DEBUG_EXCEPTION(" mail_id[%d], mail[%p]", mail_id, mail);
8709 if (err_code != NULL)
8710 *err_code = EMAIL_ERROR_INVALID_PARAM;
8714 DB_STMT hStmt = NULL;
8715 char sql_query_string[QUERY_SIZE] = {0, };
8718 int error = EMAIL_ERROR_NONE;
8720 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8721 char mailbox_id_param_string[10] = {0,};
8723 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
8725 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8726 "UPDATE mail_tbl SET"
8731 ", server_mail_status = ?"
8732 ", server_mailbox_name = ?"
8733 ", server_mail_id = ?"
8734 ", reference_mail_id = ?"
8735 ", full_address_from = ?"
8736 ", full_address_reply = ?" /* 10 */
8737 ", full_address_to = ?"
8738 ", full_address_cc = ?"
8739 ", full_address_bcc = ?"
8740 ", full_address_return = ?"
8742 ", body_download_status = ?"
8743 ", file_path_plain = ?"
8744 ", file_path_html = ?"
8745 ", file_path_mime_entity = ?"
8747 ", flags_seen_field = ?"
8748 ", flags_deleted_field = ?"
8749 ", flags_flagged_field = ?"
8750 ", flags_answered_field = ?"
8751 ", flags_recent_field = ?"
8752 ", flags_draft_field = ?"
8753 ", flags_forwarded_field = ?"
8759 ", report_status = ?"
8760 ", preview_text = ?"
8762 ", scheduled_sending_time = ?"
8763 ", remaining_resend_times = ?"
8765 ", replied_time = ?"
8766 ", forwarded_time = ?"
8767 ", default_charset = ?"
8768 ", eas_data_length = ?"
8770 " WHERE mail_id = %d AND account_id != 0 "
8774 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8775 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8776 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8778 _bind_stmt_field_data_int(hStmt, i++, mail->mail_id);
8779 _bind_stmt_field_data_int(hStmt, i++, mail->account_id);
8780 _bind_stmt_field_data_int(hStmt, i++, mail->mailbox_id);
8781 _bind_stmt_field_data_int(hStmt, i++, mail->mail_size);
8782 _bind_stmt_field_data_int(hStmt, i++, mail->server_mail_status);
8783 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mailbox_name, 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
8784 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mail_id, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
8785 _bind_stmt_field_data_int(hStmt, i++, mail->reference_mail_id);
8786 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
8787 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
8788 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
8789 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
8790 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
8791 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
8792 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8793 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
8794 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8795 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8796 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
8797 _bind_stmt_field_data_int(hStmt, i++, mail->date_time);
8798 _bind_stmt_field_data_char(hStmt, i++, mail->flags_seen_field);
8799 _bind_stmt_field_data_char(hStmt, i++, mail->flags_deleted_field);
8800 _bind_stmt_field_data_char(hStmt, i++, mail->flags_flagged_field);
8801 _bind_stmt_field_data_char(hStmt, i++, mail->flags_answered_field);
8802 _bind_stmt_field_data_char(hStmt, i++, mail->flags_recent_field);
8803 _bind_stmt_field_data_char(hStmt, i++, mail->flags_draft_field);
8804 _bind_stmt_field_data_char(hStmt, i++, mail->flags_forwarded_field);
8805 _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
8806 _bind_stmt_field_data_int(hStmt, i++, mail->priority);
8807 _bind_stmt_field_data_int(hStmt, i++, mail->save_status);
8808 _bind_stmt_field_data_int(hStmt, i++, mail->lock_status);
8809 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->message_id, 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
8810 _bind_stmt_field_data_int(hStmt, i++, mail->report_status);
8811 _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8812 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
8813 _bind_stmt_field_data_int(hStmt, i++, mail->scheduled_sending_time);
8814 _bind_stmt_field_data_int(hStmt, i++, mail->remaining_resend_times);
8815 _bind_stmt_field_data_int(hStmt, i++, mail->tag_id);
8816 _bind_stmt_field_data_int(hStmt, i++, mail->replied_time);
8817 _bind_stmt_field_data_int(hStmt, i++, mail->forwarded_time);
8818 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
8819 _bind_stmt_field_data_int(hStmt, i++, mail->eas_data_length);
8820 _bind_stmt_field_data_blob(hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
8822 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8823 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8824 ("sqlite3_step fail:%d", rc));
8826 rc = sqlite3_changes(local_db_handle);
8828 EM_DEBUG_LOG(" no matched mail found...");
8829 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8836 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
8837 if (hStmt != NULL) {
8838 rc = sqlite3_finalize(hStmt);
8839 if (rc != SQLITE_OK) {
8840 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8841 error = EMAIL_ERROR_DB_FAILURE;
8846 if (error == EMAIL_ERROR_NONE && mail) {
8847 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
8848 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail->mail_id, mailbox_id_param_string, 0))
8849 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventfailed [NOTI_MAIL_UPDATE]");
8852 if (err_code != NULL)
8855 EM_DEBUG_FUNC_END("ret [%d]", ret);
8860 * emstorage_clean_save_status(int save_status, int *err_code) - set the all mail status to the set value
8864 INTERNAL_FUNC int emstorage_clean_save_status(char *multi_user_name, int save_status, int *err_code)
8866 EM_DEBUG_FUNC_BEGIN("save_status[%d], err_code[%p]", save_status, err_code);
8868 EM_IF_NULL_RETURN_VALUE(err_code, false);
8871 int error = EMAIL_ERROR_NONE;
8873 char sql_query_string[QUERY_SIZE] = {0, };
8874 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8876 memset(sql_query_string, 0x00, sizeof(sql_query_string));
8877 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);
8879 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
8880 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
8881 if (error != EMAIL_ERROR_NONE) {
8882 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
8886 rc = sqlite3_changes(local_db_handle);
8888 EM_DEBUG_LOG(" No Matched Mail Exists ");
8889 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8896 if (err_code != NULL)
8899 EM_DEBUG_FUNC_END("ret [%d]", ret);
8903 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)
8905 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);
8907 int error = EMAIL_ERROR_NONE;
8910 int cur_mail_id_string = 0;
8911 int mail_id_string_buffer_length = 0;
8912 int parameter_string_length = 0;
8913 char *sql_query_string = NULL;
8914 char *mail_id_string_buffer = NULL;
8915 char *parameter_string = NULL;
8916 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8917 email_mail_attribute_type target_mail_attribute_type = 0;
8919 if (!mail_ids || !field_name || account_id == 0) {
8920 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8921 if (err_code != NULL)
8922 *err_code = EMAIL_ERROR_INVALID_PARAM;
8926 if ((error = emcore_get_attribute_type_by_mail_field_name(field_name, &target_mail_attribute_type)) != EMAIL_ERROR_NONE) {
8927 EM_DEBUG_EXCEPTION("emstorageemcore_get_attribute_type_by_mail_field_name failed [%d]", error);
8928 if (err_code != NULL)
8933 /* Generating mail id list string */
8934 mail_id_string_buffer_length = MAIL_ID_STRING_LENGTH * mail_ids_count;
8936 mail_id_string_buffer = em_malloc(mail_id_string_buffer_length);
8938 if (!mail_id_string_buffer) {
8939 EM_DEBUG_EXCEPTION("em_mallocfailed");
8940 if (err_code != NULL)
8941 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8945 for (i = 0; i < mail_ids_count; i++)
8946 cur_mail_id_string += SNPRINTF_OFFSET(mail_id_string_buffer, cur_mail_id_string, mail_id_string_buffer_length, "%d,", mail_ids[i]);
8948 if (EM_SAFE_STRLEN(mail_id_string_buffer) > 1)
8949 mail_id_string_buffer[EM_SAFE_STRLEN(mail_id_string_buffer) - 1] = NULL_CHAR;
8951 /* Generating notification parameter string */
8952 parameter_string_length = mail_id_string_buffer_length + EM_SAFE_STRLEN(field_name) + 2;
8953 parameter_string = em_malloc(parameter_string_length);
8955 if (!parameter_string) {
8956 EM_DEBUG_EXCEPTION("em_mallocfailed");
8957 if (err_code != NULL)
8958 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8959 EM_SAFE_FREE(mail_id_string_buffer);
8963 SNPRINTF(parameter_string, parameter_string_length, "%s%c%s", field_name, 0x01, mail_id_string_buffer);
8964 query_size = EM_SAFE_STRLEN(mail_id_string_buffer) + EM_SAFE_STRLEN(field_name) + 250;
8966 sql_query_string = em_malloc(query_size);
8967 if (sql_query_string == NULL) {
8968 EM_DEBUG_EXCEPTION("em_mallocfailed");
8969 error = EMAIL_ERROR_OUT_OF_MEMORY;
8972 /* Write query string */
8973 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);
8975 EM_DEBUG_LOG_DEV("sql_query_string [%s]", sql_query_string);
8978 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
8979 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
8980 if (error != EMAIL_ERROR_NONE) {
8981 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
8985 if (sqlite3_changes(local_db_handle) == 0)
8986 EM_DEBUG_LOG("no mail matched...");
8991 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
8993 if (error == EMAIL_ERROR_NONE && parameter_string) {
8994 if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, target_mail_attribute_type, parameter_string, value))
8995 EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_MAIL_FIELD_UPDATE [%s,%d]", field_name, value);
8998 EM_SAFE_FREE(mail_id_string_buffer);
8999 EM_SAFE_FREE(parameter_string);
9000 EM_SAFE_FREE(sql_query_string);
9003 if (err_code != NULL)
9006 EM_DEBUG_FUNC_END("error [%d]", error);
9010 #ifdef __FEATURE_BODY_SEARCH__
9011 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)
9013 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
9015 if (mail_id <= 0 || !mail_text) {
9016 EM_DEBUG_EXCEPTION(" mail_id[%d], mail_text[%p]", mail_id, mail_text);
9017 if (err_code != NULL)
9018 *err_code = EMAIL_ERROR_INVALID_PARAM;
9023 int error = EMAIL_ERROR_NONE;
9024 DB_STMT hStmt = NULL;
9025 char sql_query_string[QUERY_SIZE] = {0, };
9030 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9031 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9033 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9034 "UPDATE mail_text_tbl SET"
9036 " WHERE mail_id = %d AND account_id != 0"
9038 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9040 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9041 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9042 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9045 _bind_stmt_field_data_string(hStmt, i++, (char *)mail_text->body_text, 0, -1);
9047 if (hStmt != NULL) {
9048 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9049 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9050 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9052 rc = sqlite3_changes(local_db_handle);
9054 EM_DEBUG_LOG(" no matched mail found...");
9055 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9063 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9065 if (hStmt != NULL) {
9066 rc = sqlite3_finalize(hStmt);
9067 if (rc != SQLITE_OK) {
9068 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9069 error = EMAIL_ERROR_DB_FAILURE;
9074 if (err_code != NULL)
9077 EM_DEBUG_FUNC_END("ret [%d]", ret);
9082 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)
9084 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
9087 int error = EMAIL_ERROR_NONE;
9092 DB_STMT hStmt = NULL;
9093 char sql_query_string[QUERY_SIZE] = {0, };
9094 char mailbox_id_param_string[10] = {0,};
9095 sqlite3 *local_db_handle = NULL;
9097 if (mail_id <= 0 || !mail) {
9098 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], mail[%p]", mail_id, type, mail);
9099 if (err_code != NULL)
9100 *err_code = EMAIL_ERROR_INVALID_PARAM;
9104 local_db_handle = emstorage_get_db_connection(multi_user_name);
9106 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9110 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9111 "UPDATE mail_tbl SET"
9112 " body_download_status = ?"
9113 ", file_path_plain = ?"
9114 ", file_path_html = ?"
9115 ", file_path_mime_entity = ?"
9116 ", flags_seen_field = ?"
9117 ", flags_deleted_field = ?"
9118 ", flags_flagged_field = ?"
9119 ", flags_answered_field = ?"
9120 ", flags_recent_field = ?"
9121 ", flags_draft_field = ?"
9122 ", flags_forwarded_field = ?"
9124 ", attachment_count = ?"
9126 ", meeting_request_status = ? "
9127 ", message_class = ? "
9128 ", digest_type = ? "
9130 " WHERE mail_id = %d AND account_id != 0"
9134 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9135 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9136 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9139 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9140 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9141 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9142 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9143 _bind_stmt_field_data_int(hStmt, i++, mail->flags_seen_field);
9144 _bind_stmt_field_data_int(hStmt, i++, mail->flags_deleted_field);
9145 _bind_stmt_field_data_int(hStmt, i++, mail->flags_flagged_field);
9146 _bind_stmt_field_data_int(hStmt, i++, mail->flags_answered_field);
9147 _bind_stmt_field_data_int(hStmt, i++, mail->flags_recent_field);
9148 _bind_stmt_field_data_int(hStmt, i++, mail->flags_draft_field);
9149 _bind_stmt_field_data_int(hStmt, i++, mail->flags_forwarded_field);
9150 _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
9151 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9152 _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9153 _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
9154 _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
9155 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9156 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9159 case UPDATE_MAILBOX: {
9161 emstorage_mailbox_tbl_t *mailbox_tbl;
9163 if ((err = emstorage_get_mailbox_by_id(multi_user_name, mail->mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
9164 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_id failed [%d]", err);
9168 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9169 "UPDATE mail_tbl SET"
9170 " mailbox_id = '%d'"
9171 ",mailbox_type = '%d'"
9172 " WHERE mail_id = %d AND account_id != 0"
9173 , mailbox_tbl->mailbox_id
9174 , mailbox_tbl->mailbox_type
9179 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9180 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9181 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9184 _bind_stmt_field_data_string(hStmt, i++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
9186 emstorage_free_mailbox(&mailbox_tbl, 1, NULL); /*prevent 26251*/
9192 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9193 "UPDATE mail_tbl SET"
9194 " flags_seen_field = %d"
9195 ",flags_deleted_field = %d"
9196 ",flags_flagged_field = %d"
9197 ",flags_answered_field = %d"
9198 ",flags_recent_field = %d"
9199 ",flags_draft_field = %d"
9200 ",flags_forwarded_field = %d"
9201 " WHERE mail_id = %d AND account_id != 0"
9202 , mail->flags_seen_field
9203 , mail->flags_deleted_field
9204 , mail->flags_flagged_field
9205 , mail->flags_answered_field
9206 , mail->flags_recent_field
9207 , mail->flags_draft_field
9208 , mail->flags_forwarded_field
9210 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9213 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9214 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9215 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9219 case UPDATE_EXTRA_FLAG:
9220 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9221 "UPDATE mail_tbl SET"
9223 ", save_status = %d"
9224 ", lock_status = %d"
9225 ", report_status = %d"
9227 " WHERE mail_id = %d AND account_id != 0"
9231 , mail->report_status
9234 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9237 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9238 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9239 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9242 case UPDATE_STICKY_EXTRA_FLAG:
9243 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9244 "UPDATE mail_tbl SET"
9246 " WHERE mail_id = %d AND account_id != 0"
9249 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9252 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9253 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9254 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9258 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9259 "UPDATE mail_tbl SET"
9260 " full_address_from = ?"
9261 ", full_address_reply = ?"
9262 ", full_address_to = ?"
9263 ", full_address_cc = ?"
9264 ", full_address_bcc = ?"
9265 ", full_address_return = ?"
9267 ", file_path_plain = ?"
9269 ", flags_seen_field = ?"
9270 ", flags_deleted_field = ?"
9271 ", flags_flagged_field = ?"
9272 ", flags_answered_field = ?"
9273 ", flags_recent_field = ?"
9274 ", flags_draft_field = ?"
9275 ", flags_forwarded_field = ?"
9279 ", report_status = ?"
9281 ", file_path_html = ?"
9282 ", file_path_mime_entity = ?"
9284 ", preview_text = ?"
9285 ", body_download_status = ?"
9286 ", attachment_count = ?"
9287 ", inline_content_count = ?"
9288 ", meeting_request_status = ?"
9289 ", message_class = ?"
9292 ", scheduled_sending_time = ?"
9293 ", remaining_resend_times = ?"
9295 ", replied_time = ?"
9296 ", forwarded_time = ?"
9297 ", default_charset = ?"
9298 ", eas_data_length = ?"
9300 " WHERE mail_id = %d AND account_id != 0"
9304 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9305 EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
9306 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9307 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9309 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
9310 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
9311 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
9312 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
9313 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
9314 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
9315 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9316 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9317 _bind_stmt_field_data_time_t(hStmt, i++, mail->date_time);
9318 _bind_stmt_field_data_char(hStmt, i++, mail->flags_seen_field);
9319 _bind_stmt_field_data_char(hStmt, i++, mail->flags_deleted_field);
9320 _bind_stmt_field_data_char(hStmt, i++, mail->flags_flagged_field);
9321 _bind_stmt_field_data_char(hStmt, i++, mail->flags_answered_field);
9322 _bind_stmt_field_data_char(hStmt, i++, mail->flags_recent_field);
9323 _bind_stmt_field_data_char(hStmt, i++, mail->flags_draft_field);
9324 _bind_stmt_field_data_char(hStmt, i++, mail->flags_forwarded_field);
9325 _bind_stmt_field_data_int(hStmt, i++, mail->priority);
9326 _bind_stmt_field_data_int(hStmt, i++, mail->save_status);
9327 _bind_stmt_field_data_int(hStmt, i++, mail->lock_status);
9328 _bind_stmt_field_data_int(hStmt, i++, mail->report_status);
9329 _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
9330 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9331 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9332 _bind_stmt_field_data_int(hStmt, i++, mail->mail_size);
9333 _bind_stmt_field_data_nstring(hStmt, i++, (char*)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9334 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9335 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9336 _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
9337 _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
9338 _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
9339 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9340 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9341 _bind_stmt_field_data_int(hStmt, i++, mail->scheduled_sending_time);
9342 _bind_stmt_field_data_int(hStmt, i++, mail->remaining_resend_times);
9343 _bind_stmt_field_data_int(hStmt, i++, mail->tag_id);
9344 _bind_stmt_field_data_int(hStmt, i++, mail->replied_time);
9345 _bind_stmt_field_data_int(hStmt, i++, mail->forwarded_time);
9346 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
9347 _bind_stmt_field_data_int(hStmt, i++, mail->eas_data_length);
9348 _bind_stmt_field_data_blob(hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
9351 case UPDATE_DATETIME: {
9352 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9353 "UPDATE mail_tbl SET"
9354 " date_time = '%ld'"
9355 " WHERE mail_id = %d AND account_id != 0"
9359 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9360 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9361 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9365 case UPDATE_FROM_CONTACT_INFO:
9366 EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_FROM_CONTACT_INFO");
9367 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9368 "UPDATE mail_tbl SET"
9369 " email_address_sender = ?,"
9370 " WHERE mail_id = %d",
9375 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9376 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9377 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9379 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9382 case UPDATE_TO_CONTACT_INFO:
9383 EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_TO_CONTACT_INFO");
9384 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9385 "UPDATE mail_tbl SET"
9386 " email_address_recipient = ?,"
9387 " WHERE mail_id = %d",
9392 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9393 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9394 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9396 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9399 case UPDATE_ALL_CONTACT_INFO:
9400 EM_DEBUG_LOG("emstorage_change_mail_field - mail change type is UPDATE_ALL_CONTACT_INFO");
9401 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9402 "UPDATE mail_tbl SET"
9403 " email_address_sender = ?,"
9404 " email_address_recipient = ?,"
9405 " WHERE mail_id = %d",
9410 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9411 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9412 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9414 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9415 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9419 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
9420 case UPDATE_PARTIAL_BODY_DOWNLOAD:
9422 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9423 "UPDATE mail_tbl SET"
9424 " body_download_status = ?"
9425 ", file_path_plain = ?"
9426 ", file_path_html = ?"
9427 ", file_path_mime_entity = ?"
9428 ", attachment_count = ?"
9429 ", inline_content_count = ?"
9430 ", preview_text = ?"
9433 " WHERE mail_id = %d"
9437 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9438 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9439 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9442 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9443 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9444 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9445 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9446 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9447 _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
9448 _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9449 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9450 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9455 case UPDATE_FILE_PATH:
9456 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9457 "UPDATE mail_tbl SET"
9458 ", file_path_plain = ?"
9459 ", file_path_html = ?"
9460 ", file_path_mime_entity = ?"
9461 " WHERE mail_id = %d"
9465 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9466 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
9467 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9468 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9470 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9471 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9472 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9476 EM_DEBUG_LOG(" type[%d]", type);
9478 error = EMAIL_ERROR_INVALID_PARAM;
9482 if (hStmt != NULL) {
9484 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9485 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
9486 ("sqlite3_step fail:%d", rc));
9487 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9488 ("sqlite3_step fail:%d", rc));
9489 rc = sqlite3_changes(local_db_handle);
9491 EM_DEBUG_LOG(" no matched mail found...");
9492 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9497 if (mail->account_id == 0) {
9498 emstorage_mail_tbl_t* mail_for_account_tbl = NULL;
9499 if (!emstorage_get_mail_field_by_id(multi_user_name, mail_id, RETRIEVE_ACCOUNT, &mail_for_account_tbl, true, &error) || !mail_for_account_tbl) {
9500 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id error [%d]", error);
9503 mail->account_id = mail_for_account_tbl->account_id;
9504 if (mail_for_account_tbl)
9505 emstorage_free_mail(&mail_for_account_tbl, 1, NULL);
9511 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9513 if (hStmt != NULL) {
9514 rc = sqlite3_finalize(hStmt);
9515 if (rc != SQLITE_OK) {
9516 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9517 error = EMAIL_ERROR_DB_FAILURE;
9522 if (error == EMAIL_ERROR_NONE && move_flag != 1 && transaction) {
9523 if (!emstorage_get_mailbox_id_by_mailbox_type(multi_user_name, mail->account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &mailbox_id, false, &error))
9524 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_id_by_mailbox_type error [%d]", error);
9526 if (mail->mailbox_id == mailbox_id) {
9527 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
9528 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, mailbox_id_param_string, type))
9529 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventerror [ NOTI_MAIL_UPDATE ] >>>> ");
9531 /* h.gahlaut@samsung.com: Jan 10, 2011 Publishing noti to refresh outbox when email sending status changes */
9532 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, NULL, type))
9533 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventerror [ NOTI_MAIL_UPDATE ]");
9537 if (err_code != NULL)
9540 EM_DEBUG_FUNC_END("ret [%d]", ret);
9544 INTERNAL_FUNC int emstorage_increase_mail_id(char *multi_user_name, int *mail_id, int transaction, int *err_code)
9546 EM_DEBUG_FUNC_BEGIN("mail_id[%p], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9548 int rc, ret = false;
9549 int error = EMAIL_ERROR_NONE;
9550 int latest_mail_id = 0;
9551 sqlite3 *local_db_handle = NULL;
9552 char *sql = "SELECT MAX(mail_id) FROM mail_tbl;";
9553 char **result = NULL;
9555 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9556 _timedlock_shm_mutex(mapped_for_generating_mail_id, 2);
9557 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9559 ret = vconf_get_int(VCONF_KEY_LATEST_MAIL_ID, &latest_mail_id);
9560 if (ret < 0 || latest_mail_id == 0) {
9561 EM_DEBUG_LOG("vconf_get_int() failed [%d] or latest_mail_id is zero", ret);
9563 local_db_handle = emstorage_get_db_connection(multi_user_name);
9565 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9566 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
9567 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9568 if (NULL == result[1])
9571 rc = atoi(result[1]) + 1;
9573 sqlite3_free_table(result);
9574 latest_mail_id = rc;
9579 ret = vconf_set_int(VCONF_KEY_LATEST_MAIL_ID, latest_mail_id);
9582 *mail_id = latest_mail_id;
9584 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9585 _unlockshm_mutex(mapped_for_generating_mail_id);
9586 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9592 if (err_code != NULL)
9595 EM_DEBUG_FUNC_END("ret [%d]", ret);
9599 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)
9601 EM_PROFILE_BEGIN(profile_emstorage_add_mail);
9602 EM_DEBUG_FUNC_BEGIN("mail_tbl_data[%p], get_id[%d], transaction[%d], err_code[%p]", mail_tbl_data, get_id, transaction, err_code);
9604 if (!mail_tbl_data) {
9605 EM_DEBUG_EXCEPTION("mail_tbl_data[%p], get_id[%d]", mail_tbl_data, get_id);
9606 if (err_code != NULL)
9607 *err_code = EMAIL_ERROR_INVALID_PARAM;
9611 int rc, ret = false;
9612 int error = EMAIL_ERROR_NONE;
9613 char sql_query_string[QUERY_SIZE] = {0, };
9614 DB_STMT hStmt = NULL;
9616 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9618 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9621 /* increase unique id */
9622 char *sql = "SELECT max(rowid) FROM mail_tbl;";
9625 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9626 /* EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
9627 ("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle))); */
9628 if (rc != SQLITE_OK) {
9629 EM_DEBUG_EXCEPTION("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle));
9630 error = EMAIL_ERROR_DB_FAILURE;
9631 sqlite3_free_table(result);
9635 if (NULL == result[1])
9638 rc = atoi(result[1])+1;
9640 sqlite3_free_table(result);
9642 mail_tbl_data->mail_id = rc;
9643 mail_tbl_data->thread_id = rc;
9646 if (mail_tbl_data->date_time == 0)
9647 mail_tbl_data->date_time = time(NULL);
9649 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9650 "INSERT INTO mail_tbl VALUES "
9652 ", ?" /* account_id */
9653 ", ?" /* mailbox_id */
9654 ", ?" /* mailbox_type */
9657 ", ?" /* date_time */
9658 ", ?" /* server_mail_status */
9659 ", ?" /* server_mailbox_name */
9660 ", ?" /* server_mail_id */
9661 ", ?" /* message_id */
9663 ", ?" /* reference_mail_id */
9664 ", ?" /* full_address_from */
9665 ", ?" /* full_address_reply */
9666 ", ?" /* full_address_to */
9667 ", ?" /* full_address_cc */
9669 ", ?" /* full_address_bcc */
9670 ", ?" /* full_address_return */
9671 ", ?" /* email_address_sender */
9672 ", ?" /* email_address_recipient */
9673 ", ?" /* alias_sender */
9675 ", ?" /* alias_recipient */
9676 ", ?" /* body_download_status */
9677 ", ?" /* file_path_plain */
9678 ", ?" /* file_path_html */
9679 ", ?" /* file_path_mime_entity */
9681 ", ?" /* mail_size */
9682 ", ?" /* flags_seen_field */
9683 ", ?" /* flags_deleted_field */
9684 ", ?" /* flags_flagged_field */
9685 ", ?" /* flags_answered_field */
9687 ", ?" /* flags_recent_field */
9688 ", ?" /* flags_draft_field */
9689 ", ?" /* flags_forwarded_field */
9690 ", ?" /* DRM_status */
9691 ", ?" /* priority */
9693 ", ?" /* save_status */
9694 ", ?" /* lock_status */
9695 ", ?" /* report_status */
9696 ", ?" /* attachment_count */
9697 ", ?" /* inline_content_count */
9699 ", ?" /* thread_id */
9700 ", ?" /* thread_item_count */
9701 ", ?" /* preview_text */
9702 ", ?" /* meeting_request_status */
9703 ", ?" /* message_class */
9705 ", ?" /* digest_type */
9706 ", ?" /* smime_type */
9707 ", ?" /* scheduled_sending_time */
9708 ", ?" /* remaining_resend_times */
9711 ", ?" /* replied_time */
9712 ", ?" /* forwarded_time */
9713 ", ?" /* default charset */
9714 ", ?" /* eas_data_length */
9715 ", ?" /* eas_data */
9716 ", ?" /* user_name */
9719 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle,
9721 EM_SAFE_STRLEN(sql_query_string),
9725 if (rc != SQLITE_OK) {
9726 EM_DEBUG_EXCEPTION("sqlite3_prepare error [%d] [%s] SQL(%s) ",
9727 rc, sql_query_string, sqlite3_errmsg(local_db_handle));
9728 error = EMAIL_ERROR_DB_FAILURE;
9732 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mail_id);
9733 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, mail_tbl_data->account_id);
9734 _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_id);
9735 _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_type);
9736 _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9738 _bind_stmt_field_data_int(hStmt, DATETIME_IDX_IN_MAIL_TBL, mail_tbl_data->date_time);
9739 _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->server_mail_status);
9740 _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);
9741 _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);
9742 _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);
9744 _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, mail_tbl_data->reference_mail_id);
9745 _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);
9746 _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);
9747 _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);
9748 _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);
9750 _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);
9751 _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);
9752 _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);
9753 _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);
9754 _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);
9756 _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);
9757 _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->body_download_status);
9758 _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);
9759 _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);
9760 _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);
9762 _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, mail_tbl_data->mail_size);
9763 _bind_stmt_field_data_int(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_seen_field);
9764 _bind_stmt_field_data_int(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_deleted_field);
9765 _bind_stmt_field_data_int(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_flagged_field);
9766 _bind_stmt_field_data_int(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_answered_field);
9768 _bind_stmt_field_data_int(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_recent_field);
9769 _bind_stmt_field_data_int(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_draft_field);
9770 _bind_stmt_field_data_int(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_forwarded_field);
9771 _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->DRM_status);
9772 _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, mail_tbl_data->priority);
9774 _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->save_status);
9775 _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->lock_status);
9776 _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->report_status);
9777 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->attachment_count);
9778 _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->inline_content_count);
9780 _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, mail_tbl_data->thread_id);
9781 _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->thread_item_count);
9782 _bind_stmt_field_data_nstring(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9783 _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->meeting_request_status);
9784 _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, mail_tbl_data->message_class);
9786 _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->digest_type);
9787 _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->smime_type);
9788 _bind_stmt_field_data_int(hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->scheduled_sending_time);
9789 _bind_stmt_field_data_int(hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, mail_tbl_data->remaining_resend_times);
9790 _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, mail_tbl_data->tag_id);
9792 _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->replied_time);
9793 _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->forwarded_time);
9794 _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);
9795 _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, mail_tbl_data->eas_data_length);
9796 _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, (void*)mail_tbl_data->eas_data, mail_tbl_data->eas_data_length);
9797 _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);
9799 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9800 if (rc == SQLITE_FULL) {
9801 EM_DEBUG_EXCEPTION("sqlite3_step error [%d]", rc);
9802 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
9805 if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
9806 EM_DEBUG_EXCEPTION("sqlite3_step error [%d]", rc);
9807 error = EMAIL_ERROR_DB_FAILURE;
9813 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9815 if (hStmt != NULL) {
9816 rc = sqlite3_finalize(hStmt);
9817 if (rc != SQLITE_OK) {
9818 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9819 error = EMAIL_ERROR_DB_FAILURE;
9823 if (err_code != NULL)
9826 EM_PROFILE_END(profile_emstorage_add_mail);
9827 EM_DEBUG_FUNC_END("ret [%d]", ret);
9831 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)
9833 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);
9835 int ret = false, i, cur_conditional_clause = 0;
9836 int error = EMAIL_ERROR_NONE;
9837 int target_account_id;
9838 int conditional_clause_len = 0;
9839 char *sql_query_string = NULL, *conditional_clause = NULL;
9840 emstorage_mailbox_tbl_t *result_mailbox = NULL;
9841 email_mailbox_type_e target_mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED;
9842 char* target_mailbox_name = NULL;
9844 if (!mail_ids || input_mailbox_id <= 0) {
9845 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9846 if (err_code != NULL)
9847 *err_code = EMAIL_ERROR_INVALID_PARAM;
9851 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9853 if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &result_mailbox)) != EMAIL_ERROR_NONE || !result_mailbox) {
9854 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", error);
9855 if (err_code != NULL)
9860 if (result_mailbox->mailbox_name) {
9861 if (strstr(result_mailbox->mailbox_name, "'"))
9862 target_mailbox_name = em_replace_all_string(result_mailbox->mailbox_name, "'", "''");
9864 target_mailbox_name = strdup(result_mailbox->mailbox_name);
9867 target_mailbox_type = result_mailbox->mailbox_type;
9868 target_account_id = result_mailbox->account_id;
9869 emstorage_free_mailbox(&result_mailbox, 1, NULL);
9871 conditional_clause_len = (sizeof(char) * 8 * number_of_mails) + 512;
9872 conditional_clause = em_malloc(conditional_clause_len);
9873 if (conditional_clause == NULL) {
9874 EM_DEBUG_EXCEPTION("em_mallocfailed");
9875 error = EMAIL_ERROR_OUT_OF_MEMORY;
9878 cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE mail_id in (");
9880 for (i = 0; i < number_of_mails; i++)
9881 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
9884 char *last_comma = rindex(conditional_clause, ',');
9885 if (last_comma) *last_comma = ')'; /* replace , with) */
9887 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9889 /* Updating a mail_tbl */
9891 sql_query_string = em_malloc(conditional_clause_len);
9892 if (sql_query_string == NULL) {
9893 EM_DEBUG_EXCEPTION("em_mallocfailed");
9894 error = EMAIL_ERROR_OUT_OF_MEMORY;
9897 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);
9898 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9900 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9901 if (error != EMAIL_ERROR_NONE) {
9902 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9906 /* Updating a mail_attachment_tbl */
9907 memset(sql_query_string, 0x00, conditional_clause_len);
9908 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);
9909 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9910 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9911 if (error != EMAIL_ERROR_NONE) {
9912 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9916 /* Updating a mail_meeting_tbl */
9917 memset(sql_query_string, 0x00, conditional_clause_len);
9918 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);
9919 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9920 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9921 if (error != EMAIL_ERROR_NONE) {
9922 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9926 #ifdef __FEATURE_BODY_SEARCH__
9927 /* Updating mail_text_tbl */
9928 memset(sql_query_string, 0x00, conditional_clause_len);
9929 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);
9930 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9931 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9932 if (error != EMAIL_ERROR_NONE) {
9933 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9938 /* Updating a mail_read_mail_uid_tbl */
9939 memset(conditional_clause, 0x00, conditional_clause_len);
9940 cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE local_uid in (");
9942 for (i = 0; i < number_of_mails; i++)
9943 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
9946 last_comma = rindex(conditional_clause, ',');
9947 if (last_comma) *last_comma = ')'; /* replace , with) */
9949 memset(sql_query_string, 0x00, conditional_clause_len);
9950 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);
9951 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9952 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9953 if (error != EMAIL_ERROR_NONE) {
9954 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9961 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9963 EM_SAFE_FREE(target_mailbox_name);
9964 EM_SAFE_FREE(conditional_clause);
9965 EM_SAFE_FREE(sql_query_string);
9967 if (err_code != NULL)
9970 EM_DEBUG_FUNC_END("ret [%d]", ret);
9974 INTERNAL_FUNC int emstorage_delete_mail(char *multi_user_name, int mail_id, int from_server, int transaction, int *err_code)
9976 EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9979 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
9980 if (err_code != NULL)
9981 *err_code = EMAIL_ERROR_INVALID_PARAM;
9986 int error = EMAIL_ERROR_NONE;
9987 char sql_query_string[QUERY_SIZE] = {0, };
9988 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9989 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9991 memset(sql_query_string, 0x00, sizeof(sql_query_string));
9993 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id = %d ", mail_id);
9994 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9995 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9996 if (error != EMAIL_ERROR_NONE) {
9997 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10004 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10006 if (err_code != NULL)
10009 EM_DEBUG_FUNC_END("ret [%d]", ret);
10013 INTERNAL_FUNC int emstorage_delete_multiple_mails(char *multi_user_name, int mail_ids[], int number_of_mails, int transaction, int *err_code)
10015 EM_DEBUG_FUNC_BEGIN("mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", mail_ids, number_of_mails, transaction, err_code);
10017 int ret = false, i, cur_sql_query_string = 0;
10018 int error = EMAIL_ERROR_NONE;
10019 int query_size = 0;
10020 char *sql_query_string = NULL;
10023 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10024 if (err_code != NULL)
10025 *err_code = EMAIL_ERROR_INVALID_PARAM;
10029 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10030 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10032 query_size = (sizeof(char) * 8 * number_of_mails) + 512;
10033 sql_query_string = em_malloc(query_size);
10034 if (sql_query_string == NULL) {
10035 EM_DEBUG_EXCEPTION("em_mallocfailed");
10036 error = EMAIL_ERROR_OUT_OF_MEMORY;
10040 cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_tbl WHERE mail_id in (");
10042 for (i = 0; i < number_of_mails; i++)
10043 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
10045 /* prevent 34414 */
10046 char *last_comma = rindex(sql_query_string, ',');
10047 if (last_comma != NULL) *last_comma = ')'; /* replace , with) */
10049 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10050 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10051 if (error != EMAIL_ERROR_NONE) {
10052 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10056 #ifdef __FEATURE_BODY_SEARCH__
10057 /* delete mail_text from mail_text_tbl */
10058 cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_text_tbl WHERE mail_id in (");
10060 for (i = 0; i < number_of_mails; i++)
10061 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
10063 last_comma = rindex(sql_query_string, ',');
10064 *last_comma = ')'; /* replace , with) */
10066 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10067 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10068 if (error != EMAIL_ERROR_NONE) {
10069 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10077 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10079 EM_SAFE_FREE(sql_query_string);
10080 if (err_code != NULL)
10083 EM_DEBUG_FUNC_END("ret [%d]", ret);
10087 INTERNAL_FUNC int emstorage_delete_mail_by_account(char *multi_user_name, int account_id, int transaction, int *err_code)
10089 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
10091 if (account_id < FIRST_ACCOUNT_ID) {
10092 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
10094 if (err_code != NULL)
10095 *err_code = EMAIL_ERROR_INVALID_PARAM;
10099 int rc, ret = false;
10100 int error = EMAIL_ERROR_NONE;
10101 char sql_query_string[QUERY_SIZE] = {0, };
10103 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10104 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10106 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d", account_id);
10107 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10108 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10109 if (error != EMAIL_ERROR_NONE) {
10110 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10114 rc = sqlite3_changes(local_db_handle);
10116 EM_DEBUG_LOG(" no mail found...");
10117 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10120 /* Delete all mails mail_read_mail_uid_tbl table based on account id */
10121 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
10122 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10123 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10124 if (error != EMAIL_ERROR_NONE) {
10125 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10129 rc = sqlite3_changes(local_db_handle);
10131 EM_DEBUG_LOG("No mail found...");
10132 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10135 #ifdef __FEATURE_BODY_SEARCH__
10136 /* Delete all mail_text in mail_text_tbl table based on account id */
10137 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE account_id = %d", account_id);
10138 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10139 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10140 if (error != EMAIL_ERROR_NONE) {
10141 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10145 rc = sqlite3_changes(local_db_handle);
10147 EM_DEBUG_LOG("No mail found...");
10148 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10155 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10157 if (error == EMAIL_ERROR_NONE) {
10158 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_WITH_ACCOUNT, account_id, 0 , NULL, 0))
10159 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAIL_DELETE_ALL ]");
10162 if (err_code != NULL)
10165 EM_DEBUG_FUNC_END("ret [%d]", ret);
10169 INTERNAL_FUNC int emstorage_delete_mail_by_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox, int transaction, int *err_code)
10171 EM_DEBUG_FUNC_BEGIN("mailbox[%p], transaction[%d], err_code[%p]", mailbox, transaction, err_code);
10173 if (mailbox == NULL) {
10174 EM_DEBUG_EXCEPTION("mailbox [%p]", mailbox);
10175 if (err_code != NULL)
10176 *err_code = EMAIL_ERROR_INVALID_PARAM;
10181 int error = EMAIL_ERROR_NONE;
10182 char sql_query_string[QUERY_SIZE] = {0, };
10183 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10184 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10186 if (strcmp(mailbox->mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
10187 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);
10188 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10189 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10190 if (error != EMAIL_ERROR_NONE) {
10191 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10195 /* Delete Mails from mail_read_mail_uid_tbl */
10196 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);
10197 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10198 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10199 if (error != EMAIL_ERROR_NONE) {
10200 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10204 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);
10205 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10206 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10207 if (error != EMAIL_ERROR_NONE) {
10208 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10212 /* Delete Mails from mail_read_mail_uid_tbl */
10213 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);
10214 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10215 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10216 if (error != EMAIL_ERROR_NONE) {
10217 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10221 #ifdef __FEATURE_BODY_SEARCH__
10222 /* Delete Mails from mail_text_tbl */
10223 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);
10224 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10225 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10226 if (error != EMAIL_ERROR_NONE) {
10227 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10235 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10237 if (error == EMAIL_ERROR_NONE) {
10238 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_ALL, mailbox->account_id, mailbox->mailbox_id , mailbox->mailbox_name, 0))
10239 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventFailed [ NOTI_MAIL_DELETE_ALL ] >>>> ");
10242 if (err_code != NULL)
10245 EM_DEBUG_FUNC_END("ret [%d]", ret);
10249 INTERNAL_FUNC int emstorage_free_mail(emstorage_mail_tbl_t** mail_list, int count, int *err_code)
10251 EM_DEBUG_FUNC_BEGIN("mail_list[%p], count[%d], err_code[%p]", mail_list, count, err_code);
10254 if ((mail_list == NULL) || (*mail_list == NULL)) {
10255 EM_DEBUG_EXCEPTION("mail_ilst[%p], count[%d]", mail_list, count);
10258 *err_code = EMAIL_ERROR_INVALID_PARAM;
10262 emstorage_mail_tbl_t* p = *mail_list;
10265 for (; i < count; i++, p++) {
10266 EM_SAFE_FREE(p->server_mailbox_name);
10267 EM_SAFE_FREE(p->server_mail_id);
10268 EM_SAFE_FREE(p->full_address_from);
10269 EM_SAFE_FREE(p->full_address_reply);
10270 EM_SAFE_FREE(p->full_address_to);
10271 EM_SAFE_FREE(p->full_address_cc);
10272 EM_SAFE_FREE(p->full_address_bcc);
10273 EM_SAFE_FREE(p->full_address_return);
10274 EM_SAFE_FREE(p->subject);
10275 EM_SAFE_FREE(p->file_path_plain);
10276 EM_SAFE_FREE(p->file_path_html);
10277 EM_SAFE_FREE(p->file_path_mime_entity);
10278 EM_SAFE_FREE(p->message_id);
10279 EM_SAFE_FREE(p->email_address_sender);
10280 EM_SAFE_FREE(p->email_address_recipient);
10281 EM_SAFE_FREE(p->preview_text);
10282 EM_SAFE_FREE(p->alias_sender);
10283 EM_SAFE_FREE(p->alias_recipient);
10284 EM_SAFE_FREE(p->default_charset);
10285 EM_SAFE_FREE(p->pgp_password);
10286 EM_SAFE_FREE(p->eas_data);
10287 EM_SAFE_FREE(p->user_name);
10289 EM_SAFE_FREE(*mail_list);
10292 if (err_code != NULL)
10293 *err_code = EMAIL_ERROR_NONE;
10295 EM_DEBUG_FUNC_END();
10299 #ifdef __FEATURE_BODY_SEARCH__
10300 INTERNAL_FUNC void emstorage_free_mail_text(emstorage_mail_text_tbl_t** mail_text_list, int count, int *err_code)
10302 EM_DEBUG_FUNC_BEGIN("mail_text_list[%p], count[%d], err_code[%p]", mail_text_list, count, err_code);
10305 if ((mail_text_list == NULL) || (*mail_text_list == NULL)) {
10306 EM_DEBUG_LOG("Nothing to free: mail_text_list[%p]", mail_text_list);
10310 emstorage_mail_text_tbl_t *p = *mail_text_list;
10313 for (; i < count; i++, p++)
10314 EM_SAFE_FREE(p->body_text);
10316 EM_SAFE_FREE(*mail_text_list);
10319 EM_DEBUG_FUNC_END();
10323 INTERNAL_FUNC int emstorage_get_attachment_count(char *multi_user_name, int mail_id, int *count, int transaction, int *err_code)
10325 EM_DEBUG_FUNC_BEGIN("mail_id[%d], count[%p], transaction[%d], err_code[%p]", mail_id, count, transaction, err_code);
10327 if (mail_id <= 0 || !count) {
10328 EM_DEBUG_EXCEPTION("mail_id[%d], count[%p]", mail_id, count);
10329 if (err_code != NULL)
10330 *err_code = EMAIL_ERROR_INVALID_PARAM;
10334 int rc = -1, ret = false;
10335 int error = EMAIL_ERROR_NONE;
10336 char sql_query_string[QUERY_SIZE] = {0, };
10338 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10339 EMSTORAGE_START_READ_TRANSACTION(transaction);
10341 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
10345 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10346 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10347 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10349 *count = atoi(result[1]);
10350 sqlite3_free_table(result);
10355 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10357 if (err_code != NULL)
10360 EM_DEBUG_FUNC_END("ret [%d]", ret);
10364 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)
10366 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);
10368 if (input_mail_id <= 0 || !output_attachment_list || !output_attachment_count) {
10369 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10370 return EMAIL_ERROR_INVALID_PARAM;
10373 int error = EMAIL_ERROR_NONE;
10376 char **result = NULL;
10377 char sql_query_string[QUERY_SIZE] = {0, };
10378 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10379 DB_STMT hStmt = NULL;
10380 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10382 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
10383 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", input_mail_id);
10384 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10385 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10386 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10388 *output_attachment_count = atoi(result[1]);
10389 sqlite3_free_table(result);
10391 if (*output_attachment_count == 0) {
10392 error = EMAIL_ERROR_NONE;
10396 p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * (*output_attachment_count));
10399 EM_DEBUG_EXCEPTION("em_mallocfailed...");
10400 error = EMAIL_ERROR_OUT_OF_MEMORY;
10404 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE mail_id = %d ORDER BY attachment_id", input_mail_id);
10405 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
10407 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10408 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)));
10410 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10411 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
10413 if (rc == SQLITE_DONE) {
10414 EM_DEBUG_EXCEPTION("no matched attachment found...");
10415 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10418 for (i = 0; i < *output_attachment_count; i++) {
10419 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10420 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10421 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10422 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10423 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10424 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10425 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10426 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10427 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10428 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10429 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10430 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10431 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10432 EM_DEBUG_LOG("attachment[%d].attachment_id : %d", i, p_data_tbl[i].attachment_id);
10433 EM_DEBUG_LOG("attachment_mime_type : %s", p_data_tbl[i].attachment_mime_type);
10434 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10435 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
10440 if (error == EMAIL_ERROR_NONE)
10441 *output_attachment_list = p_data_tbl;
10442 else if (p_data_tbl != NULL)
10443 emstorage_free_attachment(&p_data_tbl, *output_attachment_count, NULL);
10446 rc = sqlite3_finalize(hStmt);
10447 if (rc != SQLITE_OK) {
10448 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10449 error = EMAIL_ERROR_DB_FAILURE;
10450 if (*output_attachment_list)
10451 emstorage_free_attachment(output_attachment_list, *output_attachment_count, NULL); /* prevent */
10455 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
10457 EM_DEBUG_FUNC_END("error [%d]", error);
10461 INTERNAL_FUNC int emstorage_get_attachment(char *multi_user_name, int attachment_id, emstorage_attachment_tbl_t **attachment, int transaction, int *err_code)
10463 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], transaction[%d], err_code[%p]", attachment_id, attachment, transaction, err_code);
10465 if (attachment_id <= 0 || !attachment) {
10466 EM_DEBUG_EXCEPTION("attachment_id[%d], attachment[%p]", attachment_id, attachment);
10467 if (err_code != NULL)
10468 *err_code = EMAIL_ERROR_INVALID_PARAM;
10472 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10473 int rc, ret = false;
10474 int error = EMAIL_ERROR_NONE;
10475 char sql_query_string[QUERY_SIZE] = {0, };
10477 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10478 EMSTORAGE_START_READ_TRANSACTION(transaction);
10480 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
10482 sqlite3_stmt* hStmt = NULL;
10484 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10485 EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
10487 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10488 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10491 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10492 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10493 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10495 if (rc == SQLITE_DONE) {
10496 EM_DEBUG_LOG("no matched attachment found...");
10497 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10501 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10502 EM_DEBUG_EXCEPTION("malloc failed...");
10503 error = EMAIL_ERROR_OUT_OF_MEMORY;
10507 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10508 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10509 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10510 _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10511 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10512 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10513 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10514 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10515 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10516 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10517 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10518 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10519 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10521 #ifdef __ATTACHMENT_OPTI__
10522 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10523 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10530 *attachment = p_data_tbl;
10532 if (hStmt != NULL) {
10533 rc = sqlite3_finalize(hStmt);
10534 if (rc != SQLITE_OK) {
10535 EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10536 error = EMAIL_ERROR_DB_FAILURE;
10540 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10542 if (err_code != NULL)
10545 EM_DEBUG_FUNC_END("ret [%d]", ret);
10549 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)
10551 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);
10553 if (mail_id <= 0 || nth <= 0 || !attachment_tbl) {
10554 EM_DEBUG_EXCEPTION(" mail_id[%d], nth[%d], attachment[%p]", mail_id, nth, attachment_tbl);
10556 if (err_code != NULL)
10557 *err_code = EMAIL_ERROR_INVALID_PARAM;
10561 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10563 int rc, ret = false;
10564 int error = EMAIL_ERROR_NONE;
10565 char sql_query_string[QUERY_SIZE] = {0, };
10567 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10568 EMSTORAGE_START_READ_TRANSACTION(transaction);
10570 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));
10571 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10573 DB_STMT hStmt = NULL;
10575 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10576 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10577 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10580 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10581 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10582 ("sqlite3_step fail:%d", rc));
10584 if (rc == SQLITE_DONE) {
10585 EM_DEBUG_EXCEPTION("no matched attachment found: mail_id[%d] nth[%d]", mail_id, nth);
10586 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10590 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10591 EM_DEBUG_EXCEPTION(" malloc failed...");
10592 error = EMAIL_ERROR_OUT_OF_MEMORY;
10596 p_data_tbl->attachment_id = sqlite3_column_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10597 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10598 p_data_tbl->attachment_name = cpy_str(p);
10599 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10600 p_data_tbl->attachment_path = cpy_str(p);
10601 if ((p = (char *)sqlite3_column_text(hStmt, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10602 p_data_tbl->content_id = cpy_str(p);
10603 p_data_tbl->attachment_size = sqlite3_column_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10604 p_data_tbl->mail_id = sqlite3_column_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10605 p_data_tbl->account_id = sqlite3_column_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10606 p_data_tbl->mailbox_id = sqlite3_column_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10607 p_data_tbl->attachment_save_status = sqlite3_column_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10608 p_data_tbl->attachment_drm_type = sqlite3_column_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10609 p_data_tbl->attachment_drm_method = sqlite3_column_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10610 p_data_tbl->attachment_inline_content_status = sqlite3_column_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10611 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10612 p_data_tbl->attachment_mime_type = cpy_str(p);
10613 #ifdef __ATTACHMENT_OPTI__
10614 p_data_tbl->encoding = sqlite3_column_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10615 if ((p = (char *)sqlite3_column_text(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10616 p_data_tbl->section = cpy_str(p);
10622 *attachment_tbl = p_data_tbl;
10624 if (hStmt != NULL) {
10625 rc = sqlite3_finalize(hStmt);
10626 if (rc != SQLITE_OK) {
10627 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10628 error = EMAIL_ERROR_DB_FAILURE;
10632 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10634 if (err_code != NULL)
10637 EM_DEBUG_FUNC_END("ret [%d]", ret);
10641 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)
10643 EM_DEBUG_FUNC_BEGIN("attachment_path[%p], attachment[%p], transaction[%d], err_code[%p]", attachment_path, attachment, transaction, err_code);
10645 if (attachment_path == NULL || !attachment) {
10646 EM_DEBUG_EXCEPTION("attachment_path[%p], attachment[%p]", attachment_path, attachment);
10647 if (err_code != NULL)
10648 *err_code = EMAIL_ERROR_INVALID_PARAM;
10652 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10653 int rc, ret = false;
10654 int error = EMAIL_ERROR_NONE;
10655 char sql_query_string[QUERY_SIZE] = {0, };
10657 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10658 EMSTORAGE_START_READ_TRANSACTION(transaction);
10660 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_path = '%s'", attachment_path);
10662 sqlite3_stmt* hStmt = NULL;
10664 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10665 EM_DEBUG_LOG_DEV("before sqlite3_prepare hStmt = %p", hStmt);
10667 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10668 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10671 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10672 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10673 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10675 if (rc == SQLITE_DONE) {
10676 EM_DEBUG_LOG("no matched attachment found...");
10677 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10681 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10682 EM_DEBUG_EXCEPTION("malloc failed...");
10683 error = EMAIL_ERROR_OUT_OF_MEMORY;
10687 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10688 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10689 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10690 _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10691 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10692 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10693 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10694 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10695 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10696 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10697 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10698 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10699 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10701 #ifdef __ATTACHMENT_OPTI__
10702 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10703 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10710 *attachment = p_data_tbl;
10712 if (hStmt != NULL) {
10713 rc = sqlite3_finalize(hStmt);
10714 if (rc != SQLITE_OK) {
10715 EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10716 error = EMAIL_ERROR_DB_FAILURE;
10720 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10722 if (err_code != NULL)
10725 EM_DEBUG_FUNC_END("ret [%d]", ret);
10729 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)
10731 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], attachment[%p], transaction[%d], err_code[%p]", mail_id, type, attachment, transaction, err_code);
10733 if (mail_id <= 0 || !attachment) {
10734 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], attachment[%p]", mail_id, type, attachment);
10735 if (err_code != NULL)
10736 *err_code = EMAIL_ERROR_INVALID_PARAM;
10740 int rc, ret = false;
10741 int error = EMAIL_ERROR_NONE;
10742 DB_STMT hStmt = NULL;
10743 char sql_query_string[QUERY_SIZE] = {0, };
10747 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10749 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10752 case UPDATE_MAILBOX:
10753 EM_DEBUG_LOG("UPDATE_MAILBOX");
10754 if (!attachment->mailbox_id) {
10755 EM_DEBUG_EXCEPTION(" attachment->mailbox_id[%d]", attachment->mailbox_id);
10756 error = EMAIL_ERROR_INVALID_PARAM;
10759 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10760 "UPDATE mail_attachment_tbl SET mailbox_id = ? WHERE mail_id = %d", mail_id);
10762 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10763 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10764 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10765 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10767 _bind_stmt_field_data_int(hStmt, i++, attachment->mailbox_id);
10770 case UPDATE_SAVENAME:
10771 EM_DEBUG_LOG("UPDATE_SAVENAME");
10772 if (!attachment->attachment_path) {
10773 EM_DEBUG_EXCEPTION(" attachment->attachment_path[%p]", attachment->attachment_path);
10774 error = EMAIL_ERROR_INVALID_PARAM;
10778 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10779 "UPDATE mail_attachment_tbl SET"
10780 " attachment_size = ?"
10781 ", attachment_save_status = ?"
10782 ", attachment_path = ?"
10783 " WHERE mail_id = %d"
10784 " AND attachment_id = %d"
10785 , attachment->mail_id
10786 , attachment->attachment_id);
10789 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10790 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10791 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10792 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10794 _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_size);
10795 _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_save_status);
10796 _bind_stmt_field_data_string(hStmt, i++, (char *)attachment->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10800 EM_DEBUG_LOG("type[%d]", type);
10801 error = EMAIL_ERROR_INVALID_PARAM;
10804 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10806 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10807 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10808 ("sqlite3_step fail:%d", rc));
10813 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10815 if (hStmt != NULL) {
10816 rc = sqlite3_finalize(hStmt);
10817 if (rc != SQLITE_OK) {
10818 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10819 error = EMAIL_ERROR_DB_FAILURE;
10823 if (err_code != NULL)
10825 EM_DEBUG_FUNC_END("ret [%d]", ret);
10829 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)
10831 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);
10835 int error = EMAIL_ERROR_NONE;
10837 int account_id = 0;
10838 char sql_query_string[QUERY_SIZE] = {0, };
10839 char *replaced_mailbox_name = NULL;
10840 char *replaced_alias = NULL;
10841 sqlite3 *local_db_handle = NULL;
10842 DB_STMT hStmt = NULL;
10843 emstorage_mailbox_tbl_t *old_mailbox_data = NULL;
10845 if (input_mailbox_id <= 0 || !input_new_mailbox_name || !input_new_mailbox_alias) {
10846 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10847 return EMAIL_ERROR_INVALID_PARAM;
10850 if (strstr(input_new_mailbox_name, "'"))
10851 replaced_mailbox_name = em_replace_all_string(input_new_mailbox_name, "'", "''");
10853 replaced_mailbox_name = strdup(input_new_mailbox_name);
10855 if (strstr(input_new_mailbox_alias, "'"))
10856 replaced_alias = em_replace_all_string(input_new_mailbox_alias, "'", "''");
10858 replaced_alias = strdup(input_new_mailbox_alias);
10860 local_db_handle = emstorage_get_db_connection(multi_user_name);
10862 if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &old_mailbox_data)) != EMAIL_ERROR_NONE) {
10863 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", error);
10864 EM_SAFE_FREE(replaced_mailbox_name);
10865 EM_SAFE_FREE(replaced_alias);
10869 if (old_mailbox_data == NULL) {
10870 EM_DEBUG_LOG("old_mailbox_data is NULL");
10871 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
10875 account_id = old_mailbox_data->account_id;
10877 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
10879 if (input_eas_data && input_eas_data_length > 0) {
10880 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10881 "UPDATE mail_box_tbl SET"
10882 " mailbox_name = ?"
10885 ",eas_data_length = ?"
10886 " WHERE mailbox_id = ?");
10888 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10890 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10891 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10893 _bind_stmt_field_data_string(hStmt, field_idx++, input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10894 _bind_stmt_field_data_string(hStmt, field_idx++, input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10895 _bind_stmt_field_data_blob(hStmt, field_idx++, input_eas_data, input_eas_data_length);
10896 _bind_stmt_field_data_int(hStmt, field_idx++, input_eas_data_length);
10897 _bind_stmt_field_data_int(hStmt, field_idx++, input_mailbox_id);
10899 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10900 "UPDATE mail_box_tbl SET"
10901 " mailbox_name = ?"
10903 " WHERE mailbox_id = ?");
10905 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10907 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10908 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10910 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10911 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10912 _bind_stmt_field_data_int(hStmt, field_idx++ , input_mailbox_id);
10916 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10917 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
10918 ("sqlite3_step fail:%d", rc));
10919 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10920 ("sqlite3_step fail:%d", rc));
10922 if (sqlite3_changes(local_db_handle) == 0)
10923 EM_DEBUG_LOG("no mail_meeting_tbl matched...");
10928 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, error);
10929 if (error == EMAIL_ERROR_NONE) {
10930 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME, account_id, input_mailbox_id, input_new_mailbox_name, 0))
10931 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAILBOX_RENAME ] >>>> ");
10933 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME_FAIL, account_id, input_mailbox_id, input_new_mailbox_name, error))
10934 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAILBOX_RENAME_FAIL ] >>>> ");
10937 EM_SAFE_FREE(replaced_mailbox_name);
10938 EM_SAFE_FREE(replaced_alias);
10940 if (old_mailbox_data)
10941 emstorage_free_mailbox(&old_mailbox_data, 1, NULL);
10943 if (hStmt != NULL) {
10944 rc = sqlite3_finalize(hStmt);
10945 if (rc != SQLITE_OK) {
10946 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10947 error = EMAIL_ERROR_DB_FAILURE;
10951 EM_DEBUG_FUNC_END("error [%d]", error);
10955 INTERNAL_FUNC int emstorage_get_new_attachment_no(char *multi_user_name, int *attachment_no, int *err_code)
10957 EM_DEBUG_FUNC_BEGIN("attachment_no [%p], err_code[%p]", attachment_no, err_code);
10958 int rc, ret = false;
10959 int error = EMAIL_ERROR_NONE;
10960 char *sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
10963 if (!attachment_no) {
10964 EM_DEBUG_EXCEPTION("Invalid attachment");
10965 if (err_code != NULL)
10966 *err_code = EMAIL_ERROR_INVALID_PARAM;
10970 *attachment_no = -1;
10972 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10975 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
10976 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10977 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
10979 if (NULL == result[1])
10982 rc = atoi(result[1])+1;
10984 sqlite3_free_table(result);
10986 *attachment_no = rc;
10987 EM_DEBUG_LOG("attachment_no [%d]", *attachment_no);
10992 if (err_code != NULL)
10995 EM_DEBUG_FUNC_END("ret [%d]", ret);
10999 INTERNAL_FUNC int emstorage_add_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int iscopy, int transaction, int *err_code)
11001 EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], iscopy[%d], transaction[%d], err_code[%p]", attachment_tbl, iscopy, transaction, err_code);
11005 int rc, ret = false;
11006 int error = EMAIL_ERROR_NONE;
11007 char sql_query_string[QUERY_SIZE] = {0, };
11009 DB_STMT hStmt = NULL;
11010 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11012 if (!attachment_tbl) {
11013 EM_DEBUG_EXCEPTION("attachment_tbl[%p], iscopy[%d]", attachment_tbl, iscopy);
11014 if (err_code != NULL)
11015 *err_code = EMAIL_ERROR_INVALID_PARAM;
11019 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11021 sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
11023 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
11024 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
11025 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
11027 if (NULL == result[1]) rc = 1;
11028 else rc = atoi(result[1]) + 1;
11029 sqlite3_free_table(result);
11031 attachment_tbl->attachment_id = rc;
11033 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11034 "INSERT INTO mail_attachment_tbl VALUES "
11035 "(?" /* attachment_id */
11036 ", ?" /* attachment_name */
11037 ", ?" /* attachment_path */
11038 ", ?" /* content_id */
11039 ", ?" /* attachment_size */
11040 ", ?" /* mail_id */
11041 ", ?" /* account_id */
11042 ", ?" /* mailbox_id */
11043 ", ?" /* attachment_save_status */
11044 ", ?" /* attachment_drm_type */
11045 ", ?" /* attachment_drm_method */
11046 ", ?" /* attachment_inline_content_status */
11047 ", ?" /* attachment_mime_type */
11048 #ifdef __ATTACHMENT_OPTI__
11055 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11056 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11057 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11059 _bind_stmt_field_data_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_id);
11060 _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);
11061 _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);
11062 _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);
11063 _bind_stmt_field_data_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_size);
11064 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mail_id);
11065 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->account_id);
11066 _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mailbox_id);
11067 _bind_stmt_field_data_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_save_status);
11068 _bind_stmt_field_data_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_type);
11069 _bind_stmt_field_data_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_method);
11070 _bind_stmt_field_data_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_inline_content_status);
11071 _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);
11072 #ifdef __ATTACHMENT_OPTI__
11073 _bind_stmt_field_data_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->encoding);
11074 _bind_stmt_field_data_string(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->section, 0, ATTACHMENT_LEN_IN_MAIL_ATTACHMENT_TBL);
11078 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11079 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
11080 ("sqlite3_step fail:%d", rc));
11081 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11082 ("sqlite3_step fail:%d", rc));
11084 rc = sqlite3_changes(local_db_handle);
11086 EM_DEBUG_LOG(" no matched mail found...");
11087 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11094 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11096 if (hStmt != NULL) {
11097 rc = sqlite3_finalize(hStmt);
11098 if (rc != SQLITE_OK) {
11099 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11100 error = EMAIL_ERROR_DB_FAILURE;
11104 if (err_code != NULL)
11107 EM_DEBUG_FUNC_END("ret [%d]", ret);
11111 INTERNAL_FUNC int emstorage_update_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int transaction, int *err_code)
11113 EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], transaction[%d], err_code[%p]", attachment_tbl, transaction, err_code);
11115 int rc, ret = false, field_idx = 0;
11116 int error = EMAIL_ERROR_NONE;
11117 DB_STMT hStmt = NULL;
11118 char sql_query_string[QUERY_SIZE] = {0, };
11120 if (!attachment_tbl) {
11121 EM_DEBUG_EXCEPTION(" attachment_tbl[%p] ", attachment_tbl);
11123 *err_code = EMAIL_ERROR_INVALID_PARAM;
11127 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11129 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11131 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11132 "UPDATE mail_attachment_tbl SET "
11133 " attachment_name = ?"
11134 ", attachment_path = ?"
11136 ", attachment_size = ?"
11140 ", attachment_save_status = ?"
11141 ", attachment_drm_type = ?"
11142 ", attachment_drm_method = ?"
11143 ", attachment_inline_content_status = ? "
11144 ", attachment_mime_type = ? "
11145 " WHERE attachment_id = ?;");
11148 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11150 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11151 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11153 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
11154 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
11155 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->content_id, 0, CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL);
11156 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_size);
11157 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->mail_id);
11158 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->account_id);
11159 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->mailbox_id);
11160 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_save_status);
11161 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_drm_type);
11162 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_drm_method);
11163 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_inline_content_status);
11164 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_mime_type, 0, ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL);
11165 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_id);
11168 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11169 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
11170 ("sqlite3_step fail:%d", rc));
11171 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11172 ("sqlite3_step fail:%d", rc));
11174 rc = sqlite3_changes(local_db_handle);
11176 EM_DEBUG_LOG(" no matched mail found...");
11177 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11184 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11185 if (hStmt != NULL) {
11186 rc = sqlite3_finalize(hStmt);
11187 if (rc != SQLITE_OK) {
11188 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11189 error = EMAIL_ERROR_DB_FAILURE;
11193 if (err_code != NULL)
11196 EM_DEBUG_FUNC_END("ret [%d]", ret);
11200 INTERNAL_FUNC int emstorage_delete_attachment_on_db(char *multi_user_name, int attachment_id, int transaction, int *err_code)
11202 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], transaction[%d], err_code[%p]", attachment_id, transaction, err_code);
11204 if (attachment_id < 0) {
11205 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
11206 if (err_code != NULL)
11207 *err_code = EMAIL_ERROR_INVALID_PARAM;
11212 int error = EMAIL_ERROR_NONE;
11213 char sql_query_string[QUERY_SIZE] = {0, };
11214 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11216 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11218 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
11220 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11221 if (error != EMAIL_ERROR_NONE) {
11222 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11229 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11234 EM_DEBUG_FUNC_END("ret [%d]", ret);
11238 INTERNAL_FUNC int emstorage_delete_all_attachments_of_mail(char *multi_user_name, int mail_id, int transaction, int *err_code)
11240 EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
11242 int error = EMAIL_ERROR_NONE;
11243 char sql_query_string[QUERY_SIZE] = {0, };
11244 sqlite3 *local_db_handle = NULL;
11246 if (mail_id <= 0) {
11247 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
11248 if (err_code != NULL)
11249 *err_code = EMAIL_ERROR_INVALID_PARAM;
11253 local_db_handle = emstorage_get_db_connection(multi_user_name);
11255 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11257 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
11258 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11259 if (error != EMAIL_ERROR_NONE) {
11260 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11267 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11272 EM_DEBUG_FUNC_END("ret [%d]", ret);
11276 INTERNAL_FUNC int emstorage_delete_attachment_all_on_db(char *multi_user_name, int account_id, char *mailbox, int transaction, int *err_code)
11278 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox, transaction, err_code);
11280 int error = EMAIL_ERROR_NONE;
11282 char sql_query_string[QUERY_SIZE] = {0, };
11283 char *replaced_mailbox = NULL;
11284 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11286 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11288 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl");
11290 if (account_id != ALL_ACCOUNT) /* '0' means all account */
11291 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);
11293 if (mailbox) /* NULL means all mailbox_name */ {
11294 if (strstr(mailbox, "'"))
11295 replaced_mailbox = em_replace_all_string(mailbox, "'", "''");
11297 replaced_mailbox = strdup(mailbox);
11299 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);
11300 EM_SAFE_FREE(replaced_mailbox); /*prevent 49434*/
11303 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11304 if (error != EMAIL_ERROR_NONE) {
11305 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11312 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11314 if (err_code != NULL)
11317 EM_DEBUG_FUNC_END("ret [%d]", ret);
11321 INTERNAL_FUNC int emstorage_free_attachment(emstorage_attachment_tbl_t** attachment_tbl_list, int count, int *err_code)
11323 EM_DEBUG_FUNC_BEGIN("attachment_tbl_list[%p], count[%d], err_code[%p]", attachment_tbl_list, count, err_code);
11326 if ((attachment_tbl_list == NULL) || (*attachment_tbl_list == NULL)) {
11327 EM_DEBUG_LOG("Nothing to free: attachment_tbl_list[%p], count[%d]", attachment_tbl_list, count);
11328 if (err_code != NULL)
11329 *err_code = EMAIL_ERROR_INVALID_PARAM;
11333 emstorage_attachment_tbl_t* p = *attachment_tbl_list;
11336 for (i = 0; i < count; i++) {
11337 EM_SAFE_FREE(p[i].attachment_name);
11338 EM_SAFE_FREE(p[i].attachment_path);
11339 EM_SAFE_FREE(p[i].content_id);
11340 EM_SAFE_FREE(p[i].attachment_mime_type);
11341 #ifdef __ATTACHMENT_OPTI__
11342 EM_SAFE_FREE(p[i].section);
11347 *attachment_tbl_list = NULL;
11350 if (err_code != NULL)
11351 *err_code = EMAIL_ERROR_NONE;
11352 EM_DEBUG_FUNC_END();
11356 INTERNAL_FUNC int emstorage_begin_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11358 EM_DEBUG_FUNC_BEGIN();
11359 EM_PROFILE_BEGIN(emStorageBeginTransaction);
11362 _timedlock_shm_mutex(mapped_for_db_lock, 2);
11364 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11367 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN immediate;", NULL, NULL, NULL), rc);
11368 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11369 ("SQL(BEGIN) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11371 if (ret == false) {
11372 if (err_code != NULL) *err_code = EMAIL_ERROR_DB_FAILURE;
11375 EM_PROFILE_END(emStorageBeginTransaction);
11376 EM_DEBUG_FUNC_END("ret [%d]", ret);
11380 INTERNAL_FUNC int emstorage_commit_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11382 EM_DEBUG_FUNC_BEGIN();
11386 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11388 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
11389 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; }, ("SQL(END) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11391 if (ret == false && err_code != NULL)
11392 *err_code = EMAIL_ERROR_DB_FAILURE;
11394 _unlockshm_mutex(mapped_for_db_lock);
11396 EM_DEBUG_FUNC_END("ret [%d]", ret);
11400 INTERNAL_FUNC int emstorage_rollback_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11402 EM_DEBUG_FUNC_BEGIN();
11405 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11407 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "ROLLBACK;", NULL, NULL, NULL), rc);
11408 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11409 ("SQL(ROLLBACK) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11411 if (ret == false && err_code != NULL)
11412 *err_code = EMAIL_ERROR_DB_FAILURE;
11414 _unlockshm_mutex(mapped_for_db_lock);
11416 EM_DEBUG_FUNC_END("ret [%d]", ret);
11420 INTERNAL_FUNC int emstorage_is_mailbox_full(char *multi_user_name, int account_id, email_mailbox_t *mailbox, int *result, int *err_code)
11422 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], result[%p], err_code[%p]", account_id, mailbox, result, err_code);
11424 if (account_id < FIRST_ACCOUNT_ID || !mailbox || !result) {
11426 EM_DEBUG_EXCEPTION("Invalid Parameter. accoun_id[%d], mailbox[%p]", account_id, mailbox);
11428 if (err_code != NULL)
11429 *err_code = EMAIL_ERROR_INVALID_PARAM;
11435 int error = EMAIL_ERROR_NONE;
11436 int mail_count = 0;
11438 if (!emstorage_get_mail_count(multi_user_name, account_id, mailbox->mailbox_id, &mail_count, NULL, true, &error)) {
11439 EM_DEBUG_EXCEPTION("emstorage_get_mail_count failed [%d]", error);
11444 EM_DEBUG_LOG("mail_count[%d] mail_slot_size[%d]", mail_count, mailbox->mail_slot_size);
11445 if (mail_count >= mailbox->mail_slot_size)
11456 if (err_code != NULL)
11459 EM_DEBUG_FUNC_END("ret [%d]", ret);
11463 INTERNAL_FUNC int emstorage_clear_mail_data(char *multi_user_name, int transaction, int *err_code)
11465 EM_DEBUG_FUNC_BEGIN("transaction[%d], err_code[%p]", transaction, err_code);
11468 int error = EMAIL_ERROR_NONE;
11469 char sql_query_string[QUERY_SIZE] = {0, };
11470 const email_db_object_t* tables = _g_db_tables;
11471 const email_db_object_t* indexes = _g_db_indexes;
11473 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11474 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11476 if (!emstorage_delete_dir((char *)MAIL_HOME, &error)) {
11477 EM_DEBUG_EXCEPTION(" emstorage_delete_dir failed - %d", error);
11482 mkdir(MAIL_HOME, DIRECTORY_PERMISSION);
11483 mkdir(MAIL_TEMP, DIRECTORY_PERMISSION);
11484 chmod(MAIL_TEMP, 0777);
11486 /* first clear index. */
11487 while (indexes->object_name) {
11488 if (indexes->data_flag) {
11489 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP index %s", indexes->object_name);
11490 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11491 if (error != EMAIL_ERROR_NONE) {
11492 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11499 while (tables->object_name) {
11500 if (tables->data_flag) {
11501 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP table %s", tables->object_name);
11502 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11503 if (error != EMAIL_ERROR_NONE) {
11504 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11514 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11516 if (err_code != NULL)
11519 EM_DEBUG_FUNC_END("ret [%d]", ret);
11522 /*======================= DB File Utils =============================================*/
11523 #include <dirent.h>
11524 #include <sys/types.h>
11525 #define DIR_SEPERATOR "/"
11527 INTERNAL_FUNC char *emstorage_make_directory_path_from_file_path(char *file_name)
11529 EM_DEBUG_FUNC_BEGIN("Filename [ %p ]", file_name);
11530 char delims[] = "/";
11531 char *result = NULL;
11532 gchar **token = NULL;
11534 token = g_strsplit_set(file_name, delims, 1);
11536 if (token && token[0]) {
11537 EM_DEBUG_LOG_SEC(">>>> Directory_name [ %s ]", token[0]);
11538 result = EM_SAFE_STRDUP(token[0]);
11540 EM_DEBUG_LOG(">>>> No Need to create Directory");
11547 INTERNAL_FUNC int emstorage_get_save_name(char *multi_user_name, int account_id, int mail_id, int atch_id,
11548 char *fname, char *move_buf, char *path_buf, int maxlen, int *err_code)
11550 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);
11551 EM_PROFILE_BEGIN(profile_emstorage_get_save_name);
11554 int error = EMAIL_ERROR_NONE;
11555 char *dir_name = NULL;
11556 char create_dir[1024] = {0};
11557 char *temp_file = NULL;
11558 char *prefix_path = NULL;
11559 char *modified_fname = NULL;
11561 if (!move_buf || !path_buf || account_id < FIRST_ACCOUNT_ID || mail_id < 0 || atch_id < 0) {
11562 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);
11563 error = EMAIL_ERROR_INVALID_PARAM;
11567 snprintf(path_buf, 512, "%s", MAIL_HOME);
11568 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, account_id);
11571 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, mail_id);
11574 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, atch_id);
11577 temp_file = EM_SAFE_STRDUP(fname);
11578 if (temp_file && strstr(temp_file, "/"))
11579 dir_name = emstorage_make_directory_path_from_file_path(temp_file);
11583 snprintf(create_dir, sizeof(create_dir), "%s%s%s", path_buf, DIR_SEPERATOR, dir_name);
11584 EM_DEBUG_LOG(">>>>> DIR PATH [%s]", create_dir);
11585 ret = mkdir(create_dir, DIRECTORY_PERMISSION);
11586 EM_DEBUG_LOG("mkdir result = [%d]", ret);
11587 EM_SAFE_FREE(dir_name);
11591 EM_DEBUG_LOG_DEV(">>>>> fname [%s]", fname);
11593 /* Did not allow the slash */
11594 modified_fname = reg_replace_new(fname, "/", "_");
11595 EM_DEBUG_LOG_SEC("modified_fname : [%s]", modified_fname);
11597 if (modified_fname == NULL) modified_fname = g_strdup(fname);
11599 if (EM_SAFE_STRLEN(modified_fname) + EM_SAFE_STRLEN(path_buf) + strlen(DIR_SEPERATOR) > maxlen - 1) {
11600 char *modified_name = NULL;
11601 int remain_len = (maxlen - 1) - EM_SAFE_STRLEN(path_buf) - strlen(DIR_SEPERATOR);
11603 if (remain_len <= 0) {
11604 error = EMAIL_ERROR_MAX_EXCEEDED;
11608 if (remain_len > MAX_FILENAME)
11609 remain_len = MAX_FILENAME;
11611 modified_name = em_shrink_filename(modified_fname, remain_len);
11613 if (!modified_name) {
11614 error = EMAIL_ERROR_MAX_EXCEEDED;
11618 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),
11619 "%s%s", DIR_SEPERATOR, modified_name);
11620 EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11621 EM_SAFE_FREE(modified_name);
11623 if (EM_SAFE_STRLEN(modified_fname) > MAX_FILENAME - 1) {
11624 char *modified_name = NULL;
11626 modified_name = em_shrink_filename(modified_fname, MAX_FILENAME);
11627 if (!modified_name) {
11628 error = EMAIL_ERROR_MAX_EXCEEDED;
11632 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),
11633 "%s%s", DIR_SEPERATOR, modified_name);
11634 EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11635 EM_SAFE_FREE(modified_name);
11637 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),
11638 "%s%s", DIR_SEPERATOR, modified_fname);
11643 EM_DEBUG_LOG_SEC(">>>>> path_buf [%s]", path_buf);
11645 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11646 error = emcore_get_container_path(multi_user_name, &prefix_path);
11647 if (error != EMAIL_ERROR_NONE) {
11648 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11651 snprintf(move_buf, 512, "%s/%s", prefix_path, path_buf);
11652 EM_DEBUG_LOG_SEC("move_buf : [%s]", move_buf);
11654 snprintf(move_buf, 512, "%s", path_buf);
11655 EM_DEBUG_LOG_SEC("move_buf : [%s]", move_buf);
11662 EM_SAFE_FREE(temp_file);
11663 EM_SAFE_FREE(prefix_path);
11664 EM_SAFE_FREE(modified_fname);
11666 if (err_code != NULL)
11669 EM_PROFILE_END(profile_emstorage_get_save_name);
11670 EM_DEBUG_FUNC_END("ret [%d]", ret);
11675 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)
11677 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);
11679 if (!name_buf || account_id < FIRST_ACCOUNT_ID) {
11680 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);
11681 if (err_code != NULL)
11682 *err_code = EMAIL_ERROR_INVALID_PARAM;
11686 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
11689 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, mail_id);
11694 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, atch_id);
11699 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), ".DELE");
11701 EM_DEBUG_FUNC_END();
11706 INTERNAL_FUNC int emstorage_create_dir(char *multi_user_name, int account_id, int mail_id, int atch_id, int *err_code)
11708 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], err_code[%p]", account_id, mail_id, atch_id, err_code);
11709 EM_PROFILE_BEGIN(profile_emcore_save_create_dir);
11711 int error = EMAIL_ERROR_NONE;
11714 char *prefix_path = NULL;
11715 char errno_buf[ERRNO_BUF_SIZE] = {0};
11717 memset(buf, 0x00, sizeof(buf));
11719 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11720 error = emcore_get_container_path(multi_user_name, &prefix_path);
11721 if (error != EMAIL_ERROR_NONE) {
11722 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11726 prefix_path = strdup("");
11729 if (account_id >= FIRST_ACCOUNT_ID) {
11730 SNPRINTF(buf, sizeof(buf), "%s%s%s%s%d", prefix_path,
11736 if (stat(buf, &sbuf) == 0) {
11737 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11738 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11739 error = EMAIL_ERROR_SYSTEM_FAILURE;
11743 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11744 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11745 EM_DEBUG_EXCEPTION("mkdir failed: %s", EM_STRERROR(errno_buf));
11746 error = EMAIL_ERROR_SYSTEM_FAILURE;
11748 error = EMAIL_ERROR_OUT_OF_MEMORY;
11752 if (account_id == EML_FOLDER)
11758 int space_left_in_buffer = sizeof(buf) - EM_SAFE_STRLEN(buf);
11760 if (account_id < FIRST_ACCOUNT_ID) {
11761 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11762 error = EMAIL_ERROR_INVALID_PARAM;
11766 if (space_left_in_buffer + 10 > sizeof(buf)) {
11767 EM_DEBUG_EXCEPTION("Buffer overflowed");
11768 error = EMAIL_ERROR_OUT_OF_MEMORY;
11772 SNPRINTF(buf+EM_SAFE_STRLEN(buf), space_left_in_buffer, "%s%d", DIR_SEPERATOR, mail_id);
11774 if (stat(buf, &sbuf) == 0) {
11775 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11776 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11777 error = EMAIL_ERROR_SYSTEM_FAILURE;
11781 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11782 EM_DEBUG_EXCEPTION("mkdir failed [%s]", buf);
11783 EM_DEBUG_EXCEPTION("mkdir failed [%d][%s]", errno, EM_STRERROR(errno_buf));
11784 error = EMAIL_ERROR_SYSTEM_FAILURE;
11786 error = EMAIL_ERROR_OUT_OF_MEMORY;
11790 if (account_id == EML_FOLDER)
11796 if (account_id < FIRST_ACCOUNT_ID || mail_id <= 0) {
11797 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11798 error = EMAIL_ERROR_INVALID_PARAM;
11802 SNPRINTF(buf+EM_SAFE_STRLEN(buf), sizeof(buf)-(EM_SAFE_STRLEN(buf)+1), "%s%d", DIR_SEPERATOR, atch_id);
11804 if (stat(buf, &sbuf) == 0) {
11805 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11806 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11807 error = EMAIL_ERROR_SYSTEM_FAILURE;
11811 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11812 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11813 error = EMAIL_ERROR_SYSTEM_FAILURE;
11815 error = EMAIL_ERROR_OUT_OF_MEMORY;
11819 if (account_id == EML_FOLDER)
11828 EM_SAFE_FREE(prefix_path);
11830 if (err_code != NULL)
11833 EM_PROFILE_END(profile_emcore_save_create_dir);
11834 EM_DEBUG_FUNC_END("ret [%d]", ret);
11838 INTERNAL_FUNC int emstorage_copy_file(char *src_file, char *dst_file, int sync_status, int *err_code)
11840 EM_DEBUG_FUNC_BEGIN_SEC("src_file[%s], dst_file[%s], err_code[%p]", src_file, dst_file, err_code);
11841 EM_DEBUG_LOG("Using the fsync function");
11843 int error = EMAIL_ERROR_NONE;
11844 struct stat st_buf;
11849 char buf[FILE_MAX_BUFFER_SIZE] = {0};
11850 char errno_buf[ERRNO_BUF_SIZE] = {0};
11852 if (!src_file || !dst_file) {
11853 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
11855 error = EMAIL_ERROR_INVALID_PARAM;
11859 if (stat(src_file, &st_buf) < 0) {
11860 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", src_file);
11862 error = EMAIL_ERROR_SYSTEM_FAILURE; /* EMAIL_ERROR_INVALID_PATH; */
11866 error = em_open(src_file, O_RDONLY, 0, &fp_src);
11867 if (error != EMAIL_ERROR_NONE) {
11868 EM_DEBUG_EXCEPTION(">>>> Source Fail em_open %s Failed: %d", src_file, error);
11872 error = em_open(dst_file, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, &fp_dst); /*prevent 24474*/
11873 if (error != EMAIL_ERROR_NONE) {
11874 EM_DEBUG_EXCEPTION(">>>> Destination Fail em_open %s: %d", dst_file, error);
11878 memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
11880 while ((nread = read(fp_src, buf, FILE_MAX_BUFFER_SIZE)) > 0) {
11881 if (nread > 0 && nread <= FILE_MAX_BUFFER_SIZE) {
11882 EM_DEBUG_LOG("Nread Value [%d]", nread);
11884 ssize_t byte_written = 0;
11885 size_t remain_byte = nread;
11889 while (remain_byte > 0 && buf_ptr && errno == 0) {
11890 byte_written = write(fp_dst, buf_ptr, remain_byte);
11892 if (byte_written < 0) {
11893 /* interrupted by a signal */
11894 if (errno == EINTR) {
11899 EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
11900 error = EMAIL_ERROR_UNKNOWN;
11903 EM_DEBUG_LOG("NWRITTEN [%d]", byte_written);
11904 remain_byte -= byte_written;
11905 buf_ptr += byte_written;
11909 memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
11915 EM_SAFE_CLOSE(fp_src);
11917 if (fp_dst >= 0) { /*prevent 24474*/
11919 EM_DEBUG_LOG("Before fsync");
11925 if (nread < 0 || error == EMAIL_ERROR_UNKNOWN)
11928 if (err_code != NULL)
11930 EM_DEBUG_FUNC_END("ret [%d]", ret);
11933 /* create Directory if user has deleted [deepam.p@samsung.com] */
11934 INTERNAL_FUNC void emstorage_create_dir_if_delete()
11936 EM_DEBUG_FUNC_BEGIN();
11938 mkdir(DATA_PATH, DIRECTORY_PERMISSION);
11939 mkdir(EMAIL_PATH, DIRECTORY_PERMISSION);
11940 mkdir(MAIL_HOME, DIRECTORY_PERMISSION);
11941 mkdir(MAIL_TEMP, DIRECTORY_PERMISSION);
11942 chmod(MAIL_TEMP, 0777);
11944 EM_DEBUG_FUNC_END();
11947 static int _get_temp_file_name(char **filename, int *err_code)
11949 EM_DEBUG_FUNC_BEGIN("filename[%p], err_code[%p]", filename, err_code);
11952 int error = EMAIL_ERROR_NONE;
11954 if (filename == NULL) {
11955 EM_DEBUG_EXCEPTION(" filename[%p]", filename);
11956 error = EMAIL_ERROR_INVALID_PARAM;
11960 char tempname[512] = {0x00, };
11963 gettimeofday(&tv, NULL);
11965 unsigned int seed = time(NULL);
11966 SNPRINTF(tempname, sizeof(tempname), "%s%c%d", MAIL_TEMP, '/', rand_r(&seed));
11968 char *p = EM_SAFE_STRDUP(tempname);
11970 EM_DEBUG_EXCEPTION(" strdup failed...");
11971 error = EMAIL_ERROR_OUT_OF_MEMORY;
11980 if (err_code != NULL)
11983 EM_DEBUG_FUNC_END("ret [%d]", ret);
11987 INTERNAL_FUNC int emstorage_add_content_type(char *file_path, char *char_set, int *err_code)
11989 EM_DEBUG_FUNC_BEGIN("File path [ %p ] Character Set [ %p ] err_code [ %p]", file_path, char_set, err_code);
11991 EM_IF_NULL_RETURN_VALUE(file_path, false);
11992 EM_IF_NULL_RETURN_VALUE(char_set, false);
11993 EM_IF_NULL_RETURN_VALUE(err_code, false);
11997 struct stat st_buf;
11999 char *low_char_set = NULL;
12000 char *match_str = NULL;
12003 int error = EMAIL_ERROR_NONE;
12004 int data_count_to_written = 0;
12005 char *temp_file_name = NULL;
12007 FILE* fp_src = NULL;
12008 FILE* fp_dest = NULL;
12012 if (stat(file_path, &st_buf) < 0) {
12013 EM_DEBUG_EXCEPTION_SEC(" stat(\"%s\") failed...", file_path);
12014 error = EMAIL_ERROR_SYSTEM_FAILURE;
12018 buf_size = st_buf.st_size;
12019 EM_DEBUG_LOG(">>>> File Size [ %d ] ", buf_size);
12020 buf = (char *)calloc(1, buf_size+1);
12023 EM_DEBUG_LOG(">>> Memory cannot be allocated ");
12027 error = em_fopen(file_path, "rb", &fp_src);
12028 if (error != EMAIL_ERROR_NONE || fp_src == NULL) {
12029 EM_DEBUG_EXCEPTION_SEC(" file_path fopen failed - %s [%d]", file_path, error);
12033 if ((nread = fread(buf, 1, buf_size, fp_src)) > 0) {
12034 if (nread > 0 && nread <= buf_size) {
12035 EM_DEBUG_LOG(">>>> Nread Value [ %d ] ", nread);
12038 * 1.Add check for whether content type is there.
12039 * 2. If not based on the character set, Append it in File
12042 low_char_set = calloc(1, EM_SAFE_STRLEN(char_set) + strlen(" \" /></head>") +1); /*prevent 34359*/
12043 if (low_char_set == NULL) {
12044 EM_DEBUG_EXCEPTION("calloc failed");
12045 error = EMAIL_ERROR_OUT_OF_MEMORY;
12049 strncat(low_char_set, char_set, sizeof(low_char_set)-EM_SAFE_STRLEN(low_char_set)-1);
12050 EM_DEBUG_LOG(">>>> CHAR SET [ %s ] ", low_char_set);
12051 strncat(low_char_set, " \" /></head>", sizeof(low_char_set)-EM_SAFE_STRLEN(low_char_set)-1); /*prevent 34359*/
12052 EM_DEBUG_LOG(">>> CHARSET [ %s ] ", low_char_set);
12055 match_str = strstr(buf, CONTENT_TYPE_DATA);
12057 if (match_str == NULL) {
12058 EM_DEBUG_LOG(">>>>emstorage_add_content_type 3 ");
12059 if (fp_src != NULL) {
12063 data_count_to_written = EM_SAFE_STRLEN(low_char_set)+strlen(CONTENT_DATA)+1; /*prevent 34359*/
12064 buf1 = (char *)calloc(1, data_count_to_written);
12067 strncat(buf1, CONTENT_DATA, sizeof(buf1)-EM_SAFE_STRLEN(buf1)-1); /*prevent 34359*/
12068 EM_DEBUG_LOG(">>>>> BUF 1 [ %s ] ", buf1);
12069 strncat(buf1, low_char_set, sizeof(buf1)-EM_SAFE_STRLEN(buf1)-1);
12070 EM_DEBUG_LOG(">>>> HTML TAG DATA [ %s ] ", buf1);
12072 /* 1. Create a temporary file name */
12073 if (!_get_temp_file_name(&temp_file_name, &err)) {
12074 EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed - %d", err);
12075 if (err_code != NULL) *err_code = err;
12078 EM_DEBUG_LOG_SEC(">>>>>>> TEMP APPEND FILE PATH [ %s ] ", temp_file_name);
12080 /* Open the Temp file in Append mode */
12081 error = em_fopen(temp_file_name, "ab", &fp_dest);
12082 if (error != EMAIL_ERROR_NONE) {
12083 EM_DEBUG_EXCEPTION_SEC(" fopen failed - %s [%d]", temp_file_name, error);
12087 /* 2. write the Latest data */
12088 nwritten = fwrite(buf1, data_count_to_written-1, 1, fp_dest);
12090 if (nwritten > 0) {
12091 EM_DEBUG_LOG(" Latest Data : [%d ] bytes written ", nwritten);
12093 /* 3. Append old data */
12094 nwritten = fwrite(buf, nread-1, 1, fp_dest);
12096 if (nwritten <= 0) {
12097 EM_DEBUG_EXCEPTION(" Error Occured while writing Old data : [%d ] bytes written ", nwritten);
12098 error = EMAIL_ERROR_SYSTEM_FAILURE;
12101 EM_DEBUG_LOG(">>>> OLD data appended [ %d ] ", nwritten);
12103 if (!emstorage_move_file(temp_file_name, file_path, false, &err)) {
12104 EM_DEBUG_EXCEPTION(" emstorage_move_file failed - %d", err);
12110 EM_DEBUG_EXCEPTION(" Error Occured while writing New data : [%d ] bytes written ", nwritten);
12111 error = EMAIL_ERROR_SYSTEM_FAILURE;
12123 EM_SAFE_FREE(buf1);
12124 EM_SAFE_FREE(low_char_set);
12125 EM_SAFE_FREE(temp_file_name);
12127 if (fp_src != NULL) {
12132 if (fp_dest != NULL) {
12140 EM_DEBUG_FUNC_END("ret [%d]", ret);
12145 INTERNAL_FUNC int emstorage_move_file(char *src_file, char *dst_file, int sync_status, int *err_code)
12147 EM_DEBUG_FUNC_BEGIN("src_file[%p], dst_file[%p], err_code[%p]", src_file, dst_file, err_code);
12150 int error = EMAIL_ERROR_NONE;
12151 char errno_buf[ERRNO_BUF_SIZE] = {0};
12153 if (src_file == NULL || dst_file == NULL) {
12154 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
12155 error = EMAIL_ERROR_INVALID_PARAM;
12159 EM_DEBUG_LOG_SEC("src_file[%s], dst_file[%s]", src_file, dst_file);
12161 if (strcmp(src_file, dst_file) != 0) {
12162 EM_DEBUG_LOG("oldpath and newpath are not on the same mounted file system.");
12163 if (!emstorage_copy_file(src_file, dst_file, sync_status, &error)) {
12164 EM_DEBUG_EXCEPTION("emstorage_copy_file failed - %d", error);
12166 struct stat temp_file_stat;
12167 if (stat(src_file, &temp_file_stat) < 0)
12168 EM_DEBUG_EXCEPTION("no src file found [%s] : %s", src_file, EM_STRERROR(errno_buf));
12170 if (stat(dst_file, &temp_file_stat) < 0)
12171 EM_DEBUG_EXCEPTION("no dst file found [%s] : %s", dst_file, EM_STRERROR(errno_buf));
12173 error = EMAIL_ERROR_FILE_NOT_FOUND;
12179 EM_DEBUG_LOG("src[%s] removed", src_file);
12181 EM_DEBUG_LOG("src[%s] = dst[%s]", src_file, dst_file);
12187 if (err_code != NULL)
12190 EM_DEBUG_FUNC_END("ret [%d]", ret);
12194 INTERNAL_FUNC int emstorage_delete_file(char *src_file, int *err_code)
12196 EM_DEBUG_FUNC_BEGIN("src_file[%p], err_code[%p]", src_file, err_code);
12199 int error = EMAIL_ERROR_NONE;
12201 if (src_file == NULL) {
12202 EM_DEBUG_EXCEPTION(" src_file[%p]", src_file);
12204 error = EMAIL_ERROR_INVALID_PARAM;
12208 if (remove(src_file) != 0) {
12209 if (errno != ENOENT) {
12210 EM_DEBUG_EXCEPTION(" remove failed - %d", errno);
12212 error = EMAIL_ERROR_SYSTEM_FAILURE;
12215 EM_DEBUG_EXCEPTION(" no file found...");
12217 error = EMAIL_ERROR_FILE_NOT_FOUND;
12224 if (err_code != NULL)
12227 EM_DEBUG_FUNC_END("ret [%d]", ret);
12232 INTERNAL_FUNC int emstorage_delete_dir(char *src_dir, int *err_code)
12234 EM_DEBUG_FUNC_BEGIN("src_dir[%p], err_code[%p]", src_dir, err_code);
12236 if (src_dir == NULL) {
12237 EM_DEBUG_EXCEPTION("src_dir[%p]", src_dir);
12239 if (err_code != NULL)
12240 *err_code = EMAIL_ERROR_INVALID_PARAM;
12244 int error = EMAIL_ERROR_NONE;
12251 dirp = opendir(src_dir);
12253 if (dirp == NULL) {
12254 if (errno == ENOENT) {
12255 EM_DEBUG_EXCEPTION("directory[%s] does not exist...", src_dir);
12256 if (err_code != NULL)
12257 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12260 EM_DEBUG_EXCEPTION("opendir failed [%s] [%d]", src_dir, errno);
12261 if (err_code != NULL)
12262 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12267 while ((dp = readdir(dirp))) {
12268 if (strncmp(dp->d_name, ".", 1) == 0 || strncmp(dp->d_name, "..", 2) == 0) /* prevent 34360 */
12271 SNPRINTF(buf, sizeof(buf), "%s/%s", src_dir, dp->d_name);
12273 if (lstat(buf, &sbuf) == 0 || stat(buf, &sbuf) == 0) {
12274 /* check directory */
12275 if ((sbuf.st_mode & S_IFMT) == S_IFDIR) { /* directory */
12276 /* recursive call */
12277 if (!emstorage_delete_dir(buf, &error)) {
12279 if (err_code != NULL)
12283 } else { /* file */
12284 if (remove(buf) < 0) {
12285 EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", buf, errno);
12287 if (err_code != NULL)
12288 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12293 EM_DEBUG_EXCEPTION("content does not exist...");
12298 EM_DEBUG_LOG_DEV("remove direcotory [%s]", src_dir);
12300 /* EM_DEBUG_FUNC_BEGIN(); */
12302 if (remove(src_dir) < 0) {
12303 EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", src_dir, errno);
12304 if (err_code != NULL)
12305 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12309 if (err_code != NULL)
12315 /* faizan.h@samsung.com */
12316 INTERNAL_FUNC int emstorage_update_server_uid(char *multi_user_name,
12318 char *old_server_uid,
12319 char *new_server_uid,
12322 EM_DEBUG_FUNC_BEGIN("new_server_uid[%s], old_server_uid[%s]", new_server_uid, old_server_uid);
12324 int error = EMAIL_ERROR_NONE;
12325 int transaction = true;
12326 int temp_strlen = 0;
12327 int and_operation = 0;
12328 char sql_query_string[QUERY_SIZE] = {0, };
12329 char conditional_clause_string[QUERY_SIZE] = {0};
12331 if ((mail_id <= 0 || !old_server_uid) && !new_server_uid) {
12332 EM_DEBUG_EXCEPTION("Invalid parameters");
12334 *err_code = EMAIL_ERROR_INVALID_PARAM;
12338 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12339 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12341 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE ");
12344 temp_strlen = strlen(conditional_clause_string);
12345 SNPRINTF(conditional_clause_string + temp_strlen, sizeof(conditional_clause_string) - temp_strlen,
12346 "mail_id = %d ", mail_id);
12350 if (old_server_uid) {
12351 temp_strlen = strlen(conditional_clause_string);
12352 if (!and_operation) {
12353 sqlite3_snprintf(sizeof(conditional_clause_string) - temp_strlen, conditional_clause_string + temp_strlen,
12354 "server_mail_id = '%q'", old_server_uid);
12356 sqlite3_snprintf(sizeof(conditional_clause_string) - temp_strlen, conditional_clause_string + temp_strlen,
12357 "and server_mail_id = '%q'", old_server_uid);
12361 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
12362 "UPDATE mail_tbl SET server_mail_id = '%q' %s", new_server_uid, conditional_clause_string);
12364 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12365 if (error != EMAIL_ERROR_NONE) {
12366 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12374 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12376 if (err_code != NULL)
12379 EM_DEBUG_FUNC_END("ret [%d]", ret);
12384 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)
12386 EM_DEBUG_FUNC_BEGIN_SEC("mail_id[%d], new_server_uid[%s], mbox_name[%s]", mail_id, new_server_uid, mbox_name);
12389 int error = EMAIL_ERROR_NONE;
12390 char sql_query_string[QUERY_SIZE] = {0, };
12391 int transaction = true;
12393 if (!mail_id || !new_server_uid || !mbox_name) {
12394 EM_DEBUG_EXCEPTION("Invalid parameters");
12395 if (err_code != NULL)
12396 *err_code = EMAIL_ERROR_INVALID_PARAM;
12400 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12401 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12404 SNPRINTF(sql_query_string, sizeof(sql_query_string),
12405 "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);
12407 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12408 if (error != EMAIL_ERROR_NONE) {
12409 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12417 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12419 if (err_code != NULL)
12422 EM_DEBUG_FUNC_END("ret [%d]", ret);
12427 INTERNAL_FUNC int emstorage_update_save_status(char *multi_user_name, int account_id, int *err_code)
12429 EM_DEBUG_FUNC_BEGIN();
12432 int transaction = true;
12433 int error = EMAIL_ERROR_NONE;
12434 char sql_query_string[QUERY_SIZE] = {0,};
12436 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12438 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12440 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12442 if (account_id <= ALL_ACCOUNT)
12443 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);
12445 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);
12447 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12448 if (error != EMAIL_ERROR_NONE) {
12449 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12456 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12458 if (err_code != NULL)
12461 EM_DEBUG_FUNC_END("ret [%d]", ret);
12466 int emstorage_get_unread_mailid(char *multi_user_name, int account_id, int vip_mode, int **mail_ids, int *mail_number, int *err_code)
12468 EM_DEBUG_FUNC_BEGIN();
12470 if ((!mail_ids) || (account_id <= 0 && account_id != -1)) {
12471 EM_DEBUG_EXCEPTION(" mail_id[%p], account_id[%d] ", mail_ids, account_id);
12472 if (err_code != NULL)
12473 *err_code = EMAIL_ERROR_INVALID_PARAM;
12479 int error = EMAIL_ERROR_NONE;
12483 int *p_mail_ids = NULL;
12484 int transaction = false;
12485 char **result = NULL;
12486 char sql_query_string[QUERY_SIZE] = {0, };
12487 char temp_query_string[QUERY_SIZE] = {0,};
12488 char sql_select_query_string[QUERY_SIZE] = {0, };
12490 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12491 EMSTORAGE_START_READ_TRANSACTION(transaction);
12493 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12495 if (account_id == -1) {
12496 SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string),
12497 "SELECT mail_id FROM mail_tbl WHERE flags_seen_field = 0 AND (save_status = %d or save_status = %d)",
12498 EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12500 SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string),
12501 "SELECT mail_id FROM mail_tbl WHERE account_id = %d AND flags_seen_field = 0 AND (save_status = %d or save_status = %d)",
12502 account_id, EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12506 SNPRINTF(temp_query_string, sizeof(temp_query_string),
12507 "%s AND tag_id < 0", sql_select_query_string);
12509 SNPRINTF(temp_query_string, sizeof(temp_query_string), "%s", sql_select_query_string);
12512 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s ORDER BY date_time ASC", temp_query_string);
12514 EM_DEBUG_LOG_SEC("query: [%s]", sql_query_string);
12516 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
12517 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
12518 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12520 EM_DEBUG_LOG("Count : %d", count);
12523 EM_DEBUG_EXCEPTION("no Mails found...");
12525 error = EMAIL_ERROR_MAIL_NOT_FOUND;
12529 p_mail_ids = em_malloc(count * sizeof(int));
12530 if (p_mail_ids == NULL) {
12531 EM_DEBUG_EXCEPTION("em_mallocfailed...");
12532 error = EMAIL_ERROR_OUT_OF_MEMORY;
12538 for (i = 0; i < count; i++)
12539 _get_table_field_data_int(result, &(p_mail_ids[i]), col_index++);
12546 sqlite3_free_table(result);
12548 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12550 // sqlite3_db_release_memory(local_db_handle);
12553 if (mail_ids != NULL)
12554 *mail_ids = p_mail_ids;
12556 if (mail_number != NULL)
12557 *mail_number = count;
12559 EM_SAFE_FREE(p_mail_ids);
12562 if (err_code != NULL)
12565 EM_DEBUG_FUNC_END("ret [%d]", ret);
12569 int setting_system_command(const char *command)
12572 int pid = 0, status = 0;
12573 char *const environ[] = { NULL };
12588 argv[2] = (char *)command;
12591 execve("/bin/sh", argv, environ);
12596 if (waitpid(pid, &status, 0) == -1) {
12597 if (errno != EINTR)
12608 INTERNAL_FUNC int emstorage_mail_get_total_diskspace_usage(unsigned long *total_usage, int transaction, int *err_code)
12610 EM_DEBUG_FUNC_BEGIN("total_usage[%p], transaction[%d], err_code[%p]", total_usage, transaction, err_code);
12612 if (!total_usage) {
12613 EM_DEBUG_EXCEPTION("total_usage[%p]", total_usage);
12615 if (err_code != NULL)
12616 *err_code = EMAIL_ERROR_INVALID_PARAM;
12621 int error = EMAIL_ERROR_NONE;
12622 char syscmd[256] = {0, };
12623 char line[256] = {0, };
12624 char *line_from_file = NULL;
12626 unsigned long total_diskusage = 0;
12628 SNPRINTF(syscmd, sizeof(syscmd), "touch %s", SETTING_MEMORY_TEMP_FILE_PATH);
12629 if (setting_system_command(syscmd) == -1) {
12630 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
12632 error = EMAIL_ERROR_SYSTEM_FAILURE;
12636 SNPRINTF(syscmd, sizeof(syscmd), "du -hsk %s > %s", EMAIL_PATH, SETTING_MEMORY_TEMP_FILE_PATH);
12637 EM_DEBUG_LOG(" cmd : %s", syscmd);
12638 if (setting_system_command(syscmd) == -1) {
12639 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : Setting > Memory] System Command [%s] is failed", syscmd);
12641 error = EMAIL_ERROR_SYSTEM_FAILURE;
12645 error = em_fopen(SETTING_MEMORY_TEMP_FILE_PATH, "r", &fp);
12646 if (error != EMAIL_ERROR_NONE) {
12647 perror(SETTING_MEMORY_TEMP_FILE_PATH);
12651 line_from_file = fgets(line, sizeof(line), fp);
12653 if (line_from_file == NULL) {
12654 EM_DEBUG_EXCEPTION("fgets failed");
12655 error = EMAIL_ERROR_SYSTEM_FAILURE;
12658 total_diskusage = strtoul(line, NULL, 10);
12660 memset(syscmd, 0, sizeof(syscmd));
12661 SNPRINTF(syscmd, sizeof(syscmd), "rm -f %s", SETTING_MEMORY_TEMP_FILE_PATH);
12662 if (setting_system_command(syscmd) == -1) {
12663 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
12664 error = EMAIL_ERROR_SYSTEM_FAILURE;
12668 EM_DEBUG_LOG("[Setting > Memory] @@@@@ Size of Directory [%s] is %ld KB", EMAIL_PATH, total_diskusage);
12673 if (err_code != NULL)
12677 *total_usage = total_diskusage;
12681 if (fp) fclose(fp); /* prevent 32730 */
12683 EM_DEBUG_FUNC_END("ret [%d]", ret);
12686 #define MAILHOME_UTF8 tzplatform_mkpath(TZ_USER_DATA, "email/.email_data/7/348/UTF-8")
12688 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)
12690 DB_STMT hStmt = NULL;
12692 int error = EMAIL_ERROR_NONE;
12694 char sql_query_string[QUERY_SIZE] = {0, };
12696 SNPRINTF(sql_query_string, sizeof(sql_query_string),
12697 "INSERT INTO mail_tbl VALUES "
12699 ", ?" /* account_id */
12700 ", ?" /* mail_size */
12701 ", ?" /* server_mail_status */
12702 ", ?" /* server_mailbox_name */
12703 ", ?" /* server_mail_id */
12704 ", ?" /* reference_mail_id */
12705 ", ?" /* full_address_from */
12706 ", ?" /* full_address_reply */
12707 ", ?" /* full_address_to */
12708 ", ?" /* full_address_cc */
12709 ", ?" /* full_address_bcc */
12710 ", ?" /* full_address_return */
12711 ", ?" /* subject */
12712 ", ?" /* body_download_status */
12713 ", ?" /* file_path_plain */
12714 ", ?" /* file_path_html */
12715 ", ?" /* date_time */
12716 ", ?" /* flags_seen_field */
12717 ", ?" /* flags_deleted_field */
12718 ", ?" /* flags_flagged_field */
12719 ", ?" /* flags_answered_field */
12720 ", ?" /* flags_recent_field */
12721 ", ?" /* flags_draft_field */
12722 ", ?" /* flags_forwarded_field */
12723 ", ?" /* DRM_status */
12724 ", ?" /* priority */
12725 ", ?" /* save_status */
12726 ", ?" /* lock_status */
12727 ", ?" /* message_id */
12728 ", ?" /* report_status */
12729 ", ?" /* email_address_sender */
12730 ", ?" /* email_address_recipient */
12731 ", ?" /* attachment_count */
12732 ", ?" /* inline_content_count */
12733 ", ?" /* preview_text */
12734 ", ?" /* thread_id */
12735 ", ?" /* mailbox_type */
12736 ", ?" /* alias_sender */
12737 ", ?" /* alias_recipient */
12738 ", ?" /* thread_item_count */
12739 ", ?" /* meeting_request_status */
12740 ", ?" /* message_class */
12741 ", ?" /* digest_type */
12742 ", ?" /* smime_type */
12743 ", ?" /* scheduled_sending_time */
12744 ", ?" /* remaining_resend_times */
12746 ", ?" /* replied_time */
12747 ", ?" /* forwarded_time */
12748 ", ?" /* default_charset */
12749 ", ?" /* eas_data_length */
12750 ", ?" /* eas_data */
12753 int transaction = true;
12754 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12756 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12759 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12760 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
12761 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12763 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_id);
12764 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, account_id);
12765 _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, EMAIL_MAILBOX_TYPE_OUTBOX);
12766 _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, "save test - long", 1, SUBJECT_LEN_IN_MAIL_TBL);
12767 _bind_stmt_field_data_string(hStmt, DATETIME_IDX_IN_MAIL_TBL, "20100316052908", 0, DATETIME_LEN_IN_MAIL_TBL);
12768 _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, 0);
12769 _bind_stmt_field_data_string(hStmt, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL, "", 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
12770 _bind_stmt_field_data_string(hStmt, SERVER_MAIL_ID_IDX_IN_MAIL_TBL, "", 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
12771 _bind_stmt_field_data_string(hStmt, MESSAGE_ID_IDX_IN_MAIL_TBL, "", 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
12772 _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, 0);
12773 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL, "<test08@streaming.s3glab.net>", 1, FROM_LEN_IN_MAIL_TBL);
12774 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL, "", 1, REPLY_TO_LEN_IN_MAIL_TBL);
12775 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL, full_address_to, 1, TO_LEN_IN_MAIL_TBL);
12776 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL, full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
12777 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL, full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
12778 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL, "", 1, RETURN_PATH_LEN_IN_MAIL_TBL);
12779 _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);
12780 _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);
12781 _bind_stmt_field_data_string(hStmt, ALIAS_SENDER_IDX_IN_MAIL_TBL, "send_alias", 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12782 _bind_stmt_field_data_string(hStmt, ALIAS_RECIPIENT_IDX_IN_MAIL_TBL, "recipient_alias", 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12783 _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, 1);
12784 _bind_stmt_field_data_string(hStmt, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL, (char *)MAILHOME_UTF8, 0, TEXT_1_LEN_IN_MAIL_TBL);
12785 _bind_stmt_field_data_string(hStmt, FILE_PATH_HTML_IDX_IN_MAIL_TBL, "", 0, TEXT_2_LEN_IN_MAIL_TBL);
12786 _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, 4);
12787 _bind_stmt_field_data_char(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, 0);
12788 _bind_stmt_field_data_char(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, 0);
12789 _bind_stmt_field_data_char(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, 0);
12790 _bind_stmt_field_data_char(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, 0);
12791 _bind_stmt_field_data_char(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, 0);
12792 _bind_stmt_field_data_char(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, 0);
12793 _bind_stmt_field_data_char(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, 0);
12794 _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, 0);
12795 _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, 0);
12796 _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, 0);
12797 _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, 0);
12798 _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, 0);
12799 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12800 _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, 0);
12801 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12802 _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, 0);
12803 _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, 0);
12804 _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, "preview body", 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
12805 _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, 0);
12806 _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, 0);
12807 _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, 0);
12808 _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, 0);
12809 _bind_stmt_field_data_int(hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, 0);
12810 _bind_stmt_field_data_int(hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, 0);
12811 _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, 0);
12812 _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, 0);
12813 _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, 0);
12814 _bind_stmt_field_data_string(hStmt, DEFAULT_CHARSET_IDX_IN_MAIL_TBL, "UTF-8", 0, TEXT_2_LEN_IN_MAIL_TBL);
12815 _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, 0);
12816 _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, NULL, 0);
12818 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12819 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
12820 ("sqlite3_step fail:%d", rc));
12821 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
12822 ("sqlite3_step fail:%d", rc));
12826 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12827 if (hStmt != NULL) {
12828 rc = sqlite3_finalize(hStmt);
12829 if (rc != SQLITE_OK) {
12830 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
12831 error = EMAIL_ERROR_DB_FAILURE;
12835 if (err_code != NULL)
12838 EM_DEBUG_FUNC_END("ret [%d]", ret);
12843 INTERNAL_FUNC int emstorage_get_max_mail_count()
12845 return EMAIL_MAIL_MAX_COUNT;
12848 #define STRIPPED_SUBJECT_BUFFER_SIZE 4086
12850 INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(char *multi_user_name,
12851 emstorage_mail_tbl_t *mail_tbl,
12853 int *result_latest_mail_id_in_thread,
12854 int *thread_item_count)
12856 EM_DEBUG_FUNC_BEGIN("mail_tbl [%p], thread_id [%p], "
12857 "result_latest_mail_id_in_thread [%p], thread_item_count [%p]",
12858 mail_tbl, thread_id, result_latest_mail_id_in_thread, thread_item_count);
12859 EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
12861 int rc = 0, query_size = 0, query_size_account = 0;
12863 int search_thread = false;
12864 int account_id = 0;
12865 int err_code = EMAIL_ERROR_NONE;
12866 int count = 0, result_thread_id = -1, latest_mail_id_in_thread = -1;
12867 time_t latest_date_time = 0;
12868 char *subject = NULL;
12869 char *p_subject = NULL;
12870 char *sql_query_string = NULL, *sql_account = NULL;
12872 int temp_thread_id = -1;
12873 char *sql_format = "SELECT mail_id, thread_id, date_time, subject "
12874 "FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
12875 char *sql_format_account = " AND account_id = %d ";
12876 char *sql_format_order_by = " ORDER BY thread_id, date_time DESC ";
12877 char **result = NULL;
12878 char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
12879 char stripped_subject2[STRIPPED_SUBJECT_BUFFER_SIZE];
12881 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12883 EM_DEBUG_LOG("subject: [%p], mail_id: [%d]", subject, mail_tbl->mail_id);
12885 EM_IF_NULL_RETURN_VALUE(mail_tbl, EMAIL_ERROR_INVALID_PARAM);
12886 EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
12887 EM_IF_NULL_RETURN_VALUE(result_latest_mail_id_in_thread, EMAIL_ERROR_INVALID_PARAM);
12888 EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
12890 account_id = mail_tbl->account_id;
12891 subject = mail_tbl->subject;
12893 EM_DEBUG_LOG_SEC("subject: [%s]", subject);
12895 if (EM_SAFE_STRLEN(subject) == 0 && mail_tbl->mail_id != 0) {
12896 result_thread_id = mail_tbl->mail_id;
12901 if (em_find_pos_stripped_subject_for_thread_view(subject,
12903 STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE) {
12904 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
12905 err_code = EMAIL_ERROR_UNKNOWN;
12906 result_thread_id = -1;
12910 EM_DEBUG_LOG_SEC("stripped_subject: [%s]", stripped_subject);
12912 if (EM_SAFE_STRLEN(stripped_subject) == 0) {
12913 result_thread_id = -1;
12917 EM_DEBUG_LOG_SEC("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s",
12918 EM_SAFE_STRLEN(stripped_subject), stripped_subject);
12920 if (account_id > 0) {
12921 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
12922 sql_account = malloc(query_size_account);
12923 if (sql_account == NULL) {
12924 EM_DEBUG_EXCEPTION("malloc for sql_account is failed %d", query_size_account);
12925 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
12928 snprintf(sql_account, query_size_account, sql_format_account, account_id);
12931 /* prevent 34362 */
12932 query_size = strlen(sql_format) + strlen(stripped_subject)*2 + 50 + query_size_account + strlen(sql_format_order_by); /* + query_size_mailbox; */
12933 sql_query_string = malloc(query_size);
12935 if (sql_query_string == NULL) {
12936 EM_DEBUG_EXCEPTION("malloc for sql is failed %d", query_size);
12937 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
12941 sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mail_tbl->mailbox_id);
12943 if (account_id > 0)
12944 EM_SAFE_STRNCAT(sql_query_string, sql_account, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
12946 EM_SAFE_STRNCAT(sql_query_string, sql_format_order_by, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
12947 EM_SAFE_STRNCAT(sql_query_string, ";", query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
12949 EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
12951 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL),
12954 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
12955 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12957 EM_DEBUG_LOG("Result rows count : %d", count);
12960 result_thread_id = -1;
12962 for (i = 0; i < count; i++) {
12963 EM_SAFE_FREE(p_subject);
12965 _get_table_field_data_int(result, &latest_mail_id_in_thread, col_index++);
12966 _get_table_field_data_int(result, &result_thread_id, col_index++);
12967 _get_table_field_data_time_t(result, &latest_date_time, col_index++);
12968 _get_table_field_data_string(result, &p_subject, 0, col_index++);
12970 if (temp_thread_id == result_thread_id)
12973 temp_thread_id = result_thread_id;
12975 if (em_find_pos_stripped_subject_for_thread_view(p_subject,
12977 STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE) {
12978 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
12979 err_code = EMAIL_ERROR_UNKNOWN;
12980 result_thread_id = -1;
12984 if (g_strcmp0(stripped_subject2, stripped_subject) == 0) {
12985 if (latest_date_time < mail_tbl->date_time)
12986 *result_latest_mail_id_in_thread = latest_mail_id_in_thread;
12988 *result_latest_mail_id_in_thread = mail_tbl->mail_id;
12990 search_thread = true;
12993 if (search_thread) {
12994 EM_DEBUG_LOG("latest_mail_id_in_thread [%d], mail_id [%d]",
12995 latest_mail_id_in_thread, mail_tbl->mail_id);
12998 result_thread_id = -1;
13007 *thread_id = result_thread_id;
13008 *thread_item_count = count;
13010 EM_DEBUG_LOG("Result thread id : %d", *thread_id);
13011 EM_DEBUG_LOG("Result count : %d", *thread_item_count);
13012 EM_DEBUG_LOG("err_code : %d", err_code);
13014 EM_SAFE_FREE(sql_account);
13015 EM_SAFE_FREE(sql_query_string);
13016 EM_SAFE_FREE(p_subject);
13018 sqlite3_free_table(result);
13020 EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
13025 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)
13027 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);
13028 EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
13031 int query_size = 0;
13032 int query_size_account = 0;
13033 int err_code = EMAIL_ERROR_NONE;
13035 int result_thread_id = -1;
13036 char *sql_query_string = NULL;
13037 char *sql_account = NULL;
13038 char *sql_format = "SELECT thread_id FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
13039 char *sql_format_account = " AND account_id = %d ";
13040 char *sql_format_order_by = " ORDER BY date_time DESC ";
13041 char **result = NULL;
13042 char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
13043 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13045 EM_IF_NULL_RETURN_VALUE(mail_subject, EMAIL_ERROR_INVALID_PARAM);
13046 EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
13047 EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
13049 EM_DEBUG_LOG_SEC("subject: [%s]", mail_subject);
13051 if (em_find_pos_stripped_subject_for_thread_view(mail_subject, stripped_subject, STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE) {
13052 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
13053 err_code = EMAIL_ERROR_UNKNOWN;
13054 result_thread_id = -1;
13058 if (EM_SAFE_STRLEN(stripped_subject) == 0) {
13059 result_thread_id = -1;
13063 EM_DEBUG_LOG("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s", EM_SAFE_STRLEN(stripped_subject), stripped_subject);
13065 if (account_id > 0) {
13066 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
13067 sql_account = malloc(query_size_account);
13068 if (sql_account == NULL) {
13069 EM_DEBUG_EXCEPTION("malloc for sql_account is failed %d", query_size_account);
13070 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
13073 snprintf(sql_account, query_size_account, sql_format_account, account_id);
13076 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; */
13077 sql_query_string = malloc(query_size);
13079 if (sql_query_string == NULL) {
13080 EM_DEBUG_EXCEPTION("malloc for sql is failed %d", query_size);
13081 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
13085 sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mailbox_id);
13087 if (account_id > 0)
13088 EM_SAFE_STRNCAT(sql_query_string, sql_account, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13089 EM_SAFE_STRNCAT(sql_query_string, sql_format_order_by, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13090 EM_SAFE_STRNCAT(sql_query_string, ";", query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13092 EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
13094 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
13096 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13097 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13099 EM_DEBUG_LOG("Result rows count : %d", count);
13102 result_thread_id = -1;
13104 _get_table_field_data_int(result, &result_thread_id, 1);
13107 *thread_id = result_thread_id;
13108 *thread_item_count = count;
13110 EM_DEBUG_LOG("Result thread id : %d", *thread_id);
13111 EM_DEBUG_LOG("Result count : %d", *thread_item_count);
13112 EM_DEBUG_LOG("err_code : %d", err_code);
13114 EM_SAFE_FREE(sql_account);
13115 EM_SAFE_FREE(sql_query_string);
13117 sqlite3_free_table(result);
13119 EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
13124 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)
13126 EM_DEBUG_FUNC_BEGIN();
13128 int count = 0, ret = false;
13129 int error = EMAIL_ERROR_NONE;
13130 emstorage_mail_tbl_t *p_data_tbl = NULL;
13131 char conditional_clause[QUERY_SIZE] = {0, };
13133 EM_IF_NULL_RETURN_VALUE(mail_tbl, false);
13135 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE thread_id = %d AND thread_item_count > 0", thread_id);
13136 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
13138 if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
13139 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
13144 EM_DEBUG_LOG("thread_id : %d, thread_item_count : %d", p_data_tbl[0].thread_id, p_data_tbl[0].thread_item_count);
13150 *mail_tbl = p_data_tbl;
13151 else if (p_data_tbl != NULL)
13152 emstorage_free_mail(&p_data_tbl, 1, NULL);
13154 if (err_code != NULL)
13157 EM_DEBUG_FUNC_END("ret [%d]", ret);
13161 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)
13163 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]"
13164 , account_id , mailbox_id , search_type , search_value , sorting , sender_list, sender_count, err_code);
13166 if ((!sender_list) || (!sender_count)) {
13167 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
13168 if (err_code != NULL)
13169 *err_code = EMAIL_ERROR_INVALID_PARAM;
13173 int rc = -1, ret = false;
13174 int error = EMAIL_ERROR_NONE;
13176 int i, col_index = 0;
13177 int read_count = 0;
13178 email_sender_list_t *p_sender_list = NULL;
13179 char sql_query_string[QUERY_SIZE] = {0, };
13180 char **result = NULL;
13181 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13183 SNPRINTF(sql_query_string, sizeof(sql_query_string),
13184 "SELECT email_address_sender, alias_sender, COUNT(email_address_sender), SUM(flags_seen_field = 1) "
13189 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);
13190 else /* NULL means all mailbox_name. but except for trash(3), spambox(5), all emails(for GMail, 7) */
13191 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) ");
13194 /* '0' (ALL_ACCOUNT) means all account */
13195 if (account_id > ALL_ACCOUNT)
13196 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);
13198 if (search_value) {
13199 switch (search_type) {
13200 case EMAIL_SEARCH_FILTER_SUBJECT:
13201 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13202 " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\')) ", search_value);
13204 case EMAIL_SEARCH_FILTER_SENDER:
13205 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13206 " AND ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13207 ") ", search_value);
13209 case EMAIL_SEARCH_FILTER_RECIPIENT:
13210 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13211 " AND ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13212 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13213 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13214 ") ", search_value, search_value, search_value);
13216 case EMAIL_SEARCH_FILTER_ALL:
13217 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13218 " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') "
13219 " OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13220 " OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13221 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13222 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13225 ")", search_value, search_value, search_value, search_value, search_value);
13231 /* sorting option is not available now. The order of sender list is ascending order by display name */
13232 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
13233 "GROUP BY email_address_sender "
13234 "ORDER BY UPPER(alias_sender) ");
13236 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
13238 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
13239 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13240 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13242 EM_DEBUG_LOG("Count of Sender [%d]", count);
13244 if (!(p_sender_list = (email_sender_list_t*)em_malloc(sizeof(email_sender_list_t) * count))) {
13245 EM_DEBUG_EXCEPTION("em_mallocfailed...");
13246 error = EMAIL_ERROR_OUT_OF_MEMORY;
13252 EM_DEBUG_LOG(">>>> DATA ASSIGN START >>");
13253 for (i = 0; i < count; i++) {
13254 _get_table_field_data_string(result, &(p_sender_list[i].address), 1, col_index++);
13255 _get_table_field_data_string(result, &(p_sender_list[i].display_name), 1, col_index++);
13256 _get_table_field_data_int(result, &(p_sender_list[i].total_count), col_index++);
13257 _get_table_field_data_int(result, &(read_count), col_index++);
13258 p_sender_list[i].unread_count = p_sender_list[i].total_count - read_count; /* unread count = total - read */
13260 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >>", count);
13262 sqlite3_free_table(result);
13269 *sender_list = p_sender_list;
13270 *sender_count = count;
13271 EM_DEBUG_LOG(">>>> COUNT : %d >>", count);
13275 if (err_code != NULL)
13278 EM_DEBUG_FUNC_END("ret [%d]", ret);
13282 INTERNAL_FUNC int emstorage_free_sender_list(email_sender_list_t **sender_list, int count)
13284 EM_DEBUG_FUNC_BEGIN("sender_list[%p], count[%d]", sender_list, count);
13286 int err = EMAIL_ERROR_NONE;
13289 if (!sender_list || !*sender_list) {
13290 EM_DEBUG_EXCEPTION("sender_list[%p], count[%d]", sender_list, count);
13291 err = EMAIL_ERROR_INVALID_PARAM;
13295 email_sender_list_t* p = *sender_list;
13298 for (; i < count; i++) {
13299 EM_SAFE_FREE(p[i].address);
13300 EM_SAFE_FREE(p[i].display_name);
13304 *sender_list = NULL;
13311 INTERNAL_FUNC int emstorage_free_address_info_list(email_address_info_list_t **address_info_list)
13313 EM_DEBUG_FUNC_BEGIN("address_info_list[%p]", address_info_list);
13315 int err = EMAIL_ERROR_NONE;
13316 email_address_info_t *p_address_info = NULL;
13317 GList *list = NULL;
13318 GList *node = NULL;
13321 if (!address_info_list || !*address_info_list) {
13322 EM_DEBUG_EXCEPTION("address_info_list[%p]", address_info_list);
13323 err = EMAIL_ERROR_INVALID_PARAM;
13327 /* delete GLists */
13328 for (i = EMAIL_ADDRESS_TYPE_FROM; i <= EMAIL_ADDRESS_TYPE_BCC; i++) {
13330 case EMAIL_ADDRESS_TYPE_FROM:
13331 list = (*address_info_list)->from;
13333 case EMAIL_ADDRESS_TYPE_TO:
13334 list = (*address_info_list)->to;
13336 case EMAIL_ADDRESS_TYPE_CC:
13337 list = (*address_info_list)->cc;
13339 case EMAIL_ADDRESS_TYPE_BCC:
13340 list = (*address_info_list)->bcc;
13344 /* delete dynamic-allocated memory for each item */
13345 node = g_list_first(list);
13346 while (node != NULL) {
13347 p_address_info = (email_address_info_t*)node->data;
13348 EM_SAFE_FREE(p_address_info->address);
13349 EM_SAFE_FREE(p_address_info->display_name);
13350 EM_SAFE_FREE(node->data);
13352 node = g_list_next(node);
13357 EM_SAFE_FREE(*address_info_list);
13358 *address_info_list = NULL;
13360 EM_DEBUG_FUNC_END("err [%d]", err);
13363 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
13365 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)
13367 EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
13369 if (!local_activity || !activity_id) {
13370 EM_DEBUG_EXCEPTION("local_activity[%p], transaction[%d], activity_id[%p], err_code[%p]", local_activity, activity_id, transaction, err_code);
13371 if (err_code != NULL)
13372 *err_code = EMAIL_ERROR_INVALID_PARAM;
13378 int error = EMAIL_ERROR_NONE;
13381 char sql_query_string[QUERY_SIZE] = {0, };
13382 DB_STMT hStmt = NULL;
13383 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13385 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13386 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13387 SNPRINTF(sql_query_string, sizeof(sql_query_string),
13388 "INSERT INTO mail_partial_body_activity_tbl VALUES "
13390 "? " /* Account ID */
13391 ",?" /* Local Mail ID */
13392 ",?" /* Server mail ID */
13393 ",?" /* Activity ID */
13394 ",?" /* Activity type*/
13395 ",?" /* Mailbox ID*/
13396 ",?" /* Mailbox name*/
13397 ",?" /* Multi User Name */
13400 char *sql = "SELECT max(rowid) FROM mail_partial_body_activity_tbl;";
13401 char **result = NULL;
13404 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13406 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13407 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13409 if (NULL == result[1]) rc = 1;
13410 else rc = atoi(result[1])+1;
13411 sqlite3_free_table(result);
13414 *activity_id = local_activity->activity_id = rc;
13416 EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [ %d ], MAIL ID [ %d ], ACTIVITY TYPE [ %d ], SERVER MAIL ID [ %lu ]", \
13417 local_activity->activity_id, local_activity->mail_id, local_activity->activity_type, local_activity->server_mail_id);
13419 if (local_activity->mailbox_id)
13420 EM_DEBUG_LOG(" MAILBOX ID [ %d ]", local_activity->mailbox_id);
13423 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13424 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13425 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13428 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
13429 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
13430 _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
13431 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
13432 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
13433 _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
13434 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->mailbox_name, 0, 3999);
13435 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
13438 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13440 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
13441 ("sqlite3_step fail:%d", rc));
13442 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13443 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
13448 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13449 if (hStmt != NULL) {
13450 rc = sqlite3_finalize(hStmt);
13452 if (rc != SQLITE_OK) {
13453 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13454 error = EMAIL_ERROR_DB_FAILURE;
13458 if (err_code != NULL)
13461 EM_DEBUG_FUNC_END("ret [%d]", ret);
13465 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)
13467 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13469 if (account_id < FIRST_ACCOUNT_ID || NULL == mailbox_list || *mailbox_list == NULL || NULL == count) {
13470 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13471 if (err_code != NULL)
13472 *err_code = EMAIL_ERROR_INVALID_PARAM;
13477 int error = EMAIL_ERROR_NONE;
13479 int i = 0, rc = -1;
13480 int *mbox_list = NULL;
13481 DB_STMT hStmt = NULL;
13482 char sql_query_string[QUERY_SIZE] = {0, };
13484 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13485 EMSTORAGE_START_READ_TRANSACTION(transaction);
13487 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13488 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);
13491 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13492 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13493 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13495 *count = atoi(result[1]);
13496 sqlite3_free_table(result);
13499 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
13500 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13504 EM_DEBUG_LOG("Mailbox count = %d", *count);
13506 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13508 /* 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); */
13509 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);
13511 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13514 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13517 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13518 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13521 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13522 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13523 ("sqlite3_step fail:%d", rc));
13525 mbox_list = (int *)em_malloc(sizeof(int) * (*count));
13526 if (NULL == mbox_list) {
13527 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
13528 error = EMAIL_ERROR_OUT_OF_MEMORY;
13532 memset(mbox_list, 0x00, sizeof(int) * (*count));
13534 for (i = 0; i < (*count); i++) {
13535 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
13536 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13537 /* EM_DEBUG_LOG("In emstorage_get_pdb_mailbox_list() loop, After sqlite3_step(), , i = %d, rc = %d.", i, rc); */
13538 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13539 ("sqlite3_step fail:%d", rc));
13540 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
13547 *mailbox_list = mbox_list;
13549 EM_SAFE_FREE(mbox_list);
13551 if (hStmt != NULL) {
13552 rc = sqlite3_finalize(hStmt);
13554 if (rc != SQLITE_OK) {
13555 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13556 error = EMAIL_ERROR_DB_FAILURE;
13560 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13561 if (err_code != NULL)
13563 EM_DEBUG_FUNC_END("ret [%d]", ret);
13567 INTERNAL_FUNC int emstorage_get_pbd_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
13569 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13571 if (NULL == account_list || NULL == count) {
13572 EM_DEBUG_EXCEPTION("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13573 if (err_code != NULL)
13574 *err_code = EMAIL_ERROR_INVALID_PARAM;
13579 int error = EMAIL_ERROR_NONE;
13580 char *sql = "SELECT count(distinct account_id) FROM mail_partial_body_activity_tbl";
13582 int i = 0, rc = -1;
13583 int *result_account_list = NULL;
13584 DB_STMT hStmt = NULL;
13585 char sql_query_string[QUERY_SIZE] = {0, };
13587 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13589 EMSTORAGE_START_READ_TRANSACTION(transaction);
13591 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13592 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13593 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13595 *count = atoi(result[1]);
13596 sqlite3_free_table(result);
13599 EM_DEBUG_EXCEPTION("no account found...");
13600 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13605 EM_DEBUG_LOG("Account count [%d]", *count);
13607 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13609 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_partial_body_activity_tbl");
13611 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13614 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13616 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
13617 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13618 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13621 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13622 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13623 ("sqlite3_step fail:%d", rc));
13625 if (NULL == (result_account_list = (int *)em_malloc(sizeof(int) * (*count)))) {
13626 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
13627 error = EMAIL_ERROR_OUT_OF_MEMORY;
13631 memset(result_account_list, 0x00, sizeof(int) * (*count));
13633 for (i = 0; i < (*count); i++) {
13634 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
13636 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13637 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13638 ("sqlite3_step fail:%d", rc));
13639 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
13646 *account_list = result_account_list;
13648 EM_SAFE_FREE(result_account_list);
13650 if (hStmt != NULL) {
13651 rc = sqlite3_finalize(hStmt);
13653 if (rc != SQLITE_OK) {
13654 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13655 error = EMAIL_ERROR_DB_FAILURE;
13659 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13660 if (err_code != NULL)
13662 EM_DEBUG_FUNC_END("ret [%d]", ret);
13666 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)
13668 EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
13670 if (account_id < FIRST_ACCOUNT_ID || NULL == event_start || 0 == input_mailbox_id || NULL == count) {
13671 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);
13673 if (err_code != NULL)
13674 *err_code = EMAIL_ERROR_INVALID_PARAM;
13681 int error = EMAIL_ERROR_NONE;
13683 DB_STMT hStmt = NULL;
13684 email_event_partial_body_thd* event_list = NULL;
13685 char sql_query_string[QUERY_SIZE] = {0, };
13687 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13689 EMSTORAGE_START_READ_TRANSACTION(transaction);
13691 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13692 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);
13695 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13696 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13697 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13699 *count = atoi(result[1]);
13700 sqlite3_free_table(result);
13702 EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
13705 EM_DEBUG_LOG("No matched activity found in mail_partial_body_activity_tbl");
13706 error = EMAIL_ERROR_MAIL_NOT_FOUND;
13710 EM_DEBUG_LOG("Activity Count = %d", *count);
13712 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13713 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);
13715 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13718 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13720 EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
13721 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13722 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13725 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13726 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13727 ("sqlite3_step fail:%d", rc));
13729 if (!(event_list = (email_event_partial_body_thd*)em_malloc(sizeof(email_event_partial_body_thd) * (*count)))) {
13730 EM_DEBUG_EXCEPTION("Malloc failed");
13732 error = EMAIL_ERROR_OUT_OF_MEMORY;
13735 memset(event_list, 0x00, sizeof(email_event_partial_body_thd) * (*count));
13737 for (i = 0; i < (*count); i++) {
13738 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_IDX_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13739 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13740 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13741 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13742 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_type), ACTIVITY_TYPE_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13743 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13744 _get_stmt_field_data_string(hStmt, &(event_list[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13745 _get_stmt_field_data_string(hStmt, &(event_list[i].multi_user_name), 0, MULTI_USER_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13747 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13748 /* EM_DEBUG_LOG("In emstorage_get_pbd_activity_data() loop, After sqlite3_step(), , i = %d, rc = %d.", i, rc); */
13749 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13750 ("sqlite3_step fail:%d", rc));
13752 event_list[i].event_type = 0;
13759 *event_start = event_list;
13761 for (i = 0; i < (*count); i++)
13762 emcore_free_partial_body_thd_event(event_list, NULL);
13763 EM_SAFE_FREE(event_list); /*prevent 54559*/
13764 *event_start = NULL;
13768 if (hStmt != NULL) {
13769 rc = sqlite3_finalize(hStmt);
13771 if (rc != SQLITE_OK) {
13772 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13773 error = EMAIL_ERROR_DB_FAILURE;
13777 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13778 if (err_code != NULL)
13781 EM_DEBUG_FUNC_END("ret [%d]", ret);
13785 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)
13787 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);
13790 if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
13791 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);
13793 if (err_code != NULL)
13794 *err_code = EMAIL_ERROR_INVALID_PARAM;
13800 int error = EMAIL_ERROR_NONE;
13801 char sql_query_string[QUERY_SIZE] = {0, };
13802 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13804 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13805 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13807 if (activity_id == 0)
13808 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);
13810 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);
13812 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13813 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
13814 if (error != EMAIL_ERROR_NONE) {
13815 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
13819 /* validate activity existence */
13820 rc = sqlite3_changes(local_db_handle);
13822 EM_DEBUG_EXCEPTION("No matching activity found");
13823 error = EMAIL_ERROR_DATA_NOT_FOUND;
13831 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13833 if (err_code != NULL)
13836 EM_DEBUG_FUNC_END("ret [%d]", ret);
13840 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)
13842 EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
13844 if (account_id < FIRST_ACCOUNT_ID || NULL == activity_count || NULL == err_code) {
13845 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
13846 if (err_code != NULL)
13847 *err_code = EMAIL_ERROR_INVALID_PARAM;
13852 int error = EMAIL_ERROR_NONE;
13853 char sql_query_string[QUERY_SIZE] = {0, };
13855 DB_STMT hStmt = NULL;
13857 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13859 EMSTORAGE_START_READ_TRANSACTION(transaction);
13860 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13862 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);
13864 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13867 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13868 EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
13869 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13870 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13873 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13874 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13875 ("sqlite3_step fail:%d", rc));
13877 _get_stmt_field_data_int(hStmt, activity_count, 0);
13879 EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
13885 if (hStmt != NULL) {
13886 rc = sqlite3_finalize(hStmt);
13888 if (rc != SQLITE_OK) {
13889 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13890 error = EMAIL_ERROR_DB_FAILURE;
13894 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13896 if (err_code != NULL)
13899 EM_DEBUG_FUNC_END("ret [%d]", ret);
13903 INTERNAL_FUNC int emstorage_get_pbd_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
13905 EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
13907 if (NULL == activity_count || NULL == err_code) {
13908 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
13909 if (err_code != NULL)
13910 *err_code = EMAIL_ERROR_INVALID_PARAM;
13915 int error = EMAIL_ERROR_NONE;
13916 DB_STMT hStmt = NULL;
13917 char sql_query_string[QUERY_SIZE] = {0, };
13919 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13921 EMSTORAGE_START_READ_TRANSACTION(transaction);
13922 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13924 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl;");
13926 EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
13928 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13929 EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
13930 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13931 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13933 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13934 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13935 ("sqlite3_step fail:%d", rc));
13937 _get_stmt_field_data_int(hStmt, activity_count, 0);
13939 EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
13946 if (hStmt != NULL) {
13947 rc = sqlite3_finalize(hStmt);
13949 if (rc != SQLITE_OK) {
13950 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13951 error = EMAIL_ERROR_DB_FAILURE;
13955 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13956 if (err_code != NULL)
13959 EM_DEBUG_FUNC_END("ret [%d]", ret);
13963 INTERNAL_FUNC int emstorage_delete_full_pbd_activity_data(char *multi_user_name, int account_id, int transaction, int *err_code)
13965 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
13966 if (account_id < FIRST_ACCOUNT_ID) {
13967 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
13968 if (err_code != NULL)
13969 *err_code = EMAIL_ERROR_INVALID_PARAM;
13975 int error = EMAIL_ERROR_NONE;
13976 char sql_query_string[QUERY_SIZE] = {0, };
13978 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13980 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13981 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13982 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d", account_id);
13984 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13985 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
13986 if (error != EMAIL_ERROR_NONE) {
13987 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
13991 rc = sqlite3_changes(local_db_handle);
13993 EM_DEBUG_EXCEPTION("No matching activities found in mail_partial_body_activity_tbl");
13994 error = EMAIL_ERROR_DATA_NOT_FOUND;
14003 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14005 if (err_code != NULL)
14008 EM_DEBUG_FUNC_END("ret [%d]", ret);
14012 /*Himanshu[h.gahlaut]-> Added below API to update mail_partial_body_activity_tbl
14013 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*/
14015 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)
14017 EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
14019 int rc = -1, ret = false;
14020 int error = EMAIL_ERROR_NONE;
14021 char sql_query_string[QUERY_SIZE] = {0, };
14023 int transaction = true;
14025 if (!old_server_uid || !new_server_uid || !mbox_name) {
14026 EM_DEBUG_EXCEPTION("Invalid parameters");
14027 error = EMAIL_ERROR_INVALID_PARAM;
14031 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14033 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14034 memset(sql_query_string, 0x00, sizeof(sql_query_string));
14035 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14036 "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);
14038 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14039 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14040 if (error != EMAIL_ERROR_NONE) {
14041 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14045 rc = sqlite3_changes(local_db_handle);
14047 EM_DEBUG_LOG("No matching found in mail_partial_body_activity_tbl");
14052 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14054 if (err_code != NULL)
14057 EM_DEBUG_FUNC_END("ret [%d]", ret);
14062 INTERNAL_FUNC int emstorage_create_file(char *data_string, size_t file_size, char *dst_file_name, int *err_code)
14064 EM_DEBUG_FUNC_BEGIN_SEC("file_size[%d] , dst_file_name[%s], err_code[%p]", file_size, dst_file_name, err_code);
14067 int error = EMAIL_ERROR_NONE;
14068 FILE* fp_dst = NULL;
14069 char errno_buf[ERRNO_BUF_SIZE] = {0};
14071 if (!data_string || !dst_file_name) {
14072 EM_DEBUG_LOG("data_string[%p], dst_file_name[%p]", data_string, dst_file_name);
14073 error = EMAIL_ERROR_INVALID_PARAM;
14077 error = em_fopen(dst_file_name, "w", &fp_dst);
14078 if (error != EMAIL_ERROR_NONE) {
14079 EM_DEBUG_EXCEPTION_SEC("em_fopen failed - %s: %d", dst_file_name, error);
14083 if (fwrite(data_string, 1, file_size, fp_dst) == 0) {
14084 EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
14085 error = EMAIL_ERROR_UNKNOWN;
14093 if (fp_dst != NULL)
14096 if (err_code != NULL)
14099 EM_DEBUG_FUNC_END("ret [%d]", ret);
14103 #endif /* __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
14107 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
14108 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)
14110 EM_DEBUG_FUNC_BEGIN();
14113 int error = EMAIL_ERROR_NONE;
14114 char sql_query_string[QUERY_SIZE] = {0, };
14116 int transaction = true;
14118 if (!old_server_uid || !new_server_uid || !mbox_name) {
14119 EM_DEBUG_EXCEPTION("Invalid parameters");
14120 if (err_code != NULL)
14121 *err_code = EMAIL_ERROR_INVALID_PARAM;
14125 EM_DEBUG_LOG_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
14127 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14128 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14131 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14132 "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);
14134 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
14135 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14136 if (error != EMAIL_ERROR_NONE) {
14137 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14141 rc = sqlite3_changes(local_db_handle);
14143 EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
14144 error = EMAIL_ERROR_DATA_NOT_FOUND;
14151 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14153 if (err_code != NULL)
14156 EM_DEBUG_FUNC_END("ret [%d]", ret);
14163 * @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);
14164 * Prepare an array of mail_id and corresponding server mail id.
14166 * @author h.gahlaut@samsung.com
14167 * @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)
14168 * where 88 is the length of fixed keywords including ending null character in the QUERY to be formed
14169 * @param[out] idset Returns the array of mail_id and corresponding server_mail_id sorted by server_mail_ids
14170 * @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
14171 * @param[out] err_code Returns the error code.
14172 * @remarks An Example of Query to be exexuted in this API:
14173 * SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (12, 13, 56, 78);
14174 * @return This function returns true on success or false on failure.
14176 INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *multi_user_name,
14178 email_id_set_t** idset,
14182 EM_DEBUG_FUNC_BEGIN();
14183 EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds);
14185 int error = EMAIL_ERROR_NONE;
14187 email_id_set_t* p_id_set = NULL;
14189 const int buf_size = QUERY_SIZE;
14190 char sql_query_string[QUERY_SIZE] = {0, };
14191 int space_left_in_query_buffer = buf_size;
14194 char *server_mail_id = NULL;
14195 char **result = NULL;
14199 if (NULL == mail_ids || NULL == idset || NULL == id_set_count) {
14200 EM_DEBUG_EXCEPTION("Invalid Parameters mail_ids[%p] idset[%p] id_set_count [%p]",
14201 mail_ids, idset, id_set_count);
14202 if (err_code != NULL)
14203 *err_code = EMAIL_ERROR_INVALID_PARAM;
14207 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14209 SNPRINTF(sql_query_string, space_left_in_query_buffer,
14210 "SELECT local_uid, server_uid FROM mail_read_mail_uid_tbl WHERE local_uid in (%s) ORDER BY server_uid",
14213 EM_DEBUG_LOG_SEC("SQL Query formed [%s] ", sql_query_string);
14215 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14216 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14217 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result);
14218 goto FINISH_OFF; }, ("SQL(%s) sqlite3_get_table fail:%d -%s",
14219 sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14221 EM_DEBUG_LOG(" Count of mails [%d ]", count);
14224 EM_DEBUG_EXCEPTION("Can't find proper mail");
14225 error = EMAIL_ERROR_DATA_NOT_FOUND;
14229 if (NULL == (p_id_set = (email_id_set_t*)em_malloc(sizeof(email_id_set_t) * count))) {
14230 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
14231 error = EMAIL_ERROR_OUT_OF_MEMORY;
14237 EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds_Loop);
14238 EM_DEBUG_LOG(">>>> DATA ASSIGN START");
14239 for (i = 0; i < count; i++) {
14240 _get_table_field_data_int(result, &(p_id_set[i].mail_id), col_index++);
14241 _get_table_field_data_string(result, &server_mail_id, 1, col_index++);
14242 if (server_mail_id) {
14243 p_id_set[i].server_mail_id = strtoul(server_mail_id, NULL, 10);
14244 EM_SAFE_FREE(server_mail_id);
14247 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d]", count);
14248 EM_PROFILE_END(EmStorageGetIdSetFromMailIds_Loop);
14250 sqlite3_free_table(result);
14259 *id_set_count = count;
14260 EM_DEBUG_LOG(" idset[%p] id_set_count [%d]", *idset, *id_set_count);
14262 EM_SAFE_FREE(p_id_set);
14265 if (err_code != NULL)
14268 EM_PROFILE_END(EmStorageGetIdSetFromMailIds);
14270 EM_DEBUG_FUNC_END("ret [%d]", ret);
14278 INTERNAL_FUNC int emstorage_delete_triggers_from_lucene(char *multi_user_name)
14280 EM_DEBUG_FUNC_BEGIN();
14281 int ret = true, transaction = true;
14282 int error = EMAIL_ERROR_NONE;
14283 char sql_query_string[QUERY_SIZE] = {0, };
14284 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14285 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14287 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerDelete;");
14288 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14289 if (error != EMAIL_ERROR_NONE) {
14290 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14294 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerInsert;");
14295 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14296 if (error != EMAIL_ERROR_NONE) {
14297 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14301 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerUpdate;");
14302 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14303 if (error != EMAIL_ERROR_NONE) {
14304 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14311 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14313 EM_DEBUG_FUNC_END("ret [%d]", ret);
14317 INTERNAL_FUNC int emstorage_update_tag_id(char *multi_user_name, int old_filter_id, int new_filter_id, int *err_code)
14319 EM_DEBUG_FUNC_BEGIN("new_filter_id[%d], old_filter_id[%d]", new_filter_id, old_filter_id);
14321 int error = EMAIL_ERROR_NONE;
14322 char sql_query_string[QUERY_SIZE] = {0, };
14323 int transaction = true;
14325 if (old_filter_id < 0 || new_filter_id < 0) {
14326 EM_DEBUG_EXCEPTION("Invalid parameters");
14327 if (err_code != NULL)
14328 *err_code = EMAIL_ERROR_INVALID_PARAM;
14332 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14334 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14336 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14337 "UPDATE mail_tbl SET tag_id=%d WHERE tag_id=%d ", new_filter_id, old_filter_id);
14339 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14340 if (error != EMAIL_ERROR_NONE) {
14341 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14348 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14350 if (err_code != NULL)
14353 EM_DEBUG_FUNC_END("ret [%d]", ret);
14357 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)
14359 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);
14361 if ((account_id < 0) || (dest_mailbox_id < 0) || (!rule)) {
14362 EM_DEBUG_EXCEPTION("Invalid Parameter");
14364 if (err_code != NULL)
14365 *err_code = EMAIL_ERROR_INVALID_PARAM;
14369 int rc = -1, ret = false, error = EMAIL_ERROR_NONE;
14370 int count = 0, col_index = 0, i = 0, where_pararaph_length = 0, *mail_list = NULL;
14371 int tag_id = rule->rule_id;
14372 char **result = NULL, *where_pararaph = NULL;
14373 char sql_query_string[QUERY_SIZE] = {0, };
14374 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14376 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl ");
14378 where_pararaph_length = EM_SAFE_STRLEN(rule->value) + 2 * (EM_SAFE_STRLEN(rule->value2)) + 100;
14379 where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
14380 if (where_pararaph == NULL) {
14381 EM_DEBUG_EXCEPTION("malloc failed for where_pararaph.");
14382 error = EMAIL_ERROR_OUT_OF_MEMORY;
14386 if (account_id != ALL_ACCOUNT)
14387 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (0)", account_id);
14389 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_type NOT in (0)");
14391 if (rule->type & EMAIL_FILTER_SUBJECT) {
14392 if (rule->flag2 == RULE_TYPE_INCLUDES)
14393 sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject like \'%%%q%%\'", rule->value);
14394 else /* RULE_TYPE_EXACTLY */
14395 sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject = \'%q\'", rule->value);
14398 if (rule->type & EMAIL_FILTER_FROM) {
14399 if (rule->flag2 == RULE_TYPE_INCLUDES)
14400 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);
14401 #ifdef __FEATURE_COMPARE_DOMAIN__
14402 else if (rule->flag2 == RULE_TYPE_COMPARE_DOMAIN)
14403 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);
14404 #endif /*__FEATURE_COMPARE_DOMAIN__ */
14405 else /* RULE_TYPE_EXACTLY */
14406 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);
14409 if (rule->type == EMAIL_PRIORITY_SENDER) {
14410 if (rule->flag2 == RULE_TYPE_INCLUDES)
14411 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);
14412 else /* RULE_TYPE_EXACTLY */
14413 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);
14415 tag_id = PRIORITY_SENDER_TAG_ID;
14418 /* prevent 34361 */
14419 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14420 EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14421 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14423 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14424 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14425 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
14426 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14428 EM_DEBUG_LOG("Count of mails [%d]", count);
14431 mail_list = malloc(sizeof(int) * count);
14432 if (mail_list == NULL) {
14433 EM_DEBUG_EXCEPTION("malloc failed for mail_list.");
14434 error = EMAIL_ERROR_OUT_OF_MEMORY;
14440 for (i = 0; i < count; i++)
14441 _get_table_field_data_int(result, &(mail_list[i]), col_index++);
14443 memset(sql_query_string, 0x00, QUERY_SIZE);
14445 switch (rule->action_type) {
14446 case EMAIL_FILTER_MOVE:
14447 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = 0 ");
14449 case EMAIL_FILTER_BLOCK:
14451 EM_DEBUG_LOG("Not support : action_type[%d]", rule->action_type);
14456 switch (rule->action_type) {
14457 case EMAIL_FILTER_MOVE:
14458 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = %d ", tag_id);
14460 case EMAIL_FILTER_BLOCK:
14461 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET mailbox_id = %d, mailbox_type = %d ", dest_mailbox_id, dest_mailbox_type);
14464 EM_DEBUG_LOG("Not support");
14469 /* prevent 34361 */
14470 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14471 EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14473 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14474 if (error != EMAIL_ERROR_NONE) {
14475 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14479 #ifdef __FEATURE_BODY_SEARCH__
14480 /* Updating mail_text_tbl */
14481 if (rule->action_type == EMAIL_FILTER_BLOCK) {
14482 memset(sql_query_string, 0x00, QUERY_SIZE);
14483 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_text_tbl SET mailbox_id = %d ", dest_mailbox_id);
14484 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14485 EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14487 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14488 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14489 if (error != EMAIL_ERROR_NONE) {
14490 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14502 if (filtered_mail_id_list)
14503 *filtered_mail_id_list = mail_list;
14505 if (count_of_mails)
14506 *count_of_mails = count;
14508 EM_SAFE_FREE(mail_list);
14510 sqlite3_free_table(result);
14514 EM_SAFE_FREE(where_pararaph);
14516 if (err_code != NULL)
14519 EM_DEBUG_FUNC_END("ret [%d]", ret);
14523 #define EMAIL_SLOT_UNIT 25
14524 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)
14526 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);
14527 int ret = false, err = EMAIL_ERROR_NONE;
14528 int where_pararaph_length = 0;
14529 char *where_pararaph = NULL;
14530 char sql_query_string[QUERY_SIZE] = {0, };
14532 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14534 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
14536 if (new_slot_size > 0)
14537 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = %d ", new_slot_size);
14538 else if (new_slot_size == 0)
14539 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", EMAIL_SLOT_UNIT);
14541 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", new_slot_size * -1);
14545 where_pararaph_length = 80;
14547 where_pararaph_length = 50;
14549 if (new_slot_size == 0)
14550 where_pararaph_length += 70;
14552 where_pararaph = malloc(sizeof(char) * where_pararaph_length);
14553 if (where_pararaph == NULL) {
14554 EM_DEBUG_EXCEPTION("Memory allocation failed for where_pararaph");
14555 err = EMAIL_ERROR_OUT_OF_MEMORY;
14558 memset(where_pararaph, 0x00, where_pararaph_length);
14560 if (account_id > ALL_ACCOUNT) {
14563 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_id = %d ", mailbox_id);
14565 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d ", account_id);
14568 if (new_slot_size == 0)
14569 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"));
14571 if (strlen(sql_query_string) + EM_SAFE_STRLEN(where_pararaph) < QUERY_SIZE) /* prevent 34363 */
14572 EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14574 EM_DEBUG_EXCEPTION("Query buffer overflowed !!!");
14575 err = EMAIL_ERROR_OUT_OF_MEMORY;
14579 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14580 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14581 if (err != EMAIL_ERROR_NONE) {
14582 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
14589 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
14591 EM_SAFE_FREE(where_pararaph);
14593 if (err_code != NULL)
14596 EM_DEBUG_FUNC_END("ret [%d]", ret);
14600 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)
14602 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);
14604 if (!meeting_req || meeting_req->mail_id <= 0) {
14606 EM_DEBUG_EXCEPTION("mail_id[%]d", meeting_req->mail_id);
14608 if (err_code != NULL)
14609 *err_code = EMAIL_ERROR_INVALID_PARAM;
14616 int error = EMAIL_ERROR_NONE;
14617 DB_STMT hStmt = NULL;
14618 char sql_query_string[QUERY_SIZE] = {0, };
14620 time_t temp_unix_time = 0;
14622 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14623 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14625 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14626 "INSERT INTO mail_meeting_tbl VALUES "
14628 ", ?" /* account_id */
14629 ", ?" /* mailbox_id */
14630 ", ?" /* meeting_response */
14631 ", ?" /* start_time */
14632 ", ?" /* end_time */
14633 ", ?" /* location */
14634 ", ?" /* global_object_id */
14636 ", ?" /* standard_name */
14637 ", ?" /* standard_time_start_date */
14638 ", ?" /* standard_biad */
14639 ", ?" /* daylight_name */
14640 ", ?" /* daylight_time_start_date */
14641 ", ?" /* daylight_bias */
14644 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14645 if (rc != SQLITE_OK) {
14646 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
14647 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
14649 error = EMAIL_ERROR_DB_FAILURE;
14655 EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
14656 EM_DEBUG_LOG_SEC(">>>>> account_id[%d]", account_id);
14657 EM_DEBUG_LOG_SEC(">>>>> mailbox_name[%s]", mailbox_name);
14658 EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
14659 EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
14660 EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
14661 EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
14662 EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
14663 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
14664 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
14665 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
14666 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
14667 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
14668 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
14669 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
14671 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->mail_id);
14672 _bind_stmt_field_data_int(hStmt, col_index++, account_id);
14673 _bind_stmt_field_data_int(hStmt, col_index++, input_mailbox_id);
14674 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
14676 temp_unix_time = timegm(&(meeting_req->start_time));
14677 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14678 temp_unix_time = timegm(&(meeting_req->end_time));
14679 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14681 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->location, 0, LOCATION_LEN_IN_MAIL_MEETING_TBL);
14682 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->global_object_id, 0, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
14684 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
14685 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.standard_name, 0, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
14686 temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
14687 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14688 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
14690 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.daylight_name, 0, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
14691 temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
14692 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14693 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
14696 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14697 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
14698 ("sqlite3_step fail:%d", rc));
14699 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14700 ("sqlite3_step fail:%d", rc));
14705 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14706 if (hStmt != NULL) {
14707 rc = sqlite3_finalize(hStmt);
14708 if (rc != SQLITE_OK) {
14709 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14710 error = EMAIL_ERROR_DB_FAILURE;
14714 if (err_code != NULL)
14717 EM_DEBUG_FUNC_END("ret [%d]", ret);
14721 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)
14723 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);
14730 int err = EMAIL_ERROR_NONE;
14731 char **result = NULL;
14732 char sql_query_string[QUERY_SIZE] = {0, };
14733 email_meeting_request_t* p_temp_meeting_req = NULL;
14734 sqlite3 *local_db_handle = NULL;
14735 time_t temp_unix_time;
14737 if (conditional_clause == NULL || output_meeting_req == NULL || output_result_count == NULL) {
14738 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14739 err = EMAIL_ERROR_INVALID_PARAM;
14743 col_index = _field_count_of_table[CREATE_TABLE_MAIL_MEETING_TBL];
14744 EM_DEBUG_LOG("col_index [%d]", col_index);
14746 local_db_handle = emstorage_get_db_connection(multi_user_name);
14748 EMSTORAGE_START_READ_TRANSACTION(transaction);
14750 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_meeting_tbl %s", conditional_clause);
14752 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14754 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14755 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
14756 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14759 EM_DEBUG_EXCEPTION("No meeting_request found...");
14760 err = EMAIL_ERROR_DATA_NOT_FOUND;
14764 EM_DEBUG_LOG("There are [%d] meeting requests.", count);
14765 if (!(p_temp_meeting_req = (email_meeting_request_t*)em_malloc(sizeof(email_meeting_request_t) * count))) {
14766 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
14767 err = EMAIL_ERROR_OUT_OF_MEMORY;
14771 EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
14773 for (i = 0; i < count; i++) {
14774 _get_table_field_data_int(result, &(p_temp_meeting_req[i].mail_id), col_index++);
14775 _get_table_field_data_int(result, &dummy, col_index++); /* account_id. but why should this field exist in DB table? */
14776 _get_table_field_data_int(result, &dummy, col_index++); /* mailbox_id */
14777 _get_table_field_data_int(result, (int*)&(p_temp_meeting_req[i].meeting_response), col_index++);
14778 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* start time */
14779 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].start_time));
14780 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* end time */
14781 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].end_time));
14782 _get_table_field_data_string(result, &p_temp_meeting_req[i].location, 1, col_index++);
14783 _get_table_field_data_string(result, &p_temp_meeting_req[i].global_object_id, 1, col_index++);
14784 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.offset_from_GMT), col_index++);
14785 _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++);
14786 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14787 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.standard_time_start_date));
14788 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.standard_bias), col_index++);
14789 _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++);
14790 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14791 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.daylight_time_start_date));
14792 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.daylight_bias), col_index++);
14798 sqlite3_free_table(result);
14800 if (err == EMAIL_ERROR_NONE) {
14801 if (p_temp_meeting_req)
14802 *output_meeting_req = p_temp_meeting_req;
14803 *output_result_count = count;
14805 EM_SAFE_FREE(p_temp_meeting_req);
14807 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14809 // sqlite3_db_release_memory(local_db_handle);
14811 EM_DEBUG_FUNC_END("err [%d]", err);
14815 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)
14817 EM_DEBUG_FUNC_BEGIN();
14821 int error = EMAIL_ERROR_NONE;
14822 char conditional_clause[QUERY_SIZE] = {0, };
14824 EM_IF_NULL_RETURN_VALUE(meeting_req, false);
14827 SNPRINTF(conditional_clause, QUERY_SIZE, " WHERE mail_id = %d", mail_id);
14828 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
14830 if ((error = emstorage_query_meeting_request(multi_user_name, conditional_clause, meeting_req, &count, transaction)) != EMAIL_ERROR_NONE) {
14831 EM_DEBUG_EXCEPTION("emstorage_query_meeting_request failed. [%d]", error);
14835 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->mail_id[%d]", (*meeting_req)->mail_id);
14836 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->meeting_response[%d]", (*meeting_req)->meeting_response);
14837 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->start_time[%s]", asctime(&((*meeting_req)->start_time)));
14838 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->end_time[%s]", asctime(&((*meeting_req)->end_time)));
14839 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->location[%s]", (*meeting_req)->location);
14840 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->global_object_id[%s]", (*meeting_req)->global_object_id);
14841 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.offset_from_GMT[%d]", (*meeting_req)->time_zone.offset_from_GMT);
14842 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_name[%s]", (*meeting_req)->time_zone.standard_name);
14843 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.standard_time_start_date)));
14844 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_bias[%d]", (*meeting_req)->time_zone.standard_bias);
14845 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_name[%s]", (*meeting_req)->time_zone.daylight_name);
14846 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.daylight_time_start_date)));
14847 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_bias[%d]", (*meeting_req)->time_zone.daylight_bias);
14852 if (err_code != NULL)
14855 EM_DEBUG_FUNC_END("ret [%d]", ret);
14859 INTERNAL_FUNC int emstorage_update_meeting_request(char *multi_user_name, email_meeting_request_t* meeting_req, int transaction, int *err_code)
14861 EM_DEBUG_FUNC_BEGIN("meeting_req[%p], transaction[%d], err_code[%p]", meeting_req, transaction, err_code);
14864 int error = EMAIL_ERROR_NONE;
14866 DB_STMT hStmt = NULL;
14867 char sql_query_string[QUERY_SIZE] = {0, };
14868 time_t temp_unix_time = 0;
14870 if (!meeting_req) {
14871 EM_DEBUG_EXCEPTION("Invalid Parameter!");
14872 if (err_code != NULL)
14873 *err_code = EMAIL_ERROR_INVALID_PARAM;
14877 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14878 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14880 memset(sql_query_string, 0x00, sizeof(sql_query_string));
14881 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14882 "UPDATE mail_meeting_tbl "
14884 /* " account_id = ?, " // not update here, this can be changed when move or copy */
14885 /* " mailbox_name = ?, " // not update here, this can be changed when move or copy */
14886 " meeting_response = ?, "
14887 " start_time = ?, "
14890 " global_object_id = ?, "
14892 " standard_name = ?, "
14893 " standard_time_start_date = ?, "
14894 " standard_bias = ?, "
14895 " daylight_name = ?, "
14896 " daylight_time_start_date = ?, "
14897 " daylight_bias = ? "
14898 "WHERE mail_id = %d",
14899 meeting_req->mail_id);
14901 EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
14903 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14904 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14905 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14907 EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
14908 EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
14909 EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
14910 EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
14911 EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
14912 EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
14913 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
14914 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
14915 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
14916 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
14917 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
14918 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
14919 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
14923 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
14924 temp_unix_time = timegm(&(meeting_req->start_time));
14925 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14926 temp_unix_time = timegm(&(meeting_req->end_time));
14927 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14928 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->location, 1, LOCATION_LEN_IN_MAIL_MEETING_TBL);
14929 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->global_object_id, 1, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
14930 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
14931 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.standard_name, 1, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
14932 temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
14933 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14934 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
14935 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.daylight_name, 1, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
14936 temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
14937 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14938 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
14941 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14942 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14943 ("sqlite3_step fail:%d", rc));
14948 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14950 if (hStmt != NULL) {
14951 rc = sqlite3_finalize(hStmt);
14952 if (rc != SQLITE_OK) {
14953 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14954 error = EMAIL_ERROR_DB_FAILURE;
14958 if (err_code != NULL)
14961 EM_DEBUG_FUNC_END("ret [%d]", ret);
14965 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)
14967 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);
14969 if (account_id < ALL_ACCOUNT || mail_id < 0) {
14970 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d]", account_id, mail_id);
14972 if (err_code != NULL)
14973 *err_code = EMAIL_ERROR_INVALID_PARAM;
14978 int error = EMAIL_ERROR_NONE;
14980 char sql_query_string[QUERY_SIZE] = {0, };
14982 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14983 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14985 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_meeting_tbl ");
14987 if (account_id != ALL_ACCOUNT) { /* NOT '0' means a specific account. '0' means all account */
14988 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);
14992 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);
14995 if (input_mailbox_id > 0) { /* 0 means all mailbox_id */
14996 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);
14999 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15000 if (error != EMAIL_ERROR_NONE) {
15001 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
15008 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15013 EM_DEBUG_FUNC_END("ret [%d]", ret);
15018 INTERNAL_FUNC void emstorage_free_meeting_request(email_meeting_request_t *meeting_req)
15020 EM_DEBUG_FUNC_BEGIN();
15022 if (!meeting_req) return;
15024 EM_SAFE_FREE(meeting_req->location);
15025 EM_SAFE_FREE(meeting_req->global_object_id);
15027 EM_DEBUG_FUNC_END();
15030 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)
15032 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);
15033 EM_PROFILE_BEGIN(profile_emstorage_get_overflowed_mail_id_list);
15034 char sql_query_string[QUERY_SIZE] = {0, };
15035 char **result = NULL;
15036 int rc = -1, ret = false;
15037 int error = EMAIL_ERROR_NONE;
15038 int counter = 0, col_index = 0;
15039 int result_mail_id_count = 0;
15040 int *result_mail_id_list = NULL;
15042 if (input_mailbox_id <= 0 || !mail_id_list || !mail_id_count || account_id < 1) {
15043 EM_DEBUG_EXCEPTION("Invalid Parameter");
15045 *err_code = EMAIL_ERROR_INVALID_PARAM;
15049 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);
15051 EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
15053 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15054 EMSTORAGE_START_READ_TRANSACTION(transaction);
15056 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_mail_id_count, 0, NULL), rc);
15057 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15058 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15060 if (!result_mail_id_count) {
15061 EM_DEBUG_LOG("No mail found...");
15063 error = EMAIL_ERROR_MAIL_NOT_FOUND;
15067 EM_DEBUG_LOG("There are [%d] overflowed mails in mailbox_id [%d]", result_mail_id_count, input_mailbox_id);
15069 if (!(result_mail_id_list = (int *)malloc(sizeof(int) * result_mail_id_count))) {
15070 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
15071 error = EMAIL_ERROR_OUT_OF_MEMORY;
15072 sqlite3_free_table(result);
15076 memset(result_mail_id_list, 0x00, sizeof(int) * result_mail_id_count);
15080 for (counter = 0; counter < result_mail_id_count; counter++)
15081 _get_table_field_data_int(result, result_mail_id_list + counter, col_index++);
15086 EM_DEBUG_LOG("finish off [%d]", ret);
15089 sqlite3_free_table(result);
15092 *mail_id_list = result_mail_id_list;
15093 *mail_id_count = result_mail_id_count;
15095 EM_SAFE_FREE(result_mail_id_list);
15097 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15099 if (err_code != NULL)
15102 EM_PROFILE_END(profile_emstorage_get_overflowed_mail_id_list);
15103 EM_DEBUG_FUNC_END("ret [%d]", ret);
15107 INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(char *multi_user_name, int mail_id, int *thread_id, int *err_code)
15109 EM_DEBUG_FUNC_BEGIN("mail_id[%d], thread_id[%p], err_code[%p]", mail_id, thread_id, err_code);
15111 int rc = -1, ret = false;
15112 int err = EMAIL_ERROR_NONE;
15113 char sql_query_string[QUERY_SIZE] = {0, };
15115 int result_count = 0;
15117 if (mail_id == 0 || thread_id == NULL) {
15118 EM_DEBUG_EXCEPTION("Invalid Parameter");
15120 *err_code = EMAIL_ERROR_INVALID_PARAM;
15124 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15126 memset(sql_query_string, 0, QUERY_SIZE);
15127 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT thread_id FROM mail_tbl WHERE mail_id = %d", mail_id);
15129 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
15130 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
15131 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15132 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15134 if (!result_count) {
15135 EM_DEBUG_LOG("No mail found...");
15137 err = EMAIL_ERROR_MAIL_NOT_FOUND;
15138 /* sqlite3_free_table(result); */
15142 _get_table_field_data_int(result, thread_id, 1);
15144 sqlite3_free_table(result);
15150 if (err_code != NULL)
15153 EM_DEBUG_FUNC_END("ret [%d]", ret);
15157 INTERNAL_FUNC int emstorage_update_latest_thread_mail(char *multi_user_name,
15162 int *updated_thread_id,
15163 int latest_mail_id,
15164 int thread_item_count,
15169 EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id [%d], thread_id[%d], updated_thread_id[%p], "
15170 "latest_mail_id [%d], thread_item_count[%d], err_code[%p]",
15171 account_id, mailbox_id, thread_id, updated_thread_id,
15172 latest_mail_id, thread_item_count, err_code);
15174 int rc = -1, ret = false;
15175 int err = EMAIL_ERROR_NONE;
15176 char sql_query_string[QUERY_SIZE] = {0, };
15177 char **result = NULL;
15178 int result_count = 0;
15180 if (thread_id == 0) {
15181 EM_DEBUG_EXCEPTION("Invalid Parameter");
15183 *err_code = EMAIL_ERROR_INVALID_PARAM;
15187 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15189 if (thread_item_count == 0 && latest_mail_id == 0) {
15190 memset(sql_query_string, 0, QUERY_SIZE);
15191 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);
15193 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
15194 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
15195 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15196 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15197 EM_DEBUG_LOG("result_count[%d]", result_count);
15198 if (result_count == 0) {
15199 EM_DEBUG_LOG("No mail found...");
15202 *err_code = EMAIL_ERROR_MAIL_NOT_FOUND;
15203 sqlite3_free_table(result);
15207 _get_table_field_data_int(result, &latest_mail_id, 2);
15208 _get_table_field_data_int(result, &thread_item_count, 3);
15210 EM_DEBUG_LOG("latest_mail_id[%d]", latest_mail_id);
15211 EM_DEBUG_LOG("thread_item_count[%d]", thread_item_count);
15213 sqlite3_free_table(result);
15216 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15218 if (thread_item_count < 0) {
15219 memset(sql_query_string, 0, QUERY_SIZE);
15220 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);
15221 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15222 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15223 if (err != EMAIL_ERROR_NONE) {
15224 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15227 } else if (thread_id != latest_mail_id) {
15228 /* Initialize the thread id */
15229 memset(sql_query_string, 0, QUERY_SIZE);
15230 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);
15231 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15232 if (err != EMAIL_ERROR_NONE) {
15233 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15237 /* update the thread item count */
15238 memset(sql_query_string, 0, QUERY_SIZE);
15239 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);
15240 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15241 if (err != EMAIL_ERROR_NONE) {
15242 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15246 memset(sql_query_string, 0, QUERY_SIZE);
15247 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);
15248 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15249 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15250 if (err != EMAIL_ERROR_NONE) {
15251 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15258 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15260 if (thread_id != latest_mail_id) {
15261 if (err == EMAIL_ERROR_NONE) {
15262 EM_DEBUG_LOG("noti_type[%d]", noti_type);
15264 if (latest_mail_id > 0 && thread_id > 0 && noti_type > 0) {
15265 char mailbox_id_str[25] = {0,};
15266 snprintf(mailbox_id_str, sizeof(mailbox_id_str), "%d", mailbox_id);
15267 if (!emcore_notify_storage_event(noti_type, thread_id, latest_mail_id, mailbox_id_str, account_id))
15268 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventfailed [NOTI_THREAD_ID_CHANGED] >>>> ");
15270 if (updated_thread_id) *updated_thread_id = latest_mail_id;
15273 } else if (thread_item_count >= 0) {
15274 if (err == EMAIL_ERROR_NONE) {
15275 char parameter_string[500] = {0,};
15276 SNPRINTF(parameter_string, sizeof(parameter_string), "%s%c%d", "thread_item_count", 0x01, latest_mail_id);
15277 if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT, parameter_string, thread_item_count))
15278 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventfailed [NOTI_MAIL_FIELD_UPDATE] >>>> ");
15282 if (err_code != NULL)
15285 EM_DEBUG_FUNC_END("ret [%d]", ret);
15289 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)
15291 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);
15294 int err = EMAIL_ERROR_NONE;
15295 char sql_query_string[QUERY_SIZE] = {0, };
15297 if (thread_id == 0) {
15298 EM_DEBUG_EXCEPTION("Invalid Parameter");
15300 *err_code = EMAIL_ERROR_INVALID_PARAM;
15304 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15306 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15308 memset(sql_query_string, 0, QUERY_SIZE);
15309 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);
15310 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15311 if (err != EMAIL_ERROR_NONE) {
15312 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15319 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15321 if (err_code != NULL)
15324 EM_DEBUG_FUNC_END("ret [%d]", ret);
15328 #ifdef __FEATURE_LOCAL_ACTIVITY__
15330 * emstorage_add_activity - Add Email Local activity during OFFLINE mode
15333 INTERNAL_FUNC int emstorage_add_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15335 EM_DEBUG_FUNC_BEGIN();
15337 EM_DEBUG_LOG(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15339 int rc = -1, ret = false;
15340 int error = EMAIL_ERROR_NONE;
15341 DB_STMT hStmt = NULL;
15342 char sql_query_string[8192] = { 0x00, };
15345 if (!local_activity) {
15346 EM_DEBUG_EXCEPTION(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15347 if (err_code != NULL)
15348 *err_code = EMAIL_ERROR_INVALID_PARAM;
15352 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15354 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15356 memset(sql_query_string, 0x00 , sizeof(sql_query_string));
15357 SNPRINTF(sql_query_string, sizeof(sql_query_string), "INSERT INTO mail_local_activity_tbl VALUES (?, ?, ?, ?, ?, ?, ?)");
15359 EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ] ", local_activity->activity_id);
15360 EM_DEBUG_LOG(">>>>> MAIL ID [ %d ] ", local_activity->mail_id);
15361 EM_DEBUG_LOG(">>>>> ACCOUNT ID [ %d ] ", local_activity->account_id);
15362 EM_DEBUG_LOG(">>>>> ACTIVITY TYPE [ %d ] ", local_activity->activity_type);
15363 EM_DEBUG_LOG_SEC(">>>>> SERVER MAIL ID [ %s ] ", local_activity->server_mailid);
15364 EM_DEBUG_LOG(">>>>> SOURCE MAILBOX [ %s ] ", local_activity->src_mbox);
15365 EM_DEBUG_LOG(">>>>> DEST MAILBOX [ %s ] ", local_activity->dest_mbox);
15367 EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15370 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15371 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15372 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15374 EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15376 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
15377 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
15378 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
15379 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
15380 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->server_mailid, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
15381 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->src_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15382 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->dest_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15385 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15387 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
15388 ("sqlite3_step fail:%d", rc));
15389 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15390 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
15395 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15397 if (hStmt != NULL) {
15398 rc = sqlite3_finalize(hStmt);
15399 if (rc != SQLITE_OK) {
15400 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15401 error = EMAIL_ERROR_DB_FAILURE;
15405 if (err_code != NULL)
15408 EM_DEBUG_FUNC_END("ret [%d]", ret);
15413 * emstorage_get_activity - Get the Local activity Information
15417 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)
15419 EM_DEBUG_FUNC_BEGIN();
15421 int i = 0, count = 0, rc = -1, ret = false;
15422 int error = EMAIL_ERROR_NONE;
15423 emstorage_activity_tbl_t *p_activity_tbl = NULL;
15424 char sql_query_string[1024] = {0x00, };
15425 char **result = NULL;
15428 EM_IF_NULL_RETURN_VALUE(activity_list, false);
15429 EM_IF_NULL_RETURN_VALUE(select_num, false);
15432 if (!select_num || !activity_list || account_id <= 0 || activityid < 0) {
15433 EM_DEBUG_LOG(" select_num[%p], activity_list[%p] account_id [%d] activityid [%d] ", select_num, activity_list, account_id, activityid);
15435 *err_code = EMAIL_ERROR_INVALID_PARAM;
15439 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15441 EMSTORAGE_START_READ_TRANSACTION(transaction);
15443 memset(sql_query_string, 0x00, sizeof(sql_query_string));
15445 if (activityid == ALL_ACTIVITIES) {
15446 SNPRINTF(sql_query_string, sizeof(sql_query_string),
15447 "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d order by activity_id", account_id);
15449 SNPRINTF(sql_query_string, sizeof(sql_query_string),
15450 "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d AND activity_id = %d ", account_id, activityid);
15453 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
15457 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15458 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15459 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15463 if (!(p_activity_tbl = (emstorage_activity_tbl_t*)em_malloc(sizeof(emstorage_activity_tbl_t) * count))) {
15464 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
15465 error = EMAIL_ERROR_OUT_OF_MEMORY;
15470 for (i = 0; i < count; i++) {
15471 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15472 if (result[col_index])
15473 p_activity_tbl[i].activity_id = atoi(result[col_index++]);
15475 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15476 if (result[col_index])
15477 p_activity_tbl[i].account_id = atoi(result[col_index++]);
15479 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15480 if (result[col_index])
15481 p_activity_tbl[i].mail_id = atoi(result[col_index++]);
15483 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15484 if (result[col_index])
15485 p_activity_tbl[i].activity_type = atoi(result[col_index++]);
15488 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15489 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15490 p_activity_tbl[i].server_mailid = EM_SAFE_STRDUP(result[col_index++]);
15494 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15495 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15496 p_activity_tbl[i].src_mbox = EM_SAFE_STRDUP(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].dest_mbox = EM_SAFE_STRDUP(result[col_index++]);
15509 sqlite3_free_table(result);
15515 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15518 *activity_list = p_activity_tbl;
15519 *select_num = count;
15520 EM_DEBUG_LOG(">>>> COUNT : %d >> ", count);
15521 } else if (p_activity_tbl != NULL) {
15522 emstorage_free_local_activity(&p_activity_tbl, count, NULL);
15526 if (err_code != NULL)
15529 EM_DEBUG_FUNC_END("ret [%d]", ret);
15534 INTERNAL_FUNC int emstorage_get_next_activity_id(int *activity_id, int *err_code)
15537 EM_DEBUG_FUNC_BEGIN();
15540 int err = EMAIL_ERROR_NONE;
15543 char **result = NULL;
15545 if (NULL == activity_id) {
15546 EM_DEBUG_EXCEPTION(" activity_id[%p]", activity_id);
15548 *err_code = EMAIL_ERROR_INVALID_PARAM;
15552 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15554 /* increase unique id */
15556 sql = "SELECT max(rowid) FROM mail_local_activity_tbl;";
15558 /* 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); */
15559 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15560 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
15562 if (NULL == result[1])
15565 rc = atoi(result[1])+1;
15570 sqlite3_free_table(result);
15579 EM_DEBUG_FUNC_END("ret [%d]", ret);
15584 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)
15587 EM_DEBUG_FUNC_BEGIN();
15589 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);
15591 if (account_id <= 0 || NULL == activity_id_list || NULL == activity_id_count || lowest_activity_type <= 0 || highest_activity_type <= 0) {
15592 if (err_code != NULL)
15593 *err_code = EMAIL_ERROR_INVALID_PARAM;
15598 int error = EMAIL_ERROR_NONE;
15599 int i = 0, rc = -1, count = 0;
15600 char sql_query_string[1024] = {0x00, };
15601 int *activity_ids = NULL;
15603 char **result = NULL;
15604 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15605 EMSTORAGE_START_READ_TRANSACTION(transaction);
15607 memset(sql_query_string, 0x00, sizeof(sql_query_string));
15609 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);
15611 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
15614 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15615 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15616 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15620 EM_DEBUG_LOG(" Activity COUNT : %d ... ", count);
15622 if (NULL == (activity_ids = (int *)em_malloc(sizeof(int) * count))) {
15623 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
15624 error = EMAIL_ERROR_OUT_OF_MEMORY;
15628 for (i = 0; i < count; i++) {
15629 activity_ids[i] = atoi(result[col_index]);
15631 EM_DEBUG_LOG("activity_id %d", activity_ids[i]);
15640 *activity_id_count = count;
15641 *activity_id_list = activity_ids;
15642 } else if (activity_ids != NULL) /* Prevent defect - 216566 */
15643 EM_SAFE_FREE(activity_ids);
15646 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15647 if (err_code != NULL)
15650 EM_DEBUG_FUNC_END("ret [%d]", ret);
15654 INTERNAL_FUNC int emstorage_free_activity_id_list(int *activity_id_list, int *error_code)
15656 EM_DEBUG_FUNC_BEGIN();
15658 int error = EMAIL_ERROR_NONE;
15661 EM_DEBUG_LOG(" activity_id_list [%p]", activity_id_list);
15663 if (NULL == activity_id_list) {
15664 error = EMAIL_ERROR_INVALID_PARAM;
15667 EM_SAFE_FREE(activity_id_list);
15675 if (NULL != error_code)
15676 *error_code = error;
15678 EM_DEBUG_FUNC_END("ret [%d]", ret);
15683 * emstorage_delete_local_activity - Deletes the Local acitivity Generated based on activity_type
15684 * or based on server mail id
15687 INTERNAL_FUNC int emstorage_delete_local_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15689 EM_DEBUG_FUNC_BEGIN();
15692 EM_DEBUG_LOG(" local_activity[%p] ", local_activity);
15694 if (!local_activity) {
15695 EM_DEBUG_EXCEPTION(" local_activity[%p] ", local_activity);
15696 if (err_code != NULL)
15697 *err_code = EMAIL_ERROR_INVALID_PARAM;
15701 int rc = -1, ret = false; /* Prevent_FIX */
15702 int err = EMAIL_ERROR_NONE;
15704 int query_where = 0;
15705 char sql_query_string[8192] = { 0x00, };
15706 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15707 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15709 memset(sql_query_string, 0x00, sizeof(sql_query_string));
15711 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_local_activity_tbl ");
15713 EM_DEBUG_LOG_SEC(">>> Query [ %s ] ", sql_query_string);
15715 if (local_activity->account_id) {
15716 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15717 " WHERE account_id = %d ", local_activity->account_id);
15722 if (local_activity->server_mailid) {
15723 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15724 " %s %s server_mailid = '%s' ", query_where ? "" : "WHERE", query_and ? "AND" : "", local_activity->server_mailid);
15730 if (local_activity->mail_id) {
15731 EM_DEBUG_LOG(">>>> MAIL ID [ %d ] , ACTIVITY TYPE [%d ]", local_activity->mail_id, local_activity->activity_type);
15733 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15734 " %s %s mail_id = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "", local_activity->mail_id);
15741 if (local_activity->activity_type > 0) {
15742 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15743 " %s %s activity_type = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "" , local_activity->activity_type);
15746 if (local_activity->activity_id > 0) {
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 activity_id = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "" , local_activity->activity_id);
15752 EM_DEBUG_LOG_SEC(">>>>> Query [ %s ] ", sql_query_string);
15753 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15754 if (error != EMAIL_ERROR_NONE) {
15755 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
15759 rc = sqlite3_changes(local_db_handle);
15761 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
15762 err = EMAIL_ERROR_MAILBOX_NOT_FOUND;
15768 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15770 if (hStmt != NULL) {
15771 rc = sqlite3_finalize(hStmt);
15772 if (rc != SQLITE_OK) {
15773 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15774 error = EMAIL_ERROR_DB_FAILURE;
15778 if (err_code != NULL)
15780 EM_DEBUG_FUNC_END("ret [%d]", ret);
15785 * emstorage_free_local_activity - Free the Local Activity data
15787 INTERNAL_FUNC int emstorage_free_local_activity(emstorage_activity_tbl_t **local_activity_list, int count, int *err_code)
15789 EM_DEBUG_FUNC_BEGIN();
15791 EM_DEBUG_LOG(" local_activity_list[%p], count[%d], err_code[%p]", local_activity_list, count, err_code);
15794 int error = EMAIL_ERROR_INVALID_PARAM;
15797 if (!local_activity_list || !*local_activity_list) {
15798 EM_DEBUG_EXCEPTION(" local_activity_list[%p], count[%d]", local_activity_list, count);
15800 error = EMAIL_ERROR_INVALID_PARAM;
15804 emstorage_activity_tbl_t* p = *local_activity_list;
15807 for (; i < count; i++) {
15808 EM_SAFE_FREE(p[i].dest_mbox);
15809 EM_SAFE_FREE(p[i].src_mbox);
15810 EM_SAFE_FREE(p[i].server_mailid);
15814 *local_activity_list = NULL;
15822 if (err_code != NULL)
15825 EM_DEBUG_FUNC_END("ret [%d]", ret);
15829 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
15832 static int _get_key_value_string_for_list_filter_rule(email_list_filter_rule_t *input_list_filter_rule, char **output_key_value_string)
15834 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_key_value_string [%p]", input_list_filter_rule, output_key_value_string);
15836 int ret = EMAIL_ERROR_NONE;
15837 char key_value_string[QUERY_SIZE] = { 0, };
15838 char *temp_key_value_1 = NULL;
15839 char *temp_key_value_2 = NULL;
15841 if (input_list_filter_rule == NULL || output_key_value_string == NULL) {
15842 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15843 return EMAIL_ERROR_INVALID_PARAM;
15846 switch (input_list_filter_rule->target_attribute) {
15847 case EMAIL_MAIL_ATTRIBUTE_MAIL_ID:
15848 case EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID:
15849 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID:
15850 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE:
15851 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_STATUS:
15852 case EMAIL_MAIL_ATTRIBUTE_REFERENCE_MAIL_ID:
15853 case EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS:
15854 case EMAIL_MAIL_ATTRIBUTE_MAIL_SIZE:
15855 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN:
15856 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML:
15857 case EMAIL_MAIL_ATTRIBUTE_FILE_SIZE:
15858 case EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD:
15859 case EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD:
15860 case EMAIL_MAIL_ATTRIBUTE_FLAGS_FLAGGED_FIELD:
15861 case EMAIL_MAIL_ATTRIBUTE_FLAGS_ANSWERED_FIELD:
15862 case EMAIL_MAIL_ATTRIBUTE_FLAGS_RECENT_FIELD:
15863 case EMAIL_MAIL_ATTRIBUTE_FLAGS_DRAFT_FIELD:
15864 case EMAIL_MAIL_ATTRIBUTE_FLAGS_FORWARDED_FIELD:
15865 case EMAIL_MAIL_ATTRIBUTE_DRM_STATUS:
15866 case EMAIL_MAIL_ATTRIBUTE_PRIORITY:
15867 case EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS:
15868 case EMAIL_MAIL_ATTRIBUTE_LOCK_STATUS:
15869 case EMAIL_MAIL_ATTRIBUTE_REPORT_STATUS:
15870 case EMAIL_MAIL_ATTRIBUTE_ATTACHMENT_COUNT:
15871 case EMAIL_MAIL_ATTRIBUTE_INLINE_CONTENT_COUNT:
15872 case EMAIL_MAIL_ATTRIBUTE_THREAD_ID:
15873 case EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT:
15874 case EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS:
15875 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_CLASS:
15876 case EMAIL_MAIL_ATTRIBUTE_DIGEST_TYPE:
15877 case EMAIL_MAIL_ATTRIBUTE_SMIME_TYPE:
15878 case EMAIL_MAIL_ATTRIBUTE_REMAINING_RESEND_TIMES:
15879 case EMAIL_MAIL_ATTRIBUTE_TAG_ID:
15880 case EMAIL_MAIL_ATTRIBUTE_EAS_DATA_LENGTH_TYPE:
15881 SNPRINTF(key_value_string, QUERY_SIZE, "%d", input_list_filter_rule->key_value.integer_type_value);
15884 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
15885 case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
15886 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
15887 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
15888 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
15889 case EMAIL_MAIL_ATTRIBUTE_FROM:
15890 case EMAIL_MAIL_ATTRIBUTE_TO:
15891 case EMAIL_MAIL_ATTRIBUTE_CC:
15892 case EMAIL_MAIL_ATTRIBUTE_BCC:
15893 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
15894 if (input_list_filter_rule->key_value.string_type_value == NULL) {
15895 EM_DEBUG_EXCEPTION("Invalid string_type_value [%p]", input_list_filter_rule->key_value.string_type_value);
15896 ret = EMAIL_ERROR_INVALID_PARAM;
15900 temp_key_value_1 = input_list_filter_rule->key_value.string_type_value;
15902 temp_key_value_2 = em_replace_all_string(temp_key_value_1, "_", "\\_");
15903 temp_key_value_1 = em_replace_all_string(temp_key_value_2, "%", "\\%");
15905 if (input_list_filter_rule->rule_type == EMAIL_LIST_FILTER_RULE_INCLUDE)
15906 SNPRINTF(key_value_string, QUERY_SIZE, "\'%%%s%%\'", temp_key_value_1);
15908 SNPRINTF(key_value_string, QUERY_SIZE, "\'%s\'", temp_key_value_1);
15911 case EMAIL_MAIL_ATTRIBUTE_DATE_TIME:
15912 case EMAIL_MAIL_ATTRIBUTE_SCHEDULED_SENDING_TIME:
15913 case EMAIL_MAIL_ATTRIBUTE_REPLIED_TIME:
15914 case EMAIL_MAIL_ATTRIBUTE_FORWARDED_TIME:
15915 SNPRINTF(key_value_string, QUERY_SIZE, "%d", (int)input_list_filter_rule->key_value.datetime_type_value);
15919 ret = EMAIL_ERROR_INVALID_PARAM;
15920 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
15924 if (ret == EMAIL_ERROR_NONE && EM_SAFE_STRLEN(key_value_string) > 0)
15925 *output_key_value_string = strdup(key_value_string);
15929 EM_SAFE_FREE(temp_key_value_1);
15930 EM_SAFE_FREE(temp_key_value_2);
15932 EM_DEBUG_FUNC_END("ret [%d]", ret);
15936 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15937 static int _get_cert_password_file_name(int index, char *cert_password_file_name)
15939 EM_DEBUG_FUNC_BEGIN("index : [%d]", index);
15941 if (index <= 0 || !cert_password_file_name) {
15942 EM_DEBUG_EXCEPTION("Invalid parameter");
15943 return EMAIL_ERROR_INVALID_PARAM;
15946 sprintf(cert_password_file_name, ".email_cert_%d", index);
15948 EM_DEBUG_FUNC_END();
15949 return EMAIL_ERROR_NONE;
15953 static int _make_filter_rule_string(email_list_filter_rule_t *input_list_filter_rule, char **output_string)
15955 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
15957 int ret = EMAIL_ERROR_NONE;
15959 int length_field_name = 0;
15960 int length_value = 0;
15961 char result_rule_string[QUERY_SIZE] = { 0 , };
15962 char *mod_field_name_string = NULL;
15963 char *mod_value_string = NULL;
15964 char *temp_field_name_string = NULL;
15965 char *temp_key_value_string = NULL;
15967 if (input_list_filter_rule == NULL || output_string == NULL) {
15968 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15969 return EMAIL_ERROR_INVALID_PARAM;
15972 temp_field_name_string = emcore_get_mail_field_name_by_attribute_type(input_list_filter_rule->target_attribute);
15974 if (temp_field_name_string == NULL) {
15975 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
15976 return EMAIL_ERROR_INVALID_PARAM;
15979 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) {
15980 EM_DEBUG_EXCEPTION("_get_key_value_string_for_list_filter_rule failed");
15981 return EMAIL_ERROR_INVALID_PARAM;
15984 length_field_name = EM_SAFE_STRLEN(temp_field_name_string);
15985 length_value = EM_SAFE_STRLEN(temp_key_value_string);
15987 switch (input_list_filter_rule->target_attribute) {
15988 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
15989 case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
15990 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
15991 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
15992 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
15993 case EMAIL_MAIL_ATTRIBUTE_FROM:
15994 case EMAIL_MAIL_ATTRIBUTE_TO:
15995 case EMAIL_MAIL_ATTRIBUTE_CC:
15996 case EMAIL_MAIL_ATTRIBUTE_BCC:
15997 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
16005 if (is_alpha == 1 && input_list_filter_rule->case_sensitivity == false) {
16006 length_field_name += strlen("UPPER() ");
16007 length_value += strlen("UPPER() ");
16008 mod_field_name_string = em_malloc(sizeof(char) * length_field_name);
16009 if (mod_field_name_string == NULL) {
16010 EM_DEBUG_EXCEPTION("em_mallocfailed");
16011 EM_SAFE_FREE(temp_field_name_string);
16012 ret = EMAIL_ERROR_OUT_OF_MEMORY;
16016 mod_value_string = em_malloc(sizeof(char) * length_value);
16017 if (mod_value_string == NULL) {
16018 EM_DEBUG_EXCEPTION("em_mallocfailed");
16019 EM_SAFE_FREE(temp_field_name_string);
16020 ret = EMAIL_ERROR_OUT_OF_MEMORY;
16024 SNPRINTF(mod_field_name_string, length_field_name, "UPPER(%s)", temp_field_name_string);
16025 SNPRINTF(mod_value_string, length_value, "UPPER(%s)", temp_key_value_string);
16026 EM_SAFE_FREE(temp_key_value_string);
16028 mod_field_name_string = strdup(temp_field_name_string);
16029 mod_value_string = temp_key_value_string;
16032 switch (input_list_filter_rule->rule_type) {
16033 case EMAIL_LIST_FILTER_RULE_EQUAL:
16034 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = %s ", mod_field_name_string, mod_value_string);
16036 case EMAIL_LIST_FILTER_RULE_NOT_EQUAL:
16037 SNPRINTF(result_rule_string, QUERY_SIZE, "%s != %s ", mod_field_name_string, mod_value_string);
16039 case EMAIL_LIST_FILTER_RULE_LESS_THAN:
16040 SNPRINTF(result_rule_string, QUERY_SIZE, "%s < %s ", mod_field_name_string, mod_value_string);
16042 case EMAIL_LIST_FILTER_RULE_GREATER_THAN:
16043 SNPRINTF(result_rule_string, QUERY_SIZE, "%s > %s ", mod_field_name_string, mod_value_string);
16045 case EMAIL_LIST_FILTER_RULE_LESS_THAN_OR_EQUAL:
16046 SNPRINTF(result_rule_string, QUERY_SIZE, "%s <= %s ", mod_field_name_string, mod_value_string);
16048 case EMAIL_LIST_FILTER_RULE_GREATER_THAN_OR_EQUAL:
16049 SNPRINTF(result_rule_string, QUERY_SIZE, "%s >= %s ", mod_field_name_string, mod_value_string);
16051 case EMAIL_LIST_FILTER_RULE_INCLUDE:
16052 SNPRINTF(result_rule_string, QUERY_SIZE, "%s LIKE %s ", mod_field_name_string, mod_value_string);
16054 case EMAIL_LIST_FILTER_RULE_IN:
16055 SNPRINTF(result_rule_string, QUERY_SIZE, "%s IN (%s) ", mod_field_name_string, mod_value_string);
16057 case EMAIL_LIST_FILTER_RULE_NOT_IN:
16058 SNPRINTF(result_rule_string, QUERY_SIZE, "%s NOT IN (%s) ", mod_field_name_string, mod_value_string);
16061 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16062 ret = EMAIL_ERROR_INVALID_PARAM;
16066 *output_string = strdup(result_rule_string);
16069 EM_SAFE_FREE(mod_field_name_string);
16070 EM_SAFE_FREE(mod_value_string);
16072 EM_DEBUG_FUNC_END("ret [%d]", ret);
16076 static int _make_filter_attach_rule_string(char *multi_user_name, email_list_filter_rule_attach_t *input_list_filter_rule, char **output_string)
16078 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16080 char *field_name_string = NULL;
16081 char key_value_string[QUERY_SIZE] = {0,};
16082 char result_rule_string[QUERY_SIZE] = {0,};
16085 int query_size = 0;
16088 int error = EMAIL_ERROR_NONE;
16089 char **result = NULL;
16090 char sql_query_string[QUERY_SIZE] = {0,};
16091 char *sql_query_string2 = NULL;
16092 sqlite3 *local_db_handle = NULL;
16093 int *mail_ids = NULL;
16095 if (input_list_filter_rule == NULL || output_string == NULL) {
16096 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16097 return EMAIL_ERROR_INVALID_PARAM;
16100 field_name_string = EM_SAFE_STRDUP("attachment_name");
16101 SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
16103 switch (input_list_filter_rule->rule_type) {
16105 case EMAIL_LIST_FILTER_RULE_INCLUDE:
16106 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
16109 case EMAIL_LIST_FILTER_RULE_MATCH:
16110 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
16114 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16115 error = EMAIL_ERROR_INVALID_PARAM;
16119 local_db_handle = emstorage_get_db_connection(multi_user_name);
16121 EMSTORAGE_START_READ_TRANSACTION(true);
16122 SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_attachment_tbl %s", result_rule_string);
16124 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16125 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16126 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16131 EM_DEBUG_LOG("No mail found...");
16132 error = EMAIL_ERROR_MAIL_NOT_FOUND;
16133 *output_string = strdup("mail_id IN () ");
16137 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
16139 if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
16140 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
16141 error = EMAIL_ERROR_OUT_OF_MEMORY;
16145 for (i = 0; i < count; i++) {
16146 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16147 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16150 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >>", count);
16151 sqlite3_free_table(result);
16152 EMSTORAGE_FINISH_READ_TRANSACTION(true);
16154 // sqlite3_db_release_memory(local_db_handle);
16157 query_size = (10 * count) + strlen("mail_id IN () ");
16159 sql_query_string2 = em_malloc(query_size);
16160 if (sql_query_string2 == NULL) {
16161 EM_DEBUG_EXCEPTION("em_mallocfailed");
16162 error = EMAIL_ERROR_OUT_OF_MEMORY;
16166 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN (");
16167 for (i = 0; i < count-1; i++)
16168 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16170 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d) ", mail_ids[count-1]);
16172 *output_string = strdup(sql_query_string2);
16175 EM_SAFE_FREE(mail_ids); /* prevent */
16176 EM_SAFE_FREE(sql_query_string2);
16177 EM_SAFE_FREE(field_name_string);
16178 EM_DEBUG_FUNC_END("error [%d]", error);
16182 static int _make_filter_fts_rule_string(char *multi_user_name, email_list_filter_rule_fts_t *input_list_filter_rule, char **output_string)
16184 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16185 char *field_name_string = NULL;
16186 char key_value_string[QUERY_SIZE] = {0,};
16187 char result_rule_string[QUERY_SIZE] = {0,};
16191 int query_size = 0;
16192 int error = EMAIL_ERROR_NONE;
16193 char **result = NULL;
16194 char sql_query_string[QUERY_SIZE] = {0,};
16195 char *sql_query_string2 = NULL;
16196 sqlite3 *local_db_handle = NULL;
16197 int *mail_ids = NULL;
16199 if (input_list_filter_rule == NULL || output_string == NULL) {
16200 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16201 return EMAIL_ERROR_INVALID_PARAM;
16204 field_name_string = EM_SAFE_STRDUP("body_text");
16205 SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
16207 switch (input_list_filter_rule->rule_type) {
16209 case EMAIL_LIST_FILTER_RULE_INCLUDE:
16210 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
16213 case EMAIL_LIST_FILTER_RULE_MATCH:
16214 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
16218 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16219 error = EMAIL_ERROR_INVALID_PARAM;
16223 local_db_handle = emstorage_get_db_connection(multi_user_name);
16225 EMSTORAGE_START_READ_TRANSACTION(true);
16226 SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_text_tbl %s", result_rule_string);
16228 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16229 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16230 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16235 EM_DEBUG_LOG("No mail found...");
16236 error = EMAIL_ERROR_MAIL_NOT_FOUND;
16237 *output_string = strdup("mail_id IN () ");
16241 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
16244 if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
16245 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
16246 error = EMAIL_ERROR_OUT_OF_MEMORY;
16250 for (i = 0; i < count; i++) {
16251 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16252 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16255 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >>", count);
16256 sqlite3_free_table(result);
16257 EMSTORAGE_FINISH_READ_TRANSACTION(true);
16259 // sqlite3_db_release_memory(local_db_handle);
16262 query_size = (10 * count) + strlen("mail_id IN () ");
16263 sql_query_string2 = em_malloc(query_size);
16264 if (sql_query_string2 == NULL) {
16265 EM_DEBUG_EXCEPTION("em_mallocfailed");
16266 error = EMAIL_ERROR_OUT_OF_MEMORY;
16270 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN (");
16271 for (i = 0; i < count-1; i++)
16272 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16274 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d) ", mail_ids[count-1]);
16276 *output_string = strdup(sql_query_string2);
16279 EM_SAFE_FREE(mail_ids); /* prevent */
16280 EM_SAFE_FREE(sql_query_string2);
16281 EM_SAFE_FREE(field_name_string);
16282 EM_DEBUG_FUNC_END("error [%d]", error);
16286 static int _make_order_rule_string(char *multi_user_name, email_list_sorting_rule_t *input_sorting_rule, char **output_string)
16288 EM_DEBUG_FUNC_BEGIN("input_sorting_rule [%p], output_string [%p]", input_sorting_rule, output_string);
16290 char result_rule_string[QUERY_SIZE] = { 0 , };
16291 int ret = EMAIL_ERROR_NONE;
16293 emstorage_account_tbl_t *account_tbl_array = NULL;
16296 char *result_str = NULL;
16297 char *tmp_str1 = NULL;
16298 char *tmp_str2 = NULL;
16299 char query_per_account[QUERY_SIZE] = { 0 , };
16301 if (input_sorting_rule->force_boolean_check) {
16302 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = 0 ",
16303 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16305 EM_SAFE_STRNCPY(result_rule_string,
16306 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16307 QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16310 switch (input_sorting_rule->sort_order) {
16311 case EMAIL_SORT_ORDER_ASCEND:
16312 EM_SAFE_STRNCAT(result_rule_string, " ASC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16315 case EMAIL_SORT_ORDER_DESCEND:
16316 EM_SAFE_STRNCAT(result_rule_string, " DESC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16319 case EMAIL_SORT_ORDER_NOCASE_ASCEND:
16320 EM_SAFE_STRNCAT(result_rule_string, " COLLATE NOCASE ASC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16323 case EMAIL_SORT_ORDER_NOCASE_DESCEND:
16324 EM_SAFE_STRNCAT(result_rule_string, " COLLATE NOCASE DESC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16327 case EMAIL_SORT_ORDER_TO_CCBCC:
16328 memset(result_rule_string, 0, QUERY_SIZE);
16329 if (input_sorting_rule->key_value.string_type_value)
16330 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16331 " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 ELSE 2 END ",
16332 input_sorting_rule->key_value.string_type_value);
16335 case EMAIL_SORT_ORDER_TO_CC_BCC:
16336 memset(result_rule_string, 0, QUERY_SIZE);
16337 if (input_sorting_rule->key_value.string_type_value)
16338 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16339 " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 WHEN full_address_cc LIKE \'%%%q%%\' THEN 2 ELSE 3 END ",
16340 input_sorting_rule->key_value.string_type_value, input_sorting_rule->key_value.string_type_value);
16343 case EMAIL_SORT_ORDER_TO_CCBCC_ALL:
16344 if (!emstorage_get_account_list(multi_user_name, &count, &account_tbl_array, true, false, NULL)) {
16345 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed");
16350 EM_DEBUG_LOG("No account exist");
16351 ret = EMAIL_ERROR_INVALID_PARAM;
16355 for (i = 0; i < count; i++) {
16356 if (i > 0 && result_str) {
16357 tmp_str2 = result_str;
16358 result_str = g_strconcat(tmp_str2, " OR ", NULL);
16359 EM_SAFE_FREE(tmp_str2);
16362 memset(query_per_account, 0, QUERY_SIZE);
16363 snprintf(query_per_account, QUERY_SIZE,
16364 "(account_id = %d AND full_address_to LIKE \'%%%s%%\')",
16365 account_tbl_array[i].account_id, account_tbl_array[i].user_email_address);
16367 tmp_str1 = result_str;
16369 result_str = g_strconcat(tmp_str1, query_per_account, NULL);
16371 result_str = g_strdup(query_per_account);
16372 EM_SAFE_FREE(tmp_str1);
16375 snprintf(result_rule_string, QUERY_SIZE,
16376 " CASE WHEN %s THEN 1 ELSE 2 END ", result_str);
16378 EM_SAFE_FREE(result_str);
16379 if (account_tbl_array)
16380 emstorage_free_account(&account_tbl_array, count, NULL);
16383 case EMAIL_SORT_ORDER_LOCALIZE_ASCEND:
16384 memset(result_rule_string, 0, QUERY_SIZE);
16385 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16386 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END ASC, %s COLLATE NOCASE ASC ",
16387 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16388 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16391 case EMAIL_SORT_ORDER_LOCALIZE_DESCEND:
16392 memset(result_rule_string, 0, QUERY_SIZE);
16393 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16394 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END DESC, %s COLLATE NOCASE DESC ",
16395 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16396 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16400 EM_DEBUG_EXCEPTION("Invalid sort_order [%d]", input_sorting_rule->sort_order);
16401 ret = EMAIL_ERROR_INVALID_PARAM;
16405 *output_string = strdup(result_rule_string);
16408 EM_DEBUG_FUNC_END("ret [%d]", ret);
16412 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)
16414 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);
16415 int ret = EMAIL_ERROR_NONE;
16417 int string_offset = 0;
16418 int query_size = 0;
16419 int new_query_size = 0;
16420 char *conditional_clause_string = NULL;
16421 char *result_string_for_a_item = NULL;
16423 if ((input_filter_count > 0 && !input_filter_list) || (input_sorting_rule_count > 0 && !input_sorting_rule_list) || output_conditional_clause == NULL) {
16424 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16425 return EMAIL_ERROR_INVALID_PARAM;
16428 conditional_clause_string = em_malloc(QUERY_SIZE);
16429 if (conditional_clause_string == NULL) {
16430 EM_DEBUG_EXCEPTION("Memory is full");
16431 return EMAIL_ERROR_OUT_OF_MEMORY;
16434 if (input_filter_count > 0) {
16435 query_size = QUERY_SIZE;
16436 g_strlcpy(conditional_clause_string, " WHERE ", QUERY_SIZE);
16438 for (i = 0; i < input_filter_count; i++) {
16439 switch (input_filter_list[i].list_filter_item_type) {
16440 case EMAIL_LIST_FILTER_ITEM_RULE:
16441 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE", i);
16442 _make_filter_rule_string(&(input_filter_list[i].list_filter_item.rule), &result_string_for_a_item);
16445 case EMAIL_LIST_FILTER_ITEM_RULE_FTS:
16446 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_FTS", i);
16447 _make_filter_fts_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_fts), &result_string_for_a_item);
16450 case EMAIL_LIST_FILTER_ITEM_RULE_ATTACH:
16451 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_ATTACH", i);
16452 _make_filter_attach_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_attach), &result_string_for_a_item);
16455 case EMAIL_LIST_FILTER_ITEM_OPERATOR:
16456 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_OPERATOR", i);
16457 switch (input_filter_list[i].list_filter_item.operator_type) {
16458 case EMAIL_LIST_FILTER_OPERATOR_AND:
16459 result_string_for_a_item = strdup("AND ");
16461 case EMAIL_LIST_FILTER_OPERATOR_OR:
16462 result_string_for_a_item = strdup("OR ");
16464 case EMAIL_LIST_FILTER_OPERATOR_LEFT_PARENTHESIS:
16465 result_string_for_a_item = strdup(" (");
16467 case EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS:
16468 result_string_for_a_item = strdup(") ");
16474 EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", input_filter_list[i].list_filter_item_type);
16475 ret = EMAIL_ERROR_INVALID_PARAM;
16479 if (result_string_for_a_item == NULL) {
16480 EM_DEBUG_EXCEPTION("result_string_for_a_item is null");
16481 ret = EMAIL_ERROR_INVALID_PARAM;
16485 if (strlen(conditional_clause_string) + EM_SAFE_STRLEN(result_string_for_a_item) >= query_size) { /* prevent 34364 */
16486 EM_DEBUG_LOG("QUERY is too long");
16487 new_query_size = EM_SAFE_STRLEN(result_string_for_a_item) + EM_SAFE_STRLEN(conditional_clause_string) + QUERY_SIZE;
16488 conditional_clause_string = realloc(conditional_clause_string, new_query_size);
16489 if (conditional_clause_string == NULL) {
16490 EM_DEBUG_EXCEPTION("realloc failed");
16491 ret = EMAIL_ERROR_OUT_OF_MEMORY;
16495 query_size = new_query_size;
16498 EM_SAFE_STRNCAT(conditional_clause_string, result_string_for_a_item , QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16499 EM_SAFE_FREE(result_string_for_a_item);
16503 if (input_sorting_rule_count > 0) {
16504 EM_SAFE_STRNCAT(conditional_clause_string, "ORDER BY ", QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16506 for (i = 0; i < input_sorting_rule_count; i++) {
16507 if ((ret = _make_order_rule_string(multi_user_name, &input_sorting_rule_list[i], &result_string_for_a_item)) != EMAIL_ERROR_NONE) {
16508 EM_DEBUG_EXCEPTION("_make_order_rule_string failed. [%d]", ret);
16512 EM_SAFE_STRNCAT(conditional_clause_string, ", " , QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16513 EM_SAFE_STRNCAT(conditional_clause_string, result_string_for_a_item , QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16514 EM_SAFE_FREE(result_string_for_a_item);
16518 if (input_start_index != -1 && input_limit_count != -1) {
16519 string_offset = strlen(conditional_clause_string);
16520 SNPRINTF_OFFSET(conditional_clause_string, string_offset, query_size, " LIMIT %d, %d", input_start_index, input_limit_count);
16523 *output_conditional_clause = strdup(conditional_clause_string);
16526 EM_SAFE_FREE(result_string_for_a_item);
16527 EM_SAFE_FREE(conditional_clause_string);
16529 EM_DEBUG_FUNC_END("ret [%d]", ret);
16533 INTERNAL_FUNC int emstorage_free_list_filter(email_list_filter_t **input_filter_list, int input_filter_count)
16535 EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d]", input_filter_list, input_filter_count);
16536 int err = EMAIL_ERROR_NONE;
16538 email_list_filter_t *temp_filter_list = NULL;
16540 EM_IF_NULL_RETURN_VALUE(input_filter_list, EMAIL_ERROR_INVALID_PARAM);
16542 for (i = 0; i < input_filter_count; i++) {
16543 temp_filter_list = (*input_filter_list) + i;
16544 if (!temp_filter_list)
16547 if (temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE) {
16548 switch (temp_filter_list->list_filter_item.rule.target_attribute) {
16549 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
16550 case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
16551 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
16552 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
16553 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
16554 case EMAIL_MAIL_ATTRIBUTE_FROM:
16555 case EMAIL_MAIL_ATTRIBUTE_TO:
16556 case EMAIL_MAIL_ATTRIBUTE_CC:
16557 case EMAIL_MAIL_ATTRIBUTE_BCC:
16558 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN:
16559 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML:
16560 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
16561 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule.key_value.string_type_value);
16566 } 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) {
16567 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_fts.key_value.string_type_value);
16568 } 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) {
16569 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_attach.key_value.string_type_value);
16573 EM_SAFE_FREE(*input_filter_list);
16575 EM_DEBUG_FUNC_END("err [%d]", err);
16579 /* Tasks --------------------------------------------------------------------------*/
16580 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)
16582 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);
16586 int err = EMAIL_ERROR_NONE;
16588 DB_STMT hStmt = NULL;
16589 char sql_query_string[QUERY_SIZE] = {0, };
16590 sqlite3 *local_db_handle = NULL;
16591 char *sql = "SELECT max(rowid) FROM mail_task_tbl;";
16592 char **result = NULL;
16594 if (input_task_parameter == NULL || output_task_id == NULL) {
16595 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16596 return EMAIL_ERROR_INVALID_PARAM;
16599 local_db_handle = emstorage_get_db_connection(multi_user_name);
16600 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, err);
16602 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
16603 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16604 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
16606 if (NULL == result[1])
16609 task_id = atoi(result[1])+1;
16611 *output_task_id = task_id;
16613 sqlite3_free_table(result);
16616 SNPRINTF(sql_query_string, sizeof(sql_query_string),
16617 "INSERT INTO mail_task_tbl VALUES "
16619 " ? " /* task_id */
16620 " , ? " /* task_type */
16621 " , ? " /* task_status */
16622 " , ? " /* task_priority */
16623 " , ? " /* task_parameter_length */
16624 " , ? " /* task_parameter */
16625 " , ? " /* date_time */
16628 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16629 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16630 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16632 EM_DEBUG_LOG_SEC(">>>> SQL STMT [%s] ", sql_query_string);
16635 _bind_stmt_field_data_int(hStmt, i++, task_id);
16636 _bind_stmt_field_data_int(hStmt, i++, input_task_type);
16637 _bind_stmt_field_data_int(hStmt, i++, EMAIL_TASK_STATUS_WAIT);
16638 _bind_stmt_field_data_int(hStmt, i++, input_task_priority);
16639 _bind_stmt_field_data_int(hStmt, i++, input_task_parameter_length);
16640 _bind_stmt_field_data_blob(hStmt, i++, input_task_parameter, input_task_parameter_length);
16641 _bind_stmt_field_data_int(hStmt, i++, time(NULL));
16643 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16645 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {err = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
16646 ("sqlite3_step fail:%d", rc));
16647 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16648 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
16650 ret = (err == EMAIL_ERROR_NONE);
16653 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, err);
16655 if (hStmt != NULL) {
16656 rc = sqlite3_finalize(hStmt);
16657 if (rc != SQLITE_OK) {
16658 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16659 err = EMAIL_ERROR_DB_FAILURE;
16663 EM_DEBUG_FUNC_END("err [%d]", err);
16667 INTERNAL_FUNC int emstorage_delete_task(char *multi_user_name, int task_id, int transaction)
16669 EM_DEBUG_FUNC_BEGIN("task_id[%d], transaction[%d]", task_id, transaction);
16671 int err = EMAIL_ERROR_NONE;
16672 char sql_query_string[QUERY_SIZE] = {0, };
16674 sqlite3 *local_db_handle = NULL;
16677 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16678 return EMAIL_ERROR_INVALID_PARAM;
16681 local_db_handle = emstorage_get_db_connection(multi_user_name);
16683 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
16685 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_task_tbl WHERE task_id = %d", task_id);
16686 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16687 if (err != EMAIL_ERROR_NONE) {
16688 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16695 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
16697 EM_DEBUG_FUNC_END("err [%d]", err);
16701 INTERNAL_FUNC int emstorage_update_task_status(char *multi_user_name, int task_id, email_task_status_type_t task_status, int transaction)
16703 EM_DEBUG_FUNC_BEGIN("task_id[%d] task_status[%d] transaction[%d]", task_id, task_status, transaction);
16705 int err = EMAIL_ERROR_NONE;
16706 char sql_query_string[QUERY_SIZE] = {0, };
16708 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16709 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
16711 SNPRINTF(sql_query_string, sizeof(sql_query_string),
16712 "UPDATE mail_task_tbl SET"
16713 " task_status = %d"
16714 " WHERE task_id = %d"
16717 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16718 if (err != EMAIL_ERROR_NONE) {
16719 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16726 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
16728 EM_DEBUG_FUNC_END("err [%d]", err);
16732 INTERNAL_FUNC int emstorage_query_task(char *multi_user_name,
16733 const char *input_conditional_clause,
16734 const char *input_ordering_clause,
16735 email_task_t **output_task_list,
16736 int *output_task_count)
16738 EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_ordering_clause [%p], "
16739 "output_task_list[%p], output_task_count[%d]",
16740 input_conditional_clause, input_ordering_clause, output_task_list, output_task_count);
16741 int i = 0, count = 0, rc = -1;
16743 int field_index = 0;
16744 int err = EMAIL_ERROR_NONE;
16745 email_task_t *task_item_from_tbl = NULL;
16746 char sql_query_string[QUERY_SIZE] = {0, };
16747 char *field_list = "task_id, task_type, task_status, task_priority, task_parameter_length, task_parameter ";
16749 sqlite3 *local_db_handle = NULL;
16750 DB_STMT hStmt = NULL;
16752 EM_IF_NULL_RETURN_VALUE(input_conditional_clause, false);
16753 EM_IF_NULL_RETURN_VALUE(output_task_count, false);
16755 local_db_handle = emstorage_get_db_connection(multi_user_name);
16757 SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
16758 "SELECT COUNT(*) FROM mail_task_tbl %s", input_conditional_clause);
16759 EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
16761 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
16762 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16763 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16765 count = atoi(result[1]);
16766 sqlite3_free_table(result);
16768 EM_DEBUG_LOG("count = %d", rc);
16771 EM_DEBUG_EXCEPTION("no task found...");
16772 err = EMAIL_ERROR_TASK_NOT_FOUND;
16776 SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
16777 "SELECT %s FROM mail_task_tbl %s %s", field_list, input_conditional_clause, input_ordering_clause);
16778 EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
16780 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16782 EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
16783 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16784 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16786 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16787 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16788 ("sqlite3_step fail:%d", rc));
16790 if (rc == SQLITE_DONE) {
16791 EM_DEBUG_EXCEPTION("no task found...");
16792 err = EMAIL_ERROR_TASK_NOT_FOUND;
16797 if (!(task_item_from_tbl = (email_task_t*)em_malloc(sizeof(email_task_t) * count))) {
16798 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
16799 err = EMAIL_ERROR_OUT_OF_MEMORY;
16803 for (i = 0; i < count; i++) {
16804 /* get recordset */
16807 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_id), field_index++);
16808 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_type), field_index++);
16809 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_status), field_index++);
16810 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_priority), field_index++);
16811 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_parameter_length), field_index++);
16812 _get_stmt_field_data_blob(hStmt, (void**)&(task_item_from_tbl[i].task_parameter), field_index++);
16814 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16815 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i, rc);
16816 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16817 ("sqlite3_step fail:%d", rc));
16822 if (err == EMAIL_ERROR_NONE) {
16823 if (output_task_list)
16824 *output_task_list = task_item_from_tbl;
16825 *output_task_count = count;
16827 if (task_item_from_tbl) {
16828 for (i = 0; i < count; i++)
16829 EM_SAFE_FREE(task_item_from_tbl[i].task_parameter);
16831 free(task_item_from_tbl);
16835 if (hStmt != NULL) {
16836 rc = sqlite3_finalize(hStmt);
16838 if (rc != SQLITE_OK) {
16839 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16840 err = EMAIL_ERROR_DB_FAILURE;
16844 EM_DEBUG_FUNC_END("err [%d]", err);
16848 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)
16850 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);
16851 int err = EMAIL_ERROR_NONE;
16853 if (message_id == NULL) {
16854 EM_DEBUG_EXCEPTION("Invalid parameter");
16855 err = EMAIL_ERROR_INVALID_PARAM;
16861 int temp_mail_id = 0;
16862 int where_pararaph_length = 0;
16863 char *where_pararaph = NULL;
16864 char sql_query_string[QUERY_SIZE] = {0, };
16865 char **result = NULL;
16866 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16868 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id from mail_tbl ");
16870 where_pararaph_length = EM_SAFE_STRLEN(message_id) + 100;
16871 where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
16872 if (where_pararaph == NULL) {
16873 EM_DEBUG_EXCEPTION("em_mallocfailed");
16874 err = EMAIL_ERROR_OUT_OF_MEMORY;
16878 if (account_id != ALL_ACCOUNT)
16879 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);
16881 sqlite3_snprintf(where_pararaph_length, where_pararaph, "WHERE mailbox_type = %d AND message_id like '%q'", input_mailbox_type, message_id);
16883 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
16884 EM_SAFE_STRNCAT(sql_query_string, where_pararaph , QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
16886 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
16888 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16889 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16890 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16893 EM_DEBUG_LOG("Count of mails [%d]", count);
16896 _get_table_field_data_int(result, &temp_mail_id, 1);
16897 EM_DEBUG_LOG("Searched mail_id [%d]", temp_mail_id);
16899 memset(sql_query_string, 0x00, QUERY_SIZE);
16900 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string, "UPDATE mail_tbl set server_mail_id = '%q'", server_uid);
16902 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE) {
16903 EM_SAFE_STRNCAT(sql_query_string, where_pararaph,
16904 QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
16907 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16908 if (err != EMAIL_ERROR_NONE) {
16909 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16914 err = EMAIL_ERROR_MAIL_NOT_FOUND;
16919 sqlite3_free_table(result);
16923 EM_SAFE_FREE(where_pararaph);
16925 if (mail_id != NULL)
16926 *mail_id = temp_mail_id;
16928 EM_DEBUG_FUNC_END("err : [%d]", err);
16931 /* Tasks --------------------------------------------------------------------------*/
16933 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
16934 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)
16936 EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
16938 if (!local_activity || !activity_id) {
16939 EM_DEBUG_EXCEPTION("local_activity[%p], activity_id[%p]", local_activity, activity_id);
16940 if (err_code != NULL)
16941 *err_code = EMAIL_ERROR_INVALID_PARAM;
16947 int error = EMAIL_ERROR_NONE;
16950 char sql_query_string[QUERY_SIZE] = {0, };
16951 DB_STMT hStmt = NULL;
16953 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16954 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
16956 memset(sql_query_string, 0x00, sizeof(sql_query_string));
16957 SNPRINTF(sql_query_string, sizeof(sql_query_string),
16958 "INSERT INTO mail_auto_download_activity_tbl VALUES "
16960 "? " /* Activity ID */
16962 ",?" /* Account ID */
16963 ",?" /* Local Mail ID */
16964 ",?" /* Server mail ID */
16965 ",?" /* Mailbox ID*/
16966 ",?" /* Multi USER NAME */
16969 char *sql = "SELECT max(rowid) FROM mail_auto_download_activity_tbl;";
16970 char **result = NULL;
16973 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
16975 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16976 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
16978 if (NULL == result[1]) rc = 1;
16979 else rc = atoi(result[1])+1;
16980 sqlite3_free_table(result);
16983 *activity_id = local_activity->activity_id = rc;
16985 EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [%d], MAIL ID [%d], SERVER MAIL ID [%lu]",
16986 local_activity->activity_id, local_activity->mail_id, local_activity->server_mail_id);
16988 if (local_activity->mailbox_id)
16989 EM_DEBUG_LOG(" MAILBOX ID [%d]", local_activity->mailbox_id);
16992 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16993 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16994 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16997 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
16998 _bind_stmt_field_data_int(hStmt, i++, local_activity->status);
16999 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
17000 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
17001 _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
17002 _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
17003 _bind_stmt_field_data_string(hStmt, i++, (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
17006 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17008 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
17009 ("sqlite3_step fail:%d", rc));
17010 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17011 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
17016 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17017 if (hStmt != NULL) {
17018 rc = sqlite3_finalize(hStmt);
17020 if (rc != SQLITE_OK) {
17021 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17022 error = EMAIL_ERROR_DB_FAILURE;
17026 if (err_code != NULL)
17029 EM_DEBUG_FUNC_END("ret [%d]", ret);
17034 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)
17036 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);
17038 if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
17039 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);
17041 if (err_code != NULL)
17042 *err_code = EMAIL_ERROR_INVALID_PARAM;
17048 int error = EMAIL_ERROR_NONE;
17049 char sql_query_string[QUERY_SIZE] = {0, };
17050 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17052 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17053 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17055 if (activity_id == 0)
17056 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);
17058 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);
17060 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17061 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17062 if (error != EMAIL_ERROR_NONE) {
17063 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17067 /* validate activity existence */
17068 rc = sqlite3_changes(local_db_handle);
17070 EM_DEBUG_EXCEPTION("No matching activity found");
17071 error = EMAIL_ERROR_DATA_NOT_FOUND;
17079 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17081 if (err_code != NULL)
17084 EM_DEBUG_FUNC_END("ret [%d]", ret);
17088 INTERNAL_FUNC int emstorage_delete_all_auto_download_activity(char *multi_user_name, int account_id, int transaction, int *err_code)
17090 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17092 if (account_id < FIRST_ACCOUNT_ID) {
17093 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17094 if (err_code != NULL)
17095 *err_code = EMAIL_ERROR_INVALID_PARAM;
17101 int error = EMAIL_ERROR_NONE;
17102 char sql_query_string[QUERY_SIZE] = {0, };
17104 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17106 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17107 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17108 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d", account_id);
17110 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17111 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17112 if (error != EMAIL_ERROR_NONE) {
17113 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17117 rc = sqlite3_changes(local_db_handle);
17119 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17120 error = EMAIL_ERROR_DATA_NOT_FOUND;
17128 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17130 if (err_code != NULL)
17133 EM_DEBUG_FUNC_END("ret [%d]", ret);
17138 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)
17140 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17142 if (account_id < FIRST_ACCOUNT_ID || mailbox_id < 0) {
17143 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17144 if (err_code != NULL)
17145 *err_code = EMAIL_ERROR_INVALID_PARAM;
17151 int error = EMAIL_ERROR_NONE;
17152 char sql_query_string[QUERY_SIZE] = {0, };
17154 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17156 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17157 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17158 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);
17160 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17161 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17162 if (error != EMAIL_ERROR_NONE) {
17163 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17167 rc = sqlite3_changes(local_db_handle);
17169 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17170 error = EMAIL_ERROR_DATA_NOT_FOUND;
17178 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17180 if (err_code != NULL)
17183 EM_DEBUG_FUNC_END("ret [%d]", ret);
17188 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)
17190 EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
17192 if (account_id < FIRST_ACCOUNT_ID || !event_start || input_mailbox_id <= 0 || !count) {
17193 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);
17195 if (err_code != NULL)
17196 *err_code = EMAIL_ERROR_INVALID_PARAM;
17203 int error = EMAIL_ERROR_NONE;
17205 DB_STMT hStmt = NULL;
17206 email_event_auto_download *event_list = NULL;
17207 char sql_query_string[QUERY_SIZE] = {0, };
17209 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17211 EMSTORAGE_START_READ_TRANSACTION(transaction);
17213 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17214 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);
17217 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17218 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17219 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17221 *count = atoi(result[1]);
17222 sqlite3_free_table(result);
17224 EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
17227 EM_DEBUG_LOG("No matched activity found in mail_auto_download_activity_tbl");
17228 error = EMAIL_ERROR_MAIL_NOT_FOUND;
17232 EM_DEBUG_LOG("Activity Count = %d", *count);
17234 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17235 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);
17237 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17240 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17242 EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
17243 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17244 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17247 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17248 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17249 ("sqlite3_step fail:%d", rc));
17251 if (!(event_list = (email_event_auto_download *)em_malloc(sizeof(email_event_auto_download)*(*count)))) {
17252 EM_DEBUG_EXCEPTION("Malloc failed");
17254 error = EMAIL_ERROR_OUT_OF_MEMORY;
17258 for (i = 0; i < (*count); i++) {
17259 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17260 _get_stmt_field_data_int(hStmt, &(event_list[i].status), STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17261 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17262 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17263 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17264 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17266 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17267 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17268 ("sqlite3_step fail:%d", rc));
17276 *event_start = event_list;
17278 EM_SAFE_FREE(event_list);
17279 *event_start = NULL;
17283 if (hStmt != NULL) {
17284 rc = sqlite3_finalize(hStmt);
17286 if (rc != SQLITE_OK) {
17287 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17288 error = EMAIL_ERROR_DB_FAILURE;
17292 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17293 if (err_code != NULL)
17296 EM_DEBUG_FUNC_END("ret [%d]", ret);
17301 INTERNAL_FUNC int emstorage_get_auto_download_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
17303 EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
17305 if (!activity_count || !err_code) {
17306 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
17307 if (err_code != NULL)
17308 *err_code = EMAIL_ERROR_INVALID_PARAM;
17314 int error = EMAIL_ERROR_NONE;
17315 DB_STMT hStmt = NULL;
17316 char sql_query_string[QUERY_SIZE] = {0, };
17318 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17320 EMSTORAGE_START_READ_TRANSACTION(transaction);
17321 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17323 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl;");
17325 EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
17328 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17329 EM_DEBUG_LOG_DEV("before sqlite3_prepare hStmt = %p", hStmt);
17330 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17331 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17334 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17335 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17336 ("sqlite3_step fail:%d", rc));
17338 _get_stmt_field_data_int(hStmt, activity_count, 0);
17340 EM_DEBUG_LOG("counts of activities in activity table [%d]", *activity_count);
17346 if (hStmt != NULL) {
17347 rc = sqlite3_finalize(hStmt);
17349 if (rc != SQLITE_OK) {
17350 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17351 error = EMAIL_ERROR_DB_FAILURE;
17355 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17356 if (err_code != NULL)
17359 EM_DEBUG_FUNC_END("ret [%d]", ret);
17364 INTERNAL_FUNC int emstorage_get_auto_download_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
17366 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
17368 if (!account_list || !count) {
17369 EM_DEBUG_EXCEPTION("account_list[%p], count[%p]", account_list, count);
17370 if (err_code != NULL)
17371 *err_code = EMAIL_ERROR_INVALID_PARAM;
17376 int error = EMAIL_ERROR_NONE;
17377 char *sql = "SELECT count(distinct account_id) FROM mail_auto_download_activity_tbl";
17379 int i = 0, rc = -1;
17380 int *result_account_list = NULL;
17381 DB_STMT hStmt = NULL;
17382 char sql_query_string[QUERY_SIZE] = {0, };
17383 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17385 EMSTORAGE_START_READ_TRANSACTION(transaction);
17387 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
17388 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17389 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
17391 *count = atoi(result[1]);
17392 sqlite3_free_table(result);
17395 EM_DEBUG_EXCEPTION("no account found...");
17396 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
17401 EM_DEBUG_LOG("Account count [%d]", *count);
17403 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17405 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_auto_download_activity_tbl");
17407 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17410 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17412 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
17413 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17414 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17417 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17418 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17419 ("sqlite3_step fail:%d", rc));
17421 if (NULL == (result_account_list = (int *)em_malloc(sizeof(int)*(*count)))) {
17422 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
17423 error = EMAIL_ERROR_OUT_OF_MEMORY;
17427 for (i = 0; i < (*count); i++) {
17428 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
17430 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17431 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17432 ("sqlite3_step fail:%d", rc));
17433 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
17441 *account_list = result_account_list;
17443 EM_SAFE_FREE(result_account_list);
17445 if (hStmt != NULL) {
17446 rc = sqlite3_finalize(hStmt);
17448 if (rc != SQLITE_OK) {
17449 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17450 error = EMAIL_ERROR_DB_FAILURE;
17454 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17455 if (err_code != NULL)
17457 EM_DEBUG_FUNC_END("ret [%d]", ret);
17462 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)
17464 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
17466 if (account_id < FIRST_ACCOUNT_ID || !mailbox_list || !count) {
17467 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p]", account_id, mailbox_list, count);
17468 if (err_code != NULL)
17469 *err_code = EMAIL_ERROR_INVALID_PARAM;
17474 int error = EMAIL_ERROR_NONE;
17476 int i = 0, rc = -1;
17477 int *mbox_list = NULL;
17478 DB_STMT hStmt = NULL;
17479 char sql_query_string[QUERY_SIZE] = {0, };
17481 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17483 EMSTORAGE_START_READ_TRANSACTION(transaction);
17485 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17486 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);
17489 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17490 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17491 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17493 *count = atoi(result[1]);
17494 sqlite3_free_table(result);
17497 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
17498 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
17502 EM_DEBUG_LOG("Mailbox count = %d", *count);
17504 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17506 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);
17508 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
17511 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17514 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17515 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17518 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17519 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17520 ("sqlite3_step fail:%d", rc));
17522 mbox_list = (int *)em_malloc(sizeof(int)*(*count)); /* prevent */
17523 if (mbox_list == NULL) {
17524 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
17525 error = EMAIL_ERROR_OUT_OF_MEMORY;
17529 for (i = 0; i < (*count); i++) {
17530 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
17531 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17533 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17534 ("sqlite3_step fail:%d", rc));
17535 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
17543 *mailbox_list = mbox_list;
17545 EM_SAFE_FREE(mbox_list);
17547 if (hStmt != NULL) {
17548 rc = sqlite3_finalize(hStmt);
17550 if (rc != SQLITE_OK) {
17551 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17552 error = EMAIL_ERROR_DB_FAILURE;
17556 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17557 if (err_code != NULL)
17559 EM_DEBUG_FUNC_END("ret [%d]", ret);
17564 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)
17566 EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
17568 if (account_id < FIRST_ACCOUNT_ID || !activity_count || !err_code) {
17569 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
17570 if (err_code != NULL)
17571 *err_code = EMAIL_ERROR_INVALID_PARAM;
17577 int error = EMAIL_ERROR_NONE;
17578 char sql_query_string[QUERY_SIZE] = {0, };
17579 DB_STMT hStmt = NULL;
17581 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17583 EMSTORAGE_START_READ_TRANSACTION(transaction);
17584 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17585 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);
17587 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
17589 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17590 EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
17591 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17592 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17594 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17595 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17596 ("sqlite3_step fail:%d", rc));
17598 _get_stmt_field_data_int(hStmt, activity_count, 0);
17600 EM_DEBUG_LOG("count of activities in activity table [%d]", *activity_count);
17606 if (hStmt != NULL) {
17607 rc = sqlite3_finalize(hStmt);
17609 if (rc != SQLITE_OK) {
17610 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17611 error = EMAIL_ERROR_DB_FAILURE;
17615 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17617 if (err_code != NULL)
17620 EM_DEBUG_FUNC_END("ret [%d]", ret);
17625 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)
17627 EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mailbox_id[%d]", old_server_uid, new_server_uid, mailbox_id);
17629 int rc = -1, ret = false;
17630 int error = EMAIL_ERROR_NONE;
17631 char sql_query_string[QUERY_SIZE] = {0, };
17632 int transaction = true;
17634 if (!old_server_uid || !new_server_uid || (!mailbox_name && mailbox_id < 0)) {
17635 EM_DEBUG_EXCEPTION("Invalid parameters");
17636 error = EMAIL_ERROR_INVALID_PARAM;
17640 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17642 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17643 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17645 if (mailbox_id > 0)
17646 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17647 "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);
17648 else if (mailbox_name)
17649 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17650 "UPDATE mail_auto_download_activity_tbl SET server_mail_id = %s WHERE server_mail_id = %s ", new_server_uid, old_server_uid);
17652 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17653 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17654 if (error != EMAIL_ERROR_NONE) {
17655 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17659 rc = sqlite3_changes(local_db_handle);
17661 EM_DEBUG_LOG("No matching found in mail_auto_download_activity_tbl");
17667 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17669 if (err_code != NULL)
17672 EM_DEBUG_FUNC_END("ret [%d]", ret);
17678 #ifdef __FEATURE_UPDATE_DB_TABLE_SCHEMA__
17683 } email_column_info_t;
17685 static int get_column_information_from_table_callback(void *arg1, int argc, char **argv, char **input_column_name)
17687 EM_DEBUG_FUNC_BEGIN("arg1[%p] argc[%d] argv[%p] column_name[%p]", arg1, argc, argv, input_column_name);
17691 char *column_name = NULL;
17692 char *column_type = NULL;
17693 GList *new_list = *((GList**)arg1);
17694 email_column_info_t *column_info_item = NULL;
17696 for (i = 0; i < argc; ++i) {
17697 /* EM_DEBUG_LOG("%s = %s", input_column_name[i], argv[i]); */
17698 if (EM_SAFE_STRCMP(input_column_name[i], "name") == 0) {
17700 EM_SAFE_FREE(column_name);
17702 column_name = EM_SAFE_STRDUP(argv[i]);
17704 } else if (EM_SAFE_STRCMP(input_column_name[i], "type") == 0) {
17706 EM_SAFE_FREE(column_type);
17708 column_type = EM_SAFE_STRDUP(argv[i]);
17713 EM_DEBUG_LOG("column_name[%s] column_type[%s]", column_name, column_type);
17714 column_info_item = em_malloc(sizeof(email_column_info_t));
17715 if (column_info_item == NULL) {
17716 EM_DEBUG_EXCEPTION("em_mallocfailed");
17720 column_info_item->column_name = EM_SAFE_STRDUP(column_name);
17721 column_info_item->column_type = EM_SAFE_STRDUP(column_type);
17722 new_list = g_list_append(new_list, (gpointer)column_info_item);
17723 *((GList**)arg1) = new_list;
17728 EM_SAFE_FREE(column_name);
17729 EM_SAFE_FREE(column_type);
17731 EM_DEBUG_FUNC_END();
17735 static int emstorage_get_column_information_from_table(char *multi_user_name, const char *input_table_name, GList **output_column_info)
17737 EM_DEBUG_FUNC_BEGIN("input_table_name[%p] output_column_info[%p]", input_table_name, output_column_info);
17738 int err = EMAIL_ERROR_NONE;
17739 int result_from_sqlite = 0;
17740 char *error_message_from_sqlite = NULL;
17741 char sql_query_string[QUERY_SIZE] = {0, };
17742 GList *new_list = NULL;
17743 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17745 SNPRINTF(sql_query_string, QUERY_SIZE, "pragma table_info(%s);", input_table_name);
17747 result_from_sqlite = sqlite3_exec(local_db_handle, sql_query_string, get_column_information_from_table_callback, &new_list, &error_message_from_sqlite);
17749 if (result_from_sqlite != SQLITE_OK)
17750 EM_DEBUG_EXCEPTION("sqlite3_exec returns [%d]", result_from_sqlite);
17752 EM_DEBUG_LOG("new_list[%p] output_column_info[%p]", new_list, output_column_info);
17754 if (new_list && output_column_info) {
17755 EM_DEBUG_LOG("g_list_length[%d]", g_list_length(new_list));
17756 *output_column_info = new_list;
17759 EM_DEBUG_FUNC_END("err [%d]", err);
17763 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)
17765 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);
17766 int error = EMAIL_ERROR_NONE;
17768 sqlite3 *local_db_handle = NULL;
17769 char sql_query_string[QUERY_SIZE] = {0, };
17771 if (input_full_query == NULL || input_source_table_name == NULL || input_new_table_name == NULL) {
17772 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17773 error = EMAIL_ERROR_INVALID_PARAM;
17777 local_db_handle = emstorage_get_db_connection(multi_user_name);
17779 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17780 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17782 EM_DEBUG_LOG("[%s] will be replaced by [%s]", input_source_table_name, input_new_table_name);
17784 EM_SAFE_STRNCPY(sql_query_string, input_full_query[input_query_index], sizeof(sql_query_string)-1); /*prevent 21984*/
17785 reg_replace(sql_query_string, input_source_table_name, input_new_table_name);
17787 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17790 if (error == EMAIL_ERROR_NONE) {
17791 EMSTORAGE_PROTECTED_FUNC_CALL(
17792 sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17794 EMSTORAGE_PROTECTED_FUNC_CALL(
17795 sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17798 EM_DEBUG_FUNC_END("error [%d]", error);
17802 static int emstorage_add_column(char *multi_user_name, char *input_table_name, email_column_info_t *input_new_column)
17804 EM_DEBUG_FUNC_BEGIN("input_table_name[%p] input_new_column[%p]", input_table_name, input_new_column);
17805 int error = EMAIL_ERROR_NONE;
17807 sqlite3 *local_db_handle = NULL;
17808 char sql_query_string[QUERY_SIZE] = {0, };
17810 if (input_table_name == NULL || input_new_column == NULL) {
17811 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17812 error = EMAIL_ERROR_INVALID_PARAM;
17816 local_db_handle = emstorage_get_db_connection(multi_user_name);
17818 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17819 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17820 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);
17821 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17824 if (error == EMAIL_ERROR_NONE) {
17825 EMSTORAGE_PROTECTED_FUNC_CALL(
17826 sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17828 EMSTORAGE_PROTECTED_FUNC_CALL(
17829 sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17832 EM_DEBUG_FUNC_END("error [%d]", error);
17836 static int emstorage_drop_table(char *multi_user_name, char *input_table_name)
17838 EM_DEBUG_FUNC_BEGIN("input_table_name[%p]", input_table_name);
17839 int error = EMAIL_ERROR_NONE;
17841 sqlite3 *local_db_handle = NULL;
17842 char sql_query_string[QUERY_SIZE] = {0, };
17844 if (input_table_name == NULL) {
17845 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17846 error = EMAIL_ERROR_INVALID_PARAM;
17850 local_db_handle = emstorage_get_db_connection(multi_user_name);
17852 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17853 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17854 SNPRINTF(sql_query_string, QUERY_SIZE, "DROP TABLE %s;", input_table_name);
17855 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17858 if (error == EMAIL_ERROR_NONE) {
17859 EMSTORAGE_PROTECTED_FUNC_CALL(
17860 sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17862 EMSTORAGE_PROTECTED_FUNC_CALL(
17863 sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17866 EM_DEBUG_FUNC_END("error [%d]", error);
17870 gint glist_compare_column_name(gconstpointer old_column_info, gconstpointer new_column_info)
17872 EM_DEBUG_FUNC_BEGIN("old_column_info[%p] new_column_info[%p]", old_column_info, new_column_info);
17873 email_column_info_t *left_one = (email_column_info_t*)old_column_info;
17874 email_column_info_t *right_one = (email_column_info_t*)new_column_info;
17876 if (old_column_info == NULL || new_column_info == NULL)
17879 return EM_SAFE_STRCMP((char*)left_one->column_name, (char*)right_one->column_name);
17882 INTERNAL_FUNC int emstorage_update_db_table_schema(char *multi_user_name)
17884 EM_DEBUG_FUNC_BEGIN();
17887 int error = EMAIL_ERROR_NONE;
17889 email_column_info_t *new_column_info = NULL;
17890 email_column_info_t *p_column_info = NULL;
17891 char **create_table_query = NULL;
17892 GList *found_data = NULL;
17893 GList *column_list_of_old_table = NULL;
17894 GList *column_list_of_new_table = NULL;
17895 char table_names[CREATE_TABLE_MAX][2][50] = { { "mail_account_tbl", "mail_account_tbl_new" },
17896 { "mail_box_tbl", "mail_box_tbl_new" },
17897 { "mail_read_mail_uid_tbl", "mail_read_mail_uid_tbl_new" },
17898 { "mail_rule_tbl", "mail_rule_tbl_new" },
17899 { "mail_tbl", "mail_tbl_new" },
17900 { "mail_attachment_tbl", "mail_attachment_tbl_new" },
17901 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
17902 { "mail_partial_body_activity_tbl", "mail_partial_body_activity_tbl_new" },
17906 { "mail_meeting_tbl", "mail_meeting_tbl_new" },
17907 #ifdef __FEATURE_LOCAL_ACTIVITY__
17908 { "mail_local_activity_tbl", "mail_local_activity_tbl_new" },
17912 { "mail_certificate_tbl", "mail_certificate_tbl_new" },
17913 { "mail_task_tbl", "mail_task_tbl_new" },
17914 #ifdef __FEATURE_BODY_SEARCH__
17915 { "mail_text_tbl", "mail_text_tbl_new" },
17920 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
17921 { "mail_auto_download_activity_tbl", "mail_auto_download_activity_tbl_new" }
17928 error = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
17930 if (error != EMAIL_ERROR_NONE) {
17931 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
17935 if (query_len < CREATE_TABLE_MAX) {
17936 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
17937 error = EMAIL_ERROR_SYSTEM_FAILURE;
17941 for (i = CREATE_TABLE_MAIL_ACCOUNT_TBL; i < CREATE_TABLE_MAX; i++) {
17942 EM_DEBUG_LOG("table [%s] new_table [%s]", table_names[i][0], table_names[i][1]);
17943 if (EM_SAFE_STRLEN(table_names[i][0]) && EM_SAFE_STRLEN(table_names[i][1])) {
17944 /* Check existing of _new table */
17945 emstorage_drop_table(multi_user_name, table_names[i][1]);
17946 error = emstorage_create_renamed_table(multi_user_name, create_table_query, i, table_names[i][0], table_names[i][1]);
17947 if (error != EMAIL_ERROR_NONE) {
17948 EM_DEBUG_EXCEPTION("emstorage_create_renamed_table failed [%d]", error);
17952 emstorage_get_column_information_from_table(multi_user_name, table_names[i][0], &column_list_of_old_table);
17953 emstorage_get_column_information_from_table(multi_user_name, table_names[i][1], &column_list_of_new_table);
17955 /* Compare fields and add new field */
17956 for (j = 0; j < g_list_length(column_list_of_new_table); j++) {
17957 new_column_info = (email_column_info_t*)g_list_nth_data(column_list_of_new_table, j);
17958 found_data = g_list_find_custom(column_list_of_old_table, (gconstpointer)new_column_info, glist_compare_column_name);
17959 if (found_data == NULL) {
17961 emstorage_add_column(multi_user_name, table_names[i][0], new_column_info);
17965 emstorage_drop_table(multi_user_name, table_names[i][1]);
17967 EM_DEBUG_LOG("Skipped");
17972 if (create_table_query) {
17974 for (i = 0; i < query_len; i++) {
17975 if (create_table_query[i])
17976 EM_SAFE_FREE(create_table_query[i]);
17978 EM_SAFE_FREE(create_table_query);
17981 found_data = g_list_first(column_list_of_old_table);
17982 while (found_data != NULL) {
17983 p_column_info = (email_column_info_t *)found_data->data;
17984 EM_SAFE_FREE(p_column_info->column_name);
17985 EM_SAFE_FREE(p_column_info->column_type);
17986 EM_SAFE_FREE(p_column_info);
17988 found_data = g_list_next(found_data);
17990 g_list_free(column_list_of_old_table);
17992 found_data = g_list_first(column_list_of_new_table);
17993 while (found_data != NULL) {
17994 p_column_info = (email_column_info_t *)found_data->data;
17995 EM_SAFE_FREE(p_column_info->column_name);
17996 EM_SAFE_FREE(p_column_info->column_type);
17997 EM_SAFE_FREE(p_column_info);
17999 found_data = g_list_next(found_data);
18001 g_list_free(column_list_of_new_table);
18003 EM_DEBUG_FUNC_END("error [%d]", error);
18006 #endif /* __FEATURE_UPDATE_DB_TABLE_SCHEMA__ */