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>
55 #include "email-internal-types.h"
56 #include "email-convert.h"
57 #include "email-core-utils.h"
58 #include "email-utilities.h"
59 #include "email-storage.h"
60 #include "email-debug-log.h"
61 #include "email-types.h"
62 #include "email-convert.h"
63 #include "email-core-signal.h"
64 #include "email-core-event.h"
65 #include "email-core-container.h"
66 #include "email-core-gmime.h"
67 #include "email-core-key-manager.h"
69 #define SETTING_MEMORY_TEMP_FILE_PATH "/tmp/email_tmp_file"
70 #define CONTENT_DATA "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset="
71 #define CONTENT_TYPE_DATA "<meta http-equiv=\"Content-Type\" content=\"text/html; charset="
73 #define FLAG_SEEN 0x01
74 #define FLAG_DELETED 0x02
75 #define FLAG_FLAGGED 0x04
76 #define FLAG_ANSWERED 0x08
78 #define FLAG_DRAFT 0x20
82 #define ISSUE_ORGANIZATION_LEN_IN_MAIL_CERTIFICATE_TBL 256
83 #define EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL 128
84 #define SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL 1027
85 #define FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL 256
87 #define ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL 50
88 #define RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 50
89 #define EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 128
90 #define USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL 50
91 #define PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL 50
92 #define SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 50
93 #define SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL 50
94 #define SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL 50
95 #define DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL 30
96 #define REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 128
97 #define RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 128
98 #define LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL 256
99 #define DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL 256
100 #define SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL 256
101 #define MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL 128
102 #define ALIAS_LEN_IN_MAIL_BOX_TBL 128
103 #define LOCAL_MBOX_LEN_IN_MAIL_READ_MAIL_UID_TBL 128
104 #define MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL 128
105 #define S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL 128
106 #define DATA2_LEN_IN_MAIL_READ_MAIL_UID_TBL 256
107 #define FILTER_NAME_LEN_IN_MAIL_RULE_TBL 256
108 #define VALUE_LEN_IN_MAIL_RULE_TBL 256
109 #define VALUE2_LEN_IN_MAIL_RULE_TBL 256
110 #define DEST_MAILBOX_LEN_IN_MAIL_RULE_TBL 128
111 #define MAILBOX_NAME_LEN_IN_MAIL_ATTACHMENT_TBL 128
112 #define ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL 256
113 #define ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL 256
114 #define CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL 256
115 #define ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL 128
116 #define MAILBOX_LEN_IN_MAIL_TBL 128
117 #define SERVER_MAILBOX_LEN_IN_MAIL_TBL 128
118 #define SERVER_MAIL_ID_LEN_IN_MAIL_TBL 128
119 #define FROM_LEN_IN_MAIL_TBL 256
120 #define SENDER_LEN_IN_MAIL_TBL 256
121 #define REPLY_TO_LEN_IN_MAIL_TBL 256
122 #define TO_LEN_IN_MAIL_TBL 3999
123 #define CC_LEN_IN_MAIL_TBL 3999
124 #define BCC_LEN_IN_MAIL_TBL 3999
125 #define RETURN_PATH_LEN_IN_MAIL_TBL 3999
126 #define SUBJECT_LEN_IN_MAIL_TBL 1027
127 #define THREAD_TOPIC_LEN_IN_MAIL_TBL 256
128 #define TEXT_1_LEN_IN_MAIL_TBL 256
129 #define TEXT_2_LEN_IN_MAIL_TBL 256
130 #define MIME_ENTITY_LEN_IN_MAIL_TBL 256
131 #define DATETIME_LEN_IN_MAIL_TBL 128
132 #define MESSAGE_ID_LEN_IN_MAIL_TBL 256
133 #define FROM_CONTACT_NAME_LEN_IN_MAIL_TBL 3999
134 #define FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL 3999
135 #define TO_CONTACT_NAME_LEN_IN_MAIL_TBL 3999
136 #define TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL 3999
137 #define MAILBOX_LEN_IN_MAIL_MEETING_TBL 128
138 #define LOCATION_LEN_IN_MAIL_MEETING_TBL 1024
139 #define GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL 512
140 #define STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL 32
141 #define DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL 32
142 #define PREVIEWBODY_LEN_IN_MAIL_TBL 512
143 #define CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL 256
145 /* this define is used for query to change data (delete, insert, update) */
146 #define EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction_flag, error_code) \
148 if (transaction_flag) { \
149 if (emstorage_begin_transaction(multi_user_name, NULL, NULL, &error_code) == false) { \
150 EM_DEBUG_EXCEPTION("emstorage_begin_transaction error [%d]", error_code); \
156 /* this define is used for query to change data (delete, insert, update) */
157 #define EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction_flag, result_code, error_code) \
159 if (transaction_flag) { \
160 if (result_code == true) { \
161 if (emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL) == false) { \
162 EM_DEBUG_EXCEPTION("emstorage_commit_transaction error"); \
163 error_code = EMAIL_ERROR_DB_FAILURE; \
164 result_code = false; \
168 if (emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL) == false) { \
169 EM_DEBUG_EXCEPTION("emstorage_rollback_transaction error"); \
170 error_code = EMAIL_ERROR_DB_FAILURE; \
176 /* this define is used for query to read (select) */
177 #define EMSTORAGE_START_READ_TRANSACTION(transaction_flag) \
178 if (transaction_flag) { \
179 /*_timedlock_shm_mutex(mapped_for_db_lock, 2);*/\
182 /* this define is used for query to read (select) */
183 #define EMSTORAGE_FINISH_READ_TRANSACTION(transaction_flag) \
184 if (transaction_flag) { \
185 /*_unlockshm_mutex(mapped_for_db_lock);*/\
188 /* for safety DB operation */
189 static pthread_mutex_t _db_handle_lock = PTHREAD_MUTEX_INITIALIZER;
191 /* for safety secure-storage operation */
192 static pthread_mutex_t _ss_handle_lock = PTHREAD_MUTEX_INITIALIZER;
194 #define _MULTIPLE_DB_HANDLE
196 #ifdef _MULTIPLE_DB_HANDLE
204 #define MAX_DB_CLIENT 100
206 /* static int _db_handle_count = 0; */
207 db_handle_t _db_handle_list[MAX_DB_CLIENT] = {{NULL, 0, 0}, };
210 sqlite3 *emstorage_get_db_handle(char *multi_user_name)
212 EM_DEBUG_FUNC_BEGIN();
214 pthread_t current_thread_id = THREAD_SELF();
215 sqlite3 *result_db_handle = NULL;
217 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
218 ENTER_CRITICAL_SECTION(_db_handle_lock);
219 for (i = 0; i < MAX_DB_CLIENT; i++) {
220 if (EM_SAFE_STRCASECMP(_db_handle_list[i].user_name, multi_user_name) != 0)
223 if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id)) {
224 EM_DEBUG_LOG_DEV("found db handle at [%d]", i);
225 result_db_handle = _db_handle_list[i].db_handle;
229 LEAVE_CRITICAL_SECTION(_db_handle_lock);
231 ENTER_CRITICAL_SECTION(_db_handle_lock);
232 for (i = 0; i < MAX_DB_CLIENT; i++) {
233 if (EM_SAFE_STRLEN(_db_handle_list[i].user_name) > 0)
236 if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id)) {
237 EM_DEBUG_LOG_DEV("found db handle at [%d]", i);
238 result_db_handle = _db_handle_list[i].db_handle;
242 LEAVE_CRITICAL_SECTION(_db_handle_lock);
245 if (!result_db_handle)
246 EM_DEBUG_LOG("no db_handle for [%d] found", current_thread_id);
249 return result_db_handle;
252 int emstorage_set_db_handle(char *multi_user_name, sqlite3 *db_handle)
254 EM_DEBUG_FUNC_BEGIN();
255 int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
256 pthread_t current_thread_id = THREAD_SELF();
258 ENTER_CRITICAL_SECTION(_db_handle_lock);
259 for (i = 0; i < MAX_DB_CLIENT; i++) {
260 if (_db_handle_list[i].thread_id == 0) {
261 _db_handle_list[i].thread_id = current_thread_id;
262 _db_handle_list[i].db_handle = db_handle;
263 /* Only distinguished container and host */
264 _db_handle_list[i].user_name = EM_SAFE_STRDUP(multi_user_name);
265 EM_DEBUG_LOG("current_thread_id [%d], index [%d]", current_thread_id, i);
266 error_code = EMAIL_ERROR_NONE;
270 LEAVE_CRITICAL_SECTION(_db_handle_lock);
272 if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
273 EM_DEBUG_EXCEPTION("Exceeded the limitation of db client. Can't find empty slot in _db_handle_list.");
275 EM_DEBUG_FUNC_END("error_code [%d]", error_code);
279 int emstorage_remove_db_handle()
281 EM_DEBUG_FUNC_BEGIN();
282 int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
283 ENTER_CRITICAL_SECTION(_db_handle_lock);
284 for (i = 0; i < MAX_DB_CLIENT; i++) {
285 if (_db_handle_list[i].thread_id == THREAD_SELF()) {
286 _db_handle_list[i].thread_id = 0;
287 _db_handle_list[i].db_handle = NULL;
288 EM_SAFE_FREE(_db_handle_list[i].user_name);
290 EM_DEBUG_LOG("index [%d]", i);
291 error_code = EMAIL_ERROR_NONE;
295 LEAVE_CRITICAL_SECTION(_db_handle_lock);
297 if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
298 EM_DEBUG_EXCEPTION("Can't find proper thread_id");
300 EM_DEBUG_FUNC_END("error_code [%d]", error_code);
304 int emstorage_reset_db_handle_list()
306 EM_DEBUG_FUNC_BEGIN();
309 ENTER_CRITICAL_SECTION(_db_handle_lock);
310 for (i = 0; i < MAX_DB_CLIENT; i++) {
311 _db_handle_list[i].thread_id = 0;
312 _db_handle_list[i].db_handle = NULL;
313 EM_SAFE_FREE(_db_handle_list[i].user_name);
315 LEAVE_CRITICAL_SECTION(_db_handle_lock)
318 return EMAIL_ERROR_NONE;
322 sqlite3 *emstorage_get_db_connection(char *multi_user_name)
324 return emstorage_db_open(multi_user_name, NULL);
327 #else /* _MULTIPLE_DB_HANDLE */
329 sqlite3 *_db_handle = NULL;
331 sqlite3 *emstorage_get_db_connection()
333 if (NULL == _db_handle)
334 emstorage_db_open(NULL);
337 #endif /* _MULTIPLE_DB_HANDLE */
339 /* ------------------------------------------------------------------------------ */
340 /* Mutex using shared memory */
342 pthread_mutex_t mutex;
346 mmapped_t *mapped_for_db_lock = NULL;
347 int shm_fd_for_db_lock = 0;
349 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
350 mmapped_t *mapped_for_generating_mail_id = NULL;
351 int shm_fd_for_generating_mail_id = 0;
352 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
354 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__
355 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
357 _timedlock_shm_mutex(mapped_for_db_lock, 2);\
358 return_value = function_call;\
359 _unlockshm_mutex(mapped_for_db_lock);\
362 #else /* __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
363 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
364 { return_value = function_call; }
365 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
367 static int emstorage_exec_query_by_prepare_v2(sqlite3 *local_db_handle, char *query_string)
369 EM_DEBUG_FUNC_BEGIN("local_db_handle[%p] query_string[%p]", local_db_handle, query_string);
370 int error = EMAIL_ERROR_NONE;
372 DB_STMT db_statement = NULL;
374 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, query_string, EM_SAFE_STRLEN(query_string), &db_statement, NULL), rc);
375 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
376 ("sqlite3_prepare failed [%d] [%s]", rc, query_string));
378 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(db_statement), rc);
379 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
380 ("sqlite3_step failed [%d] [%s]", rc, query_string));
384 if (db_statement != NULL) {
385 rc = sqlite3_finalize(db_statement);
386 if (rc != SQLITE_OK) {
387 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
388 error = EMAIL_ERROR_DB_FAILURE;
395 INTERNAL_FUNC int emstorage_shm_file_init(const char *shm_file_name)
397 EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
398 char errno_buf[ERRNO_BUF_SIZE] = {0};
400 if (!shm_file_name) {
401 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
402 return EMAIL_ERROR_INVALID_PARAM;
405 int fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); /* note: permission is not working */
407 EM_DEBUG_EXCEPTION("shm_open errno [%d] [%s]", errno, EM_STRERROR(errno_buf));
408 return EMAIL_ERROR_SYSTEM_FAILURE;
412 EM_DEBUG_LOG("** Create SHM FILE **");
413 if (ftruncate(fd, sizeof(mmapped_t)) != 0) {
414 EM_DEBUG_EXCEPTION("ftruncate errno [%d]", errno);
415 return EMAIL_ERROR_SYSTEM_FAILURE;
418 mmapped_t *m = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
419 if (m == MAP_FAILED) {
420 EM_DEBUG_EXCEPTION("mmap errno [%d]", errno);
422 return EMAIL_ERROR_SYSTEM_FAILURE;
427 pthread_mutexattr_t mattr;
428 pthread_mutexattr_init(&mattr);
429 pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
430 pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
431 pthread_mutex_init(&(m->mutex), &mattr);
432 pthread_mutexattr_destroy(&mattr);
434 pthread_mutex_destroy(&(m->mutex));
435 munmap(m, sizeof(mmapped_t));
439 return EMAIL_ERROR_NONE;
442 int emstorage_shm_file_destroy(const char *shm_file_name)
444 EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
445 char errno_buf[ERRNO_BUF_SIZE] = {0};
447 if (!shm_file_name) {
448 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
449 return EMAIL_ERROR_INVALID_PARAM;
452 if (shm_unlink(shm_file_name) != 0)
453 EM_DEBUG_EXCEPTION("shm_unlink failed: %s", EM_STRERROR(errno_buf));
455 return EMAIL_ERROR_NONE;
458 int _initialize_shm_mutex(const char *shm_file_name, int *param_shm_fd, mmapped_t **param_mapped)
460 EM_DEBUG_FUNC_BEGIN("shm_file_name [%p] param_shm_fd [%p], param_mapped [%p]", shm_file_name, param_shm_fd, param_mapped);
461 char errno_buf[ERRNO_BUF_SIZE] = {0};
463 if (!shm_file_name || !param_shm_fd || !param_mapped) {
464 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
465 return EMAIL_ERROR_INVALID_PARAM;
468 if (!(*param_mapped)) {
469 EM_DEBUG_LOG("** mapping begin **");
470 if (!(*param_shm_fd)) { /* open shm_file_name at first. Otherwise, the num of files in /proc/pid/fd will be increasing */
471 *param_shm_fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
472 if ((*param_shm_fd) == -1) {
473 EM_DEBUG_EXCEPTION("FAIL: shm_open(): %s", EM_STRERROR(errno_buf));
474 return EMAIL_ERROR_SYSTEM_FAILURE;
478 fchmod((*param_shm_fd), 0666);
479 EM_DEBUG_LOG("** Create SHM FILE **");
480 if (ftruncate((*param_shm_fd), sizeof(mmapped_t)) != 0) {
481 EM_DEBUG_EXCEPTION("ftruncate errno [%d]", errno);
482 return EMAIL_ERROR_SYSTEM_FAILURE;
485 mmapped_t *tmp = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ|PROT_WRITE, MAP_SHARED, (*param_shm_fd), 0);
486 if (tmp == MAP_FAILED) {
487 EM_DEBUG_EXCEPTION("mmap failed: %s", EM_STRERROR(errno_buf));
488 return EMAIL_ERROR_SYSTEM_FAILURE;
493 pthread_mutexattr_t mattr;
494 pthread_mutexattr_init(&mattr);
495 pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
496 pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
497 pthread_mutex_init(&(tmp->mutex), &mattr);
498 pthread_mutexattr_destroy(&mattr);
504 return EMAIL_ERROR_NONE;
507 int _timedlock_shm_mutex(mmapped_t *param_mapped, int sec)
509 EM_DEBUG_FUNC_BEGIN("param_mapped [%p], sec [%d]", param_mapped, sec);
512 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
513 return EMAIL_ERROR_INVALID_PARAM;
516 struct timespec abs_time;
517 clock_gettime(CLOCK_REALTIME, &abs_time);
518 abs_time.tv_sec += sec;
519 char errno_buf[ERRNO_BUF_SIZE] = {0};
521 int err = pthread_mutex_timedlock(&(param_mapped->mutex), &abs_time);
523 if (err == EOWNERDEAD) {
524 err = pthread_mutex_consistent(&(param_mapped->mutex));
525 EM_DEBUG_EXCEPTION("Previous owner is dead with lock. Fix mutex : %s", EM_STRERROR(errno_buf));
526 } else if (err != 0) {
527 EM_DEBUG_EXCEPTION("ERROR : %s", EM_STRERROR(errno_buf));
532 return EMAIL_ERROR_NONE;
535 void _unlockshm_mutex(mmapped_t *param_mapped)
537 EM_DEBUG_FUNC_BEGIN();
538 pthread_mutex_unlock(&(param_mapped->mutex));
541 /* ------------------------------------------------------------------------------ */
544 static int _open_counter = 0;
546 static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name);
547 static int _read_password_from_secure_storage(char *file_name, char **password);
549 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
550 static int _get_cert_password_file_name(int index, char *cert_password_file_name);
554 const char *object_name;
555 unsigned int data_flag;
558 static const email_db_object_t _g_db_tables[] = {
559 { "mail_read_mail_uid_tbl", 1},
561 { "mail_attachment_tbl", 1},
565 static const email_db_object_t _g_db_indexes[] = {
566 { "mail_read_mail_uid_idx1", 1},
568 { "mail_attachment_idx1", 1},
573 CREATE_TABLE_MAIL_ACCOUNT_TBL,
574 CREATE_TABLE_MAIL_BOX_TBL,
575 CREATE_TABLE_MAIL_READ_MAIL_UID_TBL,
576 CREATE_TABLE_MAIL_RULE_TBL,
577 CREATE_TABLE_MAIL_TBL,
578 CREATE_TABLE_MAIL_ATTACHMENT_TBL,
579 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
580 CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL,
582 CREATE_TABLE_DUMMY_INDEX1,
584 CREATE_TABLE_MAIL_MEETING_TBL,
585 #ifdef __FEATURE_LOCAL_ACTIVITY__
586 CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL,
588 CREATE_TABLE_DUMMY_INDEX2,
590 CREATE_TABLE_MAIL_CERTIFICATE_TBL,
591 CREATE_TABLE_MAIL_TASK_TBL,
592 #ifdef __FEATURE_BODY_SEARCH__
593 CREATE_TABLE_MAIL_TEXT_TBL,
595 CREATE_TABLE_DUMMY_INDEX3,
598 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
599 CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
601 CREATE_TABLE_DUMMY_INDEX4,
605 CREATE_TABLE_MAIL_ACCOUNT_IDX,
606 CREATE_TABLE_MAIL_BOX_IDX,
607 CREATE_TABLE_MAIL_READ_MAIL_UID_IDX,
608 CREATE_TABLE_MAIL_IDX,
609 CREATE_TABLE_MAIL_ATTACHMENT_IDX,
610 CREATE_TABLE_MAIL_MEETING_IDX,
611 CREATE_TABLE_MAIL_TASK_IDX,
612 CREATE_TABLE_MAIL_DATETIME_IDX,
613 CREATE_TABLE_MAIL_THREAD_IDX,
618 DATA1_IDX_IN_MAIL_ACTIVITY_TBL = 0,
619 TRANSTYPE_IDX_IN_MAIL_ACTIVITY_TBL,
620 FLAG_IDX_IN_MAIL_ACTIVITY_TBL,
624 CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL = 0,
625 ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
626 ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
627 ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
628 EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
629 EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
630 EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
631 ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL,
632 EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL,
633 SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL,
634 FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL,
643 ACCOUNT_ID_IDX_IN_MAIL_BOX_TBL = 0,
644 LOCAL_YN_IDX_IN_MAIL_BOX_TBL,
645 MAILBOX_NAME_IDX_IN_MAIL_BOX_TBL,
646 MAILBOX_TYPE_IDX_IN_MAIL_BOX_TBL,
647 ALIAS_IDX_IN_MAIL_BOX_TBL,
648 SYNC_WITH_SERVER_YN_IDX_IN_MAIL_BOX_TBL,
649 MODIFIABLE_YN_IDX_IN_MAIL_BOX_TBL,
650 TOTAL_MAIL_COUNT_ON_SERVER_IDX_IN_MAIL_BOX_TBL,
651 ARCHIVE_IDX_IN_MAIL_BOX_TBL,
652 MAIL_SLOT_SIZE_IDX_IN_MAIL_BOX_TBL,
656 ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL = 0,
657 LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
658 MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL,
659 LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
660 SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
661 RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL,
662 SYNC_STATUS_IDX_IN_MAIL_READ_MAIL_UID_TBL,
663 FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL,
664 FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL,
665 IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, /* unused */
668 #ifdef __FEATURE_BODY_SEARCH__
670 MAIL_ID_IDX_IN_MAIL_TEXT_TBL = 0,
671 ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL,
672 MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL,
673 BODY_TEXT_IDX_IN_MAIL_TEXT_TBL,
674 FIELD_COUNT_OF_MAIL_TEXT_TBL,
679 ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL = 0,
680 RULE_ID_IDX_IN_MAIL_RULE_TBL,
681 FILTER_NAME_IDX_IN_MAIL_RULE_TBL,
682 TYPE_IDX_IN_MAIL_RULE_TBL,
683 VALUE_IDX_IN_MAIL_RULE_TBL,
684 VALUE2_IDX_IN_MAIL_RULE_TBL,
685 ACTION_TYPE_IDX_IN_MAIL_RULE_TBL,
686 TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL,
687 FLAG1_IDX_IN_MAIL_RULE_TBL,
688 FLAG2_IDX_IN_MAIL_RULE_TBL,
692 MAIL_ID_IDX_IN_MAIL_TBL = 0,
693 ACCOUNT_ID_IDX_IN_MAIL_TBL,
694 MAILBOX_ID_IDX_IN_MAIL_TBL,
695 MAILBOX_TYPE_IDX_IN_MAIL_TBL,
696 SUBJECT_IDX_IN_MAIL_TBL,
697 DATETIME_IDX_IN_MAIL_TBL,
698 SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL,
699 SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL,
700 SERVER_MAIL_ID_IDX_IN_MAIL_TBL,
701 MESSAGE_ID_IDX_IN_MAIL_TBL,
702 REFERENCE_ID_IDX_IN_MAIL_TBL,
703 FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL,
704 FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL,
705 FULL_ADDRESS_TO_IDX_IN_MAIL_TBL,
706 FULL_ADDRESS_CC_IDX_IN_MAIL_TBL,
707 FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL,
708 FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL,
709 EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL,
710 EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL,
711 ALIAS_SENDER_IDX_IN_MAIL_TBL,
712 ALIAS_RECIPIENT_IDX_IN_MAIL_TBL,
713 BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL,
714 FILE_PATH_PLAIN_IDX_IN_MAIL_TBL,
715 FILE_PATH_HTML_IDX_IN_MAIL_TBL,
716 FILE_PATH_MIME_ENTITY_IDX_IN_MAIL_TBL,
717 MAIL_SIZE_IDX_IN_MAIL_TBL,
718 FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL,
719 FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL,
720 FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL,
721 FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL,
722 FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL,
723 FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL,
724 FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL,
725 DRM_STATUS_IDX_IN_MAIL_TBL,
726 PRIORITY_IDX_IN_MAIL_TBL,
727 SAVE_STATUS_IDX_IN_MAIL_TBL,
728 LOCK_STATUS_IDX_IN_MAIL_TBL,
729 REPORT_STATUS_IDX_IN_MAIL_TBL,
730 ATTACHMENT_COUNT_IDX_IN_MAIL_TBL,
731 INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL,
732 THREAD_ID_IDX_IN_MAIL_TBL,
733 THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL,
734 PREVIEW_TEXT_IDX_IN_MAIL_TBL,
735 MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL,
736 MESSAGE_CLASS_IDX_IN_MAIL_TBL,
737 DIGEST_TYPE_IDX_IN_MAIL_TBL,
738 SMIME_TYPE_IDX_IN_MAIL_TBL,
739 SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL,
740 REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL,
741 TAG_ID_IDX_IN_MAIL_TBL,
742 REPLIED_TIME_IDX_IN_MAIL_TBL,
743 FORWARDED_TIME_IDX_IN_MAIL_TBL,
744 DEFAULT_CHARSET_IDX_IN_MAIL_TBL,
745 EAS_DATA_LENGTH_IDX_IN_MAIL_TBL,
746 EAS_DATA_IDX_IN_MAIL_TBL,
747 USER_NAME_IDX_IN_MAIL_TBL,
748 FIELD_COUNT_OF_MAIL_TBL, /* End of mail_tbl */
752 ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL = 0,
753 ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL,
754 ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL,
755 CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
756 ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL,
757 MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
758 ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
759 MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
760 ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
761 ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
762 ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL,
763 ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
764 ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
765 #ifdef __ATTACHMENT_OPTI__
766 ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL,
767 SECTION_IDX_IN_MAIL_ATTACHMENT_TBL,
772 IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL = 0,
773 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
774 MAIL_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
775 ACCOUNT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
776 ADDRESS_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
777 ADDRESS_IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL,
779 ADDRESS_IDX_IN_MAIL_CONTACT_SYNC_TBL,
780 CONTACT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
781 STORAGE_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
782 CONTACT_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
783 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
784 DISPLAY_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
785 FLAG1_IDX_IN_MAIL_CONTACT_SYNC_TBL,
789 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
791 ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL = 0,
792 STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
793 ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
794 MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
795 SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
796 MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
797 MULTI_USER_NAME_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
801 /* sowmya.kr 03032010, changes for get list of mails for given addr list */
802 typedef struct _em_mail_id_list {
804 struct _em_mail_id_list *next;
807 static char *g_test_query[] = {
808 /* 1. select mail_account_tbl */
811 " incoming_server_type, "
812 " incoming_server_address, "
813 " user_email_address, "
814 " incoming_server_user_name, "
815 " incoming_server_password, "
817 " incoming_server_port_number, "
818 " incoming_server_secure_connection, "
819 " incoming_server_authentication_method,"
820 " outgoing_server_type, "
821 " outgoing_server_address, "
822 " outgoing_server_port_number, "
823 " outgoing_server_need_authentication, "
824 " outgoing_server_secure_connection, "
825 " outgoing_server_user_name, "
826 " outgoing_server_password, "
831 " keep_mails_on_pop_server_after_download, "
832 " auto_resend_times, "
833 " outgoing_server_size_limit, "
834 " wifi_auto_download, "
836 " incoming_server_requires_apop,"
838 " is_preset_account, "
842 " req_delivery_receipt, "
843 " req_read_receipt, "
847 " display_name_from, "
849 " forward_with_files, "
853 ", add_my_address_to_bcc"
854 ", notification_status "
856 ", display_content_status "
857 ", default_ringtone_status "
858 ", alert_ringtone_path "
867 " FROM mail_account_tbl",
868 /* 2. select mail_box_tbl */
878 " total_mail_count_on_server, "
879 " has_archived_mails, "
883 " FROM mail_box_tbl ",
884 /* 3. select mail_read_mail_uid_tbl */
893 " flags_seen_field, "
895 " FROM mail_read_mail_uid_tbl ",
896 /* 4. select mail_rule_tbl */
905 " target_mailbox_id, "
908 " FROM mail_rule_tbl ",
909 /* 5. select mail_tbl */
918 " server_mail_status, "
919 " server_mailbox_name, "
922 " reference_mail_id, "
923 " full_address_from, "
924 " full_address_reply, "
927 " full_address_bcc, "
928 " full_address_return, "
929 " email_address_sender, "
930 " email_address_recipient, "
933 " body_download_status, "
936 " file_path_mime_entity, "
938 " flags_seen_field ,"
939 " flags_deleted_field ,"
940 " flags_flagged_field ,"
941 " flags_answered_field ,"
942 " flags_recent_field ,"
943 " flags_draft_field ,"
944 " flags_forwarded_field,"
950 " attachment_count, "
951 " inline_content_count, "
953 " thread_item_count, "
955 " meeting_request_status, "
960 /* 6. select mail_attachment_tbl */
969 " attachment_save_status, "
970 " attachment_drm_type, "
971 " attachment_drm_method, "
972 " attachment_inline_content_status, "
973 " attachment_mime_type "
974 " FROM mail_attachment_tbl ",
976 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
986 " FROM mail_partial_body_activity_tbl ",
993 " meeting_response, "
997 " global_object_id, "
1000 " standard_time_start_date, "
1003 " daylight_time_start_date, "
1005 " FROM mail_meeting_tbl ",
1007 #ifdef __FEATURE_LOCAL_ACTIVITY__
1016 " FROM mail_local_activity_tbl ",
1023 " expiration_year, "
1024 " expiration_month, "
1026 " issue_organization_name, "
1031 " FROM mail_certificate_tbl ",
1037 " task_parameter_length, "
1038 " task_parameter , "
1040 " FROM mail_task_tbl ",
1041 #ifdef __FEATURE_BODY_SEARCH__
1047 " FROM mail_text_tbl ",
1049 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
1057 " multi_user_name, "
1058 " FROM mail_auto_download_activity_tbl ",
1063 int _field_count_of_table[CREATE_TABLE_MAX] = { 0, };
1065 static int _get_table_field_data_char(char **table, char *buf, int index)
1067 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1068 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1072 if (table[index] != NULL) {
1073 *buf = (char)atoi(table[index]);
1077 /* EM_DEBUG_LOG("Empty field. Set as zero"); */
1083 static int _get_table_field_data_int(char **table, int *buf, int index)
1085 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1086 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1090 if (table[index] != NULL) {
1091 *buf = atoi(table[index]);
1095 /* EM_DEBUG_LOG("Empty field. Set as zero"); */
1101 static int _get_table_field_data_time_t(char **table, time_t *buf, int index)
1103 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1104 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1108 if (table[index] != NULL) {
1109 *buf = (time_t)atol(table[index]);
1113 /* EM_DEBUG_LOG("Empty field. Set as zero"); */
1119 static int _get_table_field_data_string(char **table, char **buf, int ucs2, int index)
1123 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1124 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1128 char *pTemp = table[index];
1133 sLen = EM_SAFE_STRLEN(pTemp);
1135 *buf = (char *) em_malloc(sLen + 1);
1137 EM_DEBUG_EXCEPTION("malloc is failed");
1140 strncpy(*buf, pTemp, sLen);
1144 #ifdef _PRINT_STORAGE_LOG_
1146 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", *buf, index);
1148 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1156 static int _get_table_field_data_string_without_allocation(char **table, char *buf, int buffer_size, int ucs2, int index)
1158 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1159 EM_DEBUG_EXCEPTION(" table[%p], buf[%p], index[%d]", table, buf, index);
1163 char *pTemp = table[index];
1168 memset(buf, 0, buffer_size);
1169 strncpy(buf, pTemp, buffer_size - 1);
1171 #ifdef _PRINT_STORAGE_LOG_
1173 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", buf, index);
1175 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1181 static int _get_table_field_data_blob(char **table, void **buffer, int buffer_size, int index)
1183 if ((table == NULL) || (buffer == NULL) || (index < 0)) {
1184 EM_DEBUG_EXCEPTION(" table[%p], buffer[%p], buffer_size [%d], index[%d]", table, buffer, buffer_size, index);
1188 char *temp_buffer = table[index];
1190 if (temp_buffer == NULL)
1193 *buffer = malloc(buffer_size);
1194 if (*buffer == NULL) {
1195 EM_DEBUG_EXCEPTION("allocation failed.");
1198 memset(*buffer, 0, buffer_size);
1199 memcpy(*buffer, temp_buffer, buffer_size);
1201 #ifdef _PRINT_STORAGE_LOG_
1203 EM_DEBUG_LOG("_get_table_field_data_string - buffer[%s], index[%d]", buffer, index);
1205 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1211 static int _get_stmt_field_data_char(DB_STMT hStmt, char *buf, int index)
1213 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1214 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1218 if (sqlite3_column_text(hStmt, index) != NULL) {
1219 *buf = (char)sqlite3_column_int(hStmt, index);
1220 #ifdef _PRINT_STORAGE_LOG_
1221 EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1226 EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1231 static int _get_stmt_field_data_int(DB_STMT hStmt, int *buf, int index)
1233 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1234 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1238 if (sqlite3_column_text(hStmt, index) != NULL) {
1239 *buf = sqlite3_column_int(hStmt, index);
1240 #ifdef _PRINT_STORAGE_LOG_
1241 EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1246 EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1251 static int _get_stmt_field_data_time_t(DB_STMT hStmt, time_t *buf, int index)
1253 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1254 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1258 if (sqlite3_column_text(hStmt, index) != NULL) {
1259 *buf = (time_t)sqlite3_column_int(hStmt, index);
1260 #ifdef _PRINT_STORAGE_LOG_
1261 EM_DEBUG_LOG("_get_stmt_field_data_time_t [%d]", *buf);
1266 EM_DEBUG_LOG("_get_stmt_field_data_time_t fail. index [%d]", index);
1270 static int _get_stmt_field_data_string(DB_STMT hStmt, char **buf, int ucs2, int index)
1272 if (!hStmt || !buf || (index < 0)) { /*prevent 39619*/
1273 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1278 sLen = sqlite3_column_bytes(hStmt, index);
1280 #ifdef _PRINT_STORAGE_LOG_
1281 EM_DEBUG_LOG("_get_stmt_field_data_string sqlite3_column_bytes sLen[%d]", sLen);
1285 *buf = (char *) em_malloc(sLen + 1);
1287 EM_DEBUG_EXCEPTION("em_mallocfailed");
1291 strncpy(*buf, (char *)sqlite3_column_text(hStmt, index), sLen);
1295 #ifdef _PRINT_STORAGE_LOG_
1297 EM_DEBUG_LOG("buf[%s], index[%d]", *buf, index);
1299 EM_DEBUG_LOG("_get_stmt_field_data_string - No string got");
1305 static void _get_stmt_field_data_blob(DB_STMT hStmt, void **buf, int index)
1307 if (!hStmt || !buf || (index < 0)) { /*prevent 39618*/
1308 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1313 sLen = sqlite3_column_bytes(hStmt, index);
1315 #ifdef _PRINT_STORAGE_LOG_
1316 EM_DEBUG_LOG("_get_stmt_field_data_blob sqlite3_column_bytes sLen[%d]", sLen);
1320 *buf = (char *) em_malloc(sLen);
1322 EM_DEBUG_EXCEPTION("em_mallocfailed");
1326 memcpy(*buf, (void *)sqlite3_column_blob(hStmt, index), sLen);
1332 static int _get_stmt_field_data_string_without_allocation(DB_STMT hStmt, char *buf, int buffer_size, int ucs2, int index)
1334 if (!hStmt || !buf || (index < 0)) { /*prevent 39620*/
1335 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], buffer_size[%d], index[%d]", hStmt, buf, buffer_size, index);
1340 sLen = sqlite3_column_bytes(hStmt, index);
1342 #ifdef _PRINT_STORAGE_LOG_
1343 EM_DEBUG_LOG("_get_stmt_field_data_string_without_allocation sqlite3_column_bytes sLen[%d]", sLen);
1347 memset(buf, 0, buffer_size);
1348 strncpy(buf, (char *)sqlite3_column_text(hStmt, index), buffer_size - 1);
1352 #ifdef _PRINT_STORAGE_LOG_
1353 EM_DEBUG_LOG("buf[%s], index[%d]", buf, index);
1359 static int _bind_stmt_field_data_char(DB_STMT hStmt, int index, char value)
1361 if ((hStmt == NULL) || (index < 0)) {
1362 EM_DEBUG_EXCEPTION("index[%d]", index);
1366 int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1368 if (ret != SQLITE_OK) {
1369 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1376 static int _bind_stmt_field_data_int(DB_STMT hStmt, int index, int value)
1378 if ((hStmt == NULL) || (index < 0)) {
1379 EM_DEBUG_EXCEPTION("index[%d]", index);
1383 int ret = sqlite3_bind_int(hStmt, index+1, value);
1385 if (ret != SQLITE_OK) {
1386 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1393 static int _bind_stmt_field_data_time_t(DB_STMT hStmt, int index, time_t value)
1395 if ((hStmt == NULL) || (index < 0)) {
1396 EM_DEBUG_EXCEPTION("index[%d]", index);
1400 int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1402 if (ret != SQLITE_OK) {
1403 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1410 static int _bind_stmt_field_data_string(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1412 if ((hStmt == NULL) || (index < 0)) {
1413 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1417 #ifdef _PRINT_STORAGE_LOG_
1418 EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1423 ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1425 ret = sqlite3_bind_text(hStmt, index+1, "", -1, NULL);
1427 if (ret != SQLITE_OK) {
1428 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1434 /* destroy function for sqlite3_bind_text */
1435 void _bind_stmt_free_string(void* arg)
1437 EM_DEBUG_FUNC_BEGIN();
1438 char* p = (char*) arg;
1440 EM_DEBUG_FUNC_END();
1443 static int _bind_stmt_field_data_nstring(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1445 if ((hStmt == NULL) || (index < 0)) {
1446 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1450 #ifdef _PRINT_STORAGE_LOG_
1451 EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1455 if (value != NULL) {
1456 if (strlen(value) <= max_len)
1457 ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1459 char *buf = (char*)em_malloc(sizeof(char) * (max_len));
1461 EM_DEBUG_EXCEPTION("em_mallocfailed");
1464 snprintf(buf, max_len-1, "%s", value);
1465 ret = sqlite3_bind_text(hStmt, index+1, buf, -1, _bind_stmt_free_string);
1468 ret = sqlite3_bind_text(hStmt, index+1, "", -1, NULL);
1470 if (ret != SQLITE_OK) {
1471 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1478 static int _bind_stmt_field_data_blob(DB_STMT hStmt, int index, void *blob, int blob_size)
1480 if ((hStmt == NULL) || (index < 0)) {
1481 EM_DEBUG_EXCEPTION("index[%d], blob_size[%d]", index, blob_size);
1485 #ifdef _PRINT_STORAGE_LOG_
1486 EM_DEBUG_LOG("hStmt = %p, index = %d, blob_size = %d, blob = [%p]", hStmt, index, blob_size, blob);
1491 ret = sqlite3_bind_blob(hStmt, index+1, blob, blob_size, SQLITE_STATIC);
1493 ret = sqlite3_bind_null(hStmt, index+1);
1495 if (ret != SQLITE_OK) {
1496 EM_DEBUG_EXCEPTION("sqlite3_bind_blob fail [%d]", ret);
1503 static int _delete_temp_file(const char *path)
1505 EM_DEBUG_FUNC_BEGIN("path[%p]", path);
1508 struct dirent *entry = NULL;
1510 char buf[1024] = {0x00, };
1512 if ((dp = opendir(path)) == NULL) {
1513 EM_DEBUG_EXCEPTION("opendir(\"%s\") failed...", path);
1517 while ((entry = readdir(dp)) != NULL) {
1518 SNPRINTF(buf, sizeof(buf), "%s/%s", path, entry->d_name);
1523 EM_DEBUG_FUNC_END();
1527 char *cpy_str(char *src)
1532 if (!(p = em_malloc((int)EM_SAFE_STRLEN(src) + 1))) {
1533 EM_DEBUG_EXCEPTION("mailoc failed...");
1536 strncpy(p, src, EM_SAFE_STRLEN(src));
1542 static void _emstorage_close_once(void)
1544 EM_DEBUG_FUNC_BEGIN();
1546 EM_DEBUG_FUNC_END();
1549 INTERNAL_FUNC int emstorage_close(int *err_code)
1551 EM_DEBUG_FUNC_BEGIN();
1554 int error = EMAIL_ERROR_NONE;
1556 if (!emstorage_db_close(NULL, &error))
1558 if (--_open_counter == 0)
1559 _emstorage_close_once();
1563 if (err_code != NULL)
1566 EM_DEBUG_FUNC_END("ret [%d]", ret);
1570 static void *_emstorage_open_once(char *multi_user_name, int *err_code)
1572 EM_DEBUG_FUNC_BEGIN();
1574 int error = EMAIL_ERROR_NONE;
1576 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1577 char buf[MAX_PATH] = {0};
1578 char *prefix_path = NULL;
1580 error = emcore_get_container_path(multi_user_name, &prefix_path);
1581 if (error != EMAIL_ERROR_NONE) {
1582 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
1586 memset(buf, 0x00, sizeof(buf));
1587 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, EMAILPATH);
1588 mkdir(buf, DIRECTORY_PERMISSION);
1590 memset(buf, 0x00, sizeof(buf));
1591 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAILHOME);
1592 mkdir(buf, DIRECTORY_PERMISSION);
1594 memset(buf, 0x00, sizeof(buf));
1595 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAILTEMP);
1596 mkdir(buf, DIRECTORY_PERMISSION);
1598 _delete_temp_file(buf);
1599 EM_SAFE_FREE(prefix_path);
1601 mkdir(DATA_PATH, DIRECTORY_PERMISSION);
1602 mkdir(EMAILPATH, DIRECTORY_PERMISSION);
1603 mkdir(MAILHOME, DIRECTORY_PERMISSION);
1604 mkdir(MAILTEMP, DIRECTORY_PERMISSION);
1606 _delete_temp_file(MAILTEMP);
1609 if (!emstorage_create_table(multi_user_name, EMAIL_CREATE_DB_NORMAL, &error)) {
1610 EM_DEBUG_EXCEPTION(" emstorage_create_table failed - %d", error);
1617 if (err_code != NULL)
1623 /* pData : a parameter which is registered when busy handler is registerd */
1624 /* count : retry count */
1625 static int _callback_sqlite_busy_handler(void *pData, int count)
1627 if (10 - count > 0) {
1628 struct timespec time = {
1630 .tv_nsec = (count + 1) * 100 * 1000 * 1000
1632 EM_DEBUG_LOG("Busy handler called!!: PID[%d] / CNT [%d]", getpid(), count);
1633 nanosleep(&time, NULL);
1636 EM_DEBUG_EXCEPTION("Busy handler will be returned SQLITE_BUSY error PID[%d] / CNT[%d]", getpid(), count);
1641 static int _callback_collation_utf7_sort(void *data, int length_text_a, const void *text_a,
1642 int length_text_b, const void *text_b)
1644 EM_DEBUG_FUNC_BEGIN();
1646 char *converted_string_a = NULL;
1647 char *converted_string_b = NULL;
1649 EM_DEBUG_LOG_DEV("text_a : [%s]", text_a);
1650 converted_string_a = emcore_convert_mutf7_to_utf8((char *)text_a);
1651 EM_DEBUG_LOG_DEV("Converted text_a : [%s]", converted_string_a);
1653 EM_DEBUG_LOG_DEV("text_b : [%s]", text_b);
1654 converted_string_b = emcore_convert_mutf7_to_utf8((char *)text_b);
1655 EM_DEBUG_LOG_DEV("Converted text_b : [%s]", converted_string_b);
1657 if (converted_string_a && converted_string_b)
1658 result = strcmp(converted_string_a, converted_string_b);
1660 EM_SAFE_FREE(converted_string_a);
1661 EM_SAFE_FREE(converted_string_b);
1663 EM_DEBUG_FUNC_END();
1667 static int _delete_all_files_and_directories(char *db_file_path, int *err_code)
1669 EM_DEBUG_FUNC_BEGIN();
1671 int error = EMAIL_ERROR_NONE;
1674 if (!emstorage_delete_file(db_file_path, &error)) {
1675 if (error != EMAIL_ERROR_FILE_NOT_FOUND) {
1676 EM_DEBUG_EXCEPTION_SEC("remove failed - %s", EMAIL_SERVICE_DB_FILE_PATH);
1681 if (!emstorage_delete_dir(MAILHOME, &error)) {
1682 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed");
1691 EM_DEBUG_FUNC_END();
1695 static int _recovery_from_malformed_db_file(char *db_file_path, int *err_code)
1697 EM_DEBUG_FUNC_BEGIN();
1699 int error = EMAIL_ERROR_NONE;
1702 /* Delete all files and directories */
1703 if (!_delete_all_files_and_directories(db_file_path, &error)) {
1704 EM_DEBUG_EXCEPTION("_delete_all_files_and_directories failed [%d]", error);
1708 /* Delete all accounts on MyAccount */
1710 /* Delete all managed connection to DB */
1711 emstorage_reset_db_handle_list();
1718 EM_DEBUG_FUNC_END();
1722 int _xsystem(const char *argv[])
1730 perror("fork failed");
1733 if (execvp(argv[0], (char *const *)argv) == -1) {
1734 perror("execute init db script");
1744 if (waitpid(pid, &status, 0) == -1) {
1745 perror("waitpid failed");
1749 if (WIFSIGNALED(status)) {
1754 if (!WIFEXITED(status)) {
1755 perror("should not happen");
1759 return WEXITSTATUS(status);
1762 #define SCRIPT_INIT_DB "/usr/bin/email-service_init_db.sh"
1764 INTERNAL_FUNC int emstorage_init_db(char *multi_user_name)
1766 EM_DEBUG_FUNC_BEGIN();
1767 int err = EMAIL_ERROR_NONE;
1768 char *prefix_path = NULL;
1769 char *output_file_path = NULL;
1770 char temp_file_path[MAX_PATH] = {0};
1772 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1773 err = emcore_get_container_path(multi_user_name, &prefix_path);
1774 if (err != EMAIL_ERROR_NONE) {
1775 if (err != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1776 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", err);
1781 prefix_path = strdup("");
1784 if (err == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1785 err = emcore_get_canonicalize_path(EMAIL_SERVICE_DB_FILE_PATH, &output_file_path);
1786 if (err != EMAIL_ERROR_NONE) {
1787 EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", err);
1791 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
1793 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
1796 EM_DEBUG_LOG("db file path : [%s]", temp_file_path);
1798 if (!g_file_test(temp_file_path, G_FILE_TEST_EXISTS)) {
1800 const char *argv_script[] = {"/bin/sh", SCRIPT_INIT_DB, NULL};
1801 ret = _xsystem(argv_script);
1804 EM_DEBUG_EXCEPTION("_xsystem failed");
1805 err = EMAIL_ERROR_SYSTEM_FAILURE;
1811 EM_SAFE_FREE(prefix_path);
1812 EM_SAFE_FREE(output_file_path);
1814 EM_DEBUG_FUNC_END();
1818 INTERNAL_FUNC int em_db_open(char *db_file_path, sqlite3 **sqlite_handle, int *err_code)
1820 EM_DEBUG_FUNC_BEGIN();
1822 int error = EMAIL_ERROR_NONE;
1825 EM_DEBUG_LOG_DEV("*sqlite_handle[%p]", *sqlite_handle);
1827 if (*sqlite_handle) { /*prevent 33351*/
1828 EM_DEBUG_LOG_DEV(">>>>> DB Already Opened......");
1829 if (err_code != NULL)
1834 EM_DEBUG_LOG("DB file path : [%s]", db_file_path);
1837 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1838 if (SQLITE_OK != rc) {
1839 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1840 if (SQLITE_PERM == rc || SQLITE_CANTOPEN == rc) {
1841 error = EMAIL_ERROR_PERMISSION_DENIED;
1843 error = EMAIL_ERROR_DB_FAILURE;
1845 sqlite3_close(*sqlite_handle);
1846 *sqlite_handle = NULL;
1848 if (SQLITE_CORRUPT == rc) /* SQLITE_CORRUPT : The database disk image is malformed */ {/* Recovery DB file */
1849 EM_DEBUG_LOG("The database disk image is malformed. Trying to remove and create database disk image and directories");
1850 if (!_recovery_from_malformed_db_file(db_file_path, &error)) {
1851 EM_DEBUG_EXCEPTION("_recovery_from_malformed_db_file failed [%d]", error);
1855 EM_DEBUG_LOG("Open DB again");
1856 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1857 if (SQLITE_OK != rc) {
1858 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1859 if (SQLITE_PERM == rc) {
1860 error = EMAIL_ERROR_PERMISSION_DENIED;
1862 error = EMAIL_ERROR_DB_FAILURE;
1864 sqlite3_close(*sqlite_handle);
1865 *sqlite_handle = NULL;
1866 goto FINISH_OFF; /*prevent 33351*/
1872 /* register busy handler */
1873 EM_DEBUG_LOG_DEV(">>>>> Register DB Handle to busy handler: *sqlite_handle[%p]", *sqlite_handle);
1874 rc = sqlite3_busy_handler(*sqlite_handle, _callback_sqlite_busy_handler, NULL); /* Busy Handler registration, NULL is a parameter which will be passed to handler */
1875 if (SQLITE_OK != rc) {
1876 EM_DEBUG_EXCEPTION("sqlite3_busy_handler fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1877 error = EMAIL_ERROR_DB_FAILURE;
1878 sqlite3_close(*sqlite_handle);
1879 *sqlite_handle = NULL;
1883 /* Register collation callback function */
1884 rc = sqlite3_create_collation(*sqlite_handle, "CONVERTUTF8", SQLITE_UTF8, NULL, _callback_collation_utf7_sort);
1885 if (SQLITE_OK != rc) {
1886 EM_DEBUG_EXCEPTION("sqlite3_create_collation failed : [%d][%s]", rc, sqlite3_errmsg(*sqlite_handle));
1887 error = EMAIL_ERROR_DB_FAILURE;
1888 sqlite3_close(*sqlite_handle);
1889 *sqlite_handle = NULL;
1896 if (err_code != NULL)
1899 EM_DEBUG_FUNC_END("ret [%d]", ret);
1903 INTERNAL_FUNC sqlite3* emstorage_db_open(char *multi_user_name, int *err_code)
1905 EM_DEBUG_FUNC_BEGIN();
1907 sqlite3 *_db_handle = NULL;
1909 int error = EMAIL_ERROR_NONE;
1910 char *prefix_path = NULL;
1912 _db_handle = emstorage_get_db_handle(multi_user_name);
1914 if (_db_handle == NULL) {
1915 char *output_file_path = NULL;
1916 char temp_file_path[MAX_PATH] = {0};
1918 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1919 error = emcore_get_container_path(multi_user_name, &prefix_path);
1920 if (error != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION && error != EMAIL_ERROR_NONE) {
1921 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", error);
1925 prefix_path = strdup("");
1928 if (error == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1929 if ((error = emcore_get_canonicalize_path(EMAIL_SERVICE_DB_FILE_PATH, &output_file_path)) != EMAIL_ERROR_NONE) {
1930 EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", error);
1934 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
1935 EM_SAFE_FREE(output_file_path);
1937 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
1940 if (!em_db_open(temp_file_path, &_db_handle, &error)) {
1941 EM_DEBUG_EXCEPTION("em_db_open failed[%d]", error);
1945 _initialize_shm_mutex(SHM_FILE_FOR_DB_LOCK, &shm_fd_for_db_lock, &mapped_for_db_lock);
1947 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
1948 _initialize_shm_mutex(SHM_FILE_FOR_MAIL_ID_LOCK, &shm_fd_for_generating_mail_id, &mapped_for_generating_mail_id);
1949 #endif /*__FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
1951 emstorage_set_db_handle(multi_user_name, _db_handle);
1953 emstorage_initialize_field_count();
1958 EM_SAFE_FREE(prefix_path);
1960 if (err_code != NULL)
1963 EM_DEBUG_FUNC_END("ret [%p]", _db_handle);
1967 INTERNAL_FUNC int emstorage_db_close(char *multi_user_name, int *err_code)
1969 EM_DEBUG_FUNC_BEGIN();
1970 #ifdef _MULTIPLE_DB_HANDLE
1971 sqlite3 *_db_handle = emstorage_get_db_handle(multi_user_name);
1974 int error = EMAIL_ERROR_NONE;
1978 ret = sqlite3_close(_db_handle);
1979 if (ret != SQLITE_OK) {
1980 EM_DEBUG_EXCEPTION(" sqlite3_close fail - %d", ret);
1981 error = EMAIL_ERROR_DB_FAILURE;
1985 #ifdef _MULTIPLE_DB_HANDLE
1986 emstorage_remove_db_handle();
1994 if (err_code != NULL)
1997 EM_DEBUG_FUNC_END("ret [%d]", ret);
2001 INTERNAL_FUNC int emstorage_open(char *multi_user_name, int *err_code)
2003 EM_DEBUG_FUNC_BEGIN();
2006 int error = EMAIL_ERROR_NONE;
2008 char *prefix_path = NULL;
2009 char buf[MAX_PATH] = {0};
2011 if (EM_SAFE_STRLEN(multi_user_name) <= 0) {
2012 SNPRINTF(buf, sizeof(buf), "%s", DB_PATH);
2014 error = emcore_get_container_path(multi_user_name, &prefix_path);
2015 if (error != EMAIL_ERROR_NONE) {
2016 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
2019 SNPRINTF(buf, sizeof(buf), "%s/%s", prefix_path, DB_PATH);
2022 if (!g_file_test(buf, G_FILE_TEST_EXISTS)) {
2023 retValue = mkdir(buf, DIRECTORY_PERMISSION);
2025 EM_DEBUG_LOG("mkdir return- %d", retValue);
2026 EM_DEBUG_LOG("emstorage_open - before sqlite3_open - pid = %d", getpid());
2029 if (emstorage_db_open(multi_user_name, &error) == NULL) {
2030 EM_DEBUG_EXCEPTION("emstorage_db_open failed[%d]", error);
2034 if (_open_counter++ == 0) {
2035 _emstorage_open_once(multi_user_name, &error);
2042 EM_SAFE_FREE(prefix_path);
2044 if (err_code != NULL)
2047 EM_DEBUG_FUNC_END("ret [%d]", ret);
2051 static int emstorage_get_field_count_from_create_table_query(char *input_create_table_query, int *output_field_count)
2053 EM_DEBUG_FUNC_BEGIN("input_create_table_query[%d], output_field_count[%p]", input_create_table_query, output_field_count);
2054 int err = EMAIL_ERROR_NONE;
2055 int field_count = 0;
2058 if (input_create_table_query == NULL || output_field_count == NULL) {
2059 err = EMAIL_ERROR_INVALID_PARAM;
2063 pos = input_create_table_query;
2067 if (pos == NULL || *pos == (char)0)
2070 pos = strchr(pos, ',');
2073 *output_field_count = field_count;
2075 EM_DEBUG_LOG_DEV("field_count [%d]", field_count);
2079 EM_DEBUG_FUNC_END("err [%d]", err);
2083 INTERNAL_FUNC int emstorage_initialize_field_count()
2085 EM_DEBUG_FUNC_BEGIN();
2086 int err = EMAIL_ERROR_NONE;
2088 char **create_table_query = NULL;
2090 if (_field_count_of_table[CREATE_TABLE_MAIL_ACCOUNT_TBL] != 0) {
2091 err = EMAIL_ERROR_ALREADY_INITIALIZED;
2095 err = emcore_load_query_from_file(EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2096 if (err != EMAIL_ERROR_NONE) {
2097 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", err);
2101 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]));
2102 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]));
2103 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]));
2104 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]));
2105 emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_TBL]));
2106 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]));
2107 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]));
2108 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]));
2109 #ifdef __FEATURE_LOCAL_ACTIVITY__
2110 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]));
2111 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2112 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]));
2113 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]));
2114 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]));
2117 if (create_table_query) {
2119 for (i = 0; i < query_len; i++) {
2120 if (create_table_query[i]) {
2121 EM_SAFE_FREE(create_table_query[i]);
2124 EM_SAFE_FREE(create_table_query);
2127 EM_DEBUG_FUNC_END("err [%d]", err);
2131 INTERNAL_FUNC int emstorage_create_table(char *multi_user_name, emstorage_create_db_type_t type, int *err_code)
2133 EM_DEBUG_FUNC_BEGIN();
2135 int error = EMAIL_ERROR_NONE;
2136 int rc = -1, ret = false;
2138 char sql_query_string[QUERY_SIZE] = {0, };
2139 char **create_table_query = NULL;
2141 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2143 error = emcore_load_query_from_file(EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2144 if (error != EMAIL_ERROR_NONE) {
2145 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
2149 if (query_len < CREATE_TABLE_MAX) {
2150 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
2151 error = EMAIL_ERROR_SYSTEM_FAILURE;
2155 EM_DEBUG_LOG("local_db_handle = %p.", local_db_handle);
2158 char **result = NULL;
2160 /* 1. create mail_account_tbl */
2161 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_account_tbl';";
2162 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2163 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2164 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2166 EM_DEBUG_LOG("emstorage_create_table - result[1] = %s %c", result[1], result[1]);
2168 if (atoi(result[1]) < 1) {
2169 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2170 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2171 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2173 EM_DEBUG_LOG("CREATE TABLE mail_account_tbl");
2174 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2175 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2176 if (error != EMAIL_ERROR_NONE) {
2177 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2181 /* create mail_account_tbl unique index */
2182 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ACCOUNT_IDX]);
2183 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2184 if (error != EMAIL_ERROR_NONE) {
2185 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2189 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2190 } /* mail_account_tbl */
2191 else if (type == EMAIL_CREATE_DB_CHECK) {
2192 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], NULL, NULL, NULL), rc);
2193 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)));
2196 sqlite3_free_table(result);
2199 /* 2. create mail_box_tbl */
2200 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_box_tbl';";
2202 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2203 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2204 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2206 if (atoi(result[1]) < 1) {
2207 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2208 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2209 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2211 EM_DEBUG_LOG("CREATE TABLE mail_box_tbl");
2213 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_BOX_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2214 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2215 if (error != EMAIL_ERROR_NONE) {
2216 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2220 /* create mail_local_mailbox_tbl unique index */
2221 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_BOX_IDX]);
2222 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2223 if (error != EMAIL_ERROR_NONE) {
2224 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2228 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2229 } /* mail_box_tbl */
2230 else if (type == EMAIL_CREATE_DB_CHECK) {
2231 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_BOX_TBL], NULL, NULL, NULL);
2232 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2233 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_BOX_TBL], rc, sqlite3_errmsg(local_db_handle)));
2235 sqlite3_free_table(result);
2238 /* 3. create mail_read_mail_uid_tbl */
2239 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_read_mail_uid_tbl';";
2240 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2241 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2242 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2245 if (atoi(result[1]) < 1) {
2246 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2247 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2248 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2250 EM_DEBUG_LOG("CREATE TABLE mail_read_mail_uid_tbl");
2252 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2253 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2254 if (error != EMAIL_ERROR_NONE) {
2255 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2259 /* create mail_read_mail_uid_tbl unique index */
2260 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_IDX]);
2261 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2262 if (error != EMAIL_ERROR_NONE) {
2263 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2267 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2268 } /* mail_read_mail_uid_tbl */
2269 else if (type == EMAIL_CREATE_DB_CHECK) {
2270 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], NULL, NULL, NULL), rc);
2271 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2272 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], rc, sqlite3_errmsg(local_db_handle)));
2274 sqlite3_free_table(result);
2277 /* 4. create mail_rule_tbl */
2278 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_rule_tbl';";
2280 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2281 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2282 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2284 if (atoi(result[1]) < 1) {
2285 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2286 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2287 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2289 EM_DEBUG_LOG("CREATE TABLE mail_rule_tbl");
2291 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_RULE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2292 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2293 if (error != EMAIL_ERROR_NONE) {
2294 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2298 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2299 } /* mail_rule_tbl */
2300 else if (type == EMAIL_CREATE_DB_CHECK) {
2301 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_RULE_TBL], NULL, NULL, NULL), rc);
2302 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2303 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_RULE_TBL], rc, sqlite3_errmsg(local_db_handle)));
2305 sqlite3_free_table(result);
2308 /* 5. create mail_tbl */
2309 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_tbl';";
2310 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2311 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2312 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2314 if (atoi(result[1]) < 1) {
2315 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2316 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2317 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2318 EM_DEBUG_LOG("CREATE TABLE mail_tbl");
2320 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2321 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2322 if (error != EMAIL_ERROR_NONE) {
2323 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2327 /* create mail_tbl unique index */
2328 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_IDX]);
2329 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2330 if (error != EMAIL_ERROR_NONE) {
2331 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2335 /* create mail_tbl index for date_time */
2336 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_DATETIME_IDX]);
2337 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2338 if (error != EMAIL_ERROR_NONE) {
2339 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2343 /* create mail_tbl index for thread_item_count */
2344 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_THREAD_IDX]);
2345 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2346 if (error != EMAIL_ERROR_NONE) {
2347 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2351 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2352 /* just one time call */
2353 /* EFTSInitFTSIndex(FTS_EMAIL_IDX); */
2355 else if (type == EMAIL_CREATE_DB_CHECK) {
2356 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TBL], NULL, NULL, NULL), rc);
2357 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2358 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TBL], rc, sqlite3_errmsg(local_db_handle)));
2360 sqlite3_free_table(result);
2363 /* 6. create mail_attachment_tbl */
2364 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_attachment_tbl';";
2365 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2366 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2367 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2369 if (atoi(result[1]) < 1) {
2370 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2371 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2372 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2374 EM_DEBUG_LOG("CREATE TABLE mail_attachment_tbl");
2376 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2377 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2378 if (error != EMAIL_ERROR_NONE) {
2379 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2383 /* create mail_attachment_tbl unique index */
2384 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_IDX]);
2385 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2386 if (error != EMAIL_ERROR_NONE) {
2387 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2391 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2392 } /* mail_attachment_tbl */
2393 else if (type == EMAIL_CREATE_DB_CHECK) {
2394 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], NULL, NULL, NULL), rc);
2395 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2396 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], rc, sqlite3_errmsg(local_db_handle)));
2398 sqlite3_free_table(result);
2401 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
2403 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_partial_body_activity_tbl';";
2404 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2405 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2406 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2408 if (atoi(result[1]) < 1) {
2410 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2411 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2412 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2414 EM_DEBUG_LOG("CREATE TABLE mail_partial_body_activity_tbl");
2416 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2417 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2418 if (error != EMAIL_ERROR_NONE) {
2419 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2423 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2424 } /* mail_rule_tbl */
2425 else if (type == EMAIL_CREATE_DB_CHECK) {
2426 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2427 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2428 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2430 sqlite3_free_table(result);
2433 #endif /* __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
2435 /* create mail_meeting_tbl */
2436 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_meeting_tbl';";
2437 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2438 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2439 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2441 if (atoi(result[1]) < 1) {
2442 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2443 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2444 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2446 EM_DEBUG_LOG("CREATE TABLE mail_meeting_tbl");
2448 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_MEETING_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2449 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2450 if (error != EMAIL_ERROR_NONE) {
2451 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2455 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_MEETING_IDX]);
2456 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2457 if (error != EMAIL_ERROR_NONE) {
2458 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2462 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2463 } /* mail_contact_sync_tbl */
2464 else if (type == EMAIL_CREATE_DB_CHECK) {
2465 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], NULL, NULL, NULL), rc);
2466 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2467 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], rc, sqlite3_errmsg(local_db_handle)));
2469 sqlite3_free_table(result);
2472 #ifdef __FEATURE_LOCAL_ACTIVITY__
2474 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_local_activity_tbl';";
2475 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2476 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2477 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2479 if (atoi(result[1]) < 1) {
2481 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2482 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2483 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2485 EM_DEBUG_LOG(" CREATE TABLE mail_local_activity_tbl");
2487 SNPRINTF(sql_query_string, sizeof(sql_query_string), create_table_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL]);
2488 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2489 if (error != EMAIL_ERROR_NONE) {
2490 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2494 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2495 } /* mail_rule_tbl */
2496 else if (type == EMAIL_CREATE_DB_CHECK) {
2497 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL);
2498 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2499 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2500 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2502 sqlite3_free_table(result);
2504 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2505 /* create mail_certificate_tbl */
2506 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_certificate_tbl';";
2507 /* rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
2508 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2509 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)));
2511 if (atoi(result[1]) < 1) {
2512 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2513 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2515 EM_DEBUG_LOG("CREATE TABLE mail_certificate_tbl");
2517 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2518 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2519 if (error != EMAIL_ERROR_NONE) {
2520 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2524 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2525 } /* mail_contact_sync_tbl */
2526 else if (type == EMAIL_CREATE_DB_CHECK) {
2527 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], NULL, NULL, NULL), rc);
2528 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)));
2531 sqlite3_free_table(result);
2534 /* create mail_task_tbl */
2535 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_task_tbl';";
2536 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2537 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)));
2539 if (atoi(result[1]) < 1) {
2540 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2541 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2543 EM_DEBUG_LOG("CREATE TABLE mail_task_tbl");
2545 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TASK_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2546 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2547 if (error != EMAIL_ERROR_NONE) {
2548 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2552 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_TASK_IDX]);
2553 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2554 if (error != EMAIL_ERROR_NONE) {
2555 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2559 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2560 } /* mail_task_tbl */
2561 else if (type == EMAIL_CREATE_DB_CHECK) {
2562 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TASK_TBL], NULL, NULL, NULL), rc);
2563 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)));
2566 sqlite3_free_table(result);
2569 #ifdef __FEATURE_BODY_SEARCH__
2570 /* create mail_text_tbl */
2571 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_text_tbl';";
2572 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2573 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)));
2575 if (atoi(result[1]) < 1) {
2576 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2577 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2579 EM_DEBUG_LOG("CREATE TABLE mail_text_tbl");
2581 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TEXT_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2582 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2583 if (error != EMAIL_ERROR_NONE) {
2584 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2588 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2589 } /* mail_text_tbl */
2590 else if (type == EMAIL_CREATE_DB_CHECK) {
2591 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TEXT_TBL], NULL, NULL, NULL), rc);
2592 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)));
2595 sqlite3_free_table(result);
2600 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
2601 /* create mail_auto_download_activity_tbl */
2602 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_auto_download_activity_tbl';";
2603 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2604 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)));
2606 if (atoi(result[1]) < 1) {
2607 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2608 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2610 EM_DEBUG_LOG("CREATE TABLE mail_auto_download_activity_tbl");
2612 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], sizeof(sql_query_string)-1);
2613 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2614 if (error != EMAIL_ERROR_NONE) {
2615 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2619 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2620 } /* mail_auto_download_activity_tbl */
2621 else if (type == EMAIL_CREATE_DB_CHECK) {
2622 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2623 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)));
2626 sqlite3_free_table(result);
2635 if (result) sqlite3_free_table(result);
2638 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2640 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
2644 if (create_table_query) {
2646 for (i = 0; i < query_len; i++) {
2647 if (create_table_query[i]) {
2648 EM_SAFE_FREE(create_table_query[i]);
2651 EM_SAFE_FREE(create_table_query);
2654 if (err_code != NULL)
2657 EM_DEBUG_FUNC_END("ret [%d]", ret);
2661 /* Query series --------------------------------------------------------------*/
2662 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)
2664 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);
2667 int error = EMAIL_ERROR_NONE;
2668 DB_STMT hStmt = NULL;
2669 char *sql_query_string = NULL;
2671 sqlite3 *local_db_handle = NULL;
2673 if (!input_conditional_clause || (!output_total_mail_count && !output_unseen_mail_count)) {
2674 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2675 return EMAIL_ERROR_INVALID_PARAM;
2678 query_size = EM_SAFE_STRLEN(input_conditional_clause) + QUERY_SIZE;
2679 sql_query_string = em_malloc(query_size);
2680 if (sql_query_string == NULL) {
2681 EM_DEBUG_EXCEPTION("em_mallocfailed");
2682 error = EMAIL_ERROR_OUT_OF_MEMORY;
2686 local_db_handle = emstorage_get_db_connection(multi_user_name);
2688 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2690 SNPRINTF(sql_query_string, query_size, "SELECT COUNT(*) FROM mail_tbl");
2691 EM_SAFE_STRCAT(sql_query_string, (char*)input_conditional_clause);
2693 if (output_total_mail_count) {
2694 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
2695 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2696 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
2698 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2699 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2700 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
2701 _get_stmt_field_data_int(hStmt, output_total_mail_count, 0);
2704 if (output_unseen_mail_count) {
2705 EM_SAFE_STRCAT(sql_query_string, " AND flags_seen_field = 0 ");
2707 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
2708 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2709 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2711 *output_unseen_mail_count = atoi(result[1]);
2712 sqlite3_free_table(result);
2717 if (hStmt != NULL) {
2718 rc = sqlite3_finalize(hStmt);
2719 if (rc != SQLITE_OK) {
2720 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
2721 error = EMAIL_ERROR_DB_FAILURE;
2725 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2727 EM_SAFE_FREE(sql_query_string);
2729 EM_DEBUG_FUNC_END("error [%d]", error);
2733 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)
2735 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);
2743 int error = EMAIL_ERROR_NONE;
2744 int *result_mail_id_list = NULL;
2745 char **result = NULL;
2746 char *sql_query_string = NULL;
2747 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2749 EM_IF_NULL_RETURN_VALUE(input_conditional_clause, EMAIL_ERROR_INVALID_PARAM);
2750 EM_IF_NULL_RETURN_VALUE(output_mail_id_list, EMAIL_ERROR_INVALID_PARAM);
2751 EM_IF_NULL_RETURN_VALUE(output_mail_id_count, EMAIL_ERROR_INVALID_PARAM);
2753 query_size = strlen(input_conditional_clause) + strlen("SELECT mail_id FROM mail_tbl ") + 10; // 10 is extra space
2754 sql_query_string = em_malloc(query_size);
2755 if (sql_query_string == NULL) {
2756 EM_DEBUG_EXCEPTION("em_mallocfailed");
2757 error = EMAIL_ERROR_OUT_OF_MEMORY;
2761 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2763 /* Composing query */
2764 SNPRINTF_OFFSET(sql_query_string, cur_query, query_size, "SELECT mail_id FROM mail_tbl ");
2765 EM_SAFE_STRCAT(sql_query_string, (char*)input_conditional_clause);
2767 EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
2769 /* Performing query */
2770 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2771 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2772 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2776 /* to get mail list */
2778 EM_DEBUG_LOG("No mail found...");
2779 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2783 EM_DEBUG_LOG("There are [%d] mails.", count);
2785 if (!(result_mail_id_list = (int*)em_malloc(sizeof(int) * count))) {
2786 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
2787 error = EMAIL_ERROR_OUT_OF_MEMORY;
2791 EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2793 for (i = 0; i < count; i++)
2794 _get_table_field_data_int(result, result_mail_id_list + i, col_index++);
2796 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >> ", count);
2798 *output_mail_id_list = result_mail_id_list;
2799 *output_mail_id_count = count;
2804 sqlite3_free_table(result);
2806 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2808 EM_SAFE_FREE(sql_query_string);
2810 if (error != EMAIL_ERROR_NONE)
2811 EM_SAFE_FREE(result_mail_id_list);
2813 EM_DEBUG_FUNC_END("error [%d]", error);
2817 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)
2819 EM_DEBUG_FUNC_BEGIN();
2820 EM_PROFILE_BEGIN(emstorage_query_mail_list_func);
2822 int i = 0, count = 0, rc = -1, to_get_count = (result_mail_list) ? 0 : 1;
2823 int sql_query_string_length = 0;
2824 int local_inline_content_count = 0, local_attachment_count = 0;
2825 int cur_query = 0, base_count = 0, col_index;
2826 int ret = false, error = EMAIL_ERROR_NONE;
2827 char *date_time_string = NULL;
2828 char **result = NULL;
2829 char *field_mail_id = "mail_id";
2830 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 ";
2831 char *select_query_form = "SELECT %s FROM mail_tbl ";
2832 char *target_field = NULL;
2833 char *sql_query_string = NULL;
2834 email_mail_list_item_t *mail_list_item_from_tbl = NULL;
2835 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2837 EM_IF_NULL_RETURN_VALUE(conditional_clause, false);
2838 EM_IF_NULL_RETURN_VALUE(result_count, false);
2840 EMSTORAGE_START_READ_TRANSACTION(transaction);
2843 if (to_get_count) /* count only */
2844 target_field = field_mail_id;
2845 else /* mail list in plain form */
2846 target_field = field_all;
2848 sql_query_string_length = EM_SAFE_STRLEN(select_query_form) + EM_SAFE_STRLEN(target_field) + EM_SAFE_STRLEN(conditional_clause);
2850 if (sql_query_string_length)
2851 sql_query_string = em_malloc(sql_query_string_length);
2853 if (sql_query_string == NULL) {
2854 EM_DEBUG_EXCEPTION("em_mallocfailed...");
2855 error = EMAIL_ERROR_OUT_OF_MEMORY;
2859 cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, sql_query_string_length, select_query_form, target_field);
2861 strncat(sql_query_string, conditional_clause, sql_query_string_length - cur_query);
2863 EM_DEBUG_LOG_DEV("query[%s]", sql_query_string);
2865 /* performing query */
2866 EM_PROFILE_BEGIN(emstorage_query_mail_list_performing_query);
2867 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2868 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2869 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2870 EM_PROFILE_END(emstorage_query_mail_list_performing_query);
2876 for (tmp = field_all; tmp && *(tmp + 1); tmp = index(tmp + 1, ',')) i++ ;
2880 col_index = base_count;
2882 EM_DEBUG_LOG_DEV("base_count [%d]", base_count);
2887 EM_DEBUG_LOG_DEV("No mail found...");
2889 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2892 EM_DEBUG_LOG_DEV("There are [%d] mails.", count);
2894 /* to get mail list */
2896 EM_DEBUG_LOG_DEV("No mail found...");
2898 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2902 EM_DEBUG_LOG_DEV("There are [%d] mails.", count);
2903 if (!(mail_list_item_from_tbl = (email_mail_list_item_t*)em_malloc(sizeof(email_mail_list_item_t) * count))) {
2904 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
2905 error = EMAIL_ERROR_OUT_OF_MEMORY;
2909 EM_PROFILE_BEGIN(emstorage_query_mail_list_loop);
2910 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >> ");
2911 for (i = 0; i < count; i++) {
2912 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_id), col_index++);
2913 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].account_id), col_index++);
2914 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mailbox_id), col_index++);
2915 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].mailbox_type), col_index++);
2916 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].full_address_from, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2917 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_sender, MAX_EMAIL_ADDRESS_LENGTH, 1, col_index++);
2918 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_recipient, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2919 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].subject, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2920 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].body_download_status), col_index++);
2921 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_size), col_index++);
2922 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_seen_field), col_index++);
2923 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_deleted_field), col_index++);
2924 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_flagged_field), col_index++);
2925 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_answered_field), col_index++);
2926 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_recent_field), col_index++);
2927 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_draft_field), col_index++);
2928 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_forwarded_field), col_index++);
2929 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].DRM_status), col_index++);
2930 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].priority), col_index++);
2931 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].save_status), col_index++);
2932 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].lock_status), col_index++);
2933 _get_table_field_data_int(result, &local_attachment_count, col_index++);
2934 _get_table_field_data_int(result, &local_inline_content_count, col_index++);
2935 _get_table_field_data_time_t(result, &(mail_list_item_from_tbl[i].date_time), col_index++);
2936 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].preview_text, MAX_PREVIEW_TEXT_LENGTH, 1, col_index++);
2937 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_id), col_index++);
2938 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_item_count), col_index++);
2939 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].meeting_request_status), col_index++);
2940 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].message_class), col_index++);
2941 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].smime_type), col_index++);
2942 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].scheduled_sending_time), col_index++);
2943 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].remaining_resend_times), col_index++);
2944 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].tag_id), col_index++);
2945 _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].eas_data_length), col_index++);
2946 _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++);
2948 mail_list_item_from_tbl[i].attachment_count = (local_attachment_count > 0) ? 1 : 0;
2950 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >> ", count);
2951 EM_PROFILE_END(emstorage_query_mail_list_loop);
2957 EM_DEBUG_LOG("MAIL_COUNT [%d]", count);
2960 sqlite3_free_table(result);
2963 *result_count = count;
2966 if (result_mail_list)
2967 *result_mail_list = mail_list_item_from_tbl;
2968 *result_count = count;
2970 EM_SAFE_FREE(mail_list_item_from_tbl);
2973 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2975 // sqlite3_db_release_memory(local_db_handle);
2977 EM_SAFE_FREE(sql_query_string);
2978 EM_SAFE_FREE(date_time_string);
2980 if (err_code != NULL)
2983 EM_PROFILE_END(emstorage_query_mail_list_func);
2984 EM_DEBUG_FUNC_END("ret [%d]", ret);
2989 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)
2991 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);
2993 if (!conditional_clause || !result_mail_tbl || !result_count) {
2994 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: [%p] [%p] [%p]", conditional_clause, result_mail_tbl, result_mail_tbl);
2995 if (err_code != NULL)
2996 *err_code = EMAIL_ERROR_INVALID_PARAM;
3000 int i, col_index = FIELD_COUNT_OF_MAIL_TBL, rc, ret = false, count;
3001 int error = EMAIL_ERROR_NONE;
3002 char **result = NULL, sql_query_string[QUERY_SIZE] = {0, };
3003 emstorage_mail_tbl_t* p_data_tbl = NULL;
3004 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3006 EMSTORAGE_START_READ_TRANSACTION(transaction);
3008 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl %s", conditional_clause);
3010 EM_DEBUG_LOG_DEV("Query[%s]", sql_query_string);
3012 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3013 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3014 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
3017 EM_DEBUG_LOG("No mail found...");
3019 error = EMAIL_ERROR_MAIL_NOT_FOUND;
3023 EM_DEBUG_LOG("There are [%d] mails.", count);
3024 if (!(p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * count))) {
3025 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
3026 error = EMAIL_ERROR_OUT_OF_MEMORY;
3030 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >> ");
3031 for (i = 0; i < count; i++) {
3032 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
3033 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3034 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3035 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_type), col_index++);
3036 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 1, col_index++);
3037 _get_table_field_data_time_t (result, &(p_data_tbl[i].date_time), col_index++);
3038 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
3039 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
3040 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
3041 _get_table_field_data_string(result, &(p_data_tbl[i].message_id), 0, col_index++);
3042 _get_table_field_data_int(result, &(p_data_tbl[i].reference_mail_id), col_index++);
3043 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_from), 1, col_index++);
3044 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_reply), 1, col_index++);
3045 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_to), 1, col_index++);
3046 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_cc), 1, col_index++);
3047 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_bcc), 1, col_index++);
3048 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_return), 1, col_index++);
3049 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_sender), 1, col_index++);
3050 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_recipient), 1, col_index++);
3051 _get_table_field_data_string(result, &(p_data_tbl[i].alias_sender), 1, col_index++);
3052 _get_table_field_data_string(result, &(p_data_tbl[i].alias_recipient), 1, col_index++);
3053 _get_table_field_data_int(result, &(p_data_tbl[i].body_download_status), col_index++);
3054 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
3055 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
3056 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
3057 _get_table_field_data_int(result, &(p_data_tbl[i].mail_size), col_index++);
3058 _get_table_field_data_char (result, &(p_data_tbl[i].flags_seen_field), col_index++);
3059 _get_table_field_data_char (result, &(p_data_tbl[i].flags_deleted_field), col_index++);
3060 _get_table_field_data_char (result, &(p_data_tbl[i].flags_flagged_field), col_index++);
3061 _get_table_field_data_char (result, &(p_data_tbl[i].flags_answered_field), col_index++);
3062 _get_table_field_data_char (result, &(p_data_tbl[i].flags_recent_field), col_index++);
3063 _get_table_field_data_char (result, &(p_data_tbl[i].flags_draft_field), col_index++);
3064 _get_table_field_data_char (result, &(p_data_tbl[i].flags_forwarded_field), col_index++);
3065 _get_table_field_data_int(result, &(p_data_tbl[i].DRM_status), col_index++);
3066 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].priority), col_index++);
3067 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
3068 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
3069 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].report_status), col_index++);
3070 _get_table_field_data_int(result, &(p_data_tbl[i].attachment_count), col_index++);
3071 _get_table_field_data_int(result, &(p_data_tbl[i].inline_content_count), col_index++);
3072 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
3073 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
3074 _get_table_field_data_string(result, &(p_data_tbl[i].preview_text), 1, col_index++);
3075 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].meeting_request_status), col_index++);
3076 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].message_class), col_index++);
3077 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].digest_type), col_index++);
3078 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].smime_type), col_index++);
3079 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].scheduled_sending_time), col_index++);
3080 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].remaining_resend_times), col_index++);
3081 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].tag_id), col_index++);
3082 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].replied_time), col_index++);
3083 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].forwarded_time), col_index++);
3084 _get_table_field_data_string(result, &(p_data_tbl[i].default_charset), 0, col_index++);
3085 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3086 _get_table_field_data_blob(result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3087 _get_table_field_data_string(result, &(p_data_tbl[i].user_name), 0, col_index++);
3094 sqlite3_free_table(result);
3097 *result_mail_tbl = p_data_tbl;
3098 *result_count = count;
3100 *result_mail_tbl = NULL;
3104 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3106 // sqlite3_db_release_memory(local_db_handle);
3108 if (err_code != NULL)
3111 EM_DEBUG_FUNC_END("ret [%d]", ret);
3115 #ifdef __FEATURE_BODY_SEARCH__
3116 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)
3118 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);
3120 if (!conditional_clause || !result_mail_text_tbl || !result_count) { /*prevent 50930*/
3121 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3122 if (err_code != NULL)
3123 *err_code = EMAIL_ERROR_INVALID_PARAM;
3131 int col_index = FIELD_COUNT_OF_MAIL_TEXT_TBL;
3132 int error = EMAIL_ERROR_NONE;
3133 char **result = NULL;
3134 char sql_query_string[QUERY_SIZE] = {0, };
3135 emstorage_mail_text_tbl_t* p_data_tbl = NULL;
3136 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3138 EMSTORAGE_START_READ_TRANSACTION(transaction);
3140 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_text_tbl %s", conditional_clause);
3142 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
3144 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3145 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3146 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3149 EM_DEBUG_LOG("No mail found...");
3151 error = EMAIL_ERROR_MAIL_NOT_FOUND;
3155 EM_DEBUG_LOG("There are [%d] mails.", count);
3156 if (!(p_data_tbl = (emstorage_mail_text_tbl_t *)em_malloc(sizeof(emstorage_mail_text_tbl_t) * count))) {
3157 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_text_tbl_t failed...");
3158 error = EMAIL_ERROR_OUT_OF_MEMORY;
3162 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
3163 for (i = 0; i < count; i++) {
3164 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
3165 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3166 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3167 _get_table_field_data_string(result, &(p_data_tbl[i].body_text), 0, col_index++);
3174 sqlite3_free_table(result);
3177 *result_mail_text_tbl = p_data_tbl;
3178 *result_count = count;
3180 EM_SAFE_FREE(p_data_tbl);
3182 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3184 // sqlite3_db_release_memory(local_db_handle);
3186 if (err_code != NULL)
3189 EM_DEBUG_FUNC_END("ret [%d]", ret);
3194 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)
3196 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);
3202 int error = EMAIL_ERROR_NONE;
3204 int total_count = 0;
3206 char sql_query_string[QUERY_SIZE] = {0, };
3207 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 ";
3208 emstorage_mailbox_tbl_t* p_data_tbl = NULL;
3209 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3211 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
3213 if (input_get_mail_count == 0) { /* without mail count */
3215 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);
3216 } else { /* with read count and total count */
3218 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);
3221 EM_DEBUG_LOG_DEV("query[%s]", sql_query_string);
3223 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
3224 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3225 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string))
3227 EM_DEBUG_LOG_DEV("result count [%d]", count);
3230 EM_DEBUG_LOG_SEC("Can't find mailbox query[%s]", sql_query_string);
3231 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
3235 if ((p_data_tbl = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t) * count)) == NULL) {
3236 EM_DEBUG_EXCEPTION("em_mallocfailed...");
3237 error = EMAIL_ERROR_OUT_OF_MEMORY;
3241 for (i = 0; i < count; i++) {
3242 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3243 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3244 _get_table_field_data_int(result, &(p_data_tbl[i].local_yn), col_index++);
3245 _get_table_field_data_string(result, &(p_data_tbl[i].mailbox_name), 0, col_index++);
3246 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].mailbox_type), col_index++);
3247 _get_table_field_data_string(result, &(p_data_tbl[i].alias), 0, col_index++);
3248 _get_table_field_data_int(result, &(p_data_tbl[i].deleted_flag), col_index++);
3249 _get_table_field_data_int(result, &(p_data_tbl[i].modifiable_yn), col_index++);
3250 _get_table_field_data_int(result, &(p_data_tbl[i].total_mail_count_on_server), col_index++);
3251 _get_table_field_data_int(result, &(p_data_tbl[i].has_archived_mails), col_index++);
3252 _get_table_field_data_int(result, &(p_data_tbl[i].mail_slot_size), col_index++);
3253 _get_table_field_data_int(result, &(p_data_tbl[i].no_select), col_index++);
3254 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].last_sync_time), col_index++);
3255 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3256 _get_table_field_data_blob(result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3258 if (input_get_mail_count == 1) {
3259 _get_table_field_data_int(result, &(total_count), col_index++);
3260 p_data_tbl[i].total_mail_count_on_local = total_count;
3261 _get_table_field_data_int(result, &(read_count), col_index++);
3262 p_data_tbl[i].unread_count = total_count - read_count; /* return unread count, NOT */
3269 sqlite3_free_table(result);
3271 if (error == EMAIL_ERROR_NONE) {
3272 *output_mailbox_list = p_data_tbl;
3273 *output_mailbox_count = count;
3274 EM_DEBUG_LOG("Mailbox Count [%d]", count);
3277 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
3279 // sqlite3_db_release_memory(local_db_handle);
3281 EM_DEBUG_FUNC_END("error [%d]", error);
3285 /* Query series --------------------------------------------------------------*/
3286 INTERNAL_FUNC int emstorage_check_duplicated_account(char *multi_user_name, email_account_t* account, int transaction, int *err_code)
3288 EM_DEBUG_FUNC_BEGIN();
3289 int rc = -1, ret = false;
3290 int error = EMAIL_ERROR_NONE;
3293 char sql_query_string[QUERY_SIZE] = {0, };
3294 sqlite3 *local_db_handle = NULL;
3296 if (account == NULL || account->user_email_address == NULL || account->incoming_server_user_name == NULL || account->incoming_server_address == NULL ||
3297 account->outgoing_server_user_name == NULL || account->outgoing_server_address == NULL) {
3298 if (err_code != NULL)
3299 *err_code = EMAIL_ERROR_INVALID_PARAM;
3303 local_db_handle = emstorage_get_db_connection(multi_user_name);
3305 EMSTORAGE_START_READ_TRANSACTION(transaction);
3307 SNPRINTF(sql_query_string, sizeof(sql_query_string),
3308 "SELECT COUNT(*) FROM mail_account_tbl "
3310 " user_email_address = '%s' AND "
3311 " incoming_server_user_name = '%s' AND "
3312 " incoming_server_type = %d AND "
3313 " incoming_server_address = '%s' AND "
3314 " outgoing_server_user_name = '%s' AND "
3315 " outgoing_server_type = %d AND "
3316 " outgoing_server_address = '%s'; ",
3317 account->user_email_address,
3318 account->incoming_server_user_name, account->incoming_server_type, account->incoming_server_address,
3319 account->outgoing_server_user_name, account->outgoing_server_type, account->outgoing_server_address
3321 EM_DEBUG_LOG_SEC("Query[%s]", sql_query_string);
3322 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
3323 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3324 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3326 count = atoi(result[1]);
3327 sqlite3_free_table(result);
3329 EM_DEBUG_LOG("Count of Duplicated Account Information: [%d]", count);
3331 if (count == 0) { /* not duplicated account */
3333 EM_DEBUG_LOG_SEC("NOT duplicated account: user_email_address[%s]", account->user_email_address);
3334 } else { /* duplicated account */
3336 EM_DEBUG_LOG_SEC("The same account already exists. Duplicated account: user_email_address[%s]", account->user_email_address);
3337 error = EMAIL_ERROR_ALREADY_EXISTS;
3342 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3344 if (err_code != NULL)
3347 EM_DEBUG_FUNC_END("ret [%d]", ret);
3352 INTERNAL_FUNC int emstorage_get_account_count(char *multi_user_name, int *count, int transaction, int *err_code)
3354 EM_DEBUG_FUNC_BEGIN();
3357 if (err_code != NULL)
3358 *err_code = EMAIL_ERROR_INVALID_PARAM;
3362 int rc = -1, ret = false;
3363 int error = EMAIL_ERROR_NONE;
3365 DB_STMT hStmt = NULL;
3366 char sql_query_string[QUERY_SIZE] = {0, };
3369 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3370 EMSTORAGE_START_READ_TRANSACTION(transaction);
3371 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_account_tbl");
3372 EM_DEBUG_LOG_SEC("SQL STMT [%s]", sql_query_string);
3373 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string,
3374 EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3375 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
3376 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3377 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3379 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3380 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3381 ("sqlite3_step fail:%d", rc));
3383 *count = sqlite3_column_int(hStmt, 0);
3389 if (hStmt != NULL) {
3390 rc = sqlite3_finalize(hStmt);
3392 if (rc != SQLITE_OK) {
3393 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
3394 error = EMAIL_ERROR_DB_FAILURE;
3398 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3400 if (err_code != NULL)
3403 EM_DEBUG_FUNC_END("ret [%d]", ret);
3407 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)
3409 EM_DEBUG_FUNC_BEGIN();
3411 int i = 0, count = 0, rc = -1, ret = false;
3412 int field_index = 0;
3414 int error = EMAIL_ERROR_NONE;
3415 emstorage_account_tbl_t *p_data_tbl = NULL;
3416 char sql_query_string[QUERY_SIZE] = {0, };
3417 char *sql = "SELECT count(*) FROM mail_account_tbl;";
3419 sqlite3 *local_db_handle = NULL;
3420 DB_STMT hStmt = NULL;
3422 if (!select_num || !account_list) {
3423 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, account_list);
3424 if (err_code != NULL)
3425 *err_code = EMAIL_ERROR_INVALID_PARAM;
3429 local_db_handle = emstorage_get_db_connection(multi_user_name);
3431 EMSTORAGE_START_READ_TRANSACTION(transaction);
3433 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
3434 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3435 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
3437 count = atoi(result[1]);
3438 sqlite3_free_table(result);
3440 EM_DEBUG_LOG_DEV("count = %d", rc);
3443 EM_DEBUG_LOG("no account found...");
3444 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3448 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
3449 sql_len = EM_SAFE_STRLEN(sql_query_string);
3451 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
3456 "user_data_length, "
3460 "default_mail_slot_size, "
3463 "user_display_name, "
3464 "user_email_address, "
3465 "reply_to_address, "
3467 "incoming_server_type, "
3468 "incoming_server_address, "
3469 "incoming_server_port_number, "
3470 "incoming_server_user_name, "
3471 "incoming_server_password, "
3472 "incoming_server_secure_connection, "
3473 "incoming_server_authentication_method, "
3475 "keep_mails_on_pop_server_after_download, "
3477 "auto_download_size, "
3482 "outgoing_server_type, "
3483 "outgoing_server_address, "
3484 "outgoing_server_port_number, "
3485 "outgoing_server_user_name, "
3486 "outgoing_server_password, "
3487 "outgoing_server_secure_connection, "
3488 "outgoing_server_need_authentication, "
3489 "outgoing_server_use_same_authenticator, "
3492 "req_delivery_receipt, "
3493 "req_read_receipt, "
3497 "display_name_from, "
3499 "forward_with_files, "
3503 "add_my_address_to_bcc, "
3504 "notification_status, "
3506 "display_content_status, "
3507 "default_ringtone_status, "
3508 "alert_ringtone_path, "
3509 "auto_resend_times, "
3510 "outgoing_server_size_limit, "
3511 "wifi_auto_download, "
3513 "incoming_server_requires_apop, "
3515 "certificate_path, "
3520 sql_len = EM_SAFE_STRLEN(sql_query_string);
3522 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " FROM mail_account_tbl ORDER BY account_id");
3524 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3526 EM_DEBUG_LOG_DEV("After sqlite3_prepare_v2 hStmt = %p", hStmt);
3527 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3528 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3530 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3531 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3532 ("sqlite3_step fail:%d", rc));
3533 if (rc == SQLITE_DONE) {
3534 EM_DEBUG_EXCEPTION("no account found...");
3536 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3542 if (!(p_data_tbl = (emstorage_account_tbl_t*)malloc(sizeof(emstorage_account_tbl_t) * count))) {
3543 EM_DEBUG_EXCEPTION("malloc failed...");
3544 error = EMAIL_ERROR_OUT_OF_MEMORY;
3547 memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * count);
3548 for (i = 0; i < count; i++) {
3551 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_id), field_index++);
3552 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].account_name), 0, field_index++);
3553 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].logo_icon_path), 0, field_index++);
3554 _get_stmt_field_data_blob(hStmt, &(p_data_tbl[i].user_data), field_index++);
3555 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].user_data_length), field_index++);
3556 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_svc_id), field_index++);
3557 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_status), field_index++);
3558 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_disabled), field_index++);
3559 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].default_mail_slot_size), field_index++);
3560 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].roaming_option), field_index++);
3561 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].color_label), field_index++);
3562 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_display_name), 0, field_index++);
3563 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_email_address), 0, field_index++);
3564 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].reply_to_address), 0, field_index++);
3565 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].return_address), 0, field_index++);
3566 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].incoming_server_type), field_index++);
3567 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_address), 0, field_index++);
3568 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_port_number), field_index++);
3569 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_user_name), 0, field_index++);
3570 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_password), 0, field_index++);
3571 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_secure_connection), field_index++);
3572 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].incoming_server_authentication_method), field_index++);
3573 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].retrieval_mode), field_index++);
3574 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].keep_mails_on_pop_server_after_download), field_index++);
3575 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].check_interval), field_index++);
3576 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_download_size), field_index++);
3577 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_interval), field_index++);
3578 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_days), field_index++);
3579 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_start_time), field_index++);
3580 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_end_time), field_index++);
3581 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].outgoing_server_type), field_index++);
3582 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_address), 0, field_index++);
3583 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_port_number), field_index++);
3584 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_user_name), 0, field_index++);
3585 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_password), 0, field_index++);
3586 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_secure_connection), field_index++);
3587 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_need_authentication), field_index++);
3588 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_use_same_authenticator), field_index++);
3589 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.priority), field_index++);
3590 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.keep_local_copy), field_index++);
3591 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_delivery_receipt), field_index++);
3592 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_read_receipt), field_index++);
3593 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.download_limit), field_index++);
3594 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_address), field_index++);
3595 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_subject), field_index++);
3596 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.display_name_from), 0, field_index++);
3597 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.reply_with_body), field_index++);
3598 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.forward_with_files), field_index++);
3599 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_myname_card), field_index++);
3600 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_signature), field_index++);
3601 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.signature), 0, field_index++);
3602 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.add_my_address_to_bcc), field_index++);
3603 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.notification_status), field_index++);
3604 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.vibrate_status), field_index++);
3605 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.display_content_status), field_index++);
3606 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.default_ringtone_status), field_index++);
3607 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.alert_ringtone_path), 0, field_index++);
3608 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_resend_times), field_index++);
3609 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_size_limit), field_index++);
3610 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].wifi_auto_download), field_index++);
3611 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].pop_before_smtp), field_index++);
3612 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_requires_apop), field_index++);
3613 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].smime_type), field_index++);
3614 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].certificate_path), 0, field_index++);
3615 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].cipher_type), field_index++);
3616 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].digest_type), field_index++);
3618 /* EAS passwd is not accessible */
3619 if (with_password == true && p_data_tbl[i].incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
3620 /* get password from the secure storage */
3621 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3622 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3624 EM_SAFE_FREE(p_data_tbl[i].incoming_server_password);
3625 EM_SAFE_FREE(p_data_tbl[i].outgoing_server_password);
3627 /* get password file name */
3628 error = _get_password_file_name(multi_user_name, p_data_tbl[i].account_id,
3629 recv_password_file_name,
3630 send_password_file_name);
3631 if (error != EMAIL_ERROR_NONE) {
3632 EM_DEBUG_EXCEPTION("_get_password_file_name error [%d]", error);
3633 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3637 /* read password from secure storage */
3638 error = _read_password_from_secure_storage(recv_password_file_name,
3639 &(p_data_tbl[i].incoming_server_password));
3641 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s] error [%d]",
3642 recv_password_file_name, error);
3643 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3646 error = _read_password_from_secure_storage(send_password_file_name,
3647 &(p_data_tbl[i].outgoing_server_password));
3649 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s] error [%d]",
3650 send_password_file_name, error);
3651 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3656 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3657 EM_DEBUG_LOG_DEV("after sqlite3_step(), i = %d, rc = %d.", i, rc);
3658 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3659 ("sqlite3_step fail:%d", rc));
3666 *account_list = p_data_tbl;
3667 *select_num = count;
3668 EM_DEBUG_LOG_DEV("COUNT : %d", count);
3669 } else if (p_data_tbl != NULL)
3670 emstorage_free_account(&p_data_tbl, count, NULL);
3672 if (hStmt != NULL) {
3673 rc = sqlite3_finalize(hStmt);
3675 if (rc != SQLITE_OK) {
3676 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
3677 error = EMAIL_ERROR_DB_FAILURE;
3681 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3683 if (err_code != NULL)
3686 EM_DEBUG_FUNC_END("ret [%d]", ret);
3690 INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(char *multi_user_name,
3691 char *server_mail_id,
3693 emstorage_mail_tbl_t **mail,
3697 EM_DEBUG_FUNC_BEGIN_SEC("mailbox_id [%d], server_mail_id[%s], mail[%p], transaction[%d], err_code[%p]",
3698 mailbox_id, server_mail_id, mail, transaction, err_code);
3700 int ret = false, error = EMAIL_ERROR_NONE, result_count;
3701 char conditional_clause[QUERY_SIZE] = {0, };
3702 emstorage_mail_tbl_t* p_data_tbl = NULL;
3704 if (!server_mail_id || !mail) {
3705 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3706 error = EMAIL_ERROR_INVALID_PARAM;
3710 sqlite3_snprintf(sizeof(conditional_clause), conditional_clause, "WHERE server_mail_id = '%q'", server_mail_id);
3712 if (mailbox_id > 0) {
3713 SNPRINTF(conditional_clause + strlen(conditional_clause),
3714 QUERY_SIZE - strlen(conditional_clause),
3715 " AND mailbox_id = %d",
3719 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
3720 if (!emstorage_query_mail_tbl(multi_user_name,
3726 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3737 if (err_code != NULL)
3740 EM_DEBUG_FUNC_END("ret [%d]", ret);
3745 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)
3747 int cur_clause = 0, conditional_clause_count = 0, i = 0;
3749 if (account_id < ALL_ACCOUNT) {
3750 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3751 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3755 if (account_id != ALL_ACCOUNT) {
3756 cur_clause += (conditional_clause_count++ == 0) ?
3757 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE account_id = %d", account_id) :
3758 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND account_id = %d", account_id);
3761 if (mailbox_id > 0) {
3762 cur_clause += (conditional_clause_count++ == 0) ?
3763 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_id = %d", mailbox_id) :
3764 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_id = %d", mailbox_id);
3767 if (thread_id > 0) {
3768 cur_clause += (conditional_clause_count++ == 0) ?
3769 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_id = %d ", thread_id) :
3770 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_id = %d ", thread_id);
3771 } else if (thread_id == EMAIL_LIST_TYPE_THREAD) {
3772 cur_clause += (conditional_clause_count++ == 0) ?
3773 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_item_count > 0") :
3774 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_item_count > 0");
3775 } else if (thread_id == EMAIL_LIST_TYPE_LOCAL) {
3776 cur_clause += (conditional_clause_count++ == 0) ?
3777 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE server_mail_status = 0") :
3778 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND server_mail_status = 0");
3779 } else if (thread_id == EMAIL_LIST_TYPE_UNREAD) {
3780 cur_clause += (conditional_clause_count++ == 0) ?
3781 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_seen_field == 0") :
3782 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_seen_field == 0");
3785 /* EM_DEBUG_LOG("where clause added [%s]", conditional_clause_string); */
3786 if (addr_list && addr_list->address_count > 0) {
3787 if (!addr_list->address_type) {
3788 cur_clause += (conditional_clause_count++ == 0) ?
3789 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE email_address_sender IN(\"%s\"", addr_list->address_list[0]) :
3790 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND email_address_sender IN(\"%s\"", addr_list->address_list[0]);
3792 for (i = 1; i < addr_list->address_count; i++)
3793 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3795 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3797 cur_clause += (conditional_clause_count++ == 0) ?
3798 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]) :
3799 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]);
3801 for (i = 1; i < addr_list->address_count; i++)
3802 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3804 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3808 if (input_except_delete_flagged_mails) {
3809 cur_clause += (conditional_clause_count++ == 0) ?
3810 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_deleted_field = 0") :
3811 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_deleted_field = 0");
3815 switch (search_type) {
3816 case EMAIL_SEARCH_FILTER_SUBJECT:
3817 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3818 " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3820 case EMAIL_SEARCH_FILTER_SENDER:
3821 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3822 " %s ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3823 ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3825 case EMAIL_SEARCH_FILTER_RECIPIENT:
3826 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3827 " %s ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3828 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3829 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3830 ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value);
3832 case EMAIL_SEARCH_FILTER_ALL:
3833 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3834 " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\' "
3835 " OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3836 " OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3837 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3838 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3841 ")", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value, search_value, search_value);
3846 /* EM_DEBUG_LOG("where clause [%s]", conditional_clause_string); */
3847 static char sorting_str[][50] = {
3848 " ORDER BY date_time DESC", /* case EMAIL_SORT_DATETIME_HIGH: */
3849 " ORDER BY date_time ASC", /* case EMAIL_SORT_DATETIME_LOW: */
3850 " ORDER BY full_address_from DESC, date_time DESC", /* case EMAIL_SORT_SENDER_HIGH: */
3851 " ORDER BY full_address_from ASC, date_time DESC", /* case EMAIL_SORT_SENDER_LOW: */
3852 " ORDER BY full_address_to DESC, date_time DESC", /* case EMAIL_SORT_RCPT_HIGH: */
3853 " ORDER BY full_address_to ASC, date_time DESC", /* case EMAIL_SORT_RCPT_LOW: */
3854 " ORDER BY subject DESC, date_time DESC", /* case EMAIL_SORT_SUBJECT_HIGH: */
3855 " ORDER BY subject ASC, date_time DESC", /* case EMAIL_SORT_SUBJECT_LOW: */
3856 " ORDER BY priority DESC, date_time DESC", /* case EMAIL_SORT_PRIORITY_HIGH: */
3857 " ORDER BY priority ASC, date_time DESC", /* case EMAIL_SORT_PRIORITY_LOW: */
3858 " ORDER BY attachment_count DESC, date_time DESC", /* case EMAIL_SORT_ATTACHMENT_HIGH: */
3859 " ORDER BY attachment_count ASC, date_time DESC", /* case EMAIL_SORT_ATTACHMENT_LOW: */
3860 " ORDER BY lock_status DESC, date_time DESC", /* case EMAIL_SORT_FAVORITE_HIGH: */
3861 " ORDER BY lock_status ASC, date_time DESC", /* case EMAIL_SORT_FAVORITE_LOW: */
3862 " ORDER BY mailbox_id DESC, date_time DESC", /* case EMAIL_SORT_MAILBOX_ID_HIGH: */
3863 " ORDER BY mailbox_id ASC, date_time DESC", /* case EMAIL_SORT_MAILBOX_ID_LOW: */
3864 " ORDER BY flags_flagged_field DESC, date_time DESC", /* case EMAIL_SORT_FLAGGED_FLAG_HIGH: */
3865 " ORDER BY flags_flagged_field ASC, date_time DESC", /* case EMAIL_SORT_FLAGGED_FLAG_LOW: */
3866 " ORDER BY flags_seen_field DESC, date_time DESC", /* case EMAIL_SORT_SEEN_FLAG_HIGH: */
3867 " ORDER BY flags_seen_field ASC, date_time DESC" /* case EMAIL_SORT_SEEN_FLAG_LOW: */
3870 if (sorting < EMAIL_SORT_END && sorting >= 0)
3871 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " %s", sorting_str[sorting]);
3873 EM_DEBUG_LOG(" Invalid Sorting order ");
3876 if (start_index != -1 && limit_count != -1)
3877 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " LIMIT %d, %d", start_index, limit_count);
3884 * emstorage_get_mail_list - Get the mail list information.
3888 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)
3890 EM_DEBUG_FUNC_BEGIN();
3891 EM_PROFILE_BEGIN(emstorage_get_mail_list_func);
3893 int ret = false, error = EMAIL_ERROR_NONE;
3894 char conditional_clause_string[QUERY_SIZE] = { 0, };
3896 if (account_id < ALL_ACCOUNT) {
3897 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3898 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3900 EM_IF_NULL_RETURN_VALUE(result_count, false);
3902 _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);
3904 EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
3906 if (!emstorage_query_mail_list(multi_user_name, conditional_clause_string, transaction, mail_list, result_count, &error)) {
3907 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
3914 if (err_code != NULL)
3917 EM_PROFILE_END(emstorage_get_mail_list_func);
3918 EM_DEBUG_FUNC_END("ret [%d]", ret);
3924 * emstorage_get_mails - Get the Mail list information based on mailbox_name name
3928 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)
3930 EM_DEBUG_FUNC_BEGIN();
3931 EM_PROFILE_BEGIN(emStorageGetMails);
3933 int count = 0, ret = false, error = EMAIL_ERROR_NONE;
3934 emstorage_mail_tbl_t *p_data_tbl = NULL;
3935 char conditional_clause_string[QUERY_SIZE] = {0, };
3937 EM_IF_NULL_RETURN_VALUE(mail_list, false);
3938 EM_IF_NULL_RETURN_VALUE(result_count, false);
3940 if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3941 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3942 if (err_code != NULL)
3943 *err_code = EMAIL_ERROR_INVALID_PARAM;
3947 _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);
3949 EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
3951 if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause_string, transaction, &p_data_tbl, &count, &error)) {
3952 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3961 *mail_list = p_data_tbl;
3962 *result_count = count;
3963 EM_DEBUG_LOG("COUNT : %d", count);
3964 } else if (p_data_tbl != NULL)
3965 emstorage_free_mail(&p_data_tbl, count, NULL);
3967 if (err_code != NULL)
3970 EM_PROFILE_END(emStorageGetMails);
3971 EM_DEBUG_FUNC_END("ret [%d]", ret);
3978 * emstorage_get_searched_mail_list - Get the mail list information after filtering
3982 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)
3984 EM_DEBUG_FUNC_BEGIN();
3986 int ret = false, error = EMAIL_ERROR_NONE;
3987 char conditional_clause[QUERY_SIZE] = {0, };
3988 char *temp_search_value = NULL;
3989 char *temp_search_value2 = NULL;
3991 EM_IF_NULL_RETURN_VALUE(mail_list, false);
3992 EM_IF_NULL_RETURN_VALUE(result_count, false);
3994 if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3995 EM_DEBUG_EXCEPTION("select_num[%p], Mail_list[%p]", result_count, mail_list);
3996 error = EMAIL_ERROR_INVALID_PARAM;
4000 temp_search_value = em_replace_all_string((char*)search_value, "_", "\\_");
4001 temp_search_value2 = em_replace_all_string(temp_search_value, "%", "\\%");
4003 _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);
4005 EM_DEBUG_LOG("conditional_clause[%s]", conditional_clause);
4007 if (!emstorage_query_mail_list(multi_user_name, conditional_clause, transaction, mail_list, result_count, &error)) {
4008 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
4015 EM_DEBUG_LOG("emstorage_get_searched_mail_list finish off");
4017 if (err_code != NULL)
4020 EM_SAFE_FREE(temp_search_value);
4021 EM_SAFE_FREE(temp_search_value2);
4023 EM_DEBUG_FUNC_END("ret [%d]", ret);
4028 static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name)
4030 EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
4032 if (account_id <= 0 || !recv_password_file_name || !send_password_file_name) {
4033 EM_DEBUG_EXCEPTION("Invalid parameter");
4034 return EMAIL_ERROR_INVALID_PARAM;
4037 EM_DEBUG_LOG("MULTI_USER_NAME : [%s]", multi_user_name);
4039 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
4040 snprintf(recv_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_recv_%s", account_id, multi_user_name);
4041 snprintf(send_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_send_%s", account_id, multi_user_name);
4043 snprintf(recv_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_recv", account_id);
4044 snprintf(send_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_send", account_id);
4047 EM_DEBUG_FUNC_END();
4048 return EMAIL_ERROR_NONE;
4051 static int _read_password_from_secure_storage(char *file_name, char **password)
4053 EM_DEBUG_FUNC_BEGIN_SEC("file_name[%s]", file_name);
4055 if (!file_name || !password) {
4056 EM_DEBUG_EXCEPTION("Invalid Parameter");
4057 return EMAIL_ERROR_INVALID_PARAM;
4060 int ret = EMAIL_ERROR_NONE;
4062 ret = emcore_get_password_in_key_manager(file_name, password);
4063 if (ret != EMAIL_ERROR_NONE) {
4064 EM_DEBUG_EXCEPTION("emcore_get_password_in_key_manager failed : [%d]", ret);
4070 EM_DEBUG_FUNC_END("ret [%d]", ret);
4074 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)
4076 EM_DEBUG_FUNC_BEGIN("account_id[%d], pulloption[%d], account[%p], transaction[%d], err_code[%p]", account_id, pulloption, account, transaction, err_code);
4079 EM_DEBUG_EXCEPTION("account_id[%d], account[%p]", account_id, account);
4080 if (err_code != NULL)
4081 *err_code = EMAIL_ERROR_INVALID_PARAM;
4086 int error = EMAIL_ERROR_NONE;
4087 emstorage_account_tbl_t* p_data_tbl = NULL;
4088 DB_STMT hStmt = NULL;
4089 char sql_query_string[QUERY_SIZE] = {0, };
4092 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4093 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4095 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4097 EMSTORAGE_START_READ_TRANSACTION(transaction);
4099 /* Make query string */
4100 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
4101 sql_len = EM_SAFE_STRLEN(sql_query_string);
4103 if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4104 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4105 "incoming_server_type,"
4106 "incoming_server_address,"
4107 "user_email_address,"
4108 "incoming_server_user_name,"
4110 "incoming_server_port_number,"
4111 "incoming_server_secure_connection,"
4112 "incoming_server_authentication_method,"
4113 "outgoing_server_type,"
4114 "outgoing_server_address,"
4115 "outgoing_server_port_number,"
4116 "outgoing_server_need_authentication,"
4117 "outgoing_server_secure_connection,"
4118 "outgoing_server_user_name,"
4119 "user_display_name,"
4123 "keep_mails_on_pop_server_after_download,"
4124 "auto_download_size,"
4129 "outgoing_server_use_same_authenticator,"
4130 "auto_resend_times,"
4131 "outgoing_server_size_limit,"
4132 "wifi_auto_download,"
4134 "incoming_server_requires_apop,"
4141 sql_len = EM_SAFE_STRLEN(sql_query_string);
4144 if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME) {
4145 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " account_name, ");
4146 sql_len = EM_SAFE_STRLEN(sql_query_string);
4149 /* get from secure storage, not from db */
4150 if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4151 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4154 "req_delivery_receipt,"
4159 "display_name_from,"
4161 "forward_with_files,"
4165 "add_my_address_to_bcc,"
4166 "notification_status,"
4168 "display_content_status,"
4169 "default_ringtone_status,"
4170 "alert_ringtone_path,"
4173 "default_mail_slot_size,"
4180 sql_len = EM_SAFE_STRLEN(sql_query_string);
4183 /* dummy value, FROM WHERE clause */
4184 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "0 FROM mail_account_tbl WHERE account_id = %d", account_id);
4187 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
4189 /* execute a sql and count rows */
4190 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4191 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4192 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4194 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4195 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4196 ("sqlite3_step fail:%d", rc));
4198 if (rc == SQLITE_DONE) {
4199 EM_DEBUG_EXCEPTION("no matched account found...");
4200 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4204 /* Assign query result to structure */
4205 if (!(p_data_tbl = (emstorage_account_tbl_t *)malloc(sizeof(emstorage_account_tbl_t) * 1))) {
4206 EM_DEBUG_EXCEPTION("malloc failed...");
4207 error = EMAIL_ERROR_OUT_OF_MEMORY;
4211 memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * 1);
4214 if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4215 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_type), col_index++);
4216 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_address), 0, col_index++);
4217 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_email_address), 0, col_index++);
4218 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_user_name), 0, col_index++);
4219 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->retrieval_mode), col_index++);
4220 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_port_number), col_index++);
4221 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_secure_connection), col_index++);
4222 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_authentication_method), col_index++);
4223 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->outgoing_server_type), col_index++);
4224 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_address), 0, col_index++);
4225 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_port_number), col_index++);
4226 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_need_authentication), col_index++);
4227 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_secure_connection), col_index++);
4228 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_user_name), 0, col_index++);
4229 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_display_name), 0, col_index++);
4230 _get_stmt_field_data_string(hStmt, &(p_data_tbl->reply_to_address), 0, col_index++);
4231 _get_stmt_field_data_string(hStmt, &(p_data_tbl->return_address), 0, col_index++);
4232 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
4233 _get_stmt_field_data_int(hStmt, &(p_data_tbl->keep_mails_on_pop_server_after_download), col_index++);
4234 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_download_size), col_index++);
4235 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_interval), col_index++);
4236 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_days), col_index++);
4237 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_start_time), col_index++);
4238 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_end_time), col_index++);
4239 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_use_same_authenticator), col_index++);
4240 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_resend_times), col_index++);
4241 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_size_limit), col_index++);
4242 _get_stmt_field_data_int(hStmt, &(p_data_tbl->wifi_auto_download), col_index++);
4243 _get_stmt_field_data_int(hStmt, &(p_data_tbl->pop_before_smtp), col_index++);
4244 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_requires_apop), col_index++);
4245 _get_stmt_field_data_string(hStmt, &(p_data_tbl->logo_icon_path), 0, col_index++);
4246 _get_stmt_field_data_blob(hStmt, &p_data_tbl->user_data, col_index++);
4247 _get_stmt_field_data_int(hStmt, &p_data_tbl->user_data_length, col_index++);
4248 _get_stmt_field_data_int(hStmt, &p_data_tbl->color_label, col_index++);
4249 _get_stmt_field_data_int(hStmt, &(p_data_tbl->check_interval), col_index++);
4250 _get_stmt_field_data_int(hStmt, &(p_data_tbl->sync_status), col_index++);
4253 if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME)
4254 _get_stmt_field_data_string(hStmt, &(p_data_tbl->account_name), 0, col_index++);
4256 if (pulloption & EMAIL_ACC_GET_OPT_PASSWORD) {
4257 /* get password file name */
4258 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) {
4259 EM_DEBUG_EXCEPTION("_get_password_file_name failed [%d]", error);
4263 /* read password from secure storage */
4264 if ((error = _read_password_from_secure_storage(recv_password_file_name, &(p_data_tbl->incoming_server_password))) < 0) {
4265 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4269 EM_DEBUG_LOG_DEV("recv_password_file_name[%s], password[%s]", recv_password_file_name, p_data_tbl->incoming_server_password);
4271 if (p_data_tbl->outgoing_server_use_same_authenticator == 0) {
4272 if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl->outgoing_server_password))) < 0) {
4273 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4276 EM_DEBUG_LOG_DEV("send_password_file_name[%s], password[%s]", send_password_file_name, p_data_tbl->outgoing_server_password);
4280 if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4281 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.priority), col_index++);
4282 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.keep_local_copy), col_index++);
4283 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_delivery_receipt), col_index++);
4284 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_read_receipt), col_index++);
4285 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.download_limit), col_index++);
4286 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_address), col_index++);
4287 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_subject), col_index++);
4288 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.display_name_from), 0, col_index++);
4289 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.reply_with_body), col_index++);
4290 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.forward_with_files), col_index++);
4291 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_myname_card), col_index++);
4292 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_signature), col_index++);
4293 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.signature), 0, col_index++);
4294 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.add_my_address_to_bcc), col_index++);
4295 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.notification_status), col_index++);
4296 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.vibrate_status), col_index++);
4297 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.display_content_status), col_index++);
4298 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.default_ringtone_status), col_index++);
4299 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.alert_ringtone_path), 0, col_index++);
4300 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_svc_id), col_index++);
4301 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->sync_disabled), col_index++);
4302 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->default_mail_slot_size), col_index++);
4303 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->roaming_option), col_index++);
4304 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->smime_type), col_index++);
4305 _get_stmt_field_data_string(hStmt, &(p_data_tbl->certificate_path), 0, col_index++);
4306 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->cipher_type), col_index++);
4307 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->digest_type), col_index++);
4314 *account = p_data_tbl;
4317 emstorage_free_account((emstorage_account_tbl_t **)&p_data_tbl, 1, NULL);
4319 if (hStmt != NULL) {
4320 rc = sqlite3_finalize(hStmt);
4321 if (rc != SQLITE_OK) {
4322 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
4323 error = EMAIL_ERROR_DB_FAILURE;
4327 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4329 if (err_code != NULL)
4332 EM_DEBUG_FUNC_END("ret [%d]", ret);
4336 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)
4338 EM_DEBUG_FUNC_BEGIN("account_id[%d], password_length[%p], err_code[%p]", account_id, password_length, err_code);
4340 if (account_id <= 0 || password_length == NULL) {
4341 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4342 if (err_code != NULL)
4343 *err_code = EMAIL_ERROR_INVALID_PARAM;
4348 int error = EMAIL_ERROR_NONE;
4349 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4350 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4351 char *temp_password = NULL;
4354 /* get password file name */
4355 if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4356 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4360 /* read password from secure storage */
4361 if (password_type == EMAIL_GET_INCOMING_PASSWORD_LENGTH) {
4362 if ((error = _read_password_from_secure_storage(recv_password_file_name, &temp_password)) < 0 || !temp_password) {
4363 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
4366 } else if (password_type == EMAIL_GET_OUTGOING_PASSWORD_LENGTH) {
4367 if ((error = _read_password_from_secure_storage(send_password_file_name, &temp_password)) < 0 || !temp_password) {
4368 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
4372 EM_DEBUG_LOG("Invalid password type");
4373 if (err_code != NULL)
4374 *err_code = EMAIL_ERROR_INVALID_PARAM;
4378 *password_length = EM_SAFE_STRLEN(temp_password);
4380 EM_DEBUG_LOG_SEC("recv_password_file_name[%s], *password_length[%d]", recv_password_file_name, *password_length);
4385 EM_SAFE_FREE(temp_password);
4387 if (err_code != NULL)
4390 EM_DEBUG_FUNC_END("ret [%d]", ret);
4394 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)
4396 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);
4398 int err = EMAIL_ERROR_NONE;
4399 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4400 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4402 if (input_incoming_server_password == NULL && input_outgoing_server_password == NULL) {
4403 EM_DEBUG_EXCEPTION_SEC("Invalid param");
4404 err = EMAIL_ERROR_INVALID_PARAM;
4408 /* get password file name */
4409 if ((err = _get_password_file_name(multi_user_name,
4411 recv_password_file_name,
4412 send_password_file_name)) != EMAIL_ERROR_NONE) {
4413 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4417 EM_DEBUG_LOG_SEC("recv_password_file_name [%s] input_incoming_server_password [%s]",
4418 recv_password_file_name, input_incoming_server_password);
4419 EM_DEBUG_LOG_SEC("send_password_file_name [%s] input_outgoing_server_password [%s]",
4420 send_password_file_name, input_outgoing_server_password);
4422 if (input_incoming_server_password) {
4423 err = emcore_remove_password_in_key_manager(recv_password_file_name);
4424 if (err != EMAIL_ERROR_NONE) {
4425 EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", recv_password_file_name);
4429 /* save recv passwords to the secure storage */
4430 err = emcore_add_password_in_key_manager(recv_password_file_name, input_incoming_server_password);
4431 if (err != EMAIL_ERROR_NONE) {
4432 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4437 if (input_outgoing_server_password) {
4438 err = emcore_remove_password_in_key_manager(send_password_file_name);
4439 if (err != EMAIL_ERROR_NONE) {
4440 EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", send_password_file_name);
4444 /* save send passwords to the secure storage */
4445 err = emcore_add_password_in_key_manager(send_password_file_name, input_outgoing_server_password);
4446 if (err != EMAIL_ERROR_NONE) {
4447 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4453 EM_DEBUG_FUNC_END("err [%d]", err);
4457 INTERNAL_FUNC int emstorage_update_account(char *multi_user_name, int account_id, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
4459 EM_DEBUG_FUNC_BEGIN("account_id[%d], account[%p], transaction[%d], err_code[%p]", account_id, account_tbl, transaction, err_code);
4461 if (account_id < FIRST_ACCOUNT_ID || !account_tbl) {
4462 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4463 if (err_code != NULL)
4464 *err_code = EMAIL_ERROR_INVALID_PARAM;
4468 int error = EMAIL_ERROR_NONE;
4469 int rc, ret = false;
4471 DB_STMT hStmt = NULL;
4472 char sql_query_string[QUERY_SIZE] = {0, };
4474 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4476 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4478 SNPRINTF(sql_query_string, sizeof(sql_query_string),
4479 "UPDATE mail_account_tbl SET"
4481 ", logo_icon_path = ?"
4483 ", user_data_length = ?"
4484 ", account_svc_id = ?"
4486 ", sync_disabled = ?"
4487 ", default_mail_slot_size = ?"
4488 ", roaming_option = ?"
4490 ", user_display_name = ?"
4491 ", user_email_address = ?"
4492 ", reply_to_address = ?"
4493 ", return_address = ?"
4494 ", incoming_server_type = ?"
4495 ", incoming_server_address = ?"
4496 ", incoming_server_port_number = ?"
4497 ", incoming_server_user_name = ?"
4498 ", incoming_server_secure_connection = ?"
4499 ", incoming_server_authentication_method = ?"
4500 ", retrieval_mode = ?"
4501 ", keep_mails_on_pop_server_after_download = ?"
4502 ", check_interval = ?"
4503 ", auto_download_size = ?"
4504 ", peak_interval = ?"
4506 ", peak_start_time = ?"
4507 ", peak_end_time = ?"
4508 ", outgoing_server_type = ?"
4509 ", outgoing_server_address = ?"
4510 ", outgoing_server_port_number = ?"
4511 ", outgoing_server_user_name = ?"
4512 ", outgoing_server_secure_connection = ?"
4513 ", outgoing_server_need_authentication = ?"
4514 ", outgoing_server_use_same_authenticator = ?"
4516 ", keep_local_copy = ?"
4517 ", req_delivery_receipt = ?"
4518 ", req_read_receipt = ?"
4519 ", download_limit = ?"
4520 ", block_address = ?"
4521 ", block_subject = ?"
4522 ", display_name_from = ?"
4523 ", reply_with_body = ?"
4524 ", forward_with_files = ?"
4525 ", add_myname_card = ?"
4526 ", add_signature = ?"
4528 ", add_my_address_to_bcc = ?"
4529 ", notification_status = ?"
4530 ", vibrate_status = ?"
4531 ", display_content_status = ?"
4532 ", default_ringtone_status = ?"
4533 ", alert_ringtone_path = ?"
4534 ", auto_resend_times = ?"
4535 ", outgoing_server_size_limit = ?"
4536 ", wifi_auto_download = ?"
4537 ", pop_before_smtp = ?"
4538 ", incoming_server_requires_apop = ?"
4540 ", certificate_path = ?"
4544 " WHERE account_id = ?");
4546 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4547 EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
4548 EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
4549 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4550 ("sqlite3_prepare fail:(%d) %s", rc, sqlite3_errmsg(local_db_handle)));
4554 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4555 _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4556 _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4557 _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4558 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4559 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4560 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4561 _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4562 _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
4563 _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
4564 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4565 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4566 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4567 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4568 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4569 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4570 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
4571 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4572 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
4573 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
4574 _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
4575 _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
4576 _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
4577 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
4578 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
4579 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
4580 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
4581 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
4582 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
4583 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4584 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
4585 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
4586 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
4587 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
4588 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
4589 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
4590 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
4591 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
4592 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
4593 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
4594 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
4595 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
4596 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4597 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
4598 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
4599 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
4600 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
4601 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
4602 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
4603 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
4604 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
4605 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
4606 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
4607 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4608 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
4609 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
4610 _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
4611 _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
4612 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
4613 _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
4614 _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4615 _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
4616 _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
4617 _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4618 _bind_stmt_field_data_int(hStmt, i++, account_id);
4620 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4621 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
4622 ("sqlite3_step fail:%d", rc));
4623 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4624 ("sqlite3_step fail:%d", rc));
4626 /* validate account existence */
4627 rc = sqlite3_changes(local_db_handle);
4629 EM_DEBUG_EXCEPTION("no matched account found...");
4630 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4634 if (account_tbl->incoming_server_password || account_tbl->outgoing_server_password) {
4635 if ((error = emstorage_update_account_password(multi_user_name, account_id, account_tbl->incoming_server_password, account_tbl->outgoing_server_password)) != EMAIL_ERROR_NONE) {
4636 EM_DEBUG_EXCEPTION("emstorage_update_account_password failed [%d]", error);
4644 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4646 if (hStmt != NULL) {
4647 rc = sqlite3_finalize(hStmt);
4648 if (rc != SQLITE_OK) {
4649 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
4650 error = EMAIL_ERROR_DB_FAILURE;
4654 if (error == EMAIL_ERROR_NONE) {
4655 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_tbl->account_id, 0, NULL, 0))
4656 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_ACCOUNT_UPDATE] : Notification Failed >>> ");
4659 if (err_code != NULL)
4662 EM_DEBUG_FUNC_END("ret [%d]", ret);
4666 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)
4668 EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], field_name[%s], value[%d], transaction[%d]", account_id, field_name, value, transaction);
4669 int error = EMAIL_ERROR_NONE;
4671 char sql_query_string[QUERY_SIZE] = {0, };
4672 sqlite3 *local_db_handle = NULL;
4674 if (!account_id || !field_name) {
4675 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4676 return EMAIL_ERROR_INVALID_PARAM;
4679 local_db_handle = emstorage_get_db_connection(multi_user_name);
4681 /* Write query string */
4682 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_account_tbl SET %s = %d WHERE account_id = %d", field_name, value, account_id);
4684 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4687 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4689 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4690 if (error != EMAIL_ERROR_NONE) {
4691 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4695 if (sqlite3_changes(local_db_handle) == 0)
4696 EM_DEBUG_LOG("no mail matched...");
4700 result = (error == EMAIL_ERROR_NONE) ? true : false;
4701 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, error);
4703 if (error == EMAIL_ERROR_NONE) {
4704 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_id, 0, field_name, value))
4705 EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_ACCOUNT_UPDATE [%s,%d]", field_name, value);
4707 EM_DEBUG_FUNC_END("error [%d]", error);
4711 INTERNAL_FUNC int emstorage_get_sync_status_of_account(char *multi_user_name, int account_id, int *result_sync_status, int *err_code)
4713 EM_DEBUG_FUNC_BEGIN("account_id[%d], result_sync_status [%p], err_code[%p]", account_id, result_sync_status, err_code);
4715 if (!result_sync_status) {
4716 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4717 if (err_code != NULL)
4718 *err_code = EMAIL_ERROR_INVALID_PARAM;
4722 int error = EMAIL_ERROR_NONE, rc, ret = false, sync_status, count, i, col_index;
4723 char sql_query_string[QUERY_SIZE] = {0, };
4724 char **result = NULL;
4725 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4728 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl WHERE account_id = %d", account_id);
4730 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl");
4732 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
4733 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
4734 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4737 EM_DEBUG_EXCEPTION("no matched account found...");
4738 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4743 *result_sync_status = 0;
4745 for (i = 0; i < count; i++) {
4746 _get_table_field_data_int(result, &sync_status, col_index++);
4747 *result_sync_status |= sync_status;
4750 EM_DEBUG_LOG("sync_status [%d]", sync_status);
4752 sqlite3_free_table(result);
4759 if (err_code != NULL)
4762 EM_DEBUG_FUNC_END("ret [%d]", ret);
4766 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)
4768 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);
4770 int error = EMAIL_ERROR_NONE, rc, ret = false, set_value = sync_status, result_sync_status;
4771 char sql_query_string[QUERY_SIZE] = {0, };
4772 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4774 if (set_operator != SET_TYPE_SET && account_id) {
4775 if (!emstorage_get_sync_status_of_account(multi_user_name, account_id, &result_sync_status, &error)) {
4776 EM_DEBUG_EXCEPTION("emstorage_get_sync_status_of_account failed [%d]", error);
4777 if (err_code != NULL)
4781 switch (set_operator) {
4782 case SET_TYPE_UNION:
4783 set_value = result_sync_status | set_value;
4785 case SET_TYPE_MINUS:
4786 set_value = result_sync_status & (~set_value);
4789 EM_DEBUG_EXCEPTION("EMAIL_ERROR_NOT_SUPPORTED [%d]", set_operator);
4790 error = EMAIL_ERROR_NOT_SUPPORTED;
4793 EM_DEBUG_LOG("set_value [%d]", set_value);
4796 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4799 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE account_id = %d", set_value, account_id);
4801 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE incoming_server_type <> 5", set_value);
4803 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4805 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4806 if (error != EMAIL_ERROR_NONE) {
4807 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4811 rc = sqlite3_changes(local_db_handle);
4814 EM_DEBUG_EXCEPTION("no matched account found...");
4815 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4822 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4824 if (error == EMAIL_ERROR_NONE) {
4825 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE_SYNC_STATUS, account_id, set_value, NULL, 0))
4826 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_UPDATE_SYNC_STATUS] : Notification failed");
4829 if (err_code != NULL)
4832 EM_DEBUG_FUNC_END("ret [%d]", ret);
4836 INTERNAL_FUNC int emstorage_add_account(char *multi_user_name, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
4838 EM_DEBUG_FUNC_BEGIN("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4841 EM_DEBUG_EXCEPTION("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4842 if (err_code != NULL)
4843 *err_code = EMAIL_ERROR_INVALID_PARAM;
4849 int rc = -1, ret = false;
4850 int error = EMAIL_ERROR_NONE;
4851 int error_from_ssm = 0;
4852 DB_STMT hStmt = NULL;
4853 char sql_query_string[QUERY_SIZE] = {0, };
4854 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4855 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4857 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4859 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4861 char *sql = "SELECT rowid FROM mail_account_tbl;";
4862 char **result = NULL;
4864 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, &row_count, NULL, NULL), rc);
4865 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
4866 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
4868 if (NULL == result[1]) rc = 1;
4870 for (i = 1; i <= row_count; i++) {
4871 if (i != atoi(result[i])) {
4878 sqlite3_free_table(result);
4881 if (rc < 0 && rc > EMAIL_ACCOUNT_MAX) {
4882 EM_DEBUG_EXCEPTION("OVERFLOWED THE MAX ACCOUNT");
4883 error = EMAIL_ERROR_ACCOUNT_MAX_COUNT;
4887 account_tbl->account_id = rc;
4889 if ((error = _get_password_file_name(multi_user_name, account_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4890 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4894 EM_DEBUG_LOG(" >>>> ACCOUNT_ID [ %d ] ", account_tbl->account_id);
4895 SNPRINTF(sql_query_string, sizeof(sql_query_string),
4896 "INSERT INTO mail_account_tbl VALUES "
4898 " ? " /* account_id */
4899 " , ? " /* account_name */
4900 " , ? " /* logo_icon_path */
4901 " , ? " /* user_data */
4902 " , ? " /* user_data_length */
4903 " , ? " /* account_svc_id */
4904 " , ? " /* sync_status */
4905 " , ? " /* sync_disabled */
4906 " , ? " /* default_mail_slot_size */
4907 " , ? " /* roaming_option */
4908 " , ? " /* color_label */
4909 " , ? " /* user_display_name */
4910 " , ? " /* user_email_address */
4911 " , ? " /* reply_to_address */
4912 " , ? " /* return_address */
4913 " , ? " /* incoming_server_type */
4914 " , ? " /* incoming_server_address */
4915 " , ? " /* incoming_server_port_number */
4916 " , ? " /* incoming_server_user_name */
4917 " , ? " /* incoming_server_password */
4918 " , ? " /* incoming_server_secure_connection */
4919 " , ? " /* incoming_server_authentication_method */
4920 " , ? " /* retrieval_mode */
4921 " , ? " /* keep_mails_on_pop_server_after_download */
4922 " , ? " /* check_interval */
4923 " , ? " /* auto_download_size */
4924 " , ? " /* peak_interval */
4925 " , ? " /* peak_days */
4926 " , ? " /* peak_start_time */
4927 " , ? " /* peak_end_time */
4928 " , ? " /* outgoing_server_type */
4929 " , ? " /* outgoing_server_address */
4930 " , ? " /* outgoing_server_port_number */
4931 " , ? " /* outgoing_server_user_name */
4932 " , ? " /* outgoing_server_password */
4933 " , ? " /* outgoing_server_secure_connection */
4934 " , ? " /* outgoing_server_need_authentication */
4935 " , ? " /* outgoing_server_use_same_authenticator */
4936 " , ? " /* priority */
4937 " , ? " /* keep_local_copy */
4938 " , ? " /* req_delivery_receipt */
4939 " , ? " /* req_read_receipt */
4940 " , ? " /* download_limit */
4941 " , ? " /* block_address */
4942 " , ? " /* block_subject */
4943 " , ? " /* display_name_from */
4944 " , ? " /* reply_with_body */
4945 " , ? " /* forward_with_files */
4946 " , ? " /* add_myname_card */
4947 " , ? " /* add_signature */
4948 " , ? " /* signature */
4949 " , ? " /* add_my_address_to_bcc */
4950 " , ? " /* auto_resend_times */
4951 " , ? " /* outgoing_server_size_limit */
4952 " , ? " /* wifi_auto_download */
4953 " , ? " /* pop_before_smtp */
4954 " , ? " /* incoming_server_requires_apop */
4955 " , ? " /* smime_type */
4956 " , ? " /* certificate_path */
4957 " , ? " /* cipher_type */
4958 " , ? " /* digest_type */
4959 " , ? " /* notification_status */
4960 " , ? " /* vibrate_status */
4961 " , ? " /* display_content_status */
4962 " , ? " /* default_ringtone_status */
4963 " , ? " /* alert_ringtone_path */
4964 " , ? " /* user_name */
4967 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4968 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4969 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4973 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_id);
4974 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4975 _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4976 _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4977 _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4978 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4979 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4980 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4981 _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4982 _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
4983 _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
4984 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4985 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4986 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4987 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4988 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4989 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4990 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
4991 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4992 _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
4993 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
4994 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
4995 _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
4996 _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
4997 _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
4998 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
4999 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
5000 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
5001 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
5002 _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
5003 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
5004 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5005 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
5006 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
5007 _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
5008 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
5009 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
5010 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
5011 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
5012 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
5013 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
5014 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
5015 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
5016 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
5017 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
5018 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
5019 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
5020 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
5021 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
5022 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
5023 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
5024 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
5025 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
5026 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
5027 _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
5028 _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
5029 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
5030 _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
5031 _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
5032 _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
5033 _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
5034 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
5035 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
5036 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
5037 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
5038 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5039 _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5041 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5043 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
5044 ("sqlite3_step fail:%d", rc));
5045 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5046 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
5049 /* save passwords to the secure storage */
5050 EM_DEBUG_LOG_SEC("save to the secure storage : recv_file[%s], send_file[%s]", recv_password_file_name, send_password_file_name);
5051 error = emcore_add_password_in_key_manager(recv_password_file_name, account_tbl->incoming_server_password);
5052 if (error != EMAIL_ERROR_NONE) {
5053 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
5057 error = emcore_add_password_in_key_manager(send_password_file_name, account_tbl->outgoing_server_password);
5058 if (error != EMAIL_ERROR_NONE) {
5059 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
5066 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5068 if (hStmt != NULL) {
5069 rc = sqlite3_finalize(hStmt);
5070 if (rc != SQLITE_OK) {
5071 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5072 error = EMAIL_ERROR_DB_FAILURE;
5076 if (error == EMAIL_ERROR_NONE) {
5077 if (!emcore_notify_storage_event(NOTI_ACCOUNT_ADD, account_tbl->account_id, 0, NULL, 0))
5078 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_ADD] : Notification failed");
5081 if (err_code != NULL)
5084 EM_DEBUG_FUNC_END("ret [%d]", ret);
5088 INTERNAL_FUNC int emstorage_delete_account(char *multi_user_name, int account_id, int transaction, int *err_code)
5090 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
5092 if (account_id < FIRST_ACCOUNT_ID) {
5093 EM_DEBUG_EXCEPTION(" account_id[%d]", account_id);
5095 if (err_code != NULL)
5096 *err_code = EMAIL_ERROR_INVALID_PARAM;
5100 int rc = -1, ret = false;
5101 int error = EMAIL_ERROR_NONE;
5103 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5105 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5107 /* TODO : delete password files - file names can be obtained from db or a rule that makes a name */
5108 char sql_query_string[QUERY_SIZE] = {0, };
5109 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5110 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5112 /* get password file name */
5113 if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
5114 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
5118 /* delete from db */
5119 memset(sql_query_string, 0x00, sizeof(sql_query_string));
5120 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_account_tbl WHERE account_id = %d", account_id);
5121 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5122 if (error != EMAIL_ERROR_NONE) {
5123 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5127 /* validate account existence */
5128 rc = sqlite3_changes(local_db_handle);
5130 EM_DEBUG_EXCEPTION(" no matched account found...");
5131 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
5135 /* delete from secure storage */
5136 error = emcore_remove_password_in_key_manager(recv_password_file_name);
5137 if (error != EMAIL_ERROR_NONE) {
5138 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5142 error = emcore_remove_password_in_key_manager(send_password_file_name);
5143 if (error != EMAIL_ERROR_NONE) {
5144 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5151 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5153 if (err_code != NULL)
5156 EM_DEBUG_FUNC_END("ret [%d]", ret);
5161 INTERNAL_FUNC int emstorage_free_account(emstorage_account_tbl_t** account_list, int count, int *err_code)
5163 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%d], err_code[%p]", account_list, count, err_code);
5166 int error = EMAIL_ERROR_NONE;
5169 if (!account_list || !*account_list) {
5170 EM_DEBUG_EXCEPTION("account_list[%p], count[%d]", account_list, count);
5171 error = EMAIL_ERROR_INVALID_PARAM;
5175 emstorage_account_tbl_t* p = *account_list;
5178 for (; i < count; i++) {
5179 EM_SAFE_FREE(p[i].account_name);
5180 EM_SAFE_FREE(p[i].incoming_server_address);
5181 EM_SAFE_FREE(p[i].user_email_address);
5182 EM_SAFE_FREE(p[i].user_data);
5183 EM_SAFE_FREE(p[i].incoming_server_user_name);
5184 EM_SAFE_FREE(p[i].incoming_server_password);
5185 EM_SAFE_FREE(p[i].outgoing_server_address);
5186 EM_SAFE_FREE(p[i].outgoing_server_user_name);
5187 EM_SAFE_FREE(p[i].outgoing_server_password);
5188 EM_SAFE_FREE(p[i].user_display_name);
5189 EM_SAFE_FREE(p[i].reply_to_address);
5190 EM_SAFE_FREE(p[i].return_address);
5191 EM_SAFE_FREE(p[i].logo_icon_path);
5192 EM_SAFE_FREE(p[i].options.display_name_from);
5193 EM_SAFE_FREE(p[i].options.signature);
5194 EM_SAFE_FREE(p[i].options.alert_ringtone_path);
5195 EM_SAFE_FREE(p[i].certificate_path);
5196 EM_SAFE_FREE(p[i].user_name);
5200 *account_list = NULL;
5206 if (err_code != NULL)
5209 EM_DEBUG_FUNC_END("ret [%d]", ret);
5213 INTERNAL_FUNC int emstorage_get_mailbox_count(char *multi_user_name, int account_id, int local_yn, int *count, int transaction, int *err_code)
5215 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);
5217 if ((account_id < FIRST_ACCOUNT_ID) || (count == NULL)) {
5218 EM_DEBUG_EXCEPTION(" account_list[%d], local_yn[%d], count[%p]", account_id, local_yn, count);
5220 if (err_code != NULL)
5221 *err_code = EMAIL_ERROR_INVALID_PARAM;
5225 int rc = -1, ret = false;
5226 int error = EMAIL_ERROR_NONE;
5227 char sql_query_string[QUERY_SIZE] = {0, };
5229 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5230 EMSTORAGE_START_READ_TRANSACTION(transaction);
5232 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);
5236 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5237 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
5238 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5240 *count = atoi(result[1]);
5241 sqlite3_free_table(result);
5248 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5250 if (err_code != NULL)
5253 EM_DEBUG_FUNC_END("ret [%d]", ret);
5257 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)
5259 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);
5261 if (!select_num || !mailbox_list) {
5262 EM_DEBUG_EXCEPTION("Invalid parameters");
5264 if (err_code != NULL)
5265 *err_code = EMAIL_ERROR_INVALID_PARAM;
5271 int error = EMAIL_ERROR_NONE;
5272 char conditional_clause_string[QUERY_SIZE] = {0, };
5273 char ordering_clause_string[QUERY_SIZE] = {0, };
5275 if (account_id == ALL_ACCOUNT) {
5276 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5277 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);
5279 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d ", account_id);
5280 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5281 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);
5284 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5286 switch (sort_type) {
5287 case EMAIL_MAILBOX_SORT_BY_NAME_ASC:
5288 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name ASC");
5291 case EMAIL_MAILBOX_SORT_BY_NAME_DSC:
5292 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name DESC");
5295 case EMAIL_MAILBOX_SORT_BY_TYPE_ASC:
5296 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type ASC");
5299 case EMAIL_MAILBOX_SORT_BY_TYPE_DSC:
5300 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type DEC");
5304 EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5306 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5307 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5315 if (err_code != NULL)
5318 EM_DEBUG_FUNC_END("ret [%d]", ret);
5322 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)
5324 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);
5327 int error = EMAIL_ERROR_NONE;
5328 int where_clause_count = 0;
5329 char conditional_clause_string[QUERY_SIZE] = {0, };
5330 char ordering_clause_string[QUERY_SIZE] = {0, };
5332 if (!select_num || !mailbox_list) {
5333 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5334 error = EMAIL_ERROR_INVALID_PARAM;
5338 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL) {
5339 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);
5340 where_clause_count++;
5343 if (account_id > 0) {
5344 if (where_clause_count == 0) {
5345 SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " WHERE ");
5346 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);
5348 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);
5351 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY CASE WHEN MBT.mailbox_name"
5352 " GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\'"
5353 " THEN 2 ELSE 1 END ASC,"
5354 " MBT.mailbox_name COLLATE CONVERTUTF8 ASC ");
5355 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5356 EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5358 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 1, 1, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5359 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5367 if (err_code != NULL)
5370 EM_DEBUG_FUNC_END("ret [%d]", ret);
5374 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)
5376 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);
5379 int error = EMAIL_ERROR_NONE;
5380 char conditional_clause_string[QUERY_SIZE] = {0, };
5382 if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list || !parent_mailbox_name) {
5383 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);
5384 if (err_code != NULL)
5385 *err_code = EMAIL_ERROR_INVALID_PARAM;
5389 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string, "WHERE account_id = %d AND UPPER(mailbox_name) LIKE UPPER('%q%%')", account_id, parent_mailbox_name);
5390 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5392 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) {
5393 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5401 if (err_code != NULL)
5404 EM_DEBUG_FUNC_END("ret [%d]", ret);
5408 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)
5410 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);
5411 if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list) {
5412 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5414 if (err_code != NULL)
5415 *err_code = EMAIL_ERROR_INVALID_PARAM;
5421 int error = EMAIL_ERROR_NONE;
5422 char conditional_clause_string[QUERY_SIZE] = {0, };
5424 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND modifiable_yn = 0", account_id);
5425 EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
5427 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) {
5428 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5436 if (err_code != NULL)
5439 EM_DEBUG_FUNC_END("ret [%d]", ret);
5443 INTERNAL_FUNC int emstorage_stamp_last_sync_time_of_mailbox(char *multi_user_name, int input_mailbox_id, int input_transaction)
5445 EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_transaction [%d]", input_mailbox_id, input_transaction);
5447 int result_code = false;
5448 int error = EMAIL_ERROR_NONE;
5449 time_t current_time = 0;
5450 char sql_query_string[QUERY_SIZE] = {0, };
5451 sqlite3 *local_db_handle = NULL;
5453 if (!input_mailbox_id) {
5454 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5455 return EMAIL_ERROR_INVALID_PARAM;
5458 time(¤t_time);
5460 local_db_handle = emstorage_get_db_connection(multi_user_name);
5461 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
5463 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5464 "UPDATE mail_box_tbl SET"
5465 " last_sync_time = %d"
5466 " WHERE mailbox_id = %d"
5468 , input_mailbox_id);
5470 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
5471 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5472 if (error != EMAIL_ERROR_NONE) {
5473 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5479 if (error == EMAIL_ERROR_NONE)
5482 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, result_code, error);
5484 EM_DEBUG_FUNC_END("error [%d]", error);
5488 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)
5490 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);
5491 EM_PROFILE_BEGIN(profile_emstorage_get_mailbox_by_name);
5493 if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !result_mailbox) {
5494 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], mailbox_name[%s], result_mailbox[%p]", account_id, local_yn, mailbox_name, result_mailbox);
5496 if (err_code != NULL)
5497 *err_code = EMAIL_ERROR_INVALID_PARAM;
5502 int error = EMAIL_ERROR_NONE;
5503 int result_count = 0;
5504 char conditional_clause_string[QUERY_SIZE] = {0, };
5506 if (strcmp(mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
5507 if (!(*result_mailbox = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t)))) {
5508 EM_DEBUG_EXCEPTION("malloc failed...");
5509 error = EMAIL_ERROR_OUT_OF_MEMORY;
5513 (*result_mailbox)->mailbox_id = 0;
5514 (*result_mailbox)->account_id = account_id;
5515 (*result_mailbox)->local_yn = 1;
5516 (*result_mailbox)->mailbox_name = EM_SAFE_STRDUP(mailbox_name);
5517 (*result_mailbox)->mailbox_type = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;
5518 (*result_mailbox)->alias = EM_SAFE_STRDUP(mailbox_name);
5519 (*result_mailbox)->deleted_flag = 0;
5520 (*result_mailbox)->modifiable_yn = 1;
5521 (*result_mailbox)->total_mail_count_on_server = 1;
5522 (*result_mailbox)->has_archived_mails = 0;
5523 (*result_mailbox)->mail_slot_size = 0x0FFFFFFF;
5524 (*result_mailbox)->no_select = 0;
5527 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string, "WHERE account_id = %d AND mailbox_name = '%q'", account_id, mailbox_name);
5529 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);
5531 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5533 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5534 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5543 if (err_code != NULL)
5546 EM_PROFILE_END(profile_emstorage_get_mailbox_by_name);
5547 EM_DEBUG_FUNC_END("ret [%d]", ret);
5551 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)
5553 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);
5555 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_USER_DEFINED) || !output_mailbox) {
5556 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_type[%d], output_mailbox[%p]", account_id, mailbox_type, output_mailbox);
5557 if (err_code != NULL)
5558 *err_code = EMAIL_ERROR_INVALID_PARAM;
5563 int error = EMAIL_ERROR_NONE;
5564 int result_count = 0;
5565 emstorage_mailbox_tbl_t *result_mailbox = NULL;
5566 char conditional_clause_string[QUERY_SIZE] = {0,};
5569 SNPRINTF(conditional_clause_string, QUERY_SIZE, "WHERE account_id = %d AND mailbox_type = %d ", account_id, mailbox_type);
5571 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5573 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", true, false, &result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5574 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl error [%d]", error);
5582 *output_mailbox = result_mailbox;
5584 if (err_code != NULL)
5587 EM_DEBUG_FUNC_END("ret [%d]", ret);
5591 INTERNAL_FUNC int emstorage_get_mailbox_by_id(char *multi_user_name, int input_mailbox_id, emstorage_mailbox_tbl_t** output_mailbox)
5593 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], output_mailbox[%p]", input_mailbox_id, output_mailbox);
5595 if (input_mailbox_id <= 0 || !output_mailbox) {
5596 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: input_mailbox_id[%d] output_mailbox[%p]", input_mailbox_id, output_mailbox);
5597 return EMAIL_ERROR_INVALID_PARAM;
5600 int ret = EMAIL_ERROR_NONE;
5601 int result_count = 0;
5602 char conditional_clause_string[QUERY_SIZE] = {0, };
5604 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE MBT.mailbox_id = %d", input_mailbox_id);
5606 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5608 if ((ret = emstorage_query_mailbox_tbl(multi_user_name,
5609 conditional_clause_string,
5610 "", true, false, output_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5611 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", ret);
5617 EM_DEBUG_FUNC_END("ret [%d]", ret);
5621 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)
5623 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);
5625 if (account_id < 0 || !keyword || !result_mailbox) {
5626 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], keyword[%s], result_mailbox[%p]", account_id, keyword, result_mailbox);
5628 if (err_code != NULL)
5629 *err_code = EMAIL_ERROR_INVALID_PARAM;
5634 int error = EMAIL_ERROR_NONE;
5635 char conditional_clause_string[QUERY_SIZE] = {0, };
5637 if (account_id == 0)
5638 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5639 "WHERE alias LIKE \'%%%q%%\'", keyword);
5640 else if (account_id > 0)
5641 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5642 "WHERE account_id = %d AND alias LIKE \'%%%q%%\'", account_id, keyword);
5644 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5646 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, result_count)) != EMAIL_ERROR_NONE) {
5647 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5655 if (err_code != NULL)
5658 EM_DEBUG_FUNC_END("ret [%d]", ret);
5662 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)
5664 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);
5665 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_id) {
5666 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_id[%p]", account_id, mailbox_type, mailbox_id);
5667 if (err_code != NULL)
5668 *err_code = EMAIL_ERROR_INVALID_PARAM;
5672 int rc, ret = false;
5673 int error = EMAIL_ERROR_NONE;
5674 emstorage_account_tbl_t* account = NULL;
5675 DB_STMT hStmt = NULL;
5676 char sql_query_string[QUERY_SIZE] = {0, };
5678 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5679 EMSTORAGE_START_READ_TRANSACTION(transaction);
5681 /* Check whether the account exists. */
5682 if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME, &account, true, &error) || !account) {
5683 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5688 emstorage_free_account(&account, 1, NULL);
5690 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);
5692 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5695 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5697 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5698 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5701 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5702 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5703 ("sqlite3_step fail:%d", rc));
5705 if (rc == SQLITE_DONE) {
5706 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5707 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5711 _get_stmt_field_data_int(hStmt, mailbox_id, 0);
5716 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5717 if (hStmt != NULL) {
5718 rc = sqlite3_finalize(hStmt);
5719 if (rc != SQLITE_OK) {
5720 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5721 error = EMAIL_ERROR_DB_FAILURE;
5725 if (err_code != NULL)
5728 EM_DEBUG_FUNC_END("ret [%d]", ret);
5732 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)
5734 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);
5735 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_name) {
5736 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
5737 if (err_code != NULL)
5738 *err_code = EMAIL_ERROR_INVALID_PARAM;
5742 int rc, ret = false;
5743 int error = EMAIL_ERROR_NONE;
5744 emstorage_account_tbl_t* account = NULL;
5745 DB_STMT hStmt = NULL;
5746 char sql_query_string[QUERY_SIZE] = {0, };
5748 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5749 EMSTORAGE_START_READ_TRANSACTION(transaction);
5751 /* Check whether the account exists. */
5752 if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME, &account, true, &error) || !account) {
5753 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5758 emstorage_free_account(&account, 1, NULL);
5760 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);
5762 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5765 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5767 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5768 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5771 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5772 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5773 ("sqlite3_step fail:%d", rc));
5775 if (rc == SQLITE_DONE) {
5776 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5777 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5781 _get_stmt_field_data_string(hStmt, mailbox_name, 0, 0);
5786 if (hStmt != NULL) {
5787 rc = sqlite3_finalize(hStmt);
5788 if (rc != SQLITE_OK) {
5789 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5790 error = EMAIL_ERROR_DB_FAILURE;
5794 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5796 if (err_code != NULL)
5799 EM_DEBUG_FUNC_END("ret [%d]", ret);
5803 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)
5805 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);
5807 int error = EMAIL_ERROR_NONE;
5808 char sql_query_string[QUERY_SIZE] = {0, };
5809 char *replaced_mailbox_name = NULL;
5812 if (strstr(mailbox_name, "'")) {
5813 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5815 replaced_mailbox_name = strdup(mailbox_name);
5819 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5821 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5823 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5824 "UPDATE mail_box_tbl SET"
5825 " modifiable_yn = %d"
5826 " WHERE account_id = %d"
5827 " AND local_yn = %d"
5828 " AND mailbox_name = '%s'"
5832 , replaced_mailbox_name);
5834 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5835 if (error != EMAIL_ERROR_NONE) {
5836 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5843 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5845 EM_SAFE_FREE(replaced_mailbox_name);
5847 if (err_code != NULL)
5850 EM_DEBUG_FUNC_END("ret [%d]", ret);
5855 INTERNAL_FUNC int emstorage_update_mailbox_total_count(char *multi_user_name,
5857 int input_mailbox_id,
5858 int total_count_on_server,
5862 EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], total_count_on_server[%d], "
5863 "transaction[%d], err_code[%p]",
5864 account_id, input_mailbox_id, total_count_on_server, transaction, err_code);
5867 int error = EMAIL_ERROR_NONE;
5868 char sql_query_string[QUERY_SIZE] = {0, };
5870 if (account_id <= 0 || input_mailbox_id <= 0) {
5871 EM_DEBUG_EXCEPTION("account_id[%d], input_mailbox_id[%d]", account_id, input_mailbox_id);
5872 if (err_code != NULL)
5873 *err_code = EMAIL_ERROR_INVALID_PARAM;
5874 EM_DEBUG_FUNC_END("ret [%d]", ret);
5878 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5880 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5882 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5883 "UPDATE mail_box_tbl SET"
5884 " total_mail_count_on_server = %d"
5885 " WHERE account_id = %d"
5886 " AND mailbox_id = %d"
5887 , total_count_on_server
5889 , input_mailbox_id);
5891 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
5892 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5893 if (error != EMAIL_ERROR_NONE) {
5894 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5902 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5904 if (err_code != NULL)
5907 EM_DEBUG_FUNC_END("ret [%d]", ret);
5912 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)
5914 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);
5916 if (account_id < FIRST_ACCOUNT_ID || input_mailbox_id <= 0 || !result_mailbox) {
5917 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);
5919 if (err_code != NULL)
5920 *err_code = EMAIL_ERROR_INVALID_PARAM;
5921 EM_DEBUG_FUNC_END("ret [%d]", EMAIL_ERROR_INVALID_PARAM);
5925 int rc, ret = false;
5926 int error = EMAIL_ERROR_NONE;
5927 char sql_query_string[QUERY_SIZE] = {0, };
5928 DB_STMT hStmt = NULL;
5931 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5932 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5934 if (local_yn != -1) {
5935 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5936 "UPDATE mail_box_tbl SET"
5938 ", mailbox_name = ?"
5939 ", mailbox_type = ?"
5941 ", deleted_flag = ?"
5942 ", modifiable_yn= ?"
5943 ", mail_slot_size= ?"
5944 ", total_mail_count_on_server = ?"
5945 " WHERE account_id = %d"
5946 " AND local_yn = %d"
5947 " AND mailbox_id = '%d'"
5950 , input_mailbox_id);
5952 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5953 "UPDATE mail_box_tbl SET"
5955 ", mailbox_name = ?"
5956 ", mailbox_type = ?"
5958 ", deleted_flag = ?"
5959 ", modifiable_yn= ?"
5960 ", mail_slot_size= ?"
5961 ", total_mail_count_on_server = ?"
5962 " WHERE account_id = %d"
5963 " AND mailbox_id = '%d'"
5965 , input_mailbox_id);
5970 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5971 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5972 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5974 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_id);
5975 _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);
5976 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_type);
5977 _bind_stmt_field_data_string(hStmt, i++, (char *)result_mailbox->alias ? result_mailbox->alias : "", 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
5978 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->deleted_flag);
5979 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->modifiable_yn);
5980 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mail_slot_size);
5981 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->total_mail_count_on_server);
5984 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5985 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
5986 ("sqlite3_step fail:%d", rc));
5987 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5988 ("sqlite3_step fail:%d", rc));
5993 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5995 if (hStmt != NULL) {
5996 rc = sqlite3_finalize(hStmt);
5997 if (rc != SQLITE_OK) {
5998 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5999 error = EMAIL_ERROR_DB_FAILURE;
6003 if (err_code != NULL)
6006 EM_DEBUG_FUNC_END("ret [%d]", ret);
6010 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)
6012 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);
6014 if (account_id < FIRST_ACCOUNT_ID) {
6015 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
6017 if (err_code != NULL)
6018 *err_code = EMAIL_ERROR_INVALID_PARAM;
6022 int rc, ret = false;
6023 int error = EMAIL_ERROR_NONE;
6024 char sql_query_string[QUERY_SIZE] = {0, };
6026 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6027 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6029 EM_DEBUG_LOG("emstorage_update_mailbox_type");
6031 DB_STMT hStmt_box_tbl = NULL;
6032 DB_STMT hStmt_mail_tbl = NULL;
6035 /* Update mail_box_tbl */
6036 if (local_yn != -1) {
6037 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
6038 "UPDATE mail_box_tbl SET"
6040 " WHERE account_id = %d"
6041 " AND local_yn = %d"
6042 " AND mailbox_id = '%d'"
6045 , input_mailbox_id);
6047 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
6048 "UPDATE mail_box_tbl SET"
6050 " WHERE account_id = %d"
6051 " AND mailbox_id = '%d'"
6053 , input_mailbox_id);
6056 EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
6058 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_box_tbl, NULL), rc);
6060 if (SQLITE_OK != rc) {
6061 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6062 error = EMAIL_ERROR_DB_FAILURE;
6066 _bind_stmt_field_data_int(hStmt_box_tbl, i++, new_mailbox_type);
6069 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_box_tbl), rc);
6071 if (rc == SQLITE_FULL) {
6072 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6073 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
6077 if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
6078 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6079 error = EMAIL_ERROR_DB_FAILURE;
6084 /* Update mail_tbl */
6086 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6087 "UPDATE mail_tbl SET"
6089 " WHERE account_id = %d"
6090 " AND mailbox_id = '%d'"
6092 , input_mailbox_id);
6094 EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
6096 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_mail_tbl, NULL), rc);
6097 if (SQLITE_OK != rc) {
6098 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6099 error = EMAIL_ERROR_DB_FAILURE;
6103 _bind_stmt_field_data_int(hStmt_mail_tbl, i++, new_mailbox_type);
6105 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_mail_tbl), rc);
6106 if (rc == SQLITE_FULL) {
6107 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6108 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
6112 if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
6113 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6114 error = EMAIL_ERROR_DB_FAILURE;
6121 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6123 if (hStmt_box_tbl != NULL) {
6124 rc = sqlite3_finalize(hStmt_box_tbl);
6125 if (rc != SQLITE_OK) {
6126 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6127 error = EMAIL_ERROR_DB_FAILURE;
6131 if (hStmt_mail_tbl != NULL) {
6132 rc = sqlite3_finalize(hStmt_mail_tbl);
6133 if (rc != SQLITE_OK) {
6134 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6135 error = EMAIL_ERROR_DB_FAILURE;
6139 if (err_code != NULL)
6142 EM_DEBUG_FUNC_END("ret [%d]", ret);
6146 INTERNAL_FUNC int emstorage_set_local_mailbox(char *multi_user_name, int input_mailbox_id, int input_is_local_mailbox, int transaction)
6148 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);
6150 int rc, ret = false;
6151 int error = EMAIL_ERROR_NONE;
6152 char sql_query_string[QUERY_SIZE] = {0, };
6154 if (input_mailbox_id < 0) {
6155 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6156 return EMAIL_ERROR_INVALID_PARAM;
6159 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6161 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6163 EM_DEBUG_LOG("emstorage_update_mailbox_type");
6165 DB_STMT hStmt = NULL;
6168 /* Update mail_box_tbl */
6169 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6170 "UPDATE mail_box_tbl SET"
6172 " WHERE mailbox_id = %d"
6173 , input_mailbox_id);
6175 EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
6177 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6178 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6179 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6181 _bind_stmt_field_data_int(hStmt, i++, input_is_local_mailbox);
6183 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6184 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
6185 ("sqlite3_step fail:%d", rc));
6186 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6187 ("sqlite3_step fail:%d", rc));
6189 if (hStmt != NULL) {
6190 rc = sqlite3_finalize(hStmt);
6191 if (rc != SQLITE_OK) {
6192 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6193 error = EMAIL_ERROR_DB_FAILURE;
6201 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6203 if (hStmt != NULL) {
6204 rc = sqlite3_finalize(hStmt);
6205 if (rc != SQLITE_OK) {
6206 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6207 error = EMAIL_ERROR_DB_FAILURE;
6211 EM_DEBUG_FUNC_END("error [%d]", error);
6215 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)
6217 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);
6219 int err = EMAIL_ERROR_NONE;
6221 int cur_mailbox_id_string = 0;
6222 int mailbox_id_string_buffer_length = 0;
6223 char sql_query_string[QUERY_SIZE] = {0, };
6224 char *mailbox_id_string_buffer = NULL;
6225 char *parameter_string = NULL;
6226 sqlite3 *local_db_handle = NULL;
6228 if (input_mailbox_id_array == NULL || input_mailbox_id_count == 0 || input_field_name == NULL) {
6229 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6230 return EMAIL_ERROR_INVALID_PARAM;
6233 local_db_handle = emstorage_get_db_connection(multi_user_name);
6235 /* Generating mail id list string */
6236 mailbox_id_string_buffer_length = MAILBOX_ID_STRING_LENGTH * input_mailbox_id_count;
6238 mailbox_id_string_buffer = em_malloc(mailbox_id_string_buffer_length);
6240 if (!mailbox_id_string_buffer) {
6241 EM_DEBUG_EXCEPTION("em_mallocfailed");
6242 err = EMAIL_ERROR_OUT_OF_MEMORY;
6246 for (i = 0; i < input_mailbox_id_count; i++)
6247 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]);
6249 if (EM_SAFE_STRLEN(mailbox_id_string_buffer) > 1)
6250 mailbox_id_string_buffer[EM_SAFE_STRLEN(mailbox_id_string_buffer) - 1] = NULL_CHAR;
6252 /* Generating notification parameter string */
6253 parameter_string = em_malloc(mailbox_id_string_buffer_length + EM_SAFE_STRLEN(input_field_name) + 2);
6255 if (!parameter_string) {
6256 EM_DEBUG_EXCEPTION("em_mallocfailed");
6257 err = EMAIL_ERROR_OUT_OF_MEMORY;
6261 SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", input_field_name, 0x01, mailbox_id_string_buffer);
6263 /* Write query string */
6264 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);
6266 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
6269 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
6270 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6271 if (err != EMAIL_ERROR_NONE) {
6272 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
6276 if (sqlite3_changes(local_db_handle) == 0)
6277 EM_DEBUG_LOG("no mail matched...");
6282 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, err);
6284 if (err == EMAIL_ERROR_NONE && parameter_string) {
6285 if (!emcore_notify_storage_event(NOTI_MAILBOX_FIELD_UPDATE, input_account_id, 0, parameter_string, input_value))
6286 EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_MAILBOX_FIELD_UPDATE [%s,%d]",
6287 input_field_name, input_value);
6290 EM_SAFE_FREE(mailbox_id_string_buffer);
6291 EM_SAFE_FREE(parameter_string);
6293 EM_DEBUG_FUNC_END("err [%d]", err);
6297 INTERNAL_FUNC int emstorage_add_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox_tbl, int transaction, int *err_code)
6299 EM_DEBUG_FUNC_BEGIN("mailbox_tbl[%p], transaction[%d], err_code[%p]", mailbox_tbl, transaction, err_code);
6302 if (err_code != NULL)
6303 *err_code = EMAIL_ERROR_INVALID_PARAM;
6307 int rc, ret = false;
6308 int error = EMAIL_ERROR_NONE;
6309 DB_STMT hStmt = NULL;
6310 char sql_query_string[QUERY_SIZE] = {0,};
6311 char **result = NULL;
6312 time_t current_time;
6313 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6314 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6316 EM_SAFE_STRCPY(sql_query_string, "SELECT max(rowid) FROM mail_box_tbl;");
6318 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6319 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6320 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6322 time(¤t_time);
6324 if (NULL == result[1])
6327 rc = atoi(result[1]) + 1;
6328 sqlite3_free_table(result);
6330 memset(sql_query_string, 0, sizeof(char) * QUERY_SIZE);
6332 mailbox_tbl->mailbox_id = rc;
6334 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6335 "INSERT INTO mail_box_tbl VALUES "
6336 "(?" /* mailbox_id */
6337 ", ?" /* account_id */
6338 ", ?" /* local_yn */
6339 ", ?" /* mailbox_name */
6340 ", ?" /* mailbox_type */
6342 ", ?" /* deleted_flag */
6343 ", ?" /* modifiable_yn */
6344 ", ?" /* total_mail_count_on_server */
6345 ", ?" /* has_archived_mails */
6346 ", ?" /* mail_slot_size */
6347 ", ?" /* no_select */
6348 ", ?" /* last_sync_time */
6349 ", ?" /* eas_data_length */
6350 ", ?" /* eas_data */
6354 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6355 EM_DEBUG_LOG_DEV("After sqlite3_prepare hStmt = %p", hStmt);
6356 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6357 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6361 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_id);
6362 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->account_id);
6363 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->local_yn);
6364 _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
6365 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_type);
6366 _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->alias, 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
6367 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->deleted_flag);
6368 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->modifiable_yn);
6369 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->total_mail_count_on_server);
6370 _bind_stmt_field_data_int(hStmt, col_index++, 0);
6371 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mail_slot_size);
6372 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->no_select);
6373 _bind_stmt_field_data_int(hStmt, col_index++, current_time);
6374 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->eas_data_length);
6375 _bind_stmt_field_data_blob(hStmt, col_index++, (void*)mailbox_tbl->eas_data, mailbox_tbl->eas_data_length);
6378 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6379 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6380 ("sqlite3_step fail:%dn", rc));
6385 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6386 if (hStmt != NULL) {
6387 rc = sqlite3_finalize(hStmt);
6388 if (rc != SQLITE_OK) {
6389 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6390 error = EMAIL_ERROR_DB_FAILURE;
6394 if (error == EMAIL_ERROR_NONE) {
6395 if (!emcore_notify_storage_event(NOTI_MAILBOX_ADD, mailbox_tbl->account_id, mailbox_tbl->mailbox_id,
6396 mailbox_tbl->mailbox_name, mailbox_tbl->mailbox_type))
6397 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6400 if (err_code != NULL)
6403 EM_DEBUG_FUNC_END("ret [%d]", ret);
6407 INTERNAL_FUNC int emstorage_set_all_mailbox_modifiable_yn(char *multi_user_name, int account_id, int modifiable_yn, int transaction, int *err_code)
6409 EM_DEBUG_FUNC_BEGIN("account_id[%d], modifiable_yn[%d], err_code[%p]", account_id, modifiable_yn, err_code);
6411 if (account_id < FIRST_ACCOUNT_ID) {
6413 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
6415 if (err_code != NULL)
6416 *err_code = EMAIL_ERROR_INVALID_PARAM;
6419 int rc, ret = false;
6420 int error = EMAIL_ERROR_NONE;
6421 char sql_query_string[QUERY_SIZE] = {0,};
6422 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6423 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6426 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET modifiable_yn = %d WHERE account_id = %d", modifiable_yn, account_id);
6428 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6429 if (error != EMAIL_ERROR_NONE) {
6430 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6434 rc = sqlite3_changes(local_db_handle);
6436 EM_DEBUG_EXCEPTION("All mailbox_name modifiable_yn set to 0 already");
6442 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6444 if (err_code != NULL)
6446 EM_DEBUG_FUNC_END("ret [%d]", ret);
6452 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)
6454 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);
6456 if (account_id < FIRST_ACCOUNT_ID) {
6457 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
6459 if (err_code != NULL)
6460 *err_code = EMAIL_ERROR_INVALID_PARAM;
6464 int rc, ret = false;
6465 int error = EMAIL_ERROR_NONE;
6466 char sql_query_string[QUERY_SIZE] = {0, };
6467 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6468 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6471 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_box_tbl WHERE account_id = %d ", account_id);
6473 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);
6475 if (input_mailbox_id > 0) { /* 0 means all mailbox */
6476 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);
6479 EM_DEBUG_LOG_SEC("mailbox sql_query_string [%s]", sql_query_string);
6480 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6481 if (error != EMAIL_ERROR_NONE) {
6482 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6486 rc = sqlite3_changes(local_db_handle);
6488 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
6489 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
6496 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6498 if (error == EMAIL_ERROR_NONE) {
6499 if (!emcore_notify_storage_event(NOTI_MAILBOX_DELETE, account_id, input_mailbox_id, NULL, 0))
6500 EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6503 if (err_code != NULL)
6506 EM_DEBUG_FUNC_END("ret [%d]", ret);
6510 INTERNAL_FUNC int emstorage_free_mailbox(emstorage_mailbox_tbl_t** mailbox_list, int count, int *err_code)
6512 EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d], err_code[%p]", mailbox_list, count, err_code);
6515 int error = EMAIL_ERROR_NONE;
6518 if (!mailbox_list || !*mailbox_list) {
6519 EM_DEBUG_EXCEPTION(" mailbox_list[%p], count[%d]", mailbox_list, count);
6521 error = EMAIL_ERROR_INVALID_PARAM;
6525 emstorage_mailbox_tbl_t* p = *mailbox_list;
6528 for (; i < count; i++) {
6529 EM_SAFE_FREE(p[i].mailbox_name);
6530 EM_SAFE_FREE(p[i].alias);
6531 EM_SAFE_FREE(p[i].eas_data); /*valgrind*/
6534 EM_SAFE_FREE(p); *mailbox_list = NULL;
6540 if (err_code != NULL)
6543 EM_DEBUG_FUNC_END("ret [%d]", ret);
6547 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)
6549 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);
6551 if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !count) {
6552 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], count[%p], exist[%p]", account_id, mailbox_name, count);
6554 if (err_code != NULL)
6555 *err_code = EMAIL_ERROR_INVALID_PARAM;
6559 int rc = -1, ret = false;
6560 int error = EMAIL_ERROR_NONE;
6561 char sql_query_string[QUERY_SIZE] = {0, };
6562 char *replaced_mailbox_name = NULL;
6564 if (strstr(mailbox_name, "'")) {
6565 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6567 replaced_mailbox_name = EM_SAFE_STRDUP(mailbox_name);
6570 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6573 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6574 EMSTORAGE_START_READ_TRANSACTION(transaction);
6575 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);
6576 EM_DEBUG_LOG_SEC(">>> SQL [ %s ] ", sql_query_string);
6580 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6581 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6582 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6584 *count = atoi(result[1]);
6585 sqlite3_free_table(result);
6590 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6592 EM_SAFE_FREE(replaced_mailbox_name);
6594 if (err_code != NULL)
6597 EM_DEBUG_FUNC_END("ret [%d]", ret);
6601 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)
6603 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);
6605 if (account_id < FIRST_ACCOUNT_ID || !uid || !exist) {
6606 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], uid[%p], exist[%p]", account_id, mailbox_name , uid, exist);
6608 if (err_code != NULL)
6609 *err_code = EMAIL_ERROR_INVALID_PARAM;
6613 int rc = -1, ret = false;
6614 int error = EMAIL_ERROR_NONE;
6615 char sql_query_string[QUERY_SIZE] = {0, };
6616 char *replaced_mailbox_name = NULL;
6618 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6620 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6621 EMSTORAGE_START_READ_TRANSACTION(transaction);
6624 if (strstr(mailbox_name, "'")) {
6625 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6627 replaced_mailbox_name = strdup(mailbox_name);
6630 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);
6632 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);
6635 char **result = NULL;
6637 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6638 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6639 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6641 *exist = atoi(result[1]);
6642 sqlite3_free_table(result);
6652 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6654 EM_SAFE_FREE(replaced_mailbox_name);
6656 if (err_code != NULL)
6659 EM_DEBUG_FUNC_END("ret [%d]", ret);
6663 INTERNAL_FUNC int emstorage_get_downloaded_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code)
6665 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], err_code[%p]", mail_id, mail, err_code);
6667 if (!mail || mail_id <= 0) {
6668 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
6669 if (err_code != NULL)
6670 *err_code = EMAIL_ERROR_INVALID_PARAM;
6674 int rc, ret = false;
6675 int error = EMAIL_ERROR_NONE;
6676 DB_STMT hStmt = NULL;
6677 char sql_query_string[QUERY_SIZE] = {0, };
6679 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6680 EMSTORAGE_START_READ_TRANSACTION(transaction);
6682 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE local_uid = %d", mail_id);
6684 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6685 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6687 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6688 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6691 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6692 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6693 ("sqlite3_step fail:%d", rc));
6695 *mail = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
6696 if (*mail == NULL) {
6697 error = EMAIL_ERROR_OUT_OF_MEMORY;
6698 EM_DEBUG_EXCEPTION("Memory allocation for mail failed.");
6702 memset(*mail, 0x00, sizeof(emstorage_mail_tbl_t));
6704 _get_stmt_field_data_int(hStmt, &((*mail)->account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6705 _get_stmt_field_data_int(hStmt, &((*mail)->mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6706 _get_stmt_field_data_string(hStmt, &((*mail)->server_mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6707 _get_stmt_field_data_int(hStmt, &((*mail)->mail_id), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6708 _get_stmt_field_data_string(hStmt, &((*mail)->server_mail_id), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6709 _get_stmt_field_data_int(hStmt, &((*mail)->mail_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6710 _get_stmt_field_data_char(hStmt, &((*mail)->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6712 (*mail)->server_mail_status = 1;
6718 if (hStmt != NULL) {
6719 rc = sqlite3_finalize(hStmt);
6720 if (rc != SQLITE_OK) {
6721 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6722 error = EMAIL_ERROR_DB_FAILURE;
6726 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6728 if (err_code != NULL)
6731 EM_DEBUG_FUNC_END("ret [%d]", ret);
6735 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)
6737 EM_PROFILE_BEGIN(emStorageGetDownloadList);
6738 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);
6739 if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid || !count) {
6740 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_id[%s], read_mail_uid[%p], count[%p]", account_id, mailbox_id, read_mail_uid, count);
6742 if (err_code != NULL)
6743 *err_code = EMAIL_ERROR_INVALID_PARAM;
6747 int rc, ret = false;
6748 int error = EMAIL_ERROR_NONE;
6750 DB_STMT hStmt = NULL;
6751 char sql_query_string[QUERY_SIZE] = {0, };
6753 emstorage_read_mail_uid_tbl_t* p_data_tbl = NULL;
6756 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6757 EMSTORAGE_START_READ_TRANSACTION(transaction);
6760 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);
6762 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
6764 EM_DEBUG_LOG_SEC(" sql_query_string : %s", sql_query_string);
6768 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6769 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6770 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6771 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6774 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6775 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6776 ("sqlite3_step fail:%d", rc));
6779 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL); */
6780 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL), rc);
6781 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6782 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6784 sqlite3_free_table(result);
6786 EM_DEBUG_LOG("No mail found in mail_read_mail_uid_tbl");
6792 if (!(p_data_tbl = (emstorage_read_mail_uid_tbl_t*)malloc(sizeof(emstorage_read_mail_uid_tbl_t) * *count))) {
6793 EM_DEBUG_EXCEPTION(" malloc failed...");
6794 error = EMAIL_ERROR_OUT_OF_MEMORY;
6798 memset(p_data_tbl, 0x00, sizeof(emstorage_read_mail_uid_tbl_t)*(*count));
6800 for (i = 0; i < *count; ++i) {
6801 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6802 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6803 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6804 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].local_uid), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6805 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].server_uid), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6806 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rfc822_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6807 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6808 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_flagged_field), FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6810 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6811 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6812 ("sqlite3_step fail:%d", rc));
6819 *read_mail_uid = p_data_tbl;
6820 else if (p_data_tbl)
6821 emstorage_free_read_mail_uid(&p_data_tbl, *count, NULL);
6823 if (hStmt != NULL) {
6824 rc = sqlite3_finalize(hStmt);
6825 if (rc != SQLITE_OK) {
6826 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6827 error = EMAIL_ERROR_DB_FAILURE;
6831 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6833 if (err_code != NULL)
6836 EM_PROFILE_END(emStorageGetDownloadList);
6837 EM_DEBUG_FUNC_END("ret [%d]", ret);
6841 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)
6843 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);
6845 if (account_id < FIRST_ACCOUNT_ID || !mail_size) {
6846 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);
6848 if (err_code != NULL)
6849 *err_code = EMAIL_ERROR_INVALID_PARAM;
6853 int rc, ret = false;
6854 int error = EMAIL_ERROR_NONE;
6855 DB_STMT hStmt = NULL;
6856 char sql_query_string[QUERY_SIZE] = {0, };
6857 char *replaced_mailbox_name = NULL;
6859 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6860 EMSTORAGE_START_READ_TRANSACTION(transaction);
6863 if (strstr(mailbox_name, "'")) {
6864 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6866 replaced_mailbox_name = strdup(mailbox_name);
6869 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6871 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6872 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6873 "WHERE account_id = %d "
6874 "AND mailbox_id = '%s' "
6875 "AND local_uid = %d "
6876 "AND mailbox_name = '%s' "
6877 "AND server_uid = '%s'",
6878 account_id, mailbox_id, local_uid, replaced_mailbox_name, uid);
6880 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6881 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6882 "WHERE account_id = %d "
6883 "AND mailbox_id = '%s' "
6884 "AND local_uid = %d "
6885 "AND server_uid = '%s'",
6886 account_id, mailbox_id, local_uid, uid);
6890 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6891 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6892 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6895 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6896 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6897 ("sqlite3_step fail:%d", rc));
6899 if (rc == SQLITE_DONE) {
6900 EM_DEBUG_LOG("no matched mail found....");
6901 error = EMAIL_ERROR_MAIL_NOT_FOUND;
6905 _get_stmt_field_data_int(hStmt, mail_size, 0);
6910 EM_SAFE_FREE(replaced_mailbox_name);
6912 if (hStmt != NULL) {
6913 rc = sqlite3_finalize(hStmt);
6914 if (rc != SQLITE_OK) {
6915 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6916 error = EMAIL_ERROR_DB_FAILURE;
6920 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6922 if (err_code != NULL)
6925 EM_DEBUG_FUNC_END("ret [%d]", ret);
6929 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)
6931 EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], transaction[%d], err_code[%p]", read_mail_uid, transaction, err_code);
6933 if (!read_mail_uid) {
6934 EM_DEBUG_EXCEPTION("read_mail_uid[%p]", read_mail_uid);
6935 if (err_code != NULL)
6936 *err_code = EMAIL_ERROR_INVALID_PARAM;
6940 int rc, rc2, ret = false;
6941 int error = EMAIL_ERROR_NONE;
6942 DB_STMT hStmt = NULL;
6943 char sql_query_string[QUERY_SIZE] = {0, };
6945 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6946 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6948 char *sql = "SELECT max(rowid) FROM mail_read_mail_uid_tbl;";
6949 char **result = NULL;
6952 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6953 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6954 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6956 if (NULL == result[1]) rc = 1;
6957 else rc = atoi(result[1])+1;
6958 sqlite3_free_table(result);
6960 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6961 "INSERT INTO mail_read_mail_uid_tbl VALUES "
6962 "(?" /* account_id */
6963 ", ?" /* mailbox_id */
6964 ", ?" /* mailbox_name */
6965 ", ?" /* local_uid */
6966 ", ?" /* server_uid */
6967 ", ?" /* rfc822_size */
6968 ", ?" /* sync_status */
6969 ", ?" /* flags_seen_field */
6970 ", ?" /* flags_flagged_field */
6974 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
6975 if (rc2 != SQLITE_OK) {
6976 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6977 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6979 error = EMAIL_ERROR_DB_FAILURE;
6983 EM_DEBUG_LOG("account_id[%d] mailbox_id[%d] local_uid [%d]"
6984 "server_uid[%s] rfc822_size[%d] rc[%d]",
6985 read_mail_uid->account_id, read_mail_uid->mailbox_id, read_mail_uid->local_uid,
6986 read_mail_uid->server_uid, read_mail_uid->rfc822_size, rc);
6988 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->account_id);
6989 _bind_stmt_field_data_int(hStmt, LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->mailbox_id);
6990 _bind_stmt_field_data_int(hStmt, LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->local_uid);
6991 _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);
6992 _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);
6993 _bind_stmt_field_data_int(hStmt, RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->rfc822_size);
6994 _bind_stmt_field_data_int(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_seen_field);
6995 _bind_stmt_field_data_int(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_flagged_field);
6996 _bind_stmt_field_data_int(hStmt, IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, rc);
6999 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7000 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
7001 ("sqlite3_step fail:%d", rc));
7002 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7003 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
7009 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7010 if (hStmt != NULL) {
7011 rc = sqlite3_finalize(hStmt);
7012 if (rc != SQLITE_OK) {
7013 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7014 error = EMAIL_ERROR_DB_FAILURE;
7018 if (err_code != NULL)
7021 EM_DEBUG_FUNC_END("ret [%d]", ret);
7025 #ifdef __FEATURE_BODY_SEARCH__
7026 INTERNAL_FUNC int emstorage_add_mail_text(char *multi_user_name, emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
7028 EM_DEBUG_FUNC_BEGIN("mail_text[%p], transaction[%d], err_code[%p]", mail_text, transaction, err_code);
7031 EM_DEBUG_EXCEPTION("mail_text[%p]", mail_text);
7032 if (err_code != NULL)
7033 *err_code = EMAIL_ERROR_INVALID_PARAM;
7037 int rc, rc2, ret = false;
7038 int error = EMAIL_ERROR_NONE;
7039 DB_STMT hStmt = NULL;
7040 char sql_query_string[QUERY_SIZE] = {0, };
7042 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7043 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7045 char *sql = "SELECT max(rowid) FROM mail_text_tbl;";
7046 char **result = NULL;
7048 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7049 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7050 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7051 sqlite3_free_table(result);
7053 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7054 "INSERT INTO mail_text_tbl VALUES "
7060 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
7061 if (rc2 != SQLITE_OK) {
7062 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7063 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc2, sqlite3_errmsg(local_db_handle));
7065 error = EMAIL_ERROR_DB_FAILURE;
7069 EM_DEBUG_LOG("mail_id[%d] account_id[%d] mailbox_id[%d]", mail_text->mail_id,
7070 mail_text->account_id, mail_text->mailbox_id);
7071 EM_DEBUG_LOG_DEV("body_text VALUE [%s] ", mail_text->body_text);
7073 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mail_id);
7074 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->account_id);
7075 _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mailbox_id);
7076 _bind_stmt_field_data_string(hStmt, BODY_TEXT_IDX_IN_MAIL_TEXT_TBL, (char *)mail_text->body_text, 0, -1);
7078 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7079 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
7080 ("sqlite3_step fail:%d", rc));
7081 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7082 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
7087 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7088 if (hStmt != NULL) {
7089 rc = sqlite3_finalize(hStmt);
7090 if (rc != SQLITE_OK) {
7091 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7092 error = EMAIL_ERROR_DB_FAILURE;
7096 if (err_code != NULL)
7099 EM_DEBUG_FUNC_END("ret [%d]", ret);
7104 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)
7106 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);
7108 if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid) {
7109 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);
7111 if (err_code != NULL)
7112 *err_code = EMAIL_ERROR_INVALID_PARAM;
7116 int rc, ret = false;
7117 int error = EMAIL_ERROR_NONE;
7118 DB_STMT hStmt = NULL;
7119 char sql_query_string[QUERY_SIZE] = {0, };
7121 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7123 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7125 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7126 "UPDATE mail_read_mail_uid_tbl SET"
7129 ", mailbox_name = ?"
7133 ", flags_seen_field = ?"
7134 ", flags_flagged_field = ?"
7135 " WHERE account_id = ?"
7136 " AND mailbox_id = ?"
7137 " AND local_uid = ?"
7138 " AND mailbox_name= ?"
7139 " AND server_uid = ?");
7142 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7143 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7144 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7145 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7150 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->account_id);
7151 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->mailbox_id);
7152 _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7153 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->local_uid);
7154 _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->server_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7155 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->rfc822_size);
7156 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_seen_field);
7157 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_flagged_field);
7158 _bind_stmt_field_data_int(hStmt, i++, account_id);
7159 _bind_stmt_field_data_int(hStmt, i++, mailbox_id);
7160 _bind_stmt_field_data_int(hStmt, i++, local_uid);
7161 _bind_stmt_field_data_string(hStmt, i++, (char*)mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7162 _bind_stmt_field_data_string(hStmt, i++, (char*)uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7165 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7166 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7167 ("sqlite3_step fail:%d", rc));
7172 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7173 if (hStmt != NULL) {
7174 rc = sqlite3_finalize(hStmt);
7175 if (rc != SQLITE_OK) {
7176 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7177 error = EMAIL_ERROR_DB_FAILURE;
7181 if (err_code != NULL)
7184 EM_DEBUG_FUNC_END("ret [%d]", ret);
7188 INTERNAL_FUNC int emstorage_remove_downloaded_mail(char *multi_user_name,
7196 EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], mailbox_id[%d], mailbox_name[%s], "
7197 "uid[%s], transaction[%d], err_code[%p]",
7198 account_id, mailbox_id, mailbox_name, uid, transaction, err_code);
7200 if (account_id < FIRST_ACCOUNT_ID) {
7201 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_name[%s], uid[%s]", account_id, mailbox_name, uid);
7203 if (err_code != NULL)
7204 *err_code = EMAIL_ERROR_INVALID_PARAM;
7209 int error = EMAIL_ERROR_NONE;
7210 char sql_query_string[QUERY_SIZE] = {0, };
7211 char *replaced_mailbox_name = NULL;
7212 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7214 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7216 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7217 "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d ", account_id);
7219 if (mailbox_id > 0) {
7220 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7221 sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7222 "AND mailbox_id = %d ", mailbox_id);
7225 if (mailbox_name) { /* NULL means all mailbox_name */
7226 if (strstr(mailbox_name, "'")) {
7227 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
7229 replaced_mailbox_name = strdup(mailbox_name);
7232 SNPRINTF(sql_query_string+EM_SAFE_STRLEN(sql_query_string),
7233 sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7234 "AND mailbox_name = '%s' ", replaced_mailbox_name);
7237 if (uid) { /* NULL means all mail */
7238 sqlite3_snprintf(sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7239 sql_query_string + EM_SAFE_STRLEN(sql_query_string), "AND server_uid = '%q' ",
7243 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7244 if (error != EMAIL_ERROR_NONE) {
7245 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7252 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7254 EM_SAFE_FREE(replaced_mailbox_name);
7256 if (err_code != NULL)
7259 EM_DEBUG_FUNC_END("ret [%d]", ret);
7263 INTERNAL_FUNC int emstorage_free_read_mail_uid(emstorage_read_mail_uid_tbl_t** read_mail_uid, int count, int *err_code)
7265 EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], count[%d], err_code[%p]", read_mail_uid, count, err_code);
7268 int error = EMAIL_ERROR_NONE;
7271 if (!read_mail_uid || !*read_mail_uid) {
7272 EM_DEBUG_EXCEPTION(" read_mail_uid[%p], count[%d]", read_mail_uid, count);
7274 error = EMAIL_ERROR_INVALID_PARAM;
7278 emstorage_read_mail_uid_tbl_t* p = *read_mail_uid;
7281 for (i = 0; i < count; i++) {
7282 EM_SAFE_FREE(p[i].mailbox_name);
7283 EM_SAFE_FREE(p[i].server_uid);
7286 EM_SAFE_FREE(p); *read_mail_uid = NULL;
7292 if (err_code != NULL)
7295 EM_DEBUG_FUNC_END("ret [%d]", ret);
7299 INTERNAL_FUNC int emstorage_get_rule_count_by_account_id(char *multi_user_name, int account_id, int *count, int transaction, int *err_code)
7301 EM_DEBUG_FUNC_BEGIN("account_id [%d], count[%p], transaction[%d], err_code[%p]", count, transaction, err_code);
7304 EM_DEBUG_EXCEPTION("count[%p]", count);
7306 if (err_code != NULL)
7307 *err_code = EMAIL_ERROR_INVALID_PARAM;
7311 int rc = -1, ret = false;
7312 int error = EMAIL_ERROR_NONE;
7313 char sql_query_string[QUERY_SIZE] = {0, };
7315 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7316 EMSTORAGE_START_READ_TRANSACTION(transaction);
7318 if (account_id != ALL_ACCOUNT)
7319 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl where account_id = %d", account_id);
7321 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl");
7325 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7326 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7327 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7329 *count = atoi(result[1]);
7330 sqlite3_free_table(result);
7335 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7337 if (err_code != NULL)
7340 EM_DEBUG_FUNC_END("ret [%d]", ret);
7344 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)
7346 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);
7348 if (!select_num || !is_completed || !rule_list) { /* only global rule supported. */
7349 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);
7351 if (err_code != NULL)
7352 *err_code = EMAIL_ERROR_INVALID_PARAM;
7357 int error = EMAIL_ERROR_NONE;
7359 emstorage_rule_tbl_t* p_data_tbl = NULL;
7360 int i = 0, count = 0;
7361 DB_STMT hStmt = NULL;
7362 char sql_query_string[QUERY_SIZE] = {0, };
7366 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7367 EMSTORAGE_START_READ_TRANSACTION(transaction);
7369 if (account_id != ALL_ACCOUNT) {
7371 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d AND type = %d", account_id, type);
7373 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d ORDER BY type", account_id);
7376 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE type = %d", type);
7378 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl ORDER BY type");
7381 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7382 /* EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt); */
7383 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7384 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7387 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7388 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7389 ("sqlite3_step fail:%d", rc));
7392 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL); */
7393 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
7394 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7395 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7397 sqlite3_free_table(result);
7400 EM_DEBUG_LOG_DEV("No matching rule found...");
7402 error = EMAIL_ERROR_FILTER_NOT_FOUND; /*there is no matched rule*/
7407 if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t) * count))) {
7408 EM_DEBUG_EXCEPTION(" malloc failed...");
7410 error = EMAIL_ERROR_OUT_OF_MEMORY;
7414 memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t) * count);
7416 for (i = 0; i < count; i++) {
7417 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7418 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7419 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7420 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].type), TYPE_IDX_IN_MAIL_RULE_TBL);
7421 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7422 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7423 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7424 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7425 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7426 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7428 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7429 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7430 ("sqlite3_step fail:%d", rc));
7437 EM_DEBUG_LOG("[%d] rules found.", count);
7440 *rule_list = p_data_tbl;
7441 *select_num = count;
7442 } else if (p_data_tbl != NULL)
7443 emstorage_free_rule(&p_data_tbl, count, NULL); /* CID FIX */
7445 if (hStmt != NULL) {
7446 rc = sqlite3_finalize(hStmt);
7447 if (rc != SQLITE_OK) {
7448 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7449 error = EMAIL_ERROR_DB_FAILURE;
7453 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7455 if (err_code != NULL)
7458 EM_DEBUG_FUNC_END("ret [%d]", ret);
7462 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)
7464 EM_DEBUG_FUNC_BEGIN("rule_id[%d], rule[%p], transaction[%d], err_code[%p]", rule_id, rule, transaction, err_code);
7465 int error = EMAIL_ERROR_NONE;
7467 DB_STMT hStmt = NULL;
7470 EM_DEBUG_EXCEPTION("Invalid parameter");
7471 error = EMAIL_ERROR_INVALID_PARAM;
7476 EM_DEBUG_EXCEPTION("rule_id[%d], rule[%p]", rule_id, rule);
7477 error = EMAIL_ERROR_INVALID_PARAM;
7481 emstorage_rule_tbl_t* p_data_tbl = NULL;
7484 char sql_query_string[QUERY_SIZE] = {0, };
7485 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7486 EMSTORAGE_START_READ_TRANSACTION(transaction);
7488 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7490 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7491 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7492 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7495 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7496 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7497 ("sqlite3_step fail:%d", rc));
7499 if (rc == SQLITE_DONE) {
7500 EM_DEBUG_EXCEPTION(" no matched rule found...");
7501 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7505 if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t)))) {
7506 EM_DEBUG_EXCEPTION(" malloc failed...");
7507 error = EMAIL_ERROR_OUT_OF_MEMORY;
7511 memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t));
7512 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7513 _get_stmt_field_data_int(hStmt, &(p_data_tbl->rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7514 _get_stmt_field_data_string(hStmt, &(p_data_tbl->filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7515 _get_stmt_field_data_int(hStmt, &(p_data_tbl->type), TYPE_IDX_IN_MAIL_RULE_TBL);
7516 _get_stmt_field_data_string(hStmt, &(p_data_tbl->value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7517 _get_stmt_field_data_string(hStmt, &(p_data_tbl->value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7518 _get_stmt_field_data_int(hStmt, &(p_data_tbl->action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7519 _get_stmt_field_data_int(hStmt, &(p_data_tbl->target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7520 _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7521 _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7530 if (hStmt != NULL) {
7531 rc = sqlite3_finalize(hStmt);
7532 if (rc != SQLITE_OK) {
7533 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7534 error = EMAIL_ERROR_DB_FAILURE;
7538 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7540 if (err_code != NULL)
7543 EM_DEBUG_FUNC_END("ret [%d]", ret);
7547 INTERNAL_FUNC int emstorage_change_rule(char *multi_user_name, int rule_id, emstorage_rule_tbl_t* new_rule, int transaction, int *err_code)
7549 EM_DEBUG_FUNC_BEGIN("rule_id[%d], new_rule[%p], transaction[%d], err_code[%p]", rule_id, new_rule, transaction, err_code);
7551 if (!new_rule) { /* only global rule supported. */
7552 EM_DEBUG_EXCEPTION("rule_id[%d], new_rule[%p]", rule_id, new_rule);
7554 if (err_code != NULL)
7555 *err_code = EMAIL_ERROR_INVALID_PARAM;
7559 int rc, ret = false;
7560 int error = EMAIL_ERROR_NONE;
7562 DB_STMT hStmt = NULL;
7563 char sql_query_string[QUERY_SIZE] = {0, };
7564 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7565 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7567 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7568 "UPDATE mail_rule_tbl SET"
7574 ", target_mailbox_id = ?"
7579 " WHERE rule_id = %d"
7583 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7584 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
7585 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7586 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7590 _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->filter_name, 0, FILTER_NAME_LEN_IN_MAIL_RULE_TBL);
7591 _bind_stmt_field_data_int(hStmt, i++, new_rule->type);
7592 _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7593 _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7594 _bind_stmt_field_data_int(hStmt, i++, new_rule->action_type);
7595 _bind_stmt_field_data_int(hStmt, i++, new_rule->target_mailbox_id);
7596 _bind_stmt_field_data_int(hStmt, i++, new_rule->flag1);
7597 _bind_stmt_field_data_int(hStmt, i++, new_rule->flag2);
7598 _bind_stmt_field_data_int(hStmt, i++, new_rule->account_id);
7599 _bind_stmt_field_data_int(hStmt, i++, rule_id);
7602 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7603 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7604 ("sqlite3_step fail:%d", rc));
7609 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7610 if (hStmt != NULL) {
7611 rc = sqlite3_finalize(hStmt);
7612 if (rc != SQLITE_OK) {
7613 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7614 error = EMAIL_ERROR_DB_FAILURE;
7618 if (err_code != NULL)
7621 EM_DEBUG_FUNC_END("ret [%d]", ret);
7625 INTERNAL_FUNC int emstorage_find_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7627 EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7630 EM_DEBUG_LOG("rule is NULL");
7631 if (err_code != NULL)
7632 *err_code = EMAIL_ERROR_INVALID_PARAM;
7636 DB_STMT hStmt = NULL;
7637 char sql_query_string[QUERY_SIZE] = {0,};
7638 int error = EMAIL_ERROR_NONE;
7642 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7643 EMSTORAGE_START_READ_TRANSACTION(transaction);
7645 switch (rule->action_type) {
7646 case EMAIL_FILTER_MOVE:
7647 if (rule->type == EMAIL_PRIORITY_SENDER) {
7648 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7649 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7650 rule->action_type, rule->type, rule->value2);
7652 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7653 "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);
7656 case EMAIL_FILTER_BLOCK:
7657 if (rule->type == EMAIL_FILTER_FROM)
7658 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7659 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7660 rule->action_type, rule->type, rule->value2);
7661 else if (rule->type == EMAIL_FILTER_SUBJECT)
7662 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7663 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value) = UPPER(\'%q\')",
7664 rule->action_type, rule->type, rule->value);
7665 else if (rule->type == (EMAIL_FILTER_SUBJECT | EMAIL_FILTER_FROM))
7666 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7667 "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\'))",
7668 rule->action_type, EMAIL_FILTER_SUBJECT, rule->value, EMAIL_FILTER_FROM, rule->value2);
7672 EM_DEBUG_EXCEPTION("Invalid parameter : rule->action_type[%d]", rule->action_type);
7673 error = EMAIL_ERROR_INVALID_PARAM;
7678 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7679 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7680 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7682 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7683 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7684 ("sqlite3_step fail:%d", rc));
7686 if (rc == SQLITE_DONE) {
7687 EM_DEBUG_EXCEPTION(" no matched rule found...");
7688 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7695 if (hStmt != NULL) {
7696 rc = sqlite3_finalize(hStmt);
7697 if (rc != SQLITE_OK) {
7698 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7699 error = EMAIL_ERROR_DB_FAILURE;
7703 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7708 EM_DEBUG_FUNC_END("ret [%d]", ret);
7712 INTERNAL_FUNC int emstorage_add_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7714 EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7716 if (!rule) { /* only global rule supported. */
7717 EM_DEBUG_LOG("rule is NULL");
7718 if (err_code != NULL)
7719 *err_code = EMAIL_ERROR_INVALID_PARAM;
7723 int rc, rc_2, ret = false;
7724 int error = EMAIL_ERROR_NONE;
7725 DB_STMT hStmt = NULL;
7726 char sql_query_string[QUERY_SIZE] = {0, };
7728 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7730 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7734 sql = "SELECT max(rowid) FROM mail_rule_tbl;";
7736 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7737 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7738 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7740 if (NULL == result[1])
7743 rc = atoi(result[1])+1;
7745 sqlite3_free_table(result);
7749 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7750 "INSERT INTO mail_rule_tbl VALUES "
7751 "(?" /* account id */
7753 ", ?" /* filter_name */
7757 ", ?" /* action_type */
7758 ", ?" /* target_mailbox_id */
7760 ", ?)"); /* flag2 */
7762 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc_2);
7763 if (rc_2 != SQLITE_OK) {
7764 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc_2, sqlite3_errmsg(local_db_handle));
7765 error = EMAIL_ERROR_DB_FAILURE;
7769 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL, rule->account_id);
7770 _bind_stmt_field_data_int(hStmt, RULE_ID_IDX_IN_MAIL_RULE_TBL, rule->rule_id);
7771 _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);
7772 _bind_stmt_field_data_int(hStmt, TYPE_IDX_IN_MAIL_RULE_TBL, rule->type);
7773 _bind_stmt_field_data_string(hStmt, VALUE_IDX_IN_MAIL_RULE_TBL, (char*)rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7774 _bind_stmt_field_data_string(hStmt, VALUE2_IDX_IN_MAIL_RULE_TBL, (char*)rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7775 _bind_stmt_field_data_int(hStmt, ACTION_TYPE_IDX_IN_MAIL_RULE_TBL, rule->action_type);
7776 _bind_stmt_field_data_int(hStmt, TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL, rule->target_mailbox_id);
7777 _bind_stmt_field_data_int(hStmt, FLAG1_IDX_IN_MAIL_RULE_TBL, rule->flag1);
7778 _bind_stmt_field_data_int(hStmt, FLAG2_IDX_IN_MAIL_RULE_TBL, rule->flag2);
7780 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7781 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7782 ("sqlite3_step fail:%d", rc));
7787 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7788 if (hStmt != NULL) {
7789 rc = sqlite3_finalize(hStmt);
7790 if (rc != SQLITE_OK) {
7791 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7792 error = EMAIL_ERROR_DB_FAILURE;
7796 if (err_code != NULL)
7799 EM_DEBUG_FUNC_END("ret [%d]", ret);
7803 INTERNAL_FUNC int emstorage_delete_rule(char *multi_user_name, int rule_id, int transaction, int *err_code)
7805 EM_DEBUG_FUNC_BEGIN("rule_id[%d], transaction[%d], err_code[%p]", rule_id, transaction, err_code);
7807 if (rule_id <= 0) { /* only global rule supported. */
7808 EM_DEBUG_EXCEPTION("rule_id[%d]", rule_id);
7810 if (err_code != NULL)
7811 *err_code = EMAIL_ERROR_INVALID_PARAM;
7815 int rc, ret = false;
7816 int error = EMAIL_ERROR_NONE;
7817 char sql_query_string[QUERY_SIZE] = {0, };
7818 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7819 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7821 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7822 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7823 if (error != EMAIL_ERROR_NONE) {
7824 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7828 rc = sqlite3_changes(local_db_handle);
7830 EM_DEBUG_EXCEPTION(" no matched rule found...");
7832 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7839 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7841 if (err_code != NULL)
7844 EM_DEBUG_FUNC_END("ret [%d]", ret);
7848 INTERNAL_FUNC int emstorage_free_rule(emstorage_rule_tbl_t** rule_list, int count, int *err_code)
7850 EM_DEBUG_FUNC_BEGIN("rule_list[%p], conut[%d], err_code[%p]", rule_list, count, err_code);
7853 int error = EMAIL_ERROR_NONE;
7856 if (!rule_list || !*rule_list) {
7857 EM_DEBUG_EXCEPTION(" rule_list[%p], conut[%d]", rule_list, count);
7859 error = EMAIL_ERROR_INVALID_PARAM;
7863 emstorage_rule_tbl_t* p = *rule_list;
7866 for (; i < count; i++) {
7867 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), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND account_id = %d", account_id);
7941 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);
7943 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND mailbox_type NOT IN (3, 5)");
7946 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7947 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7948 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7950 *unseen = atoi(result[1]);
7951 sqlite3_free_table(result);
7959 #ifndef USE_PREPARED_QUERY_
7960 if (hStmt != NULL) {
7961 rc = sqlite3_finalize(hStmt);
7962 if (rc != SQLITE_OK) {
7963 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7964 error = EMAIL_ERROR_DB_FAILURE;
7969 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7971 EM_SAFE_FREE(replaced_mailbox_name);
7973 if (err_code != NULL)
7976 EM_DEBUG_FUNC_END("ret [%d]", ret);
7980 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)
7982 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
7984 if (mail_id <= 0 || !mail) {
7985 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
7986 if (err_code != NULL)
7987 *err_code = EMAIL_ERROR_INVALID_PARAM;
7992 emstorage_mail_tbl_t* p_data_tbl = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
7994 if (p_data_tbl == NULL) {
7995 EM_DEBUG_EXCEPTION("malloc failed...");
7996 if (err_code != NULL)
7997 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8001 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8002 DB_STMT hStmt = NULL;
8003 char sql_query_string[QUERY_SIZE] = {0, };
8005 int rc, ret = false;
8006 int error = EMAIL_ERROR_NONE;
8008 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8009 EMSTORAGE_START_READ_TRANSACTION(transaction);
8012 case RETRIEVE_SUMMARY:
8013 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8014 "SELECT account_id, "
8017 "server_mail_status, "
8018 "server_mailbox_name, "
8022 "file_path_mime_entity, "
8023 "flags_seen_field, "
8027 "thread_item_count "
8028 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8031 case RETRIEVE_FIELDS_FOR_DELETE:
8032 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8033 "SELECT account_id, "
8035 "server_mail_status, "
8036 "server_mailbox_name, "
8038 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8041 case RETRIEVE_ACCOUNT:
8042 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8043 "SELECT account_id "
8044 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8048 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8049 "SELECT account_id, "
8050 "flags_seen_field, "
8053 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8057 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
8058 error = EMAIL_ERROR_INVALID_PARAM;
8062 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
8064 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8065 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8066 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8069 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8070 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8071 ("sqlite3_step fail:%d", rc));
8073 if (rc == SQLITE_DONE) {
8074 EM_DEBUG_LOG("no matched mail found...");
8075 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8079 case RETRIEVE_SUMMARY:
8080 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8081 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
8082 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
8083 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
8084 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
8085 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8086 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, col_index++);
8087 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, col_index++);
8088 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, col_index++);
8089 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8090 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), col_index++);
8091 _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), col_index++);
8092 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8093 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_item_count), col_index++);
8096 case RETRIEVE_FIELDS_FOR_DELETE:
8097 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8098 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
8099 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
8100 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
8101 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8104 case RETRIEVE_ACCOUNT:
8105 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8109 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8110 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8111 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8112 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
8121 else if (p_data_tbl != NULL)
8122 emstorage_free_mail(&p_data_tbl, 1, NULL);
8124 if (hStmt != NULL) {
8125 rc = sqlite3_finalize(hStmt);
8126 if (rc != SQLITE_OK) {
8127 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8128 error = EMAIL_ERROR_DB_FAILURE;
8133 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8135 if (err_code != NULL)
8138 EM_DEBUG_FUNC_END("ret [%d]", ret);
8142 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)
8144 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);
8147 int error = EMAIL_ERROR_NONE;
8148 int query_string_length = 0;
8149 int i = 0, item_count = 0, rc = -1, field_count, col_index, cur_sql_query_string = 0;
8150 char **result = NULL;
8151 char *sql_query_string = NULL;
8152 emstorage_mail_tbl_t* p_data_tbl = NULL;
8153 sqlite3 *local_db_handle = NULL;
8155 if (number_of_mails <= 0 || !mail_ids) {
8156 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8157 if (err_code != NULL)
8158 *err_code = EMAIL_ERROR_INVALID_PARAM;
8162 p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * number_of_mails);
8164 query_string_length = (sizeof(char) * 8 * number_of_mails) + 512;
8165 sql_query_string = (char*)em_malloc(query_string_length);
8167 if (p_data_tbl == NULL || sql_query_string == NULL) {
8168 EM_DEBUG_EXCEPTION("malloc failed...");
8170 EM_SAFE_FREE(p_data_tbl);
8171 EM_SAFE_FREE(sql_query_string);
8173 if (err_code != NULL)
8174 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8178 local_db_handle = emstorage_get_db_connection(multi_user_name);
8180 EMSTORAGE_START_READ_TRANSACTION(transaction);
8183 case RETRIEVE_SUMMARY:
8184 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8185 "SELECT account_id, "
8188 "server_mail_status, "
8189 "server_mailbox_name, "
8193 "file_path_mime_entity, "
8195 "flags_seen_field, "
8199 "thread_item_count "
8200 "FROM mail_tbl WHERE mail_id in (");
8204 case RETRIEVE_FIELDS_FOR_DELETE:
8205 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8206 "SELECT account_id, "
8208 "server_mail_status, "
8209 "server_mailbox_name, "
8211 "FROM mail_tbl WHERE mail_id in (");
8215 case RETRIEVE_ACCOUNT:
8216 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8217 "SELECT account_id FROM mail_tbl WHERE mail_id in (");
8222 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8223 "SELECT account_id, "
8226 "flags_seen_field, "
8228 "FROM mail_tbl WHERE mail_id in (");
8233 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
8234 error = EMAIL_ERROR_INVALID_PARAM;
8238 for (i = 0; i < number_of_mails; i++)
8239 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_string_length, "%d,", mail_ids[i]);
8240 sql_query_string[EM_SAFE_STRLEN(sql_query_string) - 1] = ')';
8242 EM_DEBUG_LOG_SEC("Query [%s], Length [%d]", sql_query_string, EM_SAFE_STRLEN(sql_query_string));
8244 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &item_count, 0, NULL), rc);
8245 if (SQLITE_OK != rc && -1 != rc) {
8246 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
8247 error = EMAIL_ERROR_DB_FAILURE;
8251 if (rc == SQLITE_DONE) {
8252 EM_DEBUG_LOG("no matched mail found...");
8253 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8257 EM_DEBUG_LOG("item_count [%d]", item_count);
8259 if (number_of_mails != item_count) {
8260 EM_DEBUG_EXCEPTION("Can't find all emails");
8261 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8265 col_index = field_count;
8267 for (i = 0; i < item_count; i++) {
8269 case RETRIEVE_SUMMARY:
8270 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8271 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8272 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8273 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8274 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8275 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8276 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
8277 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
8278 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
8279 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 0, col_index++);
8280 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8281 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
8282 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
8283 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8284 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
8287 case RETRIEVE_FIELDS_FOR_DELETE:
8288 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8289 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8290 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8291 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8292 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8295 case RETRIEVE_ACCOUNT:
8296 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8300 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8301 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8302 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8303 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8304 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8315 emstorage_free_mail(&p_data_tbl, number_of_mails, NULL);
8318 sqlite3_free_table(result);
8320 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8322 EM_SAFE_FREE(sql_query_string);
8324 if (err_code != NULL)
8327 EM_DEBUG_FUNC_END("ret [%d]", ret);
8331 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)
8333 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8335 if (mail_id <= 0 || !mail) {
8336 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
8337 if (err_code != NULL)
8338 *err_code = EMAIL_ERROR_INVALID_PARAM;
8342 int ret = false, error = EMAIL_ERROR_NONE, count;
8343 char conditional_clause[QUERY_SIZE] = {0, };
8344 emstorage_mail_tbl_t* p_data_tbl = NULL;
8346 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8347 EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8349 if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8350 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8359 else if (p_data_tbl != NULL)
8360 emstorage_free_mail(&p_data_tbl, 1, &error);
8362 if (err_code != NULL)
8365 EM_DEBUG_FUNC_END("ret [%d]", ret);
8369 #ifdef __FEATURE_BODY_SEARCH__
8370 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)
8372 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
8374 if (mail_id <= 0 || !mail_text) {
8375 EM_DEBUG_EXCEPTION("mail_id[%d], mail_text[%p]", mail_id, mail_text);
8376 if (err_code != NULL)
8377 *err_code = EMAIL_ERROR_INVALID_PARAM;
8382 int error = EMAIL_ERROR_NONE;
8384 char conditional_clause[QUERY_SIZE] = {0, };
8385 emstorage_mail_text_tbl_t *p_data_tbl = NULL;
8387 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8388 EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8390 if (!emstorage_query_mail_text_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8391 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8399 *mail_text = p_data_tbl;
8400 else if (p_data_tbl != NULL)
8401 emstorage_free_mail_text(&p_data_tbl, 1, &error);
8403 if (err_code != NULL)
8406 EM_DEBUG_FUNC_END("ret [%d]", ret);
8411 INTERNAL_FUNC int emstorage_mail_search_start(char *multi_user_name,
8412 emstorage_search_filter_t *search,
8416 DB_STMT *search_handle,
8421 EM_DEBUG_FUNC_BEGIN("search[%p], account_id[%d], mailbox_id[%d], sorting[%d], "
8422 "search_handle[%p], searched[%p], transaction[%d], err_code[%p]",
8423 search, account_id, mailbox_id, sorting, search_handle,
8424 searched, transaction, err_code);
8426 if (!search_handle || !searched) {
8427 if (err_code != NULL)
8428 *err_code = EMAIL_ERROR_INVALID_PARAM;
8429 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8430 EM_DEBUG_FUNC_END("false");
8434 emstorage_search_filter_t* p = search;
8435 int error = EMAIL_ERROR_NONE;
8436 DB_STMT hStmt = NULL;
8437 char sql_query_string[QUERY_SIZE] = {0, };
8438 int rc, ret = false;
8439 int and = false, mail_count = 0;
8441 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8442 EMSTORAGE_START_READ_TRANSACTION(transaction);
8444 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl");
8446 if (account_id != ALL_ACCOUNT) {
8447 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);
8452 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);
8458 if (!strncmp(p->key_type, "subject", strlen("subject"))) {
8459 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);
8461 } else if (!strncmp(p->key_type, "full_address_from", strlen("full_address_from"))) {
8462 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);
8464 } else if (!strncmp(p->key_type, "full_address_to", strlen("full_address_to"))) {
8465 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);
8467 } else if (!strncmp(p->key_type, "email_address", strlen("email_address"))) {
8468 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);
8476 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");
8478 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
8481 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8482 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8483 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8486 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8487 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8488 ("sqlite3_step fail:%d", rc));
8492 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &mail_count, NULL, NULL), rc);
8493 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
8494 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8496 sqlite3_free_table(result);
8502 *search_handle = hStmt;
8503 *searched = mail_count;
8504 EM_DEBUG_LOG("mail_count [%d]", mail_count);
8506 if (hStmt != NULL) {
8507 rc = sqlite3_finalize(hStmt);
8508 if (rc != SQLITE_OK) {
8509 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8510 error = EMAIL_ERROR_DB_FAILURE;
8514 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8517 if (err_code != NULL)
8520 EM_DEBUG_FUNC_END("ret [%d]", ret);
8524 INTERNAL_FUNC int emstorage_mail_search_result(DB_STMT search_handle, emstorage_mail_field_type_t type, void** data, int transaction, int *err_code)
8526 EM_DEBUG_FUNC_BEGIN("search_handle[%d], type[%d], data[%p], transaction[%d], err_code[%p]", search_handle, type, data, transaction, err_code);
8528 if (search_handle == 0 || !data) {
8529 EM_DEBUG_EXCEPTION(" search_handle[%d], type[%d], data[%p]", search_handle, type, data);
8531 if (err_code != NULL)
8532 *err_code = EMAIL_ERROR_INVALID_PARAM;
8536 emstorage_mail_tbl_t* p_data_tbl = NULL;
8537 DB_STMT hStmt = search_handle;
8538 int rc, ret = false;
8539 int error = EMAIL_ERROR_NONE;
8543 _get_stmt_field_data_int(hStmt, (int *)data, MAIL_ID_IDX_IN_MAIL_TBL);
8546 case RETRIEVE_ENVELOPE:
8548 if (!(p_data_tbl = em_malloc(sizeof(emstorage_mail_tbl_t)))) {
8549 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
8550 error = EMAIL_ERROR_OUT_OF_MEMORY;
8554 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8555 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8556 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_size), MAIL_SIZE_IDX_IN_MAIL_TBL);
8557 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8558 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_from), 1, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL);
8559 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_to), 1, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL);
8560 _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject), 1, SUBJECT_IDX_IN_MAIL_TBL);
8561 _get_stmt_field_data_int(hStmt, &(p_data_tbl->body_download_status), BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL);
8562 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL);
8563 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8564 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8565 _get_stmt_field_data_time_t(hStmt, &(p_data_tbl->date_time), DATETIME_IDX_IN_MAIL_TBL);
8566 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL);
8567 _get_stmt_field_data_int(hStmt, &(p_data_tbl->DRM_status), DRM_STATUS_IDX_IN_MAIL_TBL);
8568 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->priority), PRIORITY_IDX_IN_MAIL_TBL);
8569 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), SAVE_STATUS_IDX_IN_MAIL_TBL);
8570 _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), LOCK_STATUS_IDX_IN_MAIL_TBL);
8571 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->report_status), REPORT_STATUS_IDX_IN_MAIL_TBL);
8572 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8573 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->meeting_request_status), MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL);
8574 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->message_class), MESSAGE_CLASS_IDX_IN_MAIL_TBL);
8575 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->digest_type), DIGEST_TYPE_IDX_IN_MAIL_TBL);
8576 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->smime_type), SMIME_TYPE_IDX_IN_MAIL_TBL);
8577 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->scheduled_sending_time), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8578 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->remaining_resend_times), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8579 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->tag_id), TAG_ID_IDX_IN_MAIL_TBL);
8580 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->eas_data_length), EAS_DATA_LENGTH_IDX_IN_MAIL_TBL);
8581 _get_stmt_field_data_blob(hStmt, (void**)&(p_data_tbl->eas_data), EAS_DATA_IDX_IN_MAIL_TBL);
8583 if (type == RETRIEVE_ALL) {
8584 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8585 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8586 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_reply), 1, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL);
8587 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_cc), 1, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL);
8588 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_bcc), 1, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL);
8589 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_return), 1, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL);
8590 _get_stmt_field_data_string(hStmt, &(p_data_tbl->message_id), 0, MESSAGE_ID_IDX_IN_MAIL_TBL);
8591 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8592 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8593 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_count), ATTACHMENT_COUNT_IDX_IN_MAIL_TBL);
8594 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8595 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->replied_time), REPLIED_TIME_IDX_IN_MAIL_TBL);
8596 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->forwarded_time), FORWARDED_TIME_IDX_IN_MAIL_TBL);
8597 _get_stmt_field_data_string(hStmt, &(p_data_tbl->default_charset), 0, DEFAULT_CHARSET_IDX_IN_MAIL_TBL);
8600 if (p_data_tbl->body_download_status) {
8603 if (p_data_tbl->file_path_html) {
8604 if (stat(p_data_tbl->file_path_html, &buf) == -1)
8605 p_data_tbl->body_download_status = 0;
8606 } else if (p_data_tbl->file_path_plain) {
8607 if (stat(p_data_tbl->file_path_plain, &buf) == -1)
8608 p_data_tbl->body_download_status = 0;
8610 p_data_tbl->body_download_status = 0;
8613 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8616 case RETRIEVE_SUMMARY:
8617 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
8618 EM_DEBUG_EXCEPTION(" malloc failed...");
8620 error = EMAIL_ERROR_OUT_OF_MEMORY;
8624 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8626 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8627 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8628 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8629 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8630 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8632 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8635 case RETRIEVE_ADDRESS:
8636 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
8637 EM_DEBUG_EXCEPTION(" malloc failed...");
8638 error = EMAIL_ERROR_OUT_OF_MEMORY;
8642 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8643 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8644 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8645 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8646 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8653 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8654 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8655 ("sqlite3_step fail:%d", rc));
8661 if (err_code != NULL)
8664 if (ret == false && p_data_tbl)
8665 emstorage_free_mail(&p_data_tbl, 1, NULL);
8667 EM_DEBUG_FUNC_END("ret [%d]", ret);
8671 INTERNAL_FUNC int emstorage_mail_search_end(DB_STMT search_handle, int transaction, int *err_code)
8673 EM_DEBUG_FUNC_BEGIN("search_handle[%d], transaction[%d], err_code[%p]", search_handle, transaction, err_code);
8675 int error = EMAIL_ERROR_NONE;
8676 int rc, ret = false;
8678 if (search_handle == 0) {
8679 EM_DEBUG_EXCEPTION(" search_handle[%d]", search_handle);
8680 error = EMAIL_ERROR_INVALID_PARAM;
8684 DB_STMT hStmt = search_handle;
8686 rc = sqlite3_finalize(hStmt);
8687 if (rc != SQLITE_OK) {
8688 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8689 error = EMAIL_ERROR_DB_FAILURE;
8695 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8697 if (err_code != NULL)
8700 EM_DEBUG_FUNC_END("ret [%d]", ret);
8704 INTERNAL_FUNC int emstorage_change_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t *mail, int transaction, int *err_code)
8706 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8708 if (mail_id <= 0 || !mail) {
8709 EM_DEBUG_EXCEPTION(" mail_id[%d], mail[%p]", mail_id, mail);
8711 if (err_code != NULL)
8712 *err_code = EMAIL_ERROR_INVALID_PARAM;
8716 DB_STMT hStmt = NULL;
8717 char sql_query_string[QUERY_SIZE] = {0, };
8720 int error = EMAIL_ERROR_NONE;
8722 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8723 char mailbox_id_param_string[10] = {0,};
8725 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
8727 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8728 "UPDATE mail_tbl SET"
8733 ", server_mail_status = ?"
8734 ", server_mailbox_name = ?"
8735 ", server_mail_id = ?"
8736 ", reference_mail_id = ?"
8737 ", full_address_from = ?"
8738 ", full_address_reply = ?" /* 10 */
8739 ", full_address_to = ?"
8740 ", full_address_cc = ?"
8741 ", full_address_bcc = ?"
8742 ", full_address_return = ?"
8744 ", body_download_status = ?"
8745 ", file_path_plain = ?"
8746 ", file_path_html = ?"
8747 ", file_path_mime_entity = ?"
8749 ", flags_seen_field = ?"
8750 ", flags_deleted_field = ?"
8751 ", flags_flagged_field = ?"
8752 ", flags_answered_field = ?"
8753 ", flags_recent_field = ?"
8754 ", flags_draft_field = ?"
8755 ", flags_forwarded_field = ?"
8761 ", report_status = ?"
8762 ", preview_text = ?"
8764 ", scheduled_sending_time = ?"
8765 ", remaining_resend_times = ?"
8767 ", replied_time = ?"
8768 ", forwarded_time = ?"
8769 ", default_charset = ?"
8770 ", eas_data_length = ?"
8772 " WHERE mail_id = %d AND account_id != 0 "
8776 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8777 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8778 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8780 _bind_stmt_field_data_int(hStmt, i++, mail->mail_id);
8781 _bind_stmt_field_data_int(hStmt, i++, mail->account_id);
8782 _bind_stmt_field_data_int(hStmt, i++, mail->mailbox_id);
8783 _bind_stmt_field_data_int(hStmt, i++, mail->mail_size);
8784 _bind_stmt_field_data_int(hStmt, i++, mail->server_mail_status);
8785 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mailbox_name, 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
8786 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mail_id, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
8787 _bind_stmt_field_data_int(hStmt, i++, mail->reference_mail_id);
8788 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
8789 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
8790 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
8791 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
8792 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
8793 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
8794 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8795 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
8796 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8797 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8798 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
8799 _bind_stmt_field_data_int(hStmt, i++, mail->date_time);
8800 _bind_stmt_field_data_char(hStmt, i++, mail->flags_seen_field);
8801 _bind_stmt_field_data_char(hStmt, i++, mail->flags_deleted_field);
8802 _bind_stmt_field_data_char(hStmt, i++, mail->flags_flagged_field);
8803 _bind_stmt_field_data_char(hStmt, i++, mail->flags_answered_field);
8804 _bind_stmt_field_data_char(hStmt, i++, mail->flags_recent_field);
8805 _bind_stmt_field_data_char(hStmt, i++, mail->flags_draft_field);
8806 _bind_stmt_field_data_char(hStmt, i++, mail->flags_forwarded_field);
8807 _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
8808 _bind_stmt_field_data_int(hStmt, i++, mail->priority);
8809 _bind_stmt_field_data_int(hStmt, i++, mail->save_status);
8810 _bind_stmt_field_data_int(hStmt, i++, mail->lock_status);
8811 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->message_id, 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
8812 _bind_stmt_field_data_int(hStmt, i++, mail->report_status);
8813 _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8814 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
8815 _bind_stmt_field_data_int(hStmt, i++, mail->scheduled_sending_time);
8816 _bind_stmt_field_data_int(hStmt, i++, mail->remaining_resend_times);
8817 _bind_stmt_field_data_int(hStmt, i++, mail->tag_id);
8818 _bind_stmt_field_data_int(hStmt, i++, mail->replied_time);
8819 _bind_stmt_field_data_int(hStmt, i++, mail->forwarded_time);
8820 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
8821 _bind_stmt_field_data_int(hStmt, i++, mail->eas_data_length);
8822 _bind_stmt_field_data_blob(hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
8824 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8825 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8826 ("sqlite3_step fail:%d", rc));
8828 rc = sqlite3_changes(local_db_handle);
8830 EM_DEBUG_LOG(" no matched mail found...");
8831 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8838 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
8839 if (hStmt != NULL) {
8840 rc = sqlite3_finalize(hStmt);
8841 if (rc != SQLITE_OK) {
8842 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8843 error = EMAIL_ERROR_DB_FAILURE;
8848 if (error == EMAIL_ERROR_NONE && mail) {
8849 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
8850 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail->mail_id, mailbox_id_param_string, 0))
8851 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventfailed [NOTI_MAIL_UPDATE]");
8854 if (err_code != NULL)
8857 EM_DEBUG_FUNC_END("ret [%d]", ret);
8862 * emstorage_clean_save_status(int save_status, int *err_code) - set the all mail status to the set value
8866 INTERNAL_FUNC int emstorage_clean_save_status(char *multi_user_name, int save_status, int *err_code)
8868 EM_DEBUG_FUNC_BEGIN("save_status[%d], err_code[%p]", save_status, err_code);
8870 EM_IF_NULL_RETURN_VALUE(err_code, false);
8873 int error = EMAIL_ERROR_NONE;
8875 char sql_query_string[QUERY_SIZE] = {0, };
8876 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8878 memset(sql_query_string, 0x00, sizeof(sql_query_string));
8879 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);
8881 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
8882 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
8883 if (error != EMAIL_ERROR_NONE) {
8884 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
8888 rc = sqlite3_changes(local_db_handle);
8890 EM_DEBUG_LOG(" No Matched Mail Exists ");
8891 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8898 if (err_code != NULL)
8901 EM_DEBUG_FUNC_END("ret [%d]", ret);
8905 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)
8907 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);
8909 int error = EMAIL_ERROR_NONE;
8912 int cur_mail_id_string = 0;
8913 int mail_id_string_buffer_length = 0;
8914 int parameter_string_length = 0;
8915 char *sql_query_string = NULL;
8916 char *mail_id_string_buffer = NULL;
8917 char *parameter_string = NULL;
8918 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8919 email_mail_attribute_type target_mail_attribute_type = 0;
8921 if (!mail_ids || !field_name || account_id == 0) {
8922 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8923 if (err_code != NULL)
8924 *err_code = EMAIL_ERROR_INVALID_PARAM;
8928 if ((error = emcore_get_attribute_type_by_mail_field_name(field_name, &target_mail_attribute_type)) != EMAIL_ERROR_NONE) {
8929 EM_DEBUG_EXCEPTION("emstorageemcore_get_attribute_type_by_mail_field_name failed [%d]", error);
8930 if (err_code != NULL)
8935 /* Generating mail id list string */
8936 mail_id_string_buffer_length = MAIL_ID_STRING_LENGTH * mail_ids_count;
8938 mail_id_string_buffer = em_malloc(mail_id_string_buffer_length);
8940 if (!mail_id_string_buffer) {
8941 EM_DEBUG_EXCEPTION("em_mallocfailed");
8942 if (err_code != NULL)
8943 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8947 for (i = 0; i < mail_ids_count; i++)
8948 cur_mail_id_string += SNPRINTF_OFFSET(mail_id_string_buffer, cur_mail_id_string, mail_id_string_buffer_length, "%d,", mail_ids[i]);
8950 if (EM_SAFE_STRLEN(mail_id_string_buffer) > 1)
8951 mail_id_string_buffer[EM_SAFE_STRLEN(mail_id_string_buffer) - 1] = NULL_CHAR;
8953 /* Generating notification parameter string */
8954 parameter_string_length = mail_id_string_buffer_length + EM_SAFE_STRLEN(field_name) + 2;
8955 parameter_string = em_malloc(parameter_string_length);
8957 if (!parameter_string) {
8958 EM_DEBUG_EXCEPTION("em_mallocfailed");
8959 if (err_code != NULL)
8960 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8961 EM_SAFE_FREE(mail_id_string_buffer);
8965 SNPRINTF(parameter_string, parameter_string_length, "%s%c%s", field_name, 0x01, mail_id_string_buffer);
8966 query_size = EM_SAFE_STRLEN(mail_id_string_buffer) + EM_SAFE_STRLEN(field_name) + 250;
8968 sql_query_string = em_malloc(query_size);
8969 if (sql_query_string == NULL) {
8970 EM_DEBUG_EXCEPTION("em_mallocfailed");
8971 error = EMAIL_ERROR_OUT_OF_MEMORY;
8974 /* Write query string */
8975 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);
8977 EM_DEBUG_LOG_DEV("sql_query_string [%s]", sql_query_string);
8980 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
8981 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
8982 if (error != EMAIL_ERROR_NONE) {
8983 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
8987 if (sqlite3_changes(local_db_handle) == 0)
8988 EM_DEBUG_LOG("no mail matched...");
8993 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
8995 if (error == EMAIL_ERROR_NONE && parameter_string) {
8996 if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, target_mail_attribute_type, parameter_string, value))
8997 EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_MAIL_FIELD_UPDATE [%s,%d]", field_name, value);
9000 EM_SAFE_FREE(mail_id_string_buffer);
9001 EM_SAFE_FREE(parameter_string);
9002 EM_SAFE_FREE(sql_query_string);
9005 if (err_code != NULL)
9008 EM_DEBUG_FUNC_END("error [%d]", error);
9012 #ifdef __FEATURE_BODY_SEARCH__
9013 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)
9015 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
9017 if (mail_id <= 0 || !mail_text) {
9018 EM_DEBUG_EXCEPTION(" mail_id[%d], mail_text[%p]", mail_id, mail_text);
9019 if (err_code != NULL)
9020 *err_code = EMAIL_ERROR_INVALID_PARAM;
9025 int error = EMAIL_ERROR_NONE;
9026 DB_STMT hStmt = NULL;
9027 char sql_query_string[QUERY_SIZE] = {0, };
9032 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9033 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9035 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9036 "UPDATE mail_text_tbl SET"
9038 " WHERE mail_id = %d AND account_id != 0"
9040 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9042 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9043 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9044 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9047 _bind_stmt_field_data_string(hStmt, i++, (char *)mail_text->body_text, 0, -1);
9049 if (hStmt != NULL) {
9050 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9051 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9052 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9054 rc = sqlite3_changes(local_db_handle);
9056 EM_DEBUG_LOG(" no matched mail found...");
9057 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9065 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9067 if (hStmt != NULL) {
9068 rc = sqlite3_finalize(hStmt);
9069 if (rc != SQLITE_OK) {
9070 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9071 error = EMAIL_ERROR_DB_FAILURE;
9076 if (err_code != NULL)
9079 EM_DEBUG_FUNC_END("ret [%d]", ret);
9084 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)
9086 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
9089 int error = EMAIL_ERROR_NONE;
9094 DB_STMT hStmt = NULL;
9095 char sql_query_string[QUERY_SIZE] = {0, };
9096 char mailbox_id_param_string[10] = {0,};
9097 sqlite3 *local_db_handle = NULL;
9099 if (mail_id <= 0 || !mail) {
9100 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], mail[%p]", mail_id, type, mail);
9101 if (err_code != NULL)
9102 *err_code = EMAIL_ERROR_INVALID_PARAM;
9106 local_db_handle = emstorage_get_db_connection(multi_user_name);
9108 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9112 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9113 "UPDATE mail_tbl SET"
9114 " body_download_status = ?"
9115 ", file_path_plain = ?"
9116 ", file_path_html = ?"
9117 ", file_path_mime_entity = ?"
9118 ", flags_seen_field = ?"
9119 ", flags_deleted_field = ?"
9120 ", flags_flagged_field = ?"
9121 ", flags_answered_field = ?"
9122 ", flags_recent_field = ?"
9123 ", flags_draft_field = ?"
9124 ", flags_forwarded_field = ?"
9126 ", attachment_count = ?"
9128 ", meeting_request_status = ? "
9129 ", message_class = ? "
9130 ", digest_type = ? "
9132 " WHERE mail_id = %d AND account_id != 0"
9136 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9137 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9138 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9141 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9142 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9143 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9144 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9145 _bind_stmt_field_data_int(hStmt, i++, mail->flags_seen_field);
9146 _bind_stmt_field_data_int(hStmt, i++, mail->flags_deleted_field);
9147 _bind_stmt_field_data_int(hStmt, i++, mail->flags_flagged_field);
9148 _bind_stmt_field_data_int(hStmt, i++, mail->flags_answered_field);
9149 _bind_stmt_field_data_int(hStmt, i++, mail->flags_recent_field);
9150 _bind_stmt_field_data_int(hStmt, i++, mail->flags_draft_field);
9151 _bind_stmt_field_data_int(hStmt, i++, mail->flags_forwarded_field);
9152 _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
9153 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9154 _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9155 _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
9156 _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
9157 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9158 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9161 case UPDATE_MAILBOX: {
9163 emstorage_mailbox_tbl_t *mailbox_tbl;
9165 if ((err = emstorage_get_mailbox_by_id(multi_user_name, mail->mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
9166 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_id failed [%d]", err);
9170 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9171 "UPDATE mail_tbl SET"
9172 " mailbox_id = '%d'"
9173 ",mailbox_type = '%d'"
9174 " WHERE mail_id = %d AND account_id != 0"
9175 , mailbox_tbl->mailbox_id
9176 , mailbox_tbl->mailbox_type
9181 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9182 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9183 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9186 _bind_stmt_field_data_string(hStmt, i++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
9188 emstorage_free_mailbox(&mailbox_tbl, 1, NULL); /*prevent 26251*/
9194 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9195 "UPDATE mail_tbl SET"
9196 " flags_seen_field = %d"
9197 ",flags_deleted_field = %d"
9198 ",flags_flagged_field = %d"
9199 ",flags_answered_field = %d"
9200 ",flags_recent_field = %d"
9201 ",flags_draft_field = %d"
9202 ",flags_forwarded_field = %d"
9203 " WHERE mail_id = %d AND account_id != 0"
9204 , mail->flags_seen_field
9205 , mail->flags_deleted_field
9206 , mail->flags_flagged_field
9207 , mail->flags_answered_field
9208 , mail->flags_recent_field
9209 , mail->flags_draft_field
9210 , mail->flags_forwarded_field
9212 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9215 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9216 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9217 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9221 case UPDATE_EXTRA_FLAG:
9222 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9223 "UPDATE mail_tbl SET"
9225 ", save_status = %d"
9226 ", lock_status = %d"
9227 ", report_status = %d"
9229 " WHERE mail_id = %d AND account_id != 0"
9233 , mail->report_status
9236 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9239 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9240 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9241 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9244 case UPDATE_STICKY_EXTRA_FLAG:
9245 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9246 "UPDATE mail_tbl SET"
9248 " WHERE mail_id = %d AND account_id != 0"
9251 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9254 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9255 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9256 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9260 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9261 "UPDATE mail_tbl SET"
9262 " full_address_from = ?"
9263 ", full_address_reply = ?"
9264 ", full_address_to = ?"
9265 ", full_address_cc = ?"
9266 ", full_address_bcc = ?"
9267 ", full_address_return = ?"
9269 ", file_path_plain = ?"
9271 ", flags_seen_field = ?"
9272 ", flags_deleted_field = ?"
9273 ", flags_flagged_field = ?"
9274 ", flags_answered_field = ?"
9275 ", flags_recent_field = ?"
9276 ", flags_draft_field = ?"
9277 ", flags_forwarded_field = ?"
9281 ", report_status = ?"
9283 ", file_path_html = ?"
9284 ", file_path_mime_entity = ?"
9286 ", preview_text = ?"
9287 ", body_download_status = ?"
9288 ", attachment_count = ?"
9289 ", inline_content_count = ?"
9290 ", meeting_request_status = ?"
9291 ", message_class = ?"
9294 ", scheduled_sending_time = ?"
9295 ", remaining_resend_times = ?"
9297 ", replied_time = ?"
9298 ", forwarded_time = ?"
9299 ", default_charset = ?"
9300 ", eas_data_length = ?"
9302 " WHERE mail_id = %d AND account_id != 0"
9306 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9307 EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
9308 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9309 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9311 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
9312 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
9313 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
9314 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
9315 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
9316 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
9317 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9318 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9319 _bind_stmt_field_data_time_t(hStmt, i++, mail->date_time);
9320 _bind_stmt_field_data_char(hStmt, i++, mail->flags_seen_field);
9321 _bind_stmt_field_data_char(hStmt, i++, mail->flags_deleted_field);
9322 _bind_stmt_field_data_char(hStmt, i++, mail->flags_flagged_field);
9323 _bind_stmt_field_data_char(hStmt, i++, mail->flags_answered_field);
9324 _bind_stmt_field_data_char(hStmt, i++, mail->flags_recent_field);
9325 _bind_stmt_field_data_char(hStmt, i++, mail->flags_draft_field);
9326 _bind_stmt_field_data_char(hStmt, i++, mail->flags_forwarded_field);
9327 _bind_stmt_field_data_int(hStmt, i++, mail->priority);
9328 _bind_stmt_field_data_int(hStmt, i++, mail->save_status);
9329 _bind_stmt_field_data_int(hStmt, i++, mail->lock_status);
9330 _bind_stmt_field_data_int(hStmt, i++, mail->report_status);
9331 _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
9332 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9333 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9334 _bind_stmt_field_data_int(hStmt, i++, mail->mail_size);
9335 _bind_stmt_field_data_nstring(hStmt, i++, (char*)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9336 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9337 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9338 _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
9339 _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
9340 _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
9341 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9342 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9343 _bind_stmt_field_data_int(hStmt, i++, mail->scheduled_sending_time);
9344 _bind_stmt_field_data_int(hStmt, i++, mail->remaining_resend_times);
9345 _bind_stmt_field_data_int(hStmt, i++, mail->tag_id);
9346 _bind_stmt_field_data_int(hStmt, i++, mail->replied_time);
9347 _bind_stmt_field_data_int(hStmt, i++, mail->forwarded_time);
9348 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
9349 _bind_stmt_field_data_int(hStmt, i++, mail->eas_data_length);
9350 _bind_stmt_field_data_blob(hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
9353 case UPDATE_DATETIME: {
9354 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9355 "UPDATE mail_tbl SET"
9356 " date_time = '%ld'"
9357 " WHERE mail_id = %d AND account_id != 0"
9361 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9362 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9363 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9367 case UPDATE_FROM_CONTACT_INFO:
9368 EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_FROM_CONTACT_INFO");
9369 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9370 "UPDATE mail_tbl SET"
9371 " email_address_sender = ?,"
9372 " WHERE mail_id = %d",
9377 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9378 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9379 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9381 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9384 case UPDATE_TO_CONTACT_INFO:
9385 EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_TO_CONTACT_INFO");
9386 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9387 "UPDATE mail_tbl SET"
9388 " email_address_recipient = ?,"
9389 " WHERE mail_id = %d",
9394 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9395 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9396 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9398 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9401 case UPDATE_ALL_CONTACT_INFO:
9402 EM_DEBUG_LOG("emstorage_change_mail_field - mail change type is UPDATE_ALL_CONTACT_INFO");
9403 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9404 "UPDATE mail_tbl SET"
9405 " email_address_sender = ?,"
9406 " email_address_recipient = ?,"
9407 " WHERE mail_id = %d",
9412 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9413 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9414 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9416 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9417 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9421 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
9422 case UPDATE_PARTIAL_BODY_DOWNLOAD:
9424 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9425 "UPDATE mail_tbl SET"
9426 " body_download_status = ?"
9427 ", file_path_plain = ?"
9428 ", file_path_html = ?"
9429 ", file_path_mime_entity = ?"
9430 ", attachment_count = ?"
9431 ", inline_content_count = ?"
9432 ", preview_text = ?"
9435 " WHERE mail_id = %d"
9439 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9440 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9441 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9444 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9445 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9446 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9447 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9448 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9449 _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
9450 _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9451 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9452 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9457 case UPDATE_FILE_PATH:
9458 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9459 "UPDATE mail_tbl SET"
9460 ", file_path_plain = ?"
9461 ", file_path_html = ?"
9462 ", file_path_mime_entity = ?"
9463 " WHERE mail_id = %d"
9467 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9468 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
9469 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9470 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9472 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9473 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9474 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9478 EM_DEBUG_LOG(" type[%d]", type);
9480 error = EMAIL_ERROR_INVALID_PARAM;
9484 if (hStmt != NULL) {
9486 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9487 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
9488 ("sqlite3_step fail:%d", rc));
9489 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9490 ("sqlite3_step fail:%d", rc));
9491 rc = sqlite3_changes(local_db_handle);
9493 EM_DEBUG_LOG(" no matched mail found...");
9494 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9499 if (mail->account_id == 0) {
9500 emstorage_mail_tbl_t* mail_for_account_tbl = NULL;
9501 if (!emstorage_get_mail_field_by_id(multi_user_name, mail_id, RETRIEVE_ACCOUNT, &mail_for_account_tbl, true, &error) || !mail_for_account_tbl) {
9502 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id error [%d]", error);
9505 mail->account_id = mail_for_account_tbl->account_id;
9506 if (mail_for_account_tbl)
9507 emstorage_free_mail(&mail_for_account_tbl, 1, NULL);
9513 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9515 if (hStmt != NULL) {
9516 rc = sqlite3_finalize(hStmt);
9517 if (rc != SQLITE_OK) {
9518 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9519 error = EMAIL_ERROR_DB_FAILURE;
9524 if (error == EMAIL_ERROR_NONE && move_flag != 1 && transaction) {
9525 if (!emstorage_get_mailbox_id_by_mailbox_type(multi_user_name, mail->account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &mailbox_id, false, &error))
9526 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_id_by_mailbox_type error [%d]", error);
9528 if (mail->mailbox_id == mailbox_id) {
9529 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
9530 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, mailbox_id_param_string, type))
9531 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventerror [ NOTI_MAIL_UPDATE ] >>>> ");
9533 /* h.gahlaut@samsung.com: Jan 10, 2011 Publishing noti to refresh outbox when email sending status changes */
9534 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, NULL, type))
9535 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventerror [ NOTI_MAIL_UPDATE ]");
9539 if (err_code != NULL)
9542 EM_DEBUG_FUNC_END("ret [%d]", ret);
9546 INTERNAL_FUNC int emstorage_increase_mail_id(char *multi_user_name, int *mail_id, int transaction, int *err_code)
9548 EM_DEBUG_FUNC_BEGIN("mail_id[%p], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9550 int rc, ret = false;
9551 int error = EMAIL_ERROR_NONE;
9552 int latest_mail_id = 0;
9553 sqlite3 *local_db_handle = NULL;
9554 char *sql = "SELECT MAX(mail_id) FROM mail_tbl;";
9555 char **result = NULL;
9557 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9558 _timedlock_shm_mutex(mapped_for_generating_mail_id, 2);
9559 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9561 ret = vconf_get_int(VCONF_KEY_LATEST_MAIL_ID, &latest_mail_id);
9562 if (ret < 0 || latest_mail_id == 0) {
9563 EM_DEBUG_LOG("vconf_get_int() failed [%d] or latest_mail_id is zero", ret);
9565 local_db_handle = emstorage_get_db_connection(multi_user_name);
9567 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9568 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
9569 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9570 if (NULL == result[1])
9573 rc = atoi(result[1]) + 1;
9575 sqlite3_free_table(result);
9576 latest_mail_id = rc;
9581 ret = vconf_set_int(VCONF_KEY_LATEST_MAIL_ID, latest_mail_id);
9584 *mail_id = latest_mail_id;
9586 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9587 _unlockshm_mutex(mapped_for_generating_mail_id);
9588 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9594 if (err_code != NULL)
9597 EM_DEBUG_FUNC_END("ret [%d]", ret);
9601 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)
9603 EM_PROFILE_BEGIN(profile_emstorage_add_mail);
9604 EM_DEBUG_FUNC_BEGIN("mail_tbl_data[%p], get_id[%d], transaction[%d], err_code[%p]", mail_tbl_data, get_id, transaction, err_code);
9606 if (!mail_tbl_data) {
9607 EM_DEBUG_EXCEPTION("mail_tbl_data[%p], get_id[%d]", mail_tbl_data, get_id);
9608 if (err_code != NULL)
9609 *err_code = EMAIL_ERROR_INVALID_PARAM;
9613 int rc, ret = false;
9614 int error = EMAIL_ERROR_NONE;
9615 char sql_query_string[QUERY_SIZE] = {0, };
9616 DB_STMT hStmt = NULL;
9618 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9620 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9623 /* increase unique id */
9624 char *sql = "SELECT max(rowid) FROM mail_tbl;";
9627 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9628 /* EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
9629 ("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle))); */
9630 if (rc != SQLITE_OK) {
9631 EM_DEBUG_EXCEPTION("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle));
9632 error = EMAIL_ERROR_DB_FAILURE;
9633 sqlite3_free_table(result);
9637 if (NULL == result[1])
9640 rc = atoi(result[1])+1;
9642 sqlite3_free_table(result);
9644 mail_tbl_data->mail_id = rc;
9645 mail_tbl_data->thread_id = rc;
9648 if (mail_tbl_data->date_time == 0)
9649 mail_tbl_data->date_time = time(NULL);
9651 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9652 "INSERT INTO mail_tbl VALUES "
9654 ", ?" /* account_id */
9655 ", ?" /* mailbox_id */
9656 ", ?" /* mailbox_type */
9659 ", ?" /* date_time */
9660 ", ?" /* server_mail_status */
9661 ", ?" /* server_mailbox_name */
9662 ", ?" /* server_mail_id */
9663 ", ?" /* message_id */
9665 ", ?" /* reference_mail_id */
9666 ", ?" /* full_address_from */
9667 ", ?" /* full_address_reply */
9668 ", ?" /* full_address_to */
9669 ", ?" /* full_address_cc */
9671 ", ?" /* full_address_bcc */
9672 ", ?" /* full_address_return */
9673 ", ?" /* email_address_sender */
9674 ", ?" /* email_address_recipient */
9675 ", ?" /* alias_sender */
9677 ", ?" /* alias_recipient */
9678 ", ?" /* body_download_status */
9679 ", ?" /* file_path_plain */
9680 ", ?" /* file_path_html */
9681 ", ?" /* file_path_mime_entity */
9683 ", ?" /* mail_size */
9684 ", ?" /* flags_seen_field */
9685 ", ?" /* flags_deleted_field */
9686 ", ?" /* flags_flagged_field */
9687 ", ?" /* flags_answered_field */
9689 ", ?" /* flags_recent_field */
9690 ", ?" /* flags_draft_field */
9691 ", ?" /* flags_forwarded_field */
9692 ", ?" /* DRM_status */
9693 ", ?" /* priority */
9695 ", ?" /* save_status */
9696 ", ?" /* lock_status */
9697 ", ?" /* report_status */
9698 ", ?" /* attachment_count */
9699 ", ?" /* inline_content_count */
9701 ", ?" /* thread_id */
9702 ", ?" /* thread_item_count */
9703 ", ?" /* preview_text */
9704 ", ?" /* meeting_request_status */
9705 ", ?" /* message_class */
9707 ", ?" /* digest_type */
9708 ", ?" /* smime_type */
9709 ", ?" /* scheduled_sending_time */
9710 ", ?" /* remaining_resend_times */
9713 ", ?" /* replied_time */
9714 ", ?" /* forwarded_time */
9715 ", ?" /* default charset */
9716 ", ?" /* eas_data_length */
9717 ", ?" /* eas_data */
9718 ", ?" /* user_name */
9721 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle,
9723 EM_SAFE_STRLEN(sql_query_string),
9727 if (rc != SQLITE_OK) {
9728 EM_DEBUG_EXCEPTION("sqlite3_prepare error [%d] [%s] SQL(%s) ",
9729 rc, sql_query_string, sqlite3_errmsg(local_db_handle));
9730 error = EMAIL_ERROR_DB_FAILURE;
9734 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mail_id);
9735 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, mail_tbl_data->account_id);
9736 _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_id);
9737 _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_type);
9738 _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9740 _bind_stmt_field_data_int(hStmt, DATETIME_IDX_IN_MAIL_TBL, mail_tbl_data->date_time);
9741 _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->server_mail_status);
9742 _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);
9743 _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);
9744 _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);
9746 _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, mail_tbl_data->reference_mail_id);
9747 _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);
9748 _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);
9749 _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);
9750 _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);
9752 _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);
9753 _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);
9754 _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);
9755 _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);
9756 _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);
9758 _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);
9759 _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->body_download_status);
9760 _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);
9761 _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);
9762 _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);
9764 _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, mail_tbl_data->mail_size);
9765 _bind_stmt_field_data_int(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_seen_field);
9766 _bind_stmt_field_data_int(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_deleted_field);
9767 _bind_stmt_field_data_int(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_flagged_field);
9768 _bind_stmt_field_data_int(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_answered_field);
9770 _bind_stmt_field_data_int(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_recent_field);
9771 _bind_stmt_field_data_int(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_draft_field);
9772 _bind_stmt_field_data_int(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_forwarded_field);
9773 _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->DRM_status);
9774 _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, mail_tbl_data->priority);
9776 _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->save_status);
9777 _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->lock_status);
9778 _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->report_status);
9779 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->attachment_count);
9780 _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->inline_content_count);
9782 _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, mail_tbl_data->thread_id);
9783 _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->thread_item_count);
9784 _bind_stmt_field_data_nstring(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9785 _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->meeting_request_status);
9786 _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, mail_tbl_data->message_class);
9788 _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->digest_type);
9789 _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->smime_type);
9790 _bind_stmt_field_data_int(hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->scheduled_sending_time);
9791 _bind_stmt_field_data_int(hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, mail_tbl_data->remaining_resend_times);
9792 _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, mail_tbl_data->tag_id);
9794 _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->replied_time);
9795 _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->forwarded_time);
9796 _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);
9797 _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, mail_tbl_data->eas_data_length);
9798 _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, (void*)mail_tbl_data->eas_data, mail_tbl_data->eas_data_length);
9799 _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);
9801 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9802 if (rc == SQLITE_FULL) {
9803 EM_DEBUG_EXCEPTION("sqlite3_step error [%d]", rc);
9804 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
9807 if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
9808 EM_DEBUG_EXCEPTION("sqlite3_step error [%d]", rc);
9809 error = EMAIL_ERROR_DB_FAILURE;
9815 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9817 if (hStmt != NULL) {
9818 rc = sqlite3_finalize(hStmt);
9819 if (rc != SQLITE_OK) {
9820 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9821 error = EMAIL_ERROR_DB_FAILURE;
9825 if (err_code != NULL)
9828 EM_PROFILE_END(profile_emstorage_add_mail);
9829 EM_DEBUG_FUNC_END("ret [%d]", ret);
9833 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)
9835 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);
9837 int ret = false, i, cur_conditional_clause = 0;
9838 int error = EMAIL_ERROR_NONE;
9839 int target_account_id;
9840 int conditional_clause_len = 0;
9841 char *sql_query_string = NULL, *conditional_clause = NULL;
9842 emstorage_mailbox_tbl_t *result_mailbox = NULL;
9843 email_mailbox_type_e target_mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED;
9844 char* target_mailbox_name = NULL;
9846 if (!mail_ids || input_mailbox_id <= 0) {
9847 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9848 if (err_code != NULL)
9849 *err_code = EMAIL_ERROR_INVALID_PARAM;
9853 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9855 if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &result_mailbox)) != EMAIL_ERROR_NONE || !result_mailbox) {
9856 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", error);
9857 if (err_code != NULL)
9862 if (result_mailbox->mailbox_name) {
9863 if (strstr(result_mailbox->mailbox_name, "'")) {
9864 target_mailbox_name = em_replace_all_string(result_mailbox->mailbox_name, "'", "''");
9866 target_mailbox_name = strdup(result_mailbox->mailbox_name);
9870 target_mailbox_type = result_mailbox->mailbox_type;
9871 target_account_id = result_mailbox->account_id;
9872 emstorage_free_mailbox(&result_mailbox, 1, NULL);
9874 conditional_clause_len = (sizeof(char) * 8 * number_of_mails) + 512;
9875 conditional_clause = em_malloc(conditional_clause_len);
9876 if (conditional_clause == NULL) {
9877 EM_DEBUG_EXCEPTION("em_mallocfailed");
9878 error = EMAIL_ERROR_OUT_OF_MEMORY;
9881 cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE mail_id in (");
9883 for (i = 0; i < number_of_mails; i++)
9884 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
9887 char *last_comma = rindex(conditional_clause, ',');
9888 if (last_comma) *last_comma = ')'; /* replace , with) */
9890 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9892 /* Updating a mail_tbl */
9894 sql_query_string = em_malloc(conditional_clause_len);
9895 if (sql_query_string == NULL) {
9896 EM_DEBUG_EXCEPTION("em_mallocfailed");
9897 error = EMAIL_ERROR_OUT_OF_MEMORY;
9900 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);
9901 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9903 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9904 if (error != EMAIL_ERROR_NONE) {
9905 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9909 /* Updating a mail_attachment_tbl */
9910 memset(sql_query_string, 0x00, conditional_clause_len);
9911 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);
9912 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9913 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9914 if (error != EMAIL_ERROR_NONE) {
9915 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9919 /* Updating a mail_meeting_tbl */
9920 memset(sql_query_string, 0x00, conditional_clause_len);
9921 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);
9922 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9923 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9924 if (error != EMAIL_ERROR_NONE) {
9925 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9929 #ifdef __FEATURE_BODY_SEARCH__
9930 /* Updating mail_text_tbl */
9931 memset(sql_query_string, 0x00, conditional_clause_len);
9932 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);
9933 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9934 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9935 if (error != EMAIL_ERROR_NONE) {
9936 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9941 /* Updating a mail_read_mail_uid_tbl */
9942 memset(conditional_clause, 0x00, conditional_clause_len);
9943 cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE local_uid in (");
9945 for (i = 0; i < number_of_mails; i++)
9946 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
9949 last_comma = rindex(conditional_clause, ',');
9950 if (last_comma) *last_comma = ')'; /* replace , with) */
9952 memset(sql_query_string, 0x00, conditional_clause_len);
9953 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);
9954 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9955 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9956 if (error != EMAIL_ERROR_NONE) {
9957 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9964 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9966 EM_SAFE_FREE(target_mailbox_name);
9967 EM_SAFE_FREE(conditional_clause);
9968 EM_SAFE_FREE(sql_query_string);
9970 if (err_code != NULL)
9973 EM_DEBUG_FUNC_END("ret [%d]", ret);
9977 INTERNAL_FUNC int emstorage_delete_mail(char *multi_user_name, int mail_id, int from_server, int transaction, int *err_code)
9979 EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9982 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
9983 if (err_code != NULL)
9984 *err_code = EMAIL_ERROR_INVALID_PARAM;
9989 int error = EMAIL_ERROR_NONE;
9990 char sql_query_string[QUERY_SIZE] = {0, };
9991 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9992 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9994 memset(sql_query_string, 0x00, sizeof(sql_query_string));
9996 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id = %d ", mail_id);
9997 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9998 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9999 if (error != EMAIL_ERROR_NONE) {
10000 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10007 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10009 if (err_code != NULL)
10012 EM_DEBUG_FUNC_END("ret [%d]", ret);
10016 INTERNAL_FUNC int emstorage_delete_multiple_mails(char *multi_user_name, int mail_ids[], int number_of_mails, int transaction, int *err_code)
10018 EM_DEBUG_FUNC_BEGIN("mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", mail_ids, number_of_mails, transaction, err_code);
10020 int ret = false, i, cur_sql_query_string = 0;
10021 int error = EMAIL_ERROR_NONE;
10022 int query_size = 0;
10023 char *sql_query_string = NULL;
10026 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10027 if (err_code != NULL)
10028 *err_code = EMAIL_ERROR_INVALID_PARAM;
10032 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10033 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10035 query_size = (sizeof(char) * 8 * number_of_mails) + 512;
10036 sql_query_string = em_malloc(query_size);
10037 if (sql_query_string == NULL) {
10038 EM_DEBUG_EXCEPTION("em_mallocfailed");
10039 error = EMAIL_ERROR_OUT_OF_MEMORY;
10043 cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_tbl WHERE mail_id in (");
10045 for (i = 0; i < number_of_mails; i++)
10046 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
10048 /* prevent 34414 */
10049 char *last_comma = rindex(sql_query_string, ',');
10050 if (last_comma != NULL) *last_comma = ')'; /* replace , with) */
10052 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10053 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10054 if (error != EMAIL_ERROR_NONE) {
10055 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10059 #ifdef __FEATURE_BODY_SEARCH__
10060 /* delete mail_text from mail_text_tbl */
10061 cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_text_tbl WHERE mail_id in (");
10063 for (i = 0; i < number_of_mails; i++)
10064 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
10066 last_comma = rindex(sql_query_string, ',');
10067 *last_comma = ')'; /* replace , with) */
10069 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10070 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10071 if (error != EMAIL_ERROR_NONE) {
10072 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10080 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10082 EM_SAFE_FREE(sql_query_string);
10083 if (err_code != NULL)
10086 EM_DEBUG_FUNC_END("ret [%d]", ret);
10090 INTERNAL_FUNC int emstorage_delete_mail_by_account(char *multi_user_name, int account_id, int transaction, int *err_code)
10092 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
10094 if (account_id < FIRST_ACCOUNT_ID) {
10095 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
10097 if (err_code != NULL)
10098 *err_code = EMAIL_ERROR_INVALID_PARAM;
10102 int rc, ret = false;
10103 int error = EMAIL_ERROR_NONE;
10104 char sql_query_string[QUERY_SIZE] = {0, };
10106 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10107 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10109 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d", account_id);
10110 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10111 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10112 if (error != EMAIL_ERROR_NONE) {
10113 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10117 rc = sqlite3_changes(local_db_handle);
10119 EM_DEBUG_LOG(" no mail found...");
10120 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10123 /* Delete all mails mail_read_mail_uid_tbl table based on account id */
10124 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
10125 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10126 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10127 if (error != EMAIL_ERROR_NONE) {
10128 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10132 rc = sqlite3_changes(local_db_handle);
10134 EM_DEBUG_LOG("No mail found...");
10135 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10138 #ifdef __FEATURE_BODY_SEARCH__
10139 /* Delete all mail_text in mail_text_tbl table based on account id */
10140 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE account_id = %d", account_id);
10141 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10142 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10143 if (error != EMAIL_ERROR_NONE) {
10144 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10148 rc = sqlite3_changes(local_db_handle);
10150 EM_DEBUG_LOG("No mail found...");
10151 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10158 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10160 if (error == EMAIL_ERROR_NONE) {
10161 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_WITH_ACCOUNT, account_id, 0 , NULL, 0))
10162 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAIL_DELETE_ALL ]");
10165 if (err_code != NULL)
10168 EM_DEBUG_FUNC_END("ret [%d]", ret);
10172 INTERNAL_FUNC int emstorage_delete_mail_by_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox, int transaction, int *err_code)
10174 EM_DEBUG_FUNC_BEGIN("mailbox[%p], transaction[%d], err_code[%p]", mailbox, transaction, err_code);
10176 if (mailbox == NULL) {
10177 EM_DEBUG_EXCEPTION("mailbox [%p]", mailbox);
10178 if (err_code != NULL)
10179 *err_code = EMAIL_ERROR_INVALID_PARAM;
10184 int error = EMAIL_ERROR_NONE;
10185 char sql_query_string[QUERY_SIZE] = {0, };
10186 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10187 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10189 if (strcmp(mailbox->mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
10190 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);
10191 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10192 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10193 if (error != EMAIL_ERROR_NONE) {
10194 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10198 /* Delete Mails from mail_read_mail_uid_tbl */
10199 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);
10200 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10201 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10202 if (error != EMAIL_ERROR_NONE) {
10203 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10207 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);
10208 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10209 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10210 if (error != EMAIL_ERROR_NONE) {
10211 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10215 /* Delete Mails from mail_read_mail_uid_tbl */
10216 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);
10217 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10218 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10219 if (error != EMAIL_ERROR_NONE) {
10220 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10224 #ifdef __FEATURE_BODY_SEARCH__
10225 /* Delete Mails from mail_text_tbl */
10226 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);
10227 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10228 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10229 if (error != EMAIL_ERROR_NONE) {
10230 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10238 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10240 if (error == EMAIL_ERROR_NONE) {
10241 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_ALL, mailbox->account_id, mailbox->mailbox_id , mailbox->mailbox_name, 0))
10242 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventFailed [ NOTI_MAIL_DELETE_ALL ] >>>> ");
10245 if (err_code != NULL)
10248 EM_DEBUG_FUNC_END("ret [%d]", ret);
10252 INTERNAL_FUNC int emstorage_free_mail(emstorage_mail_tbl_t** mail_list, int count, int *err_code)
10254 EM_DEBUG_FUNC_BEGIN("mail_list[%p], count[%d], err_code[%p]", mail_list, count, err_code);
10257 if ((mail_list == NULL) || (*mail_list == NULL)) {
10258 EM_DEBUG_EXCEPTION("mail_ilst[%p], count[%d]", mail_list, count);
10261 *err_code = EMAIL_ERROR_INVALID_PARAM;
10265 emstorage_mail_tbl_t* p = *mail_list;
10268 for (; i < count; i++, p++) {
10269 EM_SAFE_FREE(p->server_mailbox_name);
10270 EM_SAFE_FREE(p->server_mail_id);
10271 EM_SAFE_FREE(p->full_address_from);
10272 EM_SAFE_FREE(p->full_address_reply);
10273 EM_SAFE_FREE(p->full_address_to);
10274 EM_SAFE_FREE(p->full_address_cc);
10275 EM_SAFE_FREE(p->full_address_bcc);
10276 EM_SAFE_FREE(p->full_address_return);
10277 EM_SAFE_FREE(p->subject);
10278 EM_SAFE_FREE(p->file_path_plain);
10279 EM_SAFE_FREE(p->file_path_html);
10280 EM_SAFE_FREE(p->file_path_mime_entity);
10281 EM_SAFE_FREE(p->message_id);
10282 EM_SAFE_FREE(p->email_address_sender);
10283 EM_SAFE_FREE(p->email_address_recipient);
10284 EM_SAFE_FREE(p->preview_text);
10285 EM_SAFE_FREE(p->alias_sender);
10286 EM_SAFE_FREE(p->alias_recipient);
10287 EM_SAFE_FREE(p->default_charset);
10288 EM_SAFE_FREE(p->pgp_password);
10289 EM_SAFE_FREE(p->eas_data);
10290 EM_SAFE_FREE(p->user_name);
10292 EM_SAFE_FREE(*mail_list);
10295 if (err_code != NULL)
10296 *err_code = EMAIL_ERROR_NONE;
10298 EM_DEBUG_FUNC_END();
10302 #ifdef __FEATURE_BODY_SEARCH__
10303 INTERNAL_FUNC void emstorage_free_mail_text(emstorage_mail_text_tbl_t** mail_text_list, int count, int *err_code)
10305 EM_DEBUG_FUNC_BEGIN("mail_text_list[%p], count[%d], err_code[%p]", mail_text_list, count, err_code);
10308 if ((mail_text_list == NULL) || (*mail_text_list == NULL)) {
10309 EM_DEBUG_LOG("Nothing to free: mail_text_list[%p]", mail_text_list);
10313 emstorage_mail_text_tbl_t *p = *mail_text_list;
10316 for (; i < count; i++, p++) {
10317 EM_SAFE_FREE(p->body_text);
10319 EM_SAFE_FREE(*mail_text_list);
10322 EM_DEBUG_FUNC_END();
10326 INTERNAL_FUNC int emstorage_get_attachment_count(char *multi_user_name, int mail_id, int *count, int transaction, int *err_code)
10328 EM_DEBUG_FUNC_BEGIN("mail_id[%d], count[%p], transaction[%d], err_code[%p]", mail_id, count, transaction, err_code);
10330 if (mail_id <= 0 || !count) {
10331 EM_DEBUG_EXCEPTION("mail_id[%d], count[%p]", mail_id, count);
10332 if (err_code != NULL)
10333 *err_code = EMAIL_ERROR_INVALID_PARAM;
10337 int rc = -1, ret = false;
10338 int error = EMAIL_ERROR_NONE;
10339 char sql_query_string[QUERY_SIZE] = {0, };
10341 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10342 EMSTORAGE_START_READ_TRANSACTION(transaction);
10344 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
10348 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10349 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10350 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10352 *count = atoi(result[1]);
10353 sqlite3_free_table(result);
10358 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10360 if (err_code != NULL)
10363 EM_DEBUG_FUNC_END("ret [%d]", ret);
10367 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)
10369 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);
10371 if (input_mail_id <= 0 || !output_attachment_list || !output_attachment_count) {
10372 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10373 return EMAIL_ERROR_INVALID_PARAM;
10376 int error = EMAIL_ERROR_NONE;
10379 char **result = NULL;
10380 char sql_query_string[QUERY_SIZE] = {0, };
10381 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10382 DB_STMT hStmt = NULL;
10383 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10385 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
10386 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", input_mail_id);
10387 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10388 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10389 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10391 *output_attachment_count = atoi(result[1]);
10392 sqlite3_free_table(result);
10394 if (*output_attachment_count == 0) {
10395 error = EMAIL_ERROR_NONE;
10399 p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * (*output_attachment_count));
10402 EM_DEBUG_EXCEPTION("em_mallocfailed...");
10403 error = EMAIL_ERROR_OUT_OF_MEMORY;
10407 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE mail_id = %d ORDER BY attachment_id", input_mail_id);
10408 EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
10410 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10411 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)));
10413 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10414 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
10416 if (rc == SQLITE_DONE) {
10417 EM_DEBUG_EXCEPTION("no matched attachment found...");
10418 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10421 for (i = 0; i < *output_attachment_count; i++) {
10422 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10423 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10424 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10425 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10426 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10427 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10428 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10429 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10430 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10431 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10432 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10433 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10434 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10435 EM_DEBUG_LOG("attachment[%d].attachment_id : %d", i, p_data_tbl[i].attachment_id);
10436 EM_DEBUG_LOG("attachment_mime_type : %s", p_data_tbl[i].attachment_mime_type);
10437 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10438 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
10443 if (error == EMAIL_ERROR_NONE)
10444 *output_attachment_list = p_data_tbl;
10445 else if (p_data_tbl != NULL)
10446 emstorage_free_attachment(&p_data_tbl, *output_attachment_count, NULL);
10449 rc = sqlite3_finalize(hStmt);
10450 if (rc != SQLITE_OK) {
10451 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10452 error = EMAIL_ERROR_DB_FAILURE;
10453 if (*output_attachment_list)
10454 emstorage_free_attachment(output_attachment_list, *output_attachment_count, NULL); /* prevent */
10458 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
10460 EM_DEBUG_FUNC_END("error [%d]", error);
10464 INTERNAL_FUNC int emstorage_get_attachment(char *multi_user_name, int attachment_id, emstorage_attachment_tbl_t **attachment, int transaction, int *err_code)
10466 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], transaction[%d], err_code[%p]", attachment_id, attachment, transaction, err_code);
10468 if (attachment_id <= 0 || !attachment) {
10469 EM_DEBUG_EXCEPTION("attachment_id[%d], attachment[%p]", attachment_id, attachment);
10470 if (err_code != NULL)
10471 *err_code = EMAIL_ERROR_INVALID_PARAM;
10475 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10476 int rc, ret = false;
10477 int error = EMAIL_ERROR_NONE;
10478 char sql_query_string[QUERY_SIZE] = {0, };
10480 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10481 EMSTORAGE_START_READ_TRANSACTION(transaction);
10483 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
10485 sqlite3_stmt* hStmt = NULL;
10487 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10488 EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
10490 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10491 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10494 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10495 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10496 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10498 if (rc == SQLITE_DONE) {
10499 EM_DEBUG_LOG("no matched attachment found...");
10500 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10504 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10505 EM_DEBUG_EXCEPTION("malloc failed...");
10506 error = EMAIL_ERROR_OUT_OF_MEMORY;
10510 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10511 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10512 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10513 _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10514 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10515 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10516 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10517 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10518 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10519 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10520 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10521 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10522 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10524 #ifdef __ATTACHMENT_OPTI__
10525 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10526 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10533 *attachment = p_data_tbl;
10535 if (hStmt != NULL) {
10536 rc = sqlite3_finalize(hStmt);
10537 if (rc != SQLITE_OK) {
10538 EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10539 error = EMAIL_ERROR_DB_FAILURE;
10543 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10545 if (err_code != NULL)
10548 EM_DEBUG_FUNC_END("ret [%d]", ret);
10552 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)
10554 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);
10556 if (mail_id <= 0 || nth <= 0 || !attachment_tbl) {
10557 EM_DEBUG_EXCEPTION(" mail_id[%d], nth[%d], attachment[%p]", mail_id, nth, attachment_tbl);
10559 if (err_code != NULL)
10560 *err_code = EMAIL_ERROR_INVALID_PARAM;
10564 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10566 int rc, ret = false;
10567 int error = EMAIL_ERROR_NONE;
10568 char sql_query_string[QUERY_SIZE] = {0, };
10570 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10571 EMSTORAGE_START_READ_TRANSACTION(transaction);
10573 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));
10574 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10576 DB_STMT hStmt = NULL;
10578 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10579 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10580 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10583 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10584 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10585 ("sqlite3_step fail:%d", rc));
10587 if (rc == SQLITE_DONE) {
10588 EM_DEBUG_EXCEPTION("no matched attachment found: mail_id[%d] nth[%d]", mail_id, nth);
10589 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10593 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10594 EM_DEBUG_EXCEPTION(" malloc failed...");
10595 error = EMAIL_ERROR_OUT_OF_MEMORY;
10599 p_data_tbl->attachment_id = sqlite3_column_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10600 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10601 p_data_tbl->attachment_name = cpy_str(p);
10602 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10603 p_data_tbl->attachment_path = cpy_str(p);
10604 if ((p = (char *)sqlite3_column_text(hStmt, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10605 p_data_tbl->content_id = cpy_str(p);
10606 p_data_tbl->attachment_size = sqlite3_column_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10607 p_data_tbl->mail_id = sqlite3_column_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10608 p_data_tbl->account_id = sqlite3_column_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10609 p_data_tbl->mailbox_id = sqlite3_column_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10610 p_data_tbl->attachment_save_status = sqlite3_column_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10611 p_data_tbl->attachment_drm_type = sqlite3_column_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10612 p_data_tbl->attachment_drm_method = sqlite3_column_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10613 p_data_tbl->attachment_inline_content_status = sqlite3_column_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10614 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10615 p_data_tbl->attachment_mime_type = cpy_str(p);
10616 #ifdef __ATTACHMENT_OPTI__
10617 p_data_tbl->encoding = sqlite3_column_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10618 if ((p = (char *)sqlite3_column_text(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10619 p_data_tbl->section = cpy_str(p);
10625 *attachment_tbl = p_data_tbl;
10627 if (hStmt != NULL) {
10628 rc = sqlite3_finalize(hStmt);
10629 if (rc != SQLITE_OK) {
10630 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10631 error = EMAIL_ERROR_DB_FAILURE;
10635 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10637 if (err_code != NULL)
10640 EM_DEBUG_FUNC_END("ret [%d]", ret);
10644 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)
10646 EM_DEBUG_FUNC_BEGIN("attachment_path[%p], attachment[%p], transaction[%d], err_code[%p]", attachment_path, attachment, transaction, err_code);
10648 if (attachment_path == NULL || !attachment) {
10649 EM_DEBUG_EXCEPTION("attachment_path[%p], attachment[%p]", attachment_path, attachment);
10650 if (err_code != NULL)
10651 *err_code = EMAIL_ERROR_INVALID_PARAM;
10655 emstorage_attachment_tbl_t* p_data_tbl = NULL;
10656 int rc, ret = false;
10657 int error = EMAIL_ERROR_NONE;
10658 char sql_query_string[QUERY_SIZE] = {0, };
10660 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10661 EMSTORAGE_START_READ_TRANSACTION(transaction);
10663 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_path = '%s'", attachment_path);
10665 sqlite3_stmt* hStmt = NULL;
10667 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10668 EM_DEBUG_LOG_DEV("before sqlite3_prepare hStmt = %p", hStmt);
10670 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10671 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10674 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10675 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10676 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10678 if (rc == SQLITE_DONE) {
10679 EM_DEBUG_LOG("no matched attachment found...");
10680 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10684 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10685 EM_DEBUG_EXCEPTION("malloc failed...");
10686 error = EMAIL_ERROR_OUT_OF_MEMORY;
10690 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10691 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10692 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10693 _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10694 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10695 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10696 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10697 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10698 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10699 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10700 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10701 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10702 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10704 #ifdef __ATTACHMENT_OPTI__
10705 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10706 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10713 *attachment = p_data_tbl;
10715 if (hStmt != NULL) {
10716 rc = sqlite3_finalize(hStmt);
10717 if (rc != SQLITE_OK) {
10718 EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10719 error = EMAIL_ERROR_DB_FAILURE;
10723 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10725 if (err_code != NULL)
10728 EM_DEBUG_FUNC_END("ret [%d]", ret);
10732 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)
10734 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], attachment[%p], transaction[%d], err_code[%p]", mail_id, type, attachment, transaction, err_code);
10736 if (mail_id <= 0 || !attachment) {
10737 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], attachment[%p]", mail_id, type, attachment);
10738 if (err_code != NULL)
10739 *err_code = EMAIL_ERROR_INVALID_PARAM;
10743 int rc, ret = false;
10744 int error = EMAIL_ERROR_NONE;
10745 DB_STMT hStmt = NULL;
10746 char sql_query_string[QUERY_SIZE] = {0, };
10750 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10752 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10755 case UPDATE_MAILBOX:
10756 EM_DEBUG_LOG("UPDATE_MAILBOX");
10757 if (!attachment->mailbox_id) {
10758 EM_DEBUG_EXCEPTION(" attachment->mailbox_id[%d]", attachment->mailbox_id);
10759 error = EMAIL_ERROR_INVALID_PARAM;
10762 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10763 "UPDATE mail_attachment_tbl SET mailbox_id = ? WHERE mail_id = %d", mail_id);
10765 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10766 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10767 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10768 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10770 _bind_stmt_field_data_int(hStmt, i++, attachment->mailbox_id);
10773 case UPDATE_SAVENAME:
10774 EM_DEBUG_LOG("UPDATE_SAVENAME");
10775 if (!attachment->attachment_path) {
10776 EM_DEBUG_EXCEPTION(" attachment->attachment_path[%p]", attachment->attachment_path);
10777 error = EMAIL_ERROR_INVALID_PARAM;
10781 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10782 "UPDATE mail_attachment_tbl SET"
10783 " attachment_size = ?"
10784 ", attachment_save_status = ?"
10785 ", attachment_path = ?"
10786 " WHERE mail_id = %d"
10787 " AND attachment_id = %d"
10788 , attachment->mail_id
10789 , attachment->attachment_id);
10792 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10793 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10794 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10795 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10797 _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_size);
10798 _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_save_status);
10799 _bind_stmt_field_data_string(hStmt, i++, (char *)attachment->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10803 EM_DEBUG_LOG("type[%d]", type);
10804 error = EMAIL_ERROR_INVALID_PARAM;
10807 EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10809 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10810 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10811 ("sqlite3_step fail:%d", rc));
10816 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10818 if (hStmt != NULL) {
10819 rc = sqlite3_finalize(hStmt);
10820 if (rc != SQLITE_OK) {
10821 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10822 error = EMAIL_ERROR_DB_FAILURE;
10826 if (err_code != NULL)
10828 EM_DEBUG_FUNC_END("ret [%d]", ret);
10832 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)
10834 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);
10838 int error = EMAIL_ERROR_NONE;
10840 int account_id = 0;
10841 char sql_query_string[QUERY_SIZE] = {0, };
10842 char *replaced_mailbox_name = NULL;
10843 char *replaced_alias = NULL;
10844 sqlite3 *local_db_handle = NULL;
10845 DB_STMT hStmt = NULL;
10846 emstorage_mailbox_tbl_t *old_mailbox_data = NULL;
10848 if (input_mailbox_id <= 0 || !input_new_mailbox_name || !input_new_mailbox_alias) {
10849 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10850 return EMAIL_ERROR_INVALID_PARAM;
10853 if (strstr(input_new_mailbox_name, "'")) {
10854 replaced_mailbox_name = em_replace_all_string(input_new_mailbox_name, "'", "''");
10856 replaced_mailbox_name = strdup(input_new_mailbox_name);
10859 if (strstr(input_new_mailbox_alias, "'")) {
10860 replaced_alias = em_replace_all_string(input_new_mailbox_alias, "'", "''");
10862 replaced_alias = strdup(input_new_mailbox_alias);
10865 local_db_handle = emstorage_get_db_connection(multi_user_name);
10867 if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &old_mailbox_data)) != EMAIL_ERROR_NONE) {
10868 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", error);
10869 EM_SAFE_FREE(replaced_mailbox_name);
10870 EM_SAFE_FREE(replaced_alias);
10874 if (old_mailbox_data == NULL) {
10875 EM_DEBUG_LOG("old_mailbox_data is NULL");
10876 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
10880 account_id = old_mailbox_data->account_id;
10882 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
10884 if (input_eas_data && input_eas_data_length > 0) {
10885 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10886 "UPDATE mail_box_tbl SET"
10887 " mailbox_name = ?"
10890 ",eas_data_length = ?"
10891 " WHERE mailbox_id = ?");
10893 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10895 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10896 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10898 _bind_stmt_field_data_string(hStmt, field_idx++, input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10899 _bind_stmt_field_data_string(hStmt, field_idx++, input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10900 _bind_stmt_field_data_blob(hStmt, field_idx++, input_eas_data, input_eas_data_length);
10901 _bind_stmt_field_data_int(hStmt, field_idx++, input_eas_data_length);
10902 _bind_stmt_field_data_int(hStmt, field_idx++, input_mailbox_id);
10904 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10905 "UPDATE mail_box_tbl SET"
10906 " mailbox_name = ?"
10908 " WHERE mailbox_id = ?");
10910 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10912 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10913 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10915 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10916 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10917 _bind_stmt_field_data_int(hStmt, field_idx++ , input_mailbox_id);
10921 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10922 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
10923 ("sqlite3_step fail:%d", rc));
10924 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10925 ("sqlite3_step fail:%d", rc));
10927 if (sqlite3_changes(local_db_handle) == 0)
10928 EM_DEBUG_LOG("no mail_meeting_tbl matched...");
10933 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, error);
10934 if (error == EMAIL_ERROR_NONE) {
10935 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME, account_id, input_mailbox_id, input_new_mailbox_name, 0))
10936 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAILBOX_RENAME ] >>>> ");
10938 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME_FAIL, account_id, input_mailbox_id, input_new_mailbox_name, error))
10939 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAILBOX_RENAME_FAIL ] >>>> ");
10942 EM_SAFE_FREE(replaced_mailbox_name);
10943 EM_SAFE_FREE(replaced_alias);
10945 if (old_mailbox_data)
10946 emstorage_free_mailbox(&old_mailbox_data, 1, NULL);
10948 if (hStmt != NULL) {
10949 rc = sqlite3_finalize(hStmt);
10950 if (rc != SQLITE_OK) {
10951 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10952 error = EMAIL_ERROR_DB_FAILURE;
10956 EM_DEBUG_FUNC_END("error [%d]", error);
10960 INTERNAL_FUNC int emstorage_get_new_attachment_no(char *multi_user_name, int *attachment_no, int *err_code)
10962 EM_DEBUG_FUNC_BEGIN("attachment_no [%p], err_code[%p]", attachment_no, err_code);
10963 int rc, ret = false;
10964 int error = EMAIL_ERROR_NONE;
10965 char *sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
10968 if (!attachment_no) {
10969 EM_DEBUG_EXCEPTION("Invalid attachment");
10970 if (err_code != NULL)
10971 *err_code = EMAIL_ERROR_INVALID_PARAM;
10975 *attachment_no = -1;
10977 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10980 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
10981 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10982 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
10984 if (NULL == result[1])
10987 rc = atoi(result[1])+1;
10989 sqlite3_free_table(result);
10991 *attachment_no = rc;
10992 EM_DEBUG_LOG("attachment_no [%d]", *attachment_no);
10997 if (err_code != NULL)
11000 EM_DEBUG_FUNC_END("ret [%d]", ret);
11004 INTERNAL_FUNC int emstorage_add_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int iscopy, int transaction, int *err_code)
11006 EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], iscopy[%d], transaction[%d], err_code[%p]", attachment_tbl, iscopy, transaction, err_code);
11010 int rc, ret = false;
11011 int error = EMAIL_ERROR_NONE;
11012 char sql_query_string[QUERY_SIZE] = {0, };
11014 DB_STMT hStmt = NULL;
11015 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11017 if (!attachment_tbl) {
11018 EM_DEBUG_EXCEPTION("attachment_tbl[%p], iscopy[%d]", attachment_tbl, iscopy);
11019 if (err_code != NULL)
11020 *err_code = EMAIL_ERROR_INVALID_PARAM;
11024 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11026 sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
11028 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
11029 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
11030 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
11032 if (NULL == result[1]) rc = 1;
11033 else rc = atoi(result[1]) + 1;
11034 sqlite3_free_table(result);
11036 attachment_tbl->attachment_id = rc;
11038 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11039 "INSERT INTO mail_attachment_tbl VALUES "
11040 "(?" /* attachment_id */
11041 ", ?" /* attachment_name */
11042 ", ?" /* attachment_path */
11043 ", ?" /* content_id */
11044 ", ?" /* attachment_size */
11045 ", ?" /* mail_id */
11046 ", ?" /* account_id */
11047 ", ?" /* mailbox_id */
11048 ", ?" /* attachment_save_status */
11049 ", ?" /* attachment_drm_type */
11050 ", ?" /* attachment_drm_method */
11051 ", ?" /* attachment_inline_content_status */
11052 ", ?" /* attachment_mime_type */
11053 #ifdef __ATTACHMENT_OPTI__
11060 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11061 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11062 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11064 _bind_stmt_field_data_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_id);
11065 _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);
11066 _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);
11067 _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);
11068 _bind_stmt_field_data_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_size);
11069 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mail_id);
11070 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->account_id);
11071 _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mailbox_id);
11072 _bind_stmt_field_data_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_save_status);
11073 _bind_stmt_field_data_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_type);
11074 _bind_stmt_field_data_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_method);
11075 _bind_stmt_field_data_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_inline_content_status);
11076 _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);
11077 #ifdef __ATTACHMENT_OPTI__
11078 _bind_stmt_field_data_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->encoding);
11079 _bind_stmt_field_data_string(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->section, 0, ATTACHMENT_LEN_IN_MAIL_ATTACHMENT_TBL);
11083 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11084 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
11085 ("sqlite3_step fail:%d", rc));
11086 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11087 ("sqlite3_step fail:%d", rc));
11089 rc = sqlite3_changes(local_db_handle);
11091 EM_DEBUG_LOG(" no matched mail found...");
11092 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11099 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11101 if (hStmt != NULL) {
11102 rc = sqlite3_finalize(hStmt);
11103 if (rc != SQLITE_OK) {
11104 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11105 error = EMAIL_ERROR_DB_FAILURE;
11109 if (err_code != NULL)
11112 EM_DEBUG_FUNC_END("ret [%d]", ret);
11116 INTERNAL_FUNC int emstorage_update_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int transaction, int *err_code)
11118 EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], transaction[%d], err_code[%p]", attachment_tbl, transaction, err_code);
11120 int rc, ret = false, field_idx = 0;
11121 int error = EMAIL_ERROR_NONE;
11122 DB_STMT hStmt = NULL;
11123 char sql_query_string[QUERY_SIZE] = {0, };
11125 if (!attachment_tbl) {
11126 EM_DEBUG_EXCEPTION(" attachment_tbl[%p] ", attachment_tbl);
11128 *err_code = EMAIL_ERROR_INVALID_PARAM;
11132 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11134 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11136 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11137 "UPDATE mail_attachment_tbl SET "
11138 " attachment_name = ?"
11139 ", attachment_path = ?"
11141 ", attachment_size = ?"
11145 ", attachment_save_status = ?"
11146 ", attachment_drm_type = ?"
11147 ", attachment_drm_method = ?"
11148 ", attachment_inline_content_status = ? "
11149 ", attachment_mime_type = ? "
11150 " WHERE attachment_id = ?;");
11153 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11155 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11156 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11158 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
11159 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
11160 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->content_id, 0, CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL);
11161 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_size);
11162 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->mail_id);
11163 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->account_id);
11164 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->mailbox_id);
11165 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_save_status);
11166 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_drm_type);
11167 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_drm_method);
11168 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_inline_content_status);
11169 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_mime_type, 0, ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL);
11170 _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_id);
11173 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11174 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
11175 ("sqlite3_step fail:%d", rc));
11176 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11177 ("sqlite3_step fail:%d", rc));
11179 rc = sqlite3_changes(local_db_handle);
11181 EM_DEBUG_LOG(" no matched mail found...");
11182 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11189 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11190 if (hStmt != NULL) {
11191 rc = sqlite3_finalize(hStmt);
11192 if (rc != SQLITE_OK) {
11193 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11194 error = EMAIL_ERROR_DB_FAILURE;
11198 if (err_code != NULL)
11201 EM_DEBUG_FUNC_END("ret [%d]", ret);
11205 INTERNAL_FUNC int emstorage_delete_attachment_on_db(char *multi_user_name, int attachment_id, int transaction, int *err_code)
11207 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], transaction[%d], err_code[%p]", attachment_id, transaction, err_code);
11209 if (attachment_id < 0) {
11210 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
11211 if (err_code != NULL)
11212 *err_code = EMAIL_ERROR_INVALID_PARAM;
11217 int error = EMAIL_ERROR_NONE;
11218 char sql_query_string[QUERY_SIZE] = {0, };
11219 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11221 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11223 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
11225 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11226 if (error != EMAIL_ERROR_NONE) {
11227 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11234 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11239 EM_DEBUG_FUNC_END("ret [%d]", ret);
11243 INTERNAL_FUNC int emstorage_delete_all_attachments_of_mail(char *multi_user_name, int mail_id, int transaction, int *err_code)
11245 EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
11247 int error = EMAIL_ERROR_NONE;
11248 char sql_query_string[QUERY_SIZE] = {0, };
11249 sqlite3 *local_db_handle = NULL;
11251 if (mail_id <= 0) {
11252 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
11253 if (err_code != NULL)
11254 *err_code = EMAIL_ERROR_INVALID_PARAM;
11258 local_db_handle = emstorage_get_db_connection(multi_user_name);
11260 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11262 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
11263 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11264 if (error != EMAIL_ERROR_NONE) {
11265 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11272 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11277 EM_DEBUG_FUNC_END("ret [%d]", ret);
11281 INTERNAL_FUNC int emstorage_delete_attachment_all_on_db(char *multi_user_name, int account_id, char *mailbox, int transaction, int *err_code)
11283 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox, transaction, err_code);
11285 int error = EMAIL_ERROR_NONE;
11287 char sql_query_string[QUERY_SIZE] = {0, };
11288 char *replaced_mailbox = NULL;
11289 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11291 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11293 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl");
11295 if (account_id != ALL_ACCOUNT) /* '0' means all account */
11296 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);
11298 if (mailbox) /* NULL means all mailbox_name */ {
11299 if (strstr(mailbox, "'")) {
11300 replaced_mailbox = em_replace_all_string(mailbox, "'", "''");
11302 replaced_mailbox = strdup(mailbox);
11305 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);
11306 EM_SAFE_FREE(replaced_mailbox); /*prevent 49434*/
11309 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11310 if (error != EMAIL_ERROR_NONE) {
11311 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11318 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11320 if (err_code != NULL)
11323 EM_DEBUG_FUNC_END("ret [%d]", ret);
11327 INTERNAL_FUNC int emstorage_free_attachment(emstorage_attachment_tbl_t** attachment_tbl_list, int count, int *err_code)
11329 EM_DEBUG_FUNC_BEGIN("attachment_tbl_list[%p], count[%d], err_code[%p]", attachment_tbl_list, count, err_code);
11332 if ((attachment_tbl_list == NULL) || (*attachment_tbl_list == NULL)) {
11333 EM_DEBUG_LOG("Nothing to free: attachment_tbl_list[%p], count[%d]", attachment_tbl_list, count);
11334 if (err_code != NULL)
11335 *err_code = EMAIL_ERROR_INVALID_PARAM;
11339 emstorage_attachment_tbl_t* p = *attachment_tbl_list;
11342 for (i = 0; i < count; i++) {
11343 EM_SAFE_FREE(p[i].attachment_name);
11344 EM_SAFE_FREE(p[i].attachment_path);
11345 EM_SAFE_FREE(p[i].content_id);
11346 EM_SAFE_FREE(p[i].attachment_mime_type);
11347 #ifdef __ATTACHMENT_OPTI__
11348 EM_SAFE_FREE(p[i].section);
11353 *attachment_tbl_list = NULL;
11356 if (err_code != NULL)
11357 *err_code = EMAIL_ERROR_NONE;
11358 EM_DEBUG_FUNC_END();
11362 INTERNAL_FUNC int emstorage_begin_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11364 EM_PROFILE_BEGIN(emStorageBeginTransaction);
11367 _timedlock_shm_mutex(mapped_for_db_lock, 2);
11369 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11372 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN immediate;", NULL, NULL, NULL), rc);
11373 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11374 ("SQL(BEGIN) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11376 if (ret == false) {
11377 if (err_code != NULL) *err_code = EMAIL_ERROR_DB_FAILURE;
11380 EM_PROFILE_END(emStorageBeginTransaction);
11381 EM_DEBUG_FUNC_END("ret [%d]", ret);
11385 INTERNAL_FUNC int emstorage_commit_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11387 EM_DEBUG_FUNC_BEGIN();
11391 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11393 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
11394 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; }, ("SQL(END) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11396 if (ret == false && err_code != NULL)
11397 *err_code = EMAIL_ERROR_DB_FAILURE;
11399 _unlockshm_mutex(mapped_for_db_lock);
11401 EM_DEBUG_FUNC_END("ret [%d]", ret);
11405 INTERNAL_FUNC int emstorage_rollback_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11407 EM_DEBUG_FUNC_BEGIN();
11410 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11412 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "ROLLBACK;", NULL, NULL, NULL), rc);
11413 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11414 ("SQL(ROLLBACK) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11416 if (ret == false && err_code != NULL)
11417 *err_code = EMAIL_ERROR_DB_FAILURE;
11419 _unlockshm_mutex(mapped_for_db_lock);
11421 EM_DEBUG_FUNC_END("ret [%d]", ret);
11425 INTERNAL_FUNC int emstorage_is_mailbox_full(char *multi_user_name, int account_id, email_mailbox_t *mailbox, int *result, int *err_code)
11427 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], result[%p], err_code[%p]", account_id, mailbox, result, err_code);
11429 if (account_id < FIRST_ACCOUNT_ID || !mailbox || !result) {
11431 EM_DEBUG_EXCEPTION("Invalid Parameter. accoun_id[%d], mailbox[%p]", account_id, mailbox);
11433 if (err_code != NULL)
11434 *err_code = EMAIL_ERROR_INVALID_PARAM;
11440 int error = EMAIL_ERROR_NONE;
11441 int mail_count = 0;
11443 if (!emstorage_get_mail_count(multi_user_name, account_id, mailbox->mailbox_id, &mail_count, NULL, true, &error)) {
11444 EM_DEBUG_EXCEPTION("emstorage_get_mail_count failed [%d]", error);
11449 EM_DEBUG_LOG("mail_count[%d] mail_slot_size[%d]", mail_count, mailbox->mail_slot_size);
11450 if (mail_count >= mailbox->mail_slot_size)
11461 if (err_code != NULL)
11464 EM_DEBUG_FUNC_END("ret [%d]", ret);
11468 INTERNAL_FUNC int emstorage_clear_mail_data(char *multi_user_name, int transaction, int *err_code)
11470 EM_DEBUG_FUNC_BEGIN("transaction[%d], err_code[%p]", transaction, err_code);
11473 int error = EMAIL_ERROR_NONE;
11474 char sql_query_string[QUERY_SIZE] = {0, };
11475 const email_db_object_t* tables = _g_db_tables;
11476 const email_db_object_t* indexes = _g_db_indexes;
11478 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11479 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11481 if (!emstorage_delete_dir(MAILHOME, &error)) {
11482 EM_DEBUG_EXCEPTION(" emstorage_delete_dir failed - %d", error);
11487 mkdir(MAILHOME, DIRECTORY_PERMISSION);
11488 mkdir(MAILTEMP, DIRECTORY_PERMISSION);
11489 chmod(MAILTEMP, 0777);
11491 /* first clear index. */
11492 while (indexes->object_name) {
11493 if (indexes->data_flag) {
11494 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP index %s", indexes->object_name);
11495 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11496 if (error != EMAIL_ERROR_NONE) {
11497 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11504 while (tables->object_name) {
11505 if (tables->data_flag) {
11506 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP table %s", tables->object_name);
11507 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11508 if (error != EMAIL_ERROR_NONE) {
11509 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11519 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11521 if (err_code != NULL)
11524 EM_DEBUG_FUNC_END("ret [%d]", ret);
11527 /*======================= DB File Utils =============================================*/
11528 #include <dirent.h>
11529 #include <sys/types.h>
11530 #define DIR_SEPERATOR "/"
11532 INTERNAL_FUNC char *emstorage_make_directory_path_from_file_path(char *file_name)
11534 EM_DEBUG_FUNC_BEGIN("Filename [ %p ]", file_name);
11535 char delims[] = "/";
11536 char *result = NULL;
11537 gchar **token = NULL;
11539 token = g_strsplit_set(file_name, delims, 1);
11541 if (token && token[0]) {
11542 EM_DEBUG_LOG_SEC(">>>> Directory_name [ %s ]", token[0]);
11543 result = EM_SAFE_STRDUP(token[0]);
11545 EM_DEBUG_LOG(">>>> No Need to create Directory");
11552 INTERNAL_FUNC int emstorage_get_save_name(char *multi_user_name, int account_id, int mail_id, int atch_id,
11553 char *fname, char *move_buf, char *path_buf, int maxlen, int *err_code)
11555 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);
11556 EM_PROFILE_BEGIN(profile_emstorage_get_save_name);
11559 int error = EMAIL_ERROR_NONE;
11560 char *dir_name = NULL;
11561 char create_dir[1024] = {0};
11562 char *temp_file = NULL;
11563 char *prefix_path = NULL;
11564 char *modified_fname = NULL;
11566 if (!move_buf || !path_buf || account_id < FIRST_ACCOUNT_ID || mail_id < 0 || atch_id < 0) {
11567 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);
11568 error = EMAIL_ERROR_INVALID_PARAM;
11572 snprintf(path_buf, 512, "%s", MAILHOME);
11573 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, account_id);
11576 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, mail_id);
11579 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, atch_id);
11582 temp_file = EM_SAFE_STRDUP(fname);
11583 if (temp_file && strstr(temp_file, "/")) {
11584 dir_name = emstorage_make_directory_path_from_file_path(temp_file);
11589 snprintf(create_dir, sizeof(create_dir), "%s%s%s", path_buf, DIR_SEPERATOR, dir_name);
11590 EM_DEBUG_LOG(">>>>> DIR PATH [%s]", create_dir);
11591 mkdir(create_dir, DIRECTORY_PERMISSION);
11592 EM_SAFE_FREE(dir_name);
11596 EM_DEBUG_LOG_DEV(">>>>> fname [%s]", fname);
11598 /* Did not allow the slash */
11599 modified_fname = reg_replace_new(fname, "/", "_");
11600 EM_DEBUG_LOG("modified_fname : [%s]", modified_fname);
11602 if (modified_fname == NULL) modified_fname = g_strdup(fname);
11604 if (EM_SAFE_STRLEN(modified_fname) + EM_SAFE_STRLEN(path_buf) + strlen(DIR_SEPERATOR) > maxlen - 1) {
11605 char *modified_name = NULL;
11606 int remain_len = (maxlen - 1) - EM_SAFE_STRLEN(path_buf) - strlen(DIR_SEPERATOR);
11608 if (remain_len <= 0) {
11609 error = EMAIL_ERROR_MAX_EXCEEDED;
11613 if (remain_len > MAX_FILENAME) {
11614 remain_len = MAX_FILENAME;
11617 modified_name = em_shrink_filename(modified_fname, remain_len);
11619 if (!modified_name) {
11620 error = EMAIL_ERROR_MAX_EXCEEDED;
11624 sprintf(path_buf+EM_SAFE_STRLEN(path_buf), "%s%s", DIR_SEPERATOR, modified_name);
11625 EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11626 EM_SAFE_FREE(modified_name);
11628 if (EM_SAFE_STRLEN(modified_fname) > MAX_FILENAME - 1) {
11629 char *modified_name = NULL;
11631 modified_name = em_shrink_filename(modified_fname, MAX_FILENAME);
11632 if (!modified_name) {
11633 error = EMAIL_ERROR_MAX_EXCEEDED;
11637 sprintf(path_buf+EM_SAFE_STRLEN(path_buf), "%s%s", DIR_SEPERATOR, modified_name);
11638 EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11639 EM_SAFE_FREE(modified_name);
11641 sprintf(path_buf+EM_SAFE_STRLEN(path_buf), "%s%s", DIR_SEPERATOR, modified_fname);
11646 EM_DEBUG_LOG_SEC(">>>>> path_buf [%s]", path_buf);
11648 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11649 error = emcore_get_container_path(multi_user_name, &prefix_path);
11650 if (error != EMAIL_ERROR_NONE) {
11651 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11654 sprintf(move_buf, "%s/%s", prefix_path, path_buf);
11655 EM_DEBUG_LOG("move_buf : [%s]", move_buf);
11657 sprintf(move_buf, "%s", path_buf);
11658 EM_DEBUG_LOG("move_buf : [%s]", move_buf);
11665 EM_SAFE_FREE(temp_file);
11666 EM_SAFE_FREE(prefix_path);
11667 EM_SAFE_FREE(modified_fname);
11669 if (err_code != NULL)
11672 EM_PROFILE_END(profile_emstorage_get_save_name);
11673 EM_DEBUG_FUNC_END("ret [%d]", ret);
11678 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)
11680 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);
11682 if (!name_buf || account_id < FIRST_ACCOUNT_ID) {
11683 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);
11684 if (err_code != NULL)
11685 *err_code = EMAIL_ERROR_INVALID_PARAM;
11689 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
11692 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, mail_id);
11697 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, atch_id);
11702 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), ".DELE");
11704 EM_DEBUG_FUNC_END();
11709 INTERNAL_FUNC int emstorage_create_dir(char *multi_user_name, int account_id, int mail_id, int atch_id, int *err_code)
11711 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], err_code[%p]", account_id, mail_id, atch_id, err_code);
11712 EM_PROFILE_BEGIN(profile_emcore_save_create_dir);
11714 int error = EMAIL_ERROR_NONE;
11717 char *prefix_path = NULL;
11718 char errno_buf[ERRNO_BUF_SIZE] = {0};
11720 memset(buf, 0x00, sizeof(buf));
11722 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11723 error = emcore_get_container_path(multi_user_name, &prefix_path);
11724 if (error != EMAIL_ERROR_NONE) {
11725 EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11729 prefix_path = strdup("");
11732 if (account_id >= FIRST_ACCOUNT_ID) {
11733 SNPRINTF(buf, sizeof(buf), "%s%s%s%s%d", prefix_path,
11739 if (stat(buf, &sbuf) == 0) {
11740 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11741 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11742 error = EMAIL_ERROR_SYSTEM_FAILURE;
11746 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11747 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11748 EM_DEBUG_EXCEPTION("mkdir failed: %s", EM_STRERROR(errno_buf));
11749 error = EMAIL_ERROR_SYSTEM_FAILURE;
11751 error = EMAIL_ERROR_OUT_OF_MEMORY;
11755 if (account_id == EML_FOLDER) {
11762 int space_left_in_buffer = sizeof(buf) - EM_SAFE_STRLEN(buf);
11764 if (account_id < FIRST_ACCOUNT_ID) {
11765 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11766 error = EMAIL_ERROR_INVALID_PARAM;
11770 if (space_left_in_buffer + 10 > sizeof(buf)) {
11771 EM_DEBUG_EXCEPTION("Buffer overflowed");
11772 error = EMAIL_ERROR_OUT_OF_MEMORY;
11776 SNPRINTF(buf+EM_SAFE_STRLEN(buf), space_left_in_buffer, "%s%d", DIR_SEPERATOR, mail_id);
11778 if (stat(buf, &sbuf) == 0) {
11779 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11780 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11781 error = EMAIL_ERROR_SYSTEM_FAILURE;
11785 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11786 EM_DEBUG_EXCEPTION("mkdir failed [%s]", buf);
11787 EM_DEBUG_EXCEPTION("mkdir failed [%d][%s]", errno, EM_STRERROR(errno_buf));
11788 error = EMAIL_ERROR_SYSTEM_FAILURE;
11790 error = EMAIL_ERROR_OUT_OF_MEMORY;
11794 if (account_id == EML_FOLDER) {
11801 if (account_id < FIRST_ACCOUNT_ID || mail_id <= 0) {
11802 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11803 error = EMAIL_ERROR_INVALID_PARAM;
11807 SNPRINTF(buf+EM_SAFE_STRLEN(buf), sizeof(buf)-(EM_SAFE_STRLEN(buf)+1), "%s%d", DIR_SEPERATOR, atch_id);
11809 if (stat(buf, &sbuf) == 0) {
11810 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11811 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11812 error = EMAIL_ERROR_SYSTEM_FAILURE;
11816 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11817 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11818 error = EMAIL_ERROR_SYSTEM_FAILURE;
11820 error = EMAIL_ERROR_OUT_OF_MEMORY;
11824 if (account_id == EML_FOLDER) {
11834 EM_SAFE_FREE(prefix_path);
11836 if (err_code != NULL)
11839 EM_PROFILE_END(profile_emcore_save_create_dir);
11840 EM_DEBUG_FUNC_END("ret [%d]", ret);
11844 INTERNAL_FUNC int emstorage_copy_file(char *src_file, char *dst_file, int sync_status, int *err_code)
11846 EM_DEBUG_FUNC_BEGIN("src_file[%s], dst_file[%s], err_code[%p]", src_file, dst_file, err_code);
11847 EM_DEBUG_LOG("Using the fsync function");
11849 int error = EMAIL_ERROR_NONE;
11850 struct stat st_buf;
11855 char buf[FILE_MAX_BUFFER_SIZE] = {0};
11856 char errno_buf[ERRNO_BUF_SIZE] = {0};
11858 if (!src_file || !dst_file) {
11859 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
11861 error = EMAIL_ERROR_INVALID_PARAM;
11865 if (stat(src_file, &st_buf) < 0) {
11866 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", src_file);
11868 error = EMAIL_ERROR_SYSTEM_FAILURE; /* EMAIL_ERROR_INVALID_PATH; */
11872 error = em_open(src_file, O_RDONLY, 0, &fp_src);
11873 if (error != EMAIL_ERROR_NONE) {
11874 EM_DEBUG_EXCEPTION(">>>> Source Fail em_open %s Failed: %d", src_file, error);
11878 error = em_open(dst_file, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, &fp_dst); /*prevent 24474*/
11879 if (error != EMAIL_ERROR_NONE) {
11880 EM_DEBUG_EXCEPTION(">>>> Destination Fail em_open %s: %d", dst_file, error);
11884 memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
11886 while ((nread = read(fp_src, buf, FILE_MAX_BUFFER_SIZE)) > 0) {
11887 if (nread > 0 && nread <= FILE_MAX_BUFFER_SIZE) {
11888 EM_DEBUG_LOG("Nread Value [%d]", nread);
11890 ssize_t byte_written = 0;
11891 size_t remain_byte = nread;
11895 while (remain_byte > 0 && buf_ptr && errno == 0) {
11896 byte_written = write(fp_dst, buf_ptr, remain_byte);
11898 if (byte_written < 0) {
11899 /* interrupted by a signal */
11900 if (errno == EINTR) {
11905 EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
11906 error = EMAIL_ERROR_UNKNOWN;
11909 EM_DEBUG_LOG("NWRITTEN [%d]", byte_written);
11910 remain_byte -= byte_written;
11911 buf_ptr += byte_written;
11915 memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
11921 EM_SAFE_CLOSE(fp_src);
11923 if (fp_dst >= 0) { /*prevent 24474*/
11925 EM_DEBUG_LOG("Before fsync");
11931 if (nread < 0 || error == EMAIL_ERROR_UNKNOWN)
11934 if (err_code != NULL)
11936 EM_DEBUG_FUNC_END("ret [%d]", ret);
11939 /* create Directory if user has deleted [deepam.p@samsung.com] */
11940 INTERNAL_FUNC void emstorage_create_dir_if_delete()
11942 EM_DEBUG_FUNC_BEGIN();
11944 mkdir(DATA_PATH, DIRECTORY_PERMISSION);
11945 mkdir(EMAILPATH, DIRECTORY_PERMISSION);
11946 mkdir(MAILHOME, DIRECTORY_PERMISSION);
11947 mkdir(MAILTEMP, DIRECTORY_PERMISSION);
11948 chmod(MAILTEMP, 0777);
11950 EM_DEBUG_FUNC_END();
11953 static int _get_temp_file_name(char **filename, int *err_code)
11955 EM_DEBUG_FUNC_BEGIN("filename[%p], err_code[%p]", filename, err_code);
11958 int error = EMAIL_ERROR_NONE;
11960 if (filename == NULL) {
11961 EM_DEBUG_EXCEPTION(" filename[%p]", filename);
11962 error = EMAIL_ERROR_INVALID_PARAM;
11966 char tempname[512] = {0x00, };
11969 gettimeofday(&tv, NULL);
11972 SNPRINTF(tempname, sizeof(tempname), "%s%c%d", MAILTEMP, '/', rand());
11974 char *p = EM_SAFE_STRDUP(tempname);
11976 EM_DEBUG_EXCEPTION(" strdup failed...");
11977 error = EMAIL_ERROR_OUT_OF_MEMORY;
11986 if (err_code != NULL)
11989 EM_DEBUG_FUNC_END("ret [%d]", ret);
11993 INTERNAL_FUNC int emstorage_add_content_type(char *file_path, char *char_set, int *err_code)
11995 EM_DEBUG_FUNC_BEGIN("File path [ %p ] Character Set [ %p ] err_code [ %p]", file_path, char_set, err_code);
11997 EM_IF_NULL_RETURN_VALUE(file_path, false);
11998 EM_IF_NULL_RETURN_VALUE(char_set, false);
11999 EM_IF_NULL_RETURN_VALUE(err_code, false);
12003 struct stat st_buf;
12005 char *low_char_set = NULL;
12006 char *match_str = NULL;
12009 int error = EMAIL_ERROR_NONE;
12010 int data_count_to_written = 0;
12011 char *temp_file_name = NULL;
12013 FILE* fp_src = NULL;
12014 FILE* fp_dest = NULL;
12018 if (stat(file_path, &st_buf) < 0) {
12019 EM_DEBUG_EXCEPTION_SEC(" stat(\"%s\") failed...", file_path);
12020 error = EMAIL_ERROR_SYSTEM_FAILURE;
12024 buf_size = st_buf.st_size;
12025 EM_DEBUG_LOG(">>>> File Size [ %d ] ", buf_size);
12026 buf = (char *)calloc(1, buf_size+1);
12029 EM_DEBUG_LOG(">>> Memory cannot be allocated ");
12033 error = em_fopen(file_path, "rb", &fp_src);
12034 if (error != EMAIL_ERROR_NONE || fp_src == NULL) {
12035 EM_DEBUG_EXCEPTION_SEC(" file_path fopen failed - %s [%d]", file_path, error);
12039 if ((nread = fread(buf, 1, buf_size, fp_src)) > 0) {
12040 if (nread > 0 && nread <= buf_size) {
12041 EM_DEBUG_LOG(">>>> Nread Value [ %d ] ", nread);
12044 * 1.Add check for whether content type is there.
12045 * 2. If not based on the character set, Append it in File
12048 low_char_set = calloc(1, EM_SAFE_STRLEN(char_set) + strlen(" \" /></head>") +1); /*prevent 34359*/
12049 if (low_char_set == NULL) {
12050 EM_DEBUG_EXCEPTION("calloc failed");
12051 error = EMAIL_ERROR_OUT_OF_MEMORY;
12055 strncat(low_char_set, char_set, EM_SAFE_STRLEN(char_set));
12056 EM_DEBUG_LOG(">>>> CHAR SET [ %s ] ", low_char_set);
12057 strncat(low_char_set, " \" /></head>", strlen(" \" /></head>")); /*prevent 34359*/
12058 EM_DEBUG_LOG(">>> CHARSET [ %s ] ", low_char_set);
12059 match_str = strstr(buf, CONTENT_TYPE_DATA);
12061 if (match_str == NULL) {
12062 EM_DEBUG_LOG(">>>>emstorage_add_content_type 3 ");
12063 if (fp_src != NULL) {
12067 data_count_to_written = EM_SAFE_STRLEN(low_char_set)+strlen(CONTENT_DATA)+1; /*prevent 34359*/
12068 buf1 = (char *)calloc(1, data_count_to_written);
12071 strncat(buf1, CONTENT_DATA, strlen(CONTENT_DATA)); /*prevent 34359*/
12072 EM_DEBUG_LOG(">>>>> BUF 1 [ %s ] ", buf1);
12073 strncat(buf1, low_char_set, EM_SAFE_STRLEN(low_char_set));
12074 EM_DEBUG_LOG(">>>> HTML TAG DATA [ %s ] ", buf1);
12076 /* 1. Create a temporary file name */
12077 if (!_get_temp_file_name(&temp_file_name, &err)) {
12078 EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed - %d", err);
12079 if (err_code != NULL) *err_code = err;
12082 EM_DEBUG_LOG_SEC(">>>>>>> TEMP APPEND FILE PATH [ %s ] ", temp_file_name);
12084 /* Open the Temp file in Append mode */
12085 error = em_fopen(temp_file_name, "ab", &fp_dest);
12086 if (error != EMAIL_ERROR_NONE) {
12087 EM_DEBUG_EXCEPTION_SEC(" fopen failed - %s [%d]", temp_file_name, error);
12091 /* 2. write the Latest data */
12092 nwritten = fwrite(buf1, data_count_to_written-1, 1, fp_dest);
12094 if (nwritten > 0) {
12095 EM_DEBUG_LOG(" Latest Data : [%d ] bytes written ", nwritten);
12097 /* 3. Append old data */
12098 nwritten = fwrite(buf, nread-1, 1, fp_dest);
12100 if (nwritten <= 0) {
12101 EM_DEBUG_EXCEPTION(" Error Occured while writing Old data : [%d ] bytes written ", nwritten);
12102 error = EMAIL_ERROR_SYSTEM_FAILURE;
12105 EM_DEBUG_LOG(">>>> OLD data appended [ %d ] ", nwritten);
12107 if (!emstorage_move_file(temp_file_name, file_path, false, &err)) {
12108 EM_DEBUG_EXCEPTION(" emstorage_move_file failed - %d", err);
12114 EM_DEBUG_EXCEPTION(" Error Occured while writing New data : [%d ] bytes written ", nwritten);
12115 error = EMAIL_ERROR_SYSTEM_FAILURE;
12127 EM_SAFE_FREE(buf1);
12128 EM_SAFE_FREE(low_char_set);
12129 EM_SAFE_FREE(temp_file_name);
12131 if (fp_src != NULL) {
12136 if (fp_dest != NULL) {
12144 EM_DEBUG_FUNC_END("ret [%d]", ret);
12149 INTERNAL_FUNC int emstorage_move_file(char *src_file, char *dst_file, int sync_status, int *err_code)
12151 EM_DEBUG_FUNC_BEGIN("src_file[%p], dst_file[%p], err_code[%p]", src_file, dst_file, err_code);
12154 int error = EMAIL_ERROR_NONE;
12155 char errno_buf[ERRNO_BUF_SIZE] = {0};
12157 if (src_file == NULL || dst_file == NULL) {
12158 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
12159 error = EMAIL_ERROR_INVALID_PARAM;
12163 EM_DEBUG_LOG_SEC("src_file[%s], dst_file[%s]", src_file, dst_file);
12165 if (strcmp(src_file, dst_file) != 0) {
12166 if (rename(src_file, dst_file) != 0) {
12167 /* EM_DEBUG_EXCEPTION("%s", strerror(errno)); */
12168 if (errno == EXDEV) { /* oldpath and newpath are not on the same mounted file system. (Linux permits a file system to be mounted at multiple points, but rename() */
12169 /* does not work across different mount points, even if the same file system is mounted on both.) */
12170 EM_DEBUG_LOG("oldpath and newpath are not on the same mounted file system.");
12171 if (!emstorage_copy_file(src_file, dst_file, sync_status, &error)) {
12172 EM_DEBUG_EXCEPTION("emstorage_copy_file failed - %d", error);
12176 EM_DEBUG_LOG("src[%s] removed", src_file);
12178 if (errno == ENOENT) {
12179 struct stat temp_file_stat;
12180 if (stat(src_file, &temp_file_stat) < 0) {
12181 EM_DEBUG_EXCEPTION("no src file found [%s] : %s", src_file, EM_STRERROR(errno_buf));
12183 if (stat(dst_file, &temp_file_stat) < 0)
12184 EM_DEBUG_EXCEPTION("no dst file found [%s] : %s", dst_file, EM_STRERROR(errno_buf));
12186 error = EMAIL_ERROR_FILE_NOT_FOUND;
12189 EM_DEBUG_EXCEPTION("rename failed: %s", EM_STRERROR(errno_buf));
12190 error = EMAIL_ERROR_SYSTEM_FAILURE;
12196 EM_DEBUG_LOG("src[%s] = dst[%s]", src_file, dst_file);
12202 if (err_code != NULL)
12205 EM_DEBUG_FUNC_END("ret [%d]", ret);
12209 INTERNAL_FUNC int emstorage_delete_file(char *src_file, int *err_code)
12211 EM_DEBUG_FUNC_BEGIN("src_file[%p], err_code[%p]", src_file, err_code);
12214 int error = EMAIL_ERROR_NONE;
12216 if (src_file == NULL) {
12217 EM_DEBUG_EXCEPTION(" src_file[%p]", src_file);
12219 error = EMAIL_ERROR_INVALID_PARAM;
12223 if (remove(src_file) != 0) {
12224 if (errno != ENOENT) {
12225 EM_DEBUG_EXCEPTION(" remove failed - %d", errno);
12227 error = EMAIL_ERROR_SYSTEM_FAILURE;
12230 EM_DEBUG_EXCEPTION(" no file found...");
12232 error = EMAIL_ERROR_FILE_NOT_FOUND;
12239 if (err_code != NULL)
12242 EM_DEBUG_FUNC_END("ret [%d]", ret);
12247 INTERNAL_FUNC int emstorage_delete_dir(char *src_dir, int *err_code)
12249 EM_DEBUG_FUNC_BEGIN("src_dir[%p], err_code[%p]", src_dir, err_code);
12251 if (src_dir == NULL) {
12252 EM_DEBUG_EXCEPTION("src_dir[%p]", src_dir);
12254 if (err_code != NULL)
12255 *err_code = EMAIL_ERROR_INVALID_PARAM;
12259 int error = EMAIL_ERROR_NONE;
12266 dirp = opendir(src_dir);
12268 if (dirp == NULL) {
12269 if (errno == ENOENT) {
12270 EM_DEBUG_EXCEPTION("directory[%s] does not exist...", src_dir);
12271 if (err_code != NULL)
12272 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12275 EM_DEBUG_EXCEPTION("opendir failed [%s] [%d]", src_dir, errno);
12276 if (err_code != NULL)
12277 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12282 while ((dp = readdir(dirp))) {
12283 if (strncmp(dp->d_name, ".", 1) == 0 || strncmp(dp->d_name, "..", 2) == 0) /* prevent 34360 */
12286 SNPRINTF(buf, sizeof(buf), "%s/%s", src_dir, dp->d_name);
12288 if (lstat(buf, &sbuf) == 0 || stat(buf, &sbuf) == 0) {
12289 /* check directory */
12290 if ((sbuf.st_mode & S_IFMT) == S_IFDIR) { /* directory */
12291 /* recursive call */
12292 if (!emstorage_delete_dir(buf, &error)) {
12294 if (err_code != NULL)
12298 } else { /* file */
12299 if (remove(buf) < 0) {
12300 EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", buf, errno);
12302 if (err_code != NULL)
12303 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12308 EM_DEBUG_EXCEPTION("content does not exist...");
12313 EM_DEBUG_LOG_DEV("remove direcotory [%s]", src_dir);
12315 /* EM_DEBUG_FUNC_BEGIN(); */
12317 if (remove(src_dir) < 0) {
12318 EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", src_dir, errno);
12319 if (err_code != NULL)
12320 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12324 if (err_code != NULL)
12330 /* faizan.h@samsung.com */
12331 INTERNAL_FUNC int emstorage_update_server_uid(char *multi_user_name,
12333 char *old_server_uid,
12334 char *new_server_uid,
12337 EM_DEBUG_FUNC_BEGIN("new_server_uid[%s], old_server_uid[%s]", new_server_uid, old_server_uid);
12339 int error = EMAIL_ERROR_NONE;
12340 int transaction = true;
12341 int temp_strlen = 0;
12342 int and_operation = 0;
12343 char sql_query_string[QUERY_SIZE] = {0, };
12344 char conditional_clause_string[QUERY_SIZE] = {0};
12346 if ((mail_id <= 0 || !old_server_uid) && !new_server_uid) {
12347 EM_DEBUG_EXCEPTION("Invalid parameters");
12349 *err_code = EMAIL_ERROR_INVALID_PARAM;
12353 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12354 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12356 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE ");
12359 temp_strlen = strlen(conditional_clause_string);
12360 SNPRINTF(conditional_clause_string + temp_strlen, sizeof(conditional_clause_string) - temp_strlen,
12361 "mail_id = %d ", mail_id);
12365 if (old_server_uid) {
12366 temp_strlen = strlen(conditional_clause_string);
12367 if (!and_operation) {
12368 sqlite3_snprintf(sizeof(conditional_clause_string) - temp_strlen, conditional_clause_string + temp_strlen,
12369 "server_mail_id = '%q'", old_server_uid);
12371 sqlite3_snprintf(sizeof(conditional_clause_string) - temp_strlen, conditional_clause_string + temp_strlen,
12372 "and server_mail_id = '%q'", old_server_uid);
12376 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
12377 "UPDATE mail_tbl SET server_mail_id = '%q' %s", new_server_uid, conditional_clause_string);
12379 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12380 if (error != EMAIL_ERROR_NONE) {
12381 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12389 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12391 if (err_code != NULL)
12394 EM_DEBUG_FUNC_END("ret [%d]", ret);
12399 INTERNAL_FUNC int emstorage_update_read_mail_uid(char *multi_user_name, int mail_id, char *new_server_uid, char *mbox_name, int *err_code)
12401 EM_DEBUG_FUNC_BEGIN_SEC("mail_id[%d], new_server_uid[%s], mbox_name[%s]", mail_id, new_server_uid, mbox_name);
12404 int error = EMAIL_ERROR_NONE;
12405 char sql_query_string[QUERY_SIZE] = {0, };
12406 int transaction = true;
12408 if (!mail_id || !new_server_uid || !mbox_name) {
12409 EM_DEBUG_EXCEPTION("Invalid parameters");
12410 if (err_code != NULL)
12411 *err_code = EMAIL_ERROR_INVALID_PARAM;
12415 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12416 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12419 SNPRINTF(sql_query_string, sizeof(sql_query_string),
12420 "UPDATE mail_read_mail_uid_tbl SET server_uid=\'%s\', mailbox_id=\'%s\', mailbox_name=\'%s\' WHERE local_uid=%d ", new_server_uid, mbox_name, mbox_name, mail_id);
12422 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12423 if (error != EMAIL_ERROR_NONE) {
12424 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12432 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12434 if (err_code != NULL)
12437 EM_DEBUG_FUNC_END("ret [%d]", ret);
12442 INTERNAL_FUNC int emstorage_update_save_status(char *multi_user_name, int account_id, int *err_code)
12444 EM_DEBUG_FUNC_BEGIN();
12447 int transaction = true;
12448 int error = EMAIL_ERROR_NONE;
12449 char sql_query_string[QUERY_SIZE] = {0,};
12451 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12453 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12455 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12457 if (account_id <= ALL_ACCOUNT)
12458 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET save_status = %d WHERE (save_status = %d or save_status = %d)", EMAIL_MAIL_STATUS_NONE, EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12460 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET save_status = %d WHERE (save_status = %d or save_status = %d) and account_id = %d ", EMAIL_MAIL_STATUS_NONE, EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED, account_id);
12462 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12463 if (error != EMAIL_ERROR_NONE) {
12464 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12471 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12473 if (err_code != NULL)
12476 EM_DEBUG_FUNC_END("ret [%d]", ret);
12481 int emstorage_get_unread_mailid(char *multi_user_name, int account_id, int vip_mode, int **mail_ids, int *mail_number, int *err_code)
12483 EM_DEBUG_FUNC_BEGIN();
12485 if ((!mail_ids) || (account_id <= 0 && account_id != -1)) {
12486 EM_DEBUG_EXCEPTION(" mail_id[%p], account_id[%d] ", mail_ids, account_id);
12487 if (err_code != NULL)
12488 *err_code = EMAIL_ERROR_INVALID_PARAM;
12494 int error = EMAIL_ERROR_NONE;
12498 int *p_mail_ids = NULL;
12499 int transaction = false;
12500 char **result = NULL;
12501 char sql_query_string[QUERY_SIZE] = {0, };
12502 char temp_query_string[QUERY_SIZE] = {0,};
12503 char sql_select_query_string[QUERY_SIZE] = {0, };
12505 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12506 EMSTORAGE_START_READ_TRANSACTION(transaction);
12508 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12510 if (account_id == -1)
12511 SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string), "SELECT mail_id FROM mail_tbl WHERE flags_seen_field = 0 AND (save_status = %d or save_status = %d)", EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12513 SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string), "SELECT mail_id FROM mail_tbl WHERE account_id = %d AND flags_seen_field = 0 AND (save_status = %d or save_status = %d)", account_id, EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12516 SNPRINTF(temp_query_string, sizeof(temp_query_string), "%s AND tag_id < 0", sql_select_query_string);
12518 SNPRINTF(temp_query_string, sizeof(temp_query_string), "%s", sql_select_query_string);
12521 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s ORDER BY date_time ASC", temp_query_string);
12523 EM_DEBUG_LOG_SEC("query: [%s]", sql_query_string);
12525 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
12526 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
12527 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12529 EM_DEBUG_LOG("Count : %d", count);
12532 EM_DEBUG_EXCEPTION("no Mails found...");
12534 error = EMAIL_ERROR_MAIL_NOT_FOUND;
12538 p_mail_ids = em_malloc(count * sizeof(int));
12539 if (p_mail_ids == NULL) {
12540 EM_DEBUG_EXCEPTION("em_mallocfailed...");
12541 error = EMAIL_ERROR_OUT_OF_MEMORY;
12547 for (i = 0; i < count; i++) {
12548 _get_table_field_data_int(result, &(p_mail_ids[i]), col_index++);
12556 sqlite3_free_table(result);
12558 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12560 // sqlite3_db_release_memory(local_db_handle);
12563 if (mail_ids != NULL)
12564 *mail_ids = p_mail_ids;
12566 if (mail_number != NULL)
12567 *mail_number = count;
12569 EM_SAFE_FREE(p_mail_ids);
12572 if (err_code != NULL)
12575 EM_DEBUG_FUNC_END("ret [%d]", ret);
12579 int setting_system_command(const char *command)
12582 int pid = 0, status = 0;
12583 char *const environ[] = { NULL };
12598 argv[2] = (char *)command;
12601 execve("/bin/sh", argv, environ);
12606 if (waitpid(pid, &status, 0) == -1) {
12607 if (errno != EINTR)
12618 INTERNAL_FUNC int emstorage_mail_get_total_diskspace_usage(unsigned long *total_usage, int transaction, int *err_code)
12620 EM_DEBUG_FUNC_BEGIN("total_usage[%p], transaction[%d], err_code[%p]", total_usage, transaction, err_code);
12622 if (!total_usage) {
12623 EM_DEBUG_EXCEPTION("total_usage[%p]", total_usage);
12625 if (err_code != NULL)
12626 *err_code = EMAIL_ERROR_INVALID_PARAM;
12631 int error = EMAIL_ERROR_NONE;
12632 char syscmd[256] = {0, };
12633 char line[256] = {0, };
12634 char *line_from_file = NULL;
12636 unsigned long total_diskusage = 0;
12638 SNPRINTF(syscmd, sizeof(syscmd), "touch %s", SETTING_MEMORY_TEMP_FILE_PATH);
12639 if (setting_system_command(syscmd) == -1) {
12640 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
12642 error = EMAIL_ERROR_SYSTEM_FAILURE;
12646 SNPRINTF(syscmd, sizeof(syscmd), "du -hsk %s > %s", EMAILPATH, SETTING_MEMORY_TEMP_FILE_PATH);
12647 EM_DEBUG_LOG(" cmd : %s", syscmd);
12648 if (setting_system_command(syscmd) == -1) {
12649 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : Setting > Memory] System Command [%s] is failed", syscmd);
12651 error = EMAIL_ERROR_SYSTEM_FAILURE;
12655 error = em_fopen(SETTING_MEMORY_TEMP_FILE_PATH, "r", &fp);
12656 if (error != EMAIL_ERROR_NONE) {
12657 perror(SETTING_MEMORY_TEMP_FILE_PATH);
12661 line_from_file = fgets(line, sizeof(line), fp);
12663 if (line_from_file == NULL) {
12664 EM_DEBUG_EXCEPTION("fgets failed");
12665 error = EMAIL_ERROR_SYSTEM_FAILURE;
12668 total_diskusage = strtoul(line, NULL, 10);
12670 memset(syscmd, 0, sizeof(syscmd));
12671 SNPRINTF(syscmd, sizeof(syscmd), "rm -f %s", SETTING_MEMORY_TEMP_FILE_PATH);
12672 if (setting_system_command(syscmd) == -1) {
12673 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
12674 error = EMAIL_ERROR_SYSTEM_FAILURE;
12678 EM_DEBUG_LOG("[Setting > Memory] @@@@@ Size of Directory [%s] is %ld KB", EMAILPATH, total_diskusage);
12683 if (err_code != NULL)
12687 *total_usage = total_diskusage;
12691 if (fp) fclose(fp); /* prevent 32730 */
12693 EM_DEBUG_FUNC_END("ret [%d]", ret);
12696 #define MAILHOME_UTF8 tzplatform_mkpath(TZ_USER_DATA, "email/.email_data/7/348/UTF-8")
12698 INTERNAL_FUNC int emstorage_test(char *multi_user_name, int mail_id, int account_id, char *full_address_to, char *full_address_cc, char *full_address_bcc, int *err_code)
12700 DB_STMT hStmt = NULL;
12702 int error = EMAIL_ERROR_NONE;
12704 char sql_query_string[QUERY_SIZE] = {0, };
12706 SNPRINTF(sql_query_string, sizeof(sql_query_string),
12707 "INSERT INTO mail_tbl VALUES "
12709 ", ?" /* account_id */
12710 ", ?" /* mail_size */
12711 ", ?" /* server_mail_status */
12712 ", ?" /* server_mailbox_name */
12713 ", ?" /* server_mail_id */
12714 ", ?" /* reference_mail_id */
12715 ", ?" /* full_address_from */
12716 ", ?" /* full_address_reply */
12717 ", ?" /* full_address_to */
12718 ", ?" /* full_address_cc */
12719 ", ?" /* full_address_bcc */
12720 ", ?" /* full_address_return */
12721 ", ?" /* subject */
12722 ", ?" /* body_download_status */
12723 ", ?" /* file_path_plain */
12724 ", ?" /* file_path_html */
12725 ", ?" /* date_time */
12726 ", ?" /* flags_seen_field */
12727 ", ?" /* flags_deleted_field */
12728 ", ?" /* flags_flagged_field */
12729 ", ?" /* flags_answered_field */
12730 ", ?" /* flags_recent_field */
12731 ", ?" /* flags_draft_field */
12732 ", ?" /* flags_forwarded_field */
12733 ", ?" /* DRM_status */
12734 ", ?" /* priority */
12735 ", ?" /* save_status */
12736 ", ?" /* lock_status */
12737 ", ?" /* message_id */
12738 ", ?" /* report_status */
12739 ", ?" /* email_address_sender */
12740 ", ?" /* email_address_recipient */
12741 ", ?" /* attachment_count */
12742 ", ?" /* inline_content_count */
12743 ", ?" /* preview_text */
12744 ", ?" /* thread_id */
12745 ", ?" /* mailbox_type */
12746 ", ?" /* alias_sender */
12747 ", ?" /* alias_recipient */
12748 ", ?" /* thread_item_count */
12749 ", ?" /* meeting_request_status */
12750 ", ?" /* message_class */
12751 ", ?" /* digest_type */
12752 ", ?" /* smime_type */
12753 ", ?" /* scheduled_sending_time */
12754 ", ?" /* remaining_resend_times */
12756 ", ?" /* replied_time */
12757 ", ?" /* forwarded_time */
12758 ", ?" /* default_charset */
12759 ", ?" /* eas_data_length */
12760 ", ?" /* eas_data */
12763 int transaction = true;
12764 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12766 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12769 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12770 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
12771 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12773 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_id);
12774 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, account_id);
12775 _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, EMAIL_MAILBOX_TYPE_OUTBOX);
12776 _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, "save test - long", 1, SUBJECT_LEN_IN_MAIL_TBL);
12777 _bind_stmt_field_data_string(hStmt, DATETIME_IDX_IN_MAIL_TBL, "20100316052908", 0, DATETIME_LEN_IN_MAIL_TBL);
12778 _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, 0);
12779 _bind_stmt_field_data_string(hStmt, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL, "", 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
12780 _bind_stmt_field_data_string(hStmt, SERVER_MAIL_ID_IDX_IN_MAIL_TBL, "", 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
12781 _bind_stmt_field_data_string(hStmt, MESSAGE_ID_IDX_IN_MAIL_TBL, "", 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
12782 _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, 0);
12783 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL, "<test08@streaming.s3glab.net>", 1, FROM_LEN_IN_MAIL_TBL);
12784 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL, "", 1, REPLY_TO_LEN_IN_MAIL_TBL);
12785 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL, full_address_to, 1, TO_LEN_IN_MAIL_TBL);
12786 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL, full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
12787 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL, full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
12788 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL, "", 1, RETURN_PATH_LEN_IN_MAIL_TBL);
12789 _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL, "<sender_name@sender_host.com>", 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12790 _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL, "<recipient_name@recipient_host.com>", 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12791 _bind_stmt_field_data_string(hStmt, ALIAS_SENDER_IDX_IN_MAIL_TBL, "send_alias", 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12792 _bind_stmt_field_data_string(hStmt, ALIAS_RECIPIENT_IDX_IN_MAIL_TBL, "recipient_alias", 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12793 _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, 1);
12794 _bind_stmt_field_data_string(hStmt, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL, MAILHOME_UTF8, 0, TEXT_1_LEN_IN_MAIL_TBL);
12795 _bind_stmt_field_data_string(hStmt, FILE_PATH_HTML_IDX_IN_MAIL_TBL, "", 0, TEXT_2_LEN_IN_MAIL_TBL);
12796 _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, 4);
12797 _bind_stmt_field_data_char(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, 0);
12798 _bind_stmt_field_data_char(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, 0);
12799 _bind_stmt_field_data_char(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, 0);
12800 _bind_stmt_field_data_char(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, 0);
12801 _bind_stmt_field_data_char(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, 0);
12802 _bind_stmt_field_data_char(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, 0);
12803 _bind_stmt_field_data_char(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, 0);
12804 _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, 0);
12805 _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, 0);
12806 _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, 0);
12807 _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, 0);
12808 _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, 0);
12809 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12810 _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, 0);
12811 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12812 _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, 0);
12813 _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, 0);
12814 _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, "preview body", 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
12815 _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, 0);
12816 _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, 0);
12817 _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, 0);
12818 _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, 0);
12819 _bind_stmt_field_data_int(hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, 0);
12820 _bind_stmt_field_data_int(hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, 0);
12821 _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, 0);
12822 _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, 0);
12823 _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, 0);
12824 _bind_stmt_field_data_string(hStmt, DEFAULT_CHARSET_IDX_IN_MAIL_TBL, "UTF-8", 0, TEXT_2_LEN_IN_MAIL_TBL);
12825 _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, 0);
12826 _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, NULL, 0);
12828 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12829 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
12830 ("sqlite3_step fail:%d", rc));
12831 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
12832 ("sqlite3_step fail:%d", rc));
12836 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12837 if (hStmt != NULL) {
12838 rc = sqlite3_finalize(hStmt);
12839 if (rc != SQLITE_OK) {
12840 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
12841 error = EMAIL_ERROR_DB_FAILURE;
12845 if (err_code != NULL)
12848 EM_DEBUG_FUNC_END("ret [%d]", ret);
12853 INTERNAL_FUNC int emstorage_get_max_mail_count()
12855 return EMAIL_MAIL_MAX_COUNT;
12858 #define STRIPPED_SUBJECT_BUFFER_SIZE 4086
12860 INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(char *multi_user_name,
12861 emstorage_mail_tbl_t *mail_tbl,
12863 int *result_latest_mail_id_in_thread,
12864 int *thread_item_count)
12866 EM_DEBUG_FUNC_BEGIN("mail_tbl [%p], thread_id [%p], "
12867 "result_latest_mail_id_in_thread [%p], thread_item_count [%p]",
12868 mail_tbl, thread_id, result_latest_mail_id_in_thread, thread_item_count);
12869 EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
12871 int rc = 0, query_size = 0, query_size_account = 0;
12873 int search_thread = false;
12874 int account_id = 0;
12875 int err_code = EMAIL_ERROR_NONE;
12876 int count = 0, result_thread_id = -1, latest_mail_id_in_thread = -1;
12877 time_t latest_date_time = 0;
12878 char *subject = NULL;
12879 char *p_subject = NULL;
12880 char *sql_query_string = NULL, *sql_account = NULL;
12882 int temp_thread_id = -1;
12883 char *sql_format = "SELECT mail_id, thread_id, date_time, subject "
12884 "FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
12885 char *sql_format_account = " AND account_id = %d ";
12886 char *sql_format_order_by = " ORDER BY thread_id, date_time DESC ";
12887 char **result = NULL;
12888 char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
12889 char stripped_subject2[STRIPPED_SUBJECT_BUFFER_SIZE];
12891 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12893 EM_DEBUG_LOG("subject: [%p], mail_id: [%d]", subject, mail_tbl->mail_id);
12895 EM_IF_NULL_RETURN_VALUE(mail_tbl, EMAIL_ERROR_INVALID_PARAM);
12896 EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
12897 EM_IF_NULL_RETURN_VALUE(result_latest_mail_id_in_thread, EMAIL_ERROR_INVALID_PARAM);
12898 EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
12900 account_id = mail_tbl->account_id;
12901 subject = mail_tbl->subject;
12903 EM_DEBUG_LOG_SEC("subject: [%s]", subject);
12905 if (EM_SAFE_STRLEN(subject) == 0 && mail_tbl->mail_id != 0) {
12906 result_thread_id = mail_tbl->mail_id;
12911 if (em_find_pos_stripped_subject_for_thread_view(subject,
12913 STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE) {
12914 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
12915 err_code = EMAIL_ERROR_UNKNOWN;
12916 result_thread_id = -1;
12920 EM_DEBUG_LOG_SEC("stripped_subject: [%s]", stripped_subject);
12922 if (EM_SAFE_STRLEN(stripped_subject) == 0) {
12923 result_thread_id = -1;
12927 EM_DEBUG_LOG_SEC("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s",
12928 EM_SAFE_STRLEN(stripped_subject), stripped_subject);
12930 if (account_id > 0) {
12931 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
12932 sql_account = malloc(query_size_account);
12933 if (sql_account == NULL) {
12934 EM_DEBUG_EXCEPTION("malloc for sql_account is failed %d", query_size_account);
12935 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
12938 snprintf(sql_account, query_size_account, sql_format_account, account_id);
12941 /* prevent 34362 */
12942 query_size = strlen(sql_format) + strlen(stripped_subject)*2 + 50 + query_size_account + strlen(sql_format_order_by); /* + query_size_mailbox; */
12943 sql_query_string = malloc(query_size);
12945 if (sql_query_string == NULL) {
12946 EM_DEBUG_EXCEPTION("malloc for sql is failed %d", query_size);
12947 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
12951 sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mail_tbl->mailbox_id);
12953 if (account_id > 0)
12954 strcat(sql_query_string, sql_account);
12956 strcat(sql_query_string, sql_format_order_by);
12957 strcat(sql_query_string, ";");
12959 EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
12961 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL),
12964 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
12965 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12967 EM_DEBUG_LOG("Result rows count : %d", count);
12970 result_thread_id = -1;
12972 for (i = 0; i < count; i++) {
12973 EM_SAFE_FREE(p_subject);
12975 _get_table_field_data_int(result, &latest_mail_id_in_thread, col_index++);
12976 _get_table_field_data_int(result, &result_thread_id, col_index++);
12977 _get_table_field_data_time_t(result, &latest_date_time, col_index++);
12978 _get_table_field_data_string(result, &p_subject, 0, col_index++);
12980 if (temp_thread_id == result_thread_id)
12983 temp_thread_id = result_thread_id;
12985 if (em_find_pos_stripped_subject_for_thread_view(p_subject,
12987 STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE) {
12988 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
12989 err_code = EMAIL_ERROR_UNKNOWN;
12990 result_thread_id = -1;
12994 if (g_strcmp0(stripped_subject2, stripped_subject) == 0) {
12995 if (latest_date_time < mail_tbl->date_time)
12996 *result_latest_mail_id_in_thread = latest_mail_id_in_thread;
12998 *result_latest_mail_id_in_thread = mail_tbl->mail_id;
13000 search_thread = true;
13004 if (search_thread) {
13005 EM_DEBUG_LOG("latest_mail_id_in_thread [%d], mail_id [%d]",
13006 latest_mail_id_in_thread, mail_tbl->mail_id);
13008 result_thread_id = -1;
13017 *thread_id = result_thread_id;
13018 *thread_item_count = count;
13020 EM_DEBUG_LOG("Result thread id : %d", *thread_id);
13021 EM_DEBUG_LOG("Result count : %d", *thread_item_count);
13022 EM_DEBUG_LOG("err_code : %d", err_code);
13024 EM_SAFE_FREE(sql_account);
13025 EM_SAFE_FREE(sql_query_string);
13026 EM_SAFE_FREE(p_subject);
13028 sqlite3_free_table(result);
13030 EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
13035 INTERNAL_FUNC int emstorage_get_thread_id_from_mailbox(char *multi_user_name, int account_id, int mailbox_id, char *mail_subject, int *thread_id, int *thread_item_count)
13037 EM_DEBUG_FUNC_BEGIN("mailbox_id [%d], subject [%p], thread_id [%p], thread_item_count [%p]", mailbox_id, mail_subject, thread_id, thread_item_count);
13038 EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
13041 int query_size = 0;
13042 int query_size_account = 0;
13043 int err_code = EMAIL_ERROR_NONE;
13045 int result_thread_id = -1;
13046 char *sql_query_string = NULL;
13047 char *sql_account = NULL;
13048 char *sql_format = "SELECT thread_id FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
13049 char *sql_format_account = " AND account_id = %d ";
13050 char *sql_format_order_by = " ORDER BY date_time DESC ";
13051 char **result = NULL;
13052 char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
13053 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13055 EM_IF_NULL_RETURN_VALUE(mail_subject, EMAIL_ERROR_INVALID_PARAM);
13056 EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
13057 EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
13059 EM_DEBUG_LOG_SEC("subject: [%s]", mail_subject);
13061 if (em_find_pos_stripped_subject_for_thread_view(mail_subject, stripped_subject, STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE) {
13062 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
13063 err_code = EMAIL_ERROR_UNKNOWN;
13064 result_thread_id = -1;
13068 if (EM_SAFE_STRLEN(stripped_subject) == 0) {
13069 result_thread_id = -1;
13073 EM_DEBUG_LOG("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s", EM_SAFE_STRLEN(stripped_subject), stripped_subject);
13075 if (account_id > 0) {
13076 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
13077 sql_account = malloc(query_size_account);
13078 if (sql_account == NULL) {
13079 EM_DEBUG_EXCEPTION("malloc for sql_account is failed %d", query_size_account);
13080 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
13083 snprintf(sql_account, query_size_account, sql_format_account, account_id);
13086 query_size = EM_SAFE_STRLEN(sql_format) + EM_SAFE_STRLEN(stripped_subject)*2 + 50 + query_size_account + EM_SAFE_STRLEN(sql_format_order_by); /* + query_size_mailbox; */
13087 sql_query_string = malloc(query_size);
13089 if (sql_query_string == NULL) {
13090 EM_DEBUG_EXCEPTION("malloc for sql is failed %d", query_size);
13091 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
13095 sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mailbox_id);
13097 if (account_id > 0)
13098 strcat(sql_query_string, sql_account);
13100 strcat(sql_query_string, sql_format_order_by);
13101 strcat(sql_query_string, ";");
13103 EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
13105 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
13107 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13108 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13110 EM_DEBUG_LOG("Result rows count : %d", count);
13113 result_thread_id = -1;
13115 _get_table_field_data_int(result, &result_thread_id, 1);
13119 *thread_id = result_thread_id;
13120 *thread_item_count = count;
13122 EM_DEBUG_LOG("Result thread id : %d", *thread_id);
13123 EM_DEBUG_LOG("Result count : %d", *thread_item_count);
13124 EM_DEBUG_LOG("err_code : %d", err_code);
13126 EM_SAFE_FREE(sql_account);
13127 EM_SAFE_FREE(sql_query_string);
13129 sqlite3_free_table(result);
13131 EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
13136 INTERNAL_FUNC int emstorage_get_thread_information(char *multi_user_name, int thread_id, emstorage_mail_tbl_t** mail_tbl, int transaction, int *err_code)
13138 EM_DEBUG_FUNC_BEGIN();
13140 int count = 0, ret = false;
13141 int error = EMAIL_ERROR_NONE;
13142 emstorage_mail_tbl_t *p_data_tbl = NULL;
13143 char conditional_clause[QUERY_SIZE] = {0, };
13145 EM_IF_NULL_RETURN_VALUE(mail_tbl, false);
13147 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE thread_id = %d AND thread_item_count > 0", thread_id);
13148 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
13150 if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
13151 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
13156 EM_DEBUG_LOG("thread_id : %d, thread_item_count : %d", p_data_tbl[0].thread_id, p_data_tbl[0].thread_item_count);
13162 *mail_tbl = p_data_tbl;
13163 else if (p_data_tbl != NULL)
13164 emstorage_free_mail(&p_data_tbl, 1, NULL);
13166 if (err_code != NULL)
13169 EM_DEBUG_FUNC_END("ret [%d]", ret);
13173 INTERNAL_FUNC int emstorage_get_sender_list(char *multi_user_name, int account_id, int mailbox_id, int search_type, const char *search_value, email_sort_type_t sorting, email_sender_list_t** sender_list, int *sender_count, int *err_code)
13175 EM_DEBUG_FUNC_BEGIN_SEC("account_id [%d], mailbox_id [%d], search_type [%d], search_value [%p], sorting [%d], sender_list[%p], sender_count[%p] err_code[%p]"
13176 , account_id , mailbox_id , search_type , search_value , sorting , sender_list, sender_count, err_code);
13178 if ((!sender_list) || (!sender_count)) {
13179 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
13180 if (err_code != NULL)
13181 *err_code = EMAIL_ERROR_INVALID_PARAM;
13185 int rc = -1, ret = false;
13186 int error = EMAIL_ERROR_NONE;
13188 int i, col_index = 0;
13189 int read_count = 0;
13190 email_sender_list_t *p_sender_list = NULL;
13191 char sql_query_string[QUERY_SIZE] = {0, };
13192 char **result = NULL;
13193 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13195 SNPRINTF(sql_query_string, sizeof(sql_query_string),
13196 "SELECT email_address_sender, alias_sender, COUNT(email_address_sender), SUM(flags_seen_field = 1) "
13201 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE mailbox_id = %d ", mailbox_id);
13202 else /* NULL means all mailbox_name. but except for trash(3), spambox(5), all emails(for GMail, 7) */
13203 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE mailbox_type not in (3, 5, 7, 8) ");
13206 /* '0' (ALL_ACCOUNT) means all account */
13207 if (account_id > ALL_ACCOUNT)
13208 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1), " AND account_id = %d ", account_id);
13210 if (search_value) {
13211 switch (search_type) {
13212 case EMAIL_SEARCH_FILTER_SUBJECT:
13213 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13214 " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\')) ", search_value);
13216 case EMAIL_SEARCH_FILTER_SENDER:
13217 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13218 " AND ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13219 ") ", search_value);
13221 case EMAIL_SEARCH_FILTER_RECIPIENT:
13222 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13223 " AND ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13224 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13225 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13226 ") ", search_value, search_value, search_value);
13228 case EMAIL_SEARCH_FILTER_ALL:
13229 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13230 " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') "
13231 " OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13232 " OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13233 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13234 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13237 ")", search_value, search_value, search_value, search_value, search_value);
13243 /* sorting option is not available now. The order of sender list is ascending order by display name */
13244 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
13245 "GROUP BY email_address_sender "
13246 "ORDER BY UPPER(alias_sender) ");
13248 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
13250 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
13251 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13252 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13254 EM_DEBUG_LOG("Count of Sender [%d]", count);
13256 if (!(p_sender_list = (email_sender_list_t*)em_malloc(sizeof(email_sender_list_t) * count))) {
13257 EM_DEBUG_EXCEPTION("em_mallocfailed...");
13258 error = EMAIL_ERROR_OUT_OF_MEMORY;
13264 EM_DEBUG_LOG(">>>> DATA ASSIGN START >>");
13265 for (i = 0; i < count; i++) {
13266 _get_table_field_data_string(result, &(p_sender_list[i].address), 1, col_index++);
13267 _get_table_field_data_string(result, &(p_sender_list[i].display_name), 1, col_index++);
13268 _get_table_field_data_int(result, &(p_sender_list[i].total_count), col_index++);
13269 _get_table_field_data_int(result, &(read_count), col_index++);
13270 p_sender_list[i].unread_count = p_sender_list[i].total_count - read_count; /* unread count = total - read */
13272 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >>", count);
13274 sqlite3_free_table(result);
13281 *sender_list = p_sender_list;
13282 *sender_count = count;
13283 EM_DEBUG_LOG(">>>> COUNT : %d >>", count);
13287 if (err_code != NULL)
13290 EM_DEBUG_FUNC_END("ret [%d]", ret);
13294 INTERNAL_FUNC int emstorage_free_sender_list(email_sender_list_t **sender_list, int count)
13296 EM_DEBUG_FUNC_BEGIN("sender_list[%p], count[%d]", sender_list, count);
13298 int err = EMAIL_ERROR_NONE;
13301 if (!sender_list || !*sender_list) {
13302 EM_DEBUG_EXCEPTION("sender_list[%p], count[%d]", sender_list, count);
13303 err = EMAIL_ERROR_INVALID_PARAM;
13307 email_sender_list_t* p = *sender_list;
13310 for (; i < count; i++) {
13311 EM_SAFE_FREE(p[i].address);
13312 EM_SAFE_FREE(p[i].display_name);
13316 *sender_list = NULL;
13323 INTERNAL_FUNC int emstorage_free_address_info_list(email_address_info_list_t **address_info_list)
13325 EM_DEBUG_FUNC_BEGIN("address_info_list[%p]", address_info_list);
13327 int err = EMAIL_ERROR_NONE;
13328 email_address_info_t *p_address_info = NULL;
13329 GList *list = NULL;
13330 GList *node = NULL;
13333 if (!address_info_list || !*address_info_list) {
13334 EM_DEBUG_EXCEPTION("address_info_list[%p]", address_info_list);
13335 err = EMAIL_ERROR_INVALID_PARAM;
13339 /* delete GLists */
13340 for (i = EMAIL_ADDRESS_TYPE_FROM; i <= EMAIL_ADDRESS_TYPE_BCC; i++) {
13342 case EMAIL_ADDRESS_TYPE_FROM:
13343 list = (*address_info_list)->from;
13345 case EMAIL_ADDRESS_TYPE_TO:
13346 list = (*address_info_list)->to;
13348 case EMAIL_ADDRESS_TYPE_CC:
13349 list = (*address_info_list)->cc;
13351 case EMAIL_ADDRESS_TYPE_BCC:
13352 list = (*address_info_list)->bcc;
13356 /* delete dynamic-allocated memory for each item */
13357 node = g_list_first(list);
13358 while (node != NULL) {
13359 p_address_info = (email_address_info_t*)node->data;
13360 EM_SAFE_FREE(p_address_info->address);
13361 EM_SAFE_FREE(p_address_info->display_name);
13362 EM_SAFE_FREE(node->data);
13364 node = g_list_next(node);
13369 EM_SAFE_FREE(*address_info_list);
13370 *address_info_list = NULL;
13372 EM_DEBUG_FUNC_END("err [%d]", err);
13375 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
13377 INTERNAL_FUNC int emstorage_add_pbd_activity(char *multi_user_name, email_event_partial_body_thd* local_activity, int *activity_id, int transaction, int *err_code)
13379 EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
13381 if (!local_activity || !activity_id) {
13382 EM_DEBUG_EXCEPTION("local_activity[%p], transaction[%d], activity_id[%p], err_code[%p]", local_activity, activity_id, transaction, err_code);
13383 if (err_code != NULL)
13384 *err_code = EMAIL_ERROR_INVALID_PARAM;
13390 int error = EMAIL_ERROR_NONE;
13393 char sql_query_string[QUERY_SIZE] = {0, };
13394 DB_STMT hStmt = NULL;
13395 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13397 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13398 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13399 SNPRINTF(sql_query_string, sizeof(sql_query_string),
13400 "INSERT INTO mail_partial_body_activity_tbl VALUES "
13402 "? " /* Account ID */
13403 ",?" /* Local Mail ID */
13404 ",?" /* Server mail ID */
13405 ",?" /* Activity ID */
13406 ",?" /* Activity type*/
13407 ",?" /* Mailbox ID*/
13408 ",?" /* Mailbox name*/
13409 ",?" /* Multi User Name */
13412 char *sql = "SELECT max(rowid) FROM mail_partial_body_activity_tbl;";
13413 char **result = NULL;
13416 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13418 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13419 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13421 if (NULL == result[1]) rc = 1;
13422 else rc = atoi(result[1])+1;
13423 sqlite3_free_table(result);
13426 *activity_id = local_activity->activity_id = rc;
13428 EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [ %d ], MAIL ID [ %d ], ACTIVITY TYPE [ %d ], SERVER MAIL ID [ %lu ]", \
13429 local_activity->activity_id, local_activity->mail_id, local_activity->activity_type, local_activity->server_mail_id);
13431 if (local_activity->mailbox_id)
13432 EM_DEBUG_LOG(" MAILBOX ID [ %d ]", local_activity->mailbox_id);
13435 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13436 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13437 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13440 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
13441 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
13442 _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
13443 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
13444 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
13445 _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
13446 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->mailbox_name, 0, 3999);
13447 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
13450 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13452 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
13453 ("sqlite3_step fail:%d", rc));
13454 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13455 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
13460 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13461 if (hStmt != NULL) {
13462 rc = sqlite3_finalize(hStmt);
13464 if (rc != SQLITE_OK) {
13465 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13466 error = EMAIL_ERROR_DB_FAILURE;
13470 if (err_code != NULL)
13473 EM_DEBUG_FUNC_END("ret [%d]", ret);
13477 INTERNAL_FUNC int emstorage_get_pbd_mailbox_list(char *multi_user_name, int account_id, int **mailbox_list, int *count, int transaction, int *err_code)
13479 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13481 if (account_id < FIRST_ACCOUNT_ID || NULL == mailbox_list || *mailbox_list == NULL || NULL == count) {
13482 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13483 if (err_code != NULL)
13484 *err_code = EMAIL_ERROR_INVALID_PARAM;
13489 int error = EMAIL_ERROR_NONE;
13491 int i = 0, rc = -1;
13492 int *mbox_list = NULL;
13493 DB_STMT hStmt = NULL;
13494 char sql_query_string[QUERY_SIZE] = {0, };
13496 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13497 EMSTORAGE_START_READ_TRANSACTION(transaction);
13499 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13500 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(distinct mailbox_id) FROM mail_partial_body_activity_tbl WHERE account_id = %d order by mailbox_id", account_id);
13503 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13504 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13505 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13507 *count = atoi(result[1]);
13508 sqlite3_free_table(result);
13511 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
13512 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13516 EM_DEBUG_LOG("Mailbox count = %d", *count);
13518 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13520 /* SNPRINTF(g_sql_query, sizeof(g_sql_query), "SELECT distinct mailbox_name FROM mail_partial_body_activity_tbl WHERE account_id = %d order by activity_id", account_id); */
13521 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct mailbox_id FROM mail_partial_body_activity_tbl WHERE account_id = %d order by mailbox_id", account_id);
13523 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13526 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13529 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13530 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13533 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13534 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13535 ("sqlite3_step fail:%d", rc));
13537 mbox_list = (int *)em_malloc(sizeof(int) * (*count));
13538 if (NULL == mbox_list) {
13539 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
13540 error = EMAIL_ERROR_OUT_OF_MEMORY;
13544 memset(mbox_list, 0x00, sizeof(int) * (*count));
13546 for (i = 0; i < (*count); i++) {
13547 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
13548 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13549 /* EM_DEBUG_LOG("In emstorage_get_pdb_mailbox_list() loop, After sqlite3_step(), , i = %d, rc = %d.", i, rc); */
13550 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13551 ("sqlite3_step fail:%d", rc));
13552 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
13559 *mailbox_list = mbox_list;
13561 EM_SAFE_FREE(mbox_list);
13563 if (hStmt != NULL) {
13564 rc = sqlite3_finalize(hStmt);
13566 if (rc != SQLITE_OK) {
13567 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13568 error = EMAIL_ERROR_DB_FAILURE;
13572 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13573 if (err_code != NULL)
13575 EM_DEBUG_FUNC_END("ret [%d]", ret);
13579 INTERNAL_FUNC int emstorage_get_pbd_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
13581 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13583 if (NULL == account_list || NULL == count) {
13584 EM_DEBUG_EXCEPTION("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13585 if (err_code != NULL)
13586 *err_code = EMAIL_ERROR_INVALID_PARAM;
13591 int error = EMAIL_ERROR_NONE;
13592 char *sql = "SELECT count(distinct account_id) FROM mail_partial_body_activity_tbl";
13594 int i = 0, rc = -1;
13595 int *result_account_list = NULL;
13596 DB_STMT hStmt = NULL;
13597 char sql_query_string[QUERY_SIZE] = {0, };
13599 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13601 EMSTORAGE_START_READ_TRANSACTION(transaction);
13603 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13604 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13605 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13607 *count = atoi(result[1]);
13608 sqlite3_free_table(result);
13611 EM_DEBUG_EXCEPTION("no account found...");
13612 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13617 EM_DEBUG_LOG("Account count [%d]", *count);
13619 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13621 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_partial_body_activity_tbl");
13623 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13626 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13628 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
13629 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13630 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13633 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13634 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13635 ("sqlite3_step fail:%d", rc));
13637 if (NULL == (result_account_list = (int *)em_malloc(sizeof(int) * (*count)))) {
13638 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
13639 error = EMAIL_ERROR_OUT_OF_MEMORY;
13643 memset(result_account_list, 0x00, sizeof(int) * (*count));
13645 for (i = 0; i < (*count); i++) {
13646 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
13648 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13649 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13650 ("sqlite3_step fail:%d", rc));
13651 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
13658 *account_list = result_account_list;
13660 EM_SAFE_FREE(result_account_list);
13662 if (hStmt != NULL) {
13663 rc = sqlite3_finalize(hStmt);
13665 if (rc != SQLITE_OK) {
13666 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13667 error = EMAIL_ERROR_DB_FAILURE;
13671 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13672 if (err_code != NULL)
13674 EM_DEBUG_FUNC_END("ret [%d]", ret);
13678 INTERNAL_FUNC int emstorage_get_pbd_activity_data(char *multi_user_name, int account_id, int input_mailbox_id, email_event_partial_body_thd** event_start, int *count, int transaction, int *err_code)
13680 EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
13682 if (account_id < FIRST_ACCOUNT_ID || NULL == event_start || 0 == input_mailbox_id || NULL == count) {
13683 EM_DEBUG_EXCEPTION("account_id[%d], email_event_partial_body_thd[%p], input_mailbox_id[%d], count[%p], err_code[%p]", account_id, event_start, input_mailbox_id, count, err_code);
13685 if (err_code != NULL)
13686 *err_code = EMAIL_ERROR_INVALID_PARAM;
13693 int error = EMAIL_ERROR_NONE;
13695 DB_STMT hStmt = NULL;
13696 email_event_partial_body_thd* event_list = NULL;
13697 char sql_query_string[QUERY_SIZE] = {0, };
13699 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13701 EMSTORAGE_START_READ_TRANSACTION(transaction);
13703 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13704 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl WHERE account_id = %d AND mailbox_id = '%d' order by activity_id", account_id, input_mailbox_id);
13707 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13708 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13709 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13711 *count = atoi(result[1]);
13712 sqlite3_free_table(result);
13714 EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
13717 EM_DEBUG_LOG("No matched activity found in mail_partial_body_activity_tbl");
13718 error = EMAIL_ERROR_MAIL_NOT_FOUND;
13722 EM_DEBUG_LOG("Activity Count = %d", *count);
13724 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13725 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_partial_body_activity_tbl WHERE account_id = %d AND mailbox_id = '%d' order by activity_id", account_id, input_mailbox_id);
13727 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13730 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13732 EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
13733 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13734 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13737 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13738 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13739 ("sqlite3_step fail:%d", rc));
13741 if (!(event_list = (email_event_partial_body_thd*)em_malloc(sizeof(email_event_partial_body_thd) * (*count)))) {
13742 EM_DEBUG_EXCEPTION("Malloc failed");
13744 error = EMAIL_ERROR_OUT_OF_MEMORY;
13747 memset(event_list, 0x00, sizeof(email_event_partial_body_thd) * (*count));
13749 for (i = 0; i < (*count); i++) {
13750 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_IDX_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13751 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13752 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13753 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13754 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_type), ACTIVITY_TYPE_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13755 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13756 _get_stmt_field_data_string(hStmt, &(event_list[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13757 _get_stmt_field_data_string(hStmt, &(event_list[i].multi_user_name), 0, MULTI_USER_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13759 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13760 /* EM_DEBUG_LOG("In emstorage_get_pbd_activity_data() loop, After sqlite3_step(), , i = %d, rc = %d.", i, rc); */
13761 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13762 ("sqlite3_step fail:%d", rc));
13764 event_list[i].event_type = 0;
13771 *event_start = event_list;
13773 for (i = 0; i < (*count); i++)
13774 emcore_free_partial_body_thd_event(event_list, NULL);
13775 EM_SAFE_FREE(event_list); /*prevent 54559*/
13776 *event_start = NULL;
13780 if (hStmt != NULL) {
13781 rc = sqlite3_finalize(hStmt);
13783 if (rc != SQLITE_OK) {
13784 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13785 error = EMAIL_ERROR_DB_FAILURE;
13789 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13790 if (err_code != NULL)
13793 EM_DEBUG_FUNC_END("ret [%d]", ret);
13797 INTERNAL_FUNC int emstorage_delete_pbd_activity(char *multi_user_name, int account_id, int mail_id, int activity_id, int transaction, int *err_code)
13799 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d] , activity_id[%d], transaction[%d], err_code[%p]", account_id, mail_id, activity_id, transaction, err_code);
13802 if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
13803 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], activity_id[%d], transaction[%d], err_code[%p]", account_id, mail_id, activity_id, transaction, err_code);
13805 if (err_code != NULL)
13806 *err_code = EMAIL_ERROR_INVALID_PARAM;
13812 int error = EMAIL_ERROR_NONE;
13813 char sql_query_string[QUERY_SIZE] = {0, };
13814 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13816 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13817 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13819 if (activity_id == 0)
13820 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d AND mail_id = %d", account_id, mail_id);
13822 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d AND activity_id = %d", account_id, activity_id);
13824 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13825 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
13826 if (error != EMAIL_ERROR_NONE) {
13827 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
13831 /* validate activity existence */
13832 rc = sqlite3_changes(local_db_handle);
13834 EM_DEBUG_EXCEPTION("No matching activity found");
13835 error = EMAIL_ERROR_DATA_NOT_FOUND;
13843 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13845 if (err_code != NULL)
13848 EM_DEBUG_FUNC_END("ret [%d]", ret);
13852 INTERNAL_FUNC int emstorage_get_mailbox_pbd_activity_count(char *multi_user_name, int account_id, int input_mailbox_id, int *activity_count, int transaction, int *err_code)
13854 EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
13856 if (account_id < FIRST_ACCOUNT_ID || NULL == activity_count || NULL == err_code) {
13857 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
13858 if (err_code != NULL)
13859 *err_code = EMAIL_ERROR_INVALID_PARAM;
13864 int error = EMAIL_ERROR_NONE;
13865 char sql_query_string[QUERY_SIZE] = {0, };
13867 DB_STMT hStmt = NULL;
13869 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13871 EMSTORAGE_START_READ_TRANSACTION(transaction);
13872 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13874 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl WHERE account_id = %d and mailbox_id = '%d'", account_id, input_mailbox_id);
13876 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13879 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13880 EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
13881 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13882 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13885 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13886 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13887 ("sqlite3_step fail:%d", rc));
13889 _get_stmt_field_data_int(hStmt, activity_count, 0);
13891 EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
13897 if (hStmt != NULL) {
13898 rc = sqlite3_finalize(hStmt);
13900 if (rc != SQLITE_OK) {
13901 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13902 error = EMAIL_ERROR_DB_FAILURE;
13906 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13908 if (err_code != NULL)
13911 EM_DEBUG_FUNC_END("ret [%d]", ret);
13915 INTERNAL_FUNC int emstorage_get_pbd_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
13917 EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
13919 if (NULL == activity_count || NULL == err_code) {
13920 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
13921 if (err_code != NULL)
13922 *err_code = EMAIL_ERROR_INVALID_PARAM;
13927 int error = EMAIL_ERROR_NONE;
13928 DB_STMT hStmt = NULL;
13929 char sql_query_string[QUERY_SIZE] = {0, };
13931 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13933 EMSTORAGE_START_READ_TRANSACTION(transaction);
13934 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13936 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl;");
13938 EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
13940 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13941 EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
13942 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13943 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13945 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13946 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13947 ("sqlite3_step fail:%d", rc));
13949 _get_stmt_field_data_int(hStmt, activity_count, 0);
13951 EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
13958 if (hStmt != NULL) {
13959 rc = sqlite3_finalize(hStmt);
13961 if (rc != SQLITE_OK) {
13962 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13963 error = EMAIL_ERROR_DB_FAILURE;
13967 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13968 if (err_code != NULL)
13971 EM_DEBUG_FUNC_END("ret [%d]", ret);
13975 INTERNAL_FUNC int emstorage_delete_full_pbd_activity_data(char *multi_user_name, int account_id, int transaction, int *err_code)
13977 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
13978 if (account_id < FIRST_ACCOUNT_ID) {
13979 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
13980 if (err_code != NULL)
13981 *err_code = EMAIL_ERROR_INVALID_PARAM;
13987 int error = EMAIL_ERROR_NONE;
13988 char sql_query_string[QUERY_SIZE] = {0, };
13990 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13992 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13993 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13994 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d", account_id);
13996 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13997 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
13998 if (error != EMAIL_ERROR_NONE) {
13999 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14003 rc = sqlite3_changes(local_db_handle);
14005 EM_DEBUG_EXCEPTION("No matching activities found in mail_partial_body_activity_tbl");
14006 error = EMAIL_ERROR_DATA_NOT_FOUND;
14015 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14017 if (err_code != NULL)
14020 EM_DEBUG_FUNC_END("ret [%d]", ret);
14024 /*Himanshu[h.gahlaut]-> Added below API to update mail_partial_body_activity_tbl
14025 if a mail is moved before its partial body is downloaded.Currently not used but should be used if mail move from server is enabled*/
14027 INTERNAL_FUNC int emstorage_update_pbd_activity(char *multi_user_name, char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code)
14029 EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
14031 int rc = -1, ret = false;
14032 int error = EMAIL_ERROR_NONE;
14033 char sql_query_string[QUERY_SIZE] = {0, };
14035 int transaction = true;
14037 if (!old_server_uid || !new_server_uid || !mbox_name) {
14038 EM_DEBUG_EXCEPTION("Invalid parameters");
14039 error = EMAIL_ERROR_INVALID_PARAM;
14043 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14045 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14046 memset(sql_query_string, 0x00, sizeof(sql_query_string));
14047 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14048 "UPDATE mail_partial_body_activity_tbl SET server_mail_id = %s , mailbox_name=\'%s\' WHERE server_mail_id = %s ", new_server_uid, mbox_name, old_server_uid);
14050 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14051 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14052 if (error != EMAIL_ERROR_NONE) {
14053 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14057 rc = sqlite3_changes(local_db_handle);
14059 EM_DEBUG_LOG("No matching found in mail_partial_body_activity_tbl");
14065 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14067 if (err_code != NULL)
14070 EM_DEBUG_FUNC_END("ret [%d]", ret);
14075 INTERNAL_FUNC int emstorage_create_file(char *data_string, size_t file_size, char *dst_file_name, int *err_code)
14077 EM_DEBUG_FUNC_BEGIN_SEC("file_size[%d] , dst_file_name[%s], err_code[%p]", file_size, dst_file_name, err_code);
14080 int error = EMAIL_ERROR_NONE;
14081 FILE* fp_dst = NULL;
14082 char errno_buf[ERRNO_BUF_SIZE] = {0};
14084 if (!data_string || !dst_file_name) {
14085 EM_DEBUG_LOG("data_string[%p], dst_file_name[%p]", data_string, dst_file_name);
14086 error = EMAIL_ERROR_INVALID_PARAM;
14090 error = em_fopen(dst_file_name, "w", &fp_dst);
14091 if (error != EMAIL_ERROR_NONE) {
14092 EM_DEBUG_EXCEPTION_SEC("em_fopen failed - %s: %d", dst_file_name, error);
14096 if (fwrite(data_string, 1, file_size, fp_dst) == 0) {
14097 EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
14098 error = EMAIL_ERROR_UNKNOWN;
14106 if (fp_dst != NULL)
14109 if (err_code != NULL)
14112 EM_DEBUG_FUNC_END("ret [%d]", ret);
14116 #endif /* __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
14120 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
14121 INTERNAL_FUNC int emstorage_update_read_mail_uid_by_server_uid(char *multi_user_name, char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code)
14123 EM_DEBUG_FUNC_BEGIN();
14126 int error = EMAIL_ERROR_NONE;
14127 char sql_query_string[QUERY_SIZE] = {0, };
14129 int transaction = true;
14131 if (!old_server_uid || !new_server_uid || !mbox_name) {
14132 EM_DEBUG_EXCEPTION("Invalid parameters");
14133 if (err_code != NULL)
14134 *err_code = EMAIL_ERROR_INVALID_PARAM;
14138 EM_DEBUG_LOG_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
14140 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14141 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14144 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14145 "UPDATE mail_read_mail_uid_tbl SET server_uid=\'%s\' , mailbox_name=\'%s\' WHERE server_uid=%s ", new_server_uid, mbox_name, old_server_uid);
14147 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
14148 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14149 if (error != EMAIL_ERROR_NONE) {
14150 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14154 rc = sqlite3_changes(local_db_handle);
14156 EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
14157 error = EMAIL_ERROR_DATA_NOT_FOUND;
14164 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14166 if (err_code != NULL)
14169 EM_DEBUG_FUNC_END("ret [%d]", ret);
14176 * @fn emstorage_get_id_set_from_mail_ids(int mail_ids[], int mail_id_count, email_id_set_t** server_uids, int *id_set_count, int *err_code);
14177 * Prepare an array of mail_id and corresponding server mail id.
14179 *@author h.gahlaut@samsung.com
14180 * @param[in] mail_ids Specifies the comma separated string of mail_ids. Maximaum size of string should be less than or equal to (QUERY_SIZE - 88)
14181 * where 88 is the length of fixed keywords including ending null character in the QUERY to be formed
14182 * @param[out] idset Returns the array of mail_id and corresponding server_mail_id sorted by server_mail_ids
14183 * @param[out] id_set_count Returns the no. of cells in idset array i.e. no. of sets of mail_ids and server_mail_ids
14184 * @param[out] err_code Returns the error code.
14185 * @remarks An Example of Query to be exexuted in this API:
14186 * SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (12, 13, 56, 78);
14187 * @return This function returns true on success or false on failure.
14189 INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *multi_user_name,
14191 email_id_set_t** idset,
14195 EM_DEBUG_FUNC_BEGIN();
14196 EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds);
14198 int error = EMAIL_ERROR_NONE;
14200 email_id_set_t* p_id_set = NULL;
14202 const int buf_size = QUERY_SIZE;
14203 char sql_query_string[QUERY_SIZE] = {0, };
14204 int space_left_in_query_buffer = buf_size;
14207 char *server_mail_id = NULL;
14208 char **result = NULL;
14212 if (NULL == mail_ids || NULL == idset || NULL == id_set_count) {
14213 EM_DEBUG_EXCEPTION("Invalid Parameters mail_ids[%p] idset[%p] id_set_count [%p]",
14214 mail_ids, idset, id_set_count);
14215 if (err_code != NULL)
14216 *err_code = EMAIL_ERROR_INVALID_PARAM;
14220 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14222 SNPRINTF(sql_query_string, space_left_in_query_buffer,
14223 "SELECT local_uid, server_uid FROM mail_read_mail_uid_tbl WHERE local_uid in (%s) ORDER BY server_uid",
14226 EM_DEBUG_LOG_SEC("SQL Query formed [%s] ", sql_query_string);
14228 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14229 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14230 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result);
14231 goto FINISH_OFF; }, ("SQL(%s) sqlite3_get_table fail:%d -%s",
14232 sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14234 EM_DEBUG_LOG(" Count of mails [%d ]", count);
14237 EM_DEBUG_EXCEPTION("Can't find proper mail");
14238 error = EMAIL_ERROR_DATA_NOT_FOUND;
14242 if (NULL == (p_id_set = (email_id_set_t*)em_malloc(sizeof(email_id_set_t) * count))) {
14243 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
14244 error = EMAIL_ERROR_OUT_OF_MEMORY;
14250 EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds_Loop);
14251 EM_DEBUG_LOG(">>>> DATA ASSIGN START");
14252 for (i = 0; i < count; i++) {
14253 _get_table_field_data_int(result, &(p_id_set[i].mail_id), col_index++);
14254 _get_table_field_data_string(result, &server_mail_id, 1, col_index++);
14255 if (server_mail_id) {
14256 p_id_set[i].server_mail_id = strtoul(server_mail_id, NULL, 10);
14257 EM_SAFE_FREE(server_mail_id);
14260 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d]", count);
14261 EM_PROFILE_END(EmStorageGetIdSetFromMailIds_Loop);
14263 sqlite3_free_table(result);
14272 *id_set_count = count;
14273 EM_DEBUG_LOG(" idset[%p] id_set_count [%d]", *idset, *id_set_count);
14275 EM_SAFE_FREE(p_id_set);
14278 if (err_code != NULL)
14281 EM_PROFILE_END(EmStorageGetIdSetFromMailIds);
14283 EM_DEBUG_FUNC_END("ret [%d]", ret);
14291 INTERNAL_FUNC int emstorage_delete_triggers_from_lucene(char *multi_user_name)
14293 EM_DEBUG_FUNC_BEGIN();
14294 int ret = true, transaction = true;
14295 int error = EMAIL_ERROR_NONE;
14296 char sql_query_string[QUERY_SIZE] = {0, };
14297 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14298 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14300 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerDelete;");
14301 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14302 if (error != EMAIL_ERROR_NONE) {
14303 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14307 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerInsert;");
14308 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14309 if (error != EMAIL_ERROR_NONE) {
14310 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14314 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerUpdate;");
14315 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14316 if (error != EMAIL_ERROR_NONE) {
14317 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14324 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14326 EM_DEBUG_FUNC_END("ret [%d]", ret);
14330 INTERNAL_FUNC int emstorage_update_tag_id(char *multi_user_name, int old_filter_id, int new_filter_id, int *err_code)
14332 EM_DEBUG_FUNC_BEGIN("new_filter_id[%d], old_filter_id[%d]", new_filter_id, old_filter_id);
14334 int error = EMAIL_ERROR_NONE;
14335 char sql_query_string[QUERY_SIZE] = {0, };
14336 int transaction = true;
14338 if (old_filter_id < 0 || new_filter_id < 0) {
14339 EM_DEBUG_EXCEPTION("Invalid parameters");
14340 if (err_code != NULL)
14341 *err_code = EMAIL_ERROR_INVALID_PARAM;
14345 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14347 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14349 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14350 "UPDATE mail_tbl SET tag_id=%d WHERE tag_id=%d ", new_filter_id, old_filter_id);
14352 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14353 if (error != EMAIL_ERROR_NONE) {
14354 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14361 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14363 if (err_code != NULL)
14366 EM_DEBUG_FUNC_END("ret [%d]", ret);
14370 INTERNAL_FUNC int emstorage_filter_mails_by_rule(char *multi_user_name, int account_id, int dest_mailbox_id, int dest_mailbox_type, int reset, emstorage_rule_tbl_t *rule, int ** filtered_mail_id_list, int *count_of_mails, int *err_code)
14372 EM_DEBUG_FUNC_BEGIN("account_id[%d], dest_mailbox_id[%d] rule[%p], filtered_mail_id_list[%p], count_of_mails[%p], err_code[%p]", account_id, dest_mailbox_id, rule, filtered_mail_id_list, count_of_mails, err_code);
14374 if ((account_id < 0) || (dest_mailbox_id < 0) || (!rule)) {
14375 EM_DEBUG_EXCEPTION("Invalid Parameter");
14377 if (err_code != NULL)
14378 *err_code = EMAIL_ERROR_INVALID_PARAM;
14382 int rc = -1, ret = false, error = EMAIL_ERROR_NONE;
14383 int count = 0, col_index = 0, i = 0, where_pararaph_length = 0, *mail_list = NULL;
14384 int tag_id = rule->rule_id;
14385 char **result = NULL, *where_pararaph = NULL;
14386 char sql_query_string[QUERY_SIZE] = {0, };
14387 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14389 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl ");
14391 where_pararaph_length = EM_SAFE_STRLEN(rule->value) + 2 * (EM_SAFE_STRLEN(rule->value2)) + 100;
14392 where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
14393 if (where_pararaph == NULL) {
14394 EM_DEBUG_EXCEPTION("malloc failed for where_pararaph.");
14395 error = EMAIL_ERROR_OUT_OF_MEMORY;
14399 if (account_id != ALL_ACCOUNT)
14400 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (0)", account_id);
14402 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_type NOT in (0)");
14404 if (rule->type & EMAIL_FILTER_SUBJECT) {
14405 if (rule->flag2 == RULE_TYPE_INCLUDES)
14406 sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject like \'%%%q%%\'", rule->value);
14407 else /* RULE_TYPE_EXACTLY */
14408 sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject = \'%q\'", rule->value);
14411 if (rule->type & EMAIL_FILTER_FROM) {
14412 if (rule->flag2 == RULE_TYPE_INCLUDES)
14413 sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND full_address_from like \'%%%q%%\'", rule->value2);
14414 #ifdef __FEATURE_COMPARE_DOMAIN__
14415 else if (rule->flag2 == RULE_TYPE_COMPARE_DOMAIN)
14416 sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND (full_address_from like \'@%%%q\' OR full_address_from like \'@%%%q>%%\')", rule->value2, rule->value2);
14417 #endif /*__FEATURE_COMPARE_DOMAIN__ */
14418 else /* RULE_TYPE_EXACTLY */
14419 sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND full_address_from = \'%q\'", rule->value2);
14422 if (rule->type == EMAIL_PRIORITY_SENDER) {
14423 if (rule->flag2 == RULE_TYPE_INCLUDES)
14424 sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND full_address_from like \'%%%q%%\'", rule->value2);
14425 else /* RULE_TYPE_EXACTLY */
14426 sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND full_address_from = \'%q\' OR email_address_sender = \'%q\'", rule->value2, rule->value2);
14428 tag_id = PRIORITY_SENDER_TAG_ID;
14431 /* prevent 34361 */
14432 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14433 strcat(sql_query_string, where_pararaph);
14435 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14437 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14438 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14439 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
14440 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14442 EM_DEBUG_LOG("Count of mails [%d]", count);
14445 mail_list = malloc(sizeof(int) * count);
14446 if (mail_list == NULL) {
14447 EM_DEBUG_EXCEPTION("malloc failed for mail_list.");
14448 error = EMAIL_ERROR_OUT_OF_MEMORY;
14454 for (i = 0; i < count; i++)
14455 _get_table_field_data_int(result, &(mail_list[i]), col_index++);
14457 memset(sql_query_string, 0x00, QUERY_SIZE);
14459 switch (rule->action_type) {
14460 case EMAIL_FILTER_MOVE:
14461 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = 0 ");
14463 case EMAIL_FILTER_BLOCK:
14465 EM_DEBUG_LOG("Not support : action_type[%d]", rule->action_type);
14470 switch (rule->action_type) {
14471 case EMAIL_FILTER_MOVE:
14472 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = %d ", tag_id);
14474 case EMAIL_FILTER_BLOCK:
14475 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET mailbox_id = %d, mailbox_type = %d ", dest_mailbox_id, dest_mailbox_type);
14478 EM_DEBUG_LOG("Not support");
14483 /* prevent 34361 */
14484 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14485 strcat(sql_query_string, where_pararaph);
14487 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14488 if (error != EMAIL_ERROR_NONE) {
14489 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14493 #ifdef __FEATURE_BODY_SEARCH__
14494 /* Updating mail_text_tbl */
14495 if (rule->action_type == EMAIL_FILTER_BLOCK) {
14496 memset(sql_query_string, 0x00, QUERY_SIZE);
14497 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_text_tbl SET mailbox_id = %d ", dest_mailbox_id);
14498 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14499 strcat(sql_query_string, where_pararaph);
14501 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14502 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14503 if (error != EMAIL_ERROR_NONE) {
14504 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14516 if (filtered_mail_id_list)
14517 *filtered_mail_id_list = mail_list;
14519 if (count_of_mails)
14520 *count_of_mails = count;
14522 EM_SAFE_FREE(mail_list);
14524 sqlite3_free_table(result);
14528 EM_SAFE_FREE(where_pararaph);
14530 if (err_code != NULL)
14533 EM_DEBUG_FUNC_END("ret [%d]", ret);
14537 #define EMAIL_SLOT_UNIT 25
14538 INTERNAL_FUNC int emstorage_set_mail_slot_size(char *multi_user_name, int account_id, int mailbox_id, int new_slot_size, int transaction, int *err_code)
14540 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%p] new_slot_size[%d], err_code[%p]", account_id, mailbox_id, new_slot_size, err_code);
14541 int ret = false, err = EMAIL_ERROR_NONE;
14542 int where_pararaph_length = 0;
14543 char *where_pararaph = NULL;
14544 char sql_query_string[QUERY_SIZE] = {0, };
14546 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14548 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
14550 if (new_slot_size > 0)
14551 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = %d ", new_slot_size);
14552 else if (new_slot_size == 0)
14553 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", EMAIL_SLOT_UNIT);
14555 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", new_slot_size * -1);
14559 where_pararaph_length = 80;
14561 where_pararaph_length = 50;
14563 if (new_slot_size == 0)
14564 where_pararaph_length += 70;
14566 where_pararaph = malloc(sizeof(char) * where_pararaph_length);
14567 if (where_pararaph == NULL) {
14568 EM_DEBUG_EXCEPTION("Memory allocation failed for where_pararaph");
14569 err = EMAIL_ERROR_OUT_OF_MEMORY;
14572 memset(where_pararaph, 0x00, where_pararaph_length);
14574 if (account_id > ALL_ACCOUNT) {
14577 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_id = %d ", mailbox_id);
14579 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d ", account_id);
14582 if (new_slot_size == 0)
14583 SNPRINTF(where_pararaph + EM_SAFE_STRLEN(where_pararaph), where_pararaph_length - EM_SAFE_STRLEN(where_pararaph), " %s total_mail_count_on_server > mail_slot_size ", (and ? "AND" : "WHERE"));
14585 if (strlen(sql_query_string) + EM_SAFE_STRLEN(where_pararaph) < QUERY_SIZE) /* prevent 34363 */
14586 strcat(sql_query_string, where_pararaph);
14588 EM_DEBUG_EXCEPTION("Query buffer overflowed !!!");
14589 err = EMAIL_ERROR_OUT_OF_MEMORY;
14593 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14594 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14595 if (err != EMAIL_ERROR_NONE) {
14596 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
14603 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
14605 EM_SAFE_FREE(where_pararaph);
14607 if (err_code != NULL)
14610 EM_DEBUG_FUNC_END("ret [%d]", ret);
14614 INTERNAL_FUNC int emstorage_add_meeting_request(char *multi_user_name, int account_id, int input_mailbox_id, email_meeting_request_t* meeting_req, int transaction, int *err_code)
14616 EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], meeting_req[%p], transaction[%d], err_code[%p]", account_id, input_mailbox_id, meeting_req, transaction, err_code);
14618 if (!meeting_req || meeting_req->mail_id <= 0) {
14620 EM_DEBUG_EXCEPTION("mail_id[%]d", meeting_req->mail_id);
14622 if (err_code != NULL)
14623 *err_code = EMAIL_ERROR_INVALID_PARAM;
14630 int error = EMAIL_ERROR_NONE;
14631 DB_STMT hStmt = NULL;
14632 char sql_query_string[QUERY_SIZE] = {0, };
14634 time_t temp_unix_time = 0;
14636 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14637 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14639 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14640 "INSERT INTO mail_meeting_tbl VALUES "
14642 ", ?" /* account_id */
14643 ", ?" /* mailbox_id */
14644 ", ?" /* meeting_response */
14645 ", ?" /* start_time */
14646 ", ?" /* end_time */
14647 ", ?" /* location */
14648 ", ?" /* global_object_id */
14650 ", ?" /* standard_name */
14651 ", ?" /* standard_time_start_date */
14652 ", ?" /* standard_biad */
14653 ", ?" /* daylight_name */
14654 ", ?" /* daylight_time_start_date */
14655 ", ?" /* daylight_bias */
14658 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14659 if (rc != SQLITE_OK) {
14660 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
14661 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
14663 error = EMAIL_ERROR_DB_FAILURE;
14669 EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
14670 EM_DEBUG_LOG_SEC(">>>>> account_id[%d]", account_id);
14671 EM_DEBUG_LOG_SEC(">>>>> mailbox_name[%s]", mailbox_name);
14672 EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
14673 EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
14674 EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
14675 EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
14676 EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
14677 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
14678 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
14679 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
14680 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
14681 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
14682 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
14683 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
14685 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->mail_id);
14686 _bind_stmt_field_data_int(hStmt, col_index++, account_id);
14687 _bind_stmt_field_data_int(hStmt, col_index++, input_mailbox_id);
14688 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
14690 temp_unix_time = timegm(&(meeting_req->start_time));
14691 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14692 temp_unix_time = timegm(&(meeting_req->end_time));
14693 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14695 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->location, 0, LOCATION_LEN_IN_MAIL_MEETING_TBL);
14696 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->global_object_id, 0, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
14698 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
14699 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.standard_name, 0, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
14700 temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
14701 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14702 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
14704 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.daylight_name, 0, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
14705 temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
14706 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14707 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
14710 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14711 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
14712 ("sqlite3_step fail:%d", rc));
14713 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14714 ("sqlite3_step fail:%d", rc));
14719 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14720 if (hStmt != NULL) {
14721 rc = sqlite3_finalize(hStmt);
14722 if (rc != SQLITE_OK) {
14723 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14724 error = EMAIL_ERROR_DB_FAILURE;
14728 if (err_code != NULL)
14731 EM_DEBUG_FUNC_END("ret [%d]", ret);
14735 INTERNAL_FUNC int emstorage_query_meeting_request(char *multi_user_name, const char *conditional_clause, email_meeting_request_t **output_meeting_req, int *output_result_count, int transaction)
14737 EM_DEBUG_FUNC_BEGIN("conditional_clause[%s] output_meeting_req[%p] output_result_count[%p] transaction[%d]", conditional_clause, output_meeting_req, output_result_count, transaction);
14744 int err = EMAIL_ERROR_NONE;
14745 char **result = NULL;
14746 char sql_query_string[QUERY_SIZE] = {0, };
14747 email_meeting_request_t* p_temp_meeting_req = NULL;
14748 sqlite3 *local_db_handle = NULL;
14749 time_t temp_unix_time;
14751 if (conditional_clause == NULL || output_meeting_req == NULL || output_result_count == NULL) {
14752 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14753 err = EMAIL_ERROR_INVALID_PARAM;
14757 col_index = _field_count_of_table[CREATE_TABLE_MAIL_MEETING_TBL];
14758 EM_DEBUG_LOG("col_index [%d]", col_index);
14760 local_db_handle = emstorage_get_db_connection(multi_user_name);
14762 EMSTORAGE_START_READ_TRANSACTION(transaction);
14764 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_meeting_tbl %s", conditional_clause);
14766 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14768 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14769 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
14770 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14773 EM_DEBUG_EXCEPTION("No meeting_request found...");
14774 err = EMAIL_ERROR_DATA_NOT_FOUND;
14778 EM_DEBUG_LOG("There are [%d] meeting requests.", count);
14779 if (!(p_temp_meeting_req = (email_meeting_request_t*)em_malloc(sizeof(email_meeting_request_t) * count))) {
14780 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
14781 err = EMAIL_ERROR_OUT_OF_MEMORY;
14785 EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
14787 for (i = 0; i < count; i++) {
14788 _get_table_field_data_int(result, &(p_temp_meeting_req[i].mail_id), col_index++);
14789 _get_table_field_data_int(result, &dummy, col_index++); /* account_id. but why should this field exist in DB table? */
14790 _get_table_field_data_int(result, &dummy, col_index++); /* mailbox_id */
14791 _get_table_field_data_int(result, (int*)&(p_temp_meeting_req[i].meeting_response), col_index++);
14792 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* start time */
14793 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].start_time));
14794 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* end time */
14795 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].end_time));
14796 _get_table_field_data_string(result, &p_temp_meeting_req[i].location, 1, col_index++);
14797 _get_table_field_data_string(result, &p_temp_meeting_req[i].global_object_id, 1, col_index++);
14798 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.offset_from_GMT), col_index++);
14799 _get_table_field_data_string_without_allocation(result, p_temp_meeting_req[i].time_zone.standard_name, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL, 1, col_index++);
14800 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14801 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.standard_time_start_date));
14802 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.standard_bias), col_index++);
14803 _get_table_field_data_string_without_allocation(result, p_temp_meeting_req[i].time_zone.daylight_name, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL, 1, col_index++);
14804 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14805 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.daylight_time_start_date));
14806 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.daylight_bias), col_index++);
14812 sqlite3_free_table(result);
14814 if (err == EMAIL_ERROR_NONE) {
14815 if (p_temp_meeting_req)
14816 *output_meeting_req = p_temp_meeting_req;
14817 *output_result_count = count;
14819 EM_SAFE_FREE(p_temp_meeting_req);
14821 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14823 // sqlite3_db_release_memory(local_db_handle);
14825 EM_DEBUG_FUNC_END("err [%d]", err);
14829 INTERNAL_FUNC int emstorage_get_meeting_request(char *multi_user_name, int mail_id, email_meeting_request_t ** meeting_req, int transaction, int *err_code)
14831 EM_DEBUG_FUNC_BEGIN();
14835 int error = EMAIL_ERROR_NONE;
14836 char conditional_clause[QUERY_SIZE] = {0, };
14838 EM_IF_NULL_RETURN_VALUE(meeting_req, false);
14841 SNPRINTF(conditional_clause, QUERY_SIZE, " WHERE mail_id = %d", mail_id);
14842 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
14844 if ((error = emstorage_query_meeting_request(multi_user_name, conditional_clause, meeting_req, &count, transaction)) != EMAIL_ERROR_NONE) {
14845 EM_DEBUG_EXCEPTION("emstorage_query_meeting_request failed. [%d]", error);
14849 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->mail_id[%d]", (*meeting_req)->mail_id);
14850 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->meeting_response[%d]", (*meeting_req)->meeting_response);
14851 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->start_time[%s]", asctime(&((*meeting_req)->start_time)));
14852 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->end_time[%s]", asctime(&((*meeting_req)->end_time)));
14853 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->location[%s]", (*meeting_req)->location);
14854 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->global_object_id[%s]", (*meeting_req)->global_object_id);
14855 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.offset_from_GMT[%d]", (*meeting_req)->time_zone.offset_from_GMT);
14856 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_name[%s]", (*meeting_req)->time_zone.standard_name);
14857 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.standard_time_start_date)));
14858 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_bias[%d]", (*meeting_req)->time_zone.standard_bias);
14859 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_name[%s]", (*meeting_req)->time_zone.daylight_name);
14860 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.daylight_time_start_date)));
14861 EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_bias[%d]", (*meeting_req)->time_zone.daylight_bias);
14866 if (err_code != NULL)
14869 EM_DEBUG_FUNC_END("ret [%d]", ret);
14873 INTERNAL_FUNC int emstorage_update_meeting_request(char *multi_user_name, email_meeting_request_t* meeting_req, int transaction, int *err_code)
14875 EM_DEBUG_FUNC_BEGIN("meeting_req[%p], transaction[%d], err_code[%p]", meeting_req, transaction, err_code);
14878 int error = EMAIL_ERROR_NONE;
14880 DB_STMT hStmt = NULL;
14881 char sql_query_string[QUERY_SIZE] = {0, };
14882 time_t temp_unix_time = 0;
14884 if (!meeting_req) {
14885 EM_DEBUG_EXCEPTION("Invalid Parameter!");
14886 if (err_code != NULL)
14887 *err_code = EMAIL_ERROR_INVALID_PARAM;
14891 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14892 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14894 memset(sql_query_string, 0x00, sizeof(sql_query_string));
14895 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14896 "UPDATE mail_meeting_tbl "
14898 /* " account_id = ?, " // not update here, this can be changed when move or copy */
14899 /* " mailbox_name = ?, " // not update here, this can be changed when move or copy */
14900 " meeting_response = ?, "
14901 " start_time = ?, "
14904 " global_object_id = ?, "
14906 " standard_name = ?, "
14907 " standard_time_start_date = ?, "
14908 " standard_bias = ?, "
14909 " daylight_name = ?, "
14910 " daylight_time_start_date = ?, "
14911 " daylight_bias = ? "
14912 "WHERE mail_id = %d",
14913 meeting_req->mail_id);
14915 EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
14917 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14918 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14919 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14921 EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
14922 EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
14923 EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
14924 EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
14925 EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
14926 EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
14927 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
14928 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
14929 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
14930 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
14931 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
14932 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
14933 EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
14937 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
14938 temp_unix_time = timegm(&(meeting_req->start_time));
14939 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14940 temp_unix_time = timegm(&(meeting_req->end_time));
14941 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14942 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->location, 1, LOCATION_LEN_IN_MAIL_MEETING_TBL);
14943 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->global_object_id, 1, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
14944 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
14945 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.standard_name, 1, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
14946 temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
14947 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14948 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
14949 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.daylight_name, 1, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
14950 temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
14951 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14952 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
14955 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14956 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14957 ("sqlite3_step fail:%d", rc));
14962 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14964 if (hStmt != NULL) {
14965 rc = sqlite3_finalize(hStmt);
14966 if (rc != SQLITE_OK) {
14967 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14968 error = EMAIL_ERROR_DB_FAILURE;
14972 if (err_code != NULL)
14975 EM_DEBUG_FUNC_END("ret [%d]", ret);
14979 INTERNAL_FUNC int emstorage_delete_meeting_request(char *multi_user_name, int account_id, int mail_id, int input_mailbox_id, int transaction, int *err_code)
14981 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], input_mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mail_id, input_mailbox_id, transaction, err_code);
14983 if (account_id < ALL_ACCOUNT || mail_id < 0) {
14984 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d]", account_id, mail_id);
14986 if (err_code != NULL)
14987 *err_code = EMAIL_ERROR_INVALID_PARAM;
14992 int error = EMAIL_ERROR_NONE;
14994 char sql_query_string[QUERY_SIZE] = {0, };
14996 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14997 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14999 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_meeting_tbl ");
15001 if (account_id != ALL_ACCOUNT) { /* NOT '0' means a specific account. '0' means all account */
15002 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE account_id = %d", account_id);
15006 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s mail_id = %d", (and ? "AND" : "WHERE"), mail_id);
15009 if (input_mailbox_id > 0) { /* 0 means all mailbox_id */
15010 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s mailbox_id = '%d'", (and ? "AND" : "WHERE"), input_mailbox_id);
15013 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15014 if (error != EMAIL_ERROR_NONE) {
15015 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
15022 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15027 EM_DEBUG_FUNC_END("ret [%d]", ret);
15032 INTERNAL_FUNC void emstorage_free_meeting_request(email_meeting_request_t *meeting_req)
15034 EM_DEBUG_FUNC_BEGIN();
15036 if (!meeting_req) return;
15038 EM_SAFE_FREE(meeting_req->location);
15039 EM_SAFE_FREE(meeting_req->global_object_id);
15041 EM_DEBUG_FUNC_END();
15044 INTERNAL_FUNC int emstorage_get_overflowed_mail_id_list(char *multi_user_name, int account_id, int input_mailbox_id, int mail_slot_size, int **mail_id_list, int *mail_id_count, int transaction, int *err_code)
15046 EM_DEBUG_FUNC_BEGIN("account_id [%d], input_mailbox_id [%d], mail_slot_size [%d], mail_id_list [%p], mail_id_count [%p], transaction [%d], err_code [%p]", account_id, input_mailbox_id, mail_slot_size, mail_id_list, mail_id_count, transaction, err_code);
15047 EM_PROFILE_BEGIN(profile_emstorage_get_overflowed_mail_id_list);
15048 char sql_query_string[QUERY_SIZE] = {0, };
15049 char **result = NULL;
15050 int rc = -1, ret = false;
15051 int error = EMAIL_ERROR_NONE;
15052 int counter = 0, col_index = 0;
15053 int result_mail_id_count = 0;
15054 int *result_mail_id_list = NULL;
15056 if (input_mailbox_id <= 0 || !mail_id_list || !mail_id_count || account_id < 1) {
15057 EM_DEBUG_EXCEPTION("Invalid Parameter");
15059 *err_code = EMAIL_ERROR_INVALID_PARAM;
15063 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl WHERE account_id = %d AND mailbox_id = %d ORDER BY date_time DESC LIMIT %d, 10000", account_id, input_mailbox_id, mail_slot_size);
15065 EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
15067 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15068 EMSTORAGE_START_READ_TRANSACTION(transaction);
15070 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_mail_id_count, 0, NULL), rc);
15071 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15072 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15074 if (!result_mail_id_count) {
15075 EM_DEBUG_LOG("No mail found...");
15077 error = EMAIL_ERROR_MAIL_NOT_FOUND;
15081 EM_DEBUG_LOG("There are [%d] overflowed mails in mailbox_id [%d]", result_mail_id_count, input_mailbox_id);
15083 if (!(result_mail_id_list = (int *)malloc(sizeof(int) * result_mail_id_count))) {
15084 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
15085 error = EMAIL_ERROR_OUT_OF_MEMORY;
15086 sqlite3_free_table(result);
15090 memset(result_mail_id_list, 0x00, sizeof(int) * result_mail_id_count);
15094 for (counter = 0; counter < result_mail_id_count; counter++)
15095 _get_table_field_data_int(result, result_mail_id_list + counter, col_index++);
15100 EM_DEBUG_LOG("finish off [%d]", ret);
15103 sqlite3_free_table(result);
15106 *mail_id_list = result_mail_id_list;
15107 *mail_id_count = result_mail_id_count;
15109 EM_SAFE_FREE(result_mail_id_list);
15111 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15113 if (err_code != NULL)
15116 EM_PROFILE_END(profile_emstorage_get_overflowed_mail_id_list);
15117 EM_DEBUG_FUNC_END("ret [%d]", ret);
15121 INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(char *multi_user_name, int mail_id, int *thread_id, int *err_code)
15123 EM_DEBUG_FUNC_BEGIN("mail_id[%d], thread_id[%p], err_code[%p]", mail_id, thread_id, err_code);
15125 int rc = -1, ret = false;
15126 int err = EMAIL_ERROR_NONE;
15127 char sql_query_string[QUERY_SIZE] = {0, };
15129 int result_count = 0;
15131 if (mail_id == 0 || thread_id == NULL) {
15132 EM_DEBUG_EXCEPTION("Invalid Parameter");
15134 *err_code = EMAIL_ERROR_INVALID_PARAM;
15138 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15140 memset(sql_query_string, 0, QUERY_SIZE);
15141 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT thread_id FROM mail_tbl WHERE mail_id = %d", mail_id);
15143 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
15144 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
15145 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15146 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15148 if (!result_count) {
15149 EM_DEBUG_LOG("No mail found...");
15151 err = EMAIL_ERROR_MAIL_NOT_FOUND;
15152 /* sqlite3_free_table(result); */
15156 _get_table_field_data_int(result, thread_id, 1);
15158 sqlite3_free_table(result);
15164 if (err_code != NULL)
15167 EM_DEBUG_FUNC_END("ret [%d]", ret);
15171 INTERNAL_FUNC int emstorage_update_latest_thread_mail(char *multi_user_name,
15176 int *updated_thread_id,
15177 int latest_mail_id,
15178 int thread_item_count,
15183 EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id [%d], thread_id[%d], updated_thread_id[%p], "
15184 "latest_mail_id [%d], thread_item_count[%d], err_code[%p]",
15185 account_id, mailbox_id, thread_id, updated_thread_id,
15186 latest_mail_id, thread_item_count, err_code);
15188 int rc = -1, ret = false;
15189 int err = EMAIL_ERROR_NONE;
15190 char sql_query_string[QUERY_SIZE] = {0, };
15191 char **result = NULL;
15192 int result_count = 0;
15194 if (thread_id == 0) {
15195 EM_DEBUG_EXCEPTION("Invalid Parameter");
15197 *err_code = EMAIL_ERROR_INVALID_PARAM;
15201 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15203 if (thread_item_count == 0 && latest_mail_id == 0) {
15204 memset(sql_query_string, 0, QUERY_SIZE);
15205 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id, count(*) FROM (SELECT account_id, mail_id, thread_id, mailbox_id FROM mail_tbl ORDER BY date_time) WHERE account_id = %d AND thread_id = %d AND mailbox_id = %d", account_id, thread_id, mailbox_id);
15207 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
15208 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
15209 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15210 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15211 EM_DEBUG_LOG("result_count[%d]", result_count);
15212 if (result_count == 0) {
15213 EM_DEBUG_LOG("No mail found...");
15216 *err_code = EMAIL_ERROR_MAIL_NOT_FOUND;
15217 sqlite3_free_table(result);
15221 _get_table_field_data_int(result, &latest_mail_id, 2);
15222 _get_table_field_data_int(result, &thread_item_count, 3);
15224 EM_DEBUG_LOG("latest_mail_id[%d]", latest_mail_id);
15225 EM_DEBUG_LOG("thread_item_count[%d]", thread_item_count);
15227 sqlite3_free_table(result);
15230 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15232 if (thread_item_count < 0) {
15233 memset(sql_query_string, 0, QUERY_SIZE);
15234 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = 0 WHERE account_id = %d AND thread_id = %d", account_id, thread_id);
15235 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15236 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15237 if (err != EMAIL_ERROR_NONE) {
15238 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15241 } else if (thread_id != latest_mail_id) {
15242 /* Initialize the thread id */
15243 memset(sql_query_string, 0, QUERY_SIZE);
15244 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = 0, thread_id = %d WHERE account_id = %d AND mailbox_id = %d AND thread_id = %d", latest_mail_id, account_id, mailbox_id, thread_id);
15245 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15246 if (err != EMAIL_ERROR_NONE) {
15247 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15251 /* update the thread item count */
15252 memset(sql_query_string, 0, QUERY_SIZE);
15253 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = %d WHERE account_id = %d AND mail_id = %d ", thread_item_count, account_id, latest_mail_id);
15254 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15255 if (err != EMAIL_ERROR_NONE) {
15256 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15260 memset(sql_query_string, 0, QUERY_SIZE);
15261 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = %d WHERE account_id = %d AND mail_id = %d ", thread_item_count, account_id, latest_mail_id);
15262 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15263 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15264 if (err != EMAIL_ERROR_NONE) {
15265 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15272 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15274 if (thread_id != latest_mail_id) {
15275 if (err == EMAIL_ERROR_NONE) {
15276 EM_DEBUG_LOG("noti_type[%d]", noti_type);
15278 if (latest_mail_id > 0 && thread_id > 0 && noti_type > 0) {
15279 char mailbox_id_str[25] = {0,};
15280 snprintf(mailbox_id_str, sizeof(mailbox_id_str), "%d", mailbox_id);
15281 if (!emcore_notify_storage_event(noti_type, thread_id, latest_mail_id, mailbox_id_str, account_id))
15282 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventfailed [NOTI_THREAD_ID_CHANGED] >>>> ");
15284 if (updated_thread_id) *updated_thread_id = latest_mail_id;
15287 } else if (thread_item_count >= 0) {
15288 if (err == EMAIL_ERROR_NONE) {
15289 char parameter_string[500] = {0,};
15290 SNPRINTF(parameter_string, sizeof(parameter_string), "%s%c%d", "thread_item_count", 0x01, latest_mail_id);
15291 if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT, parameter_string, thread_item_count))
15292 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventfailed [NOTI_MAIL_FIELD_UPDATE] >>>> ");
15296 if (err_code != NULL)
15299 EM_DEBUG_FUNC_END("ret [%d]", ret);
15303 INTERNAL_FUNC int emstorage_update_thread_id_of_mail(char *multi_user_name, int account_id, int mailbox_id, int mail_id, int thread_id, int thread_item_count, int transaction, int *err_code)
15305 EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id [%d], mail_id[%d], thread_id[%d], thread_item_count[%d], err_code[%p]", account_id, mailbox_id, mail_id, thread_id, thread_item_count, err_code);
15308 int err = EMAIL_ERROR_NONE;
15309 char sql_query_string[QUERY_SIZE] = {0, };
15311 if (thread_id == 0) {
15312 EM_DEBUG_EXCEPTION("Invalid Parameter");
15314 *err_code = EMAIL_ERROR_INVALID_PARAM;
15318 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15320 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15322 memset(sql_query_string, 0, QUERY_SIZE);
15323 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = %d, thread_id = %d WHERE account_id = %d AND mail_id = %d", thread_item_count, thread_id, account_id, mail_id);
15324 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15325 if (err != EMAIL_ERROR_NONE) {
15326 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15333 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15335 if (err_code != NULL)
15338 EM_DEBUG_FUNC_END("ret [%d]", ret);
15342 #ifdef __FEATURE_LOCAL_ACTIVITY__
15344 * emstorage_add_activity - Add Email Local activity during OFFLINE mode
15347 INTERNAL_FUNC int emstorage_add_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15349 EM_DEBUG_FUNC_BEGIN();
15351 EM_DEBUG_LOG(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15353 int rc = -1, ret = false;
15354 int error = EMAIL_ERROR_NONE;
15355 DB_STMT hStmt = NULL;
15356 char sql_query_string[8192] = { 0x00, };
15359 if (!local_activity) {
15360 EM_DEBUG_EXCEPTION(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15361 if (err_code != NULL)
15362 *err_code = EMAIL_ERROR_INVALID_PARAM;
15366 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15368 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15370 memset(sql_query_string, 0x00 , sizeof(sql_query_string));
15371 SNPRINTF(sql_query_string, sizeof(sql_query_string), "INSERT INTO mail_local_activity_tbl VALUES (?, ?, ?, ?, ?, ?, ?)");
15373 EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ] ", local_activity->activity_id);
15374 EM_DEBUG_LOG(">>>>> MAIL ID [ %d ] ", local_activity->mail_id);
15375 EM_DEBUG_LOG(">>>>> ACCOUNT ID [ %d ] ", local_activity->account_id);
15376 EM_DEBUG_LOG(">>>>> ACTIVITY TYPE [ %d ] ", local_activity->activity_type);
15377 EM_DEBUG_LOG_SEC(">>>>> SERVER MAIL ID [ %s ] ", local_activity->server_mailid);
15378 EM_DEBUG_LOG(">>>>> SOURCE MAILBOX [ %s ] ", local_activity->src_mbox);
15379 EM_DEBUG_LOG(">>>>> DEST MAILBOX [ %s ] ", local_activity->dest_mbox);
15381 EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15384 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15385 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15386 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15388 EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15390 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
15391 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
15392 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
15393 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
15394 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->server_mailid, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
15395 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->src_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15396 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->dest_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15399 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15401 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
15402 ("sqlite3_step fail:%d", rc));
15403 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15404 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
15409 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15411 if (hStmt != NULL) {
15412 rc = sqlite3_finalize(hStmt);
15413 if (rc != SQLITE_OK) {
15414 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15415 error = EMAIL_ERROR_DB_FAILURE;
15419 if (err_code != NULL)
15422 EM_DEBUG_FUNC_END("ret [%d]", ret);
15427 * emstorage_get_activity - Get the Local activity Information
15431 INTERNAL_FUNC int emstorage_get_activity(int account_id, int activityid, emstorage_activity_tbl_t** activity_list, int *select_num, int transaction, int *err_code)
15433 EM_DEBUG_FUNC_BEGIN();
15435 int i = 0, count = 0, rc = -1, ret = false;
15436 int error = EMAIL_ERROR_NONE;
15437 emstorage_activity_tbl_t *p_activity_tbl = NULL;
15438 char sql_query_string[1024] = {0x00, };
15439 char **result = NULL;
15442 EM_IF_NULL_RETURN_VALUE(activity_list, false);
15443 EM_IF_NULL_RETURN_VALUE(select_num, false);
15446 if (!select_num || !activity_list || account_id <= 0 || activityid < 0) {
15447 EM_DEBUG_LOG(" select_num[%p], activity_list[%p] account_id [%d] activityid [%d] ", select_num, activity_list, account_id, activityid);
15449 *err_code = EMAIL_ERROR_INVALID_PARAM;
15453 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15455 EMSTORAGE_START_READ_TRANSACTION(transaction);
15457 memset(sql_query_string, 0x00, sizeof(sql_query_string));
15459 if (activityid == ALL_ACTIVITIES) {
15460 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d order by activity_id", account_id);
15462 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d AND activity_id = %d ", account_id, activityid);
15465 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
15469 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15470 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15471 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15475 if (!(p_activity_tbl = (emstorage_activity_tbl_t*)em_malloc(sizeof(emstorage_activity_tbl_t) * count))) {
15476 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
15477 error = EMAIL_ERROR_OUT_OF_MEMORY;
15482 for (i = 0; i < count; i++) {
15483 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15484 if (result[col_index])
15485 p_activity_tbl[i].activity_id = atoi(result[col_index++]);
15487 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15488 if (result[col_index])
15489 p_activity_tbl[i].account_id = atoi(result[col_index++]);
15491 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15492 if (result[col_index])
15493 p_activity_tbl[i].mail_id = atoi(result[col_index++]);
15495 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15496 if (result[col_index])
15497 p_activity_tbl[i].activity_type = atoi(result[col_index++]);
15500 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15501 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15502 p_activity_tbl[i].server_mailid = EM_SAFE_STRDUP(result[col_index++]);
15506 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15507 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15508 p_activity_tbl[i].src_mbox = EM_SAFE_STRDUP(result[col_index++]);
15512 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15513 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15514 p_activity_tbl[i].dest_mbox = EM_SAFE_STRDUP(result[col_index++]);
15521 sqlite3_free_table(result);
15527 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15530 *activity_list = p_activity_tbl;
15531 *select_num = count;
15532 EM_DEBUG_LOG(">>>> COUNT : %d >> ", count);
15533 } else if (p_activity_tbl != NULL) {
15534 emstorage_free_local_activity(&p_activity_tbl, count, NULL);
15538 if (err_code != NULL)
15541 EM_DEBUG_FUNC_END("ret [%d]", ret);
15546 INTERNAL_FUNC int emstorage_get_next_activity_id(int *activity_id, int *err_code)
15549 EM_DEBUG_FUNC_BEGIN();
15552 int err = EMAIL_ERROR_NONE;
15555 char **result = NULL;
15557 if (NULL == activity_id) {
15558 EM_DEBUG_EXCEPTION(" activity_id[%p]", activity_id);
15560 *err_code = EMAIL_ERROR_INVALID_PARAM;
15564 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15566 /* increase unique id */
15568 sql = "SELECT max(rowid) FROM mail_local_activity_tbl;";
15570 /* rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); n EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc); */
15571 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15572 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
15574 if (NULL == result[1])
15577 rc = atoi(result[1])+1;
15582 sqlite3_free_table(result);
15591 EM_DEBUG_FUNC_END("ret [%d]", ret);
15596 INTERNAL_FUNC int emstorage_get_activity_id_list(char *multi_user_name, int account_id, int ** activity_id_list, int *activity_id_count, int lowest_activity_type, int highest_activity_type, int transaction, int *err_code)
15599 EM_DEBUG_FUNC_BEGIN();
15601 EM_DEBUG_LOG(" account_id[%d], activity_id_list[%p], activity_id_count[%p] err_code[%p]", account_id, activity_id_list, activity_id_count, err_code);
15603 if (account_id <= 0 || NULL == activity_id_list || NULL == activity_id_count || lowest_activity_type <= 0 || highest_activity_type <= 0) {
15604 if (err_code != NULL)
15605 *err_code = EMAIL_ERROR_INVALID_PARAM;
15610 int error = EMAIL_ERROR_NONE;
15611 int i = 0, rc = -1, count = 0;
15612 char sql_query_string[1024] = {0x00, };
15613 int *activity_ids = NULL;
15615 char **result = NULL;
15616 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15617 EMSTORAGE_START_READ_TRANSACTION(transaction);
15619 memset(sql_query_string, 0x00, sizeof(sql_query_string));
15621 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct activity_id FROM mail_local_activity_tbl WHERE account_id = %d AND activity_type >= %d AND activity_type <= %d order by activity_id", account_id, lowest_activity_type, highest_activity_type);
15623 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
15626 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15627 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15628 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15632 EM_DEBUG_LOG(" Activity COUNT : %d ... ", count);
15634 if (NULL == (activity_ids = (int *)em_malloc(sizeof(int) * count))) {
15635 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
15636 error = EMAIL_ERROR_OUT_OF_MEMORY;
15640 for (i = 0; i < count; i++) {
15641 activity_ids[i] = atoi(result[col_index]);
15643 EM_DEBUG_LOG("activity_id %d", activity_ids[i]);
15652 *activity_id_count = count;
15653 *activity_id_list = activity_ids;
15654 } else if (activity_ids != NULL) /* Prevent defect - 216566 */
15655 EM_SAFE_FREE(activity_ids);
15658 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15659 if (err_code != NULL) {
15663 EM_DEBUG_FUNC_END("ret [%d]", ret);
15667 INTERNAL_FUNC int emstorage_free_activity_id_list(int *activity_id_list, int *error_code)
15669 EM_DEBUG_FUNC_BEGIN();
15671 int error = EMAIL_ERROR_NONE;
15674 EM_DEBUG_LOG(" activity_id_list [%p]", activity_id_list);
15676 if (NULL == activity_id_list) {
15677 error = EMAIL_ERROR_INVALID_PARAM;
15680 EM_SAFE_FREE(activity_id_list);
15688 if (NULL != error_code) {
15689 *error_code = error;
15692 EM_DEBUG_FUNC_END("ret [%d]", ret);
15697 * emstorage_delete_local_activity - Deletes the Local acitivity Generated based on activity_type
15698 * or based on server mail id
15701 INTERNAL_FUNC int emstorage_delete_local_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15703 EM_DEBUG_FUNC_BEGIN();
15706 EM_DEBUG_LOG(" local_activity[%p] ", local_activity);
15708 if (!local_activity) {
15709 EM_DEBUG_EXCEPTION(" local_activity[%p] ", local_activity);
15710 if (err_code != NULL)
15711 *err_code = EMAIL_ERROR_INVALID_PARAM;
15715 int rc = -1, ret = false; /* Prevent_FIX */
15716 int err = EMAIL_ERROR_NONE;
15718 int query_where = 0;
15719 char sql_query_string[8192] = { 0x00, };
15720 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15721 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15723 memset(sql_query_string, 0x00, sizeof(sql_query_string));
15725 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_local_activity_tbl ");
15727 EM_DEBUG_LOG_SEC(">>> Query [ %s ] ", sql_query_string);
15729 if (local_activity->account_id) {
15730 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15731 " WHERE account_id = %d ", local_activity->account_id);
15736 if (local_activity->server_mailid) {
15737 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15738 " %s %s server_mailid = '%s' ", query_where ? "" : "WHERE", query_and ? "AND" : "", local_activity->server_mailid);
15744 if (local_activity->mail_id) {
15745 EM_DEBUG_LOG(">>>> MAIL ID [ %d ] , ACTIVITY TYPE [%d ]", local_activity->mail_id, local_activity->activity_type);
15747 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15748 " %s %s mail_id = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "", local_activity->mail_id);
15755 if (local_activity->activity_type > 0) {
15756 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15757 " %s %s activity_type = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "" , local_activity->activity_type);
15760 if (local_activity->activity_id > 0) {
15761 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15762 " %s %s activity_id = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "" , local_activity->activity_id);
15766 EM_DEBUG_LOG_SEC(">>>>> Query [ %s ] ", sql_query_string);
15767 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15768 if (error != EMAIL_ERROR_NONE) {
15769 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
15773 rc = sqlite3_changes(local_db_handle);
15775 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
15776 err = EMAIL_ERROR_MAILBOX_NOT_FOUND;
15782 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15784 if (hStmt != NULL) {
15785 rc = sqlite3_finalize(hStmt);
15786 if (rc != SQLITE_OK) {
15787 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15788 error = EMAIL_ERROR_DB_FAILURE;
15792 if (err_code != NULL)
15794 EM_DEBUG_FUNC_END("ret [%d]", ret);
15799 * emstorage_free_local_activity - Free the Local Activity data
15801 INTERNAL_FUNC int emstorage_free_local_activity(emstorage_activity_tbl_t **local_activity_list, int count, int *err_code)
15803 EM_DEBUG_FUNC_BEGIN();
15805 EM_DEBUG_LOG(" local_activity_list[%p], count[%d], err_code[%p]", local_activity_list, count, err_code);
15808 int error = EMAIL_ERROR_INVALID_PARAM;
15811 if (!local_activity_list || !*local_activity_list) {
15812 EM_DEBUG_EXCEPTION(" local_activity_list[%p], count[%d]", local_activity_list, count);
15814 error = EMAIL_ERROR_INVALID_PARAM;
15818 emstorage_activity_tbl_t* p = *local_activity_list;
15821 for (; i < count; i++) {
15822 EM_SAFE_FREE(p[i].dest_mbox);
15823 EM_SAFE_FREE(p[i].src_mbox);
15824 EM_SAFE_FREE(p[i].server_mailid);
15828 *local_activity_list = NULL;
15836 if (err_code != NULL)
15839 EM_DEBUG_FUNC_END("ret [%d]", ret);
15843 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
15846 static int _get_key_value_string_for_list_filter_rule(email_list_filter_rule_t *input_list_filter_rule, char **output_key_value_string)
15848 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_key_value_string [%p]", input_list_filter_rule, output_key_value_string);
15850 int ret = EMAIL_ERROR_NONE;
15851 char key_value_string[QUERY_SIZE] = { 0, };
15852 char *temp_key_value_1 = NULL;
15853 char *temp_key_value_2 = NULL;
15855 if (input_list_filter_rule == NULL || output_key_value_string == NULL) {
15856 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15857 return EMAIL_ERROR_INVALID_PARAM;
15860 switch (input_list_filter_rule->target_attribute) {
15861 case EMAIL_MAIL_ATTRIBUTE_MAIL_ID:
15862 case EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID:
15863 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID:
15864 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE:
15865 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_STATUS:
15866 case EMAIL_MAIL_ATTRIBUTE_REFERENCE_MAIL_ID:
15867 case EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS:
15868 case EMAIL_MAIL_ATTRIBUTE_MAIL_SIZE:
15869 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN:
15870 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML:
15871 case EMAIL_MAIL_ATTRIBUTE_FILE_SIZE:
15872 case EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD:
15873 case EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD:
15874 case EMAIL_MAIL_ATTRIBUTE_FLAGS_FLAGGED_FIELD:
15875 case EMAIL_MAIL_ATTRIBUTE_FLAGS_ANSWERED_FIELD:
15876 case EMAIL_MAIL_ATTRIBUTE_FLAGS_RECENT_FIELD:
15877 case EMAIL_MAIL_ATTRIBUTE_FLAGS_DRAFT_FIELD:
15878 case EMAIL_MAIL_ATTRIBUTE_FLAGS_FORWARDED_FIELD:
15879 case EMAIL_MAIL_ATTRIBUTE_DRM_STATUS:
15880 case EMAIL_MAIL_ATTRIBUTE_PRIORITY:
15881 case EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS:
15882 case EMAIL_MAIL_ATTRIBUTE_LOCK_STATUS:
15883 case EMAIL_MAIL_ATTRIBUTE_REPORT_STATUS:
15884 case EMAIL_MAIL_ATTRIBUTE_ATTACHMENT_COUNT:
15885 case EMAIL_MAIL_ATTRIBUTE_INLINE_CONTENT_COUNT:
15886 case EMAIL_MAIL_ATTRIBUTE_THREAD_ID:
15887 case EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT:
15888 case EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS:
15889 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_CLASS:
15890 case EMAIL_MAIL_ATTRIBUTE_DIGEST_TYPE:
15891 case EMAIL_MAIL_ATTRIBUTE_SMIME_TYPE:
15892 case EMAIL_MAIL_ATTRIBUTE_REMAINING_RESEND_TIMES:
15893 case EMAIL_MAIL_ATTRIBUTE_TAG_ID:
15894 case EMAIL_MAIL_ATTRIBUTE_EAS_DATA_LENGTH_TYPE:
15895 SNPRINTF(key_value_string, QUERY_SIZE, "%d", input_list_filter_rule->key_value.integer_type_value);
15898 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
15899 case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
15900 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
15901 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
15902 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
15903 case EMAIL_MAIL_ATTRIBUTE_FROM:
15904 case EMAIL_MAIL_ATTRIBUTE_TO:
15905 case EMAIL_MAIL_ATTRIBUTE_CC:
15906 case EMAIL_MAIL_ATTRIBUTE_BCC:
15907 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
15908 if (input_list_filter_rule->key_value.string_type_value == NULL) {
15909 EM_DEBUG_EXCEPTION("Invalid string_type_value [%p]", input_list_filter_rule->key_value.string_type_value);
15910 ret = EMAIL_ERROR_INVALID_PARAM;
15914 temp_key_value_1 = input_list_filter_rule->key_value.string_type_value;
15916 temp_key_value_2 = em_replace_all_string(temp_key_value_1, "_", "\\_");
15917 temp_key_value_1 = em_replace_all_string(temp_key_value_2, "%", "\\%");
15919 if (input_list_filter_rule->rule_type == EMAIL_LIST_FILTER_RULE_INCLUDE)
15920 SNPRINTF(key_value_string, QUERY_SIZE, "\'%%%s%%\'", temp_key_value_1);
15922 SNPRINTF(key_value_string, QUERY_SIZE, "\'%s\'", temp_key_value_1);
15925 case EMAIL_MAIL_ATTRIBUTE_DATE_TIME:
15926 case EMAIL_MAIL_ATTRIBUTE_SCHEDULED_SENDING_TIME:
15927 case EMAIL_MAIL_ATTRIBUTE_REPLIED_TIME:
15928 case EMAIL_MAIL_ATTRIBUTE_FORWARDED_TIME:
15929 SNPRINTF(key_value_string, QUERY_SIZE, "%d", (int)input_list_filter_rule->key_value.datetime_type_value);
15933 ret = EMAIL_ERROR_INVALID_PARAM;
15934 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
15938 if (ret == EMAIL_ERROR_NONE && EM_SAFE_STRLEN(key_value_string) > 0) {
15939 *output_key_value_string = strdup(key_value_string);
15944 EM_SAFE_FREE(temp_key_value_1);
15945 EM_SAFE_FREE(temp_key_value_2);
15947 EM_DEBUG_FUNC_END("ret [%d]", ret);
15951 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15952 static int _get_cert_password_file_name(int index, char *cert_password_file_name)
15954 EM_DEBUG_FUNC_BEGIN("index : [%d]", index);
15956 if (index <= 0 || !cert_password_file_name) {
15957 EM_DEBUG_EXCEPTION("Invalid parameter");
15958 return EMAIL_ERROR_INVALID_PARAM;
15961 sprintf(cert_password_file_name, ".email_cert_%d", index);
15963 EM_DEBUG_FUNC_END();
15964 return EMAIL_ERROR_NONE;
15968 static int _make_filter_rule_string(email_list_filter_rule_t *input_list_filter_rule, char **output_string)
15970 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
15972 int ret = EMAIL_ERROR_NONE;
15974 int length_field_name = 0;
15975 int length_value = 0;
15976 char result_rule_string[QUERY_SIZE] = { 0 , };
15977 char *mod_field_name_string = NULL;
15978 char *mod_value_string = NULL;
15979 char *temp_field_name_string = NULL;
15980 char *temp_key_value_string = NULL;
15982 if (input_list_filter_rule == NULL || output_string == NULL) {
15983 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15984 return EMAIL_ERROR_INVALID_PARAM;
15987 temp_field_name_string = emcore_get_mail_field_name_by_attribute_type(input_list_filter_rule->target_attribute);
15989 if (temp_field_name_string == NULL) {
15990 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
15991 return EMAIL_ERROR_INVALID_PARAM;
15994 if (_get_key_value_string_for_list_filter_rule(input_list_filter_rule, &temp_key_value_string) != EMAIL_ERROR_NONE || temp_key_value_string == NULL) {
15995 EM_DEBUG_EXCEPTION("_get_key_value_string_for_list_filter_rule failed");
15996 return EMAIL_ERROR_INVALID_PARAM;
15999 length_field_name = EM_SAFE_STRLEN(temp_field_name_string);
16000 length_value = EM_SAFE_STRLEN(temp_key_value_string);
16002 switch (input_list_filter_rule->target_attribute) {
16003 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
16004 case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
16005 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
16006 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
16007 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
16008 case EMAIL_MAIL_ATTRIBUTE_FROM:
16009 case EMAIL_MAIL_ATTRIBUTE_TO:
16010 case EMAIL_MAIL_ATTRIBUTE_CC:
16011 case EMAIL_MAIL_ATTRIBUTE_BCC:
16012 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
16020 if (is_alpha == 1 && input_list_filter_rule->case_sensitivity == false) {
16021 length_field_name += strlen("UPPER() ");
16022 length_value += strlen("UPPER() ");
16023 mod_field_name_string = em_malloc(sizeof(char) * length_field_name);
16024 if (mod_field_name_string == NULL) {
16025 EM_DEBUG_EXCEPTION("em_mallocfailed");
16026 EM_SAFE_FREE(temp_field_name_string);
16027 ret = EMAIL_ERROR_OUT_OF_MEMORY;
16031 mod_value_string = em_malloc(sizeof(char) * length_value);
16032 if (mod_value_string == NULL) {
16033 EM_DEBUG_EXCEPTION("em_mallocfailed");
16034 EM_SAFE_FREE(temp_field_name_string);
16035 ret = EMAIL_ERROR_OUT_OF_MEMORY;
16039 SNPRINTF(mod_field_name_string, length_field_name, "UPPER(%s)", temp_field_name_string);
16040 SNPRINTF(mod_value_string, length_value, "UPPER(%s)", temp_key_value_string);
16041 EM_SAFE_FREE(temp_key_value_string);
16043 mod_field_name_string = strdup(temp_field_name_string);
16044 mod_value_string = temp_key_value_string;
16047 switch (input_list_filter_rule->rule_type) {
16048 case EMAIL_LIST_FILTER_RULE_EQUAL:
16049 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = %s ", mod_field_name_string, mod_value_string);
16051 case EMAIL_LIST_FILTER_RULE_NOT_EQUAL:
16052 SNPRINTF(result_rule_string, QUERY_SIZE, "%s != %s ", mod_field_name_string, mod_value_string);
16054 case EMAIL_LIST_FILTER_RULE_LESS_THAN:
16055 SNPRINTF(result_rule_string, QUERY_SIZE, "%s < %s ", mod_field_name_string, mod_value_string);
16057 case EMAIL_LIST_FILTER_RULE_GREATER_THAN:
16058 SNPRINTF(result_rule_string, QUERY_SIZE, "%s > %s ", mod_field_name_string, mod_value_string);
16060 case EMAIL_LIST_FILTER_RULE_LESS_THAN_OR_EQUAL:
16061 SNPRINTF(result_rule_string, QUERY_SIZE, "%s <= %s ", mod_field_name_string, mod_value_string);
16063 case EMAIL_LIST_FILTER_RULE_GREATER_THAN_OR_EQUAL:
16064 SNPRINTF(result_rule_string, QUERY_SIZE, "%s >= %s ", mod_field_name_string, mod_value_string);
16066 case EMAIL_LIST_FILTER_RULE_INCLUDE:
16067 SNPRINTF(result_rule_string, QUERY_SIZE, "%s LIKE %s ", mod_field_name_string, mod_value_string);
16069 case EMAIL_LIST_FILTER_RULE_IN:
16070 SNPRINTF(result_rule_string, QUERY_SIZE, "%s IN (%s) ", mod_field_name_string, mod_value_string);
16072 case EMAIL_LIST_FILTER_RULE_NOT_IN:
16073 SNPRINTF(result_rule_string, QUERY_SIZE, "%s NOT IN (%s) ", mod_field_name_string, mod_value_string);
16076 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16077 ret = EMAIL_ERROR_INVALID_PARAM;
16081 *output_string = strdup(result_rule_string);
16084 EM_SAFE_FREE(mod_field_name_string);
16085 EM_SAFE_FREE(mod_value_string);
16087 EM_DEBUG_FUNC_END("ret [%d]", ret);
16091 static int _make_filter_attach_rule_string(char *multi_user_name, email_list_filter_rule_attach_t *input_list_filter_rule, char **output_string)
16093 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16095 char *field_name_string = NULL;
16096 char key_value_string[QUERY_SIZE] = {0,};
16097 char result_rule_string[QUERY_SIZE] = {0,};
16100 int query_size = 0;
16103 int error = EMAIL_ERROR_NONE;
16104 char **result = NULL;
16105 char sql_query_string[QUERY_SIZE] = {0,};
16106 char *sql_query_string2 = NULL;
16107 sqlite3 *local_db_handle = NULL;
16108 int *mail_ids = NULL;
16110 if (input_list_filter_rule == NULL || output_string == NULL) {
16111 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16112 return EMAIL_ERROR_INVALID_PARAM;
16115 field_name_string = EM_SAFE_STRDUP("attachment_name");
16116 SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
16118 switch (input_list_filter_rule->rule_type) {
16120 case EMAIL_LIST_FILTER_RULE_INCLUDE:
16121 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
16124 case EMAIL_LIST_FILTER_RULE_MATCH:
16125 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
16129 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16130 error = EMAIL_ERROR_INVALID_PARAM;
16134 local_db_handle = emstorage_get_db_connection(multi_user_name);
16136 EMSTORAGE_START_READ_TRANSACTION(true);
16137 SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_attachment_tbl %s", result_rule_string);
16139 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16140 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16141 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16146 EM_DEBUG_LOG("No mail found...");
16147 error = EMAIL_ERROR_MAIL_NOT_FOUND;
16148 *output_string = strdup("mail_id IN () ");
16152 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
16154 if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
16155 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
16156 error = EMAIL_ERROR_OUT_OF_MEMORY;
16160 for (i = 0; i < count; i++) {
16161 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16162 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16165 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >>", count);
16166 sqlite3_free_table(result);
16167 EMSTORAGE_FINISH_READ_TRANSACTION(true);
16169 // sqlite3_db_release_memory(local_db_handle);
16172 query_size = (10 * count) + strlen("mail_id IN () ");
16174 sql_query_string2 = em_malloc(query_size);
16175 if (sql_query_string2 == NULL) {
16176 EM_DEBUG_EXCEPTION("em_mallocfailed");
16177 error = EMAIL_ERROR_OUT_OF_MEMORY;
16181 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN (");
16182 for (i = 0; i < count-1; i++) {
16183 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16185 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d) ", mail_ids[count-1]);
16187 *output_string = strdup(sql_query_string2);
16190 EM_SAFE_FREE(mail_ids); /* prevent */
16191 EM_SAFE_FREE(sql_query_string2);
16192 EM_SAFE_FREE(field_name_string);
16193 EM_DEBUG_FUNC_END("error [%d]", error);
16197 static int _make_filter_fts_rule_string(char *multi_user_name, email_list_filter_rule_fts_t *input_list_filter_rule, char **output_string)
16199 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16200 char *field_name_string = NULL;
16201 char key_value_string[QUERY_SIZE] = {0,};
16202 char result_rule_string[QUERY_SIZE] = {0,};
16206 int query_size = 0;
16207 int error = EMAIL_ERROR_NONE;
16208 char **result = NULL;
16209 char sql_query_string[QUERY_SIZE] = {0,};
16210 char *sql_query_string2 = NULL;
16211 sqlite3 *local_db_handle = NULL;
16212 int *mail_ids = NULL;
16214 if (input_list_filter_rule == NULL || output_string == NULL) {
16215 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16216 return EMAIL_ERROR_INVALID_PARAM;
16219 field_name_string = EM_SAFE_STRDUP("body_text");
16220 SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
16222 switch (input_list_filter_rule->rule_type) {
16224 case EMAIL_LIST_FILTER_RULE_INCLUDE:
16225 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
16228 case EMAIL_LIST_FILTER_RULE_MATCH:
16229 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
16233 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16234 error = EMAIL_ERROR_INVALID_PARAM;
16238 local_db_handle = emstorage_get_db_connection(multi_user_name);
16240 EMSTORAGE_START_READ_TRANSACTION(true);
16241 SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_text_tbl %s", result_rule_string);
16243 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16244 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16245 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16250 EM_DEBUG_LOG("No mail found...");
16251 error = EMAIL_ERROR_MAIL_NOT_FOUND;
16252 *output_string = strdup("mail_id IN () ");
16256 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
16259 if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
16260 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
16261 error = EMAIL_ERROR_OUT_OF_MEMORY;
16265 for (i = 0; i < count; i++) {
16266 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16267 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16270 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >>", count);
16271 sqlite3_free_table(result);
16272 EMSTORAGE_FINISH_READ_TRANSACTION(true);
16274 // sqlite3_db_release_memory(local_db_handle);
16277 query_size = (10 * count) + strlen("mail_id IN () ");
16278 sql_query_string2 = em_malloc(query_size);
16279 if (sql_query_string2 == NULL) {
16280 EM_DEBUG_EXCEPTION("em_mallocfailed");
16281 error = EMAIL_ERROR_OUT_OF_MEMORY;
16285 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN (");
16286 for (i = 0; i < count-1; i++) {
16287 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16289 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d) ", mail_ids[count-1]);
16291 *output_string = strdup(sql_query_string2);
16294 EM_SAFE_FREE(mail_ids); /* prevent */
16295 EM_SAFE_FREE(sql_query_string2);
16296 EM_SAFE_FREE(field_name_string);
16297 EM_DEBUG_FUNC_END("error [%d]", error);
16301 static int _make_order_rule_string(char *multi_user_name, email_list_sorting_rule_t *input_sorting_rule, char **output_string)
16303 EM_DEBUG_FUNC_BEGIN("input_sorting_rule [%p], output_string [%p]", input_sorting_rule, output_string);
16305 char result_rule_string[QUERY_SIZE] = { 0 , };
16306 int ret = EMAIL_ERROR_NONE;
16308 emstorage_account_tbl_t *account_tbl_array = NULL;
16311 char *result_str = NULL;
16312 char *tmp_str1 = NULL;
16313 char *tmp_str2 = NULL;
16314 char query_per_account[QUERY_SIZE] = { 0 , };
16316 if (input_sorting_rule->force_boolean_check) {
16317 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = 0 ", emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16319 EM_SAFE_STRCPY(result_rule_string, emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16321 switch (input_sorting_rule->sort_order) {
16322 case EMAIL_SORT_ORDER_ASCEND:
16323 EM_SAFE_STRCAT(result_rule_string, " ASC ");
16326 case EMAIL_SORT_ORDER_DESCEND:
16327 EM_SAFE_STRCAT(result_rule_string, " DESC ");
16330 case EMAIL_SORT_ORDER_NOCASE_ASCEND:
16331 EM_SAFE_STRCAT(result_rule_string, " COLLATE NOCASE ASC ");
16334 case EMAIL_SORT_ORDER_NOCASE_DESCEND:
16335 EM_SAFE_STRCAT(result_rule_string, " COLLATE NOCASE DESC ");
16338 case EMAIL_SORT_ORDER_TO_CCBCC:
16339 memset(result_rule_string, 0, QUERY_SIZE);
16340 if (input_sorting_rule->key_value.string_type_value)
16341 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16342 " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 ELSE 2 END ",
16343 input_sorting_rule->key_value.string_type_value);
16346 case EMAIL_SORT_ORDER_TO_CC_BCC:
16347 memset(result_rule_string, 0, QUERY_SIZE);
16348 if (input_sorting_rule->key_value.string_type_value)
16349 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16350 " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 WHEN full_address_cc LIKE \'%%%q%%\' THEN 2 ELSE 3 END ",
16351 input_sorting_rule->key_value.string_type_value, input_sorting_rule->key_value.string_type_value);
16354 case EMAIL_SORT_ORDER_TO_CCBCC_ALL:
16355 if (!emstorage_get_account_list(multi_user_name, &count, &account_tbl_array, true, false, NULL)) {
16356 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed");
16361 EM_DEBUG_LOG("No account exist");
16362 ret = EMAIL_ERROR_INVALID_PARAM;
16366 for (i = 0; i < count; i++) {
16367 if (i > 0 && result_str) {
16368 tmp_str2 = result_str;
16369 result_str = g_strconcat(tmp_str2, " OR ", NULL);
16370 EM_SAFE_FREE(tmp_str2);
16373 memset(query_per_account, 0, QUERY_SIZE);
16374 snprintf(query_per_account, QUERY_SIZE,
16375 "(account_id = %d AND full_address_to LIKE \'%%%s%%\')",
16376 account_tbl_array[i].account_id, account_tbl_array[i].user_email_address);
16378 tmp_str1 = result_str;
16380 result_str = g_strconcat(tmp_str1, query_per_account, NULL);
16382 result_str = g_strdup(query_per_account);
16383 EM_SAFE_FREE(tmp_str1);
16386 snprintf(result_rule_string, QUERY_SIZE,
16387 " CASE WHEN %s THEN 1 ELSE 2 END ", result_str);
16389 EM_SAFE_FREE(result_str);
16390 if (account_tbl_array)
16391 emstorage_free_account(&account_tbl_array, count, NULL);
16394 case EMAIL_SORT_ORDER_LOCALIZE_ASCEND:
16395 memset(result_rule_string, 0, QUERY_SIZE);
16396 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16397 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END ASC, %s COLLATE NOCASE ASC ",
16398 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16399 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16402 case EMAIL_SORT_ORDER_LOCALIZE_DESCEND:
16403 memset(result_rule_string, 0, QUERY_SIZE);
16404 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16405 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END DESC, %s COLLATE NOCASE DESC ",
16406 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16407 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16411 EM_DEBUG_EXCEPTION("Invalid sort_order [%d]", input_sorting_rule->sort_order);
16412 ret = EMAIL_ERROR_INVALID_PARAM;
16416 *output_string = strdup(result_rule_string);
16419 EM_DEBUG_FUNC_END("ret [%d]", ret);
16423 INTERNAL_FUNC int emstorage_write_conditional_clause_for_getting_mail_list(char *multi_user_name, email_list_filter_t *input_filter_list, int input_filter_count, email_list_sorting_rule_t *input_sorting_rule_list, int input_sorting_rule_count, int input_start_index, int input_limit_count, char **output_conditional_clause)
16425 EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d], input_sorting_rule_list[%p], input_sorting_rule_count [%d], input_start_index [%d], input_limit_count [%d], output_conditional_clause [%p]", input_filter_list, input_filter_count, input_sorting_rule_list, input_sorting_rule_count, input_start_index, input_limit_count, output_conditional_clause);
16426 int ret = EMAIL_ERROR_NONE;
16428 int string_offset = 0;
16429 int query_size = 0;
16430 int new_query_size = 0;
16431 char *conditional_clause_string = NULL;
16432 char *result_string_for_a_item = NULL;
16434 if ((input_filter_count > 0 && !input_filter_list) || (input_sorting_rule_count > 0 && !input_sorting_rule_list) || output_conditional_clause == NULL) {
16435 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16436 return EMAIL_ERROR_INVALID_PARAM;
16439 conditional_clause_string = em_malloc(QUERY_SIZE);
16440 if (conditional_clause_string == NULL) {
16441 EM_DEBUG_EXCEPTION("Memory is full");
16442 return EMAIL_ERROR_OUT_OF_MEMORY;
16445 if (input_filter_count > 0) {
16446 query_size = QUERY_SIZE;
16447 strcpy(conditional_clause_string, " WHERE ");
16449 for (i = 0; i < input_filter_count; i++) {
16450 switch (input_filter_list[i].list_filter_item_type) {
16451 case EMAIL_LIST_FILTER_ITEM_RULE:
16452 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE", i);
16453 _make_filter_rule_string(&(input_filter_list[i].list_filter_item.rule), &result_string_for_a_item);
16456 case EMAIL_LIST_FILTER_ITEM_RULE_FTS:
16457 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_FTS", i);
16458 _make_filter_fts_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_fts), &result_string_for_a_item);
16461 case EMAIL_LIST_FILTER_ITEM_RULE_ATTACH:
16462 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_ATTACH", i);
16463 _make_filter_attach_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_attach), &result_string_for_a_item);
16466 case EMAIL_LIST_FILTER_ITEM_OPERATOR:
16467 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_OPERATOR", i);
16468 switch (input_filter_list[i].list_filter_item.operator_type) {
16469 case EMAIL_LIST_FILTER_OPERATOR_AND:
16470 result_string_for_a_item = strdup("AND ");
16472 case EMAIL_LIST_FILTER_OPERATOR_OR:
16473 result_string_for_a_item = strdup("OR ");
16475 case EMAIL_LIST_FILTER_OPERATOR_LEFT_PARENTHESIS:
16476 result_string_for_a_item = strdup(" (");
16478 case EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS:
16479 result_string_for_a_item = strdup(") ");
16485 EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", input_filter_list[i].list_filter_item_type);
16486 ret = EMAIL_ERROR_INVALID_PARAM;
16490 if (result_string_for_a_item == NULL) {
16491 EM_DEBUG_EXCEPTION("result_string_for_a_item is null");
16492 ret = EMAIL_ERROR_INVALID_PARAM;
16496 if (strlen(conditional_clause_string) + EM_SAFE_STRLEN(result_string_for_a_item) >= query_size) { /* prevent 34364 */
16497 EM_DEBUG_LOG("QUERY is too long");
16498 new_query_size = EM_SAFE_STRLEN(result_string_for_a_item) + EM_SAFE_STRLEN(conditional_clause_string) + QUERY_SIZE;
16499 conditional_clause_string = realloc(conditional_clause_string, new_query_size);
16500 if (conditional_clause_string == NULL) {
16501 EM_DEBUG_EXCEPTION("realloc failed");
16502 ret = EMAIL_ERROR_OUT_OF_MEMORY;
16506 query_size = new_query_size;
16509 strcat(conditional_clause_string, result_string_for_a_item);
16510 EM_SAFE_FREE(result_string_for_a_item);
16514 if (input_sorting_rule_count > 0) {
16515 strcat(conditional_clause_string, "ORDER BY ");
16517 for (i = 0; i < input_sorting_rule_count; i++) {
16518 if ((ret = _make_order_rule_string(multi_user_name, &input_sorting_rule_list[i], &result_string_for_a_item)) != EMAIL_ERROR_NONE) {
16519 EM_DEBUG_EXCEPTION("_make_order_rule_string failed. [%d]", ret);
16523 strcat(conditional_clause_string, ", ");
16524 strcat(conditional_clause_string, result_string_for_a_item);
16525 EM_SAFE_FREE(result_string_for_a_item);
16529 if (input_start_index != -1 && input_limit_count != -1) {
16530 string_offset = strlen(conditional_clause_string);
16531 SNPRINTF_OFFSET(conditional_clause_string, string_offset, query_size, " LIMIT %d, %d", input_start_index, input_limit_count);
16534 *output_conditional_clause = strdup(conditional_clause_string);
16537 EM_SAFE_FREE(result_string_for_a_item);
16538 EM_SAFE_FREE(conditional_clause_string);
16540 EM_DEBUG_FUNC_END("ret [%d]", ret);
16544 INTERNAL_FUNC int emstorage_free_list_filter(email_list_filter_t **input_filter_list, int input_filter_count)
16546 EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d]", input_filter_list, input_filter_count);
16547 int err = EMAIL_ERROR_NONE;
16549 email_list_filter_t *temp_filter_list = NULL;
16551 EM_IF_NULL_RETURN_VALUE(input_filter_list, EMAIL_ERROR_INVALID_PARAM);
16553 for (i = 0; i < input_filter_count; i++) {
16554 temp_filter_list = (*input_filter_list) + i;
16555 if (!temp_filter_list) {
16559 if (temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE) {
16560 switch (temp_filter_list->list_filter_item.rule.target_attribute) {
16561 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
16562 case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
16563 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
16564 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
16565 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
16566 case EMAIL_MAIL_ATTRIBUTE_FROM:
16567 case EMAIL_MAIL_ATTRIBUTE_TO:
16568 case EMAIL_MAIL_ATTRIBUTE_CC:
16569 case EMAIL_MAIL_ATTRIBUTE_BCC:
16570 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN:
16571 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML:
16572 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
16573 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule.key_value.string_type_value);
16578 } else if (temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE_FTS && temp_filter_list->list_filter_item.rule_fts.target_attribute == EMAIL_MAIL_TEXT_ATTRIBUTE_FULL_TEXT) {
16579 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_fts.key_value.string_type_value);
16580 } else if (temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE_ATTACH && temp_filter_list->list_filter_item.rule_attach.target_attribute == EMAIL_MAIL_ATTACH_ATTRIBUTE_ATTACHMENT_NAME) {
16581 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_attach.key_value.string_type_value);
16585 EM_SAFE_FREE(*input_filter_list);
16587 EM_DEBUG_FUNC_END("err [%d]", err);
16591 /* Tasks --------------------------------------------------------------------------*/
16592 INTERNAL_FUNC int emstorage_add_task(char *multi_user_name, email_task_type_t input_task_type, email_task_priority_t input_task_priority, char *input_task_parameter, int input_task_parameter_length, int input_transaction, int *output_task_id)
16594 EM_DEBUG_FUNC_BEGIN("input_task_type [%d] input_task_priority[%p], input_task_parameter[%p] input_task_parameter_length[%d] input_transaction[%d] output_task_id[%p]", input_task_type, input_task_priority, input_task_parameter, input_task_parameter_length, input_transaction, output_task_id);
16598 int err = EMAIL_ERROR_NONE;
16600 DB_STMT hStmt = NULL;
16601 char sql_query_string[QUERY_SIZE] = {0, };
16602 sqlite3 *local_db_handle = NULL;
16603 char *sql = "SELECT max(rowid) FROM mail_task_tbl;";
16604 char **result = NULL;
16606 if (input_task_parameter == NULL || output_task_id == NULL) {
16607 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16608 return EMAIL_ERROR_INVALID_PARAM;
16611 local_db_handle = emstorage_get_db_connection(multi_user_name);
16612 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, err);
16614 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
16615 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16616 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
16618 if (NULL == result[1])
16621 task_id = atoi(result[1])+1;
16623 *output_task_id = task_id;
16625 sqlite3_free_table(result);
16628 SNPRINTF(sql_query_string, sizeof(sql_query_string),
16629 "INSERT INTO mail_task_tbl VALUES "
16631 " ? " /* task_id */
16632 " , ? " /* task_type */
16633 " , ? " /* task_status */
16634 " , ? " /* task_priority */
16635 " , ? " /* task_parameter_length */
16636 " , ? " /* task_parameter */
16637 " , ? " /* date_time */
16640 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16641 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16642 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16644 EM_DEBUG_LOG_SEC(">>>> SQL STMT [%s] ", sql_query_string);
16647 _bind_stmt_field_data_int(hStmt, i++, task_id);
16648 _bind_stmt_field_data_int(hStmt, i++, input_task_type);
16649 _bind_stmt_field_data_int(hStmt, i++, EMAIL_TASK_STATUS_WAIT);
16650 _bind_stmt_field_data_int(hStmt, i++, input_task_priority);
16651 _bind_stmt_field_data_int(hStmt, i++, input_task_parameter_length);
16652 _bind_stmt_field_data_blob(hStmt, i++, input_task_parameter, input_task_parameter_length);
16653 _bind_stmt_field_data_int(hStmt, i++, time(NULL));
16655 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16657 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {err = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
16658 ("sqlite3_step fail:%d", rc));
16659 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16660 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
16662 ret = (err == EMAIL_ERROR_NONE);
16665 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, err);
16667 if (hStmt != NULL) {
16668 rc = sqlite3_finalize(hStmt);
16669 if (rc != SQLITE_OK) {
16670 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16671 err = EMAIL_ERROR_DB_FAILURE;
16675 EM_DEBUG_FUNC_END("err [%d]", err);
16679 INTERNAL_FUNC int emstorage_delete_task(char *multi_user_name, int task_id, int transaction)
16681 EM_DEBUG_FUNC_BEGIN("task_id[%d], transaction[%d]", task_id, transaction);
16683 int err = EMAIL_ERROR_NONE;
16684 char sql_query_string[QUERY_SIZE] = {0, };
16686 sqlite3 *local_db_handle = NULL;
16689 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16690 return EMAIL_ERROR_INVALID_PARAM;
16693 local_db_handle = emstorage_get_db_connection(multi_user_name);
16695 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
16697 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_task_tbl WHERE task_id = %d", task_id);
16698 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16699 if (err != EMAIL_ERROR_NONE) {
16700 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16707 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
16709 EM_DEBUG_FUNC_END("err [%d]", err);
16713 INTERNAL_FUNC int emstorage_update_task_status(char *multi_user_name, int task_id, email_task_status_type_t task_status, int transaction)
16715 EM_DEBUG_FUNC_BEGIN("task_id[%d] task_status[%d] transaction[%d]", task_id, task_status, transaction);
16717 int err = EMAIL_ERROR_NONE;
16718 char sql_query_string[QUERY_SIZE] = {0, };
16720 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16721 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
16723 SNPRINTF(sql_query_string, sizeof(sql_query_string),
16724 "UPDATE mail_task_tbl SET"
16725 " task_status = %d"
16726 " WHERE task_id = %d"
16729 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16730 if (err != EMAIL_ERROR_NONE) {
16731 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16738 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
16740 EM_DEBUG_FUNC_END("err [%d]", err);
16744 INTERNAL_FUNC int emstorage_query_task(char *multi_user_name,
16745 const char *input_conditional_clause,
16746 const char *input_ordering_clause,
16747 email_task_t **output_task_list,
16748 int *output_task_count)
16750 EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_ordering_clause [%p], "
16751 "output_task_list[%p], output_task_count[%d]",
16752 input_conditional_clause, input_ordering_clause, output_task_list, output_task_count);
16753 int i = 0, count = 0, rc = -1;
16755 int field_index = 0;
16756 int err = EMAIL_ERROR_NONE;
16757 email_task_t *task_item_from_tbl = NULL;
16758 char sql_query_string[QUERY_SIZE] = {0, };
16759 char *field_list = "task_id, task_type, task_status, task_priority, task_parameter_length, task_parameter ";
16761 sqlite3 *local_db_handle = NULL;
16762 DB_STMT hStmt = NULL;
16764 EM_IF_NULL_RETURN_VALUE(input_conditional_clause, false);
16765 EM_IF_NULL_RETURN_VALUE(output_task_count, false);
16767 local_db_handle = emstorage_get_db_connection(multi_user_name);
16769 SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
16770 "SELECT COUNT(*) FROM mail_task_tbl %s", input_conditional_clause);
16771 EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
16773 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
16774 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16775 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16777 count = atoi(result[1]);
16778 sqlite3_free_table(result);
16780 EM_DEBUG_LOG("count = %d", rc);
16783 EM_DEBUG_EXCEPTION("no task found...");
16784 err = EMAIL_ERROR_TASK_NOT_FOUND;
16788 SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
16789 "SELECT %s FROM mail_task_tbl %s %s", field_list, input_conditional_clause, input_ordering_clause);
16790 EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
16792 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16794 EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
16795 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16796 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16798 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16799 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16800 ("sqlite3_step fail:%d", rc));
16802 if (rc == SQLITE_DONE) {
16803 EM_DEBUG_EXCEPTION("no task found...");
16804 err = EMAIL_ERROR_TASK_NOT_FOUND;
16809 if (!(task_item_from_tbl = (email_task_t*)em_malloc(sizeof(email_task_t) * count))) {
16810 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
16811 err = EMAIL_ERROR_OUT_OF_MEMORY;
16815 for (i = 0; i < count; i++) {
16816 /* get recordset */
16819 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_id), field_index++);
16820 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_type), field_index++);
16821 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_status), field_index++);
16822 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_priority), field_index++);
16823 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_parameter_length), field_index++);
16824 _get_stmt_field_data_blob(hStmt, (void**)&(task_item_from_tbl[i].task_parameter), field_index++);
16826 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16827 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i, rc);
16828 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16829 ("sqlite3_step fail:%d", rc));
16834 if (err == EMAIL_ERROR_NONE) {
16835 if (output_task_list)
16836 *output_task_list = task_item_from_tbl;
16837 *output_task_count = count;
16839 if (task_item_from_tbl) {
16840 for (i = 0; i < count; i++)
16841 EM_SAFE_FREE(task_item_from_tbl[i].task_parameter);
16843 free(task_item_from_tbl);
16847 if (hStmt != NULL) {
16848 rc = sqlite3_finalize(hStmt);
16850 if (rc != SQLITE_OK) {
16851 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16852 err = EMAIL_ERROR_DB_FAILURE;
16856 EM_DEBUG_FUNC_END("err [%d]", err);
16860 INTERNAL_FUNC int emstorage_check_and_update_server_uid_by_message_id(char *multi_user_name, int account_id, email_mailbox_type_e input_mailbox_type, char *message_id, char *server_uid, int *mail_id)
16862 EM_DEBUG_FUNC_BEGIN("account_id:[%d], mailbox_type:[%d], message_id:[%s], server_uid:[%s]", account_id, input_mailbox_type, message_id, server_uid);
16863 int err = EMAIL_ERROR_NONE;
16865 if (message_id == NULL) {
16866 EM_DEBUG_EXCEPTION("Invalid parameter");
16867 err = EMAIL_ERROR_INVALID_PARAM;
16873 int temp_mail_id = 0;
16874 int where_pararaph_length = 0;
16875 char *where_pararaph = NULL;
16876 char sql_query_string[QUERY_SIZE] = {0, };
16877 char **result = NULL;
16878 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16880 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id from mail_tbl ");
16882 where_pararaph_length = EM_SAFE_STRLEN(message_id) + 100;
16883 where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
16884 if (where_pararaph == NULL) {
16885 EM_DEBUG_EXCEPTION("em_mallocfailed");
16886 err = EMAIL_ERROR_OUT_OF_MEMORY;
16890 if (account_id != ALL_ACCOUNT)
16891 sqlite3_snprintf(where_pararaph_length, where_pararaph, "WHERE account_id = %d AND mailbox_type = %d AND message_id like '%q'", account_id, input_mailbox_type, message_id);
16893 sqlite3_snprintf(where_pararaph_length, where_pararaph, "WHERE mailbox_type = %d AND message_id like '%q'", input_mailbox_type, message_id);
16895 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
16896 strcat(sql_query_string, where_pararaph);
16898 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
16900 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16901 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16902 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16905 EM_DEBUG_LOG("Count of mails [%d]", count);
16908 _get_table_field_data_int(result, &temp_mail_id, 1);
16909 EM_DEBUG_LOG("Searched mail_id [%d]", temp_mail_id);
16911 memset(sql_query_string, 0x00, QUERY_SIZE);
16912 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string, "UPDATE mail_tbl set server_mail_id = '%q'", server_uid);
16914 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
16915 strcat(sql_query_string, where_pararaph);
16917 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16918 if (err != EMAIL_ERROR_NONE) {
16919 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16924 err = EMAIL_ERROR_MAIL_NOT_FOUND;
16929 sqlite3_free_table(result);
16933 EM_SAFE_FREE(where_pararaph);
16935 if (mail_id != NULL)
16936 *mail_id = temp_mail_id;
16938 EM_DEBUG_FUNC_END("err : [%d]", err);
16941 /* Tasks --------------------------------------------------------------------------*/
16943 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
16944 INTERNAL_FUNC int emstorage_add_auto_download_activity(char *multi_user_name, email_event_auto_download *local_activity, int *activity_id, int transaction, int *err_code)
16946 EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
16948 if (!local_activity || !activity_id) {
16949 EM_DEBUG_EXCEPTION("local_activity[%p], activity_id[%p]", local_activity, activity_id);
16950 if (err_code != NULL)
16951 *err_code = EMAIL_ERROR_INVALID_PARAM;
16957 int error = EMAIL_ERROR_NONE;
16960 char sql_query_string[QUERY_SIZE] = {0, };
16961 DB_STMT hStmt = NULL;
16963 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16964 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
16966 memset(sql_query_string, 0x00, sizeof(sql_query_string));
16967 SNPRINTF(sql_query_string, sizeof(sql_query_string),
16968 "INSERT INTO mail_auto_download_activity_tbl VALUES "
16970 "? " /* Activity ID */
16972 ",?" /* Account ID */
16973 ",?" /* Local Mail ID */
16974 ",?" /* Server mail ID */
16975 ",?" /* Mailbox ID*/
16976 ",?" /* Multi USER NAME */
16979 char *sql = "SELECT max(rowid) FROM mail_auto_download_activity_tbl;";
16980 char **result = NULL;
16983 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
16985 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16986 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
16988 if (NULL == result[1]) rc = 1;
16989 else rc = atoi(result[1])+1;
16990 sqlite3_free_table(result);
16993 *activity_id = local_activity->activity_id = rc;
16995 EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [%d], MAIL ID [%d], SERVER MAIL ID [%lu]",
16996 local_activity->activity_id, local_activity->mail_id, local_activity->server_mail_id);
16998 if (local_activity->mailbox_id)
16999 EM_DEBUG_LOG(" MAILBOX ID [%d]", local_activity->mailbox_id);
17002 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17003 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17004 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17007 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
17008 _bind_stmt_field_data_int(hStmt, i++, local_activity->status);
17009 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
17010 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
17011 _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
17012 _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
17013 _bind_stmt_field_data_string(hStmt, i++, (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
17016 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17018 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
17019 ("sqlite3_step fail:%d", rc));
17020 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17021 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
17026 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17027 if (hStmt != NULL) {
17028 rc = sqlite3_finalize(hStmt);
17030 if (rc != SQLITE_OK) {
17031 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17032 error = EMAIL_ERROR_DB_FAILURE;
17036 if (err_code != NULL)
17039 EM_DEBUG_FUNC_END("ret [%d]", ret);
17044 INTERNAL_FUNC int emstorage_delete_auto_download_activity(char *multi_user_name, int account_id, int mail_id, int activity_id, int transaction, int *err_code)
17046 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d] , activity_id[%d], transaction[%d], err_code[%p]", account_id, mail_id, activity_id, transaction, err_code);
17048 if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
17049 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], activity_id[%d], transaction[%d], err_code[%p]", account_id, mail_id, activity_id, transaction, err_code);
17051 if (err_code != NULL)
17052 *err_code = EMAIL_ERROR_INVALID_PARAM;
17058 int error = EMAIL_ERROR_NONE;
17059 char sql_query_string[QUERY_SIZE] = {0, };
17060 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17062 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17063 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17065 if (activity_id == 0)
17066 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d AND mail_id = %d", account_id, mail_id);
17068 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d AND activity_id = %d", account_id, activity_id);
17070 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17071 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17072 if (error != EMAIL_ERROR_NONE) {
17073 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17077 /* validate activity existence */
17078 rc = sqlite3_changes(local_db_handle);
17080 EM_DEBUG_EXCEPTION("No matching activity found");
17081 error = EMAIL_ERROR_DATA_NOT_FOUND;
17089 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17091 if (err_code != NULL)
17094 EM_DEBUG_FUNC_END("ret [%d]", ret);
17098 INTERNAL_FUNC int emstorage_delete_all_auto_download_activity(char *multi_user_name, int account_id, int transaction, int *err_code)
17100 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17102 if (account_id < FIRST_ACCOUNT_ID) {
17103 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17104 if (err_code != NULL)
17105 *err_code = EMAIL_ERROR_INVALID_PARAM;
17111 int error = EMAIL_ERROR_NONE;
17112 char sql_query_string[QUERY_SIZE] = {0, };
17114 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17116 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17117 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17118 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d", account_id);
17120 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17121 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17122 if (error != EMAIL_ERROR_NONE) {
17123 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17127 rc = sqlite3_changes(local_db_handle);
17129 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17130 error = EMAIL_ERROR_DATA_NOT_FOUND;
17138 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17140 if (err_code != NULL)
17143 EM_DEBUG_FUNC_END("ret [%d]", ret);
17148 INTERNAL_FUNC int emstorage_delete_auto_download_activity_by_mailbox(char *multi_user_name, int account_id, int mailbox_id, int transaction, int *err_code)
17150 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17152 if (account_id < FIRST_ACCOUNT_ID || mailbox_id < 0) {
17153 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17154 if (err_code != NULL)
17155 *err_code = EMAIL_ERROR_INVALID_PARAM;
17161 int error = EMAIL_ERROR_NONE;
17162 char sql_query_string[QUERY_SIZE] = {0, };
17164 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17166 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17167 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17168 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d AND mailbox_id = %d", account_id, mailbox_id);
17170 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17171 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17172 if (error != EMAIL_ERROR_NONE) {
17173 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17177 rc = sqlite3_changes(local_db_handle);
17179 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17180 error = EMAIL_ERROR_DATA_NOT_FOUND;
17188 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17190 if (err_code != NULL)
17193 EM_DEBUG_FUNC_END("ret [%d]", ret);
17198 INTERNAL_FUNC int emstorage_get_auto_download_activity(char *multi_user_name, int account_id, int input_mailbox_id, email_event_auto_download **event_start, int *count, int transaction, int *err_code)
17200 EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
17202 if (account_id < FIRST_ACCOUNT_ID || !event_start || input_mailbox_id <= 0 || !count) {
17203 EM_DEBUG_EXCEPTION("account_id[%d], event_start[%p], input_mailbox_id[%d], count[%p], err_code[%p]", account_id, event_start, input_mailbox_id, count, err_code);
17205 if (err_code != NULL)
17206 *err_code = EMAIL_ERROR_INVALID_PARAM;
17213 int error = EMAIL_ERROR_NONE;
17215 DB_STMT hStmt = NULL;
17216 email_event_auto_download *event_list = NULL;
17217 char sql_query_string[QUERY_SIZE] = {0, };
17219 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17221 EMSTORAGE_START_READ_TRANSACTION(transaction);
17223 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17224 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl WHERE account_id = %d AND mailbox_id = '%d' order by activity_id", account_id, input_mailbox_id);
17227 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17228 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17229 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17231 *count = atoi(result[1]);
17232 sqlite3_free_table(result);
17234 EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
17237 EM_DEBUG_LOG("No matched activity found in mail_auto_download_activity_tbl");
17238 error = EMAIL_ERROR_MAIL_NOT_FOUND;
17242 EM_DEBUG_LOG("Activity Count = %d", *count);
17244 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17245 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_auto_download_activity_tbl WHERE account_id = %d AND mailbox_id = '%d' order by activity_id", account_id, input_mailbox_id);
17247 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17250 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17252 EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
17253 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17254 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17257 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17258 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17259 ("sqlite3_step fail:%d", rc));
17261 if (!(event_list = (email_event_auto_download *)em_malloc(sizeof(email_event_auto_download)*(*count)))) {
17262 EM_DEBUG_EXCEPTION("Malloc failed");
17264 error = EMAIL_ERROR_OUT_OF_MEMORY;
17268 for (i = 0; i < (*count); i++) {
17269 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17270 _get_stmt_field_data_int(hStmt, &(event_list[i].status), STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17271 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17272 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17273 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17274 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17276 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17277 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17278 ("sqlite3_step fail:%d", rc));
17286 *event_start = event_list;
17288 EM_SAFE_FREE(event_list);
17289 *event_start = NULL;
17293 if (hStmt != NULL) {
17294 rc = sqlite3_finalize(hStmt);
17296 if (rc != SQLITE_OK) {
17297 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17298 error = EMAIL_ERROR_DB_FAILURE;
17302 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17303 if (err_code != NULL)
17306 EM_DEBUG_FUNC_END("ret [%d]", ret);
17311 INTERNAL_FUNC int emstorage_get_auto_download_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
17313 EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
17315 if (!activity_count || !err_code) {
17316 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
17317 if (err_code != NULL)
17318 *err_code = EMAIL_ERROR_INVALID_PARAM;
17324 int error = EMAIL_ERROR_NONE;
17325 DB_STMT hStmt = NULL;
17326 char sql_query_string[QUERY_SIZE] = {0, };
17328 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17330 EMSTORAGE_START_READ_TRANSACTION(transaction);
17331 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17333 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl;");
17335 EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
17338 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17339 EM_DEBUG_LOG_DEV("before sqlite3_prepare hStmt = %p", hStmt);
17340 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17341 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17344 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17345 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17346 ("sqlite3_step fail:%d", rc));
17348 _get_stmt_field_data_int(hStmt, activity_count, 0);
17350 EM_DEBUG_LOG("counts of activities in activity table [%d]", *activity_count);
17356 if (hStmt != NULL) {
17357 rc = sqlite3_finalize(hStmt);
17359 if (rc != SQLITE_OK) {
17360 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17361 error = EMAIL_ERROR_DB_FAILURE;
17365 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17366 if (err_code != NULL)
17369 EM_DEBUG_FUNC_END("ret [%d]", ret);
17374 INTERNAL_FUNC int emstorage_get_auto_download_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
17376 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
17378 if (!account_list || !count) {
17379 EM_DEBUG_EXCEPTION("account_list[%p], count[%p]", account_list, count);
17380 if (err_code != NULL)
17381 *err_code = EMAIL_ERROR_INVALID_PARAM;
17386 int error = EMAIL_ERROR_NONE;
17387 char *sql = "SELECT count(distinct account_id) FROM mail_auto_download_activity_tbl";
17389 int i = 0, rc = -1;
17390 int *result_account_list = NULL;
17391 DB_STMT hStmt = NULL;
17392 char sql_query_string[QUERY_SIZE] = {0, };
17393 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17395 EMSTORAGE_START_READ_TRANSACTION(transaction);
17397 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
17398 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17399 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
17401 *count = atoi(result[1]);
17402 sqlite3_free_table(result);
17405 EM_DEBUG_EXCEPTION("no account found...");
17406 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
17411 EM_DEBUG_LOG("Account count [%d]", *count);
17413 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17415 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_auto_download_activity_tbl");
17417 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17420 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17422 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
17423 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17424 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17427 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17428 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17429 ("sqlite3_step fail:%d", rc));
17431 if (NULL == (result_account_list = (int *)em_malloc(sizeof(int)*(*count)))) {
17432 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
17433 error = EMAIL_ERROR_OUT_OF_MEMORY;
17437 for (i = 0; i < (*count); i++) {
17438 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
17440 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17441 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17442 ("sqlite3_step fail:%d", rc));
17443 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
17451 *account_list = result_account_list;
17453 EM_SAFE_FREE(result_account_list);
17455 if (hStmt != NULL) {
17456 rc = sqlite3_finalize(hStmt);
17458 if (rc != SQLITE_OK) {
17459 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17460 error = EMAIL_ERROR_DB_FAILURE;
17464 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17465 if (err_code != NULL)
17467 EM_DEBUG_FUNC_END("ret [%d]", ret);
17472 INTERNAL_FUNC int emstorage_get_auto_download_mailbox_list(char *multi_user_name, int account_id, int **mailbox_list, int *count, int transaction, int *err_code)
17474 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
17476 if (account_id < FIRST_ACCOUNT_ID || !mailbox_list || !count) {
17477 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p]", account_id, mailbox_list, count);
17478 if (err_code != NULL)
17479 *err_code = EMAIL_ERROR_INVALID_PARAM;
17484 int error = EMAIL_ERROR_NONE;
17486 int i = 0, rc = -1;
17487 int *mbox_list = NULL;
17488 DB_STMT hStmt = NULL;
17489 char sql_query_string[QUERY_SIZE] = {0, };
17491 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17493 EMSTORAGE_START_READ_TRANSACTION(transaction);
17495 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17496 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(distinct mailbox_id) FROM mail_auto_download_activity_tbl WHERE account_id = %d order by mailbox_id", account_id);
17499 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17500 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17501 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17503 *count = atoi(result[1]);
17504 sqlite3_free_table(result);
17507 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
17508 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
17512 EM_DEBUG_LOG("Mailbox count = %d", *count);
17514 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17516 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct mailbox_id FROM mail_auto_download_activity_tbl WHERE account_id = %d order by mailbox_id", account_id);
17518 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
17521 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17524 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17525 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17528 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17529 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17530 ("sqlite3_step fail:%d", rc));
17532 mbox_list = (int *)em_malloc(sizeof(int)*(*count)); /* prevent */
17533 if (mbox_list == NULL) {
17534 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
17535 error = EMAIL_ERROR_OUT_OF_MEMORY;
17539 for (i = 0; i < (*count); i++) {
17540 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
17541 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17543 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17544 ("sqlite3_step fail:%d", rc));
17545 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
17553 *mailbox_list = mbox_list;
17555 EM_SAFE_FREE(mbox_list);
17557 if (hStmt != NULL) {
17558 rc = sqlite3_finalize(hStmt);
17560 if (rc != SQLITE_OK) {
17561 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17562 error = EMAIL_ERROR_DB_FAILURE;
17566 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17567 if (err_code != NULL)
17569 EM_DEBUG_FUNC_END("ret [%d]", ret);
17574 INTERNAL_FUNC int emstorage_get_auto_download_activity_count_by_mailbox(char *multi_user_name, int account_id, int input_mailbox_id, int *activity_count, int transaction, int *err_code)
17576 EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
17578 if (account_id < FIRST_ACCOUNT_ID || !activity_count || !err_code) {
17579 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
17580 if (err_code != NULL)
17581 *err_code = EMAIL_ERROR_INVALID_PARAM;
17587 int error = EMAIL_ERROR_NONE;
17588 char sql_query_string[QUERY_SIZE] = {0, };
17589 DB_STMT hStmt = NULL;
17591 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17593 EMSTORAGE_START_READ_TRANSACTION(transaction);
17594 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17595 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl WHERE account_id = %d and mailbox_id = '%d'", account_id, input_mailbox_id);
17597 EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
17599 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17600 EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
17601 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17602 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17604 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17605 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17606 ("sqlite3_step fail:%d", rc));
17608 _get_stmt_field_data_int(hStmt, activity_count, 0);
17610 EM_DEBUG_LOG("count of activities in activity table [%d]", *activity_count);
17616 if (hStmt != NULL) {
17617 rc = sqlite3_finalize(hStmt);
17619 if (rc != SQLITE_OK) {
17620 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17621 error = EMAIL_ERROR_DB_FAILURE;
17625 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17627 if (err_code != NULL)
17630 EM_DEBUG_FUNC_END("ret [%d]", ret);
17635 INTERNAL_FUNC int emstorage_update_auto_download_activity(char *multi_user_name, char *old_server_uid, char *new_server_uid, char *mailbox_name, int mailbox_id, int *err_code)
17637 EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mailbox_id[%d]", old_server_uid, new_server_uid, mailbox_id);
17639 int rc = -1, ret = false;
17640 int error = EMAIL_ERROR_NONE;
17641 char sql_query_string[QUERY_SIZE] = {0, };
17642 int transaction = true;
17644 if (!old_server_uid || !new_server_uid || (!mailbox_name && mailbox_id < 0)) {
17645 EM_DEBUG_EXCEPTION("Invalid parameters");
17646 error = EMAIL_ERROR_INVALID_PARAM;
17650 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17652 EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17653 memset(sql_query_string, 0x00, sizeof(sql_query_string));
17655 if (mailbox_id > 0)
17656 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17657 "UPDATE mail_auto_download_activity_tbl SET server_mail_id = %s , mailbox_id ='%d' WHERE server_mail_id = %s ", new_server_uid, mailbox_id, old_server_uid);
17658 else if (mailbox_name)
17659 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17660 "UPDATE mail_auto_download_activity_tbl SET server_mail_id = %s WHERE server_mail_id = %s ", new_server_uid, old_server_uid);
17662 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17663 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17664 if (error != EMAIL_ERROR_NONE) {
17665 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17669 rc = sqlite3_changes(local_db_handle);
17671 EM_DEBUG_LOG("No matching found in mail_auto_download_activity_tbl");
17678 EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17680 if (err_code != NULL)
17683 EM_DEBUG_FUNC_END("ret [%d]", ret);
17689 #ifdef __FEATURE_UPDATE_DB_TABLE_SCHEMA__
17694 } email_column_info_t;
17696 static int get_column_information_from_table_callback(void *arg1, int argc, char **argv, char **input_column_name)
17698 EM_DEBUG_FUNC_BEGIN("arg1[%p] argc[%d] argv[%p] column_name[%p]", arg1, argc, argv, input_column_name);
17702 char *column_name = NULL;
17703 char *column_type = NULL;
17704 GList *new_list = *((GList**)arg1);
17705 email_column_info_t *column_info_item = NULL;
17707 for (i = 0; i < argc; ++i) {
17708 /* EM_DEBUG_LOG("%s = %s", input_column_name[i], argv[i]); */
17709 if (EM_SAFE_STRCMP(input_column_name[i], "name") == 0) {
17711 EM_SAFE_FREE(column_name);
17713 column_name = EM_SAFE_STRDUP(argv[i]);
17715 } else if (EM_SAFE_STRCMP(input_column_name[i], "type") == 0) {
17717 EM_SAFE_FREE(column_type);
17719 column_type = EM_SAFE_STRDUP(argv[i]);
17724 EM_DEBUG_LOG("column_name[%s] column_type[%s]", column_name, column_type);
17725 column_info_item = em_malloc(sizeof(email_column_info_t));
17726 if (column_info_item == NULL) {
17727 EM_DEBUG_EXCEPTION("em_mallocfailed");
17731 column_info_item->column_name = EM_SAFE_STRDUP(column_name);
17732 column_info_item->column_type = EM_SAFE_STRDUP(column_type);
17733 new_list = g_list_append(new_list, (gpointer)column_info_item);
17734 *((GList**)arg1) = new_list;
17739 EM_SAFE_FREE(column_name);
17740 EM_SAFE_FREE(column_type);
17742 EM_DEBUG_FUNC_END();
17746 static int emstorage_get_column_information_from_table(char *multi_user_name, const char *input_table_name, GList **output_column_info)
17748 EM_DEBUG_FUNC_BEGIN("input_table_name[%p] output_column_info[%p]", input_table_name, output_column_info);
17749 int err = EMAIL_ERROR_NONE;
17750 int result_from_sqlite = 0;
17751 char *error_message_from_sqlite = NULL;
17752 char sql_query_string[QUERY_SIZE] = {0, };
17753 GList *new_list = NULL;
17754 sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17756 SNPRINTF(sql_query_string, QUERY_SIZE, "pragma table_info(%s);", input_table_name);
17758 result_from_sqlite = sqlite3_exec(local_db_handle, sql_query_string, get_column_information_from_table_callback, &new_list, &error_message_from_sqlite);
17760 if (result_from_sqlite != SQLITE_OK)
17761 EM_DEBUG_EXCEPTION("sqlite3_exec returns [%d]", result_from_sqlite);
17763 EM_DEBUG_LOG("new_list[%p] output_column_info[%p]", new_list, output_column_info);
17765 if (new_list && output_column_info) {
17766 EM_DEBUG_LOG("g_list_length[%d]", g_list_length(new_list));
17767 *output_column_info = new_list;
17770 EM_DEBUG_FUNC_END("err [%d]", err);
17774 static int emstorage_create_renamed_table(char *multi_user_name, char **input_full_query, int input_query_index, char *input_source_table_name, char *input_new_table_name)
17776 EM_DEBUG_FUNC_BEGIN("input_full_query [%p] input_query_index[%d] input_source_table_name[%p] input_new_table_name[%p]", input_full_query, input_query_index, input_source_table_name, input_new_table_name);
17777 int error = EMAIL_ERROR_NONE;
17779 sqlite3 *local_db_handle = NULL;
17780 char sql_query_string[QUERY_SIZE] = {0, };
17782 if (input_full_query == NULL || input_source_table_name == NULL || input_new_table_name == NULL) {
17783 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17784 error = EMAIL_ERROR_INVALID_PARAM;
17788 local_db_handle = emstorage_get_db_connection(multi_user_name);
17790 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17791 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17793 EM_DEBUG_LOG("[%s] will be replaced by [%s]", input_source_table_name, input_new_table_name);
17795 EM_SAFE_STRNCPY(sql_query_string, input_full_query[input_query_index], sizeof(sql_query_string)-1); /*prevent 21984*/
17796 reg_replace(sql_query_string, input_source_table_name, input_new_table_name);
17798 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17801 if (error == EMAIL_ERROR_NONE) {
17802 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17804 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17807 EM_DEBUG_FUNC_END("error [%d]", error);
17811 static int emstorage_add_column(char *multi_user_name, char *input_table_name, email_column_info_t *input_new_column)
17813 EM_DEBUG_FUNC_BEGIN("input_table_name[%p] input_new_column[%p]", input_table_name, input_new_column);
17814 int error = EMAIL_ERROR_NONE;
17816 sqlite3 *local_db_handle = NULL;
17817 char sql_query_string[QUERY_SIZE] = {0, };
17819 if (input_table_name == NULL || input_new_column == NULL) {
17820 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17821 error = EMAIL_ERROR_INVALID_PARAM;
17825 local_db_handle = emstorage_get_db_connection(multi_user_name);
17827 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17828 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17829 SNPRINTF(sql_query_string, QUERY_SIZE, "ALTER TABLE %s ADD COLUMN %s %s;", input_table_name, input_new_column->column_name, input_new_column->column_type);
17830 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17833 if (error == EMAIL_ERROR_NONE) {
17834 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17836 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17839 EM_DEBUG_FUNC_END("error [%d]", error);
17843 static int emstorage_drop_table(char *multi_user_name, char *input_table_name)
17845 EM_DEBUG_FUNC_BEGIN("input_table_name[%p]", input_table_name);
17846 int error = EMAIL_ERROR_NONE;
17848 sqlite3 *local_db_handle = NULL;
17849 char sql_query_string[QUERY_SIZE] = {0, };
17851 if (input_table_name == NULL) {
17852 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17853 error = EMAIL_ERROR_INVALID_PARAM;
17857 local_db_handle = emstorage_get_db_connection(multi_user_name);
17859 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17860 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17861 SNPRINTF(sql_query_string, QUERY_SIZE, "DROP TABLE %s;", input_table_name);
17862 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17865 if (error == EMAIL_ERROR_NONE) {
17866 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17868 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17871 EM_DEBUG_FUNC_END("error [%d]", error);
17875 gint glist_compare_column_name(gconstpointer old_column_info, gconstpointer new_column_info)
17877 EM_DEBUG_FUNC_BEGIN("old_column_info[%p] new_column_info[%p]", old_column_info, new_column_info);
17878 email_column_info_t *left_one = (email_column_info_t*)old_column_info;
17879 email_column_info_t *right_one = (email_column_info_t*)new_column_info;
17881 if (old_column_info == NULL || new_column_info == NULL)
17884 return EM_SAFE_STRCMP((char*)left_one->column_name, (char*)right_one->column_name);
17887 INTERNAL_FUNC int emstorage_update_db_table_schema(char *multi_user_name)
17889 EM_DEBUG_FUNC_BEGIN();
17892 int error = EMAIL_ERROR_NONE;
17894 email_column_info_t *new_column_info = NULL;
17895 email_column_info_t *p_column_info = NULL;
17896 char **create_table_query = NULL;
17897 GList *found_data = NULL;
17898 GList *column_list_of_old_table = NULL;
17899 GList *column_list_of_new_table = NULL;
17900 char table_names[CREATE_TABLE_MAX][2][50] = { { "mail_account_tbl", "mail_account_tbl_new" },
17901 { "mail_box_tbl", "mail_box_tbl_new" },
17902 { "mail_read_mail_uid_tbl", "mail_read_mail_uid_tbl_new" },
17903 { "mail_rule_tbl", "mail_rule_tbl_new" },
17904 { "mail_tbl", "mail_tbl_new" },
17905 { "mail_attachment_tbl", "mail_attachment_tbl_new" },
17906 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
17907 { "mail_partial_body_activity_tbl", "mail_partial_body_activity_tbl_new" },
17911 { "mail_meeting_tbl", "mail_meeting_tbl_new" },
17912 #ifdef __FEATURE_LOCAL_ACTIVITY__
17913 { "mail_local_activity_tbl", "mail_local_activity_tbl_new" },
17917 { "mail_certificate_tbl", "mail_certificate_tbl_new" },
17918 { "mail_task_tbl", "mail_task_tbl_new" },
17919 #ifdef __FEATURE_BODY_SEARCH__
17920 { "mail_text_tbl", "mail_text_tbl_new" },
17925 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
17926 { "mail_auto_download_activity_tbl", "mail_auto_download_activity_tbl_new" }
17933 error = emcore_load_query_from_file(EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
17935 if (error != EMAIL_ERROR_NONE) {
17936 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
17940 if (query_len < CREATE_TABLE_MAX) {
17941 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
17942 error = EMAIL_ERROR_SYSTEM_FAILURE;
17946 for (i = CREATE_TABLE_MAIL_ACCOUNT_TBL; i < CREATE_TABLE_MAX; i++) {
17947 EM_DEBUG_LOG("table [%s] new_table [%s]", table_names[i][0], table_names[i][1]);
17948 if (EM_SAFE_STRLEN(table_names[i][0]) && EM_SAFE_STRLEN(table_names[i][1])) {
17949 /* Check existing of _new table */
17950 emstorage_drop_table(multi_user_name, table_names[i][1]);
17951 error = emstorage_create_renamed_table(multi_user_name, create_table_query, i, table_names[i][0], table_names[i][1]);
17952 if (error != EMAIL_ERROR_NONE) {
17953 EM_DEBUG_EXCEPTION("emstorage_create_renamed_table failed [%d]", error);
17957 emstorage_get_column_information_from_table(multi_user_name, table_names[i][0], &column_list_of_old_table);
17958 emstorage_get_column_information_from_table(multi_user_name, table_names[i][1], &column_list_of_new_table);
17960 /* Compare fields and add new field */
17961 for (j = 0; j < g_list_length(column_list_of_new_table); j++) {
17962 new_column_info = (email_column_info_t*)g_list_nth_data(column_list_of_new_table, j);
17963 found_data = g_list_find_custom(column_list_of_old_table, (gconstpointer)new_column_info, glist_compare_column_name);
17964 if (found_data == NULL) {
17966 emstorage_add_column(multi_user_name, table_names[i][0], new_column_info);
17970 emstorage_drop_table(multi_user_name, table_names[i][1]);
17972 EM_DEBUG_LOG("Skipped");
17976 if (create_table_query) {
17978 for (i = 0; i < query_len; i++) {
17979 if (create_table_query[i]) {
17980 EM_SAFE_FREE(create_table_query[i]);
17983 EM_SAFE_FREE(create_table_query);
17986 found_data = g_list_first(column_list_of_old_table);
17987 while (found_data != NULL) {
17988 p_column_info = (email_column_info_t *)found_data->data;
17989 EM_SAFE_FREE(p_column_info->column_name);
17990 EM_SAFE_FREE(p_column_info->column_type);
17991 EM_SAFE_FREE(p_column_info);
17993 found_data = g_list_next(found_data);
17995 g_list_free(column_list_of_old_table);
17997 found_data = g_list_first(column_list_of_new_table);
17998 while (found_data != NULL) {
17999 p_column_info = (email_column_info_t *)found_data->data;
18000 EM_SAFE_FREE(p_column_info->column_name);
18001 EM_SAFE_FREE(p_column_info->column_type);
18002 EM_SAFE_FREE(p_column_info);
18004 found_data = g_list_next(found_data);
18006 g_list_free(column_list_of_new_table);
18008 EM_DEBUG_FUNC_END("error [%d]", error);
18011 #endif /* __FEATURE_UPDATE_DB_TABLE_SCHEMA__ */