4 * Copyright (c) 2000 - 2011 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>
48 #include <ss_manager.h>
50 #include <dbus/dbus.h>
57 #include "email-internal-types.h"
58 #include "email-convert.h"
59 #include "email-core-utils.h"
60 #include "email-utilities.h"
61 #include "email-storage.h"
62 #include "email-debug-log.h"
63 #include "email-types.h"
64 #include "email-convert.h"
66 #define DB_STMT sqlite3_stmt *
68 #define SETTING_MEMORY_TEMP_FILE_PATH "/tmp/email_tmp_file"
70 #define EMAIL_STORAGE_CHANGE_NOTI "User.Email.StorageChange"
71 #define EMAIL_NETOWRK_CHANGE_NOTI "User.Email.NetworkStatus"
72 #define EMAIL_RESPONSE_TO_API_NOTI "User.Email.ResponseToAPI"
74 #define CONTENT_DATA "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset="
75 #define CONTENT_TYPE_DATA "<meta http-equiv=\"Content-Type\" content=\"text/html; charset="
77 #define FLAG_SEEN 0x01
78 #define FLAG_DELETED 0x02
79 #define FLAG_FLAGGED 0x04
80 #define FLAG_ANSWERED 0x08
82 #define FLAG_DRAFT 0x20
86 #define ISSUE_ORGANIZATION_LEN_IN_MAIL_CERTIFICATE_TBL 256
87 #define EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL 128
88 #define SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL 1027
89 #define FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL 256
91 #define ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL 50
92 #define RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 50
93 #define EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 128
94 #define USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL 50
95 #define PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL 50
96 #define SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 50
97 #define SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL 50
98 #define SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL 50
99 #define DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL 30
100 #define REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 128
101 #define RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL 128
102 #define LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL 256
103 #define DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL 256
104 #define SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL 256
105 #define MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL 128
106 #define ALIAS_LEN_IN_MAIL_BOX_TBL 128
107 #define LOCAL_MBOX_LEN_IN_MAIL_READ_MAIL_UID_TBL 128
108 #define MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL 128
109 #define S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL 128
110 #define DATA2_LEN_IN_MAIL_READ_MAIL_UID_TBL 256
111 #define VALUE_LEN_IN_MAIL_RULE_TBL 256
112 #define DEST_MAILBOX_LEN_IN_MAIL_RULE_TBL 128
113 #define MAILBOX_NAME_LEN_IN_MAIL_ATTACHMENT_TBL 128
114 #define ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL 256
115 #define ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL 256
116 #define CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL 256
117 #define ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL 128
118 #define MAILBOX_LEN_IN_MAIL_TBL 128
119 #define SERVER_MAILBOX_LEN_IN_MAIL_TBL 128
120 #define SERVER_MAIL_ID_LEN_IN_MAIL_TBL 128
121 #define FROM_LEN_IN_MAIL_TBL 256
122 #define SENDER_LEN_IN_MAIL_TBL 256
123 #define REPLY_TO_LEN_IN_MAIL_TBL 256
124 #define TO_LEN_IN_MAIL_TBL 3999
125 #define CC_LEN_IN_MAIL_TBL 3999
126 #define BCC_LEN_IN_MAIL_TBL 3999
127 #define RETURN_PATH_LEN_IN_MAIL_TBL 3999
128 #define SUBJECT_LEN_IN_MAIL_TBL 1027
129 #define THREAD_TOPIC_LEN_IN_MAIL_TBL 256
130 #define TEXT_1_LEN_IN_MAIL_TBL 256
131 #define TEXT_2_LEN_IN_MAIL_TBL 256
132 #define MIME_ENTITY_LEN_IN_MAIL_TBL 256
133 #define DATETIME_LEN_IN_MAIL_TBL 128
134 #define MESSAGE_ID_LEN_IN_MAIL_TBL 256
135 #define FROM_CONTACT_NAME_LEN_IN_MAIL_TBL 3999
136 #define FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL 3999
137 #define TO_CONTACT_NAME_LEN_IN_MAIL_TBL 3999
138 #define TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL 3999
139 #define MAILBOX_LEN_IN_MAIL_MEETING_TBL 128
140 #define LOCATION_LEN_IN_MAIL_MEETING_TBL 1024
141 #define GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL 512
142 #define STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL 32
143 #define DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL 32
144 #define PREVIEWBODY_LEN_IN_MAIL_TBL 512
145 #define CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL 256
147 /* this define is used for query to change data (delete, insert, update) */
148 #define EMSTORAGE_START_WRITE_TRANSACTION(transaction_flag, error_code) \
149 if (transaction_flag)\
151 _timedlock_shm_mutex(&mapped_for_db_lock, 2);\
152 if (emstorage_begin_transaction(NULL, NULL, &error_code) == false) \
154 EM_DEBUG_EXCEPTION("emstorage_begin_transaction() error[%d]", error_code);\
159 /* this define is used for query to change data (delete, insert, update) */
160 #define EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction_flag, result_code, error_code) \
161 if (transaction_flag)\
163 if (result_code == true)\
165 if (emstorage_commit_transaction(NULL, NULL, NULL) == false)\
167 error_code = EMAIL_ERROR_DB_FAILURE;\
168 result_code = false;\
173 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)\
174 error_code = EMAIL_ERROR_DB_FAILURE;\
176 _unlockshm_mutex(&mapped_for_db_lock);\
179 /* this define is used for query to read (select) */
180 #define EMSTORAGE_START_READ_TRANSACTION(transaction_flag) \
181 if (transaction_flag)\
183 /*_timedlock_shm_mutex(&mapped_for_db_lock, 2);*/\
186 /* this define is used for query to read (select) */
187 #define EMSTORAGE_FINISH_READ_TRANSACTION(transaction_flag) \
188 if (transaction_flag)\
190 /*_unlockshm_mutex(&mapped_for_db_lock);*/\
193 /* for safety DB operation */
194 static char g_db_path[248] = {0x00, };
196 static pthread_mutex_t _transactionBeginLock = PTHREAD_MUTEX_INITIALIZER;
197 static pthread_mutex_t _transactionEndLock = PTHREAD_MUTEX_INITIALIZER;
198 static pthread_mutex_t _dbus_noti_lock = PTHREAD_MUTEX_INITIALIZER;
199 static pthread_mutex_t _db_handle_lock = PTHREAD_MUTEX_INITIALIZER;
201 static int _get_attribute_type_by_mail_field_name(char *input_mail_field_name, email_mail_attribute_type *output_mail_attribute_type);
203 #define _MULTIPLE_DB_HANDLE
205 #ifdef _MULTIPLE_DB_HANDLE
207 #define _DISCONNECT_DB /* db_util_close(_db_handle); */
215 #define MAX_DB_CLIENT 100
217 /* static int _db_handle_count = 0; */
218 db_handle_t _db_handle_list[MAX_DB_CLIENT] = {{0, 0}, };
220 sqlite3 *emstorage_get_db_handle()
222 EM_DEBUG_FUNC_BEGIN();
224 pthread_t current_thread_id = THREAD_SELF();
225 sqlite3 *result_db_handle = NULL;
227 ENTER_CRITICAL_SECTION(_db_handle_lock);
228 for (i = 0; i < MAX_DB_CLIENT; i++) {
229 if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id)) {
230 EM_DEBUG_LOG("found db handle at [%d]", i);
231 result_db_handle = _db_handle_list[i].db_handle;
235 LEAVE_CRITICAL_SECTION(_db_handle_lock);
237 if (!result_db_handle)
238 EM_DEBUG_EXCEPTION("Can't find proper handle for [%d]", current_thread_id);
241 return result_db_handle;
244 int emstorage_set_db_handle(sqlite3 *db_handle)
246 EM_DEBUG_FUNC_BEGIN();
247 int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
248 pthread_t current_thread_id = THREAD_SELF();
250 ENTER_CRITICAL_SECTION(_db_handle_lock);
251 for (i = 0; i < MAX_DB_CLIENT; i++) {
252 if (_db_handle_list[i].thread_id == 0) {
253 _db_handle_list[i].thread_id = current_thread_id;
254 _db_handle_list[i].db_handle = db_handle;
255 EM_DEBUG_LOG("current_thread_id [%d], index [%d]", current_thread_id, i);
256 error_code = EMAIL_ERROR_NONE;
260 LEAVE_CRITICAL_SECTION(_db_handle_lock);
262 if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
263 EM_DEBUG_EXCEPTION("Exceeded the limitation of db client. Can't find empty slot in _db_handle_list.");
265 EM_DEBUG_FUNC_END("error_code [%d]", error_code);
269 int emstorage_remove_db_handle()
271 EM_DEBUG_FUNC_BEGIN();
272 int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
273 ENTER_CRITICAL_SECTION(_db_handle_lock);
274 for (i = 0; i < MAX_DB_CLIENT; i++)
276 if (_db_handle_list[i].thread_id == THREAD_SELF())
278 _db_handle_list[i].thread_id = 0;
279 _db_handle_list[i].db_handle = NULL;
280 EM_DEBUG_LOG("index [%d]", i);
281 error_code = EMAIL_ERROR_NONE;
285 LEAVE_CRITICAL_SECTION(_db_handle_lock);
287 if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
288 EM_DEBUG_EXCEPTION("Can't find proper thread_id");
290 EM_DEBUG_FUNC_END("error_code [%d]", error_code);
295 int emstorage_reset_db_handle_list()
297 EM_DEBUG_FUNC_BEGIN();
300 ENTER_CRITICAL_SECTION(_db_handle_lock);
301 for (i = 0; i < MAX_DB_CLIENT; i++)
303 _db_handle_list[i].thread_id = 0;
304 _db_handle_list[i].db_handle = NULL;
306 LEAVE_CRITICAL_SECTION(_db_handle_lock)
309 return EMAIL_ERROR_NONE;
312 sqlite3 *emstorage_get_db_connection()
314 sqlite3 *tmp_db_handle = emstorage_get_db_handle();
315 if (NULL == tmp_db_handle)
316 tmp_db_handle = emstorage_db_open(NULL);
317 return tmp_db_handle;
321 #else /* _MULTIPLE_DB_HANDLE */
322 #define _DISCONNECT_DB /* db_util_close(_db_handle); */
324 sqlite3 *_db_handle = NULL;
326 sqlite3 *emstorage_get_db_connection()
328 if (NULL == _db_handle)
329 emstorage_db_open(NULL);
332 #endif /* _MULTIPLE_DB_HANDLE */
334 /* ------------------------------------------------------------------------------ */
335 /* Mutex using shared memory */
338 pthread_mutex_t mutex;
342 mmapped_t *mapped_for_db_lock = NULL;
343 int shm_fd_for_db_lock = 0;
345 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
346 mmapped_t *mapped_for_generating_mail_id = NULL;
347 int shm_fd_for_generating_mail_id = 0;
348 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
350 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__
351 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
352 { _timedlock_shm_mutex(&mapped_for_db_lock, 2); return_value = function_call; _unlockshm_mutex(&mapped_for_db_lock); }
353 #else /* __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
354 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
355 { return_value = function_call; }
356 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
358 INTERNAL_FUNC int emstorage_shm_file_init(const char *shm_file_name)
360 EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
363 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
364 return EMAIL_ERROR_INVALID_PARAM;
367 int fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); /* note: permission is not working */
371 EM_DEBUG_LOG("** Create SHM FILE **");
372 if (ftruncate(fd, sizeof(mmapped_t)) != 0) {
373 EM_DEBUG_EXCEPTION("ftruncate failed: %s", strerror(errno));
374 return EMAIL_ERROR_SYSTEM_FAILURE;
377 mmapped_t *m = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
378 if (m == MAP_FAILED) {
379 EM_DEBUG_EXCEPTION("mmap failed: %s", strerror(errno));
380 return EMAIL_ERROR_SYSTEM_FAILURE;
385 pthread_mutexattr_t mattr;
386 pthread_mutexattr_init(&mattr);
387 pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
388 pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
389 pthread_mutex_init(&(m->mutex), &mattr);
390 pthread_mutexattr_destroy(&mattr);
393 EM_DEBUG_EXCEPTION("shm_open failed: %s", strerror(errno));
394 return EMAIL_ERROR_SYSTEM_FAILURE;
398 return EMAIL_ERROR_NONE;
401 int emstorage_shm_file_destroy(const char *shm_file_name)
403 EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
405 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
406 return EMAIL_ERROR_INVALID_PARAM;
409 if (shm_unlink(shm_file_name) != 0)
410 EM_DEBUG_EXCEPTION("shm_unlink failed: %s", strerror(errno));
412 return EMAIL_ERROR_NONE;
415 static int _initialize_shm_mutex(const char *shm_file_name, int *param_shm_fd, mmapped_t **param_mapped)
417 EM_DEBUG_FUNC_BEGIN("shm_file_name [%p] param_shm_fd [%p], param_mapped [%p]", shm_file_name, param_shm_fd, param_mapped);
419 if(!shm_file_name || !param_shm_fd || !param_mapped) {
420 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
421 return EMAIL_ERROR_INVALID_PARAM;
424 if (!(*param_mapped)) {
425 EM_DEBUG_LOG("** mapping begin **");
427 if (!(*param_shm_fd)) { /* open shm_file_name at first. Otherwise, the num of files in /proc/pid/fd will be increasing */
428 *param_shm_fd = shm_open(shm_file_name, O_RDWR, 0);
429 if ((*param_shm_fd) == -1) {
430 EM_DEBUG_EXCEPTION("FAIL: shm_open(): %s", strerror(errno));
431 return EMAIL_ERROR_SYSTEM_FAILURE;
434 mmapped_t *tmp = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ|PROT_WRITE, MAP_SHARED, (*param_shm_fd), 0);
436 if (tmp == MAP_FAILED) {
437 EM_DEBUG_EXCEPTION("mmap failed: %s", strerror(errno));
438 return EMAIL_ERROR_SYSTEM_FAILURE;
444 return EMAIL_ERROR_NONE;
447 static int _timedlock_shm_mutex(mmapped_t **param_mapped, int sec)
449 EM_DEBUG_FUNC_BEGIN("param_mapped [%p], sec [%d]", param_mapped, sec);
452 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
453 return EMAIL_ERROR_INVALID_PARAM;
456 struct timespec abs_time;
457 clock_gettime(CLOCK_REALTIME, &abs_time);
458 abs_time.tv_sec += sec;
460 int err = pthread_mutex_timedlock(&((*param_mapped)->mutex), &abs_time);
462 if (err == EOWNERDEAD) {
463 err = pthread_mutex_consistent(&((*param_mapped)->mutex));
464 EM_DEBUG_EXCEPTION("Previous owner is dead with lock. Fix mutex : %s", EM_STRERROR(err));
467 EM_DEBUG_EXCEPTION("ERROR : %s", EM_STRERROR(err));
472 return EMAIL_ERROR_NONE;
475 void _unlockshm_mutex(mmapped_t **param_mapped)
477 EM_DEBUG_FUNC_BEGIN();
478 pthread_mutex_unlock(&((*param_mapped)->mutex));
481 /* ------------------------------------------------------------------------------ */
484 static int _open_counter = 0;
485 static int g_transaction = false;
487 static int _get_password_file_name(int account_id, char *recv_password_file_name, char *send_password_file_name);
488 static int _read_password_from_secure_storage(char *file_name, char **password);
490 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
491 static int _get_cert_password_file_name(int index, char *cert_password_file_name);
495 const char *object_name;
496 unsigned int data_flag;
499 static const email_db_object_t _g_db_tables[] =
501 { "mail_read_mail_uid_tbl", 1},
503 { "mail_attachment_tbl", 1},
507 static const email_db_object_t _g_db_indexes[] =
509 { "mail_read_mail_uid_idx1", 1},
511 { "mail_attachment_idx1", 1},
517 CREATE_TABLE_MAIL_ACCOUNT_TBL,
518 CREATE_TABLE_MAIL_BOX_TBL,
519 CREATE_TABLE_MAIL_READ_MAIL_UID_TBL,
520 CREATE_TABLE_MAIL_RULE_TBL,
521 CREATE_TABLE_MAIL_TBL,
522 CREATE_TABLE_MAIL_ATTACHMENT_TBL,
523 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
524 CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL,
526 CREATE_TABLE_MAIL_MEETING_TBL,
527 #ifdef __FEATURE_LOCAL_ACTIVITY__
528 CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL,
530 CREATE_TABLE_MAIL_CERTIFICATE_TBL,
536 DATA1_IDX_IN_MAIL_ACTIVITY_TBL = 0,
537 TRANSTYPE_IDX_IN_MAIL_ACTIVITY_TBL,
538 FLAG_IDX_IN_MAIL_ACTIVITY_TBL,
543 CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL = 0,
544 ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
545 ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
546 ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
547 EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
548 EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
549 EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
550 ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL,
551 EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL,
552 SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL,
553 FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL,
564 ACCOUNT_ID_IDX_IN_MAIL_BOX_TBL = 0,
565 LOCAL_YN_IDX_IN_MAIL_BOX_TBL,
566 MAILBOX_NAME_IDX_IN_MAIL_BOX_TBL,
567 MAILBOX_TYPE_IDX_IN_MAIL_BOX_TBL,
568 ALIAS_IDX_IN_MAIL_BOX_TBL,
569 SYNC_WITH_SERVER_YN_IDX_IN_MAIL_BOX_TBL,
570 MODIFIABLE_YN_IDX_IN_MAIL_BOX_TBL,
571 TOTAL_MAIL_COUNT_ON_SERVER_IDX_IN_MAIL_BOX_TBL,
572 ARCHIVE_IDX_IN_MAIL_BOX_TBL,
573 MAIL_SLOT_SIZE_IDX_IN_MAIL_BOX_TBL,
578 ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL = 0,
579 LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
580 LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
581 MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL,
582 S_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
583 DATA1_IDX_IN_MAIL_READ_MAIL_UID_TBL,
584 DATA2_IDX_IN_MAIL_READ_MAIL_UID_TBL,
585 FLAG_IDX_IN_MAIL_READ_MAIL_UID_TBL,
586 IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, /* unused */
591 ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL = 0,
592 RULE_ID_IDX_IN_MAIL_RULE_TBL,
593 TYPE_IDX_IN_MAIL_RULE_TBL,
594 VALUE_IDX_IN_MAIL_RULE_TBL,
595 ACTION_TYPE_IDX_IN_MAIL_RULE_TBL,
596 TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL,
597 FLAG1_IDX_IN_MAIL_RULE_TBL,
598 FLAG2_IDX_IN_MAIL_RULE_TBL,
603 MAIL_ID_IDX_IN_MAIL_TBL = 0,
604 ACCOUNT_ID_IDX_IN_MAIL_TBL,
605 MAILBOX_ID_IDX_IN_MAIL_TBL,
606 MAILBOX_NAME_IDX_IN_MAIL_TBL,
607 MAILBOX_TYPE_IDX_IN_MAIL_TBL,
608 SUBJECT_IDX_IN_MAIL_TBL,
609 DATETIME_IDX_IN_MAIL_TBL,
610 SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL,
611 SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL,
612 SERVER_MAIL_ID_IDX_IN_MAIL_TBL,
613 MESSAGE_ID_IDX_IN_MAIL_TBL,
614 FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL,
615 FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL,
616 FULL_ADDRESS_TO_IDX_IN_MAIL_TBL,
617 FULL_ADDRESS_CC_IDX_IN_MAIL_TBL,
618 FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL,
619 FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL,
620 EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL,
621 EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL,
622 ALIAS_SENDER_IDX_IN_MAIL_TBL,
623 ALIAS_RECIPIENT_IDX_IN_MAIL_TBL,
624 BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL,
625 FILE_PATH_PLAIN_IDX_IN_MAIL_TBL,
626 FILE_PATH_HTML_IDX_IN_MAIL_TBL,
627 FILE_PATH_MIME_ENTITY_IDX_IN_MAIL_TBL,
628 MAIL_SIZE_IDX_IN_MAIL_TBL,
629 FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL,
630 FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL,
631 FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL,
632 FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL,
633 FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL,
634 FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL,
635 FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL,
636 DRM_STATUS_IDX_IN_MAIL_TBL,
637 PRIORITY_IDX_IN_MAIL_TBL,
638 SAVE_STATUS_IDX_IN_MAIL_TBL,
639 LOCK_STATUS_IDX_IN_MAIL_TBL,
640 REPORT_STATUS_IDX_IN_MAIL_TBL,
641 ATTACHMENT_COUNT_IDX_IN_MAIL_TBL,
642 INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL,
643 THREAD_ID_IDX_IN_MAIL_TBL,
644 THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL,
645 PREVIEW_TEXT_IDX_IN_MAIL_TBL,
646 MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL,
647 MESSAGE_CLASS_IDX_IN_MAIL_TBL,
648 DIGEST_TYPE_IDX_IN_MAIL_TBL,
649 SMIME_TYPE_IDX_IN_MAIL_TBL,
650 FIELD_COUNT_OF_MAIL_TBL, /* End of mail_tbl */
655 ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL = 0,
656 ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL,
657 ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL,
658 ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL,
659 MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
660 ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
661 MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
662 ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
663 ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
664 ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL,
665 ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
666 ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
667 #ifdef __ATTACHMENT_OPTI__
668 ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL,
669 SECTION_IDX_IN_MAIL_ATTACHMENT_TBL,
674 IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL = 0,
675 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
676 MAIL_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
677 ACCOUNT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
678 ADDRESS_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
679 ADDRESS_IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL,
681 ADDRESS_IDX_IN_MAIL_CONTACT_SYNC_TBL,
682 CONTACT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
683 STORAGE_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
684 CONTACT_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
685 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
686 DISPLAY_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
687 FLAG1_IDX_IN_MAIL_CONTACT_SYNC_TBL,
691 /* sowmya.kr 03032010, changes for get list of mails for given addr list */
692 typedef struct _em_mail_id_list {
694 struct _em_mail_id_list *next;
697 static char *g_test_query[] = {
698 /* 1. select mail_account_tbl */
701 " incoming_server_type, "
702 " incoming_server_address, "
703 " user_email_address, "
704 " incoming_server_user_name, "
705 " incoming_server_password, "
707 " incoming_server_port_number, "
708 " incoming_server_secure_connection, "
709 " outgoing_server_type, "
710 " outgoing_server_address, "
711 " outgoing_server_port_number, "
712 " outgoing_server_need_authentication, "
713 " outgoing_server_secure_connection, "
714 " outgoing_server_user_name, "
715 " outgoing_server_password, "
720 " keep_mails_on_pop_server_after_download, "
724 " incoming_server_requires_apop"
726 " is_preset_account, "
730 " req_delivery_receipt, "
731 " req_read_receipt, "
735 " display_name_from, "
737 " forward_with_files, "
741 ", add_my_address_to_bcc"
750 " FROM mail_account_tbl",
751 /* 2. select mail_box_tbl */
759 " sync_with_server_yn, "
761 " total_mail_count_on_server, "
762 " has_archived_mails, "
765 " FROM mail_box_tbl ",
766 /* 3. select mail_read_mail_uid_tbl */
777 " FROM mail_read_mail_uid_tbl ",
778 /* 4. select mail_rule_tbl */
785 " target_mailbox_id, "
788 " FROM mail_rule_tbl ",
789 /* 5. select mail_tbl */
798 " server_mail_status, "
799 " server_mailbox_name, "
802 " full_address_from, "
803 " full_address_reply, "
806 " full_address_bcc, "
807 " full_address_return, "
808 " email_address_sender, "
809 " email_address_recipient, "
812 " body_download_status, "
815 " file_path_mime_entity, "
817 " flags_seen_field ,"
818 " flags_deleted_field ,"
819 " flags_flagged_field ,"
820 " flags_answered_field ,"
821 " flags_recent_field ,"
822 " flags_draft_field ,"
823 " flags_forwarded_field,"
829 " attachment_count, "
830 " inline_content_count, "
832 " thread_item_count, "
834 " meeting_request_status, "
839 /* 6. select mail_attachment_tbl */
848 " attachment_save_status, "
849 " attachment_drm_type, "
850 " attachment_drm_method, "
851 " attachment_inline_content_status, "
852 " attachment_mime_type "
853 " FROM mail_attachment_tbl ",
855 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
864 " FROM mail_partial_body_activity_tbl ",
871 " meeting_response, "
875 " global_object_id, "
878 " standard_time_start_date, "
881 " daylight_time_start_date, "
883 " FROM mail_meeting_tbl ",
885 #ifdef __FEATURE_LOCAL_ACTIVITY__
894 " FROM mail_local_activity_tbl ",
900 /* ----------- Notification Changes ----------- */
903 _NOTI_TYPE_STORAGE = 0,
904 _NOTI_TYPE_NETWORK = 1,
905 _NOTI_TYPE_RESPONSE_TO_API = 2,
909 INTERNAL_FUNC int emstorage_send_noti(enotitype_t notiType, int subType, int data1, int data2, char *data3, int data4)
911 EM_DEBUG_FUNC_BEGIN();
912 EM_PROFILE_BEGIN(profile_emstorage_send_noti);
915 DBusConnection *connection;
916 DBusMessage *signal = NULL;
917 DBusError dbus_error;
919 const char *nullString = "";
921 ENTER_CRITICAL_SECTION(_dbus_noti_lock);
923 dbus_error_init (&dbus_error);
924 connection = dbus_bus_get(DBUS_BUS_SYSTEM, &dbus_error);
926 if (connection == NULL) {
927 EM_DEBUG_LOG("dbus_bus_get is failed");
931 if (notiType == _NOTI_TYPE_STORAGE) {
932 signal = dbus_message_new_signal("/User/Email/StorageChange", EMAIL_STORAGE_CHANGE_NOTI, "email");
934 if (signal == NULL) {
935 EM_DEBUG_EXCEPTION("dbus_message_new_signal is failed");
938 EM_DEBUG_LOG("/User/Email/StorageChange Signal is created by dbus_message_new_signal");
940 dbus_message_append_args(signal, DBUS_TYPE_INT32, &subType, DBUS_TYPE_INVALID);
941 dbus_message_append_args(signal, DBUS_TYPE_INT32, &data1, DBUS_TYPE_INVALID);
942 dbus_message_append_args(signal, DBUS_TYPE_INT32, &data2, DBUS_TYPE_INVALID);
944 dbus_message_append_args(signal, DBUS_TYPE_STRING, &nullString, DBUS_TYPE_INVALID);
946 dbus_message_append_args(signal, DBUS_TYPE_STRING, &data3, DBUS_TYPE_INVALID);
947 dbus_message_append_args(signal, DBUS_TYPE_INT32, &data4, DBUS_TYPE_INVALID);
949 else if (notiType == _NOTI_TYPE_NETWORK) {
950 signal = dbus_message_new_signal("/User/Email/NetworkStatus", EMAIL_NETOWRK_CHANGE_NOTI, "email");
952 if (signal == NULL) {
953 EM_DEBUG_EXCEPTION("dbus_message_new_signal is failed");
957 EM_DEBUG_LOG("/User/Email/NetworkStatus Signal is created by dbus_message_new_signal");
959 dbus_message_append_args(signal, DBUS_TYPE_INT32, &subType, DBUS_TYPE_INVALID);
960 dbus_message_append_args(signal, DBUS_TYPE_INT32, &data1, DBUS_TYPE_INVALID);
962 dbus_message_append_args(signal, DBUS_TYPE_STRING, &nullString, DBUS_TYPE_INVALID);
964 dbus_message_append_args(signal, DBUS_TYPE_STRING, &data3, DBUS_TYPE_INVALID);
965 dbus_message_append_args(signal, DBUS_TYPE_INT32, &data2, DBUS_TYPE_INVALID);
966 dbus_message_append_args(signal, DBUS_TYPE_INT32, &data4, DBUS_TYPE_INVALID);
968 else if (notiType == _NOTI_TYPE_RESPONSE_TO_API) {
969 signal = dbus_message_new_signal("/User/Email/ResponseToAPI", EMAIL_RESPONSE_TO_API_NOTI, "email");
971 if (signal == NULL) {
972 EM_DEBUG_EXCEPTION("dbus_message_new_signal is failed");
976 EM_DEBUG_LOG("/User/Email/ResponseToAPI Signal is created by dbus_message_new_signal");
978 dbus_message_append_args(signal, DBUS_TYPE_INT32, &subType, DBUS_TYPE_INVALID);
979 dbus_message_append_args(signal, DBUS_TYPE_INT32, &data1, DBUS_TYPE_INVALID);
980 dbus_message_append_args(signal, DBUS_TYPE_INT32, &data2, DBUS_TYPE_INVALID);
983 EM_DEBUG_EXCEPTION("Wrong notification type [%d]", notiType);
984 error = EMAIL_ERROR_IPC_CRASH;
988 if (!dbus_connection_send(connection, signal, &error)) {
989 EM_DEBUG_LOG("dbus_connection_send is failed [%d]", error);
992 EM_DEBUG_LOG("dbus_connection_send is successful");
996 /* EM_DEBUG_LOG("Before dbus_connection_flush"); */
997 /* dbus_connection_flush(connection); */
998 /* EM_DEBUG_LOG("After dbus_connection_flush"); */
1003 dbus_message_unref(signal);
1005 LEAVE_CRITICAL_SECTION(_dbus_noti_lock);
1006 EM_PROFILE_END(profile_emstorage_send_noti);
1007 EM_DEBUG_FUNC_END("ret [%d]", ret);
1012 INTERNAL_FUNC int emstorage_notify_storage_event(email_noti_on_storage_event transaction_type, int data1, int data2 , char *data3, int data4)
1014 EM_DEBUG_FUNC_BEGIN("transaction_type[%d], data1[%d], data2[%d], data3[%p], data4[%d]", transaction_type, data1, data2, data3, data4);
1015 return emstorage_send_noti(_NOTI_TYPE_STORAGE, (int)transaction_type, data1, data2, data3, data4);
1018 INTERNAL_FUNC int emstorage_notify_network_event(email_noti_on_network_event status_type, int data1, char *data2, int data3, int data4)
1020 EM_DEBUG_FUNC_BEGIN("status_type[%d], data1[%d], data2[%p], data3[%d], data4[%d]", status_type, data1, data2, data3, data4);
1021 return emstorage_send_noti(_NOTI_TYPE_NETWORK, (int)status_type, data1, data3, data2, data4);
1024 INTERNAL_FUNC int emstorage_notify_response_to_api(email_event_type_t event_type, int data1, int data2)
1026 EM_DEBUG_FUNC_BEGIN("event_type[%d], data1[%d], data2[%p], data3[%d], data4[%d]", event_type, data1, data2);
1027 return emstorage_send_noti(_NOTI_TYPE_RESPONSE_TO_API, (int)event_type, data1, data2, NULL, 0);
1030 /* ----------- Notification Changes End----------- */
1031 static int _get_table_field_data_char(char **table, char *buf, int index)
1033 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1034 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1038 if (table[index] != NULL) {
1039 *buf = (char)atoi(table[index]);
1043 /* EM_DEBUG_LOG("Empty field. Set as zero"); */
1049 static int _get_table_field_data_int(char **table, int *buf, int index)
1051 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1052 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1056 if (table[index] != NULL) {
1057 *buf = atoi(table[index]);
1061 /* EM_DEBUG_LOG("Empty field. Set as zero"); */
1067 static int _get_table_field_data_time_t(char **table, time_t *buf, int index)
1069 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1070 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1074 if (table[index] != NULL) {
1075 *buf = (time_t)atol(table[index]);
1079 /* EM_DEBUG_LOG("Empty field. Set as zero"); */
1085 static int _get_table_field_data_string(char **table, char **buf, int ucs2, int index)
1089 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1090 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1094 char *pTemp = table[index];
1099 sLen = strlen(pTemp);
1101 *buf = (char *) em_malloc(sLen + 1);
1103 EM_DEBUG_EXCEPTION("malloc is failed");
1106 strncpy(*buf, pTemp, sLen);
1111 #ifdef _PRINT_STORAGE_LOG_
1113 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", *buf, index);
1115 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1123 static int _get_table_field_data_string_without_allocation(char **table, char *buf, int buffer_size, int ucs2, int index)
1125 if ((table == NULL) || (buf == NULL) || (index < 0)) {
1126 EM_DEBUG_EXCEPTION(" table[%p], buf[%p], index[%d]", table, buf, index);
1130 char *pTemp = table[index];
1134 memset(buf, 0, buffer_size);
1135 strncpy(buf, pTemp, buffer_size - 1);
1137 #ifdef _PRINT_STORAGE_LOG_
1139 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", buf, index);
1141 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1147 static int _get_stmt_field_data_char(DB_STMT hStmt, char *buf, int index)
1149 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1150 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1154 if (sqlite3_column_text(hStmt, index) != NULL) {
1155 *buf = (char)sqlite3_column_int(hStmt, index);
1156 #ifdef _PRINT_STORAGE_LOG_
1157 EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1162 EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1167 static int _get_stmt_field_data_int(DB_STMT hStmt, int *buf, int index)
1169 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1170 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1174 if (sqlite3_column_text(hStmt, index) != NULL) {
1175 *buf = sqlite3_column_int(hStmt, index);
1176 #ifdef _PRINT_STORAGE_LOG_
1177 EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1182 EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1187 static int _get_stmt_field_data_time_t(DB_STMT hStmt, time_t *buf, int index)
1189 if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1190 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1194 if (sqlite3_column_text(hStmt, index) != NULL) {
1195 *buf = (time_t)sqlite3_column_int(hStmt, index);
1196 #ifdef _PRINT_STORAGE_LOG_
1197 EM_DEBUG_LOG("_get_stmt_field_data_time_t [%d]", *buf);
1202 EM_DEBUG_LOG("_get_stmt_field_data_time_t fail. index [%d]", index);
1206 static int _get_stmt_field_data_string(DB_STMT hStmt, char **buf, int ucs2, int index)
1208 if ((hStmt < 0) || (buf == NULL) || (index < 0)) {
1209 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1214 sLen = sqlite3_column_bytes(hStmt, index);
1216 #ifdef _PRINT_STORAGE_LOG_
1217 EM_DEBUG_LOG("_get_stmt_field_data_string sqlite3_column_bytes sLen[%d]", sLen);
1221 *buf = (char *) em_malloc(sLen + 1);
1222 strncpy(*buf, (char *)sqlite3_column_text(hStmt, index), sLen);
1227 #ifdef _PRINT_STORAGE_LOG_
1229 EM_DEBUG_LOG("buf[%s], index[%d]", *buf, index);
1231 EM_DEBUG_LOG("_get_stmt_field_data_string - No string got");
1237 static void _get_stmt_field_data_blob(DB_STMT hStmt, void **buf, int index)
1239 if ((hStmt < 0) || (buf == NULL) || (index < 0)) {
1240 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1245 sLen = sqlite3_column_bytes(hStmt, index);
1247 #ifdef _PRINT_STORAGE_LOG_
1248 EM_DEBUG_LOG("_get_stmt_field_data_blob sqlite3_column_bytes sLen[%d]", sLen);
1252 *buf = (char *) em_malloc(sLen);
1253 memcpy(*buf, (void *)sqlite3_column_blob(hStmt, index), sLen);
1260 static int _get_stmt_field_data_string_without_allocation(DB_STMT hStmt, char *buf, int buffer_size, int ucs2, int index)
1262 if ((hStmt < 0) || (buf == NULL) || (index < 0)) {
1263 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], buffer_size[%d], index[%d]", hStmt, buf, buffer_size, index);
1268 sLen = sqlite3_column_bytes(hStmt, index);
1270 #ifdef _PRINT_STORAGE_LOG_
1271 EM_DEBUG_LOG("_get_stmt_field_data_string_without_allocation sqlite3_column_bytes sLen[%d]", sLen);
1275 memset(buf, 0, buffer_size);
1276 strncpy(buf, (char *)sqlite3_column_text(hStmt, index), buffer_size - 1);
1281 #ifdef _PRINT_STORAGE_LOG_
1282 EM_DEBUG_LOG("buf[%s], index[%d]", buf, index);
1288 static int _bind_stmt_field_data_char(DB_STMT hStmt, int index, char value)
1290 if ((hStmt == NULL) || (index < 0)) {
1291 EM_DEBUG_EXCEPTION("index[%d]", index);
1295 int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1297 if (ret != SQLITE_OK) {
1298 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1305 static int _bind_stmt_field_data_int(DB_STMT hStmt, int index, int value)
1307 if ((hStmt == NULL) || (index < 0)) {
1308 EM_DEBUG_EXCEPTION("index[%d]", index);
1312 int ret = sqlite3_bind_int(hStmt, index+1, value);
1314 if (ret != SQLITE_OK) {
1315 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1322 static int _bind_stmt_field_data_time_t(DB_STMT hStmt, int index, time_t value)
1324 if ((hStmt == NULL) || (index < 0)) {
1325 EM_DEBUG_EXCEPTION("index[%d]", index);
1329 int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1331 if (ret != SQLITE_OK) {
1332 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1339 static int _bind_stmt_field_data_string(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1341 if ((hStmt == NULL) || (index < 0)) {
1342 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1346 #ifdef _PRINT_STORAGE_LOG_
1347 EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1352 ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1354 ret = sqlite3_bind_text(hStmt, index+1, "", -1, NULL);
1356 if (ret != SQLITE_OK) {
1357 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1364 static int _bind_stmt_field_data_blob(DB_STMT hStmt, int index, void *blob, int blob_size)
1366 if ((hStmt == NULL) || (index < 0)) {
1367 EM_DEBUG_EXCEPTION("index[%d], blob_size[%d]", index, blob_size);
1371 #ifdef _PRINT_STORAGE_LOG_
1372 EM_DEBUG_LOG("hStmt = %p, index = %d, blob_size = %d, blob = [%p]", hStmt, index, blob_size, blob);
1377 ret = sqlite3_bind_blob(hStmt, index+1, blob, blob_size, SQLITE_STATIC);
1379 ret = sqlite3_bind_null(hStmt, index+1);
1381 if (ret != SQLITE_OK) {
1382 EM_DEBUG_EXCEPTION("sqlite3_bind_blob fail [%d]", ret);
1389 static int _delete_temp_file(const char *path)
1391 EM_DEBUG_FUNC_BEGIN("path[%p]", path);
1394 struct dirent *entry = NULL;
1396 char buf[1024] = {0x00, };
1398 if ((dp = opendir(path)) == NULL) {
1399 EM_DEBUG_EXCEPTION("opendir(\"%s\") failed...", path);
1403 while ((entry = readdir(dp)) != NULL) {
1404 SNPRINTF(buf, sizeof(buf), "%s/%s", path, entry->d_name);
1409 EM_DEBUG_FUNC_END();
1413 char *cpy_str(char *src)
1418 if (!(p = em_malloc((int)strlen(src) + 1))) {
1419 EM_DEBUG_EXCEPTION("mailoc failed...");
1422 strncpy(p, src, strlen(src));
1428 static void _emstorage_close_once(void)
1430 EM_DEBUG_FUNC_BEGIN();
1432 DELETE_CRITICAL_SECTION(_transactionBeginLock);
1433 DELETE_CRITICAL_SECTION(_transactionEndLock);
1435 EM_DEBUG_FUNC_END();
1438 INTERNAL_FUNC int emstorage_close(int *err_code)
1440 EM_DEBUG_FUNC_BEGIN();
1443 int error = EMAIL_ERROR_NONE;
1445 emstorage_db_close(&error);
1447 if (--_open_counter == 0)
1448 _emstorage_close_once();
1452 if (err_code != NULL)
1455 EM_DEBUG_FUNC_END("ret [%d]", ret);
1459 static void *_emstorage_open_once(int *err_code)
1461 EM_DEBUG_FUNC_BEGIN();
1463 int error = EMAIL_ERROR_NONE;
1465 mkdir(EMAILPATH, DIRECTORY_PERMISSION);
1466 mkdir(DATA_PATH, DIRECTORY_PERMISSION);
1468 mkdir(MAILHOME, DIRECTORY_PERMISSION);
1470 SNPRINTF(g_db_path, sizeof(g_db_path), "%s/%s", MAILHOME, MAILTEMP);
1471 mkdir(g_db_path, DIRECTORY_PERMISSION);
1473 _delete_temp_file(g_db_path);
1475 g_transaction = false;
1477 if (!emstorage_create_table(EMAIL_CREATE_DB_NORMAL, &error)) {
1478 EM_DEBUG_EXCEPTION(" emstorage_create_table failed - %d", error);
1483 if (err_code != NULL)
1490 /* pData : a parameter which is registered when busy handler is registerd */
1491 /* count : retry count */
1492 #define EMAIL_STORAGE_MAX_RETRY_COUNT 20
1493 static int _callback_sqlite_busy_handler(void *pData, int count)
1495 EM_DEBUG_LOG("Busy Handler Called!!: [%d]", count);
1496 usleep(200000); /* sleep time when SQLITE_LOCK */
1498 /* retry will be stopped if busy handler return 0 */
1499 return EMAIL_STORAGE_MAX_RETRY_COUNT - count;
1502 static int _delete_all_files_and_directories(int *err_code)
1504 EM_DEBUG_FUNC_BEGIN();
1506 int error = EMAIL_ERROR_NONE;
1509 if (!emstorage_delete_file(EMAIL_SERVICE_DB_FILE_PATH, &error)) {
1510 if (error != EMAIL_ERROR_FILE_NOT_FOUND) {
1511 EM_DEBUG_EXCEPTION("remove failed - %s", EMAIL_SERVICE_DB_FILE_PATH);
1516 if (!emstorage_delete_dir(MAILHOME, &error)) {
1517 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed");
1526 EM_DEBUG_FUNC_END();
1530 static int _recovery_from_malformed_db_file(int *err_code)
1532 EM_DEBUG_FUNC_BEGIN();
1534 int error = EMAIL_ERROR_NONE;
1537 /* Delete all files and directories */
1538 if (!_delete_all_files_and_directories(&error)) {
1539 EM_DEBUG_EXCEPTION("_delete_all_files_and_directories failed [%d]", error);
1543 /* Delete all accounts on MyAccount */
1545 /* Delete all managed connection to DB */
1546 emstorage_reset_db_handle_list();
1553 EM_DEBUG_FUNC_END();
1558 INTERNAL_FUNC int em_db_open(sqlite3 **sqlite_handle, int *err_code)
1560 EM_DEBUG_FUNC_BEGIN();
1562 int error = EMAIL_ERROR_NONE;
1565 EM_DEBUG_LOG("*sqlite_handle[%p]", *sqlite_handle);
1567 if (NULL == *sqlite_handle) {
1569 EM_DEBUG_LOG("Open DB");
1570 EMSTORAGE_PROTECTED_FUNC_CALL(db_util_open(EMAIL_SERVICE_DB_FILE_PATH, sqlite_handle, DB_UTIL_REGISTER_HOOK_METHOD), rc);
1571 if (SQLITE_OK != rc) {
1572 EM_DEBUG_EXCEPTION("db_util_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1573 error = EMAIL_ERROR_DB_FAILURE;
1574 db_util_close(*sqlite_handle);
1575 *sqlite_handle = NULL;
1577 if (SQLITE_CORRUPT == rc) /* SQLITE_CORRUPT : The database disk image is malformed */ {/* Recovery DB file */
1578 EM_DEBUG_LOG("The database disk image is malformed. Trying to remove and create database disk image and directories");
1579 if (!_recovery_from_malformed_db_file(&error)) {
1580 EM_DEBUG_EXCEPTION("_recovery_from_malformed_db_file failed [%d]", error);
1584 EM_DEBUG_LOG("Open DB again");
1585 EMSTORAGE_PROTECTED_FUNC_CALL(db_util_open(EMAIL_SERVICE_DB_FILE_PATH, sqlite_handle, DB_UTIL_REGISTER_HOOK_METHOD), rc);
1586 if (SQLITE_OK != rc) {
1587 EM_DEBUG_EXCEPTION("db_util_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1588 error = EMAIL_ERROR_DB_FAILURE;
1589 db_util_close(*sqlite_handle);
1590 *sqlite_handle = NULL;
1596 EM_DEBUG_LOG(">>>>> DB Handle : *sqlite_handle[%p]", *sqlite_handle);
1598 /* register busy handler */
1599 EM_DEBUG_LOG(">>>>> Register busy handler.....");
1600 rc = sqlite3_busy_handler(*sqlite_handle, _callback_sqlite_busy_handler, NULL); /* Busy Handler registration, NULL is a parameter which will be passed to handler */
1601 if (SQLITE_OK != rc) {
1602 EM_DEBUG_EXCEPTION("sqlite3_busy_handler fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1603 error = EMAIL_ERROR_DB_FAILURE;
1604 db_util_close(*sqlite_handle);
1605 *sqlite_handle = NULL;
1610 EM_DEBUG_LOG(">>>>> DB Already Opened......");
1616 if (err_code != NULL)
1619 EM_DEBUG_FUNC_END("ret [%d]", ret);
1623 INTERNAL_FUNC sqlite3* emstorage_db_open(int *err_code)
1625 EM_DEBUG_FUNC_BEGIN();
1626 #ifdef _MULTIPLE_DB_HANDLE
1627 sqlite3 *_db_handle = NULL;
1630 int error = EMAIL_ERROR_NONE;
1632 _initialize_shm_mutex(SHM_FILE_FOR_DB_LOCK, &shm_fd_for_db_lock, &mapped_for_db_lock);
1634 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
1635 _initialize_shm_mutex(SHM_FILE_FOR_MAIL_ID_LOCK, &shm_fd_for_generating_mail_id, &mapped_for_generating_mail_id);
1636 #endif /*__FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
1638 if (!em_db_open(&_db_handle, &error)) {
1639 EM_DEBUG_EXCEPTION("em_db_open failed[%d]", error);
1643 #ifdef _MULTIPLE_DB_HANDLE
1644 emstorage_set_db_handle(_db_handle);
1648 if (err_code != NULL)
1651 EM_DEBUG_FUNC_END("ret [%p]", _db_handle);
1655 INTERNAL_FUNC int emstorage_db_close(int *err_code)
1657 EM_DEBUG_FUNC_BEGIN();
1658 #ifdef _MULTIPLE_DB_HANDLE
1659 sqlite3 *_db_handle = emstorage_get_db_handle();
1662 int error = EMAIL_ERROR_NONE;
1665 DELETE_CRITICAL_SECTION(_transactionBeginLock);
1666 DELETE_CRITICAL_SECTION(_transactionEndLock);
1667 DELETE_CRITICAL_SECTION(_dbus_noti_lock);
1670 ret = db_util_close(_db_handle);
1672 if (ret != SQLITE_OK) {
1673 EM_DEBUG_EXCEPTION(" db_util_close fail - %d", ret);
1674 error = EMAIL_ERROR_DB_FAILURE;
1678 #ifdef _MULTIPLE_DB_HANDLE
1679 emstorage_remove_db_handle();
1687 if (err_code != NULL)
1690 EM_DEBUG_FUNC_END("ret [%d]", ret);
1695 INTERNAL_FUNC int emstorage_open(int *err_code)
1697 EM_DEBUG_FUNC_BEGIN();
1700 int error = EMAIL_ERROR_NONE;
1704 retValue = mkdir(DB_PATH, DIRECTORY_PERMISSION);
1706 EM_DEBUG_LOG("mkdir return- %d", retValue);
1707 EM_DEBUG_LOG("emstorage_open - before db_util_open - pid = %d", getpid());
1709 if (emstorage_db_open(&error) == NULL) {
1710 EM_DEBUG_EXCEPTION("emstorage_db_open failed[%d]", error);
1715 if (_open_counter++ == 0)
1716 _emstorage_open_once(&error);
1721 if (err_code != NULL)
1724 EM_DEBUG_FUNC_END("ret [%d]", ret);
1729 INTERNAL_FUNC int emstorage_create_table(emstorage_create_db_type_t type, int *err_code)
1731 EM_DEBUG_FUNC_BEGIN();
1733 int error = EMAIL_ERROR_NONE;
1734 int rc = -1, ret = false;
1735 char sql_query_string[QUERY_SIZE] = {0, };
1736 char *create_table_query[] =
1738 /* 1. create mail_account_tbl */
1739 "CREATE TABLE mail_account_tbl \n"
1741 "account_id INTEGER PRIMARY KEY,\n"
1742 "account_name VARCHAR(51),\n"
1743 "logo_icon_path VARCHAR(256),\n"
1745 "user_data_length INTEGER,\n"
1746 "account_svc_id INTEGER,\n"
1747 "sync_status INTEGER,\n"
1748 "sync_disabled INTEGER,\n"
1749 "default_mail_slot_size INTEGER,\n"
1750 "user_display_name VARCHAR(31),\n"
1751 "user_email_address VARCHAR(129),\n"
1752 "reply_to_address VARCHAR(129),\n"
1753 "return_address VARCHAR(129),\n"
1754 "incoming_server_type INTEGER,\n"
1755 "incoming_server_address VARCHAR(51),\n"
1756 "incoming_server_port_number INTEGER,\n"
1757 "incoming_server_user_name VARCHAR(51),\n"
1758 "incoming_server_password VARCHAR(51),\n"
1759 "incoming_server_secure_connection INTEGER,\n"
1760 "retrieval_mode INTEGER,\n"
1761 "keep_mails_on_pop_server_after_download INTEGER,\n"
1762 "check_interval INTEGER,\n"
1763 "auto_download_size INTEGER,\n"
1764 "outgoing_server_type INTEGER,\n"
1765 "outgoing_server_address VARCHAR(51),\n"
1766 "outgoing_server_port_number INTEGER,\n"
1767 "outgoing_server_user_name VARCHAR(51),\n"
1768 "outgoing_server_password VARCHAR(51),\n"
1769 "outgoing_server_secure_connection INTEGER,\n"
1770 "outgoing_server_need_authentication INTEGER,\n"
1771 "outgoing_server_use_same_authenticator INTEGER,\n"
1772 "priority INTEGER,\n"
1773 "keep_local_copy INTEGER,\n"
1774 "req_delivery_receipt INTEGER,\n"
1775 "req_read_receipt INTEGER,\n"
1776 "download_limit INTEGER,\n"
1777 "block_address INTEGER,\n"
1778 "block_subject INTEGER,\n"
1779 "display_name_from VARCHAR(256),\n"
1780 "reply_with_body INTEGER,\n"
1781 "forward_with_files INTEGER,\n"
1782 "add_myname_card INTEGER,\n"
1783 "add_signature INTEGER,\n"
1784 "signature VARCHAR(256),\n"
1785 "add_my_address_to_bcc INTEGER,\n"
1786 "pop_before_smtp INTEGER,\n"
1787 "incoming_server_requires_apop INTEGER,\n"
1788 "smime_type INTEGER,\n"
1789 "certificate_path VARCHAR(256),\n"
1790 "cipher_type INTEGER,\n"
1791 "digest_type INTEGER\n"
1793 /* 2. create mail_box_tbl */
1794 "CREATE TABLE mail_box_tbl \n"
1796 " mailbox_id INTEGER \n"
1797 ", account_id INTEGER \n"
1798 ", local_yn INTEGER \n"
1799 ", mailbox_name VARCHAR(256) \n"
1800 ", mailbox_type INTEGER \n"
1801 ", alias VARCHAR(256) \n"
1802 ", sync_with_server_yn INTEGER \n"
1803 ", modifiable_yn INTEGER \n"
1804 ", total_mail_count_on_server INTEGER \n"
1805 ", has_archived_mails INTEGER \n"
1806 ", mail_slot_size INTEGER \n"
1807 ", last_sync_time DATETIME \n"
1810 /* 3. create mail_read_mail_uid_tbl */
1811 "CREATE TABLE mail_read_mail_uid_tbl \n"
1813 " account_id INTEGER \n"
1814 ", mailbox_id VARCHAR(129) \n"
1815 ", local_uid INTEGER \n"
1816 ", mailbox_name VARCHAR(256) \n"
1817 ", s_uid VARCHAR(129) \n"
1818 ", data1 INTEGER \n"
1819 ", data2 VARCHAR(257) \n"
1821 ", idx_num INTEGER PRIMARY KEY \n"
1823 /* 4. create mail_rule_tbl */
1824 "CREATE TABLE mail_rule_tbl \n"
1826 " account_id INTEGER \n"
1827 ", rule_id INTEGER PRIMARY KEY\n"
1829 ", value VARCHAR(257) \n"
1830 ", action_type INTEGER \n"
1831 ", target_mailbox_id INTEGER \n"
1832 ", flag1 INTEGER \n"
1833 ", flag2 INTEGER \n"
1835 /* 5. create mail_tbl */
1836 "CREATE TABLE mail_tbl \n"
1838 " mail_id INTEGER PRIMARY KEY \n"
1839 ", account_id INTEGER \n"
1840 ", mailbox_id INTEGER \n"
1841 ", mailbox_name VARCHAR(129) \n"
1842 ", mailbox_type INTEGER \n"
1844 ", date_time DATETIME \n"
1845 ", server_mail_status INTEGER \n"
1846 ", server_mailbox_name VARCHAR(129) \n"
1847 ", server_mail_id VARCHAR(129) \n"
1848 ", message_id VARCHAR(257) \n"
1849 ", full_address_from TEXT \n"
1850 ", full_address_reply TEXT \n"
1851 ", full_address_to TEXT \n"
1852 ", full_address_cc TEXT \n"
1853 ", full_address_bcc TEXT \n"
1854 ", full_address_return TEXT \n"
1855 ", email_address_sender TEXT collation user1 \n"
1856 ", email_address_recipient TEXT collation user1 \n"
1857 ", alias_sender TEXT \n"
1858 ", alias_recipient TEXT \n"
1859 ", body_download_status INTEGER \n"
1860 ", file_path_plain VARCHAR(257) \n"
1861 ", file_path_html VARCHAR(257) \n"
1862 ", file_path_mime_entity VARCHAR(257) \n"
1863 ", mail_size INTEGER \n"
1864 ", flags_seen_field BOOLEAN \n"
1865 ", flags_deleted_field BOOLEAN \n"
1866 ", flags_flagged_field BOOLEAN \n"
1867 ", flags_answered_field BOOLEAN \n"
1868 ", flags_recent_field BOOLEAN \n"
1869 ", flags_draft_field BOOLEAN \n"
1870 ", flags_forwarded_field BOOLEAN \n"
1871 ", DRM_status INTEGER \n"
1872 ", priority INTEGER \n"
1873 ", save_status INTEGER \n"
1874 ", lock_status INTEGER \n"
1875 ", report_status INTEGER \n"
1876 ", attachment_count INTEGER \n"
1877 ", inline_content_count INTEGER \n"
1878 ", thread_id INTEGER \n"
1879 ", thread_item_count INTEGER \n"
1880 ", preview_text TEXT \n"
1881 ", meeting_request_status INTEGER \n"
1882 ", message_class INTEGER \n"
1883 ", digest_type INTEGER \n"
1884 ", smime_type INTEGER \n"
1885 ", FOREIGN KEY(account_id) REFERENCES mail_account_tbl(account_id) \n"
1888 /* 6. create mail_attachment_tbl */
1889 "CREATE TABLE mail_attachment_tbl \n"
1891 " attachment_id INTEGER PRIMARY KEY"
1892 ", attachment_name VARCHAR(257) \n"
1893 ", attachment_path VARCHAR(257) \n"
1894 ", attachment_size INTEGER \n"
1895 ", mail_id INTEGER \n"
1896 ", account_id INTEGER \n"
1897 ", mailbox_id INTEGER \n"
1898 ", attachment_save_status INTEGER \n"
1899 ", attachment_drm_type INTEGER \n"
1900 ", attachment_drm_method INTEGER \n"
1901 ", attachment_inline_content_status INTEGER \n"
1902 ", attachment_mime_type VARCHAR(257) \n"
1903 #ifdef __ATTACHMENT_OPTI__
1904 ", encoding INTEGER \n"
1905 ", section varchar(257) \n"
1909 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
1911 "CREATE TABLE mail_partial_body_activity_tbl \n"
1913 " account_id INTEGER \n"
1914 ", mail_id INTEGER \n"
1915 ", server_mail_id INTEGER \n"
1916 ", activity_id INTEGER PRIMARY KEY \n"
1917 ", activity_type INTEGER \n"
1918 ", mailbox_id INTEGER \n"
1919 ", mailbox_name VARCHAR(4000) \n"
1923 "CREATE TABLE mail_meeting_tbl \n"
1925 " mail_id INTEGER PRIMARY KEY \n"
1926 ", account_id INTEGER \n"
1927 ", mailbox_id INTEGER \n"
1928 ", meeting_response INTEGER \n"
1929 ", start_time INTEGER \n"
1930 ", end_time INTEGER \n"
1931 ", location TEXT \n"
1932 ", global_object_id TEXT \n"
1933 ", offset INTEGER \n"
1934 ", standard_name TEXT \n"
1935 ", standard_time_start_date INTEGER \n"
1936 ", standard_bias INTEGER \n"
1937 ", daylight_name TEXT \n"
1938 ", daylight_time_start_date INTEGER \n"
1939 ", daylight_bias INTEGER \n"
1942 #ifdef __FEATURE_LOCAL_ACTIVITY__
1943 "CREATE TABLE mail_local_activity_tbl \n"
1945 " activity_id INTEGER \n"
1946 ", account_id INTEGER \n"
1947 ", mail_id INTEGER \n"
1948 ", activity_type INTEGER \n"
1949 ", server_mailid VARCHAR(129) \n"
1950 ", src_mbox VARCHAR(129) \n"
1951 ", dest_mbox VARCHAR(129) \n"
1954 "CREATE TABLE mail_certificate_tbl \n"
1956 " certificate_id INTEGER \n"
1957 ", issue_year INTEGER \n"
1958 ", issue_month INTEGER \n"
1959 ", issue_day INTEGER \n"
1960 ", expiration_year INTEGER \n"
1961 ", expiration_month INTEGER \n"
1962 ", expiration_day INTEGER \n"
1963 ", issue_organization_name VARCHAR(256) \n"
1964 ", email_address VARCHAR(129) \n"
1965 ", subject_str VARCHAR(256) \n"
1966 ", filepath VARCHAR(256) \n"
1967 ", password VARCHAR(51) \n"
1972 sqlite3 *local_db_handle = emstorage_get_db_connection();
1974 EM_DEBUG_LOG("local_db_handle = %p.", local_db_handle);
1979 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_account_tbl';";
1980 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
1981 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1982 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
1984 EM_DEBUG_LOG("emstorage_create_table - result[1] = %s %c", result[1], result[1]);
1986 if (atoi(result[1]) < 1) {
1987 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
1988 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
1989 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
1991 EM_DEBUG_LOG("CREATE TABLE mail_account_tbl");
1993 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], sizeof(sql_query_string));
1995 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
1996 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
1998 /* create mail_account_tbl unique index */
1999 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE UNIQUE INDEX mail_account_idx1 ON mail_account_tbl (account_id)");
2001 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2002 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2004 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2006 } /* mail_account_tbl */
2007 else if (type == EMAIL_CREATE_DB_CHECK) {
2008 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], NULL, NULL, NULL), rc);
2009 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)));
2012 sqlite3_free_table(result);
2015 /* 2. create mail_box_tbl */
2016 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_box_tbl';";
2018 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2019 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2020 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2022 if (atoi(result[1]) < 1) {
2023 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2024 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2025 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2027 EM_DEBUG_LOG("CREATE TABLE mail_box_tbl");
2029 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_BOX_TBL], sizeof(sql_query_string));
2031 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2032 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2033 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2035 /* create mail_local_mailbox_tbl unique index */
2036 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE UNIQUE INDEX mail_box_idx1 ON mail_box_tbl (account_id, local_yn, mailbox_name)");
2038 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2039 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2040 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2042 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2044 } /* mail_box_tbl */
2045 else if (type == EMAIL_CREATE_DB_CHECK) {
2046 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_BOX_TBL], NULL, NULL, NULL);
2047 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2048 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_BOX_TBL], rc, sqlite3_errmsg(local_db_handle)));
2050 sqlite3_free_table(result);
2052 /* 3. create mail_read_mail_uid_tbl */
2053 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_read_mail_uid_tbl';";
2054 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2055 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2056 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2059 if (atoi(result[1]) < 1) {
2060 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2061 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2062 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2064 EM_DEBUG_LOG("CREATE TABLE mail_read_mail_uid_tbl");
2066 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], sizeof(sql_query_string));
2068 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2069 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2070 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2072 /* create mail_read_mail_uid_tbl unique index */
2073 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE UNIQUE INDEX mail_read_mail_uid_idx1 ON mail_read_mail_uid_tbl (account_id, mailbox_id, local_uid, mailbox_name, s_uid)");
2075 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2076 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2077 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2078 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2080 } /* mail_read_mail_uid_tbl */
2081 else if (type == EMAIL_CREATE_DB_CHECK) {
2082 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], NULL, NULL, NULL), rc);
2083 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2084 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], rc, sqlite3_errmsg(local_db_handle)));
2086 sqlite3_free_table(result);
2089 /* 4. create mail_rule_tbl */
2090 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_rule_tbl';";
2092 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2093 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2094 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2096 if (atoi(result[1]) < 1) {
2097 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2098 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2099 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2101 EM_DEBUG_LOG("CREATE TABLE mail_rule_tbl");
2103 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_RULE_TBL], sizeof(sql_query_string));
2105 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2106 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2107 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2108 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2110 } /* mail_rule_tbl */
2111 else if (type == EMAIL_CREATE_DB_CHECK) {
2112 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_RULE_TBL], NULL, NULL, NULL), rc);
2113 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2114 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_RULE_TBL], rc, sqlite3_errmsg(local_db_handle)));
2116 sqlite3_free_table(result);
2119 /* 5. create mail_tbl */
2120 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_tbl';";
2121 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2122 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2123 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2125 if (atoi(result[1]) < 1) {
2126 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2127 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2128 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2129 EM_DEBUG_LOG("CREATE TABLE mail_tbl");
2131 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TBL], sizeof(sql_query_string));
2133 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2134 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2135 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2137 /* create mail_tbl unique index */
2138 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE UNIQUE INDEX mail_idx1 ON mail_tbl (mail_id, account_id)");
2139 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2140 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2141 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2143 /* create mail_tbl index for date_time */
2144 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE INDEX mail_idx_date_time ON mail_tbl (date_time)");
2145 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2146 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2147 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2149 /* create mail_tbl index for thread_item_count */
2150 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE INDEX mail_idx_thread_mail_count ON mail_tbl (thread_item_count)");
2151 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2152 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2153 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2155 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2157 /* just one time call */
2158 /* EFTSInitFTSIndex(FTS_EMAIL_IDX); */
2160 else if (type == EMAIL_CREATE_DB_CHECK) {
2161 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TBL], NULL, NULL, NULL), rc);
2162 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2163 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TBL], rc, sqlite3_errmsg(local_db_handle)));
2165 sqlite3_free_table(result);
2168 /* 6. create mail_attachment_tbl */
2169 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_attachment_tbl';";
2170 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2171 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2172 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2174 if (atoi(result[1]) < 1) {
2175 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2176 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2177 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2178 EM_DEBUG_LOG("CREATE TABLE mail_attachment_tbl");
2180 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], sizeof(sql_query_string));
2182 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2183 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2184 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2186 /* create mail_attachment_tbl unique index */
2187 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE UNIQUE INDEX mail_attachment_idx1 ON mail_attachment_tbl (mail_id, attachment_id) ");
2189 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2190 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2191 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2192 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2194 } /* mail_attachment_tbl */
2195 else if (type == EMAIL_CREATE_DB_CHECK) {
2196 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], NULL, NULL, NULL), rc);
2197 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2198 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], rc, sqlite3_errmsg(local_db_handle)));
2200 sqlite3_free_table(result);
2202 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
2204 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_partial_body_activity_tbl';";
2205 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2206 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2207 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2209 if (atoi(result[1]) < 1) {
2211 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2212 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2213 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2215 EM_DEBUG_LOG("CREATE TABLE mail_partial_body_activity_tbl");
2217 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], sizeof(sql_query_string));
2219 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2220 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2221 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2222 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2224 } /* mail_rule_tbl */
2225 else if (type == EMAIL_CREATE_DB_CHECK) {
2226 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2227 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2228 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2230 sqlite3_free_table(result);
2232 #endif /* __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
2234 /* create mail_meeting_tbl */
2235 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_meeting_tbl';";
2236 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2237 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2238 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2240 if (atoi(result[1]) < 1) {
2241 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2242 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2243 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2245 EM_DEBUG_LOG("CREATE TABLE mail_meeting_tbl");
2247 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_MEETING_TBL], sizeof(sql_query_string));
2249 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2250 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2251 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2252 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE UNIQUE INDEX mail_meeting_idx1 ON mail_meeting_tbl (mail_id)");
2254 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2255 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2256 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2257 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2259 } /* mail_contact_sync_tbl */
2260 else if (type == EMAIL_CREATE_DB_CHECK) {
2261 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], NULL, NULL, NULL), rc);
2262 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2263 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], rc, sqlite3_errmsg(local_db_handle)));
2265 sqlite3_free_table(result);
2267 #ifdef __FEATURE_LOCAL_ACTIVITY__
2269 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_local_activity_tbl';";
2270 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2271 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2272 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2274 if (atoi(result[1]) < 1) {
2276 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2277 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2278 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2280 EM_DEBUG_LOG(" CREATE TABLE mail_local_activity_tbl");
2282 SNPRINTF(sql_query_string, sizeof(sql_query_string), create_table_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL]);
2284 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2285 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2286 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2287 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2289 } /* mail_rule_tbl */
2290 else if (type == EMAIL_CREATE_DB_CHECK) {
2291 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL);
2292 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2293 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2294 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2296 sqlite3_free_table(result);
2298 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2299 /* create mail_certificate_tbl */
2300 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_certificate_tbl';";
2301 /* rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
2302 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2303 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)));
2305 if (atoi(result[1]) < 1) {
2306 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2307 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2309 EM_DEBUG_LOG("CREATE TABLE mail_certificate_tbl");
2311 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], sizeof(sql_query_string));
2313 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2314 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2315 SNPRINTF(sql_query_string, sizeof(sql_query_string), "CREATE UNIQUE INDEX mail_certificate_idx1 ON mail_certificate_tbl (certificate_id)");
2317 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
2318 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2319 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2320 } /* mail_contact_sync_tbl */
2321 else if (type == EMAIL_CREATE_DB_CHECK) {
2322 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], NULL, NULL, NULL), rc);
2323 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)));
2325 sqlite3_free_table(result);
2331 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2334 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
2339 if (err_code != NULL)
2342 EM_DEBUG_FUNC_END("ret [%d]", ret);
2346 /* Query series --------------------------------------------------------------*/
2348 INTERNAL_FUNC int emstorage_query_mail_count(const char *input_conditional_clause, int input_transaction, int *output_total_mail_count, int *output_unseen_mail_count)
2350 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);
2352 int error = EMAIL_ERROR_NONE;
2353 DB_STMT hStmt = NULL;
2354 char sql_query_string[QUERY_SIZE] = {0, };
2357 sqlite3 *local_db_handle = NULL;
2359 if (!input_conditional_clause || (!output_total_mail_count && !output_unseen_mail_count)) {
2360 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2361 return EMAIL_ERROR_INVALID_PARAM;
2364 memset(&sql_query_string, 0x00, sizeof(sql_query_string));
2365 local_db_handle = emstorage_get_db_connection();
2367 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2369 SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT COUNT(*) FROM mail_tbl");
2370 EM_SAFE_STRCAT(sql_query_string, (char*)input_conditional_clause);
2372 if (output_total_mail_count) {
2373 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
2374 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2375 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2377 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2378 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2379 ("sqlite3_step fail:%d", rc));
2380 _get_stmt_field_data_int(hStmt, output_total_mail_count, 0);
2383 if (output_unseen_mail_count) {
2384 EM_SAFE_STRCAT(sql_query_string, " AND flags_seen_field = 0 ");
2386 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
2387 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2388 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2390 *output_unseen_mail_count = atoi(result[1]);
2391 sqlite3_free_table(result);
2396 if (hStmt != NULL) {
2397 rc = sqlite3_finalize(hStmt);
2398 if (rc != SQLITE_OK) {
2399 EM_DEBUG_LOG("sqlite3_finalize failed [%d] : %s", rc, err_msg);
2400 error = EMAIL_ERROR_DB_FAILURE;
2404 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2407 EM_DEBUG_FUNC_END("error [%d]", error);
2411 INTERNAL_FUNC int emstorage_query_mail_id_list(const char *input_conditional_clause, int input_transaction, int **output_mail_id_list, int *output_mail_id_count)
2413 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);
2420 int error = EMAIL_ERROR_NONE;
2421 int *result_mail_id_list = NULL;
2422 char **result = NULL;
2423 char sql_query_string[QUERY_SIZE] = {0, };
2424 sqlite3 *local_db_handle = emstorage_get_db_connection();
2426 EM_IF_NULL_RETURN_VALUE(input_conditional_clause, EMAIL_ERROR_INVALID_PARAM);
2427 EM_IF_NULL_RETURN_VALUE(output_mail_id_list, EMAIL_ERROR_INVALID_PARAM);
2428 EM_IF_NULL_RETURN_VALUE(output_mail_id_count, EMAIL_ERROR_INVALID_PARAM);
2430 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2432 /* Composing query */
2433 SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, "SELECT mail_id FROM mail_tbl ");
2434 EM_SAFE_STRCAT(sql_query_string, (char*)input_conditional_clause);
2436 EM_DEBUG_LOG("query[%s].", sql_query_string);
2438 /* Performing query */
2439 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2440 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2441 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2445 /* to get mail list */
2447 EM_DEBUG_EXCEPTION("No mail found...");
2448 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2452 EM_DEBUG_LOG("There are [%d] mails.", count);
2454 if (!(result_mail_id_list = (int*)em_malloc(sizeof(int) * count))) {
2455 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
2456 error = EMAIL_ERROR_OUT_OF_MEMORY;
2460 EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2462 for (i = 0; i < count; i++)
2463 _get_table_field_data_int(result, result_mail_id_list + i, col_index++);
2465 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >> ", count);
2467 *output_mail_id_list = result_mail_id_list;
2468 *output_mail_id_count = count;
2473 sqlite3_free_table(result);
2475 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2478 if(error != EMAIL_ERROR_NONE)
2479 EM_SAFE_FREE(result_mail_id_list);
2481 EM_DEBUG_FUNC_END("error [%d]", error);
2485 INTERNAL_FUNC int emstorage_query_mail_list(const char *conditional_clause, int transaction, email_mail_list_item_t** result_mail_list, int *result_count, int *err_code)
2487 EM_DEBUG_FUNC_BEGIN();
2488 EM_PROFILE_BEGIN(emstorage_query_mail_list_func);
2490 int i = 0, count = 0, rc = -1, to_get_count = (result_mail_list)?0:1;
2491 int local_inline_content_count = 0, local_attachment_count = 0;
2492 int cur_query = 0, base_count = 0, col_index;
2493 int ret = false, error = EMAIL_ERROR_NONE;
2494 char *date_time_string = NULL;
2495 char **result = NULL, sql_query_string[QUERY_SIZE] = {0, };
2496 char *field_list = "mail_id, account_id, mailbox_id, full_address_from, email_address_sender, full_address_to, subject, body_download_status, 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 ";
2497 email_mail_list_item_t *mail_list_item_from_tbl = NULL;
2498 sqlite3 *local_db_handle = emstorage_get_db_connection();
2500 EM_IF_NULL_RETURN_VALUE(conditional_clause, false);
2501 EM_IF_NULL_RETURN_VALUE(result_count, false);
2503 EMSTORAGE_START_READ_TRANSACTION(transaction);
2506 if (to_get_count) /* count only */
2507 cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, "SELECT mail_id FROM mail_tbl");
2508 else /* mail list in plain form */
2509 cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, "SELECT %s FROM mail_tbl ", field_list);
2511 /* cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, conditional_clause); This code caused some crashes.*/
2512 strncat(sql_query_string, conditional_clause, QUERY_SIZE - cur_query);
2514 EM_DEBUG_LOG("emstorage_query_mail_list : query[%s].", sql_query_string);
2516 /* performing query */
2517 EM_PROFILE_BEGIN(emstorage_query_mail_list_performing_query);
2518 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2519 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2520 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2521 EM_PROFILE_END(emstorage_query_mail_list_performing_query);
2524 base_count = ({ int i=0; char *tmp = field_list; for (i=0; tmp && *(tmp + 1); tmp = index(tmp + 1, ','), i++); i; });
2526 col_index = base_count;
2528 EM_DEBUG_LOG("base_count [%d]", base_count);
2533 EM_DEBUG_EXCEPTION("No mail found...");
2535 error= EMAIL_ERROR_MAIL_NOT_FOUND;
2538 EM_DEBUG_LOG("There are [%d] mails.", count);
2541 /* to get mail list */
2543 EM_DEBUG_EXCEPTION("No mail found...");
2545 error= EMAIL_ERROR_MAIL_NOT_FOUND;
2549 EM_DEBUG_LOG("There are [%d] mails.", count);
2550 if (!(mail_list_item_from_tbl = (email_mail_list_item_t*)em_malloc(sizeof(email_mail_list_item_t) * count))) {
2551 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
2552 error = EMAIL_ERROR_OUT_OF_MEMORY;
2556 EM_PROFILE_BEGIN(emstorage_query_mail_list_loop);
2557 EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2558 for (i = 0; i < count; i++) {
2559 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_id), col_index++);
2560 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].account_id), col_index++);
2561 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mailbox_id), col_index++);
2562 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].from, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2563 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].from_email_address, MAX_EMAIL_ADDRESS_LENGTH, 1, col_index++);
2564 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].recipients, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2565 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].subject, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2566 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].is_text_downloaded), col_index++);
2567 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_seen_field), col_index++);
2568 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_deleted_field), col_index++);
2569 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_flagged_field), col_index++);
2570 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_answered_field), col_index++);
2571 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_recent_field), col_index++);
2572 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_draft_field), col_index++);
2573 _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_forwarded_field), col_index++);
2574 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].has_drm_attachment), col_index++);
2575 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].priority), col_index++);
2576 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].save_status), col_index++);
2577 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].is_locked), col_index++);
2578 _get_table_field_data_int(result, &local_attachment_count, col_index++);
2579 _get_table_field_data_int(result, &local_inline_content_count, col_index++);
2580 _get_table_field_data_time_t(result, &(mail_list_item_from_tbl[i].date_time), col_index++);
2581 _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].previewBodyText, MAX_PREVIEW_TEXT_LENGTH, 1, col_index++);
2582 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_id), col_index++);
2583 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_item_count), col_index++);
2584 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].is_meeting_request), col_index++);
2585 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].message_class), col_index++);
2586 _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].smime_type), col_index++);
2588 mail_list_item_from_tbl[i].has_attachment = ((local_attachment_count - local_inline_content_count)>0)?1:0;
2590 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >> ", count);
2591 EM_PROFILE_END(emstorage_query_mail_list_loop);
2594 sqlite3_free_table(result);
2599 EM_DEBUG_LOG("COUNT [%d]", count);
2602 *result_count = count;
2605 if (result_mail_list)
2606 *result_mail_list = mail_list_item_from_tbl;
2607 *result_count = count;
2610 EM_SAFE_FREE(mail_list_item_from_tbl);
2613 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2616 EM_SAFE_FREE(date_time_string);
2618 if (err_code != NULL)
2621 EM_PROFILE_END(emstorage_query_mail_list_func);
2622 EM_DEBUG_FUNC_END("ret [%d]", ret);
2627 INTERNAL_FUNC int emstorage_query_mail_tbl(const char *conditional_clause, int transaction, emstorage_mail_tbl_t** result_mail_tbl, int *result_count, int *err_code)
2629 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);
2631 if (!conditional_clause) {
2632 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2634 if (err_code != NULL)
2635 *err_code = EMAIL_ERROR_INVALID_PARAM;
2639 int i, col_index = FIELD_COUNT_OF_MAIL_TBL, rc, ret = false, count;
2640 int error = EMAIL_ERROR_NONE;
2641 char **result = NULL, sql_query_string[QUERY_SIZE] = {0, };
2642 emstorage_mail_tbl_t* p_data_tbl = NULL;
2643 sqlite3 *local_db_handle = emstorage_get_db_connection();
2645 EMSTORAGE_START_READ_TRANSACTION(transaction);
2647 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl %s", conditional_clause);
2649 EM_DEBUG_LOG("Query [%s]", sql_query_string);
2651 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2652 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2653 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2656 EM_DEBUG_EXCEPTION("No mail found...");
2658 error= EMAIL_ERROR_MAIL_NOT_FOUND;
2662 EM_DEBUG_LOG("There are [%d] mails.", count);
2663 if (!(p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * count))) {
2664 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
2665 error = EMAIL_ERROR_OUT_OF_MEMORY;
2669 EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2670 for (i = 0; i < count; i++) {
2671 _get_table_field_data_int (result, &(p_data_tbl[i].mail_id), col_index++);
2672 _get_table_field_data_int (result, &(p_data_tbl[i].account_id), col_index++);
2673 _get_table_field_data_int (result, &(p_data_tbl[i].mailbox_id), col_index++);
2674 _get_table_field_data_string(result, &(p_data_tbl[i].mailbox_name), 0, col_index++);
2675 _get_table_field_data_int (result, &(p_data_tbl[i].mailbox_type), col_index++);
2676 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 1, col_index++);
2677 _get_table_field_data_time_t (result, &(p_data_tbl[i].date_time), col_index++);
2678 _get_table_field_data_int (result, &(p_data_tbl[i].server_mail_status), col_index++);
2679 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
2680 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
2681 _get_table_field_data_string(result, &(p_data_tbl[i].message_id), 0, col_index++);
2682 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_from), 1, col_index++);
2683 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_reply), 1, col_index++);
2684 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_to), 1, col_index++);
2685 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_cc), 1, col_index++);
2686 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_bcc), 1, col_index++);
2687 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_return), 1, col_index++);
2688 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_sender), 1, col_index++);
2689 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_recipient), 1, col_index++);
2690 _get_table_field_data_string(result, &(p_data_tbl[i].alias_sender), 1, col_index++);
2691 _get_table_field_data_string(result, &(p_data_tbl[i].alias_recipient), 1, col_index++);
2692 _get_table_field_data_int (result, &(p_data_tbl[i].body_download_status), col_index++);
2693 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
2694 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
2695 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
2696 _get_table_field_data_int (result, &(p_data_tbl[i].mail_size), col_index++);
2697 _get_table_field_data_char (result, &(p_data_tbl[i].flags_seen_field), col_index++);
2698 _get_table_field_data_char (result, &(p_data_tbl[i].flags_deleted_field), col_index++);
2699 _get_table_field_data_char (result, &(p_data_tbl[i].flags_flagged_field), col_index++);
2700 _get_table_field_data_char (result, &(p_data_tbl[i].flags_answered_field), col_index++);
2701 _get_table_field_data_char (result, &(p_data_tbl[i].flags_recent_field), col_index++);
2702 _get_table_field_data_char (result, &(p_data_tbl[i].flags_draft_field), col_index++);
2703 _get_table_field_data_char (result, &(p_data_tbl[i].flags_forwarded_field), col_index++);
2704 _get_table_field_data_int (result, &(p_data_tbl[i].DRM_status), col_index++);
2705 _get_table_field_data_int (result, &(p_data_tbl[i].priority), col_index++);
2706 _get_table_field_data_int (result, &(p_data_tbl[i].save_status), col_index++);
2707 _get_table_field_data_int (result, &(p_data_tbl[i].lock_status), col_index++);
2708 _get_table_field_data_int (result, &(p_data_tbl[i].report_status), col_index++);
2709 _get_table_field_data_int (result, &(p_data_tbl[i].attachment_count), col_index++);
2710 _get_table_field_data_int (result, &(p_data_tbl[i].inline_content_count), col_index++);
2711 _get_table_field_data_int (result, &(p_data_tbl[i].thread_id), col_index++);
2712 _get_table_field_data_int (result, &(p_data_tbl[i].thread_item_count), col_index++);
2713 _get_table_field_data_string(result, &(p_data_tbl[i].preview_text), 1, col_index++);
2714 _get_table_field_data_int (result, &(p_data_tbl[i].meeting_request_status), col_index++);
2715 _get_table_field_data_int (result, &(p_data_tbl[i].message_class), col_index++);
2716 _get_table_field_data_int (result, &(p_data_tbl[i].digest_type), col_index++);
2717 _get_table_field_data_int (result, &(p_data_tbl[i].smime_type), col_index++);
2718 /* check real body file... */
2719 if (p_data_tbl[i].body_download_status) {
2722 if (p_data_tbl[i].file_path_html && strlen(p_data_tbl[i].file_path_html) > 0) {
2723 if (stat(p_data_tbl[i].file_path_html, &buf) == -1) {
2725 p_data_tbl[i].body_download_status = 0;
2728 else if (p_data_tbl[i].file_path_plain && strlen(p_data_tbl[i].file_path_plain) > 0) {
2729 if (stat(p_data_tbl[i].file_path_plain, &buf) == -1){
2731 p_data_tbl[i].body_download_status = 0;
2735 p_data_tbl[i].body_download_status = 0;
2743 sqlite3_free_table(result);
2746 if (result_mail_tbl)
2747 *result_mail_tbl = p_data_tbl;
2748 *result_count = count;
2751 EM_SAFE_FREE(p_data_tbl);
2753 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2756 if (err_code != NULL)
2759 EM_DEBUG_FUNC_END("ret [%d]", ret);
2763 INTERNAL_FUNC int emstorage_query_mailbox_tbl(const char *input_conditional_clause, int input_get_mail_count, int input_transaction, emstorage_mailbox_tbl_t **output_mailbox_list, int *output_mailbox_count)
2765 EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_get_mail_count[%d], input_transaction[%d], output_mailbox_list[%p], output_mailbox_count[%d]", input_conditional_clause, input_get_mail_count, input_transaction, output_mailbox_list, output_mailbox_count);
2771 int error = EMAIL_ERROR_NONE;
2773 int total_count = 0;
2775 char sql_query_string[QUERY_SIZE] = {0, };
2776 char *fields = "MBT.mailbox_id, MBT.account_id, local_yn, MBT.mailbox_name, MBT.mailbox_type, alias, sync_with_server_yn, modifiable_yn, total_mail_count_on_server, has_archived_mails, mail_slot_size, last_sync_time ";
2777 emstorage_mailbox_tbl_t* p_data_tbl = NULL;
2778 DB_STMT hStmt = NULL;
2779 sqlite3 *local_db_handle = emstorage_get_db_connection();
2781 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2783 if (input_get_mail_count == 0) { /* without mail count */
2785 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT %s FROM mail_box_tbl AS MBT %s", fields, input_conditional_clause);
2787 else { /* with read count and total count */
2789 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT %s, total, read FROM mail_box_tbl AS MBT LEFT OUTER JOIN (SELECT mailbox_name, count(mail_id) AS total, SUM(flags_seen_field) AS read FROM mail_tbl GROUP BY mailbox_name) AS MT ON MBT.mailbox_name = MT.mailbox_name %s", fields, input_conditional_clause);
2792 EM_DEBUG_LOG("query[%s]", sql_query_string);
2794 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
2795 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2796 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)))
2798 EM_DEBUG_LOG("result count [%d]", count);
2801 EM_DEBUG_EXCEPTION("Can't find mailbox");
2802 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
2806 if ((p_data_tbl = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t) * count)) == NULL) {
2807 EM_DEBUG_EXCEPTION("malloc failed...");
2808 error = EMAIL_ERROR_OUT_OF_MEMORY;
2812 for (i = 0; i < count; i++) {
2813 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
2814 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
2815 _get_table_field_data_int(result, &(p_data_tbl[i].local_yn), col_index++);
2816 _get_table_field_data_string(result, &(p_data_tbl[i].mailbox_name), 0, col_index++);
2817 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].mailbox_type), col_index++);
2818 _get_table_field_data_string(result, &(p_data_tbl[i].alias), 0, col_index++);
2819 _get_table_field_data_int(result, &(p_data_tbl[i].sync_with_server_yn), col_index++);
2820 _get_table_field_data_int(result, &(p_data_tbl[i].modifiable_yn), col_index++);
2821 _get_table_field_data_int(result, &(p_data_tbl[i].total_mail_count_on_server), col_index++);
2822 _get_table_field_data_int(result, &(p_data_tbl[i].has_archived_mails), col_index++);
2823 _get_table_field_data_int(result, &(p_data_tbl[i].mail_slot_size), col_index++);
2824 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].last_sync_time), col_index++);
2826 if (input_get_mail_count == 1) {
2827 _get_table_field_data_int(result, &(total_count), col_index++);
2828 p_data_tbl[i].total_mail_count_on_local = total_count;
2829 _get_table_field_data_int(result, &(read_count), col_index++);
2830 p_data_tbl[i].unread_count = total_count - read_count; /* return unread count, NOT */
2837 sqlite3_free_table(result);
2839 if (error == EMAIL_ERROR_NONE) {
2840 *output_mailbox_list = p_data_tbl;
2841 *output_mailbox_count = count;
2842 EM_DEBUG_LOG("Mailbox Count [ %d]", count);
2844 else if (p_data_tbl != NULL)
2845 emstorage_free_mailbox(&p_data_tbl, count, NULL);
2847 if (hStmt != NULL) {
2848 rc = sqlite3_finalize(hStmt);
2849 if (rc != SQLITE_OK) {
2850 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
2852 error = EMAIL_ERROR_DB_FAILURE;
2856 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2859 EM_DEBUG_FUNC_END("error [%d]", error);
2863 /* Query series --------------------------------------------------------------*/
2865 INTERNAL_FUNC int emstorage_check_duplicated_account(email_account_t* account, int transaction, int *err_code)
2867 EM_DEBUG_FUNC_BEGIN();
2870 if (err_code != NULL)
2871 *err_code = EMAIL_ERROR_INVALID_PARAM;
2875 int rc = -1, ret = false;
2876 int error = EMAIL_ERROR_NONE;
2879 char sql_query_string[QUERY_SIZE] = {0, };
2881 sqlite3 *local_db_handle = emstorage_get_db_connection();
2882 EMSTORAGE_START_READ_TRANSACTION(transaction);
2884 SNPRINTF(sql_query_string, sizeof(sql_query_string),
2885 "SELECT COUNT(*) FROM mail_account_tbl "
2887 " user_email_address = '%s' AND "
2888 " incoming_server_user_name = '%s' AND "
2889 " incoming_server_type = %d AND "
2890 " incoming_server_address = '%s' AND "
2891 " outgoing_server_user_name = '%s' AND "
2892 " outgoing_server_type = %d AND "
2893 " outgoing_server_address = '%s'; ",
2894 account->user_email_address,
2895 account->incoming_server_user_name, account->incoming_server_type, account->incoming_server_address,
2896 account->outgoing_server_user_name, account->outgoing_server_type, account->outgoing_server_address
2898 EM_DEBUG_LOG("Query[%s]", sql_query_string);
2899 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
2900 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2901 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2903 count = atoi(result[1]);
2904 sqlite3_free_table(result);
2906 EM_DEBUG_LOG("Count of Duplicated Account Information: [%d]", count);
2908 if (count == 0) { /* not duplicated account */
2910 EM_DEBUG_LOG("NOT duplicated account: user_email_address[%s]", account->user_email_address);
2912 else { /* duplicated account */
2914 EM_DEBUG_LOG("The same account already exists. Duplicated account: user_email_address[%s]", account->user_email_address);
2915 error = EMAIL_ERROR_ALREADY_EXISTS;
2920 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2923 if (err_code != NULL)
2926 EM_DEBUG_FUNC_END("ret [%d]", ret);
2931 INTERNAL_FUNC int emstorage_get_account_count(int *count, int transaction, int *err_code)
2933 EM_DEBUG_FUNC_BEGIN();
2936 if (err_code != NULL)
2937 *err_code = EMAIL_ERROR_INVALID_PARAM;
2941 int rc = -1, ret = false;
2942 int error = EMAIL_ERROR_NONE;
2944 DB_STMT hStmt = NULL;
2945 char sql_query_string[QUERY_SIZE] = {0, };
2948 sqlite3 *local_db_handle = emstorage_get_db_connection();
2949 EMSTORAGE_START_READ_TRANSACTION(transaction);
2950 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_account_tbl");
2951 EM_DEBUG_LOG("SQL STMT [ %s ]", sql_query_string);
2952 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
2953 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
2954 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2955 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2957 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2958 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2959 ("sqlite3_step fail:%d", rc));
2961 *count = sqlite3_column_int(hStmt, 0);
2967 if (hStmt != NULL) {
2968 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
2970 rc = sqlite3_finalize(hStmt);
2972 if (rc != SQLITE_OK) {
2973 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d: %s", rc, err_msg);
2974 error = EMAIL_ERROR_DB_FAILURE;
2976 EM_DEBUG_LOG("sqlite3_finalize- %d", rc);
2979 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2982 if (err_code != NULL)
2985 EM_DEBUG_FUNC_END("ret [%d]", ret);
2990 INTERNAL_FUNC int emstorage_get_account_list(int *select_num, emstorage_account_tbl_t** account_list, int transaction, int with_password, int *err_code)
2992 EM_DEBUG_FUNC_BEGIN();
2994 int i = 0, count = 0, rc = -1, ret = false;
2995 int field_index = 0;
2996 int error = EMAIL_ERROR_NONE;
2997 emstorage_account_tbl_t *p_data_tbl = NULL;
2998 char sql_query_string[QUERY_SIZE] = {0, };
2999 char *sql = "SELECT count(*) FROM mail_account_tbl;";
3001 sqlite3 *local_db_handle = NULL;
3002 DB_STMT hStmt = NULL;
3004 if (!select_num || !account_list) {
3005 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, account_list);
3006 if (err_code != NULL)
3007 *err_code = EMAIL_ERROR_INVALID_PARAM;
3011 local_db_handle = emstorage_get_db_connection();
3012 EMSTORAGE_START_READ_TRANSACTION(transaction);
3014 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
3015 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
3016 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
3018 count = atoi(result[1]);
3019 sqlite3_free_table(result);
3021 EM_DEBUG_LOG("count = %d", rc);
3024 EM_DEBUG_EXCEPTION("no account found...");
3025 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3029 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_account_tbl ORDER BY account_id");
3031 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
3033 EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
3034 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3035 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3037 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3038 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3039 ("sqlite3_step fail:%d", rc));
3040 if (rc == SQLITE_DONE) {
3041 EM_DEBUG_EXCEPTION("no account found...");
3043 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3049 if (!(p_data_tbl = (emstorage_account_tbl_t*)malloc(sizeof(emstorage_account_tbl_t) * count))) {
3050 EM_DEBUG_EXCEPTION("malloc failed...");
3051 error = EMAIL_ERROR_OUT_OF_MEMORY;
3054 memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * count);
3055 for (i = 0; i < count; i++) {
3058 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_id), field_index++);
3059 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].account_name), 0, field_index++);
3060 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].logo_icon_path), 0, field_index++);
3061 _get_stmt_field_data_blob(hStmt, &(p_data_tbl[i].user_data), field_index++);
3062 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].user_data_length), field_index++);
3063 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_svc_id), field_index++);
3064 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_status), field_index++);
3065 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_disabled), field_index++);
3066 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].default_mail_slot_size), field_index++);
3067 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_display_name), 0, field_index++);
3068 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_email_address), 0, field_index++);
3069 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].reply_to_address), 0, field_index++);
3070 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].return_address), 0, field_index++);
3071 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].incoming_server_type), field_index++);
3072 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_address), 0, field_index++);
3073 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_port_number), field_index++);
3074 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_user_name), 0, field_index++);
3075 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_password), 0, field_index++);
3076 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_secure_connection), field_index++);
3077 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].retrieval_mode), field_index++);
3078 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].keep_mails_on_pop_server_after_download), field_index++);
3079 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].check_interval), field_index++);
3080 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_download_size), field_index++);
3081 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].outgoing_server_type), field_index++);
3082 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_address), 0, field_index++);
3083 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_port_number), field_index++);
3084 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_user_name), 0, field_index++);
3085 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_password), 0, field_index++);
3086 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_secure_connection), field_index++);
3087 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_need_authentication), field_index++);
3088 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_use_same_authenticator), field_index++);
3089 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.priority), field_index++);
3090 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.keep_local_copy), field_index++);
3091 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_delivery_receipt), field_index++);
3092 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_read_receipt), field_index++);
3093 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.download_limit), field_index++);
3094 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_address), field_index++);
3095 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_subject), field_index++);
3096 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.display_name_from), 0, field_index++);
3097 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.reply_with_body), field_index++);
3098 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.forward_with_files), field_index++);
3099 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_myname_card), field_index++);
3100 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_signature), field_index++);
3101 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.signature), 0, field_index++);
3102 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.add_my_address_to_bcc), field_index++);
3103 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].pop_before_smtp), field_index++);
3104 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_requires_apop), field_index++);
3105 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].smime_type), field_index++);
3106 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].certificate_path), 0, field_index++);
3107 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].cipher_type), field_index++);
3108 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].digest_type), field_index++);
3110 if (with_password == true) {
3111 /* get password from the secure storage */
3112 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3113 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3115 EM_SAFE_FREE(p_data_tbl[i].incoming_server_password);
3116 EM_SAFE_FREE(p_data_tbl[i].outgoing_server_password);
3118 /* get password file name */
3119 if ((error = _get_password_file_name(p_data_tbl[i].account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
3120 EM_DEBUG_EXCEPTION("_get_password_file_name failed. [%d]", error);
3121 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3125 /* read password from secure storage */
3126 if ((error = _read_password_from_secure_storage(recv_password_file_name, &(p_data_tbl[i].incoming_server_password))) < 0 ) {
3127 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage() failed. [%d]", error);
3128 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3131 EM_DEBUG_LOG("recv_password_file_name[%s], incoming_server_password[%s]", recv_password_file_name, p_data_tbl[i].incoming_server_password);
3133 if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl[i].outgoing_server_password))) < 0) {
3134 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage() failed. [%d]", error);
3135 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3138 EM_DEBUG_LOG("send_password_file_name[%s], password[%s]", send_password_file_name, p_data_tbl[i].outgoing_server_password);
3141 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3142 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i, rc);
3143 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3144 ("sqlite3_step fail:%d", rc));
3151 *account_list = p_data_tbl;
3152 *select_num = count;
3153 EM_DEBUG_LOG("COUNT : %d", count);
3155 else if (p_data_tbl != NULL)
3156 emstorage_free_account(&p_data_tbl, count, NULL);
3158 if (hStmt != NULL) {
3159 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
3161 rc = sqlite3_finalize(hStmt);
3163 if (rc != SQLITE_OK) {
3164 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
3165 error = EMAIL_ERROR_DB_FAILURE;
3169 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3172 if (err_code != NULL)
3175 EM_DEBUG_FUNC_END("ret [%d]", ret);
3180 INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(int account_id, char *server_mail_id, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
3182 EM_DEBUG_FUNC_BEGIN("account_id [%d], server_mail_id[%s], mail[%p], transaction[%d], err_code[%p]", account_id, server_mail_id, mail, transaction, err_code);
3184 int ret = false, error = EMAIL_ERROR_NONE, result_count;
3185 char conditional_clause[QUERY_SIZE] = {0, };
3186 emstorage_mail_tbl_t* p_data_tbl = NULL;
3188 if (!server_mail_id || !mail) {
3189 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3190 error = EMAIL_ERROR_INVALID_PARAM;
3194 if (account_id == ALL_ACCOUNT)
3195 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE UPPER(server_mail_id) =UPPER('%s')", server_mail_id);
3197 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE UPPER(server_mail_id) =UPPER('%s') AND account_id = %d", server_mail_id, account_id);
3199 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
3201 if(!emstorage_query_mail_tbl(conditional_clause, transaction, &p_data_tbl, &result_count, &error)) {
3202 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3212 if (err_code != NULL)
3215 EM_DEBUG_FUNC_END("ret [%d]", ret);
3220 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)
3222 int cur_clause = 0, conditional_clause_count = 0, i = 0;
3224 if (account_id < ALL_ACCOUNT) {
3225 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3226 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3230 if (account_id == ALL_ACCOUNT) {
3231 cur_clause += (conditional_clause_count++ == 0)?
3232 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_type not in (3, 5, 7, 8)"):
3233 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_type not in (3, 5, 7, 8)");
3236 cur_clause += (conditional_clause_count++ == 0)?
3237 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE account_id = %d", account_id):
3238 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND account_id = %d", account_id);
3241 if (mailbox_id > 0) {
3242 cur_clause += (conditional_clause_count++ == 0)?
3243 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_id = %d", mailbox_id):
3244 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_id = %d", mailbox_id);
3246 else if(account_id != ALL_ACCOUNT) {
3247 cur_clause += (conditional_clause_count++ == 0)?
3248 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_type not in (3, 5, 7, 8)"):
3249 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_type not in (3, 5, 7, 8)");
3252 if (thread_id > 0) {
3253 cur_clause += (conditional_clause_count++ == 0)?
3254 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_id = %d ", thread_id):
3255 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_id = %d ", thread_id);
3257 else if (thread_id == EMAIL_LIST_TYPE_THREAD) {
3258 cur_clause += (conditional_clause_count++ == 0)?
3259 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_item_count > 0"):
3260 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_item_count > 0");
3262 else if (thread_id == EMAIL_LIST_TYPE_LOCAL) {
3263 cur_clause += (conditional_clause_count++ == 0)?
3264 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE server_mail_status = 0"):
3265 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND server_mail_status = 0");
3267 else if (thread_id == EMAIL_LIST_TYPE_UNREAD) {
3268 cur_clause += (conditional_clause_count++ == 0)?
3269 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_seen_field == 0"):
3270 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_seen_field == 0");
3273 /* EM_DEBUG_LOG("where clause added [%s]", conditional_clause_string); */
3274 if (addr_list && addr_list->address_count > 0) {
3275 if (!addr_list->address_type) {
3276 cur_clause += (conditional_clause_count++ == 0)?
3277 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE email_address_sender IN(\"%s\"", addr_list->address_list[0]):
3278 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND email_address_sender IN(\"%s\"", addr_list->address_list[0]);
3280 for (i = 1; i < addr_list->address_count; i++)
3281 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3283 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3285 cur_clause += (conditional_clause_count++ == 0)?
3286 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]):
3287 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]);
3289 for (i = 1; i < addr_list->address_count; i++)
3290 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3292 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3296 if (input_except_delete_flagged_mails) {
3297 cur_clause += (conditional_clause_count++ == 0)?
3298 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_deleted_field = 0"):
3299 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_deleted_field = 0");
3303 switch (search_type) {
3304 case EMAIL_SEARCH_FILTER_SUBJECT:
3305 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3306 " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3308 case EMAIL_SEARCH_FILTER_SENDER:
3309 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3310 " %s ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3311 ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3313 case EMAIL_SEARCH_FILTER_RECIPIENT:
3314 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3315 " %s ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3316 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3317 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3318 ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value);
3320 case EMAIL_SEARCH_FILTER_ALL:
3321 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3322 " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\' "
3323 " OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3324 " OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3325 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3326 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3329 ")", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value, search_value, search_value);
3334 /* EM_DEBUG_LOG("where clause [%s]", conditional_clause_string); */
3335 static char sorting_str[][50] = {
3336 " ORDER BY date_time DESC", /* case EMAIL_SORT_DATETIME_HIGH: */
3337 " ORDER BY date_time ASC", /* case EMAIL_SORT_DATETIME_LOW: */
3338 " ORDER BY full_address_from DESC, date_time DESC", /* case EMAIL_SORT_SENDER_HIGH: */
3339 " ORDER BY full_address_from ASC, date_time DESC", /* case EMAIL_SORT_SENDER_LOW: */
3340 " ORDER BY full_address_to DESC, date_time DESC", /* case EMAIL_SORT_RCPT_HIGH: */
3341 " ORDER BY full_address_to ASC, date_time DESC", /* case EMAIL_SORT_RCPT_LOW: */
3342 " ORDER BY subject DESC, date_time DESC", /* case EMAIL_SORT_SUBJECT_HIGH: */
3343 " ORDER BY subject ASC, date_time DESC", /* case EMAIL_SORT_SUBJECT_LOW: */
3344 " ORDER BY priority DESC, date_time DESC", /* case EMAIL_SORT_PRIORITY_HIGH: */
3345 " ORDER BY priority ASC, date_time DESC", /* case EMAIL_SORT_PRIORITY_LOW: */
3346 " ORDER BY attachment_count DESC, date_time DESC", /* case EMAIL_SORT_ATTACHMENT_HIGH: */
3347 " ORDER BY attachment_count ASC, date_time DESC", /* case EMAIL_SORT_ATTACHMENT_LOW: */
3348 " ORDER BY lock_status DESC, date_time DESC", /* case EMAIL_SORT_FAVORITE_HIGH: */
3349 " ORDER BY lock_status ASC, date_time DESC", /* case EMAIL_SORT_FAVORITE_LOW: */
3350 " ORDER BY mailbox_name DESC, date_time DESC", /* case EMAIL_SORT_MAILBOX_NAME_HIGH: */
3351 " ORDER BY mailbox_name ASC, date_time DESC" /* case EMAIL_SORT_MAILBOX_NAME_LOW: */
3354 if (sorting < EMAIL_SORT_END && sorting >= 0)
3355 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " %s", sorting_str[sorting]);
3357 EM_DEBUG_LOG(" Invalid Sorting order ");
3360 if (start_index != -1 && limit_count != -1)
3361 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " LIMIT %d, %d", start_index, limit_count);
3368 * emstorage_get_mail_list - Get the mail list information.
3372 INTERNAL_FUNC int emstorage_get_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, int transaction, email_mail_list_item_t** mail_list, int *result_count, int *err_code)
3374 EM_DEBUG_FUNC_BEGIN();
3375 EM_PROFILE_BEGIN(emstorage_get_mail_list_func);
3377 int ret = false, error = EMAIL_ERROR_NONE;
3378 char conditional_clause_string[QUERY_SIZE] = { 0, };
3380 if (account_id < ALL_ACCOUNT) {
3381 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3382 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3384 EM_IF_NULL_RETURN_VALUE(result_count, false);
3386 _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);
3388 EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
3390 if(!emstorage_query_mail_list(conditional_clause_string, transaction, mail_list, result_count, &error)) {
3391 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
3398 if (err_code != NULL)
3401 EM_PROFILE_END(emstorage_get_mail_list_func);
3402 EM_DEBUG_FUNC_END("ret [%d]", ret);
3408 * emstorage_get_mails - Get the Mail list information based on mailbox_name name
3412 INTERNAL_FUNC int emstorage_get_mails(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)
3414 EM_DEBUG_FUNC_BEGIN();
3415 EM_PROFILE_BEGIN(emStorageGetMails);
3417 int count = 0, ret = false, error = EMAIL_ERROR_NONE;
3418 emstorage_mail_tbl_t *p_data_tbl = NULL;
3419 char conditional_clause_string[QUERY_SIZE] = {0, };
3421 EM_IF_NULL_RETURN_VALUE(mail_list, false);
3422 EM_IF_NULL_RETURN_VALUE(result_count, false);
3424 if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3425 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3426 if (err_code != NULL)
3427 *err_code = EMAIL_ERROR_INVALID_PARAM;
3431 _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);
3433 EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
3435 if(!emstorage_query_mail_tbl(conditional_clause_string, transaction, &p_data_tbl, &count, &error)) {
3436 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3444 *mail_list = p_data_tbl;
3445 *result_count = count;
3446 EM_DEBUG_LOG("COUNT : %d", count);
3448 else if (p_data_tbl != NULL)
3449 emstorage_free_mail(&p_data_tbl, count, NULL);
3451 if (err_code != NULL)
3454 EM_PROFILE_END(emStorageGetMails);
3455 EM_DEBUG_FUNC_END("ret [%d]", ret);
3462 * emstorage_get_searched_mail_list - Get the mail list information after filtering
3466 INTERNAL_FUNC int emstorage_get_searched_mail_list(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)
3468 EM_DEBUG_FUNC_BEGIN();
3470 int ret = false, error = EMAIL_ERROR_NONE;
3471 char conditional_clause[QUERY_SIZE] = {0, };
3472 char *temp_search_value = (char *)search_value;
3474 EM_IF_NULL_RETURN_VALUE(mail_list, false);
3475 EM_IF_NULL_RETURN_VALUE(result_count, false);
3477 if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3478 EM_DEBUG_EXCEPTION("select_num[%p], Mail_list[%p]", result_count, mail_list);
3479 error = EMAIL_ERROR_INVALID_PARAM;
3483 temp_search_value = em_replace_all_string(temp_search_value, "_", "\\_");
3484 temp_search_value = em_replace_all_string(temp_search_value, "%", "\\%");
3486 _write_conditional_clause_for_getting_mail_list(account_id, mailbox_id, NULL, thread_id, start_index, limit_count, search_type, temp_search_value, sorting, true, conditional_clause, QUERY_SIZE, &error);
3488 EM_DEBUG_LOG("conditional_clause[%s]", conditional_clause);
3490 if(!emstorage_query_mail_list(conditional_clause, transaction, mail_list, result_count, &error)) {
3491 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
3498 EM_DEBUG_LOG("emstorage_get_searched_mail_list finish off");
3500 if (err_code != NULL)
3503 EM_DEBUG_FUNC_END("ret [%d]", ret);
3508 static int _get_password_file_name(int account_id, char *recv_password_file_name, char *send_password_file_name)
3510 EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
3512 if (account_id <= 0 || !recv_password_file_name || !send_password_file_name) {
3513 EM_DEBUG_EXCEPTION("Invalid parameter");
3514 return EMAIL_ERROR_INVALID_PARAM;
3517 sprintf(recv_password_file_name, ".email_account_%d_recv", account_id);
3518 sprintf(send_password_file_name, ".email_account_%d_send", account_id);
3519 EM_DEBUG_FUNC_END();
3520 return EMAIL_ERROR_NONE;
3523 static int _read_password_from_secure_storage(char *file_name, char **password)
3525 EM_DEBUG_FUNC_BEGIN("file_name[%s], password[%p]", file_name, password);
3527 if (!file_name || !password) {
3528 EM_DEBUG_EXCEPTION("Invalid Parameter");
3529 return EMAIL_ERROR_INVALID_PARAM;
3532 size_t buf_len = 0, read_len = 0;
3533 ssm_file_info_t sfi;
3534 char *temp_password = NULL;
3535 int error_code_from_ssm = 0;
3536 int ret = EMAIL_ERROR_NONE;
3538 if ( (error_code_from_ssm = ssm_getinfo(file_name, &sfi, SSM_FLAG_SECRET_OPERATION, NULL)) < 0) {
3539 EM_DEBUG_EXCEPTION("ssm_getinfo() failed. [%d]", error_code_from_ssm);
3540 ret = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3544 buf_len = sfi.originSize;
3545 EM_DEBUG_LOG("password buf_len[%d]", buf_len);
3546 if ((temp_password = (char *)malloc(buf_len + 1)) == NULL) {
3547 EM_DEBUG_EXCEPTION("malloc failed...");
3548 ret = EMAIL_ERROR_OUT_OF_MEMORY;
3551 memset(temp_password, 0x00, buf_len + 1);
3553 if ( (error_code_from_ssm = ssm_read(file_name, temp_password, buf_len, &read_len, SSM_FLAG_SECRET_OPERATION, NULL)) < 0) {
3554 EM_DEBUG_EXCEPTION("ssm_read() failed. [%d]", error_code_from_ssm);
3555 ret = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3559 EM_DEBUG_LOG("password_file_name[%s], password[%s], originSize[%d], read len[%d]", file_name, temp_password, sfi.originSize, read_len);
3561 *password = temp_password;
3562 temp_password = NULL;
3565 EM_SAFE_FREE(temp_password);
3567 EM_DEBUG_FUNC_END("ret [%d]", ret);
3572 INTERNAL_FUNC int emstorage_get_account_by_id(int account_id, int pulloption, emstorage_account_tbl_t **account, int transaction, int *err_code)
3574 EM_DEBUG_FUNC_BEGIN("account_id[%d], pulloption[%d], account[%p], transaction[%d], err_code[%p]", account_id, pulloption, account, transaction, err_code);
3577 EM_DEBUG_EXCEPTION("account_id[%d], account[%p]", account_id, account);
3578 if (err_code != NULL)
3579 *err_code = EMAIL_ERROR_INVALID_PARAM;
3584 int error = EMAIL_ERROR_NONE;
3585 emstorage_account_tbl_t* p_data_tbl = NULL;
3586 DB_STMT hStmt = NULL;
3587 char sql_query_string[QUERY_SIZE] = {0, };
3590 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3591 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3593 sqlite3 *local_db_handle = emstorage_get_db_connection();
3594 EMSTORAGE_START_READ_TRANSACTION(transaction);
3596 /* Make query string */
3597 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
3598 sql_len = strlen(sql_query_string);
3600 if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
3601 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
3602 "incoming_server_type,"
3603 "incoming_server_address,"
3604 "user_email_address,"
3605 "incoming_server_user_name,"
3607 "incoming_server_port_number,"
3608 "incoming_server_secure_connection,"
3609 "outgoing_server_type,"
3610 "outgoing_server_address,"
3611 "outgoing_server_port_number,"
3612 "outgoing_server_need_authentication,"
3613 "outgoing_server_secure_connection,"
3614 "outgoing_server_user_name,"
3615 "user_display_name,"
3619 "keep_mails_on_pop_server_after_download,"
3620 "auto_download_size,"
3621 "outgoing_server_use_same_authenticator,"
3623 "incoming_server_requires_apop,"
3629 sql_len = strlen(sql_query_string);
3632 if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME) {
3633 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " account_name, ");
3634 sql_len = strlen(sql_query_string);
3637 /* get from secure storage, not from db */
3638 if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
3639 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
3642 "req_delivery_receipt,"
3647 "display_name_from,"
3649 "forward_with_files,"
3653 "add_my_address_to_bcc,"
3656 "default_mail_slot_size,"
3662 sql_len = strlen(sql_query_string);
3665 /* dummy value, FROM WHERE clause */
3666 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "0 FROM mail_account_tbl WHERE account_id = %d", account_id);
3669 EM_DEBUG_LOG("query = [%s]", sql_query_string);
3671 /* execute a sql and count rows */
3672 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
3673 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3674 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3676 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3677 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3678 ("sqlite3_step fail:%d", rc));
3680 if (rc == SQLITE_DONE) {
3681 EM_DEBUG_EXCEPTION("no matched account found...");
3682 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3686 /* Assign query result to structure */
3687 if (!(p_data_tbl = (emstorage_account_tbl_t *)malloc(sizeof(emstorage_account_tbl_t) * 1))) {
3688 EM_DEBUG_EXCEPTION("malloc failed...");
3689 error = EMAIL_ERROR_OUT_OF_MEMORY;
3693 memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * 1);
3696 if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
3697 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_type), col_index++);
3698 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_address), 0, col_index++);
3699 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_email_address), 0, col_index++);
3700 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_user_name), 0, col_index++);
3701 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->retrieval_mode), col_index++);
3702 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_port_number), col_index++);
3703 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_secure_connection), col_index++);
3704 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->outgoing_server_type), col_index++);
3705 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_address), 0, col_index++);
3706 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_port_number), col_index++);
3707 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_need_authentication), col_index++);
3708 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_secure_connection), col_index++);
3709 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_user_name), 0, col_index++);
3710 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_display_name), 0, col_index++);
3711 _get_stmt_field_data_string(hStmt, &(p_data_tbl->reply_to_address), 0, col_index++);
3712 _get_stmt_field_data_string(hStmt, &(p_data_tbl->return_address), 0, col_index++);
3713 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
3714 _get_stmt_field_data_int(hStmt, &(p_data_tbl->keep_mails_on_pop_server_after_download), col_index++);
3715 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_download_size), col_index++);
3716 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_use_same_authenticator), col_index++);
3717 _get_stmt_field_data_int(hStmt, &(p_data_tbl->pop_before_smtp), col_index++);
3718 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_requires_apop), col_index++);
3719 _get_stmt_field_data_string(hStmt, &(p_data_tbl->logo_icon_path), 0, col_index++);
3720 _get_stmt_field_data_blob(hStmt, &p_data_tbl->user_data, col_index++);
3721 _get_stmt_field_data_int(hStmt, &p_data_tbl->user_data_length, col_index++);
3722 _get_stmt_field_data_int(hStmt, &(p_data_tbl->check_interval), col_index++);
3723 _get_stmt_field_data_int(hStmt, &(p_data_tbl->sync_status), col_index++);
3726 if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME)
3727 _get_stmt_field_data_string(hStmt, &(p_data_tbl->account_name), 0, col_index++);
3729 if (pulloption & EMAIL_ACC_GET_OPT_PASSWORD) {
3730 /* get password file name */
3731 if ((error = _get_password_file_name(p_data_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
3732 EM_DEBUG_EXCEPTION("_get_password_file_name failed. [%d]", error);
3736 /* read password from secure storage */
3737 if ((error = _read_password_from_secure_storage(recv_password_file_name, &(p_data_tbl->incoming_server_password))) < 0) {
3738 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed. [%d]", error);
3742 EM_DEBUG_LOG("recv_password_file_name[%s], password[%s]", recv_password_file_name, p_data_tbl->incoming_server_password);
3744 if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl->outgoing_server_password))) < 0) {
3745 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed. [%d]", error);
3748 EM_DEBUG_LOG("send_password_file_name[%s], password[%s]", send_password_file_name, p_data_tbl->outgoing_server_password);
3751 if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
3752 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.priority), col_index++);
3753 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.keep_local_copy), col_index++);
3754 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_delivery_receipt), col_index++);
3755 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_read_receipt), col_index++);
3756 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.download_limit), col_index++);
3757 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_address), col_index++);
3758 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_subject), col_index++);
3759 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.display_name_from), 0, col_index++);
3760 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.reply_with_body), col_index++);
3761 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.forward_with_files), col_index++);
3762 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_myname_card), col_index++);
3763 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_signature), col_index++);
3764 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.signature), 0, col_index++);
3765 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.add_my_address_to_bcc), col_index++);
3766 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_svc_id), col_index++);
3767 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->sync_disabled), col_index++);
3768 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->default_mail_slot_size), col_index++);
3769 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->smime_type), col_index++);
3770 _get_stmt_field_data_string(hStmt, &(p_data_tbl->certificate_path), 0, col_index++);
3771 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->cipher_type), col_index++);
3772 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->digest_type), col_index++);
3779 *account = p_data_tbl;
3782 emstorage_free_account((emstorage_account_tbl_t **)&p_data_tbl, 1, NULL);
3784 if (hStmt != NULL) {
3785 rc = sqlite3_finalize(hStmt);
3786 if (rc != SQLITE_OK) {
3787 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
3788 error = EMAIL_ERROR_DB_FAILURE;
3792 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3795 if (err_code != NULL)
3798 EM_DEBUG_FUNC_END("ret [%d]", ret);
3802 INTERNAL_FUNC int emstorage_get_password_length_of_account(int account_id, int *password_length, int *err_code)
3804 EM_DEBUG_FUNC_BEGIN("account_id[%d], password_length[%p], err_code[%p]", account_id, password_length, err_code);
3806 if (account_id <= 0 || password_length == NULL) {
3807 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3808 if (err_code != NULL)
3809 *err_code = EMAIL_ERROR_INVALID_PARAM;
3814 int error = EMAIL_ERROR_NONE;
3815 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3816 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3817 char *temp_password = NULL;
3820 /* get password file name */
3821 if ((error = _get_password_file_name(account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
3822 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
3826 /* read password from secure storage */
3827 if ((error = _read_password_from_secure_storage(recv_password_file_name, &temp_password)) < 0 || !temp_password) {
3828 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
3832 *password_length = strlen(temp_password);
3834 EM_DEBUG_LOG("recv_password_file_name[%s], *password_length[%d]", recv_password_file_name, *password_length);
3839 EM_SAFE_FREE(temp_password);
3841 if (err_code != NULL)
3844 EM_DEBUG_FUNC_END("ret [%d]", ret);
3848 INTERNAL_FUNC int emstorage_update_account(int account_id, emstorage_account_tbl_t* account_tbl, int transaction, int *err_code)
3850 EM_DEBUG_FUNC_BEGIN("account_id[%d], account[%p], transaction[%d], err_code[%p]", account_id, account_tbl, transaction, err_code);
3852 if (account_id < FIRST_ACCOUNT_ID || !account_tbl) {
3853 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3854 if (err_code != NULL)
3855 *err_code = EMAIL_ERROR_INVALID_PARAM;
3859 int error = EMAIL_ERROR_NONE;
3860 int rc, ret = false;
3862 DB_STMT hStmt = NULL;
3863 char sql_query_string[QUERY_SIZE] = {0, };
3864 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3865 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
3867 sqlite3 *local_db_handle = emstorage_get_db_connection();
3868 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
3870 SNPRINTF(sql_query_string, sizeof(sql_query_string),
3871 "UPDATE mail_account_tbl SET"
3873 ", logo_icon_path = ?"
3875 ", user_data_length = ?"
3876 ", account_svc_id = ?"
3878 ", sync_disabled = ?"
3879 ", default_mail_slot_size = ?"
3880 ", user_display_name = ?"
3881 ", user_email_address = ?"
3882 ", reply_to_address = ?"
3883 ", return_address = ?"
3884 ", incoming_server_type = ?"
3885 ", incoming_server_address = ?"
3886 ", incoming_server_port_number = ?"
3887 ", incoming_server_user_name = ?"
3888 ", incoming_server_secure_connection = ?"
3889 ", retrieval_mode = ?"
3890 ", keep_mails_on_pop_server_after_download = ?"
3891 ", check_interval = ?"
3892 ", auto_download_size = ?"
3893 ", outgoing_server_type = ?"
3894 ", outgoing_server_address = ?"
3895 ", outgoing_server_port_number = ?"
3896 ", outgoing_server_user_name = ?"
3897 ", outgoing_server_secure_connection = ?"
3898 ", outgoing_server_need_authentication = ?"
3899 ", outgoing_server_use_same_authenticator = ?"
3901 ", keep_local_copy = ?"
3902 ", req_delivery_receipt = ?"
3903 ", req_read_receipt = ?"
3904 ", download_limit = ?"
3905 ", block_address = ?"
3906 ", block_subject = ?"
3907 ", display_name_from = ?"
3908 ", reply_with_body = ?"
3909 ", forward_with_files = ?"
3910 ", add_myname_card = ?"
3911 ", add_signature = ?"
3913 ", add_my_address_to_bcc = ?"
3914 ", pop_before_smtp = ?"
3915 ", incoming_server_requires_apop = ?"
3917 ", certificate_path = ?"
3920 " WHERE account_id = ?");
3922 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
3923 EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
3924 EM_DEBUG_LOG("SQL[%s]", sql_query_string);
3925 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3926 ("sqlite3_prepare fail:(%d) %s", rc, sqlite3_errmsg(local_db_handle)));
3930 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
3931 _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
3932 _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
3933 _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
3934 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
3935 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
3936 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
3937 _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
3938 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
3939 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
3940 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
3941 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
3942 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
3943 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
3944 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
3945 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
3946 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
3947 _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
3948 _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
3949 _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
3950 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
3951 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
3952 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
3953 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
3954 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
3955 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
3956 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
3957 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
3958 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
3959 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
3960 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
3961 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
3962 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
3963 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
3964 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
3965 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
3966 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
3967 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
3968 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
3969 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
3970 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
3971 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
3972 _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
3973 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
3974 _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
3975 _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
3976 _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
3977 _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
3978 _bind_stmt_field_data_int(hStmt, i++, account_id);
3980 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3981 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
3982 ("sqlite3_step fail:%d", rc));
3983 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3984 ("sqlite3_step fail:%d", rc));
3986 /* validate account existence */
3987 rc = sqlite3_changes(local_db_handle);
3989 EM_DEBUG_EXCEPTION(" no matched account found...");
3991 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3995 /* get password file name */
3996 if ((error = _get_password_file_name(account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
3997 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4001 /* save passwords to the secure storage */
4002 EM_DEBUG_LOG("save to the secure storage : recv_file[%s], recv_pass[%s], send_file[%s], send_pass[%s]", recv_password_file_name, account_tbl->incoming_server_password, send_password_file_name, account_tbl->outgoing_server_password);
4003 if (account_tbl->incoming_server_password && (strlen(account_tbl->incoming_server_password) > 0)) {
4004 if (ssm_write_buffer(account_tbl->incoming_server_password, strlen(account_tbl->incoming_server_password), recv_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
4005 EM_DEBUG_EXCEPTION(" ssm_write_buffer failed -recv incoming_server_password : file[%s]", recv_password_file_name);
4006 error = EMAIL_ERROR_SYSTEM_FAILURE;
4011 if (account_tbl->outgoing_server_password && (strlen(account_tbl->outgoing_server_password) > 0)) {
4012 if (ssm_write_buffer(account_tbl->outgoing_server_password, strlen(account_tbl->outgoing_server_password), send_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
4013 EM_DEBUG_EXCEPTION(" ssm_write_buffer failed -send password : file[%s]", send_password_file_name);
4014 error = EMAIL_ERROR_SYSTEM_FAILURE;
4019 if (!emstorage_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_tbl->account_id, 0, NULL, 0))
4020 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event[ NOTI_ACCOUNT_UPDATE] : Notification Failed >>> ");
4025 if (hStmt != NULL) {
4026 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
4028 rc = sqlite3_finalize(hStmt);
4029 if (rc != SQLITE_OK) {
4030 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
4031 error = EMAIL_ERROR_DB_FAILURE;
4035 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
4039 if (err_code != NULL)
4042 EM_DEBUG_FUNC_END("ret [%d]", ret);
4046 INTERNAL_FUNC int emstorage_set_field_of_accounts_with_integer_value(int account_id, char *field_name, int value, int transaction)
4048 EM_DEBUG_FUNC_BEGIN("account_id[%d], field_name[%s], value[%d], transaction[%d]", account_id, field_name, value, transaction);
4049 int error = EMAIL_ERROR_NONE;
4052 char sql_query_string[QUERY_SIZE] = {0, };
4053 sqlite3 *local_db_handle = NULL;
4055 if (!account_id || !field_name) {
4056 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4057 return EMAIL_ERROR_INVALID_PARAM;
4060 local_db_handle = emstorage_get_db_connection();
4062 /* Write query string */
4063 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_account_tbl SET %s = %d WHERE account_id = %d", field_name, value, account_id);
4065 EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
4068 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
4069 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
4070 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4071 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4072 if (sqlite3_changes(local_db_handle) == 0)
4073 EM_DEBUG_LOG("no mail matched...");
4077 result = (error == EMAIL_ERROR_NONE) ? true : false;
4078 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, result, error);
4081 if (!emstorage_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_id, 0, field_name, value))
4082 EM_DEBUG_EXCEPTION("emstorage_notify_storage_event failed : NOTI_ACCOUNT_UPDATE [%s,%d]", field_name, value);
4084 EM_DEBUG_FUNC_END("error [%d]", error);
4088 INTERNAL_FUNC int emstorage_get_sync_status_of_account(int account_id, int *result_sync_status,int *err_code)
4090 EM_DEBUG_FUNC_BEGIN("account_id[%d], result_sync_status [%p], err_code[%p]", account_id, result_sync_status, err_code);
4092 if(!result_sync_status) {
4093 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4094 if (err_code != NULL)
4095 *err_code = EMAIL_ERROR_INVALID_PARAM;
4099 int error = EMAIL_ERROR_NONE, rc, ret = false, sync_status, count, i, col_index;
4100 char sql_query_string[QUERY_SIZE] = {0, };
4101 char **result = NULL;
4102 sqlite3 *local_db_handle = emstorage_get_db_connection();
4105 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl WHERE account_id = %d", account_id);
4107 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl");
4109 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
4110 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
4111 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4114 EM_DEBUG_EXCEPTION("no matched account found...");
4115 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4120 *result_sync_status = 0;
4122 for(i = 0; i < count; i++) {
4123 _get_table_field_data_int(result, &sync_status, col_index++);
4124 *result_sync_status |= sync_status;
4127 EM_DEBUG_LOG("sync_status [%d]", sync_status);
4129 sqlite3_free_table(result);
4137 if (err_code != NULL)
4140 EM_DEBUG_FUNC_END("ret [%d]", ret);
4144 INTERNAL_FUNC int emstorage_update_sync_status_of_account(int account_id, email_set_type_t set_operator, int sync_status, int transaction, int *err_code)
4146 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);
4148 int error = EMAIL_ERROR_NONE, rc, ret = false, set_value = sync_status, result_sync_status;
4149 char sql_query_string[QUERY_SIZE] = {0, };
4150 sqlite3 *local_db_handle = emstorage_get_db_connection();
4152 if(set_operator != SET_TYPE_SET && account_id) {
4153 if(!emstorage_get_sync_status_of_account(account_id, &result_sync_status, &error)) {
4154 EM_DEBUG_EXCEPTION("emstorage_get_sync_status_of_account failed [%d]", error);
4157 switch(set_operator) {
4158 case SET_TYPE_UNION :
4159 set_value = result_sync_status | set_value;
4161 case SET_TYPE_MINUS :
4162 set_value = result_sync_status & (~set_value);
4165 EM_DEBUG_EXCEPTION("EMAIL_ERROR_NOT_SUPPORTED [%d]", set_operator);
4166 error = EMAIL_ERROR_NOT_SUPPORTED;
4169 EM_DEBUG_LOG("set_value [%d]", set_value);
4172 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
4175 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE account_id = %d", set_value, account_id);
4177 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE incoming_server_type <> 5", set_value);
4179 EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
4181 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
4182 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4183 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4185 rc = sqlite3_changes(local_db_handle);
4188 EM_DEBUG_EXCEPTION("no matched account found...");
4189 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4193 if (!emstorage_notify_storage_event(NOTI_ACCOUNT_UPDATE_SYNC_STATUS, account_id, 0, NULL, 0))
4194 EM_DEBUG_EXCEPTION("emstorage_notify_storage_event[NOTI_ACCOUNT_UPDATE_SYNC_STATUS] : Notification failed");
4199 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
4203 if (err_code != NULL)
4206 EM_DEBUG_FUNC_END("ret [%d]", ret);
4210 INTERNAL_FUNC int emstorage_add_account(emstorage_account_tbl_t* account_tbl, int transaction, int *err_code)
4212 EM_DEBUG_FUNC_BEGIN("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4215 EM_DEBUG_EXCEPTION("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4216 if (err_code != NULL)
4217 *err_code = EMAIL_ERROR_INVALID_PARAM;
4221 int rc = -1, ret = false;
4222 int error = EMAIL_ERROR_NONE;
4223 int error_from_ssm = 0;
4224 DB_STMT hStmt = NULL;
4225 char sql_query_string[QUERY_SIZE] = {0, };
4226 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4227 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4229 sqlite3 *local_db_handle = emstorage_get_db_connection();
4231 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
4233 char *sql = "SELECT max(rowid) FROM mail_account_tbl;";
4234 char **result = NULL;
4236 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
4237 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
4238 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
4240 if (NULL==result[1]) rc = 1;
4241 else rc = atoi(result[1])+1;
4242 sqlite3_free_table(result);
4245 account_tbl->account_id = rc;
4247 if ((error = _get_password_file_name(account_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4248 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4252 EM_DEBUG_LOG(" >>>> ACCOUNT_ID [ %d ] ", account_tbl->account_id);
4253 SNPRINTF(sql_query_string, sizeof(sql_query_string),
4254 "INSERT INTO mail_account_tbl VALUES "
4256 " ? " /* account_id */
4257 " , ? " /* account_name */
4258 " , ? " /* logo_icon_path */
4259 " , ? " /* user_data */
4260 " , ? " /* user_data_length */
4261 " , ? " /* account_svc_id */
4262 " , ? " /* sync_status */
4263 " , ? " /* sync_disabled */
4264 " , ? " /* default_mail_slot_size */
4265 " , ? " /* user_display_name */
4266 " , ? " /* user_email_address */
4267 " , ? " /* reply_to_address */
4268 " , ? " /* return_address */
4269 " , ? " /* incoming_server_type */
4270 " , ? " /* incoming_server_address */
4271 " , ? " /* incoming_server_port_number */
4272 " , ? " /* incoming_server_user_name */
4273 " , ? " /* incoming_server_password */
4274 " , ? " /* incoming_server_secure_connection */
4275 " , ? " /* retrieval_mode */
4276 " , ? " /* keep_mails_on_pop_server_after_download */
4277 " , ? " /* check_interval */
4278 " , ? " /* auto_download_size */
4279 " , ? " /* outgoing_server_type */
4280 " , ? " /* outgoing_server_address */
4281 " , ? " /* outgoing_server_port_number */
4282 " , ? " /* outgoing_server_user_name */
4283 " , ? " /* outgoing_server_password */
4284 " , ? " /* outgoing_server_secure_connection */
4285 " , ? " /* outgoing_server_need_authentication */
4286 " , ? " /* outgoing_server_use_same_authenticator */
4287 " , ? " /* priority */
4288 " , ? " /* keep_local_copy */
4289 " , ? " /* req_delivery_receipt */
4290 " , ? " /* req_read_receipt */
4291 " , ? " /* download_limit */
4292 " , ? " /* block_address */
4293 " , ? " /* block_subject */
4294 " , ? " /* display_name_from */
4295 " , ? " /* reply_with_body */
4296 " , ? " /* forward_with_files */
4297 " , ? " /* add_myname_card */
4298 " , ? " /* add_signature */
4299 " , ? " /* signature */
4300 " , ? " /* add_my_address_to_bcc */
4301 " , ? " /* pop_before_smtp */
4302 " , ? " /* incoming_server_requires_apop */
4303 " , ? " /* smime_type */
4304 " , ? " /* certificate_path */
4305 " , ? " /* cipher_type */
4306 " , ? " /* digest_type */
4309 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
4310 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4311 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4313 EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
4316 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_id);
4317 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4318 _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4319 _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4320 _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4321 _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4322 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4323 _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4324 _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4325 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4326 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4327 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4328 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4329 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4330 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4331 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
4332 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4333 _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
4334 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
4335 _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
4336 _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
4337 _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
4338 _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
4339 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
4340 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4341 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
4342 _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
4343 _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
4344 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
4345 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
4346 _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
4347 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
4348 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
4349 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
4350 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
4351 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
4352 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
4353 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
4354 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4355 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
4356 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
4357 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
4358 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
4359 _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
4360 _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
4361 _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
4362 _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
4363 _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
4364 _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
4365 _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
4366 _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
4368 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4370 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
4371 ("sqlite3_step fail:%d", rc));
4372 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4373 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
4376 /* save passwords to the secure storage */
4377 EM_DEBUG_LOG("save to the secure storage : recv_file[%s], send_file[%s]", recv_password_file_name, send_password_file_name);
4378 if ( (error_from_ssm = ssm_write_buffer(account_tbl->incoming_server_password, strlen(account_tbl->incoming_server_password), recv_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL)) < 0) {
4379 EM_DEBUG_EXCEPTION("ssm_write_buffer failed [%d] - recv password : file[%s]", error_from_ssm, recv_password_file_name);
4380 error = EMAIL_ERROR_SYSTEM_FAILURE;
4383 if ( (error_from_ssm = ssm_write_buffer(account_tbl->outgoing_server_password, strlen(account_tbl->outgoing_server_password), send_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL)) < 0) {
4384 EM_DEBUG_EXCEPTION("ssm_write_buffer failed [%d] - send password : file[%s]", error_from_ssm, send_password_file_name);
4385 error = EMAIL_ERROR_SYSTEM_FAILURE;
4391 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
4393 if (!emstorage_notify_storage_event(NOTI_ACCOUNT_ADD, account_tbl->account_id, 0, NULL, 0))
4394 EM_DEBUG_EXCEPTION("emstorage_notify_storage_event[NOTI_ACCOUNT_ADD] : Notification failed");
4398 if (hStmt != NULL) {
4399 rc = sqlite3_finalize(hStmt);
4400 if (rc != SQLITE_OK) {
4401 EM_DEBUG_LOG("sqlite3_finalize failed [%d]", rc);
4402 error = EMAIL_ERROR_DB_FAILURE;
4406 EM_DEBUG_LOG("hStmt is NULL!!!");
4410 if (err_code != NULL)
4413 EM_DEBUG_FUNC_END("ret [%d]", ret);
4418 INTERNAL_FUNC int emstorage_delete_account(int account_id, int transaction, int *err_code)
4420 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
4422 if (account_id < FIRST_ACCOUNT_ID) {
4423 EM_DEBUG_EXCEPTION(" account_id[%d]", account_id);
4425 if (err_code != NULL)
4426 *err_code = EMAIL_ERROR_INVALID_PARAM;
4430 int rc = -1, ret = false;
4431 int error = EMAIL_ERROR_NONE;
4432 sqlite3 *local_db_handle = emstorage_get_db_connection();
4433 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
4435 /* TODO : delete password files - file names can be obtained from db or a rule that makes a name */
4436 DB_STMT hStmt = NULL;
4437 char sql_query_string[QUERY_SIZE] = {0, };
4438 char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4439 char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4441 /* get password file name */
4442 if ((error = _get_password_file_name(account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4443 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4447 /* delete from db */
4448 memset(sql_query_string, 0x00, sizeof(sql_query_string));
4449 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_account_tbl WHERE account_id = %d", account_id);
4451 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
4452 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
4453 ("sqlite3_exec fail:%d", rc));
4454 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4455 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4457 /* validate account existence */
4458 rc = sqlite3_changes(local_db_handle);
4460 EM_DEBUG_EXCEPTION(" no matched account found...");
4461 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4465 /* delete from secure storage */
4466 if (ssm_delete_file(recv_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
4467 EM_DEBUG_EXCEPTION(" ssm_delete_file failed -recv password : file[%s]", recv_password_file_name);
4468 error = EMAIL_ERROR_SYSTEM_FAILURE;
4471 if (ssm_delete_file(send_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
4472 EM_DEBUG_EXCEPTION(" ssm_delete_file failed -send password : file[%s]", send_password_file_name);
4473 error = EMAIL_ERROR_SYSTEM_FAILURE;
4480 if (hStmt != NULL) {
4481 rc = sqlite3_finalize(hStmt);
4482 if (rc != SQLITE_OK) {
4483 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
4484 error = EMAIL_ERROR_DB_FAILURE;
4488 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
4491 if (err_code != NULL)
4494 EM_DEBUG_FUNC_END("ret [%d]", ret);
4499 INTERNAL_FUNC int emstorage_free_account(emstorage_account_tbl_t** account_list, int count, int *err_code)
4501 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%d], err_code[%p]", account_list, count, err_code);
4504 int error = EMAIL_ERROR_NONE;
4507 if (!account_list || !*account_list) {
4508 EM_DEBUG_EXCEPTION("account_list[%p], count[%d]", account_list, count);
4509 error = EMAIL_ERROR_INVALID_PARAM;
4513 emstorage_account_tbl_t* p = *account_list;
4516 for (; i < count; i++) {
4517 EM_SAFE_FREE(p[i].account_name);
4518 EM_SAFE_FREE(p[i].incoming_server_address);
4519 EM_SAFE_FREE(p[i].user_email_address);
4520 EM_SAFE_FREE(p[i].user_data);
4521 EM_SAFE_FREE(p[i].incoming_server_user_name);
4522 EM_SAFE_FREE(p[i].incoming_server_password);
4523 EM_SAFE_FREE(p[i].outgoing_server_address);
4524 EM_SAFE_FREE(p[i].outgoing_server_user_name);
4525 EM_SAFE_FREE(p[i].outgoing_server_password);
4526 EM_SAFE_FREE(p[i].user_display_name);
4527 EM_SAFE_FREE(p[i].reply_to_address);
4528 EM_SAFE_FREE(p[i].return_address);
4529 EM_SAFE_FREE(p[i].logo_icon_path);
4530 EM_SAFE_FREE(p[i].options.display_name_from);
4531 EM_SAFE_FREE(p[i].options.signature);
4532 EM_SAFE_FREE(p[i].certificate_path);
4536 *account_list = NULL;
4542 if (err_code != NULL)
4545 EM_DEBUG_FUNC_END("ret [%d]", ret);
4549 INTERNAL_FUNC int emstorage_get_mailbox_count(int account_id, int local_yn, int *count, int transaction, int *err_code)
4551 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);
4553 if ((account_id < FIRST_ACCOUNT_ID) || (count == NULL)) {
4554 EM_DEBUG_EXCEPTION(" account_list[%d], local_yn[%d], count[%p]", account_id, local_yn, count);
4556 if (err_code != NULL)
4557 *err_code = EMAIL_ERROR_INVALID_PARAM;
4561 int rc = -1, ret = false;
4562 int error = EMAIL_ERROR_NONE;
4563 char sql_query_string[QUERY_SIZE] = {0, };
4565 sqlite3 *local_db_handle = emstorage_get_db_connection();
4566 EMSTORAGE_START_READ_TRANSACTION(transaction);
4568 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);
4572 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
4573 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
4574 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4576 *count = atoi(result[1]);
4577 sqlite3_free_table(result);
4584 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4587 if (err_code != NULL)
4590 EM_DEBUG_FUNC_END("ret [%d]", ret);
4594 INTERNAL_FUNC int emstorage_get_mailbox_list(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)
4596 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);
4598 if (!select_num || !mailbox_list) {
4599 EM_DEBUG_EXCEPTION("Invalid parameters");
4601 if (err_code != NULL)
4602 *err_code = EMAIL_ERROR_INVALID_PARAM;
4608 int error = EMAIL_ERROR_NONE;
4609 char conditional_clause_string[QUERY_SIZE] = {0, };
4611 if (account_id == ALL_ACCOUNT) {
4612 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
4613 SNPRINTF(conditional_clause_string + strlen(conditional_clause_string), sizeof(conditional_clause_string)-(strlen(conditional_clause_string)+1), " WHERE local_yn = %d ", local_yn);
4616 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d ", account_id);
4617 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
4618 SNPRINTF(conditional_clause_string + strlen(conditional_clause_string), sizeof(conditional_clause_string)-(strlen(conditional_clause_string)+1), " AND local_yn = %d ", local_yn);
4621 switch (sort_type) {
4622 case EMAIL_MAILBOX_SORT_BY_NAME_ASC :
4623 SNPRINTF(conditional_clause_string + strlen(conditional_clause_string), sizeof(conditional_clause_string)-(strlen(conditional_clause_string)+1), " ORDER BY mailbox_name ASC");
4626 case EMAIL_MAILBOX_SORT_BY_NAME_DSC :
4627 SNPRINTF(conditional_clause_string + strlen(conditional_clause_string), sizeof(conditional_clause_string)-(strlen(conditional_clause_string)+1), " ORDER BY mailbox_name DESC");
4630 case EMAIL_MAILBOX_SORT_BY_TYPE_ASC :
4631 SNPRINTF(conditional_clause_string + strlen(conditional_clause_string), sizeof(conditional_clause_string)-(strlen(conditional_clause_string)+1), " ORDER BY mailbox_type ASC");
4634 case EMAIL_MAILBOX_SORT_BY_TYPE_DSC :
4635 SNPRINTF(conditional_clause_string + strlen(conditional_clause_string), sizeof(conditional_clause_string)-(strlen(conditional_clause_string)+1), " ORDER BY mailbox_type DEC");
4639 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
4641 if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
4642 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4650 if (err_code != NULL)
4653 EM_DEBUG_FUNC_END("ret [%d]", ret);
4657 INTERNAL_FUNC int emstorage_get_mailbox_list_ex(int account_id, int local_yn, int with_count, int *select_num, emstorage_mailbox_tbl_t** mailbox_list, int transaction, int *err_code)
4659 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);
4662 int error = EMAIL_ERROR_NONE;
4663 int where_clause_count = 0;
4664 char conditional_clause_string[QUERY_SIZE] = {0, };
4666 if (!select_num || !mailbox_list) {
4667 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4668 error = EMAIL_ERROR_INVALID_PARAM;
4672 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL) {
4673 SNPRINTF(conditional_clause_string + strlen(conditional_clause_string), sizeof(conditional_clause_string)-(strlen(conditional_clause_string)+1), "WHERE local_yn = %d ", local_yn);
4674 where_clause_count++;
4677 if (account_id > 0) {
4678 if (where_clause_count == 0) {
4679 SNPRINTF(conditional_clause_string + strlen(conditional_clause_string), sizeof(conditional_clause_string)-(strlen(conditional_clause_string)+1), " WHERE ");
4680 SNPRINTF(conditional_clause_string + strlen(conditional_clause_string), sizeof(conditional_clause_string)-(strlen(conditional_clause_string)+1), " account_id = %d ", account_id);
4683 SNPRINTF(conditional_clause_string + strlen(conditional_clause_string), sizeof(conditional_clause_string)-(strlen(conditional_clause_string)+1), " AND account_id = %d ", account_id);
4686 SNPRINTF(conditional_clause_string + strlen(conditional_clause_string), sizeof(conditional_clause_string)-(strlen(conditional_clause_string)+1), " ORDER BY MBT.mailbox_name ");
4687 EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
4689 if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, 1, 1, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
4690 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4698 if (err_code != NULL)
4701 EM_DEBUG_FUNC_END("ret [%d]", ret);
4705 INTERNAL_FUNC int emstorage_get_child_mailbox_list(int account_id, char *parent_mailbox_name, int *select_num, emstorage_mailbox_tbl_t** mailbox_list, int transaction, int *err_code)
4707 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);
4708 if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list) {
4709 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);
4711 if (err_code != NULL)
4712 *err_code = EMAIL_ERROR_INVALID_PARAM;
4717 int error = EMAIL_ERROR_NONE;
4718 char conditional_clause_string[QUERY_SIZE] = {0, };
4720 if (parent_mailbox_name)
4721 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND UPPER(mailbox_name) LIKE UPPER('%s/%%') ORDER BY mailbox_name", account_id, parent_mailbox_name);
4723 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND (mailbox_name NOT LIKE '%%/%%') ORDER BY mailbox_name", account_id);
4725 EM_DEBUG_LOG("conditional_clause_string", conditional_clause_string);
4727 if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
4728 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4736 if (err_code != NULL)
4739 EM_DEBUG_FUNC_END("ret [%d]", ret);
4743 INTERNAL_FUNC int emstorage_get_mailbox_by_modifiable_yn(int account_id, int local_yn, int *select_num, emstorage_mailbox_tbl_t** mailbox_list, int transaction, int *err_code)
4745 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);
4746 if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list) {
4747 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4749 if (err_code != NULL)
4750 *err_code = EMAIL_ERROR_INVALID_PARAM;
4756 int error = EMAIL_ERROR_NONE;
4757 char conditional_clause_string[QUERY_SIZE] = {0, };
4759 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND modifiable_yn = 0 ORDER BY mailbox_name", account_id);
4760 EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
4762 if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
4763 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4771 if (err_code != NULL)
4774 EM_DEBUG_FUNC_END("ret [%d]", ret);
4778 INTERNAL_FUNC int emstorage_stamp_last_sync_time_of_mailbox(int input_mailbox_id, int input_transaction)
4780 EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_transaction [%d]", input_mailbox_id, input_transaction);
4782 int result_code = false;
4783 int error = EMAIL_ERROR_NONE;
4785 time_t current_time = 0;
4786 char sql_query_string[QUERY_SIZE] = {0, };
4787 sqlite3 *local_db_handle = NULL;
4789 if (!input_mailbox_id ) {
4790 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4791 return EMAIL_ERROR_INVALID_PARAM;
4794 time(¤t_time);
4796 local_db_handle = emstorage_get_db_connection();
4798 EMSTORAGE_START_WRITE_TRANSACTION(input_transaction, error);
4800 SNPRINTF(sql_query_string, sizeof(sql_query_string),
4801 "UPDATE mail_box_tbl SET"
4802 " last_sync_time = %d"
4803 " WHERE mailbox_id = %d"
4805 , input_mailbox_id);
4807 EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
4809 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
4810 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4811 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4815 if(error == EMAIL_ERROR_NONE)
4818 EMSTORAGE_FINISH_WRITE_TRANSACTION(input_transaction, result_code, error);
4821 EM_DEBUG_FUNC_END("error [%d]", error);
4825 INTERNAL_FUNC int emstorage_get_mailbox_by_name(int account_id, int local_yn, char *mailbox_name, emstorage_mailbox_tbl_t** result_mailbox, int transaction, int *err_code)
4827 EM_DEBUG_FUNC_BEGIN("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);
4828 EM_PROFILE_BEGIN(profile_emstorage_get_mailbox_by_name);
4830 if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !result_mailbox) {
4831 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], mailbox_name[%s], result_mailbox[%p]", account_id, local_yn, mailbox_name, result_mailbox);
4833 if (err_code != NULL)
4834 *err_code = EMAIL_ERROR_INVALID_PARAM;
4839 int error = EMAIL_ERROR_NONE;
4840 int result_count = 0;
4841 char conditional_clause_string[QUERY_SIZE] = {0, };
4843 if(strcmp(mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
4844 if (!(*result_mailbox = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t)))) {
4845 EM_DEBUG_EXCEPTION("malloc failed...");
4846 error = EMAIL_ERROR_OUT_OF_MEMORY;
4850 (*result_mailbox)->mailbox_id = 0;
4851 (*result_mailbox)->account_id = account_id;
4852 (*result_mailbox)->local_yn = 1;
4853 (*result_mailbox)->mailbox_name = EM_SAFE_STRDUP(mailbox_name);
4854 (*result_mailbox)->mailbox_type = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;
4855 (*result_mailbox)->alias = EM_SAFE_STRDUP(mailbox_name);
4856 (*result_mailbox)->sync_with_server_yn = 0;
4857 (*result_mailbox)->modifiable_yn = 1;
4858 (*result_mailbox)->total_mail_count_on_server = 1;
4859 (*result_mailbox)->has_archived_mails = 0;
4860 (*result_mailbox)->mail_slot_size = 0x0FFFFFFF;
4865 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND mailbox_name = '%s'", account_id, mailbox_name);
4867 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND local_yn = %d AND mailbox_name = '%s'", account_id, local_yn, mailbox_name);
4869 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
4871 if( (error = emstorage_query_mailbox_tbl(conditional_clause_string, 0, transaction, result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
4872 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
4881 if (err_code != NULL)
4884 EM_PROFILE_END(profile_emstorage_get_mailbox_by_name);
4885 EM_DEBUG_FUNC_END("ret [%d]", ret);
4889 INTERNAL_FUNC int emstorage_get_mailbox_by_mailbox_type(int account_id, email_mailbox_type_e mailbox_type, emstorage_mailbox_tbl_t** mailbox_name, int transaction, int *err_code)
4891 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);
4893 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_name) {
4895 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
4897 if (err_code != NULL)
4898 *err_code = EMAIL_ERROR_INVALID_PARAM;
4902 int rc, ret = false;
4903 int error = EMAIL_ERROR_NONE;
4904 emstorage_mailbox_tbl_t *p_data_tbl = NULL;
4905 emstorage_account_tbl_t *account = NULL;
4906 DB_STMT hStmt = NULL;
4907 char sql_query_string[QUERY_SIZE] = {0,};
4908 char *fields = "mailbox_id, account_id, local_yn, mailbox_name, mailbox_type, alias, sync_with_server_yn, modifiable_yn, total_mail_count_on_server, has_archived_mails, mail_slot_size ";
4910 sqlite3 *local_db_handle = emstorage_get_db_connection();
4911 EMSTORAGE_START_READ_TRANSACTION(transaction);
4913 /* validate account */
4914 /* Check whether the account exists. */
4915 if (!emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME, &account, true, &error) || !account) {
4916 EM_DEBUG_EXCEPTION(" emstorage_get_account_by_id failed - %d", error);
4921 emstorage_free_account(&account, 1, NULL);
4923 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT %s FROM mail_box_tbl WHERE account_id = %d AND mailbox_type = %d ", fields, account_id, mailbox_type);
4925 EM_DEBUG_LOG("query = [%s]", sql_query_string);
4928 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
4929 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)));
4932 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4933 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
4935 if (rc == SQLITE_DONE) {
4936 EM_DEBUG_EXCEPTION(" no matched mailbox_name found...");
4937 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
4942 if (!(p_data_tbl = (emstorage_mailbox_tbl_t*)malloc(sizeof(emstorage_mailbox_tbl_t)))) {
4943 EM_DEBUG_EXCEPTION(" malloc failed...");
4944 error = EMAIL_ERROR_OUT_OF_MEMORY;
4948 memset(p_data_tbl, 0x00, sizeof(emstorage_mailbox_tbl_t));
4952 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
4953 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
4954 _get_stmt_field_data_int(hStmt, &(p_data_tbl->local_yn), col_index++);
4955 _get_stmt_field_data_string(hStmt, &(p_data_tbl->mailbox_name), 0, col_index++);
4956 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->mailbox_type), col_index++);
4957 _get_stmt_field_data_string(hStmt, &(p_data_tbl->alias), 0, col_index++);
4958 _get_stmt_field_data_int(hStmt, &(p_data_tbl->sync_with_server_yn), col_index++);
4959 _get_stmt_field_data_int(hStmt, &(p_data_tbl->modifiable_yn), col_index++);
4960 _get_stmt_field_data_int(hStmt, &(p_data_tbl->total_mail_count_on_server), col_index++);
4961 _get_stmt_field_data_int(hStmt, &(p_data_tbl->has_archived_mails), col_index++);
4962 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_slot_size), col_index++);
4968 *mailbox_name = p_data_tbl;
4969 else if (p_data_tbl != NULL)
4970 emstorage_free_mailbox(&p_data_tbl, 1, NULL);
4972 if (hStmt != NULL) {
4973 rc = sqlite3_finalize(hStmt);
4974 if (rc != SQLITE_OK) {
4975 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
4976 error = EMAIL_ERROR_DB_FAILURE;
4980 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4983 if (err_code != NULL)
4986 EM_DEBUG_FUNC_END("ret [%d]", ret);
4990 INTERNAL_FUNC int emstorage_get_mailbox_by_id(int input_mailbox_id, emstorage_mailbox_tbl_t** output_mailbox)
4992 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], output_mailbox[%p]", input_mailbox_id, output_mailbox);
4994 if (input_mailbox_id <= 0 || !output_mailbox) {
4995 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4996 return EMAIL_ERROR_INVALID_PARAM;
4999 int ret = EMAIL_ERROR_NONE;
5000 int result_count = 0;
5001 char conditional_clause_string[QUERY_SIZE] = {0, };
5003 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE mailbox_id = %d", input_mailbox_id);
5005 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5007 if( (ret = emstorage_query_mailbox_tbl(conditional_clause_string, false, false, output_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5008 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", ret);
5014 EM_DEBUG_FUNC_END("ret [%d]", ret);
5018 INTERNAL_FUNC int emstorage_get_mailbox_id_by_mailbox_type(int account_id, email_mailbox_type_e mailbox_type, int *mailbox_id, int transaction, int *err_code)
5020 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);
5021 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_id) {
5022 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_id[%p]", account_id, mailbox_type, mailbox_id);
5023 if (err_code != NULL)
5024 *err_code = EMAIL_ERROR_INVALID_PARAM;
5028 int rc, ret = false;
5029 int error = EMAIL_ERROR_NONE;
5030 emstorage_account_tbl_t* account = NULL;
5031 DB_STMT hStmt = NULL;
5032 char sql_query_string[QUERY_SIZE] = {0, };
5034 sqlite3 *local_db_handle = emstorage_get_db_connection();
5035 EMSTORAGE_START_READ_TRANSACTION(transaction);
5037 /* Check whether the account exists. */
5038 if (!emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME, &account, true, &error) || !account) {
5039 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5044 emstorage_free_account(&account, 1, NULL);
5046 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);
5048 EM_DEBUG_LOG("query = [%s]", sql_query_string);
5051 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
5053 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5054 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5057 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5058 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5059 ("sqlite3_step fail:%d", rc));
5061 if (rc == SQLITE_DONE) {
5062 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5063 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5067 _get_stmt_field_data_int(hStmt, mailbox_id, 0);
5072 if (hStmt != NULL) {
5073 rc = sqlite3_finalize(hStmt);
5074 if (rc != SQLITE_OK) {
5075 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5076 error = EMAIL_ERROR_DB_FAILURE;
5080 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5083 if (err_code != NULL)
5086 EM_DEBUG_FUNC_END("ret [%d]", ret);
5090 INTERNAL_FUNC int emstorage_get_mailbox_name_by_mailbox_type(int account_id, email_mailbox_type_e mailbox_type, char **mailbox_name, int transaction, int *err_code)
5092 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);
5093 if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_name) {
5094 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
5095 if (err_code != NULL)
5096 *err_code = EMAIL_ERROR_INVALID_PARAM;
5100 int rc, ret = false;
5101 int error = EMAIL_ERROR_NONE;
5102 emstorage_account_tbl_t* account = NULL;
5103 DB_STMT hStmt = NULL;
5104 char sql_query_string[QUERY_SIZE] = {0, };
5106 sqlite3 *local_db_handle = emstorage_get_db_connection();
5107 EMSTORAGE_START_READ_TRANSACTION(transaction);
5109 /* Check whether the account exists. */
5110 if (!emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME, &account, true, &error) || !account) {
5111 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5116 emstorage_free_account(&account, 1, NULL);
5118 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);
5120 EM_DEBUG_LOG("query = [%s]", sql_query_string);
5123 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
5125 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5126 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5129 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5130 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5131 ("sqlite3_step fail:%d", rc));
5133 if (rc == SQLITE_DONE) {
5134 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5135 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5139 _get_stmt_field_data_string(hStmt, mailbox_name, 0, 0);
5144 if (hStmt != NULL) {
5145 rc = sqlite3_finalize(hStmt);
5146 if (rc != SQLITE_OK) {
5147 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5148 error = EMAIL_ERROR_DB_FAILURE;
5152 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5155 if (err_code != NULL)
5158 EM_DEBUG_FUNC_END("ret [%d]", ret);
5162 INTERNAL_FUNC int emstorage_update_mailbox_modifiable_yn(int account_id, int local_yn, char *mailbox_name, int modifiable_yn, int transaction, int *err_code)
5164 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);
5165 int rc, ret = false;
5166 int error = EMAIL_ERROR_NONE;
5167 char sql_query_string[QUERY_SIZE] = {0, };
5169 sqlite3 *local_db_handle = emstorage_get_db_connection();
5171 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5173 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5174 "UPDATE mail_box_tbl SET"
5175 " modifiable_yn = %d"
5176 " WHERE account_id = %d"
5177 " AND local_yn = %d"
5178 " AND mailbox_name = '%s'"
5183 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5184 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5185 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5190 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5193 if (err_code != NULL)
5196 EM_DEBUG_FUNC_END("ret [%d]", ret);
5202 INTERNAL_FUNC int emstorage_update_mailbox_total_count(int account_id, int input_mailbox_id, int total_count_on_server, int transaction, int *err_code)
5204 EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], total_count_on_server[%d], transaction[%d], err_code[%p]", account_id, input_mailbox_id, total_count_on_server, transaction, err_code);
5205 int rc, ret = false;
5206 int error = EMAIL_ERROR_NONE;
5207 char sql_query_string[QUERY_SIZE] = {0, };
5209 if (account_id <= 0 || input_mailbox_id <= 0) {
5210 EM_DEBUG_EXCEPTION("account_id[%d], input_mailbox_id[%d]", account_id, input_mailbox_id);
5212 if (err_code != NULL)
5213 *err_code = EMAIL_ERROR_INVALID_PARAM;
5216 sqlite3 *local_db_handle = emstorage_get_db_connection();
5218 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5220 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5221 "UPDATE mail_box_tbl SET"
5222 " total_mail_count_on_server = %d"
5223 " WHERE account_id = %d"
5224 " AND mailbox_id = %d"
5225 , total_count_on_server
5227 , input_mailbox_id);
5228 EM_DEBUG_LOG("query[%s]", sql_query_string);
5229 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5230 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5231 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5236 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5239 if (err_code != NULL)
5242 EM_DEBUG_FUNC_END("ret [%d]", ret);
5248 INTERNAL_FUNC int emstorage_update_mailbox(int account_id, int local_yn, int input_mailbox_id, emstorage_mailbox_tbl_t* result_mailbox, int transaction, int *err_code)
5250 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);
5252 if (account_id < FIRST_ACCOUNT_ID || input_mailbox_id <= 0 || !result_mailbox) {
5253 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);
5255 if (err_code != NULL)
5256 *err_code = EMAIL_ERROR_INVALID_PARAM;
5260 int rc, ret = false;
5261 int error = EMAIL_ERROR_NONE;
5262 char sql_query_string[QUERY_SIZE] = {0, };
5263 DB_STMT hStmt = NULL;
5266 sqlite3 *local_db_handle = emstorage_get_db_connection();
5267 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5269 if (local_yn != -1) {
5270 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5271 "UPDATE mail_box_tbl SET"
5273 ", mailbox_name = ?"
5274 ", mailbox_type = ?"
5276 ", sync_with_server_yn = ?"
5277 ", modifiable_yn= ?"
5278 ", mail_slot_size= ?"
5279 ", total_mail_count_on_server = ?"
5280 " WHERE account_id = %d"
5281 " AND local_yn = %d"
5282 " AND mailbox_id = '%d'"
5285 , input_mailbox_id);
5288 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5289 "UPDATE mail_box_tbl SET"
5291 ", mailbox_name = ?"
5292 ", mailbox_type = ?"
5294 ", sync_with_server_yn = ?"
5295 ", modifiable_yn= ?"
5296 ", mail_slot_size= ?"
5297 ", total_mail_count_on_server = ?"
5298 " WHERE account_id = %d"
5299 " AND mailbox_id = '%d'"
5301 , input_mailbox_id);
5306 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
5307 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5308 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5310 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_id);
5311 _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);
5312 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_type);
5313 _bind_stmt_field_data_string(hStmt, i++, (char *)result_mailbox->alias ? result_mailbox->alias : "", 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
5314 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->sync_with_server_yn);
5315 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->modifiable_yn);
5316 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mail_slot_size);
5317 _bind_stmt_field_data_int(hStmt, i++, result_mailbox->total_mail_count_on_server);
5320 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5321 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
5322 ("sqlite3_step fail:%d", rc));
5323 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5324 ("sqlite3_step fail:%d", rc));
5329 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5332 if (hStmt != NULL) {
5333 rc = sqlite3_finalize(hStmt);
5334 if (rc != SQLITE_OK) {
5335 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5336 error = EMAIL_ERROR_DB_FAILURE;
5340 if (err_code != NULL)
5343 EM_DEBUG_FUNC_END("ret [%d]", ret);
5348 INTERNAL_FUNC int emstorage_update_mailbox_type(int account_id, int local_yn, char *mailbox_name, email_mailbox_type_e new_mailbox_type, int transaction, int *err_code)
5350 EM_DEBUG_FUNC_BEGIN("account_id[%d], local_yn[%d], mailbox_name[%s], new_mailbox_type[%d], transaction[%d], err_code[%p]", account_id, local_yn, mailbox_name, new_mailbox_type, transaction, err_code);
5352 if (account_id < FIRST_ACCOUNT_ID || !mailbox_name) {
5353 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], mailbox_name[%s]", account_id, local_yn, mailbox_name);
5355 if (err_code != NULL)
5356 *err_code = EMAIL_ERROR_INVALID_PARAM;
5360 int rc, ret = false;
5361 int error = EMAIL_ERROR_NONE;
5362 char sql_query_string[QUERY_SIZE] = {0, };
5364 sqlite3 *local_db_handle = emstorage_get_db_connection();
5365 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5367 EM_DEBUG_LOG("emstorage_update_mailbox_type");
5369 DB_STMT hStmt = NULL;
5372 /* Update mail_box_tbl */
5373 if (local_yn != -1) {
5374 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5375 "UPDATE mail_box_tbl SET"
5377 " WHERE account_id = %d"
5378 " AND local_yn = %d"
5379 " AND mailbox_name = '%s'"
5385 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5386 "UPDATE mail_box_tbl SET"
5388 " WHERE account_id = %d"
5389 " AND mailbox_name = '%s'"
5394 EM_DEBUG_LOG("SQL(%s)", sql_query_string);
5396 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
5397 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5398 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5400 _bind_stmt_field_data_int(hStmt, i++, new_mailbox_type);
5403 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5404 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
5405 ("sqlite3_step fail:%d", rc));
5406 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5407 ("sqlite3_step fail:%d", rc));
5409 if (hStmt != NULL) {
5410 rc = sqlite3_finalize(hStmt);
5411 if (rc != SQLITE_OK) {
5412 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5413 error = EMAIL_ERROR_DB_FAILURE;
5418 /* Update mail_tbl */
5420 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5421 "UPDATE mail_tbl SET"
5423 " WHERE account_id = %d"
5424 " AND mailbox_name = '%s'"
5428 EM_DEBUG_LOG("SQL[%s]", sql_query_string);
5430 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
5431 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5432 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5434 _bind_stmt_field_data_int(hStmt, i++, new_mailbox_type);
5437 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5438 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
5439 ("sqlite3_step fail:%d", rc));
5440 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5441 ("sqlite3_step fail:%d", rc));
5446 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5449 if (hStmt != NULL) {
5450 rc = sqlite3_finalize(hStmt);
5451 if (rc != SQLITE_OK) {
5452 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5453 error = EMAIL_ERROR_DB_FAILURE;
5457 if (err_code != NULL)
5460 EM_DEBUG_FUNC_END("ret [%d]", ret);
5465 INTERNAL_FUNC int emstorage_add_mailbox(emstorage_mailbox_tbl_t* mailbox_tbl, int transaction, int *err_code)
5467 EM_DEBUG_FUNC_BEGIN("mailbox_tbl[%p], transaction[%d], err_code[%p]", mailbox_tbl, transaction, err_code);
5470 if (err_code != NULL)
5471 *err_code = EMAIL_ERROR_INVALID_PARAM;
5475 int rc, ret = false;
5476 int error = EMAIL_ERROR_NONE;
5477 DB_STMT hStmt = NULL;
5478 char sql_query_string[QUERY_SIZE] = {0,};
5479 char **result = NULL;
5480 time_t current_time;
5481 sqlite3 *local_db_handle = emstorage_get_db_connection();
5482 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5484 EM_SAFE_STRCPY(sql_query_string, "SELECT max(rowid) FROM mail_box_tbl;");
5486 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5487 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
5488 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5490 time(¤t_time);
5492 if (NULL == result[1])
5495 rc = atoi(result[1]) + 1;
5496 sqlite3_free_table(result);
5498 memset(sql_query_string, 0, sizeof(char) * QUERY_SIZE);
5500 mailbox_tbl->mailbox_id = rc;
5502 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5503 "INSERT INTO mail_box_tbl VALUES "
5504 "( ?" /* mailbox_id */
5505 ", ?" /* account_id */
5506 ", ?" /* local_yn */
5507 ", ?" /* mailbox_name */
5508 ", ?" /* mailbox_type */
5510 ", ?" /* sync_with_server_yn */
5511 ", ?" /* modifiable_yn */
5512 ", ?" /* total_mail_count_on_server */
5513 ", ?" /* has_archived_mails */
5514 ", ?" /* mail_slot_size */
5515 ", ? )");/* last_sync_time */
5518 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
5519 EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
5520 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5521 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5525 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_id);
5526 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->account_id);
5527 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->local_yn);
5528 _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
5529 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_type);
5530 _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->alias, 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
5531 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->sync_with_server_yn);
5532 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->modifiable_yn);
5533 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->total_mail_count_on_server);
5534 _bind_stmt_field_data_int(hStmt, col_index++, 0);
5535 _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mail_slot_size);
5536 _bind_stmt_field_data_int(hStmt, col_index++, current_time);
5539 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5540 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5541 ("sqlite3_step fail:%dn", rc));
5546 if (hStmt != NULL) {
5547 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
5549 rc = sqlite3_finalize(hStmt);
5550 if (rc != SQLITE_OK) {
5551 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
5552 error = EMAIL_ERROR_DB_FAILURE;
5556 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5559 if (error == EMAIL_ERROR_NONE && !emstorage_notify_storage_event(NOTI_MAILBOX_ADD, mailbox_tbl->account_id, mailbox_tbl->mailbox_id, mailbox_tbl->mailbox_name, 0))
5560 EM_DEBUG_EXCEPTION("emstorage_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
5562 if (err_code != NULL)
5565 EM_DEBUG_FUNC_END("ret [%d]", ret);
5569 INTERNAL_FUNC int emstorage_set_all_mailbox_modifiable_yn(int account_id, int modifiable_yn, int transaction, int *err_code)
5571 EM_DEBUG_FUNC_BEGIN("account_id[%d], modifiable_yn[%d], err_code[%p]", account_id, modifiable_yn, err_code);
5573 if (account_id < FIRST_ACCOUNT_ID) {
5575 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
5577 if (err_code != NULL)
5578 *err_code = EMAIL_ERROR_INVALID_PARAM;
5581 int rc, ret = false;
5582 int error = EMAIL_ERROR_NONE;
5583 char sql_query_string[QUERY_SIZE] = {0,};
5585 sqlite3 *local_db_handle = emstorage_get_db_connection();
5586 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5589 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET modifiable_yn = %d WHERE account_id = %d", modifiable_yn, account_id);
5591 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5592 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5593 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5595 rc = sqlite3_changes(local_db_handle);
5597 EM_DEBUG_EXCEPTION("All mailbox_name modifiable_yn set to 0 already");
5603 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5606 if (err_code != NULL)
5608 EM_DEBUG_FUNC_END("ret [%d]", ret);
5614 INTERNAL_FUNC int emstorage_delete_mailbox(int account_id, int local_yn, int input_mailbox_id, int transaction, int *err_code)
5616 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);
5618 if (account_id < FIRST_ACCOUNT_ID) {
5619 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
5621 if (err_code != NULL)
5622 *err_code = EMAIL_ERROR_INVALID_PARAM;
5626 int rc, ret = false;
5627 int error = EMAIL_ERROR_NONE;
5628 char sql_query_string[QUERY_SIZE] = {0, };
5630 sqlite3 *local_db_handle = emstorage_get_db_connection();
5631 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
5634 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_box_tbl WHERE account_id = %d ", account_id);
5636 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);
5638 if (input_mailbox_id > 0) { /* 0 means all mailbox_name */
5639 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(1+ strlen(sql_query_string)), "AND mailbox_id = %d", input_mailbox_id);
5642 EM_DEBUG_LOG("mailbox sql_query_string [%s]", sql_query_string);
5643 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
5644 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5645 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5647 rc = sqlite3_changes(local_db_handle);
5649 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
5650 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5657 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
5660 if(error == EMAIL_ERROR_NONE) {
5661 if (!emstorage_notify_storage_event(NOTI_MAILBOX_DELETE, account_id, input_mailbox_id, NULL, 0))
5662 EM_DEBUG_EXCEPTION("emstorage_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
5665 if (err_code != NULL)
5668 EM_DEBUG_FUNC_END("ret [%d]", ret);
5672 INTERNAL_FUNC int emstorage_free_mailbox(emstorage_mailbox_tbl_t** mailbox_list, int count, int *err_code)
5674 EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d], err_code[%p]", mailbox_list, count, err_code);
5677 int error = EMAIL_ERROR_NONE;
5680 if (!mailbox_list || !*mailbox_list) {
5681 EM_DEBUG_EXCEPTION(" mailbox_list[%p], count[%d]", mailbox_list, count);
5683 error = EMAIL_ERROR_INVALID_PARAM;
5687 emstorage_mailbox_tbl_t* p = *mailbox_list;
5690 for (; i < count; i++) {
5691 EM_SAFE_FREE(p[i].mailbox_name);
5692 EM_SAFE_FREE(p[i].alias);
5695 EM_SAFE_FREE(p); *mailbox_list = NULL;
5701 if (err_code != NULL)
5704 EM_DEBUG_FUNC_END("ret [%d]", ret);
5708 INTERNAL_FUNC int emstorage_get_count_read_mail_uid(int account_id, char *mailbox_name, int *count, int transaction, int *err_code)
5710 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);
5712 if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !count) {
5713 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], count[%p], exist[%p]", account_id, mailbox_name, count);
5715 if (err_code != NULL)
5716 *err_code = EMAIL_ERROR_INVALID_PARAM;
5720 int rc = -1, ret = false;
5721 int error = EMAIL_ERROR_NONE;
5722 char sql_query_string[QUERY_SIZE] = {0, };
5724 sqlite3 *local_db_handle = emstorage_get_db_connection();
5725 EMSTORAGE_START_READ_TRANSACTION(transaction);
5726 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, mailbox_name);
5727 EM_DEBUG_LOG(">>> SQL [ %s ] ", sql_query_string);
5731 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5732 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
5733 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5735 *count = atoi(result[1]);
5736 sqlite3_free_table(result);
5741 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5744 if (err_code != NULL)
5747 EM_DEBUG_FUNC_END("ret [%d]", ret);
5753 INTERNAL_FUNC int emstorage_check_read_mail_uid(int account_id, char *mailbox_name, char *uid, int *exist, int transaction, int *err_code)
5755 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);
5757 if (account_id < FIRST_ACCOUNT_ID || !uid || !exist) {
5758 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], uid[%p], exist[%p]", account_id, mailbox_name , uid, exist);
5760 if (err_code != NULL)
5761 *err_code = EMAIL_ERROR_INVALID_PARAM;
5765 int rc = -1, ret = false;
5766 int error = EMAIL_ERROR_NONE;
5767 char sql_query_string[QUERY_SIZE] = {0, };
5768 sqlite3 *local_db_handle = emstorage_get_db_connection();
5769 EMSTORAGE_START_READ_TRANSACTION(transaction);
5772 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 s_uid = '%s' ", account_id, mailbox_name, uid);
5775 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_read_mail_uid_tbl WHERE account_id = %d AND s_uid = '%s' ", account_id, uid);
5780 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5781 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
5782 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5784 *exist = atoi(result[1]);
5785 sqlite3_free_table(result);
5795 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5798 if (err_code != NULL)
5801 EM_DEBUG_FUNC_END("ret [%d]", ret);
5805 INTERNAL_FUNC int emstorage_get_downloaded_mail(int mail_id, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
5807 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], err_code[%p]", mail_id, mail, err_code);
5810 EM_DEBUG_EXCEPTION(" mail_id[%d], mail[%p]", mail_id, mail);
5811 if (err_code != NULL)
5812 *err_code = EMAIL_ERROR_INVALID_PARAM;
5816 int rc, ret = false, temp_rule;
5817 int error = EMAIL_ERROR_NONE;
5818 DB_STMT hStmt = NULL;
5819 char sql_query_string[QUERY_SIZE] = {0, };
5821 sqlite3 *local_db_handle = emstorage_get_db_connection();
5822 EMSTORAGE_START_READ_TRANSACTION(transaction);
5824 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE local_uid = %d", mail_id);
5826 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
5827 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
5829 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5830 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5833 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5834 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5835 ("sqlite3_step fail:%d", rc));
5837 *mail = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
5838 if (*mail == NULL) {
5839 error = EMAIL_ERROR_OUT_OF_MEMORY;
5840 EM_DEBUG_EXCEPTION("Memory allocation for mail failed.");
5844 memset(*mail, 0x00, sizeof(emstorage_mail_tbl_t));
5846 _get_stmt_field_data_int(hStmt, &((*mail)->account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5847 _get_stmt_field_data_int(hStmt, &((*mail)->mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5848 _get_stmt_field_data_string(hStmt, &((*mail)->mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5849 _get_stmt_field_data_int(hStmt, &((*mail)->mail_id), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5850 _get_stmt_field_data_string(hStmt, &((*mail)->server_mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5851 _get_stmt_field_data_string(hStmt, &((*mail)->server_mail_id), 0, S_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5852 _get_stmt_field_data_int(hStmt, &((*mail)->mail_size), DATA1_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5853 _get_stmt_field_data_int(hStmt, &temp_rule, FLAG_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5855 (*mail)->server_mail_status = 1;
5860 if (hStmt != NULL) {
5861 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
5862 rc = sqlite3_finalize(hStmt);
5863 if (rc != SQLITE_OK) {
5864 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
5865 error = EMAIL_ERROR_DB_FAILURE;
5869 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5872 if (err_code != NULL)
5875 EM_DEBUG_FUNC_END("ret [%d]", ret);
5879 INTERNAL_FUNC int emstorage_get_downloaded_list(int account_id, int mailbox_id, emstorage_read_mail_uid_tbl_t** read_mail_uid, int *count, int transaction, int *err_code)
5881 EM_PROFILE_BEGIN(emStorageGetDownloadList);
5882 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);
5883 if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid || !count) {
5884 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_id[%s], read_mail_uid[%p], count[%p]", account_id, mailbox_id, read_mail_uid, count);
5886 if (err_code != NULL)
5887 *err_code = EMAIL_ERROR_INVALID_PARAM;
5891 int rc, ret = false;
5892 int error = EMAIL_ERROR_NONE;
5894 DB_STMT hStmt = NULL;
5895 char sql_query_string[QUERY_SIZE] = {0, };
5897 emstorage_read_mail_uid_tbl_t* p_data_tbl = NULL;
5900 sqlite3 *local_db_handle = emstorage_get_db_connection();
5901 EMSTORAGE_START_READ_TRANSACTION(transaction);
5904 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);
5906 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
5908 EM_DEBUG_LOG(" sql_query_string : %s", sql_query_string);
5912 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
5913 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
5914 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5915 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5918 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5919 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5920 ("sqlite3_step fail:%d", rc));
5923 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL); */
5924 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL), rc);
5925 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
5926 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5928 sqlite3_free_table(result);
5930 EM_DEBUG_LOG("No mail found in mail_read_mail_uid_tbl");
5936 if (!(p_data_tbl = (emstorage_read_mail_uid_tbl_t*)malloc(sizeof(emstorage_read_mail_uid_tbl_t) * *count))) {
5937 EM_DEBUG_EXCEPTION(" malloc failed...");
5938 error = EMAIL_ERROR_OUT_OF_MEMORY;
5942 memset(p_data_tbl, 0x00, sizeof(emstorage_read_mail_uid_tbl_t)* *count);
5944 for (i = 0; i < *count; ++i) {
5945 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5946 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id),LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5947 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].local_uid), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5948 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5949 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].s_uid), 0, S_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5950 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].data1), DATA1_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5951 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].data2), 0, DATA2_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5952 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag), FLAG_IDX_IN_MAIL_READ_MAIL_UID_TBL);
5954 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5955 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5956 ("sqlite3_step fail:%d", rc));
5963 *read_mail_uid = p_data_tbl;
5964 else if (p_data_tbl)
5965 emstorage_free_read_mail_uid(&p_data_tbl, *count, NULL);
5967 if (hStmt != NULL) {
5968 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
5969 rc = sqlite3_finalize(hStmt);
5970 if (rc != SQLITE_OK) {
5971 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
5973 error = EMAIL_ERROR_DB_FAILURE;
5977 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5980 if (err_code != NULL)
5983 EM_PROFILE_END(emStorageGetDownloadList);
5984 EM_DEBUG_FUNC_END("ret [%d]", ret);
5988 INTERNAL_FUNC int emstorage_get_downloaded_mail_size(int account_id, char *mailbox_id, int local_uid, char *mailbox_name, char *uid, int *mail_size, int transaction, int *err_code)
5990 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);
5992 if (account_id < FIRST_ACCOUNT_ID || !mail_size) {
5993 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);
5995 if (err_code != NULL)
5996 *err_code = EMAIL_ERROR_INVALID_PARAM;
6000 int rc, ret = false;
6001 int error = EMAIL_ERROR_NONE;
6002 DB_STMT hStmt = NULL;
6003 char sql_query_string[QUERY_SIZE] = {0, };
6006 sqlite3 *local_db_handle = emstorage_get_db_connection();
6007 EMSTORAGE_START_READ_TRANSACTION(transaction);
6010 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6011 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6012 "WHERE account_id = %d "
6013 "AND mailbox_id = '%s' "
6014 "AND local_uid = %d "
6015 "AND mailbox_name = '%s' "
6017 account_id, mailbox_id, local_uid, mailbox_name, uid);
6020 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6021 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6022 "WHERE account_id = %d "
6023 "AND mailbox_id = '%s' "
6024 "AND local_uid = %d "
6026 account_id, mailbox_id, local_uid, uid);
6030 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
6031 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6032 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6035 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6036 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6037 ("sqlite3_step fail:%d", rc));
6039 if (rc == SQLITE_DONE) {
6040 EM_DEBUG_EXCEPTION(" no matched mail found....");
6041 error = EMAIL_ERROR_MAIL_NOT_FOUND;
6045 _get_stmt_field_data_int(hStmt, mail_size, 0);
6050 if (hStmt != NULL) {
6051 rc = sqlite3_finalize(hStmt);
6052 if (rc != SQLITE_OK) {
6053 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6055 error = EMAIL_ERROR_DB_FAILURE;
6059 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6062 if (err_code != NULL)
6065 EM_DEBUG_FUNC_END("ret [%d]", ret);
6069 INTERNAL_FUNC int emstorage_add_downloaded_mail(emstorage_read_mail_uid_tbl_t* read_mail_uid, int transaction, int *err_code)
6071 EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], transaction[%d], err_code[%p]", read_mail_uid, transaction, err_code);
6073 if (!read_mail_uid) {
6074 EM_DEBUG_EXCEPTION("read_mail_uid[%p]", read_mail_uid);
6075 if (err_code != NULL)
6076 *err_code = EMAIL_ERROR_INVALID_PARAM;
6080 int rc, rc2, ret = false;
6081 int error = EMAIL_ERROR_NONE;
6082 DB_STMT hStmt = NULL;
6083 char sql_query_string[QUERY_SIZE] = {0, };
6085 sqlite3 *local_db_handle = emstorage_get_db_connection();
6086 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6088 char *sql = "SELECT max(rowid) FROM mail_read_mail_uid_tbl;";
6089 char **result = NULL;
6092 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6093 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6094 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6096 if (NULL==result[1]) rc = 1;
6097 else rc = atoi(result[1])+1;
6098 sqlite3_free_table(result);
6100 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6101 "INSERT INTO mail_read_mail_uid_tbl VALUES "
6113 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc2);
6114 if (rc2 != SQLITE_OK) {
6115 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6116 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6118 error = EMAIL_ERROR_DB_FAILURE;
6122 EM_DEBUG_LOG("account_id VALUE [%d] ", read_mail_uid->account_id);
6123 EM_DEBUG_LOG("mailbox_id VALUE [%d] ", read_mail_uid->mailbox_id);
6124 EM_DEBUG_LOG("local_uid VALUE [%d] ", read_mail_uid->local_uid);
6125 EM_DEBUG_LOG("mailbox_name VALUE [%s] ", read_mail_uid->mailbox_name);
6126 EM_DEBUG_LOG("s_uid VALUE [%s] ", read_mail_uid->s_uid);
6127 EM_DEBUG_LOG("data1 VALUE [%d] ", read_mail_uid->data1);
6128 EM_DEBUG_LOG("data2 VALUE [%s] ", read_mail_uid->data2);
6129 EM_DEBUG_LOG("flag VALUE [%d] ", read_mail_uid->flag);
6130 EM_DEBUG_LOG("rc VALUE [%d] ", rc);
6132 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->account_id);
6133 _bind_stmt_field_data_int(hStmt, LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->mailbox_id);
6134 _bind_stmt_field_data_int(hStmt, LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->local_uid);
6135 _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);
6136 _bind_stmt_field_data_string(hStmt, S_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, (char *)read_mail_uid->s_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6137 _bind_stmt_field_data_int(hStmt, DATA1_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->data1);
6138 _bind_stmt_field_data_string(hStmt, DATA2_IDX_IN_MAIL_READ_MAIL_UID_TBL, (char *)read_mail_uid->data2, 0, DATA2_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6139 _bind_stmt_field_data_int(hStmt, FLAG_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flag);
6140 _bind_stmt_field_data_int(hStmt, IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, rc);
6143 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6144 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
6145 ("sqlite3_step fail:%d", rc));
6146 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6147 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
6153 if (hStmt != NULL) {
6154 EM_DEBUG_LOG(" sqlite3_finalize hStmt = %p", hStmt);
6156 rc = sqlite3_finalize(hStmt);
6157 if (rc != SQLITE_OK) {
6158 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6159 error = EMAIL_ERROR_DB_FAILURE;
6163 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6166 if (err_code != NULL)
6169 EM_DEBUG_FUNC_END("ret [%d]", ret);
6173 INTERNAL_FUNC int emstorage_change_read_mail_uid(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)
6175 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);
6177 if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid) {
6178 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);
6180 if (err_code != NULL)
6181 *err_code = EMAIL_ERROR_INVALID_PARAM;
6185 int rc, ret = false;
6186 int error = EMAIL_ERROR_NONE;
6187 DB_STMT hStmt = NULL;
6188 char sql_query_string[QUERY_SIZE] = {0, };
6190 sqlite3 *local_db_handle = emstorage_get_db_connection();
6192 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6194 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6195 "UPDATE mail_read_mail_uid_tbl SET"
6199 ", mailbox_name = ?"
6204 " WHERE account_id = ?"
6205 " AND mailbox_id = ?"
6206 " AND local_uid = ?"
6207 " AND mailbox_name= ?"
6211 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
6212 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6213 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6214 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6219 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->account_id);
6220 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->mailbox_id);
6221 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->local_uid);
6222 _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6223 _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->s_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6224 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->data1);
6225 _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->data2, 0, DATA2_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6226 _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flag);
6227 _bind_stmt_field_data_int(hStmt, i++, account_id);
6228 _bind_stmt_field_data_int(hStmt, i++, mailbox_id);
6229 _bind_stmt_field_data_int(hStmt, i++, local_uid);
6230 _bind_stmt_field_data_string(hStmt, i++, (char*)mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6231 _bind_stmt_field_data_string(hStmt, i++, (char*)uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6234 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6235 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6236 ("sqlite3_step fail:%d", rc));
6241 if (hStmt != NULL) {
6242 EM_DEBUG_LOG("sqlite3_finalize hStmt = %p", hStmt);
6244 rc = sqlite3_finalize(hStmt);
6245 if (rc != SQLITE_OK) {
6246 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6248 error = EMAIL_ERROR_DB_FAILURE;
6252 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6255 if (err_code != NULL)
6258 EM_DEBUG_FUNC_END("ret [%d]", ret);
6262 INTERNAL_FUNC int emstorage_remove_downloaded_mail(int account_id, char *mailbox_name, char *uid, int transaction, int *err_code)
6264 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_name[%s], uid[%s], transaction[%d], err_code[%p]", account_id, mailbox_name, uid, transaction, err_code);
6266 if (account_id < FIRST_ACCOUNT_ID) {
6267 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%s], uid[%s]", account_id, mailbox_name, uid);
6269 if (err_code != NULL)
6270 *err_code = EMAIL_ERROR_INVALID_PARAM;
6274 int rc, ret = false;
6275 int error = EMAIL_ERROR_NONE;
6276 char sql_query_string[QUERY_SIZE] = {0, };
6278 sqlite3 *local_db_handle = emstorage_get_db_connection();
6280 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6282 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d ", account_id);
6284 if (mailbox_name) { /* NULL means all mailbox_name */
6285 SNPRINTF(sql_query_string+strlen(sql_query_string), sizeof(sql_query_string) - (1 + strlen(sql_query_string)), "AND mailbox_name = '%s' ", mailbox_name);
6288 if (uid) { /* NULL means all mail */
6289 SNPRINTF(sql_query_string+strlen(sql_query_string), sizeof(sql_query_string) - (1 + strlen(sql_query_string)), "AND s_uid='%s' ", uid);
6292 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
6293 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6294 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6301 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6304 if (err_code != NULL)
6307 EM_DEBUG_FUNC_END("ret [%d]", ret);
6311 INTERNAL_FUNC int emstorage_free_read_mail_uid(emstorage_read_mail_uid_tbl_t** read_mail_uid, int count, int *err_code)
6313 EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], count[%d], err_code[%p]", read_mail_uid, count, err_code);
6316 int error = EMAIL_ERROR_NONE;
6319 if (!read_mail_uid || !*read_mail_uid) {
6320 EM_DEBUG_EXCEPTION(" read_mail_uid[%p], count[%d]", read_mail_uid, count);
6322 error = EMAIL_ERROR_INVALID_PARAM;
6326 emstorage_read_mail_uid_tbl_t* p = *read_mail_uid;
6329 for (i = 0; i < count; i++) {
6330 EM_SAFE_FREE(p[i].mailbox_name);
6331 EM_SAFE_FREE(p[i].s_uid);
6332 EM_SAFE_FREE(p[i].data2);
6335 EM_SAFE_FREE(p); *read_mail_uid = NULL;
6341 if (err_code != NULL)
6344 EM_DEBUG_FUNC_END("ret [%d]", ret);
6348 INTERNAL_FUNC int emstorage_get_rule_count(int account_id, int *count, int transaction, int *err_code)
6350 EM_DEBUG_FUNC_BEGIN("account_id[%d], count[%p], transaction[%d], err_code[%p]", account_id, count, transaction, err_code);
6352 if (account_id != ALL_ACCOUNT || !count) { /* only global rule supported. */
6353 EM_DEBUG_EXCEPTION(" account_id[%d], count[%p]", account_id, count);
6355 if (err_code != NULL)
6356 *err_code = EMAIL_ERROR_INVALID_PARAM;
6360 int rc = -1, ret = false;
6361 int error = EMAIL_ERROR_NONE;
6362 char sql_query_string[QUERY_SIZE] = {0, };
6364 sqlite3 *local_db_handle = emstorage_get_db_connection();
6365 EMSTORAGE_START_READ_TRANSACTION(transaction);
6367 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl WHERE account_id = %d", account_id);
6371 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6372 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6373 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6375 *count = atoi(result[1]);
6376 sqlite3_free_table(result);
6381 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6384 if (err_code != NULL)
6387 EM_DEBUG_FUNC_END("ret [%d]", ret);
6391 INTERNAL_FUNC int emstorage_get_rule(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)
6393 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);
6395 if (account_id != ALL_ACCOUNT || !select_num || !is_completed || !rule_list) { /* only global rule supported. */
6396 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);
6398 if (err_code != NULL)
6399 *err_code = EMAIL_ERROR_INVALID_PARAM;
6404 int error = EMAIL_ERROR_NONE;
6406 emstorage_rule_tbl_t* p_data_tbl = NULL;
6407 int i = 0, count = 0;
6408 DB_STMT hStmt = NULL;
6409 char sql_query_string[QUERY_SIZE] = {0, };
6413 sqlite3 *local_db_handle = emstorage_get_db_connection();
6414 EMSTORAGE_START_READ_TRANSACTION(transaction);
6417 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d AND type = %d", account_id, type);
6419 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d ORDER BY type", account_id);
6422 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
6423 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6424 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6425 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6428 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6429 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6430 ("sqlite3_step fail:%d", rc));
6433 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL); */
6434 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
6435 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6436 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6438 sqlite3_free_table(result);
6441 EM_DEBUG_LOG(" no matched rule found...");
6447 if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t) * count))) {
6448 EM_DEBUG_EXCEPTION(" malloc failed...");
6450 error = EMAIL_ERROR_OUT_OF_MEMORY;
6454 memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t) * count);
6456 for (i = 0; i < count; i++) {
6457 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
6458 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
6459 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].type), TYPE_IDX_IN_MAIL_RULE_TBL);
6460 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
6461 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
6462 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
6463 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
6464 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
6466 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6467 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6468 ("sqlite3_step fail:%d", rc));
6475 EM_DEBUG_LOG("[%d] rules found.", count);
6478 *rule_list = p_data_tbl;
6479 *select_num = count;
6481 else if (p_data_tbl != NULL)
6482 emstorage_free_rule(&p_data_tbl, count, NULL); /* CID FIX */
6484 if (hStmt != NULL) {
6485 EM_DEBUG_LOG(" sqlite3_finalize hStmt = %p", hStmt);
6487 rc = sqlite3_finalize(hStmt);
6488 if (rc != SQLITE_OK) {
6489 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6490 error = EMAIL_ERROR_DB_FAILURE;
6495 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6498 if (err_code != NULL)
6501 EM_DEBUG_FUNC_END("ret [%d]", ret);
6505 INTERNAL_FUNC int emstorage_get_rule_by_id(int account_id, int rule_id, emstorage_rule_tbl_t** rule, int transaction, int *err_code)
6507 EM_DEBUG_FUNC_BEGIN("account_id[%d], rule_id[%d], rule[%p], transaction[%d], err_code[%p]", account_id, rule_id, rule, transaction, err_code);
6509 if (account_id != ALL_ACCOUNT || !rule) {
6510 EM_DEBUG_EXCEPTION(" account_id[%d], rule_id[%d], rule[%p]", account_id, rule_id, rule);
6512 if (err_code != NULL)
6513 *err_code = EMAIL_ERROR_INVALID_PARAM;
6517 emstorage_rule_tbl_t* p_data_tbl = NULL;
6518 int rc, ret = false;
6519 int error = EMAIL_ERROR_NONE;
6521 DB_STMT hStmt = NULL;
6522 char sql_query_string[QUERY_SIZE] = {0, };
6523 sqlite3 *local_db_handle = emstorage_get_db_connection();
6524 EMSTORAGE_START_READ_TRANSACTION(transaction);
6526 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d AND rule_id = %d", account_id, rule_id);
6529 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
6530 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6531 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6534 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6535 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6536 ("sqlite3_step fail:%d", rc));
6538 if (rc == SQLITE_DONE) {
6539 EM_DEBUG_EXCEPTION(" no matched rule found...");
6540 error = EMAIL_ERROR_FILTER_NOT_FOUND;
6544 if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t)))) {
6545 EM_DEBUG_EXCEPTION(" malloc failed...");
6546 error = EMAIL_ERROR_OUT_OF_MEMORY;
6550 memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t));
6551 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
6552 _get_stmt_field_data_int(hStmt, &(p_data_tbl->rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
6553 _get_stmt_field_data_int(hStmt, &(p_data_tbl->type), TYPE_IDX_IN_MAIL_RULE_TBL);
6554 _get_stmt_field_data_string(hStmt, &(p_data_tbl->value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
6555 _get_stmt_field_data_int(hStmt, &(p_data_tbl->action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
6556 _get_stmt_field_data_int(hStmt, &(p_data_tbl->target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
6557 _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
6558 _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
6565 else if (p_data_tbl != NULL)
6566 emstorage_free_rule(&p_data_tbl, 1, NULL);
6568 if (hStmt != NULL) {
6569 EM_DEBUG_LOG(" before sqlite3_finalize hStmt = %p", hStmt);
6571 rc = sqlite3_finalize(hStmt);
6572 if (rc != SQLITE_OK) {
6573 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6575 error = EMAIL_ERROR_DB_FAILURE;
6581 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6584 if (err_code != NULL)
6587 EM_DEBUG_FUNC_END("ret [%d]", ret);
6591 INTERNAL_FUNC int emstorage_change_rule(int account_id, int rule_id, emstorage_rule_tbl_t* new_rule, int transaction, int *err_code)
6593 EM_DEBUG_FUNC_BEGIN("account_id[%d], rule_id[%d], new_rule[%p], transaction[%d], err_code[%p]", account_id, rule_id, new_rule, transaction, err_code);
6595 if (account_id != ALL_ACCOUNT || !new_rule) { /* only global rule supported. */
6596 EM_DEBUG_EXCEPTION(" account_id[%d], rule_id[%d], new_rule[%p]", account_id, rule_id, new_rule);
6598 if (err_code != NULL)
6599 *err_code = EMAIL_ERROR_INVALID_PARAM;
6603 int rc, ret = false;
6604 int error = EMAIL_ERROR_NONE;
6606 DB_STMT hStmt = NULL;
6607 char sql_query_string[QUERY_SIZE] = {0, };
6608 sqlite3 *local_db_handle = emstorage_get_db_connection();
6609 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6611 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6612 "UPDATE mail_rule_tbl SET"
6616 ", target_mailbox_id = ?"
6619 " WHERE account_id = ?"
6620 " AND rule_id = ?");
6623 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
6624 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
6625 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6626 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6630 _bind_stmt_field_data_int(hStmt, i++, new_rule->type);
6631 _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
6632 _bind_stmt_field_data_int(hStmt, i++, new_rule->action_type);
6633 _bind_stmt_field_data_int(hStmt, i++, new_rule->target_mailbox_id);
6634 _bind_stmt_field_data_int(hStmt, i++, new_rule->flag1);
6635 _bind_stmt_field_data_int(hStmt, i++, new_rule->flag2);
6636 _bind_stmt_field_data_int(hStmt, i++, account_id);
6637 _bind_stmt_field_data_int(hStmt, i++, rule_id);
6640 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6641 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6642 ("sqlite3_step fail:%d", rc));
6647 if (hStmt != NULL) {
6648 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
6650 rc = sqlite3_finalize(hStmt);
6651 if (rc != SQLITE_OK) {
6652 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6654 error = EMAIL_ERROR_DB_FAILURE;
6658 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6661 if (err_code != NULL)
6664 EM_DEBUG_FUNC_END("ret [%d]", ret);
6668 INTERNAL_FUNC int emstorage_find_rule(emstorage_rule_tbl_t* rule, int transaction, int *err_code)
6670 EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
6672 if (!rule || rule->account_id != ALL_ACCOUNT) { /* only global rule supported. */
6674 EM_DEBUG_EXCEPTION(" rule->account_id[%d]", rule->account_id);
6676 if (err_code != NULL)
6677 *err_code = EMAIL_ERROR_INVALID_PARAM;
6681 DB_STMT hStmt = NULL;
6682 char sql_query_string[QUERY_SIZE] = {0, };
6684 int error = EMAIL_ERROR_NONE;
6685 int rc, ret = false;
6687 sqlite3 *local_db_handle = emstorage_get_db_connection();
6688 EMSTORAGE_START_READ_TRANSACTION(transaction);
6690 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6691 "SELECT rule_id FROM mail_rule_tbl WHERE type = %d AND UPPER(value) = UPPER('%s')",
6692 rule->type, rule->value);
6695 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
6696 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6697 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6700 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6701 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6702 ("sqlite3_step fail:%d", rc));
6704 if (rc == SQLITE_DONE) {
6705 EM_DEBUG_EXCEPTION(" no matched rule found...");
6706 error = EMAIL_ERROR_FILTER_NOT_FOUND;
6714 if (hStmt != NULL) {
6715 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
6717 rc = sqlite3_finalize(hStmt);
6718 if (rc != SQLITE_OK) {
6719 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
6720 error = EMAIL_ERROR_DB_FAILURE;
6725 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6731 EM_DEBUG_FUNC_END("ret [%d]", ret);
6735 INTERNAL_FUNC int emstorage_add_rule(emstorage_rule_tbl_t* rule, int transaction, int *err_code)
6737 EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
6739 if (!rule || rule->account_id != ALL_ACCOUNT) { /* only global rule supported. */
6741 EM_DEBUG_EXCEPTION(" rule->account_id[%d]", rule->account_id);
6743 if (err_code != NULL)
6744 *err_code = EMAIL_ERROR_INVALID_PARAM;
6748 int rc, rc_2, ret = false;
6749 int error = EMAIL_ERROR_NONE;
6750 DB_STMT hStmt = NULL;
6751 char sql_query_string[QUERY_SIZE] = {0, };
6754 sqlite3 *local_db_handle = emstorage_get_db_connection();
6756 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6760 sql = "SELECT max(rowid) FROM mail_rule_tbl;";
6762 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6763 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6764 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6766 if (NULL==result[1])
6769 rc = atoi(result[1])+1;
6770 sqlite3_free_table(result);
6772 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6773 "INSERT INTO mail_rule_tbl VALUES "
6774 "( ?" /* account id */
6778 ", ?" /* action_type */
6779 ", ?" /* target_mailbox_id */
6781 ", ?)"); /* flag2 */
6783 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc_2);
6784 if (rc_2 != SQLITE_OK) {
6785 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc_2, sqlite3_errmsg(local_db_handle));
6786 error = EMAIL_ERROR_DB_FAILURE;
6790 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL, rule->account_id);
6791 _bind_stmt_field_data_int(hStmt, RULE_ID_IDX_IN_MAIL_RULE_TBL, rc);
6792 _bind_stmt_field_data_int(hStmt, TYPE_IDX_IN_MAIL_RULE_TBL, rule->type);
6793 _bind_stmt_field_data_string(hStmt, VALUE_IDX_IN_MAIL_RULE_TBL, (char*)rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
6794 _bind_stmt_field_data_int(hStmt, ACTION_TYPE_IDX_IN_MAIL_RULE_TBL, rule->action_type);
6795 _bind_stmt_field_data_int(hStmt, TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL, rule->target_mailbox_id);
6796 _bind_stmt_field_data_int(hStmt, FLAG1_IDX_IN_MAIL_RULE_TBL, rule->flag1);
6797 _bind_stmt_field_data_int(hStmt, FLAG2_IDX_IN_MAIL_RULE_TBL, rule->flag2);
6799 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6800 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6801 ("sqlite3_step fail:%d", rc));
6806 if (hStmt != NULL) {
6807 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
6809 rc = sqlite3_finalize(hStmt);
6810 if (rc != SQLITE_OK) {
6811 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
6813 error = EMAIL_ERROR_DB_FAILURE;
6817 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6820 if (err_code != NULL)
6823 EM_DEBUG_FUNC_END("ret [%d]", ret);
6827 INTERNAL_FUNC int emstorage_delete_rule(int account_id, int rule_id, int transaction, int *err_code)
6829 EM_DEBUG_FUNC_BEGIN("account_id[%d], rule_id[%d], transaction[%d], err_code[%p]", account_id, rule_id, transaction, err_code);
6831 if (account_id != ALL_ACCOUNT) { /* only global rule supported. */
6832 EM_DEBUG_EXCEPTION(" account_id[%d], rule_id[%d]", account_id, rule_id);
6834 if (err_code != NULL)
6835 *err_code = EMAIL_ERROR_INVALID_PARAM;
6839 int rc, ret = false;
6840 int error = EMAIL_ERROR_NONE;
6841 char sql_query_string[QUERY_SIZE] = {0, };
6843 sqlite3 *local_db_handle = emstorage_get_db_connection();
6844 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
6846 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_rule_tbl WHERE account_id = %d AND rule_id = %d", account_id, rule_id);
6847 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
6848 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6849 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6850 rc = sqlite3_changes(local_db_handle);
6852 EM_DEBUG_EXCEPTION(" no matched rule found...");
6854 error = EMAIL_ERROR_FILTER_NOT_FOUND;
6861 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
6864 if (err_code != NULL)
6867 EM_DEBUG_FUNC_END("ret [%d]", ret);
6871 INTERNAL_FUNC int emstorage_free_rule(emstorage_rule_tbl_t** rule_list, int count, int *err_code)
6873 EM_DEBUG_FUNC_BEGIN("rule_list[%p], conut[%d], err_code[%p]", rule_list, count, err_code);
6876 int error = EMAIL_ERROR_NONE;
6879 if (!rule_list || !*rule_list) {
6880 EM_DEBUG_EXCEPTION(" rule_list[%p], conut[%d]", rule_list, count);
6882 error = EMAIL_ERROR_INVALID_PARAM;
6886 emstorage_rule_tbl_t* p = *rule_list;
6889 for (; i < count; i++) {
6890 EM_SAFE_FREE(p[i].value);
6893 EM_SAFE_FREE(p); *rule_list = NULL;
6897 if (err_code != NULL)
6900 EM_DEBUG_FUNC_END("ret [%d]", ret);
6904 INTERNAL_FUNC int emstorage_get_mail_count(int account_id, const char *mailbox_name, int *total, int *unseen, int transaction, int *err_code)
6906 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_name[%s], total[%p], unseen[%p], transaction[%d], err_code[%p]", account_id, mailbox_name, total, unseen, transaction, err_code);
6908 if (!total && !unseen) {
6909 EM_DEBUG_EXCEPTION(" accoun_id[%d], mailbox_name[%s], total[%p], unseen[%p]", account_id, mailbox_name, total, unseen);
6910 if (err_code != NULL)
6911 *err_code = EMAIL_ERROR_INVALID_PARAM;
6915 int rc = -1, ret = false;
6916 int error = EMAIL_ERROR_NONE;
6917 DB_STMT hStmt = NULL;
6918 char sql_query_string[QUERY_SIZE] = {0, };
6922 memset(&sql_query_string, 0x00, sizeof(sql_query_string));
6923 sqlite3 *local_db_handle = emstorage_get_db_connection();
6924 EMSTORAGE_START_READ_TRANSACTION(transaction);
6927 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl");
6929 if (account_id != ALL_ACCOUNT) {
6930 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " WHERE account_id = %d", account_id);
6932 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " AND UPPER(mailbox_name) = UPPER('%s')", mailbox_name);
6934 else if (mailbox_name)
6935 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " WHERE UPPER(mailbox_name) = UPPER('%s')", mailbox_name);
6937 #ifdef USE_GET_RECORD_COUNT_API
6940 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6941 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF2; },
6942 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6944 *total = atoi(result[1]);
6945 sqlite3_free_table(result);
6948 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
6949 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF2; },
6950 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6952 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6953 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF2; },
6954 ("sqlite3_step fail:%d", rc));
6955 _get_stmt_field_data_int(hStmt, total, 0);
6956 #endif /* USE_GET_RECORD_COUNT_API */
6960 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl WHERE flags_seen_field = 0"); /* fSEEN = 0x01 */
6962 if (account_id != ALL_ACCOUNT)
6963 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " AND account_id = %d", account_id);
6966 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " AND mailbox_name = '%s'", mailbox_name);
6968 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " AND mailbox_type NOT IN (3, 5)");
6971 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6972 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6973 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6975 *unseen = atoi(result[1]);
6976 sqlite3_free_table(result);
6984 #ifndef USE_PREPARED_QUERY_
6985 if (hStmt != NULL) {
6986 rc = sqlite3_finalize(hStmt);
6987 if (rc != SQLITE_OK) {
6988 EM_DEBUG_LOG(" EDBStmtClearRow failed - %d: %s", rc, err_msg);
6989 error = EMAIL_ERROR_DB_FAILURE;
6994 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6997 if (err_code != NULL)
7000 EM_DEBUG_FUNC_END("ret [%d]", ret);
7004 INTERNAL_FUNC int emstorage_get_mail_field_by_id(int mail_id, int type, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
7006 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
7008 if (mail_id <= 0 || !mail) {
7009 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
7010 if (err_code != NULL)
7011 *err_code = EMAIL_ERROR_INVALID_PARAM;
7015 emstorage_mail_tbl_t* p_data_tbl = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
7017 if (p_data_tbl == NULL) {
7018 EM_DEBUG_EXCEPTION("malloc failed...");
7019 if (err_code != NULL)
7020 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
7024 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
7025 DB_STMT hStmt = NULL;
7026 char sql_query_string[QUERY_SIZE] = {0, };
7028 int rc, ret = false;
7029 int error = EMAIL_ERROR_NONE;
7031 sqlite3 *local_db_handle = emstorage_get_db_connection();
7032 EMSTORAGE_START_READ_TRANSACTION(transaction);
7035 case RETRIEVE_SUMMARY:
7036 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7037 "SELECT account_id, mail_id, mailbox_id, mailbox_name, server_mail_status, server_mailbox_name, server_mail_id, file_path_plain, file_path_html, flags_seen_field, save_status, lock_status, thread_id, thread_item_count FROM mail_tbl WHERE mail_id = %d", mail_id);
7040 case RETRIEVE_FIELDS_FOR_DELETE:
7041 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7042 "SELECT account_id, mail_id, server_mail_status, server_mailbox_name, server_mail_id FROM mail_tbl WHERE mail_id = %d", mail_id);
7045 case RETRIEVE_ACCOUNT:
7046 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7047 "SELECT account_id FROM mail_tbl WHERE mail_id = %d", mail_id);
7051 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7052 "SELECT account_id, mailbox_name, flags_seen_field, thread_id, mailbox_id FROM mail_tbl WHERE mail_id = %d", mail_id);
7056 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
7057 error = EMAIL_ERROR_INVALID_PARAM;
7061 EM_DEBUG_LOG("Query [%s]", sql_query_string);
7063 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
7064 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7065 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7068 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7069 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7070 ("sqlite3_step fail:%d", rc));
7072 if (rc == SQLITE_DONE) {
7073 EM_DEBUG_EXCEPTION("no matched mail found...");
7074 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7078 case RETRIEVE_SUMMARY:
7079 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), 0);
7080 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), 1);
7081 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), 2);
7082 _get_stmt_field_data_string(hStmt, &(p_data_tbl->mailbox_name), 0, 3);
7083 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), 4);
7084 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, 5);
7085 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, 6);
7086 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, 7);
7087 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, 8);
7088 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), 9);
7089 _get_stmt_field_data_int(hStmt, &(p_data_tbl->save_status), 10);
7090 _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), 11);
7091 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), 12);
7092 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_item_count), 13);
7095 case RETRIEVE_FIELDS_FOR_DELETE:
7096 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), 0);
7097 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), 1);
7098 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), 2);
7099 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, 3);
7100 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, 4);
7103 case RETRIEVE_ACCOUNT:
7104 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), 0);
7108 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), 0);
7109 _get_stmt_field_data_string(hStmt, &(p_data_tbl->mailbox_name), 0, 1);
7110 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), 2);
7111 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), 3);
7112 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), 4);
7121 else if (p_data_tbl != NULL)
7122 emstorage_free_mail(&p_data_tbl, 1, NULL);
7124 if (hStmt != NULL) {
7125 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
7127 rc = sqlite3_finalize(hStmt);
7128 if (rc != SQLITE_OK) {
7129 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
7130 error = EMAIL_ERROR_DB_FAILURE;
7135 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7138 if (err_code != NULL)
7141 EM_DEBUG_FUNC_END("ret [%d]", ret);
7145 INTERNAL_FUNC int emstorage_get_mail_field_by_multiple_mail_id(int mail_ids[], int number_of_mails, int type, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
7147 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);
7149 if (number_of_mails <= 0 || !mail_ids) {
7150 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
7151 if (err_code != NULL)
7152 *err_code = EMAIL_ERROR_INVALID_PARAM;
7156 emstorage_mail_tbl_t* p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * number_of_mails);
7158 if (p_data_tbl == NULL) {
7159 EM_DEBUG_EXCEPTION("malloc failed...");
7160 if (err_code != NULL)
7161 *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
7165 DB_STMT hStmt = NULL;
7167 int error = EMAIL_ERROR_NONE;
7168 int i = 0, item_count = 0, rc = -1, field_count, col_index, cur_sql_query_string = 0;
7169 char **result = NULL;
7170 char sql_query_string[QUERY_SIZE] = {0, };
7172 sqlite3 *local_db_handle = emstorage_get_db_connection();
7173 EMSTORAGE_START_READ_TRANSACTION(transaction);
7176 case RETRIEVE_SUMMARY:
7177 cur_sql_query_string = SNPRINTF(sql_query_string, sizeof(sql_query_string),
7178 "SELECT account_id, mail_id, mailbox_name, server_mail_status, server_mailbox_name, server_mail_id, file_path_plain, file_path_html, flags_seen_field, save_status, lock_status, thread_id, thread_item_count FROM mail_tbl WHERE mail_id in (");
7182 case RETRIEVE_FIELDS_FOR_DELETE:
7183 cur_sql_query_string = SNPRINTF(sql_query_string, sizeof(sql_query_string),
7184 "SELECT account_id, mail_id, server_mail_status, server_mailbox_name, server_mail_id FROM mail_tbl WHERE mail_id in (");
7188 case RETRIEVE_ACCOUNT:
7189 cur_sql_query_string = SNPRINTF(sql_query_string, sizeof(sql_query_string),
7190 "SELECT account_id FROM mail_tbl WHERE mail_id in (");
7195 cur_sql_query_string = SNPRINTF(sql_query_string, sizeof(sql_query_string),
7196 "SELECT account_id, mailbox_id, flags_seen_field, thread_id FROM mail_tbl WHERE mail_id in (");
7201 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
7202 error = EMAIL_ERROR_INVALID_PARAM;
7206 for(i = 0; i < number_of_mails; i++)
7207 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, QUERY_SIZE, "%d,", mail_ids[i]);
7208 sql_query_string[strlen(sql_query_string) - 1] = ')';
7210 EM_DEBUG_LOG("Query [%s], Length [%d]", sql_query_string, strlen(sql_query_string));
7212 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &item_count, 0, NULL), rc);
7213 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
7214 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7216 if (rc == SQLITE_DONE) {
7217 EM_DEBUG_EXCEPTION("no matched mail found...");
7218 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7222 EM_DEBUG_LOG("item_count [%d]", item_count);
7224 if(number_of_mails != item_count) {
7225 EM_DEBUG_EXCEPTION("Can't find all emails");
7226 /* error = EMAIL_ERROR_MAIL_NOT_FOUND;
7227 goto FINISH_OFF; */ /* removed temporarily */
7230 col_index = field_count;
7232 for(i = 0; i < item_count; i++) {
7234 case RETRIEVE_SUMMARY:
7235 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7236 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
7237 _get_table_field_data_string(result, &(p_data_tbl[i].mailbox_name), 0, col_index++);
7238 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
7239 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
7240 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
7241 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
7242 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
7243 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
7244 _get_table_field_data_int(result, &(p_data_tbl[i].save_status), col_index++);
7245 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
7246 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
7247 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
7250 case RETRIEVE_FIELDS_FOR_DELETE:
7251 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7252 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
7253 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
7254 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
7255 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
7258 case RETRIEVE_ACCOUNT:
7259 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7263 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
7264 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
7265 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
7266 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
7271 sqlite3_free_table(result);
7278 else if (p_data_tbl != NULL)
7279 emstorage_free_mail(&p_data_tbl, 1, NULL);
7281 if (hStmt != NULL) {
7282 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
7284 rc = sqlite3_finalize(hStmt);
7285 if (rc != SQLITE_OK) {
7286 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
7287 error = EMAIL_ERROR_DB_FAILURE;
7291 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7294 if (err_code != NULL)
7297 EM_DEBUG_FUNC_END("ret [%d]", ret);
7301 INTERNAL_FUNC int emstorage_get_mail_by_id(int mail_id, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
7303 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
7305 if (mail_id <= 0 || !mail) {
7306 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
7307 if (err_code != NULL)
7308 *err_code = EMAIL_ERROR_INVALID_PARAM;
7312 int ret = false, error = EMAIL_ERROR_NONE, count;
7313 char conditional_clause[QUERY_SIZE] = {0, };
7314 emstorage_mail_tbl_t* p_data_tbl = NULL;
7316 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
7317 EM_DEBUG_LOG("query = [%s]", conditional_clause);
7319 if(!emstorage_query_mail_tbl(conditional_clause, transaction, &p_data_tbl, &count, &error)) {
7320 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
7329 else if (p_data_tbl != NULL)
7330 emstorage_free_mail(&p_data_tbl, 1, &error);
7332 if (err_code != NULL)
7335 EM_DEBUG_FUNC_END("ret [%d]", ret);
7339 INTERNAL_FUNC int emstorage_mail_search_start(emstorage_search_filter_t* search, int account_id, char *mailbox_name, int sorting, int *search_handle, int *searched, int transaction, int *err_code)
7341 EM_DEBUG_FUNC_BEGIN("search[%p], account_id[%d], mailbox_name[%p], sorting[%d], search_handle[%p], searched[%p], transaction[%d], err_code[%p]", search, account_id, mailbox_name, sorting, search_handle, searched, transaction, err_code);
7343 if (!search_handle || !searched) {
7344 if (err_code != NULL)
7345 *err_code = EMAIL_ERROR_INVALID_PARAM;
7346 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
7347 EM_DEBUG_FUNC_END("false");
7351 emstorage_search_filter_t* p = search;
7352 int error = EMAIL_ERROR_NONE;
7353 DB_STMT hStmt = NULL;
7354 char sql_query_string[QUERY_SIZE] = {0, };
7355 int rc, ret = false;
7356 int and = false, mail_count = 0;
7358 sqlite3 *local_db_handle = emstorage_get_db_connection();
7359 EMSTORAGE_START_READ_TRANSACTION(transaction);
7361 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl");
7363 if (account_id != ALL_ACCOUNT) {
7364 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " WHERE account_id = %d", account_id);
7369 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " %s mailbox_name = '%s'", and ? "AND" : "WHERE", mailbox_name);
7376 if (!strncmp(p->key_type, "subject", strlen("subject"))) {
7377 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " %s subject LIKE '%%%%%s%%%%'", and ? "AND" : "WHERE", p->key_value);
7380 else if (!strncmp(p->key_type, "full_address_from", strlen("full_address_from"))) {
7381 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " %s full_address_from LIKE '%%%%%s%%%%'", and ? "AND" : "WHERE", p->key_value);
7384 else if (!strncmp(p->key_type, "full_address_to", strlen("full_address_to"))) {
7385 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " %s full_address_to LIKE '%%%%%s%%%%'", and ? "AND" : "WHERE", p->key_value);
7388 else if (!strncmp(p->key_type, "email_address", strlen("email_address"))) {
7389 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " %s email_address_sender = '%s' OR email_address_recipient = '%s'", and ? "AND" : "WHERE", p->key_value, p->key_value);
7397 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " ORDER BY date_time");
7399 EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
7402 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
7403 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7404 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7407 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7408 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7409 ("sqlite3_step fail:%d", rc));
7413 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &mail_count, NULL, NULL), rc);
7414 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
7415 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7417 sqlite3_free_table(result);
7423 *search_handle = (int)hStmt;
7424 *searched = mail_count;
7425 EM_DEBUG_LOG("mail_count [%d]", mail_count);
7428 if (hStmt != NULL) {
7429 rc = sqlite3_finalize(hStmt);
7430 if (rc != SQLITE_OK) {
7431 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
7432 error = EMAIL_ERROR_DB_FAILURE;
7436 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7440 if (err_code != NULL)
7443 EM_DEBUG_FUNC_END("ret [%d]", ret);
7447 INTERNAL_FUNC int emstorage_mail_search_result(int search_handle, emstorage_mail_field_type_t type, void** data, int transaction, int *err_code)
7449 EM_DEBUG_FUNC_BEGIN("search_handle[%d], type[%d], data[%p], transaction[%d], err_code[%p]", search_handle, type, data, transaction, err_code);
7451 if (search_handle < 0 || !data) {
7452 EM_DEBUG_EXCEPTION(" search_handle[%d], type[%d], data[%p]", search_handle, type, data);
7454 if (err_code != NULL)
7455 *err_code = EMAIL_ERROR_INVALID_PARAM;
7459 emstorage_mail_tbl_t* p_data_tbl = NULL;
7460 DB_STMT hStmt = (DB_STMT)search_handle;
7461 int rc, ret = false;
7462 int error = EMAIL_ERROR_NONE;
7466 _get_stmt_field_data_int(hStmt, (int *)data, MAIL_ID_IDX_IN_MAIL_TBL);
7469 case RETRIEVE_ENVELOPE:
7471 if (!(p_data_tbl = em_malloc(sizeof(emstorage_mail_tbl_t)))) {
7472 EM_DEBUG_EXCEPTION(" em_malloc failed...");
7474 error = EMAIL_ERROR_OUT_OF_MEMORY;
7478 _get_stmt_field_data_int (hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
7479 _get_stmt_field_data_int (hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
7480 _get_stmt_field_data_string(hStmt, &(p_data_tbl->mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_TBL);
7481 _get_stmt_field_data_int (hStmt, &(p_data_tbl->mail_size), MAIL_SIZE_IDX_IN_MAIL_TBL);
7482 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
7483 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_from), 1, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL);
7484 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_to), 1, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL);
7485 _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject), 1, SUBJECT_IDX_IN_MAIL_TBL);
7486 _get_stmt_field_data_int (hStmt, &(p_data_tbl->body_download_status), BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL);
7487 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL);
7488 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
7489 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
7490 _get_stmt_field_data_time_t(hStmt, &(p_data_tbl->date_time), DATETIME_IDX_IN_MAIL_TBL);
7491 _get_stmt_field_data_char (hStmt, &(p_data_tbl->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL);
7492 _get_stmt_field_data_int (hStmt, &(p_data_tbl->DRM_status), DRM_STATUS_IDX_IN_MAIL_TBL);
7493 _get_stmt_field_data_int (hStmt, &(p_data_tbl->priority), PRIORITY_IDX_IN_MAIL_TBL);
7494 _get_stmt_field_data_int (hStmt, &(p_data_tbl->save_status), SAVE_STATUS_IDX_IN_MAIL_TBL);
7495 _get_stmt_field_data_int (hStmt, &(p_data_tbl->lock_status), LOCK_STATUS_IDX_IN_MAIL_TBL);
7496 _get_stmt_field_data_int (hStmt, &(p_data_tbl->report_status), REPORT_STATUS_IDX_IN_MAIL_TBL);
7497 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
7498 _get_stmt_field_data_int (hStmt, &(p_data_tbl->meeting_request_status), MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL);
7499 _get_stmt_field_data_int (hStmt, &(p_data_tbl->message_class), MESSAGE_CLASS_IDX_IN_MAIL_TBL);
7500 _get_stmt_field_data_int (hStmt, &(p_data_tbl->digest_type), DIGEST_TYPE_IDX_IN_MAIL_TBL);
7501 _get_stmt_field_data_int (hStmt, &(p_data_tbl->smime_type), SMIME_TYPE_IDX_IN_MAIL_TBL);
7503 if (type == RETRIEVE_ALL) {
7504 _get_stmt_field_data_int (hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
7505 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
7506 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_reply), 1, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL);
7507 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_cc), 1, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL);
7508 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_bcc), 1, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL);
7509 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_return), 1, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL);
7510 _get_stmt_field_data_string(hStmt, &(p_data_tbl->message_id), 0, MESSAGE_ID_IDX_IN_MAIL_TBL);
7511 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
7512 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
7513 _get_stmt_field_data_int (hStmt, &(p_data_tbl->attachment_count), ATTACHMENT_COUNT_IDX_IN_MAIL_TBL);
7514 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
7518 if (p_data_tbl->body_download_status) {
7521 if (p_data_tbl->file_path_html) {
7522 if (stat(p_data_tbl->file_path_html, &buf) == -1)
7523 p_data_tbl->body_download_status = 0;
7525 else if (p_data_tbl->file_path_plain) {
7526 if (stat(p_data_tbl->file_path_plain, &buf) == -1)
7527 p_data_tbl->body_download_status = 0;
7530 p_data_tbl->body_download_status = 0;
7533 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
7536 case RETRIEVE_SUMMARY:
7537 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
7538 EM_DEBUG_EXCEPTION(" malloc failed...");
7540 error = EMAIL_ERROR_OUT_OF_MEMORY;
7544 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
7546 _get_stmt_field_data_int (hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
7547 _get_stmt_field_data_int (hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
7548 _get_stmt_field_data_string(hStmt, &(p_data_tbl->mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_TBL);
7549 _get_stmt_field_data_int (hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
7550 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
7551 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
7553 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
7556 case RETRIEVE_ADDRESS:
7557 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
7558 EM_DEBUG_EXCEPTION(" malloc failed...");
7559 error = EMAIL_ERROR_OUT_OF_MEMORY;
7563 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
7564 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
7565 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
7566 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
7567 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
7575 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7576 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7577 ("sqlite3_step fail:%d", rc));
7583 if (err_code != NULL)
7586 EM_DEBUG_FUNC_END("ret [%d]", ret);
7590 INTERNAL_FUNC int emstorage_mail_search_end(int search_handle, int transaction, int *err_code)
7592 EM_DEBUG_FUNC_BEGIN("search_handle[%d], transaction[%d], err_code[%p]", search_handle, transaction, err_code);
7594 int error = EMAIL_ERROR_NONE;
7595 int rc, ret = false;
7597 if (search_handle < 0) {
7598 EM_DEBUG_EXCEPTION(" search_handle[%d]", search_handle);
7599 error = EMAIL_ERROR_INVALID_PARAM;
7603 DB_STMT hStmt = (DB_STMT)search_handle;
7605 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
7607 rc = sqlite3_finalize(hStmt);
7608 if (rc != SQLITE_OK) {
7609 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
7610 error = EMAIL_ERROR_DB_FAILURE;
7616 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7619 if (err_code != NULL)
7622 EM_DEBUG_FUNC_END("ret [%d]", ret);
7626 INTERNAL_FUNC int emstorage_change_mail(int mail_id, emstorage_mail_tbl_t* mail, int transaction, int *err_code)
7628 EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
7630 if (mail_id <= 0 || !mail) {
7631 EM_DEBUG_EXCEPTION(" mail_id[%d], mail[%p]", mail_id, mail);
7633 if (err_code != NULL)
7634 *err_code = EMAIL_ERROR_INVALID_PARAM;
7638 DB_STMT hStmt = NULL;
7639 char sql_query_string[QUERY_SIZE] = {0, };
7642 int error = EMAIL_ERROR_NONE;
7644 sqlite3 *local_db_handle = emstorage_get_db_connection();
7645 char mailbox_id_param_string[10] = {0,};
7647 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
7649 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7650 "UPDATE mail_tbl SET"
7654 ", mailbox_name = ?"
7656 ", server_mail_status = ?"
7657 ", server_mailbox_name = ?"
7658 ", server_mail_id = ?"
7659 ", full_address_from = ?"
7660 ", full_address_reply = ?" /* 10 */
7661 ", full_address_to = ?"
7662 ", full_address_cc = ?"
7663 ", full_address_bcc = ?"
7664 ", full_address_return = ?"
7666 ", body_download_status = ?"
7667 ", file_path_plain = ?"
7668 ", file_path_html = ?"
7670 ", flags_seen_field = ?"
7671 ", flags_deleted_field = ?"
7672 ", flags_flagged_field = ?"
7673 ", flags_answered_field = ?"
7674 ", flags_recent_field = ?"
7675 ", flags_draft_field = ?"
7676 ", flags_forwarded_field = ?"
7682 ", report_status = ?"
7683 ", preview_text = ?"
7685 " WHERE mail_id = %d AND account_id != 0 "
7689 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
7690 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7691 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7693 _bind_stmt_field_data_int (hStmt, i++, mail->mail_id);
7694 _bind_stmt_field_data_int (hStmt, i++, mail->account_id);
7695 _bind_stmt_field_data_int (hStmt, i++, mail->mailbox_id);
7696 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->mailbox_name, 0, MAILBOX_LEN_IN_MAIL_TBL);
7697 _bind_stmt_field_data_int (hStmt, i++, mail->mail_size);
7698 _bind_stmt_field_data_int (hStmt, i++, mail->server_mail_status);
7699 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mailbox_name, 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
7700 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mail_id, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
7701 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
7702 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
7703 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
7704 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
7705 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
7706 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
7707 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
7708 _bind_stmt_field_data_int (hStmt, i++, mail->body_download_status);
7709 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
7710 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
7711 _bind_stmt_field_data_int (hStmt, i++, mail->date_time);
7712 _bind_stmt_field_data_char (hStmt, i++, mail->flags_seen_field);
7713 _bind_stmt_field_data_char (hStmt, i++, mail->flags_deleted_field);
7714 _bind_stmt_field_data_char (hStmt, i++, mail->flags_flagged_field);
7715 _bind_stmt_field_data_char (hStmt, i++, mail->flags_answered_field);
7716 _bind_stmt_field_data_char (hStmt, i++, mail->flags_recent_field);
7717 _bind_stmt_field_data_char (hStmt, i++, mail->flags_draft_field);
7718 _bind_stmt_field_data_char (hStmt, i++, mail->flags_forwarded_field);
7719 _bind_stmt_field_data_int (hStmt, i++, mail->DRM_status);
7720 _bind_stmt_field_data_int (hStmt, i++, mail->priority);
7721 _bind_stmt_field_data_int (hStmt, i++, mail->save_status);
7722 _bind_stmt_field_data_int (hStmt, i++, mail->lock_status);
7723 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->message_id, 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
7724 _bind_stmt_field_data_int (hStmt, i++, mail->report_status);
7725 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
7726 _bind_stmt_field_data_int (hStmt, i++, mail->smime_type);
7728 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7729 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7730 ("sqlite3_step fail:%d", rc));
7732 rc = sqlite3_changes(local_db_handle);
7734 EM_DEBUG_EXCEPTION(" no matched mail found...");
7735 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7738 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
7739 if (!emstorage_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail->mail_id, mailbox_id_param_string, 0))
7740 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event Failed [ NOTI_MAIL_UPDATE ] >>>> ");
7745 if (hStmt != NULL) {
7746 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
7747 rc = sqlite3_finalize(hStmt);
7748 if (rc != SQLITE_OK) {
7749 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
7750 error = EMAIL_ERROR_DB_FAILURE;
7754 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
7757 if (err_code != NULL)
7760 EM_DEBUG_FUNC_END("ret [%d]", ret);
7764 INTERNAL_FUNC int emstorage_modify_mailbox_of_mails(char *old_mailbox_name, char *new_mailbox_name, int transaction, int *err_code)
7766 EM_DEBUG_FUNC_BEGIN("old_mailbox_name[%p], new_mailbox_name[%p], transaction[%d], err_code[%p]", old_mailbox_name, new_mailbox_name, transaction, err_code);
7768 if (!old_mailbox_name && !new_mailbox_name) {
7769 EM_DEBUG_EXCEPTION(" old_mailbox_name[%p], new_mailbox_name[%p]", old_mailbox_name, new_mailbox_name);
7771 if (err_code != NULL)
7772 *err_code = EMAIL_ERROR_INVALID_PARAM;
7778 int error = EMAIL_ERROR_NONE;
7779 char sql_query_string[QUERY_SIZE] = {0, };
7781 sqlite3 *local_db_handle = emstorage_get_db_connection();
7783 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
7785 EM_DEBUG_LOG("Old Mailbox Name [ %s ] , New Mailbox name [ %s ] ", old_mailbox_name, new_mailbox_name);
7787 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET mailbox_name = '%s' WHERE mailbox_name = '%s'", new_mailbox_name, old_mailbox_name);
7789 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
7790 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7791 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7793 rc = sqlite3_changes(local_db_handle);
7795 EM_DEBUG_EXCEPTION(" no matched mail found...");
7797 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7801 EM_DEBUG_LOG(" Modification done in mail_read_mail_uid_tbl based on Mailbox name ");
7802 /* Modify the mailbox_name name in mail_read_mail_uid_tbl table */
7803 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_read_mail_uid_tbl SET mailbox_name = '%s' WHERE mailbox_name = '%s'", new_mailbox_name, old_mailbox_name);
7805 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
7806 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7807 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7809 rc = sqlite3_changes(local_db_handle);
7811 EM_DEBUG_EXCEPTION(" no matched mail found...");
7812 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7816 if (!emstorage_notify_storage_event(NOTI_MAILBOX_UPDATE, 1, 0, new_mailbox_name, 0))
7817 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event[ NOTI_MAILBOX_UPDATE] : Notification Failed >>> ");
7823 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
7826 if (err_code != NULL)
7829 EM_DEBUG_FUNC_END("ret [%d]", ret);
7834 * emstorage_clean_save_status(int save_status, int *err_code) - set the all mail status to the set value
7838 INTERNAL_FUNC int emstorage_clean_save_status(int save_status, int *err_code)
7840 EM_DEBUG_FUNC_BEGIN("save_status[%d], err_code[%p]", save_status, err_code);
7842 EM_IF_NULL_RETURN_VALUE(err_code, false);
7845 int error = EMAIL_ERROR_NONE;
7847 char sql_query_string[QUERY_SIZE] = {0, };
7848 sqlite3 *local_db_handle = emstorage_get_db_connection();
7850 memset(sql_query_string, 0x00, sizeof(sql_query_string));
7851 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);
7852 EM_DEBUG_LOG("Query [%s]", sql_query_string);
7854 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
7855 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7856 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7858 rc = sqlite3_changes(local_db_handle);
7860 EM_DEBUG_LOG(" No Matched Mail Exists ");
7861 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7868 if (err_code != NULL)
7871 EM_DEBUG_FUNC_END("ret [%d]", ret);
7875 INTERNAL_FUNC int emstorage_set_field_of_mails_with_integer_value(int account_id, int mail_ids[], int mail_ids_count, char *field_name, int value, int transaction, int *err_code)
7877 EM_DEBUG_FUNC_BEGIN("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);
7879 int error = EMAIL_ERROR_NONE;
7882 int cur_mail_id_string = 0;
7883 int mail_id_string_buffer_length = 0;
7884 char sql_query_string[QUERY_SIZE] = {0, };
7885 char *mail_id_string_buffer = NULL;
7886 char *parameter_string = NULL;
7887 sqlite3 *local_db_handle = emstorage_get_db_connection();
7888 email_mail_attribute_type target_mail_attribute_type = 0;
7890 if (!mail_ids || !field_name || account_id == 0) {
7891 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
7892 if (err_code != NULL)
7893 *err_code = EMAIL_ERROR_INVALID_PARAM;
7897 if( (error = _get_attribute_type_by_mail_field_name(field_name, &target_mail_attribute_type)) != EMAIL_ERROR_NONE) {
7898 EM_DEBUG_EXCEPTION("emstorage_get_attribute_type_by_mail_field_name failed [%d]", error);
7899 if (err_code != NULL)
7904 /* Generating mail id list string */
7905 mail_id_string_buffer_length = MAIL_ID_STRING_LENGTH * mail_ids_count;
7907 mail_id_string_buffer = em_malloc(mail_id_string_buffer_length);
7909 if(!mail_id_string_buffer) {
7910 EM_DEBUG_EXCEPTION("em_malloc failed");
7911 error = EMAIL_ERROR_OUT_OF_MEMORY;
7915 for(i = 0; i < mail_ids_count; i++)
7916 cur_mail_id_string += SNPRINTF_OFFSET(mail_id_string_buffer, cur_mail_id_string, mail_id_string_buffer_length, "%d,", mail_ids[i]);
7918 if(strlen(mail_id_string_buffer) > 1)
7919 mail_id_string_buffer[strlen(mail_id_string_buffer) - 1] = NULL_CHAR;
7921 /* Generating notification parameter string */
7922 parameter_string = em_malloc(mail_id_string_buffer_length + strlen(field_name) + 2);
7924 if(!parameter_string) {
7925 EM_DEBUG_EXCEPTION("em_malloc failed");
7926 error = EMAIL_ERROR_OUT_OF_MEMORY;
7930 SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", field_name, 0x01, mail_id_string_buffer);
7932 /* Write query string */
7933 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);
7935 EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
7938 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
7939 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
7940 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7941 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7942 if (sqlite3_changes(local_db_handle) == 0)
7943 EM_DEBUG_LOG("no mail matched...");
7948 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
7951 if (ret && parameter_string && !emstorage_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, target_mail_attribute_type, parameter_string, value))
7952 EM_DEBUG_EXCEPTION("emstorage_notify_storage_event failed : NOTI_MAIL_FIELD_UPDATE [%s,%d]", field_name, value);
7954 EM_SAFE_FREE(mail_id_string_buffer);
7955 EM_SAFE_FREE(parameter_string);
7957 if (err_code != NULL)
7960 EM_DEBUG_FUNC_END("error [%d]", error);
7964 INTERNAL_FUNC int emstorage_change_mail_field(int mail_id, email_mail_change_type_t type, emstorage_mail_tbl_t* mail, int transaction, int *err_code)
7966 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
7968 if (mail_id <= 0 || !mail) {
7969 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], mail[%p]", mail_id, type, mail);
7970 if (err_code != NULL)
7971 *err_code = EMAIL_ERROR_INVALID_PARAM;
7976 int error = EMAIL_ERROR_NONE;
7978 DB_STMT hStmt = NULL;
7979 char sql_query_string[QUERY_SIZE] = {0, };
7984 char *mailbox_name = NULL;
7985 char mailbox_id_param_string[10] = {0,};
7987 sqlite3 *local_db_handle = emstorage_get_db_connection();
7988 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
7992 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7993 "UPDATE mail_tbl SET"
7994 " body_download_status = ?"
7995 ", file_path_plain = ?"
7996 ", file_path_html = ?"
7997 ", flags_seen_field = ?"
7998 ", flags_deleted_field = ?"
7999 ", flags_flagged_field = ?"
8000 ", flags_answered_field = ?"
8001 ", flags_recent_field = ?"
8002 ", flags_draft_field = ?"
8003 ", flags_forwarded_field = ?"
8005 ", attachment_count = ?"
8007 ", meeting_request_status = ? "
8008 ", message_class = ? "
8009 ", digest_type = ? "
8011 " WHERE mail_id = %d AND account_id != 0"
8015 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
8016 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8017 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8020 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
8021 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8022 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8023 _bind_stmt_field_data_int(hStmt, i++, mail->flags_seen_field);
8024 _bind_stmt_field_data_int(hStmt, i++, mail->flags_deleted_field);
8025 _bind_stmt_field_data_int(hStmt, i++, mail->flags_flagged_field);
8026 _bind_stmt_field_data_int(hStmt, i++, mail->flags_answered_field);
8027 _bind_stmt_field_data_int(hStmt, i++, mail->flags_recent_field);
8028 _bind_stmt_field_data_int(hStmt, i++, mail->flags_draft_field);
8029 _bind_stmt_field_data_int(hStmt, i++, mail->flags_forwarded_field);
8030 _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
8031 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
8032 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
8033 _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
8034 _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
8035 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
8036 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
8039 case UPDATE_MAILBOX: {
8041 emstorage_mailbox_tbl_t *mailbox_tbl;
8043 if (!emstorage_get_mailbox_by_name(mail->account_id, -1, mail->mailbox_name, &mailbox_tbl, false, &err)) {
8044 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_name failed - %d", err);
8049 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8050 "UPDATE mail_tbl SET"
8051 " mailbox_id = '%d'"
8052 " mailbox_name = '%s'"
8053 ",mailbox_type = '%d'"
8054 " WHERE mail_id = %d AND account_id != 0"
8055 , mailbox_tbl->mailbox_id
8056 , mail->mailbox_name ? mail->mailbox_name : ""
8057 , mailbox_tbl->mailbox_type
8062 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8064 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, 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)));
8071 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8072 "UPDATE mail_tbl SET"
8073 " flags_seen_field = %d"
8074 ",flags_deleted_field = %d"
8075 ",flags_flagged_field = %d"
8076 ",flags_answered_field = %d"
8077 ",flags_recent_field = %d"
8078 ",flags_draft_field = %d"
8079 ",flags_forwarded_field = %d"
8080 " WHERE mail_id = %d AND account_id != 0"
8081 , mail->flags_seen_field
8082 , mail->flags_deleted_field
8083 , mail->flags_flagged_field
8084 , mail->flags_answered_field
8085 , mail->flags_recent_field
8086 , mail->flags_draft_field
8087 , mail->flags_forwarded_field
8089 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8092 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
8093 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8094 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8098 case UPDATE_EXTRA_FLAG:
8099 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8100 "UPDATE mail_tbl SET"
8102 ", save_status = %d"
8103 ", lock_status = %d"
8104 ", report_status = %d"
8106 " WHERE mail_id = %d AND account_id != 0"
8110 , mail->report_status
8113 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8116 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
8117 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8118 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8121 case UPDATE_STICKY_EXTRA_FLAG:
8122 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8123 "UPDATE mail_tbl SET"
8125 " WHERE mail_id = %d AND account_id != 0"
8128 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8131 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
8132 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8133 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8137 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8138 "UPDATE mail_tbl SET"
8139 " full_address_from = ?"
8140 ", full_address_reply = ?"
8141 ", full_address_to = ?"
8142 ", full_address_cc = ?"
8143 ", full_address_bcc = ?"
8144 ", full_address_return = ?"
8146 ", file_path_plain = ?"
8148 ", flags_seen_field = ?"
8149 ", flags_deleted_field = ?"
8150 ", flags_flagged_field = ?"
8151 ", flags_answered_field = ?"
8152 ", flags_recent_field = ?"
8153 ", flags_draft_field = ?"
8154 ", flags_forwarded_field = ?"
8158 ", report_status = ?"
8160 ", file_path_html = ?"
8162 ", preview_text = ?"
8163 ", body_download_status = ?"
8164 ", attachment_count = ?"
8165 ", inline_content_count = ?"
8166 ", meeting_request_status = ?"
8167 ", message_class = ?"
8170 " WHERE mail_id = %d AND account_id != 0"
8174 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
8175 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
8176 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8177 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8179 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
8180 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
8181 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
8182 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
8183 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
8184 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
8185 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8186 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8187 _bind_stmt_field_data_time_t(hStmt, i++, mail->date_time);
8188 _bind_stmt_field_data_char (hStmt, i++, mail->flags_seen_field);
8189 _bind_stmt_field_data_char (hStmt, i++, mail->flags_deleted_field);
8190 _bind_stmt_field_data_char (hStmt, i++, mail->flags_flagged_field);
8191 _bind_stmt_field_data_char (hStmt, i++, mail->flags_answered_field);
8192 _bind_stmt_field_data_char (hStmt, i++, mail->flags_recent_field);
8193 _bind_stmt_field_data_char (hStmt, i++, mail->flags_draft_field);
8194 _bind_stmt_field_data_char (hStmt, i++, mail->flags_forwarded_field);
8195 _bind_stmt_field_data_int (hStmt, i++, mail->priority);
8196 _bind_stmt_field_data_int (hStmt, i++, mail->save_status);
8197 _bind_stmt_field_data_int (hStmt, i++, mail->lock_status);
8198 _bind_stmt_field_data_int (hStmt, i++, mail->report_status);
8199 _bind_stmt_field_data_int (hStmt, i++, mail->DRM_status);
8200 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8201 _bind_stmt_field_data_int (hStmt, i++, mail->mail_size);
8202 _bind_stmt_field_data_string(hStmt, i++, (char*)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8203 _bind_stmt_field_data_int (hStmt, i++, mail->body_download_status);
8204 _bind_stmt_field_data_int (hStmt, i++, mail->attachment_count);
8205 _bind_stmt_field_data_int (hStmt, i++, mail->inline_content_count);
8206 _bind_stmt_field_data_int (hStmt, i++, mail->meeting_request_status);
8207 _bind_stmt_field_data_int (hStmt, i++, mail->message_class);
8208 _bind_stmt_field_data_int (hStmt, i++, mail->digest_type);
8209 _bind_stmt_field_data_int (hStmt, i++, mail->smime_type);
8212 case UPDATE_DATETIME: {
8213 time_t now = time(NULL);
8215 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8216 "UPDATE mail_tbl SET"
8218 " WHERE mail_id = %d AND account_id != 0"
8222 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
8223 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8224 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8228 case UPDATE_FROM_CONTACT_INFO:
8229 EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_FROM_CONTACT_INFO");
8230 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8231 "UPDATE mail_tbl SET"
8232 " email_address_sender = ?,"
8233 " WHERE mail_id = %d",
8238 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
8239 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8240 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8242 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8245 case UPDATE_TO_CONTACT_INFO:
8246 EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_TO_CONTACT_INFO");
8247 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8248 "UPDATE mail_tbl SET"
8249 " email_address_recipient = ?,"
8250 " WHERE mail_id = %d",
8255 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
8256 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8257 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8259 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8262 case UPDATE_ALL_CONTACT_INFO:
8263 EM_DEBUG_LOG("emstorage_change_mail_field - mail change type is UPDATE_ALL_CONTACT_INFO");
8264 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8265 "UPDATE mail_tbl SET"
8266 " email_address_sender = ?,"
8267 " email_address_recipient = ?,"
8268 " WHERE mail_id = %d",
8273 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
8274 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8275 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8277 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8278 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
8282 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
8283 case UPDATE_PARTIAL_BODY_DOWNLOAD:
8285 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8286 "UPDATE mail_tbl SET"
8287 " body_download_status = ?"
8288 ", file_path_plain = ?"
8289 ", file_path_html = ?"
8290 ", attachment_count = ?"
8291 ", inline_content_count = ?"
8293 " WHERE mail_id = %d"
8297 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
8298 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8299 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8302 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
8303 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8304 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8305 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
8306 _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
8307 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
8314 EM_DEBUG_LOG(" type[%d]", type);
8316 error = EMAIL_ERROR_INVALID_PARAM;
8320 if (hStmt != NULL) {
8322 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8323 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
8324 ("sqlite3_step fail:%d", rc));
8325 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8326 ("sqlite3_step fail:%d", rc));
8327 rc = sqlite3_changes(local_db_handle);
8329 EM_DEBUG_EXCEPTION(" no matched mail found...");
8331 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8336 if (mail->account_id == 0) {
8337 emstorage_mail_tbl_t* mail_for_account_tbl = NULL;
8338 if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_ACCOUNT, &mail_for_account_tbl, true, &error) || !mail_for_account_tbl) {
8339 EM_DEBUG_EXCEPTION(" emstorage_get_mail_field_by_id failed - %d", error);
8343 mail->account_id = mail_for_account_tbl->account_id;
8344 if (mail_for_account_tbl)
8345 emstorage_free_mail(&mail_for_account_tbl, 1, NULL);
8352 if (hStmt != NULL) {
8353 rc = sqlite3_finalize(hStmt);
8354 if (rc != SQLITE_OK) {
8355 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
8356 error = EMAIL_ERROR_DB_FAILURE;
8361 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8364 /*h.gahlaut@samsung.com: Moving publication of notification after commiting transaction to DB */
8366 if (ret == true && move_flag != 1) {
8367 if (!emstorage_get_mailbox_name_by_mailbox_type(mail->account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &mailbox_name, false, &error))
8368 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_name_by_mailbox_type failed - %d", error);
8370 if (mail->mailbox_name && mailbox_name) {
8371 if (strcmp(mail->mailbox_name, mailbox_name) != 0) {
8372 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
8373 if (!emstorage_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, mailbox_id_param_string, type))
8374 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event Failed [ NOTI_MAIL_UPDATE ] >>>> ");
8378 /* h.gahlaut@samsung.com: Jan 10, 2011 Publishing noti to refresh outbox when email sending status changes */
8379 if (!emstorage_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, NULL, type))
8380 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event Failed [ NOTI_MAIL_UPDATE ] ");
8384 EM_SAFE_FREE(mailbox_name);
8386 if (err_code != NULL)
8389 EM_DEBUG_FUNC_END("ret [%d]", ret);
8392 INTERNAL_FUNC int emstorage_increase_mail_id(int *mail_id, int transaction, int *err_code)
8394 EM_DEBUG_FUNC_BEGIN("mail_id[%p], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
8396 int rc, ret = false;
8397 int error = EMAIL_ERROR_NONE;
8398 int latest_mail_id = 0;
8399 sqlite3 *local_db_handle = NULL;
8400 char *sql = "SELECT MAX(mail_id) FROM mail_tbl;";
8401 char **result = NULL;
8403 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
8404 _timedlock_shm_mutex(&mapped_for_generating_mail_id, 2);
8405 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
8407 ret = vconf_get_int(VCONF_KEY_LATEST_MAIL_ID, &latest_mail_id);
8408 if (ret < 0 || latest_mail_id == 0) {
8409 EM_DEBUG_LOG("vconf_get_int() failed [%d] or latest_mail_id is zero", ret);
8411 local_db_handle = emstorage_get_db_connection();
8413 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
8414 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
8415 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
8416 if (NULL == result[1])
8419 rc = atoi(result[1]) + 1;
8421 sqlite3_free_table(result);
8422 latest_mail_id = rc;
8427 ret = vconf_set_int(VCONF_KEY_LATEST_MAIL_ID, latest_mail_id);
8430 *mail_id = latest_mail_id;
8432 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
8433 _unlockshm_mutex(&mapped_for_generating_mail_id);
8434 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
8441 if (err_code != NULL)
8444 EM_DEBUG_FUNC_END("ret [%d]", ret);
8449 INTERNAL_FUNC int emstorage_add_mail(emstorage_mail_tbl_t *mail_tbl_data, int get_id, int transaction, int *err_code)
8451 EM_PROFILE_BEGIN(profile_emstorage_add_mail);
8452 EM_DEBUG_FUNC_BEGIN("mail_tbl_data[%p], get_id[%d], transaction[%d], err_code[%p]", mail_tbl_data, get_id, transaction, err_code);
8454 if (!mail_tbl_data) {
8455 EM_DEBUG_EXCEPTION("mail_tbl_data[%p], get_id[%d]", mail_tbl_data, get_id);
8456 if (err_code != NULL)
8457 *err_code = EMAIL_ERROR_INVALID_PARAM;
8461 int rc, ret = false;
8462 int error = EMAIL_ERROR_NONE;
8463 char sql_query_string[QUERY_SIZE] = {0, };
8464 DB_STMT hStmt = NULL;
8465 sqlite3 *local_db_handle = emstorage_get_db_connection();
8467 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8470 /* increase unique id */
8471 char *sql = "SELECT max(rowid) FROM mail_tbl;";
8474 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
8475 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
8476 ("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle)));
8478 if (NULL == result[1])
8481 rc = atoi(result[1])+1;
8483 sqlite3_free_table(result);
8485 mail_tbl_data->mail_id = rc;
8486 mail_tbl_data->thread_id = rc;
8489 if (mail_tbl_data->date_time == 0)
8490 mail_tbl_data->date_time = time(NULL);
8492 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8493 "INSERT INTO mail_tbl VALUES "
8495 ", ?" /* account_id */
8496 ", ?" /* mailbox_id */
8497 ", ?" /* mailbox_name */
8498 ", ?" /* mailbox_type */
8500 ", ?" /* date_time */
8501 ", ?" /* server_mail_status */
8502 ", ?" /* server_mailbox_name */
8503 ", ?" /* server_mail_id */
8504 ", ?" /* message_id */
8505 ", ?" /* full_address_from */
8506 ", ?" /* full_address_reply */
8507 ", ?" /* full_address_to */
8508 ", ?" /* full_address_cc */
8509 ", ?" /* full_address_bcc */
8510 ", ?" /* full_address_return */
8511 ", ?" /* email_address_sender */
8512 ", ?" /* email_address_recipient */
8513 ", ?" /* alias_sender */
8514 ", ?" /* alias_recipient */
8515 ", ?" /* body_download_status */
8516 ", ?" /* file_path_plain */
8517 ", ?" /* file_path_html */
8518 ", ?" /* file_path_mime_entity */
8519 ", ?" /* mail_size */
8520 ", ?" /* flags_seen_field */
8521 ", ?" /* flags_deleted_field */
8522 ", ?" /* flags_flagged_field */
8523 ", ?" /* flags_answered_field */
8524 ", ?" /* flags_recent_field */
8525 ", ?" /* flags_draft_field */
8526 ", ?" /* flags_forwarded_field */
8527 ", ?" /* DRM_status */
8528 ", ?" /* priority */
8529 ", ?" /* save_status */
8530 ", ?" /* lock_status */
8531 ", ?" /* report_status */
8532 ", ?" /* attachment_count */
8533 ", ?" /* inline_content_count */
8534 ", ?" /* thread_id */
8535 ", ?" /* thread_item_count */
8536 ", ?" /* preview_text */
8537 ", ?" /* meeting_request_status */
8538 ", ?" /* message_class */
8539 ", ?" /* digest_type */
8540 ", ?" /* smime_type */
8543 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
8544 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8545 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8547 _bind_stmt_field_data_int (hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mail_id);
8548 _bind_stmt_field_data_int (hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, mail_tbl_data->account_id);
8549 _bind_stmt_field_data_int (hStmt, MAILBOX_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_id);
8550 _bind_stmt_field_data_string(hStmt, MAILBOX_NAME_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->mailbox_name, 0, MAILBOX_LEN_IN_MAIL_TBL);
8551 _bind_stmt_field_data_int (hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_type);
8552 _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8553 _bind_stmt_field_data_int (hStmt, DATETIME_IDX_IN_MAIL_TBL, mail_tbl_data->date_time);
8554 _bind_stmt_field_data_int (hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->server_mail_status);
8555 _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);
8556 _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);
8557 _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);
8558 _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);
8559 _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);
8560 _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);
8561 _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);
8562 _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);
8563 _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);
8564 _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);
8565 _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);
8566 _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);
8567 _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);
8568 _bind_stmt_field_data_int (hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->body_download_status);
8569 _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);
8570 _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);
8571 _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);
8572 _bind_stmt_field_data_int (hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, mail_tbl_data->mail_size);
8573 _bind_stmt_field_data_int (hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_seen_field);
8574 _bind_stmt_field_data_int (hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_deleted_field);
8575 _bind_stmt_field_data_int (hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_flagged_field);
8576 _bind_stmt_field_data_int (hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_answered_field);
8577 _bind_stmt_field_data_int (hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_recent_field);
8578 _bind_stmt_field_data_int (hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_draft_field);
8579 _bind_stmt_field_data_int (hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_forwarded_field);
8580 _bind_stmt_field_data_int (hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->DRM_status);
8581 _bind_stmt_field_data_int (hStmt, PRIORITY_IDX_IN_MAIL_TBL, mail_tbl_data->priority);
8582 _bind_stmt_field_data_int (hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->save_status);
8583 _bind_stmt_field_data_int (hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->lock_status);
8584 _bind_stmt_field_data_int (hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->report_status);
8585 _bind_stmt_field_data_int (hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->attachment_count);
8586 _bind_stmt_field_data_int (hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->inline_content_count);
8587 _bind_stmt_field_data_int (hStmt, THREAD_ID_IDX_IN_MAIL_TBL, mail_tbl_data->thread_id);
8588 _bind_stmt_field_data_int (hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->thread_item_count);
8589 _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8590 _bind_stmt_field_data_int (hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->meeting_request_status);
8591 _bind_stmt_field_data_int (hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, mail_tbl_data->message_class);
8592 _bind_stmt_field_data_int (hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->digest_type);
8593 _bind_stmt_field_data_int (hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->smime_type);
8595 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8596 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
8597 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
8602 if (hStmt != NULL) {
8603 rc = sqlite3_finalize(hStmt);
8604 if (rc != SQLITE_OK) {
8605 EM_DEBUG_LOG("sqlite3_finalize failed [%d]", rc);
8606 error = EMAIL_ERROR_DB_FAILURE;
8610 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8614 if (err_code != NULL)
8617 EM_PROFILE_END(profile_emstorage_add_mail);
8618 EM_DEBUG_FUNC_END("ret [%d]", ret);
8622 INTERNAL_FUNC int emstorage_move_multiple_mails(int account_id, int input_mailbox_id, int mail_ids[], int number_of_mails, int transaction, int *err_code)
8624 EM_DEBUG_FUNC_BEGIN("account_id [%d], input_mailbox_id [%d], mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", account_id, input_mailbox_id, mail_ids, number_of_mails, transaction, err_code);
8626 int rc, ret = false, i, cur_conditional_clause = 0;
8627 int error = EMAIL_ERROR_NONE;
8628 char sql_query_string[QUERY_SIZE] = {0, }, conditional_clause[QUERY_SIZE] = {0, };
8629 emstorage_mailbox_tbl_t *result_mailbox = NULL;
8630 email_mailbox_type_e target_mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED;
8631 char* target_mailbox_name = NULL;
8633 if (!mail_ids || input_mailbox_id <= 0) {
8634 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8635 if (err_code != NULL)
8636 *err_code = EMAIL_ERROR_INVALID_PARAM;
8640 sqlite3 *local_db_handle = emstorage_get_db_connection();
8642 if ((error = emstorage_get_mailbox_by_id(input_mailbox_id, &result_mailbox)) != EMAIL_ERROR_NONE || !result_mailbox) {
8643 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", error);
8647 if(result_mailbox) {
8648 target_mailbox_name = EM_SAFE_STRDUP(result_mailbox->mailbox_name);
8649 target_mailbox_type = result_mailbox->mailbox_type;
8650 emstorage_free_mailbox(&result_mailbox, 1, NULL);
8653 cur_conditional_clause = SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id in (");
8655 for(i = 0; i < number_of_mails; i++)
8656 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, QUERY_SIZE, "%d,", mail_ids[i]);
8658 conditional_clause[strlen(conditional_clause) - 1] = ')';
8660 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8662 /* Updating a mail_tbl */
8664 memset(sql_query_string, 0x00, QUERY_SIZE);
8665 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_tbl SET mailbox_name = '%s', mailbox_type = %d, mailbox_id = %d %s", target_mailbox_name, target_mailbox_type, input_mailbox_id, conditional_clause);
8666 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8668 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8669 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
8670 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8672 /* Updating a mail_attachment_tbl */
8674 memset(sql_query_string, 0x00, QUERY_SIZE);
8675 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_attachment_tbl SET mailbox_id = '%d' %s", input_mailbox_id, conditional_clause);
8676 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8678 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8679 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
8680 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8682 /* Updating a mail_meeting_tbl */
8683 memset(sql_query_string, 0x00, QUERY_SIZE);
8684 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_meeting_tbl SET mailbox_id = %d %s", input_mailbox_id, conditional_clause);
8685 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8687 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8688 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
8689 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8691 /* Updating a mail_read_mail_uid_tbl */
8692 memset(conditional_clause, 0x00, QUERY_SIZE);
8693 cur_conditional_clause = SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE local_uid in (");
8695 for(i = 0; i < number_of_mails; i++)
8696 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, QUERY_SIZE, "%d,", mail_ids[i]);
8698 conditional_clause[strlen(conditional_clause) - 1] = ')';
8700 memset(sql_query_string, 0x00, QUERY_SIZE);
8701 SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_read_mail_uid_tbl SET mailbox_name = '%s', mailbox_id = %d %s", target_mailbox_name, input_mailbox_id, conditional_clause);
8702 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8704 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8705 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
8706 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8711 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8714 EM_SAFE_FREE(target_mailbox_name);
8716 if (err_code != NULL)
8719 EM_DEBUG_FUNC_END("ret [%d]", ret);
8723 INTERNAL_FUNC int emstorage_delete_mail(int mail_id, int from_server, int transaction, int *err_code)
8725 EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
8728 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
8729 if (err_code != NULL)
8730 *err_code = EMAIL_ERROR_INVALID_PARAM;
8734 int rc, ret = false;
8735 int error = EMAIL_ERROR_NONE;
8736 char sql_query_string[QUERY_SIZE] = {0, };
8738 sqlite3 *local_db_handle = emstorage_get_db_connection();
8739 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8741 memset(sql_query_string, 0x00, sizeof(sql_query_string));
8743 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id = %d ", mail_id);
8744 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8747 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8748 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
8749 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8754 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8757 if (err_code != NULL)
8760 EM_DEBUG_FUNC_END("ret [%d]", ret);
8764 INTERNAL_FUNC int emstorage_delete_multiple_mails(int mail_ids[], int number_of_mails, int transaction, int *err_code)
8766 EM_DEBUG_FUNC_BEGIN("mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", mail_ids, number_of_mails, transaction, err_code);
8768 int rc, ret = false, i, cur_sql_query_string = 0;
8769 int error = EMAIL_ERROR_NONE;
8770 char sql_query_string[QUERY_SIZE] = {0, };
8773 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8774 if (err_code != NULL)
8775 *err_code = EMAIL_ERROR_INVALID_PARAM;
8779 sqlite3 *local_db_handle = emstorage_get_db_connection();
8780 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8782 cur_sql_query_string = SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id in (");
8784 for(i = 0; i < number_of_mails; i++)
8785 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, QUERY_SIZE, "%d,", mail_ids[i]);
8787 sql_query_string[strlen(sql_query_string) - 1] = ')';
8789 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8791 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8792 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
8793 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8798 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8801 if (err_code != NULL)
8804 EM_DEBUG_FUNC_END("ret [%d]", ret);
8808 INTERNAL_FUNC int emstorage_delete_mail_by_account(int account_id, int transaction, int *err_code)
8810 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
8812 if (account_id < FIRST_ACCOUNT_ID) {
8813 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
8815 if (err_code != NULL)
8816 *err_code = EMAIL_ERROR_INVALID_PARAM;
8820 int rc, ret = false;
8821 int error = EMAIL_ERROR_NONE;
8822 char sql_query_string[QUERY_SIZE] = {0, };
8824 sqlite3 *local_db_handle = emstorage_get_db_connection();
8825 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8827 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d", account_id);
8828 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8830 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8831 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8832 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8834 rc = sqlite3_changes(local_db_handle);
8836 EM_DEBUG_EXCEPTION(" no mail found...");
8837 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8840 /* Delete all mails mail_read_mail_uid_tbl table based on account id */
8841 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
8842 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8844 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8845 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8846 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8848 rc = sqlite3_changes(local_db_handle);
8850 EM_DEBUG_EXCEPTION("no mail found...");
8851 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8854 if (!emstorage_notify_storage_event(NOTI_MAIL_DELETE_WITH_ACCOUNT, account_id, 0 , NULL, 0))
8855 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event Failed [ NOTI_MAIL_DELETE_ALL ]");
8861 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8864 if (err_code != NULL)
8867 EM_DEBUG_FUNC_END("ret [%d]", ret);
8871 INTERNAL_FUNC int emstorage_delete_mail_by_mailbox(int account_id, char *mailbox, int transaction, int *err_code)
8873 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox, transaction, err_code);
8875 if (account_id < FIRST_ACCOUNT_ID || !mailbox) {
8876 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox[%p]", account_id, mailbox);
8877 if (err_code != NULL)
8878 *err_code = EMAIL_ERROR_INVALID_PARAM;
8882 int rc, ret = false;
8883 int error = EMAIL_ERROR_NONE;
8884 char sql_query_string[QUERY_SIZE] = {0, };
8886 sqlite3 *local_db_handle = emstorage_get_db_connection();
8888 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
8890 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d AND mailbox_name = '%s'", account_id, mailbox);
8891 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8893 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8894 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8895 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8898 /* Delete Mails from mail_read_mail_uid_tbl */
8899 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d AND mailbox_name = '%s'", account_id, mailbox);
8900 EM_DEBUG_LOG("Query [%s]", sql_query_string);
8902 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
8903 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8904 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8906 if (!emstorage_notify_storage_event(NOTI_MAIL_DELETE_ALL, account_id, 0 , mailbox, 0))
8907 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event Failed [ NOTI_MAIL_DELETE_ALL ] >>>> ");
8912 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
8915 if (err_code != NULL)
8918 EM_DEBUG_FUNC_END("ret [%d]", ret);
8922 INTERNAL_FUNC int emstorage_free_mail(emstorage_mail_tbl_t** mail_list, int count, int *err_code)
8924 EM_DEBUG_FUNC_BEGIN("mail_list[%p], count[%d], err_code[%p]", mail_list, count, err_code);
8927 if ((mail_list == NULL) || (*mail_list == NULL)) {
8928 EM_DEBUG_EXCEPTION("mail_ilst[%p], count[%d]", mail_list, count);
8931 *err_code = EMAIL_ERROR_INVALID_PARAM;
8935 emstorage_mail_tbl_t* p = *mail_list;
8938 for (; i < count; i++, p++) {
8939 EM_SAFE_FREE(p->mailbox_name);
8940 EM_SAFE_FREE(p->server_mailbox_name);
8941 EM_SAFE_FREE(p->server_mail_id);
8942 EM_SAFE_FREE(p->full_address_from);
8943 EM_SAFE_FREE(p->full_address_reply);
8944 EM_SAFE_FREE(p->full_address_to);
8945 EM_SAFE_FREE(p->full_address_cc);
8946 EM_SAFE_FREE(p->full_address_bcc);
8947 EM_SAFE_FREE(p->full_address_return);
8948 EM_SAFE_FREE(p->subject);
8949 EM_SAFE_FREE(p->file_path_plain);
8950 EM_SAFE_FREE(p->file_path_html);
8951 EM_SAFE_FREE(p->file_path_mime_entity);
8952 EM_SAFE_FREE(p->message_id);
8953 EM_SAFE_FREE(p->email_address_sender);
8954 EM_SAFE_FREE(p->email_address_recipient);
8955 EM_SAFE_FREE(p->preview_text);
8956 EM_SAFE_FREE(p->alias_sender);
8957 EM_SAFE_FREE(p->alias_recipient);
8959 EM_SAFE_FREE(*mail_list);
8962 if (err_code != NULL)
8963 *err_code = EMAIL_ERROR_NONE;
8965 EM_DEBUG_FUNC_END();
8969 INTERNAL_FUNC int emstorage_get_attachment_count(int mail_id, int *count, int transaction, int *err_code)
8971 EM_DEBUG_FUNC_BEGIN("mail_id[%d], count[%p], transaction[%d], err_code[%p]", mail_id, count, transaction, err_code);
8973 if (mail_id <= 0 || !count) {
8974 EM_DEBUG_EXCEPTION("mail_id[%d], count[%p]", mail_id, count);
8975 if (err_code != NULL)
8976 *err_code = EMAIL_ERROR_INVALID_PARAM;
8980 int rc = -1, ret = false;
8981 int error = EMAIL_ERROR_NONE;
8982 char sql_query_string[QUERY_SIZE] = {0, };
8984 sqlite3 *local_db_handle = emstorage_get_db_connection();
8985 EMSTORAGE_START_READ_TRANSACTION(transaction);
8987 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
8991 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
8992 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
8993 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8995 *count = atoi(result[1]);
8996 sqlite3_free_table(result);
9002 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
9005 if (err_code != NULL)
9008 EM_DEBUG_FUNC_END("ret [%d]", ret);
9012 INTERNAL_FUNC int emstorage_get_attachment_list(int input_mail_id, int input_transaction, emstorage_attachment_tbl_t** output_attachment_list, int *output_attachment_count)
9014 EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], input_transaction[%d], output_attachment_list[%p], output_attachment_count[%p]", input_mail_id, output_attachment_list, input_transaction, output_attachment_count);
9016 if (input_mail_id <= 0 || !output_attachment_list || !output_attachment_count) {
9017 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9018 return EMAIL_ERROR_INVALID_PARAM;
9021 int error = EMAIL_ERROR_NONE;
9024 char **result = NULL;
9025 char sql_query_string[QUERY_SIZE] = {0, };
9026 emstorage_attachment_tbl_t* p_data_tbl = NULL;
9027 DB_STMT hStmt = NULL;
9028 sqlite3 *local_db_handle = emstorage_get_db_connection();
9030 EMSTORAGE_START_READ_TRANSACTION(input_transaction);
9031 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", input_mail_id);
9032 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
9033 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9034 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9036 *output_attachment_count = atoi(result[1]);
9037 sqlite3_free_table(result);
9039 if(*output_attachment_count == 0) {
9040 error = EMAIL_ERROR_NONE;
9044 p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * (*output_attachment_count));
9047 EM_DEBUG_EXCEPTION("em_malloc failed...");
9048 error = EMAIL_ERROR_OUT_OF_MEMORY;
9052 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE mail_id = %d ORDER BY attachment_id", input_mail_id);
9053 EM_DEBUG_LOG("sql_query_string [%s]", sql_query_string);
9055 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
9056 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)));
9058 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9059 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9061 if (rc == SQLITE_DONE) {
9062 EM_DEBUG_EXCEPTION("no matched attachment found...");
9063 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
9066 for (i = 0; i < *output_attachment_count; i++) {
9067 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9068 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
9069 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
9070 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
9071 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9072 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9073 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9074 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9075 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9076 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
9077 _get_stmt_field_data_int (hStmt, &(p_data_tbl[i].attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9078 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9080 EM_DEBUG_LOG("attachment[%d].attachment_id : %d", i, p_data_tbl[i].attachment_id);
9081 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9082 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9087 if (error == EMAIL_ERROR_NONE)
9088 *output_attachment_list = p_data_tbl;
9089 else if (p_data_tbl != NULL)
9090 emstorage_free_attachment(&p_data_tbl, *output_attachment_count, NULL);
9092 rc = sqlite3_finalize(hStmt);
9094 if (rc != SQLITE_OK) {
9095 EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
9096 error = EMAIL_ERROR_DB_FAILURE;
9099 EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
9103 EM_DEBUG_FUNC_END("error [%d]", error);
9107 INTERNAL_FUNC int emstorage_get_attachment(int attachment_id, emstorage_attachment_tbl_t** attachment, int transaction, int *err_code)
9109 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], transaction[%d], err_code[%p]", attachment_id, attachment, transaction, err_code);
9111 if (attachment_id <= 0 || !attachment) {
9112 EM_DEBUG_EXCEPTION("attachment_id[%d], attachment[%p]", attachment_id, attachment);
9113 if (err_code != NULL)
9114 *err_code = EMAIL_ERROR_INVALID_PARAM;
9118 emstorage_attachment_tbl_t* p_data_tbl = NULL;
9119 int rc, ret = false;
9120 int error = EMAIL_ERROR_NONE;
9121 char sql_query_string[QUERY_SIZE] = {0, };
9123 sqlite3 *local_db_handle = emstorage_get_db_connection();
9124 EMSTORAGE_START_READ_TRANSACTION(transaction);
9126 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
9128 sqlite3_stmt* hStmt = NULL;
9130 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
9131 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
9133 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9134 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9137 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9138 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9139 ("sqlite3_step fail:%d", rc));
9141 if (rc == SQLITE_DONE) {
9142 EM_DEBUG_EXCEPTION("no matched attachment found...");
9143 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
9147 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
9148 EM_DEBUG_EXCEPTION("malloc failed...");
9149 error = EMAIL_ERROR_OUT_OF_MEMORY;
9153 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9154 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
9155 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
9156 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
9157 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9158 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9159 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9160 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9161 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9162 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
9163 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9164 _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9166 #ifdef __ATTACHMENT_OPTI__
9167 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
9168 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
9175 *attachment = p_data_tbl;
9177 if (hStmt != NULL) {
9178 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
9180 rc = sqlite3_finalize(hStmt);
9181 if (rc != SQLITE_OK) {
9182 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
9183 error = EMAIL_ERROR_DB_FAILURE;
9187 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
9190 if (err_code != NULL)
9193 EM_DEBUG_FUNC_END("ret [%d]", ret);
9197 INTERNAL_FUNC int emstorage_get_attachment_nth(int mail_id, int nth, emstorage_attachment_tbl_t** attachment_tbl, int transaction, int *err_code)
9199 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);
9201 if (mail_id <= 0 || nth <= 0 || !attachment_tbl) {
9202 EM_DEBUG_EXCEPTION(" mail_id[%d], nth[%d], attachment[%p]", mail_id, nth, attachment_tbl);
9204 if (err_code != NULL)
9205 *err_code = EMAIL_ERROR_INVALID_PARAM;
9209 emstorage_attachment_tbl_t* p_data_tbl = NULL;
9211 int rc, ret = false;
9212 int error = EMAIL_ERROR_NONE;
9213 char sql_query_string[QUERY_SIZE] = {0, };
9215 sqlite3 *local_db_handle = emstorage_get_db_connection();
9216 EMSTORAGE_START_READ_TRANSACTION(transaction);
9218 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));
9219 EM_DEBUG_LOG("query = [%s]", sql_query_string);
9221 DB_STMT hStmt = NULL;
9223 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
9224 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9225 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9228 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9229 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9230 ("sqlite3_step fail:%d", rc));
9232 if (rc == SQLITE_DONE) {
9233 EM_DEBUG_EXCEPTION(" no matched attachment found...");
9234 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
9238 if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
9239 EM_DEBUG_EXCEPTION(" malloc failed...");
9240 error = EMAIL_ERROR_OUT_OF_MEMORY;
9244 p_data_tbl->attachment_id = sqlite3_column_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9245 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)strlen(p))
9246 p_data_tbl->attachment_name = cpy_str(p);
9247 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)strlen(p))
9248 p_data_tbl->attachment_path = cpy_str(p);
9249 p_data_tbl->attachment_size = sqlite3_column_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
9250 p_data_tbl->mail_id = sqlite3_column_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9251 p_data_tbl->account_id = sqlite3_column_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9252 p_data_tbl->mailbox_id = sqlite3_column_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
9253 p_data_tbl->attachment_save_status = sqlite3_column_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9254 p_data_tbl->attachment_drm_type = sqlite3_column_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
9255 p_data_tbl->attachment_drm_method = sqlite3_column_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
9256 p_data_tbl->attachment_inline_content_status = sqlite3_column_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
9257 if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)strlen(p))
9258 p_data_tbl->attachment_mime_type = cpy_str(p);
9259 #ifdef __ATTACHMENT_OPTI__
9260 p_data_tbl->encoding = sqlite3_column_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
9261 if ((p = (char *)sqlite3_column_text(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)strlen(p))
9262 p_data_tbl->section= cpy_str(p);
9268 *attachment_tbl = p_data_tbl;
9269 else if (p_data_tbl != NULL)
9270 emstorage_free_attachment(&p_data_tbl, 1, NULL);
9272 if (hStmt != NULL) {
9273 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
9275 rc = sqlite3_finalize(hStmt);
9276 if (rc != SQLITE_OK) {
9277 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
9278 error = EMAIL_ERROR_DB_FAILURE;
9282 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
9285 if (err_code != NULL)
9288 EM_DEBUG_FUNC_END("ret [%d]", ret);
9292 INTERNAL_FUNC int emstorage_change_attachment_field(int mail_id, email_mail_change_type_t type, emstorage_attachment_tbl_t* attachment, int transaction, int *err_code)
9294 EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], attachment[%p], transaction[%d], err_code[%p]", mail_id, type, attachment, transaction, err_code);
9296 if (mail_id <= 0 || !attachment) {
9297 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], attachment[%p]", mail_id, type, attachment);
9298 if (err_code != NULL)
9299 *err_code = EMAIL_ERROR_INVALID_PARAM;
9303 int rc, ret = false;
9304 int error = EMAIL_ERROR_NONE;
9305 DB_STMT hStmt = NULL;
9306 char sql_query_string[QUERY_SIZE] = {0, };
9310 sqlite3 *local_db_handle = emstorage_get_db_connection();
9312 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9315 case UPDATE_MAILBOX:
9316 EM_DEBUG_LOG("UPDATE_MAILBOX");
9317 if (!attachment->mailbox_id) {
9318 EM_DEBUG_EXCEPTION(" attachment->mailbox_id[%d]", attachment->mailbox_id);
9319 error = EMAIL_ERROR_INVALID_PARAM;
9322 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9323 "UPDATE mail_attachment_tbl SET mailbox_id = ? WHERE mail_id = %d", mail_id);
9325 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
9326 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
9327 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9328 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9330 _bind_stmt_field_data_int(hStmt, i++, attachment->mailbox_id);
9333 case UPDATE_SAVENAME:
9334 EM_DEBUG_LOG("UPDATE_SAVENAME");
9335 if (!attachment->attachment_path) {
9336 EM_DEBUG_EXCEPTION(" attachment->attachment_path[%p]", attachment->attachment_path);
9337 error = EMAIL_ERROR_INVALID_PARAM;
9341 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9342 "UPDATE mail_attachment_tbl SET"
9343 " attachment_size = ?"
9344 ", attachment_save_status = 1"
9345 ", attachment_path = ?"
9346 " WHERE mail_id = %d"
9347 " AND attachment_id = %d"
9348 , attachment->mail_id
9349 , attachment->attachment_id);
9352 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
9353 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
9354 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9355 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9357 _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_size);
9358 _bind_stmt_field_data_string(hStmt, i++, (char *)attachment->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
9362 EM_DEBUG_LOG("type[%d]", type);
9363 error = EMAIL_ERROR_INVALID_PARAM;
9366 EM_DEBUG_LOG("query = [%s]", sql_query_string);
9368 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9369 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9370 ("sqlite3_step fail:%d", rc));
9374 if (hStmt != NULL) {
9375 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
9376 rc = sqlite3_finalize(hStmt);
9377 if (rc != SQLITE_OK) {
9378 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
9379 error = EMAIL_ERROR_DB_FAILURE;
9383 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9386 if (err_code != NULL)
9388 EM_DEBUG_FUNC_END("ret [%d]", ret);
9393 INTERNAL_FUNC int emstorage_rename_mailbox(int input_mailbox_id, char *input_new_mailbox_name, char *input_new_mailbox_alias, int input_transaction)
9395 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], input_new_mailbox_name[%p], input_new_mailbox_alias [%p], input_transaction[%d]", input_mailbox_id, input_new_mailbox_name, input_new_mailbox_alias, input_transaction);
9399 int error = EMAIL_ERROR_NONE;
9400 char sql_query_string[QUERY_SIZE] = {0, };
9401 sqlite3 *local_db_handle = NULL;
9403 emstorage_mailbox_tbl_t *old_mailbox_data = NULL;
9405 if (input_mailbox_id <= 0 || !input_new_mailbox_name || !input_new_mailbox_alias) {
9406 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9407 return EMAIL_ERROR_INVALID_PARAM;
9410 local_db_handle = emstorage_get_db_connection();
9412 if ((error = emstorage_get_mailbox_by_id(input_mailbox_id, &old_mailbox_data)) != EMAIL_ERROR_NONE) {
9413 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", error);
9416 account_id = old_mailbox_data->account_id;
9418 EMSTORAGE_START_WRITE_TRANSACTION(input_transaction, error);
9420 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9421 "UPDATE mail_box_tbl SET"
9422 " mailbox_name = '%s'"
9424 " WHERE mailbox_id = %d"
9425 , input_new_mailbox_name
9426 , input_new_mailbox_alias
9427 , input_mailbox_id);
9429 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9431 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9432 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9434 if (sqlite3_changes(local_db_handle) == 0)
9435 EM_DEBUG_LOG("no mail_meeting_tbl matched...");
9437 /* Update mail_tbl */
9438 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9439 "UPDATE mail_tbl SET"
9440 " mailbox_name = '%s'"
9441 " WHERE mailbox_id = %d"
9442 , input_new_mailbox_name
9443 , input_mailbox_id);
9445 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9447 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9448 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9450 if (sqlite3_changes(local_db_handle) == 0)
9451 EM_DEBUG_LOG("no mail matched...");
9457 EMSTORAGE_FINISH_WRITE_TRANSACTION(input_transaction, ret, error);
9460 if (!emstorage_notify_storage_event(NOTI_MAILBOX_RENAME, account_id, input_mailbox_id, input_new_mailbox_name, 0))
9461 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event Failed [ NOTI_MAILBOX_RENAME ] >>>> ");
9464 if (!emstorage_notify_storage_event(NOTI_MAILBOX_RENAME_FAIL, account_id, input_mailbox_id, input_new_mailbox_name, 0))
9465 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event Failed [ NOTI_MAILBOX_RENAME_FAIL ] >>>> ");
9468 if (old_mailbox_data)
9469 emstorage_free_mailbox(&old_mailbox_data, 1, NULL);
9473 EM_DEBUG_FUNC_END("error [%d]", error);
9477 INTERNAL_FUNC int emstorage_get_new_attachment_no(int *attachment_no, int *err_code)
9479 EM_DEBUG_FUNC_BEGIN("attachment_no [%p], err_code[%p]", attachment_no, err_code);
9480 int rc, ret = false;
9481 int error = EMAIL_ERROR_NONE;
9482 char *sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
9485 if (!attachment_no) {
9486 EM_DEBUG_EXCEPTION("Invalid attachment");
9487 error = EMAIL_ERROR_INVALID_PARAM;
9491 *attachment_no = -1;
9493 sqlite3 *local_db_handle = emstorage_get_db_connection();
9496 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9497 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9498 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9500 if (NULL == result[1])
9503 rc = atoi(result[1])+1;
9505 sqlite3_free_table(result);
9507 *attachment_no = rc;
9508 EM_DEBUG_LOG("attachment_no [%d]", *attachment_no);
9514 if (err_code != NULL)
9517 EM_DEBUG_FUNC_END("ret [%d]", ret);
9521 INTERNAL_FUNC int emstorage_add_attachment(emstorage_attachment_tbl_t* attachment_tbl, int iscopy, int transaction, int *err_code)
9523 EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], iscopy[%d], transaction[%d], err_code[%p]", attachment_tbl, iscopy, transaction, err_code);
9527 int rc, ret = false;
9528 int error = EMAIL_ERROR_NONE;
9529 DB_STMT hStmt = NULL;
9530 char sql_query_string[QUERY_SIZE] = {0, };
9531 sqlite3 *local_db_handle = emstorage_get_db_connection();
9533 if (!attachment_tbl) {
9534 EM_DEBUG_EXCEPTION("attachment_tbl[%p], iscopy[%d]", attachment_tbl, iscopy);
9535 if (err_code != NULL)
9536 *err_code = EMAIL_ERROR_INVALID_PARAM;
9540 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9542 sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
9544 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9545 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9546 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9548 if (NULL==result[1]) rc = 1;
9549 else rc = atoi(result[1]) + 1;
9550 sqlite3_free_table(result);
9552 attachment_tbl->attachment_id = rc;
9554 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9555 "INSERT INTO mail_attachment_tbl VALUES "
9556 "( ?" /* attachment_id */
9557 ", ?" /* attachment_name */
9558 ", ?" /* attachment_path */
9559 ", ?" /* attachment_size */
9561 ", ?" /* account_id */
9562 ", ?" /* mailbox_id */
9563 ", ?" /* attachment_save_status */
9564 ", ?" /* attachment_drm_type */
9565 ", ?" /* attachment_drm_method */
9566 ", ?" /* attachment_inline_content_status */
9567 ", ?" /* attachment_mime_type */
9568 #ifdef __ATTACHMENT_OPTI__
9575 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
9576 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9577 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9579 _bind_stmt_field_data_int (hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_id);
9580 _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);
9581 _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);
9582 _bind_stmt_field_data_int (hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_size);
9583 _bind_stmt_field_data_int (hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mail_id);
9584 _bind_stmt_field_data_int (hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->account_id);
9585 _bind_stmt_field_data_int (hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mailbox_id);
9586 _bind_stmt_field_data_int (hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_save_status);
9587 _bind_stmt_field_data_int (hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_type);
9588 _bind_stmt_field_data_int (hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_method);
9589 _bind_stmt_field_data_int (hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_inline_content_status);
9590 _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);
9591 #ifdef __ATTACHMENT_OPTI__
9592 _bind_stmt_field_data_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->encoding);
9593 _bind_stmt_field_data_string(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->section, 0, ATTACHMENT_LEN_IN_MAIL_ATTACHMENT_TBL);
9597 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9598 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
9599 ("sqlite3_step fail:%d", rc));
9600 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9601 ("sqlite3_step fail:%d", rc));
9603 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9604 "UPDATE mail_tbl SET attachment_count = 1 WHERE mail_id = %d", attachment_tbl->mail_id);
9606 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9608 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
9609 ("sqlite3_exec fail:%d", rc));
9610 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9611 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9613 rc = sqlite3_changes(local_db_handle);
9615 EM_DEBUG_EXCEPTION(" no matched mail found...");
9616 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9623 if (hStmt != NULL) {
9624 rc = sqlite3_finalize(hStmt);
9625 if (rc != SQLITE_OK) {
9626 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
9627 error = EMAIL_ERROR_DB_FAILURE;
9631 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9634 if (err_code != NULL)
9637 EM_DEBUG_FUNC_END("ret [%d]", ret);
9641 INTERNAL_FUNC int emstorage_update_attachment(emstorage_attachment_tbl_t* attachment_tbl, int transaction, int *err_code)
9643 EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], transaction[%d], err_code[%p]", attachment_tbl, transaction, err_code);
9645 int rc, ret = false, field_idx = 0;
9646 int error = EMAIL_ERROR_NONE;
9647 DB_STMT hStmt = NULL;
9648 char sql_query_string[QUERY_SIZE] = {0, };
9650 if (!attachment_tbl) {
9651 EM_DEBUG_EXCEPTION(" attachment_tbl[%p] ", attachment_tbl);
9652 error = EMAIL_ERROR_INVALID_PARAM;
9656 sqlite3 *local_db_handle = emstorage_get_db_connection();
9658 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9660 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9661 "UPDATE mail_attachment_tbl SET "
9662 " attachment_name = ?"
9663 ", attachment_path = ?"
9664 ", attachment_size = ?"
9668 ", attachment_save_status = ?"
9669 ", attachment_drm_type = ?"
9670 ", attachment_drm_method = ?"
9671 ", attachment_inline_content_status = ? "
9672 ", attachment_mime_type = ? "
9673 " WHERE attachment_id = ?;");
9676 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
9678 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9679 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9681 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
9682 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
9683 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->attachment_size);
9684 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->mail_id);
9685 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->account_id);
9686 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->mailbox_id);
9687 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->attachment_save_status);
9688 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->attachment_drm_type);
9689 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->attachment_drm_method);
9690 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->attachment_inline_content_status);
9691 _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_mime_type, 0, ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL);
9692 _bind_stmt_field_data_int (hStmt, field_idx++ , attachment_tbl->attachment_id);
9695 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9696 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
9697 ("sqlite3_step fail:%d", rc));
9698 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9699 ("sqlite3_step fail:%d", rc));
9701 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9702 "UPDATE mail_tbl SET attachment_count = 1 WHERE mail_id = %d", attachment_tbl->mail_id);
9704 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9706 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
9707 ("sqlite3_exec fail:%d", rc));
9708 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9709 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9711 rc = sqlite3_changes(local_db_handle);
9713 EM_DEBUG_EXCEPTION(" no matched mail found...");
9714 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9721 if (hStmt != NULL) {
9722 rc = sqlite3_finalize(hStmt);
9723 if (rc != SQLITE_OK) {
9724 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
9725 error = EMAIL_ERROR_DB_FAILURE;
9729 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9732 if (err_code != NULL)
9735 EM_DEBUG_FUNC_END("ret [%d]", ret);
9739 INTERNAL_FUNC int emstorage_delete_attachment_on_db(int attachment_id, int transaction, int *err_code)
9741 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], transaction[%d], err_code[%p]", attachment_id, transaction, err_code);
9743 if (attachment_id < 0) {
9744 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
9745 if (err_code != NULL)
9746 *err_code = EMAIL_ERROR_INVALID_PARAM;
9750 int rc, ret = false;
9751 int error = EMAIL_ERROR_NONE;
9752 char sql_query_string[QUERY_SIZE] = {0, };
9754 sqlite3 *local_db_handle = emstorage_get_db_connection();
9756 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9758 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
9760 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9761 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9762 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9767 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9773 EM_DEBUG_FUNC_END("ret [%d]", ret);
9777 INTERNAL_FUNC int emstorage_delete_all_attachments_of_mail(int mail_id, int transaction, int *err_code)
9779 EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9782 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
9783 if (err_code != NULL)
9784 *err_code = EMAIL_ERROR_INVALID_PARAM;
9788 int rc, ret = false;
9789 int error = EMAIL_ERROR_NONE;
9790 char sql_query_string[QUERY_SIZE] = {0, };
9792 sqlite3 *local_db_handle = emstorage_get_db_connection();
9794 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9796 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
9798 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9799 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9800 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9805 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9811 EM_DEBUG_FUNC_END("ret [%d]", ret);
9815 INTERNAL_FUNC int emstorage_delete_attachment_all_on_db(int account_id, char *mailbox, int transaction, int *err_code)
9817 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox, transaction, err_code);
9819 int error = EMAIL_ERROR_NONE;
9820 int rc, ret = false;
9821 char sql_query_string[QUERY_SIZE] = {0, };
9823 sqlite3 *local_db_handle = emstorage_get_db_connection();
9825 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
9827 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl");
9829 if (account_id != ALL_ACCOUNT) /* '0' means all account */
9830 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " WHERE account_id = %d", account_id);
9832 if (mailbox) /* NULL means all mailbox_name */
9833 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " %s mailbox_name = '%s'", account_id != ALL_ACCOUNT ? "AND" : "WHERE", mailbox);
9835 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
9836 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9837 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9842 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
9845 if (err_code != NULL)
9848 EM_DEBUG_FUNC_END("ret [%d]", ret);
9852 INTERNAL_FUNC int emstorage_free_attachment(emstorage_attachment_tbl_t** attachment_tbl_list, int count, int *err_code)
9854 EM_DEBUG_FUNC_BEGIN("attachment_tbl_list[%p], count[%d], err_code[%p]", attachment_tbl_list, count, err_code);
9857 if ((attachment_tbl_list == NULL) || (*attachment_tbl_list == NULL)) {
9858 EM_DEBUG_EXCEPTION(" attachment_tbl_list[%p], count[%d]", attachment_tbl_list, count);
9859 if (err_code != NULL)
9860 *err_code = EMAIL_ERROR_INVALID_PARAM;
9864 emstorage_attachment_tbl_t* p = *attachment_tbl_list;
9867 for (i = 0; i < count; i++) {
9868 EM_SAFE_FREE(p[i].attachment_name);
9869 EM_SAFE_FREE(p[i].attachment_path);
9870 #ifdef __ATTACHMENT_OPTI__
9871 EM_SAFE_FREE(p[i].section);
9876 *attachment_tbl_list = NULL;
9879 if (err_code != NULL)
9880 *err_code = EMAIL_ERROR_NONE;
9881 EM_DEBUG_FUNC_END();
9887 INTERNAL_FUNC int emstorage_begin_transaction(void *d1, void *d2, int *err_code)
9889 EM_PROFILE_BEGIN(emStorageBeginTransaction);
9892 ENTER_CRITICAL_SECTION(_transactionBeginLock);
9894 /* wait for the trnasaction authority to be changed. */
9895 while (g_transaction) {
9896 EM_DEBUG_LOG(">>>>>>>> Wait for the transaction authority to be changed");
9900 /* take the transaction authority. */
9901 g_transaction = true;
9903 LEAVE_CRITICAL_SECTION(_transactionBeginLock);
9905 sqlite3 *local_db_handle = emstorage_get_db_connection();
9907 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN immediate;", NULL, NULL, NULL), rc);
9908 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
9909 ("SQL(BEGIN) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
9910 if (ret == false && err_code != NULL)
9911 *err_code = EMAIL_ERROR_DB_FAILURE;
9913 EM_PROFILE_END(emStorageBeginTransaction);
9914 EM_DEBUG_FUNC_END("ret [%d]", ret);
9918 INTERNAL_FUNC int emstorage_commit_transaction(void *d1, void *d2, int *err_code)
9920 EM_DEBUG_FUNC_BEGIN();
9922 sqlite3 *local_db_handle = emstorage_get_db_connection();
9924 ENTER_CRITICAL_SECTION(_transactionEndLock);
9927 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
9928 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; }, ("SQL(END) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
9929 /* release the transaction authority. */
9930 g_transaction = false;
9932 LEAVE_CRITICAL_SECTION(_transactionEndLock);
9933 if (ret == false && err_code != NULL)
9934 *err_code = EMAIL_ERROR_DB_FAILURE;
9936 EM_DEBUG_FUNC_END("ret [%d]", ret);
9940 INTERNAL_FUNC int emstorage_rollback_transaction(void *d1, void *d2, int *err_code)
9942 EM_DEBUG_FUNC_BEGIN();
9944 sqlite3 *local_db_handle = emstorage_get_db_connection();
9947 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "ROLLBACK;", NULL, NULL, NULL), rc);
9949 ENTER_CRITICAL_SECTION(_transactionEndLock);
9950 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
9951 ("SQL(ROLLBACK) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
9953 /* release the transaction authority. */
9954 g_transaction = false;
9956 LEAVE_CRITICAL_SECTION(_transactionEndLock);
9958 if (ret == false && err_code != NULL)
9959 *err_code = EMAIL_ERROR_DB_FAILURE;
9961 EM_DEBUG_FUNC_END("ret [%d]", ret);
9965 INTERNAL_FUNC int emstorage_is_mailbox_full(int account_id, email_mailbox_t *mailbox, int *result, int *err_code)
9967 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], result[%p], err_code[%p]", account_id, mailbox, result, err_code);
9969 if (account_id < FIRST_ACCOUNT_ID || !mailbox || !result) {
9971 EM_DEBUG_EXCEPTION("Invalid Parameter. accoun_id[%d], mailbox[%p]", account_id, mailbox);
9973 if (err_code != NULL)
9974 *err_code = EMAIL_ERROR_INVALID_PARAM;
9980 int error = EMAIL_ERROR_NONE;
9983 if (!emstorage_get_mail_count(account_id, mailbox->mailbox_name, &mail_count, NULL, true, &error)) {
9984 EM_DEBUG_EXCEPTION("emstorage_get_mail_count failed [%d]", error);
9989 EM_DEBUG_LOG("mail_count[%d] mail_slot_size[%d]", mail_count, mailbox->mail_slot_size);
9990 if (mail_count >= mailbox->mail_slot_size)
10001 if (err_code != NULL)
10004 EM_DEBUG_FUNC_END("ret [%d]", ret);
10008 INTERNAL_FUNC int emstorage_clear_mail_data(int transaction, int *err_code)
10010 EM_DEBUG_FUNC_BEGIN("transaction[%d], err_code[%p]", transaction, err_code);
10012 int rc, ret = false;
10013 int error = EMAIL_ERROR_NONE;
10014 char sql_query_string[QUERY_SIZE] = {0, };
10016 const email_db_object_t* tables = _g_db_tables;
10017 const email_db_object_t* indexes = _g_db_indexes;
10018 char data_path[256];
10020 sqlite3 *local_db_handle = emstorage_get_db_connection();
10021 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10023 if (!emstorage_delete_dir(MAILHOME, &error)) {
10024 EM_DEBUG_EXCEPTION(" emstorage_delete_dir failed - %d", error);
10029 SNPRINTF(data_path, sizeof(data_path), "%s/%s", MAILHOME, MAILTEMP);
10031 mkdir(MAILHOME, DIRECTORY_PERMISSION);
10032 mkdir(data_path, DIRECTORY_PERMISSION);
10034 /* first clear index. */
10035 while (indexes->object_name) {
10036 if (indexes->data_flag) {
10037 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP index %s", indexes->object_name);
10038 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10039 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10040 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10045 while (tables->object_name) {
10046 if (tables->data_flag) {
10047 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP table %s", tables->object_name);
10048 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10049 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10050 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10058 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10061 if (err_code != NULL)
10064 EM_DEBUG_FUNC_END("ret [%d]", ret);
10067 /*======================= DB File Utils =============================================*/
10068 #include <dirent.h>
10069 #include <sys/types.h>
10070 #define DIR_SEPERATOR "/"
10072 char *__em_create_dir_by_file_name(char *file_name)
10074 EM_DEBUG_FUNC_BEGIN("Filename [ %p ]", file_name);
10075 char delims[] = "/";
10076 char *result = NULL;
10078 result = strtok(file_name, delims);
10081 EM_DEBUG_LOG(">>>> Directory_name [ %s ]", result);
10083 EM_DEBUG_LOG(">>>> No Need to create Directory");
10088 INTERNAL_FUNC int emstorage_get_save_name(int account_id, int mail_id, int atch_id, char *fname, char *name_buf, int *err_code)
10090 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], fname[%s], name_buf[%p], err_code[%p]", account_id, mail_id, atch_id, fname, name_buf, err_code);
10091 EM_PROFILE_BEGIN(profile_emstorage_get_save_name);
10094 int error = EMAIL_ERROR_NONE;
10095 char *dir_name = NULL;
10096 char create_dir[1024]={0};
10097 char *temp_file = NULL;
10099 if (!name_buf || account_id < FIRST_ACCOUNT_ID || mail_id < 0 || atch_id < 0) {
10100 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);
10101 error = EMAIL_ERROR_INVALID_PARAM;
10105 sprintf(name_buf, "%s", MAILHOME);
10106 sprintf(name_buf+strlen(name_buf), "%s%d", DIR_SEPERATOR, account_id);
10109 sprintf(name_buf+strlen(name_buf), "%s%d", DIR_SEPERATOR, mail_id);
10112 sprintf(name_buf+strlen(name_buf), "%s%d", DIR_SEPERATOR, atch_id);
10115 temp_file = EM_SAFE_STRDUP(fname);
10116 if (strstr(temp_file, "/")) {
10117 dir_name = __em_create_dir_by_file_name(temp_file);
10122 sprintf(create_dir, "%s%s%s", name_buf, DIR_SEPERATOR, dir_name);
10123 EM_DEBUG_LOG(">>>>> DIR PATH [ %s ]", create_dir);
10124 mkdir(create_dir, DIRECTORY_PERMISSION);
10125 EM_SAFE_FREE(temp_file);
10130 EM_DEBUG_LOG(">>>>> fname [ %s ]", fname);
10131 sprintf(name_buf+strlen(name_buf), "%s%s", DIR_SEPERATOR, fname);
10135 EM_DEBUG_LOG(">>>>> name_buf [ %s ]", name_buf);
10140 EM_SAFE_FREE(temp_file);
10142 if (err_code != NULL)
10145 EM_PROFILE_END(profile_emstorage_get_save_name);
10146 EM_DEBUG_FUNC_END("ret [%d]", ret);
10150 INTERNAL_FUNC int emstorage_get_dele_name(int account_id, int mail_id, int atch_id, char *fname, char *name_buf, int *err_code)
10152 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);
10154 if (!name_buf || account_id < FIRST_ACCOUNT_ID) {
10155 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);
10156 if (err_code != NULL)
10157 *err_code = EMAIL_ERROR_INVALID_PARAM;
10161 sprintf(name_buf+strlen(name_buf), "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
10164 sprintf(name_buf+strlen(name_buf), "%s%d", DIR_SEPERATOR, mail_id);
10169 sprintf(name_buf+strlen(name_buf), "%s%d", DIR_SEPERATOR, atch_id);
10174 sprintf(name_buf+strlen(name_buf), ".DELE");
10176 EM_DEBUG_FUNC_END();
10180 INTERNAL_FUNC int emstorage_create_dir(int account_id, int mail_id, int atch_id, int *err_code)
10182 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], err_code[%p]", account_id, mail_id, atch_id, err_code);
10183 EM_PROFILE_BEGIN(profile_emcore_save_create_dir);
10185 int error = EMAIL_ERROR_NONE;
10189 if (account_id >= FIRST_ACCOUNT_ID) {
10190 SNPRINTF(buf, sizeof(buf), "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
10192 if (stat(buf, &sbuf) == 0) {
10193 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
10194 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
10195 error = EMAIL_ERROR_SYSTEM_FAILURE;
10200 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
10201 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
10202 EM_DEBUG_EXCEPTION("mkdir failed l(Errno=%d)][ErrStr=%s]", errno, strerror(errno));
10203 error = EMAIL_ERROR_SYSTEM_FAILURE;
10210 if (account_id < FIRST_ACCOUNT_ID) {
10211 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
10212 error = EMAIL_ERROR_INVALID_PARAM;
10216 SNPRINTF(buf+strlen(buf), sizeof(buf), "%s%d", DIR_SEPERATOR, mail_id);
10218 if (stat(buf, &sbuf) == 0) {
10219 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
10220 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
10222 error = EMAIL_ERROR_SYSTEM_FAILURE;
10227 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
10228 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
10229 EM_DEBUG_EXCEPTION("mkdir failed l (Errno=%d)][ErrStr=%s]", errno, strerror(errno));
10230 error = EMAIL_ERROR_SYSTEM_FAILURE;
10237 if (account_id < FIRST_ACCOUNT_ID || mail_id <= 0) {
10238 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
10240 error = EMAIL_ERROR_INVALID_PARAM;
10244 SNPRINTF(buf+strlen(buf), sizeof(buf)-(strlen(buf)+1), "%s%d", DIR_SEPERATOR, atch_id);
10246 if (stat(buf, &sbuf) == 0) {
10247 if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
10248 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
10250 error = EMAIL_ERROR_SYSTEM_FAILURE;
10255 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
10256 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
10257 error = EMAIL_ERROR_SYSTEM_FAILURE;
10266 if (err_code != NULL)
10269 EM_PROFILE_END(profile_emcore_save_create_dir);
10270 EM_DEBUG_FUNC_END("ret [%d]", ret);
10274 INTERNAL_FUNC int emstorage_copy_file(char *src_file, char *dst_file, int sync_status, int *err_code)
10276 EM_DEBUG_FUNC_BEGIN("src_file[%s], dst_file[%s], err_code[%p]", src_file, dst_file, err_code);
10277 EM_DEBUG_LOG("Using the fsync function");
10279 int error = EMAIL_ERROR_NONE;
10280 struct stat st_buf;
10289 if (!src_file || !dst_file) {
10290 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
10292 error = EMAIL_ERROR_INVALID_PARAM;
10296 if (stat(src_file, &st_buf) < 0) {
10297 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", src_file);
10299 error = EMAIL_ERROR_SYSTEM_FAILURE; /* EMAIL_ERROR_INVALID_PATH; */
10303 buf_size = st_buf.st_size;
10304 EM_DEBUG_LOG(">>>> File Size [ %d ]", buf_size);
10305 buf = (char *)calloc(1, buf_size+1);
10308 EM_DEBUG_EXCEPTION(">>> Memory cannot be allocated");
10309 error = EMAIL_ERROR_OUT_OF_MEMORY;
10314 if (!(fp_src = open(src_file, O_RDONLY))) {
10315 EM_DEBUG_EXCEPTION(">>>> Source Fail open %s Failed [ %d ] - Error [ %s ]", src_file, errno, strerror(errno));
10316 error = EMAIL_ERROR_SYSTEM_FAILURE;
10320 if (!(fp_dst = open(dst_file, O_CREAT | O_WRONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH))) {
10321 EM_DEBUG_EXCEPTION(">>>> Destination Fail open %s Failed [ %d ] - Error [ %s ]", dst_file, errno, strerror(errno));
10322 error = EMAIL_ERROR_SYSTEM_FAILURE;
10326 while ((nread = read(fp_src, buf, buf_size)) > 0) {
10327 if (nread > 0 && nread <= buf_size) {
10328 EM_DEBUG_LOG("Nread Value [%d]", nread);
10329 if ((nwritten = write(fp_dst, buf, nread)) != nread) {
10330 EM_DEBUG_EXCEPTION("fwrite failed...");
10331 error = EMAIL_ERROR_UNKNOWN;
10334 EM_DEBUG_LOG("NWRITTEN [%d]", nwritten);
10347 EM_DEBUG_LOG("Before fsync");
10353 if (nread < 0 || error == EMAIL_ERROR_UNKNOWN)
10356 if (err_code != NULL)
10358 EM_DEBUG_FUNC_END("ret [%d]", ret);
10361 /* create Directory if user has deleted [deepam.p@samsung.com] */
10362 INTERNAL_FUNC void emstorage_create_dir_if_delete()
10364 EM_DEBUG_FUNC_BEGIN();
10366 mkdir(EMAILPATH, DIRECTORY_PERMISSION);
10367 mkdir(DATA_PATH, DIRECTORY_PERMISSION);
10369 mkdir(MAILHOME, DIRECTORY_PERMISSION);
10371 SNPRINTF(g_db_path, sizeof(g_db_path), "%s/%s", MAILHOME, MAILTEMP);
10372 mkdir(g_db_path, DIRECTORY_PERMISSION);
10374 /* _delete_temp_file(g_db_path); */
10375 EM_DEBUG_FUNC_END();
10377 static int _get_temp_file_name(char **filename, int *err_code)
10379 EM_DEBUG_FUNC_BEGIN("filename[%p], err_code[%p]", filename, err_code);
10382 int error = EMAIL_ERROR_NONE;
10384 if (filename == NULL) {
10385 EM_DEBUG_EXCEPTION(" filename[%p]", filename);
10386 error = EMAIL_ERROR_INVALID_PARAM;
10390 char tempname[512] = {0x00, };
10393 gettimeofday(&tv, NULL);
10396 SNPRINTF(tempname, sizeof(tempname), "%s%c%s%c%d", MAILHOME, '/', MAILTEMP, '/', rand());
10398 char *p = EM_SAFE_STRDUP(tempname);
10400 EM_DEBUG_EXCEPTION(" strdup failed...");
10401 error = EMAIL_ERROR_OUT_OF_MEMORY;
10410 if (err_code != NULL)
10413 EM_DEBUG_FUNC_END("ret [%d]", ret);
10417 INTERNAL_FUNC int emstorage_add_content_type(char *file_path, char *char_set, int *err_code)
10419 EM_DEBUG_FUNC_BEGIN("File path [ %p ] Character Set [ %p ] err_code [ %p]", file_path, char_set, err_code);
10421 EM_IF_NULL_RETURN_VALUE(file_path, false);
10422 EM_IF_NULL_RETURN_VALUE(char_set, false);
10423 EM_IF_NULL_RETURN_VALUE(err_code, false);
10427 struct stat st_buf;
10429 char *low_char_set = NULL;
10430 char *match_str = NULL;
10433 int error = EMAIL_ERROR_NONE;
10434 int data_count_to_written = 0;
10435 char *temp_file_name = NULL;
10438 FILE* fp_src = NULL;
10439 FILE* fp_dest = NULL;
10443 if (stat(file_path, &st_buf) < 0) {
10444 EM_DEBUG_EXCEPTION(" stat(\"%s\") failed...", file_path);
10446 error = EMAIL_ERROR_SYSTEM_FAILURE; /* EMAIL_ERROR_INVALID_PATH; */
10450 buf_size = st_buf.st_size;
10452 EM_DEBUG_LOG(">>>> File Size [ %d ] ", buf_size);
10454 buf = (char *)calloc(1, buf_size+1);
10457 EM_DEBUG_LOG(">>> Memory cannot be allocated ");
10461 if (!(fp_src = fopen(file_path, "rb"))) {
10462 EM_DEBUG_EXCEPTION(" file_path fopen failed - %s", file_path);
10464 error = EMAIL_ERROR_SYSTEM_FAILURE;
10468 if ((nread = fread(buf, 1, buf_size, fp_src)) > 0) {
10469 if (nread > 0 && nread <= buf_size) {
10470 EM_DEBUG_LOG(">>>> Nread Value [ %d ] ", nread);
10473 * 1.Add check for whether content type is there.
10474 * 2. If not based on the character set, Append it in File
10477 low_char_set = calloc(1, strlen(char_set) + strlen(" \" /></head>") +1);
10479 strncat(low_char_set, char_set, strlen(char_set));
10481 EM_DEBUG_LOG(">>>> CHAR SET [ %s ] ", low_char_set);
10483 strncat(low_char_set, " \" /></head>", strlen(" \" /></head>"));
10485 EM_DEBUG_LOG(">>> CHARSET [ %s ] ", low_char_set);
10487 EM_DEBUG_LOG(">>>>emstorage_add_content_type 1 ");
10489 match_str = strstr(buf, CONTENT_TYPE_DATA);
10490 EM_DEBUG_LOG(">>>>emstorage_add_content_type 2 ");
10492 if (match_str == NULL) {
10493 EM_DEBUG_LOG(">>>>emstorage_add_content_type 3 ");
10494 if (fp_src !=NULL) {
10495 fclose(fp_src);fp_src = NULL;
10497 data_count_to_written = strlen(low_char_set)+strlen(CONTENT_DATA)+1;
10498 EM_DEBUG_LOG(">>>>emstorage_add_content_type 4 ");
10499 buf1 = (char *)calloc(1, data_count_to_written);
10500 EM_DEBUG_LOG(">>>>emstorage_add_content_type 5 ");
10503 EM_DEBUG_LOG(">>>>emstorage_add_content_type 6 ");
10504 strncat(buf1, CONTENT_DATA, strlen(CONTENT_DATA));
10506 EM_DEBUG_LOG(">>>>> BUF 1 [ %s ] ", buf1);
10508 strncat(buf1, low_char_set, strlen(low_char_set));
10510 EM_DEBUG_LOG(">>>> HTML TAG DATA [ %s ] ", buf1);
10513 /* 1. Create a temporary file name */
10514 if (!_get_temp_file_name(&temp_file_name, &err)) {
10515 EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed - %d", err);
10516 if (err_code != NULL) *err_code = err;
10517 EM_SAFE_FREE(temp_file_name);
10520 EM_DEBUG_LOG(">>>>>>> TEMP APPEND FILE PATH [ %s ] ", temp_file_name);
10522 /* Open the Temp file in Append mode */
10523 if (!(fp_dest = fopen(temp_file_name, "ab"))) {
10524 EM_DEBUG_EXCEPTION(" fopen failed - %s", temp_file_name);
10525 error = EMAIL_ERROR_SYSTEM_FAILURE;
10529 /* 2. write the Latest data */
10530 nwritten = fwrite(buf1, data_count_to_written-1, 1, fp_dest);
10532 if (nwritten > 0) {
10533 EM_DEBUG_LOG(" Latest Data : [%d ] bytes written ", nwritten);
10535 /* 3. Append old data */
10536 nwritten = fwrite(buf, nread-1, 1, fp_dest);
10538 if (nwritten <= 0) {
10539 EM_DEBUG_EXCEPTION(" Error Occured while writing Old data : [%d ] bytes written ", nwritten);
10540 error = EMAIL_ERROR_SYSTEM_FAILURE;
10544 EM_DEBUG_LOG(">>>> OLD data appended [ %d ] ", nwritten);
10546 if (!emstorage_move_file(temp_file_name, file_path, false, &err)) {
10547 EM_DEBUG_EXCEPTION(" emstorage_move_file failed - %d", err);
10554 EM_DEBUG_EXCEPTION(" Error Occured while writing New data : [%d ] bytes written ", nwritten);
10555 error = EMAIL_ERROR_SYSTEM_FAILURE;
10562 EM_DEBUG_LOG(">>>>emstorage_add_content_type 15 ");
10572 EM_SAFE_FREE(buf1);
10573 EM_SAFE_FREE(low_char_set);
10575 if (fp_src != NULL) {
10580 if (fp_dest != NULL) {
10584 EM_DEBUG_FUNC_END("ret [%d]", ret);
10589 INTERNAL_FUNC int emstorage_move_file(char *src_file, char *dst_file, int sync_status, int *err_code)
10591 EM_DEBUG_FUNC_BEGIN("src_file[%p], dst_file[%p], err_code[%p]", src_file, dst_file, err_code);
10594 int error = EMAIL_ERROR_NONE;
10596 if (src_file == NULL || dst_file == NULL) {
10597 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
10598 error = EMAIL_ERROR_INVALID_PARAM;
10602 EM_DEBUG_LOG("src_file[%s], dst_file[%s]", src_file, dst_file);
10604 if (strcmp(src_file, dst_file) != 0) {
10605 if (rename(src_file, dst_file) != 0) {
10606 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() */
10607 /* does not work across different mount points, even if the same file system is mounted on both.) */
10608 EM_DEBUG_LOG("oldpath and newpath are not on the same mounted file system.");
10609 if (!emstorage_copy_file(src_file, dst_file, sync_status, &error)) {
10610 EM_DEBUG_EXCEPTION("emstorage_copy_file failed - %d", error);
10614 EM_DEBUG_LOG("src[%s] removed", src_file);
10618 if (errno == ENOENT) {
10619 struct stat temp_file_stat;
10620 if (stat(src_file, &temp_file_stat) < 0)
10621 EM_DEBUG_EXCEPTION("no src file found [%s]", src_file);
10622 if (stat(dst_file, &temp_file_stat) < 0)
10623 EM_DEBUG_EXCEPTION("no dst file found [%s]", src_file);
10625 EM_DEBUG_EXCEPTION("no file found [%d]", errno);
10626 error = EMAIL_ERROR_FILE_NOT_FOUND;
10631 EM_DEBUG_EXCEPTION("rename failed [%d]", errno);
10632 error = EMAIL_ERROR_SYSTEM_FAILURE;
10639 EM_DEBUG_LOG("src[%s] = dst[%d]", src_file, dst_file);
10645 if (err_code != NULL)
10648 EM_DEBUG_FUNC_END("ret [%d]", ret);
10652 INTERNAL_FUNC int emstorage_delete_file(char *src_file, int *err_code)
10654 EM_DEBUG_FUNC_BEGIN("src_file[%p], err_code[%p]", src_file, err_code);
10657 int error = EMAIL_ERROR_NONE;
10659 if (src_file == NULL) {
10660 EM_DEBUG_EXCEPTION(" src_file[%p]", src_file);
10662 error = EMAIL_ERROR_INVALID_PARAM;
10666 if (remove(src_file) != 0) {
10667 if (errno != ENOENT) {
10668 EM_DEBUG_EXCEPTION(" remove failed - %d", errno);
10670 error = EMAIL_ERROR_SYSTEM_FAILURE;
10674 EM_DEBUG_EXCEPTION(" no file found...");
10676 error = EMAIL_ERROR_FILE_NOT_FOUND;
10683 if (err_code != NULL)
10686 EM_DEBUG_FUNC_END("ret [%d]", ret);
10691 INTERNAL_FUNC int emstorage_delete_dir(char *src_dir, int *err_code)
10693 EM_DEBUG_FUNC_BEGIN("src_dir[%p], err_code[%p]", src_dir, err_code);
10695 if (src_dir == NULL) {
10696 EM_DEBUG_EXCEPTION(" src_dir[%p]", src_dir);
10698 if (err_code != NULL)
10699 *err_code = EMAIL_ERROR_INVALID_PARAM;
10703 int error = EMAIL_ERROR_NONE;
10710 dirp = opendir(src_dir);
10712 if (dirp == NULL) {
10713 if (errno == ENOENT) {
10714 EM_DEBUG_EXCEPTION("directory[%s] does not exist...", src_dir);
10715 if (err_code != NULL)
10716 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
10720 EM_DEBUG_EXCEPTION("opendir failed - %d", errno);
10721 if (err_code != NULL)
10722 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
10727 while ((dp=readdir(dirp))) {
10728 if (strncmp(dp->d_name, ".", strlen(".")) == 0 || strncmp(dp->d_name, "..", strlen("..")) == 0)
10731 SNPRINTF(buf, sizeof(buf), "%s/%s", src_dir, dp->d_name);
10733 if (lstat(buf, &sbuf) == 0 || stat(buf, &sbuf) == 0) {
10734 /* check directory */
10735 if ((sbuf.st_mode & S_IFMT) == S_IFDIR) { /* directory */
10736 /* recursive call */
10737 if (!emstorage_delete_dir(buf, &error)) {
10739 if (err_code != NULL)
10745 if (remove(buf) < 0) {
10746 EM_DEBUG_EXCEPTION("remove failed - %s", buf);
10748 if (err_code != NULL)
10749 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
10755 EM_DEBUG_EXCEPTION("content does not exist...");
10760 EM_DEBUG_LOG("remove direcotory [%s]", src_dir);
10762 /* EM_DEBUG_FUNC_BEGIN(); */
10764 if (remove(src_dir) < 0) {
10765 EM_DEBUG_EXCEPTION("remove failed [%s]", src_dir);
10766 if (err_code != NULL)
10767 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
10771 if (err_code != NULL)
10777 /* faizan.h@samsung.com */
10778 INTERNAL_FUNC int emstorage_update_server_uid(char *old_server_uid, char *new_server_uid, int *err_code)
10780 EM_DEBUG_FUNC_BEGIN("new_server_uid[%s], old_server_uid[%s]", new_server_uid, old_server_uid);
10781 int rc, ret = false;
10782 int error = EMAIL_ERROR_NONE;
10783 char sql_query_string[QUERY_SIZE] = {0, };
10785 int transaction = true;
10787 if (!old_server_uid || !new_server_uid) {
10788 EM_DEBUG_EXCEPTION("Invalid parameters");
10789 error = EMAIL_ERROR_INVALID_PARAM;
10792 sqlite3 *local_db_handle = emstorage_get_db_connection();
10794 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10796 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10797 "UPDATE mail_tbl SET server_mail_id=\'%s\' WHERE server_mail_id=%s ", new_server_uid, old_server_uid);
10798 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10800 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
10801 ("sqlite3_exec fail:%d", rc));
10802 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10803 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10808 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10811 if (err_code != NULL)
10814 EM_DEBUG_FUNC_END("ret [%d]", ret);
10819 INTERNAL_FUNC int emstorage_update_read_mail_uid(int mail_id, char *new_server_uid, char *mbox_name, int *err_code)
10821 EM_DEBUG_FUNC_BEGIN("mail_id[%d], new_server_uid[%s], mbox_name[%s]", mail_id, new_server_uid, mbox_name);
10823 int rc, ret = false;
10824 int error = EMAIL_ERROR_NONE;
10825 char sql_query_string[QUERY_SIZE] = {0, };
10827 int transaction = true;
10829 if (!mail_id || !new_server_uid || !mbox_name) {
10830 EM_DEBUG_EXCEPTION("Invalid parameters");
10831 error = EMAIL_ERROR_INVALID_PARAM;
10834 sqlite3 *local_db_handle = emstorage_get_db_connection();
10835 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
10838 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10839 "UPDATE mail_read_mail_uid_tbl SET s_uid=\'%s\', mailbox_id=\'%s\', mailbox_name=\'%s\' WHERE local_uid=%d ", new_server_uid, mbox_name, mbox_name, mail_id);
10840 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
10841 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10842 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10847 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
10850 if (err_code != NULL)
10853 EM_DEBUG_FUNC_END("ret [%d]", ret);
10859 int emstorage_get_latest_unread_mailid(int account_id, int *mail_id, int *err_code)
10861 EM_DEBUG_FUNC_BEGIN();
10863 if ((!mail_id) ||(account_id <= 0 && account_id != -1)) {
10864 EM_DEBUG_EXCEPTION(" mail_id[%p], account_id[%d] ", mail_id, account_id);
10865 if (err_code != NULL)
10866 *err_code = EMAIL_ERROR_INVALID_PARAM;
10872 int error = EMAIL_ERROR_NONE;
10873 DB_STMT hStmt = NULL;
10876 int transaction = false;
10877 char sql_query_string[QUERY_SIZE] = {0, };
10879 sqlite3 *local_db_handle = emstorage_get_db_connection();
10880 EMSTORAGE_START_READ_TRANSACTION(transaction);
10882 memset(sql_query_string, 0x00, sizeof(sql_query_string));
10884 if (account_id == -1)
10885 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl WHERE flags_seen_field = 0 ORDER BY mail_id DESC");
10887 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl WHERE account_id = %d AND flags_seen_field = 0 ORDER BY mail_id DESC", account_id);
10889 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
10890 EM_DEBUG_LOG(" sqlite3_prepare hStmt = %p", hStmt);
10891 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10892 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10894 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10895 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10896 ("sqlite3_step fail:%d", rc));
10900 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
10901 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
10902 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10904 sqlite3_free_table(result);
10906 EM_DEBUG_EXCEPTION("no Mails found...");
10908 error= EMAIL_ERROR_MAIL_NOT_FOUND;
10912 _get_stmt_field_data_int(hStmt, &mailid, 0);
10917 if (hStmt != NULL) {
10918 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
10920 rc = sqlite3_finalize(hStmt);
10921 if (rc != SQLITE_OK) {
10922 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
10924 error = EMAIL_ERROR_DB_FAILURE;
10927 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10930 if (err_code != NULL)
10933 EM_DEBUG_FUNC_END("ret [%d]", ret);
10937 int setting_system_command(const char *command)
10940 int pid = 0, status = 0;
10941 char *const environ[] = { NULL };
10956 argv[2] = (char *)command;
10959 execve("/bin/sh", argv, environ);
10963 if (waitpid(pid, &status, 0) == -1) {
10964 if (errno != EINTR)
10976 INTERNAL_FUNC int emstorage_mail_get_total_diskspace_usage(unsigned long *total_usage, int transaction, int *err_code)
10978 EM_DEBUG_FUNC_BEGIN("total_usage[%p], transaction[%d], err_code[%p]", total_usage, transaction, err_code);
10980 if (!total_usage) {
10981 EM_DEBUG_EXCEPTION("total_usage[%p]", total_usage);
10983 if (err_code != NULL)
10984 *err_code = EMAIL_ERROR_INVALID_PARAM;
10989 int error = EMAIL_ERROR_NONE;
10990 char syscmd[256] = {0, };
10991 char line[256] = {0, };
10992 char *line_from_file = NULL;
10994 unsigned long total_diskusage = 0;
10996 SNPRINTF(syscmd, sizeof(syscmd), "touch %s", SETTING_MEMORY_TEMP_FILE_PATH);
10997 if (setting_system_command(syscmd) == -1) {
10998 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
11000 error = EMAIL_ERROR_SYSTEM_FAILURE;
11004 SNPRINTF(syscmd, sizeof(syscmd), "du -hsk %s > %s", EMAILPATH, SETTING_MEMORY_TEMP_FILE_PATH);
11005 EM_DEBUG_LOG(" cmd : %s", syscmd);
11006 if (setting_system_command(syscmd) == -1) {
11007 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : Setting > Memory] System Command [%s] is failed", syscmd);
11009 error = EMAIL_ERROR_SYSTEM_FAILURE;
11013 fp = fopen(SETTING_MEMORY_TEMP_FILE_PATH, "r");
11015 perror(SETTING_MEMORY_TEMP_FILE_PATH);
11017 error = EMAIL_ERROR_SYSTEM_FAILURE;
11021 line_from_file = fgets(line, sizeof(line), fp);
11023 if(line_from_file == NULL) {
11024 EM_DEBUG_EXCEPTION("fgets failed");
11025 error = EMAIL_ERROR_SYSTEM_FAILURE;
11028 total_diskusage = strtoul(line, NULL, 10);
11032 memset(syscmd, 0, sizeof(syscmd));
11033 SNPRINTF(syscmd, sizeof(syscmd), "rm -f %s", SETTING_MEMORY_TEMP_FILE_PATH);
11034 if (setting_system_command(syscmd) == -1) {
11035 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
11036 error = EMAIL_ERROR_SYSTEM_FAILURE;
11040 EM_DEBUG_LOG("[Setting > Memory] @@@@@ Size of Directory [%s] is %ld KB", EMAILPATH, total_diskusage);
11045 if (err_code != NULL)
11049 *total_usage = total_diskusage;
11053 EM_DEBUG_FUNC_END("ret [%d]", ret);
11058 INTERNAL_FUNC int emstorage_test(int mail_id, int account_id, char *full_address_to, char *full_address_cc, char *full_address_bcc, int *err_code)
11060 DB_STMT hStmt = NULL;
11062 int error = EMAIL_ERROR_NONE;
11064 char sql_query_string[QUERY_SIZE] = {0, };
11066 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11067 "INSERT INTO mail_tbl VALUES "
11068 "( ?" /* mail_id */
11069 ", ?" /* account_id */
11070 ", ?" /* mailbox_name */
11071 ", ?" /* mail_size */
11072 ", ?" /* server_mail_status */
11073 ", ?" /* server_mailbox_name */
11074 ", ?" /* server_mail_id */
11075 ", ?" /* full_address_from */
11076 ", ?" /* full_address_reply */
11077 ", ?" /* full_address_to */
11078 ", ?" /* full_address_cc */
11079 ", ?" /* full_address_bcc */
11080 ", ?" /* full_address_return */
11081 ", ?" /* subject */
11082 ", ?" /* body_download_status */
11083 ", ?" /* file_path_plain */
11084 ", ?" /* file_path_html */
11085 ", ?" /* date_time */
11086 ", ?" /* flags_seen_field */
11087 ", ?" /* flags_deleted_field */
11088 ", ?" /* flags_flagged_field */
11089 ", ?" /* flags_answered_field */
11090 ", ?" /* flags_recent_field */
11091 ", ?" /* flags_draft_field */
11092 ", ?" /* flags_forwarded_field */
11093 ", ?" /* DRM_status */
11094 ", ?" /* priority */
11095 ", ?" /* save_status */
11096 ", ?" /* lock_status */
11097 ", ?" /* message_id */
11098 ", ?" /* report_status */
11099 ", ?" /* email_address_sender */
11100 ", ?" /* email_address_recipient */
11101 ", ?" /* attachment_count */
11102 ", ?" /* inline_content_count */
11103 ", ?" /* preview_text */
11104 ", ?" /* thread_id */
11105 ", ?" /* mailbox_type */
11106 ", ?" /* alias_sender */
11107 ", ?" /* alias_recipient */
11108 ", ?" /* thread_item_count */
11109 ", ?" /* meeting_request_status */
11110 ", ?" /* message_class */
11111 ", ?" /* digest_type */
11112 ", ?" /* smime_type */
11115 int transaction = true;
11116 sqlite3 *local_db_handle = emstorage_get_db_connection();
11118 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
11121 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
11122 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11123 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11125 _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_id);
11126 _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, account_id);
11127 _bind_stmt_field_data_string(hStmt, MAILBOX_NAME_IDX_IN_MAIL_TBL, "OUTBOX", 0, MAILBOX_LEN_IN_MAIL_TBL);
11128 _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, EMAIL_MAILBOX_TYPE_OUTBOX);
11129 _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, "save test - long", 1, SUBJECT_LEN_IN_MAIL_TBL);
11130 _bind_stmt_field_data_string(hStmt, DATETIME_IDX_IN_MAIL_TBL, "20100316052908", 0, DATETIME_LEN_IN_MAIL_TBL);
11131 _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, 0);
11132 _bind_stmt_field_data_string(hStmt, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL, "", 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
11133 _bind_stmt_field_data_string(hStmt, SERVER_MAIL_ID_IDX_IN_MAIL_TBL, "", 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
11134 _bind_stmt_field_data_string(hStmt, MESSAGE_ID_IDX_IN_MAIL_TBL, "", 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
11135 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL, "<test08@streaming.s3glab.net>", 1, FROM_LEN_IN_MAIL_TBL);
11136 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL, "", 1, REPLY_TO_LEN_IN_MAIL_TBL);
11137 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL, full_address_to, 1, TO_LEN_IN_MAIL_TBL);
11138 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL, full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
11139 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL, full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
11140 _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL, "", 1, RETURN_PATH_LEN_IN_MAIL_TBL);
11141 _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);
11142 _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);
11143 _bind_stmt_field_data_string(hStmt, ALIAS_SENDER_IDX_IN_MAIL_TBL, "send_alias", 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
11144 _bind_stmt_field_data_string(hStmt, ALIAS_RECIPIENT_IDX_IN_MAIL_TBL, "recipient_alias", 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
11145 _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, 1);
11146 _bind_stmt_field_data_string(hStmt, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL, "/opt/system/rsr/email/.emfdata/7/348/UTF-8", 0, TEXT_1_LEN_IN_MAIL_TBL);
11147 _bind_stmt_field_data_string(hStmt, FILE_PATH_HTML_IDX_IN_MAIL_TBL, "", 0, TEXT_2_LEN_IN_MAIL_TBL);
11148 _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, 4);
11149 _bind_stmt_field_data_char(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, 0);
11150 _bind_stmt_field_data_char(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, 0);
11151 _bind_stmt_field_data_char(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, 0);
11152 _bind_stmt_field_data_char(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, 0);
11153 _bind_stmt_field_data_char(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, 0);
11154 _bind_stmt_field_data_char(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, 0);
11155 _bind_stmt_field_data_char(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, 0);
11156 _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, 0);
11157 _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, 0);
11158 _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, 0);
11159 _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, 0);
11160 _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, 0);
11161 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
11162 _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, 0);
11163 _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
11164 _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, 0);
11165 _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, 0);
11166 _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, "preview body", 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
11167 _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, 0);
11168 _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, 0);
11169 _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, 0);
11170 _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, 0);
11172 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11173 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
11174 ("sqlite3_step fail:%d", rc));
11175 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11176 ("sqlite3_step fail:%d", rc));
11180 if (hStmt != NULL) {
11181 rc = sqlite3_finalize(hStmt);
11182 if (rc != SQLITE_OK) {
11183 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
11184 error = EMAIL_ERROR_DB_FAILURE;
11188 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
11191 if (err_code != NULL)
11194 EM_DEBUG_FUNC_END("ret [%d]", ret);
11199 INTERNAL_FUNC int emstorage_get_max_mail_count()
11201 return EMAIL_MAIL_MAX_COUNT;
11204 INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(emstorage_mail_tbl_t *mail_tbl, int *thread_id, int *result_latest_mail_id_in_thread, int *thread_item_count)
11206 EM_DEBUG_FUNC_BEGIN("mail_tbl [%p], thread_id [%p], result_latest_mail_id_in_thread [%p], thread_item_count [%p]", mail_tbl, thread_id, result_latest_mail_id_in_thread, thread_item_count);
11207 EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
11208 int rc = 0, query_size = 0, query_size_account = 0;
11209 int account_id = 0;
11210 int err_code = EMAIL_ERROR_NONE;
11211 int count = 0, result_thread_id = -1, latest_mail_id_in_thread = -1;
11212 time_t latest_date_time = 0;
11213 time_t date_time = 0;
11214 char *mailbox_name = NULL, *subject = NULL;
11215 char *sql_query_string = NULL, *sql_account = NULL;
11216 char *sql_format = "SELECT thread_id, date_time, mail_id FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_type NOT IN (3, 5, 7, 8)";
11217 char *sql_format_account = " AND account_id = %d ";
11218 char *sql_format_order_by = " ORDER BY date_time DESC ";
11219 char **result = NULL;
11220 char stripped_subject[1025];
11221 sqlite3 *local_db_handle = emstorage_get_db_connection();
11223 EM_IF_NULL_RETURN_VALUE(mail_tbl, EMAIL_ERROR_INVALID_PARAM);
11224 EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
11225 EM_IF_NULL_RETURN_VALUE(result_latest_mail_id_in_thread, EMAIL_ERROR_INVALID_PARAM);
11226 EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
11228 if (mail_tbl->mailbox_type == EMAIL_MAILBOX_TYPE_TRASH ||
11229 mail_tbl->mailbox_type == EMAIL_MAILBOX_TYPE_SPAMBOX ||
11230 mail_tbl->mailbox_type == EMAIL_MAILBOX_TYPE_ALL_EMAILS) {
11231 EM_DEBUG_LOG("the mail in trash, spambox, all email could not be thread mail.");
11235 account_id = mail_tbl->account_id;
11236 mailbox_name = mail_tbl->mailbox_name;
11237 subject = mail_tbl->subject;
11238 date_time = mail_tbl->date_time;
11240 EM_DEBUG_LOG("subject : %s", subject);
11242 if (em_find_pos_stripped_subject_for_thread_view(subject, stripped_subject) != EMAIL_ERROR_NONE) {
11243 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
11244 err_code = EMAIL_ERROR_UNKNOWN;
11245 result_thread_id = -1;
11249 if (strlen(stripped_subject) < 2) {
11250 result_thread_id = -1;
11254 EM_DEBUG_LOG("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s", strlen(stripped_subject), stripped_subject);
11256 if (account_id > 0) {
11257 query_size_account = 3 + strlen(sql_format_account);
11258 sql_account = malloc(query_size_account);
11259 if (sql_account == NULL) {
11260 EM_DEBUG_EXCEPTION("malloc for sql_account is failed %d", query_size_account);
11261 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
11264 snprintf(sql_account, query_size_account, sql_format_account, account_id);
11267 query_size = strlen(sql_format) + strlen(stripped_subject) + 50 + query_size_account + strlen(sql_format_order_by); /* + query_size_mailbox; */
11269 sql_query_string = malloc(query_size);
11271 if (sql_query_string == NULL) {
11272 EM_DEBUG_EXCEPTION("malloc for sql is failed %d", query_size);
11273 err_code = EMAIL_ERROR_OUT_OF_MEMORY;
11277 sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject);
11279 if (account_id > 0)
11280 strcat(sql_query_string, sql_account);
11282 strcat(sql_query_string, sql_format_order_by);
11283 strcat(sql_query_string, ";");
11285 EM_DEBUG_LOG("Query : %s", sql_query_string);
11287 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
11289 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11290 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11292 EM_DEBUG_LOG("Result rows count : %d", count);
11295 result_thread_id = -1;
11297 _get_table_field_data_int (result, &result_thread_id, 3);
11298 _get_table_field_data_time_t(result, &latest_date_time, 4);
11299 _get_table_field_data_int (result, &latest_mail_id_in_thread, 5);
11301 if (latest_date_time < mail_tbl->date_time)
11302 *result_latest_mail_id_in_thread = latest_mail_id_in_thread;
11304 *result_latest_mail_id_in_thread = mail_tbl->mail_id;
11305 EM_DEBUG_LOG("latest_mail_id_in_thread [%d], mail_id [%d]", latest_mail_id_in_thread, mail_tbl->mail_id);
11309 *thread_id = result_thread_id;
11310 *thread_item_count = count;
11312 EM_DEBUG_LOG("Result thread id : %d", *thread_id);
11313 EM_DEBUG_LOG("Result count : %d", *thread_item_count);
11314 EM_DEBUG_LOG("err_code : %d", err_code);
11316 EM_SAFE_FREE(sql_account);
11317 EM_SAFE_FREE(sql_query_string);
11319 sqlite3_free_table(result);
11321 EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
11327 INTERNAL_FUNC int emstorage_get_thread_information(int thread_id, emstorage_mail_tbl_t** mail_tbl, int transaction, int *err_code)
11329 EM_DEBUG_FUNC_BEGIN();
11331 int count = 0, ret = false;
11332 int error = EMAIL_ERROR_NONE;
11333 emstorage_mail_tbl_t *p_data_tbl = NULL;
11334 char conditional_clause[QUERY_SIZE] = {0, };
11336 EM_IF_NULL_RETURN_VALUE(mail_tbl, false);
11338 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE thread_id = %d AND thread_item_count > 0", thread_id);
11339 EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
11341 if(!emstorage_query_mail_tbl(conditional_clause, transaction, &p_data_tbl, &count, &error)) {
11342 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
11347 EM_DEBUG_LOG("thread_id : %d, thread_item_count : %d", p_data_tbl[0].thread_id, p_data_tbl[0].thread_item_count);
11353 *mail_tbl = p_data_tbl;
11354 else if (p_data_tbl != NULL)
11355 emstorage_free_mail(&p_data_tbl, 1, NULL);
11357 if (err_code != NULL)
11360 EM_DEBUG_FUNC_END("ret [%d]", ret);
11365 INTERNAL_FUNC int emstorage_get_sender_list(int account_id, const char *mailbox_name, int search_type, const char *search_value, email_sort_type_t sorting, email_sender_list_t** sender_list, int *sender_count, int *err_code)
11367 EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_name [%p], search_type [%d], search_value [%p], sorting [%d], sender_list[%p], sender_count[%p] err_code[%p]"
11368 , account_id , mailbox_name , search_type , search_value , sorting , sender_list, sender_count, err_code);
11370 if ((!sender_list) ||(!sender_count)) {
11371 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
11372 if (err_code != NULL)
11373 *err_code = EMAIL_ERROR_INVALID_PARAM;
11377 int rc = -1, ret = false;
11378 int error = EMAIL_ERROR_NONE;
11380 int i, col_index = 0;
11381 int read_count = 0;
11382 email_sender_list_t *p_sender_list = NULL;
11383 char sql_query_string[QUERY_SIZE] = {0, };
11384 char **result = NULL;
11385 sqlite3 *local_db_handle = emstorage_get_db_connection();
11387 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11388 "SELECT email_address_sender, alias_sender, COUNT(email_address_sender), SUM(flags_seen_field = 1) "
11393 SNPRINTF(sql_query_string + strlen(sql_query_string), QUERY_SIZE-(strlen(sql_query_string)+1), " WHERE UPPER(mailbox_name) = UPPER('%s') ", mailbox_name);
11394 else /* NULL means all mailbox_name. but except for trash(3), spambox(5), all emails(for GMail, 7) */
11395 SNPRINTF(sql_query_string + strlen(sql_query_string), QUERY_SIZE-(strlen(sql_query_string)+1), " WHERE mailbox_type not in (3, 5, 7, 8) ");
11398 /* '0' (ALL_ACCOUNT) means all account */
11399 if (account_id > ALL_ACCOUNT)
11400 SNPRINTF(sql_query_string + strlen(sql_query_string), QUERY_SIZE-(strlen(sql_query_string)+1), " AND account_id = %d ", account_id);
11402 if (search_value) {
11403 switch (search_type) {
11404 case EMAIL_SEARCH_FILTER_SUBJECT:
11405 SNPRINTF(sql_query_string + strlen(sql_query_string), QUERY_SIZE-(strlen(sql_query_string)+1),
11406 " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\')) ", search_value);
11408 case EMAIL_SEARCH_FILTER_SENDER:
11409 SNPRINTF(sql_query_string + strlen(sql_query_string), QUERY_SIZE-(strlen(sql_query_string)+1),
11410 " AND ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
11411 ") ", search_value);
11413 case EMAIL_SEARCH_FILTER_RECIPIENT:
11414 SNPRINTF(sql_query_string + strlen(sql_query_string), QUERY_SIZE-(strlen(sql_query_string)+1),
11415 " AND ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
11416 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
11417 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
11418 ") ", search_value, search_value, search_value);
11420 case EMAIL_SEARCH_FILTER_ALL:
11421 SNPRINTF(sql_query_string + strlen(sql_query_string), QUERY_SIZE-(strlen(sql_query_string)+1),
11422 " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') "
11423 " OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
11424 " OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
11425 " OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
11426 " OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
11429 ")", search_value, search_value, search_value, search_value, search_value);
11435 /* sorting option is not available now. The order of sender list is ascending order by display name */
11436 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1),
11437 "GROUP BY email_address_sender "
11438 "ORDER BY UPPER(alias_sender) ");
11440 EM_DEBUG_LOG("query[%s]", sql_query_string);
11442 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
11443 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11444 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11446 EM_DEBUG_LOG("Count of Sender [%d]", count);
11448 if (!(p_sender_list = (email_sender_list_t*)em_malloc(sizeof(email_sender_list_t) * count))) {
11449 EM_DEBUG_EXCEPTION("em_malloc failed...");
11450 error = EMAIL_ERROR_OUT_OF_MEMORY;
11456 EM_DEBUG_LOG(">>>> DATA ASSIGN START >>");
11457 for (i = 0; i < count; i++) {
11458 _get_table_field_data_string(result, &(p_sender_list[i].address), 1, col_index++);
11459 _get_table_field_data_string(result, &(p_sender_list[i].display_name), 1, col_index++);
11460 _get_table_field_data_int(result, &(p_sender_list[i].total_count), col_index++);
11461 _get_table_field_data_int(result, &(read_count), col_index++);
11462 p_sender_list[i].unread_count = p_sender_list[i].total_count - read_count; /* unread count = total - read */
11464 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >>", count);
11466 sqlite3_free_table(result);
11473 *sender_list = p_sender_list;
11474 *sender_count = count;
11475 EM_DEBUG_LOG(">>>> COUNT : %d >>", count);
11477 else if (p_sender_list != NULL)
11478 emstorage_free_sender_list(&p_sender_list, count);
11482 if (err_code != NULL)
11485 EM_DEBUG_FUNC_END("ret [%d]", ret);
11489 INTERNAL_FUNC int emstorage_free_sender_list(email_sender_list_t **sender_list, int count)
11491 EM_DEBUG_FUNC_BEGIN("sender_list[%p], count[%d]", sender_list, count);
11493 int err = EMAIL_ERROR_NONE;
11496 if (!sender_list || !*sender_list) {
11497 EM_DEBUG_EXCEPTION("sender_list[%p], count[%d]", sender_list, count);
11498 err = EMAIL_ERROR_INVALID_PARAM;
11502 email_sender_list_t* p = *sender_list;
11505 for (; i < count; i++) {
11506 EM_SAFE_FREE(p[i].address);
11507 EM_SAFE_FREE(p[i].display_name);
11511 *sender_list = NULL;
11518 INTERNAL_FUNC int emstorage_free_address_info_list(email_address_info_list_t **address_info_list)
11520 EM_DEBUG_FUNC_BEGIN("address_info_list[%p]", address_info_list);
11522 int err = EMAIL_ERROR_NONE;
11523 email_address_info_t *p_address_info = NULL;
11524 GList *list = NULL;
11525 GList *node = NULL;
11528 if (!address_info_list || !*address_info_list) {
11529 EM_DEBUG_EXCEPTION("address_info_list[%p]", address_info_list);
11530 err = EMAIL_ERROR_INVALID_PARAM;
11534 /* delete GLists */
11535 for (i = EMAIL_ADDRESS_TYPE_FROM; i <= EMAIL_ADDRESS_TYPE_BCC; i++) {
11537 case EMAIL_ADDRESS_TYPE_FROM:
11538 list = (*address_info_list)->from;
11540 case EMAIL_ADDRESS_TYPE_TO:
11541 list = (*address_info_list)->to;
11543 case EMAIL_ADDRESS_TYPE_CC:
11544 list = (*address_info_list)->cc;
11546 case EMAIL_ADDRESS_TYPE_BCC:
11547 list = (*address_info_list)->bcc;
11551 /* delete dynamic-allocated memory for each item */
11552 node = g_list_first(list);
11553 while (node != NULL) {
11554 p_address_info = (email_address_info_t*)node->data;
11555 EM_SAFE_FREE(p_address_info->address);
11556 EM_SAFE_FREE(p_address_info->display_name);
11557 EM_SAFE_FREE(node->data);
11559 node = g_list_next(node);
11563 EM_SAFE_FREE(*address_info_list);
11564 *address_info_list = NULL;
11566 EM_DEBUG_FUNC_END("err [%d]", err);
11569 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
11571 INTERNAL_FUNC int emstorage_add_pbd_activity(email_event_partial_body_thd* local_activity, int *activity_id, int transaction, int *err_code)
11573 EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
11575 if (!local_activity || !activity_id) {
11576 EM_DEBUG_EXCEPTION("local_activity[%p], transaction[%d], activity_id[%p], err_code[%p]", local_activity, activity_id, transaction, err_code);
11577 if (err_code != NULL)
11578 *err_code = EMAIL_ERROR_INVALID_PARAM;
11584 int error = EMAIL_ERROR_NONE;
11587 char sql_query_string[QUERY_SIZE] = {0, };
11588 DB_STMT hStmt = NULL;
11589 sqlite3 *local_db_handle = emstorage_get_db_connection();
11590 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
11591 memset(sql_query_string, 0x00, sizeof(sql_query_string));
11592 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11593 "INSERT INTO mail_partial_body_activity_tbl VALUES "
11595 "? " /* Account ID */
11596 ",?" /* Local Mail ID */
11597 ",?" /* Server mail ID */
11598 ",?" /* Activity ID */
11599 ",?" /* Activity type*/
11600 ",?" /* Mailbox ID*/
11601 ",?" /* Mailbox name*/
11604 char *sql = "SELECT max(rowid) FROM mail_partial_body_activity_tbl;";
11605 char **result = NULL;
11608 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
11610 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11611 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
11613 if (NULL==result[1]) rc = 1;
11614 else rc = atoi(result[1])+1;
11615 sqlite3_free_table(result);
11618 *activity_id = local_activity->activity_id = rc;
11620 EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ], MAIL ID [ %d ], ACTIVITY TYPE [ %d ], SERVER MAIL ID [ %lu ]", \
11621 local_activity->activity_id, local_activity->mail_id, local_activity->activity_type, local_activity->server_mail_id);
11623 if (local_activity->mailbox_id)
11624 EM_DEBUG_LOG(" MAILBOX ID [ %d ]", local_activity->mailbox_id);
11627 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
11628 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11629 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11631 EM_DEBUG_LOG(">>>> SQL STMT [ %s ]", sql_query_string);
11633 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
11634 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
11635 _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
11636 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
11637 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
11638 _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
11639 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->mailbox_name, 0, 3999);
11642 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11644 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
11645 ("sqlite3_step fail:%d", rc));
11646 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11647 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
11653 if (hStmt != NULL) {
11654 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
11656 rc = sqlite3_finalize(hStmt);
11658 if (rc != SQLITE_OK) {
11659 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
11660 error = EMAIL_ERROR_DB_FAILURE;
11664 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
11666 if (err_code != NULL)
11669 EM_DEBUG_FUNC_END("ret [%d]", ret);
11674 INTERNAL_FUNC int emstorage_get_pbd_mailbox_list(int account_id, int **mailbox_list, int *count, int transaction, int *err_code)
11676 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
11678 if (account_id < FIRST_ACCOUNT_ID || NULL == &mailbox_list || NULL == count) {
11679 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
11680 if (err_code != NULL)
11681 *err_code = EMAIL_ERROR_INVALID_PARAM;
11686 int error = EMAIL_ERROR_NONE;
11688 int i = 0, rc = -1;
11689 int *mbox_list = NULL;
11690 DB_STMT hStmt = NULL;
11691 char sql_query_string[QUERY_SIZE] = {0, };
11693 sqlite3 *local_db_handle = emstorage_get_db_connection();
11695 EMSTORAGE_START_READ_TRANSACTION(transaction);
11697 memset(sql_query_string, 0x00, sizeof(sql_query_string));
11698 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);
11701 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
11702 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11703 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11705 *count = atoi(result[1]);
11706 sqlite3_free_table(result);
11709 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
11710 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
11714 EM_DEBUG_LOG("Mailbox count = %d", *count);
11716 memset(sql_query_string, 0x00, sizeof(sql_query_string));
11718 /* 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); */
11719 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);
11721 EM_DEBUG_LOG(" Query [%s]", sql_query_string);
11724 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
11726 EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
11727 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11728 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11731 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11732 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11733 ("sqlite3_step fail:%d", rc));
11735 if (NULL == (mbox_list = (int *)em_malloc(sizeof(int *) * (*count)))) {
11736 EM_DEBUG_EXCEPTION(" em_malloc failed...");
11737 error = EMAIL_ERROR_OUT_OF_MEMORY;
11741 memset(mbox_list, 0x00, sizeof(int) * (*count));
11743 for (i = 0; i < (*count); i++) {
11744 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
11745 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11746 /* EM_DEBUG_LOG("In emstorage_get_pdb_mailbox_list() loop, After sqlite3_step(), , i = %d, rc = %d.", i, rc); */
11747 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11748 ("sqlite3_step fail:%d", rc));
11749 EM_DEBUG_LOG("mbox_list %d", mbox_list + i);
11756 *mailbox_list = mbox_list;
11758 if (hStmt != NULL) {
11759 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
11761 rc = sqlite3_finalize(hStmt);
11763 if (rc != SQLITE_OK) {
11764 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
11765 error = EMAIL_ERROR_DB_FAILURE;
11769 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
11771 if (err_code != NULL)
11773 EM_DEBUG_FUNC_END("ret [%d]", ret);
11778 INTERNAL_FUNC int emstorage_get_pbd_account_list(int **account_list, int *count, int transaction, int *err_code)
11780 EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
11782 if (NULL == &account_list || NULL == count) {
11783 EM_DEBUG_EXCEPTION("mailbox_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
11784 if (err_code != NULL)
11785 *err_code = EMAIL_ERROR_INVALID_PARAM;
11790 int error = EMAIL_ERROR_NONE;
11793 int i = 0, rc = -1;
11794 int *result_account_list = NULL;
11795 DB_STMT hStmt = NULL;
11796 char sql_query_string[QUERY_SIZE] = {0, };
11798 sqlite3 *local_db_handle = emstorage_get_db_connection();
11800 EMSTORAGE_START_READ_TRANSACTION(transaction);
11803 sql = "SELECT count(distinct account_id) FROM mail_partial_body_activity_tbl";
11806 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
11807 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11808 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
11810 *count = atoi(result[1]);
11811 sqlite3_free_table(result);
11814 EM_DEBUG_EXCEPTION("no account found...");
11815 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
11820 EM_DEBUG_LOG("Account count [%d]", *count);
11822 memset(sql_query_string, 0x00, sizeof(sql_query_string));
11824 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_partial_body_activity_tbl");
11826 EM_DEBUG_LOG("Query [%s]", sql_query_string);
11829 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
11831 EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
11832 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11833 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11836 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11837 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11838 ("sqlite3_step fail:%d", rc));
11840 if (NULL == (result_account_list = (int *)em_malloc(sizeof(int) * (*count)))) {
11841 EM_DEBUG_EXCEPTION(" em_malloc failed...");
11842 error = EMAIL_ERROR_OUT_OF_MEMORY;
11846 memset(result_account_list, 0x00, sizeof(int) * (*count));
11848 for (i = 0; i < (*count); i++) {
11849 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
11851 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11852 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11853 ("sqlite3_step fail:%d", rc));
11854 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
11861 *account_list = result_account_list;
11863 if (hStmt != NULL) {
11864 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
11866 rc = sqlite3_finalize(hStmt);
11868 if (rc != SQLITE_OK) {
11869 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
11870 error = EMAIL_ERROR_DB_FAILURE;
11874 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
11876 if (err_code != NULL)
11878 EM_DEBUG_FUNC_END("ret [%d]", ret);
11884 INTERNAL_FUNC int emstorage_get_pbd_activity_data(int account_id, int input_mailbox_id, email_event_partial_body_thd** event_start, int *count, int transaction, int *err_code)
11886 EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
11888 if (account_id < FIRST_ACCOUNT_ID || NULL == event_start || 0 == input_mailbox_id || NULL == count) {
11889 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);
11891 if (err_code != NULL)
11892 *err_code = EMAIL_ERROR_INVALID_PARAM;
11899 int error = EMAIL_ERROR_NONE;
11901 DB_STMT hStmt = NULL;
11902 email_event_partial_body_thd* event_list = NULL;
11903 char sql_query_string[QUERY_SIZE] = {0, };
11905 sqlite3 *local_db_handle = emstorage_get_db_connection();
11907 EMSTORAGE_START_READ_TRANSACTION(transaction);
11909 memset(sql_query_string, 0x00, sizeof(sql_query_string));
11910 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);
11913 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
11914 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
11915 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11917 *count = atoi(result[1]);
11918 sqlite3_free_table(result);
11920 EM_DEBUG_LOG("Query = [%s]", sql_query_string);
11923 EM_DEBUG_EXCEPTION("No matched activity found in mail_partial_body_activity_tbl");
11925 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11929 EM_DEBUG_LOG("Activity Count = %d", *count);
11931 memset(sql_query_string, 0x00, sizeof(sql_query_string));
11932 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);
11934 EM_DEBUG_LOG("Query [%s]", sql_query_string);
11937 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
11939 EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
11940 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11941 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11944 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11945 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11946 ("sqlite3_step fail:%d", rc));
11948 if (!(event_list = (email_event_partial_body_thd*)em_malloc(sizeof(email_event_partial_body_thd) * (*count)))) {
11949 EM_DEBUG_EXCEPTION("Malloc failed");
11951 error = EMAIL_ERROR_OUT_OF_MEMORY;
11954 memset(event_list, 0x00, sizeof(email_event_partial_body_thd) * (*count));
11956 for (i=0; i < (*count); i++) {
11957 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_IDX_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
11958 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
11959 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
11960 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
11961 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_type), ACTIVITY_TYPE_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
11962 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
11963 _get_stmt_field_data_string(hStmt, &(event_list[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
11965 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11966 /* EM_DEBUG_LOG("In emstorage_get_pbd_activity_data() loop, After sqlite3_step(), , i = %d, rc = %d.", i, rc); */
11967 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11968 ("sqlite3_step fail:%d", rc));
11970 event_list[i].event_type = 0;
11977 *event_start = event_list;
11979 EM_SAFE_FREE(event_list);
11982 if (hStmt != NULL) {
11983 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
11985 rc = sqlite3_finalize(hStmt);
11987 if (rc != SQLITE_OK) {
11988 EM_DEBUG_LOG("sqlite3_finalize failed - %d", rc);
11989 error = EMAIL_ERROR_DB_FAILURE;
11993 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
11995 if (err_code != NULL)
11998 EM_DEBUG_FUNC_END("ret [%d]", ret);
12004 INTERNAL_FUNC int emstorage_delete_pbd_activity(int account_id, int mail_id, int activity_id, int transaction, int *err_code)
12006 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);
12009 if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
12010 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);
12012 if (err_code != NULL)
12013 *err_code = EMAIL_ERROR_INVALID_PARAM;
12019 int error = EMAIL_ERROR_NONE;
12020 char sql_query_string[QUERY_SIZE] = {0, };
12022 sqlite3 *local_db_handle = emstorage_get_db_connection();
12024 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12025 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12027 if (activity_id == 0)
12028 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);
12030 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);
12032 EM_DEBUG_LOG("Query [%s]", sql_query_string);
12034 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12035 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12036 ("sqlite3_exec fail:%d", rc));
12037 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12038 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12040 /* validate activity existence */
12041 rc = sqlite3_changes(local_db_handle);
12043 EM_DEBUG_EXCEPTION("No matching activity found");
12044 error = EMAIL_ERROR_DATA_NOT_FOUND;
12053 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12056 if (err_code != NULL)
12059 EM_DEBUG_FUNC_END("ret [%d]", ret);
12063 INTERNAL_FUNC int emstorage_get_mailbox_pbd_activity_count(int account_id, int input_mailbox_id, int *activity_count, int transaction, int *err_code)
12065 EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
12067 if (account_id < FIRST_ACCOUNT_ID || NULL == activity_count || NULL == err_code) {
12068 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
12069 if (err_code != NULL)
12070 *err_code = EMAIL_ERROR_INVALID_PARAM;
12075 int error = EMAIL_ERROR_NONE;
12076 char sql_query_string[QUERY_SIZE] = {0, };
12078 DB_STMT hStmt = NULL;
12080 sqlite3 *local_db_handle = emstorage_get_db_connection();
12082 EMSTORAGE_START_READ_TRANSACTION(transaction);
12083 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12085 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);
12087 EM_DEBUG_LOG(" Query [%s]", sql_query_string);
12090 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
12091 EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
12092 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12093 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12096 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12097 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12098 ("sqlite3_step fail:%d", rc));
12100 _get_stmt_field_data_int(hStmt, activity_count, 0);
12102 EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
12108 if (hStmt != NULL) {
12109 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12110 rc = sqlite3_finalize(hStmt);
12112 if (rc != SQLITE_OK) {
12113 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
12114 error = EMAIL_ERROR_DB_FAILURE;
12116 EM_DEBUG_LOG("sqlite3_finalize- %d", rc);
12119 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12122 if (err_code != NULL)
12125 EM_DEBUG_FUNC_END("ret [%d]", ret);
12130 INTERNAL_FUNC int emstorage_get_pbd_activity_count(int *activity_count, int transaction, int *err_code)
12132 EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
12134 if (NULL == activity_count || NULL == err_code) {
12135 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
12136 if (err_code != NULL)
12137 *err_code = EMAIL_ERROR_INVALID_PARAM;
12142 int error = EMAIL_ERROR_NONE;
12143 DB_STMT hStmt = NULL;
12144 char sql_query_string[QUERY_SIZE] = {0, };
12146 sqlite3 *local_db_handle = emstorage_get_db_connection();
12148 EMSTORAGE_START_READ_TRANSACTION(transaction);
12149 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12151 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl;");
12153 EM_DEBUG_LOG(" Query [%s]", sql_query_string);
12156 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
12157 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
12158 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12159 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12162 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12163 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12164 ("sqlite3_step fail:%d", rc));
12166 _get_stmt_field_data_int(hStmt, activity_count, 0);
12168 EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
12175 if (hStmt != NULL) {
12176 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
12178 rc = sqlite3_finalize(hStmt);
12180 if (rc != SQLITE_OK) {
12181 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
12182 error = EMAIL_ERROR_DB_FAILURE;
12184 EM_DEBUG_LOG("sqlite3_finalize- %d", rc);
12187 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12189 if (err_code != NULL)
12192 EM_DEBUG_FUNC_END("ret [%d]", ret);
12196 INTERNAL_FUNC int emstorage_delete_full_pbd_activity_data(int account_id, int transaction, int *err_code)
12198 EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
12199 if (account_id < FIRST_ACCOUNT_ID) {
12200 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
12201 if (err_code != NULL)
12202 *err_code = EMAIL_ERROR_INVALID_PARAM;
12208 int error = EMAIL_ERROR_NONE;
12209 char sql_query_string[QUERY_SIZE] = {0, };
12211 sqlite3 *local_db_handle = emstorage_get_db_connection();
12212 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12213 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12214 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d", account_id);
12216 EM_DEBUG_LOG("Query [%s]", sql_query_string);
12218 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12219 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12220 ("sqlite3_exec fail:%d", rc));
12221 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12222 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12225 rc = sqlite3_changes(local_db_handle);
12227 EM_DEBUG_EXCEPTION("No matching activities found in mail_partial_body_activity_tbl");
12228 error = EMAIL_ERROR_DATA_NOT_FOUND;
12237 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12239 if (err_code != NULL)
12242 EM_DEBUG_FUNC_END("ret [%d]", ret);
12246 /*Himanshu[h.gahlaut]-> Added below API to update mail_partial_body_activity_tbl
12247 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*/
12249 INTERNAL_FUNC int emstorage_update_pbd_activity(char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code)
12251 EM_DEBUG_FUNC_BEGIN("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
12253 int rc = -1, ret = false;
12254 int error = EMAIL_ERROR_NONE;
12255 char sql_query_string[QUERY_SIZE] = {0, };
12257 int transaction = true;
12259 if (!old_server_uid || !new_server_uid || !mbox_name) {
12260 EM_DEBUG_EXCEPTION("Invalid parameters");
12261 error = EMAIL_ERROR_INVALID_PARAM;
12265 sqlite3 *local_db_handle = emstorage_get_db_connection();
12266 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12267 memset(sql_query_string, 0x00, sizeof(sql_query_string));
12268 SNPRINTF(sql_query_string, sizeof(sql_query_string),
12269 "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);
12271 EM_DEBUG_LOG("Query [%s]", sql_query_string);
12273 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12274 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12275 ("sqlite3_exec fail:%d", rc));
12276 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12277 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12279 rc = sqlite3_changes(local_db_handle);
12281 EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
12282 error = EMAIL_ERROR_DATA_NOT_FOUND;
12290 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12292 if (err_code != NULL)
12295 EM_DEBUG_FUNC_END("ret [%d]", ret);
12300 INTERNAL_FUNC int emstorage_create_file(char *data_string, size_t file_size, char *dst_file_name, int *err_code)
12302 EM_DEBUG_FUNC_BEGIN("file_size[%d] , dst_file_name[%s], err_code[%p]", file_size, dst_file_name, err_code);
12305 int error = EMAIL_ERROR_NONE;
12307 FILE* fp_dst = NULL;
12309 if (!data_string || !dst_file_name) {
12310 EM_DEBUG_EXCEPTION("data_string[%p], dst_file_name[%p]", data_string, dst_file_name);
12311 error = EMAIL_ERROR_INVALID_PARAM;
12315 fp_dst = fopen(dst_file_name, "w");
12318 EM_DEBUG_EXCEPTION("fopen failed - %s", dst_file_name);
12320 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
12322 error = EMAIL_ERROR_SYSTEM_FAILURE;
12326 if (fwrite(data_string, 1, file_size, fp_dst) < 0) {
12327 EM_DEBUG_EXCEPTION("fwrite failed...");
12328 error = EMAIL_ERROR_UNKNOWN;
12336 if (fp_dst != NULL)
12339 if (err_code != NULL)
12342 EM_DEBUG_FUNC_END("ret [%d]", ret);
12346 #endif /* __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
12350 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
12352 INTERNAL_FUNC int emstorage_update_read_mail_uid_by_server_uid(char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code)
12354 EM_DEBUG_FUNC_BEGIN();
12357 int error = EMAIL_ERROR_NONE;
12358 char sql_query_string[QUERY_SIZE] = {0, };
12360 int transaction = true;
12362 if (!old_server_uid || !new_server_uid || !mbox_name) {
12363 EM_DEBUG_EXCEPTION("Invalid parameters");
12364 error = EMAIL_ERROR_INVALID_PARAM;
12368 EM_DEBUG_LOG("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
12370 sqlite3 *local_db_handle = emstorage_get_db_connection();
12371 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12374 SNPRINTF(sql_query_string, sizeof(sql_query_string),
12375 "UPDATE mail_read_mail_uid_tbl SET s_uid=\'%s\' , mailbox_id=\'%s\', mailbox_name=\'%s\' WHERE s_uid=%s ", new_server_uid, mbox_name, mbox_name, old_server_uid);
12377 EM_DEBUG_LOG(" Query [%s]", sql_query_string);
12379 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12380 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12381 ("sqlite3_exec fail:%d", rc));
12382 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12383 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12386 rc = sqlite3_changes(local_db_handle);
12389 EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
12390 error = EMAIL_ERROR_DATA_NOT_FOUND;
12399 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12402 if (err_code != NULL)
12405 EM_DEBUG_FUNC_END("ret [%d]", ret);
12412 * @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);
12413 * Prepare an array of mail_id and corresponding server mail id.
12415 *@author h.gahlaut@samsung.com
12416 * @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)
12417 * where 88 is the length of fixed keywords including ending null character in the QUERY to be formed
12418 * @param[out] idset Returns the array of mail_id and corresponding server_mail_id sorted by server_mail_ids
12419 * @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
12420 * @param[out] err_code Returns the error code.
12421 * @remarks An Example of Query to be exexuted in this API:
12422 * SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (12, 13, 56, 78);
12423 * @return This function returns true on success or false on failure.
12426 INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *mail_ids, email_id_set_t** idset, int *id_set_count, int *err_code)
12428 EM_DEBUG_FUNC_BEGIN();
12429 EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds);
12431 int error = EMAIL_ERROR_NONE;
12433 email_id_set_t* p_id_set = NULL;
12435 const int buf_size = QUERY_SIZE;
12436 char sql_query_string[QUERY_SIZE] = {0, };
12437 int space_left_in_query_buffer = buf_size;
12440 char *server_mail_id = NULL;
12441 char **result = NULL;
12445 if (NULL == mail_ids || NULL == idset || NULL == id_set_count) {
12446 EM_DEBUG_EXCEPTION("Invalid Parameters mail_ids[%p] idset[%p] id_set_count [%p]", mail_ids, idset, id_set_count);
12447 error = EMAIL_ERROR_INVALID_PARAM;
12451 sqlite3 *local_db_handle = emstorage_get_db_connection();
12453 SNPRINTF(sql_query_string, space_left_in_query_buffer, "SELECT local_uid, s_uid FROM mail_read_mail_uid_tbl WHERE local_uid in (%s) ORDER BY s_uid", mail_ids);
12455 EM_DEBUG_LOG("SQL Query formed [%s] ", sql_query_string);
12457 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
12458 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
12459 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12460 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12462 EM_DEBUG_LOG(" Count of mails [%d ]", count);
12465 EM_DEBUG_EXCEPTION("Can't find proper mail");
12466 error = EMAIL_ERROR_DATA_NOT_FOUND;
12471 if (NULL == (p_id_set = (email_id_set_t*)em_malloc(sizeof(email_id_set_t) * count))) {
12472 EM_DEBUG_EXCEPTION(" em_malloc failed...");
12473 error = EMAIL_ERROR_OUT_OF_MEMORY;
12479 EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds_Loop);
12480 EM_DEBUG_LOG(">>>> DATA ASSIGN START");
12481 for (i = 0; i < count; i++) {
12482 _get_table_field_data_int(result, &(p_id_set[i].mail_id), col_index++);
12483 _get_table_field_data_string(result, &server_mail_id, 1, col_index++);
12484 p_id_set[i].server_mail_id = strtoul(server_mail_id, NULL, 10);
12485 EM_SAFE_FREE(server_mail_id);
12487 EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d]", count);
12488 EM_PROFILE_END(EmStorageGetIdSetFromMailIds_Loop);
12490 sqlite3_free_table(result);
12499 *id_set_count = count;
12500 EM_DEBUG_LOG(" idset[%p] id_set_count [%d]", *idset, *id_set_count);
12503 EM_SAFE_FREE(p_id_set);
12507 if (err_code != NULL)
12510 EM_PROFILE_END(EmStorageGetIdSetFromMailIds);
12512 EM_DEBUG_FUNC_END("ret [%d]", ret);
12520 INTERNAL_FUNC int emstorage_delete_triggers_from_lucene()
12522 EM_DEBUG_FUNC_BEGIN();
12523 int rc, ret = true, transaction = true;
12524 int error = EMAIL_ERROR_NONE;
12525 char sql_query_string[QUERY_SIZE] = {0, };
12527 sqlite3 *local_db_handle = emstorage_get_db_connection();
12528 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12530 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerDelete;");
12532 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12533 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12534 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12536 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerInsert;");
12538 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12539 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12540 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12543 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerUpdate;");
12545 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12546 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12547 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12553 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12557 EM_DEBUG_FUNC_END("ret [%d]", ret);
12561 INTERNAL_FUNC int emstorage_filter_mails_by_rule(int account_id, int dest_mailbox_id, emstorage_rule_tbl_t *rule, int ** filtered_mail_id_list, int *count_of_mails, int *err_code)
12563 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);
12565 if ((account_id <= 0) || (dest_mailbox_id <= 0) || (!rule) || (!rule->value)|| (!filtered_mail_id_list) || (!count_of_mails)) {
12566 EM_DEBUG_EXCEPTION("Invalid Parameter");
12568 if (err_code != NULL)
12569 *err_code = EMAIL_ERROR_INVALID_PARAM;
12573 int rc = -1, ret = false, error = EMAIL_ERROR_NONE;
12574 int count = 0, col_index = 0, i = 0, where_pararaph_length = 0, *mail_list = NULL;
12575 char **result = NULL, *where_pararaph = NULL;
12576 char sql_query_string[QUERY_SIZE] = {0, };
12578 sqlite3 *local_db_handle = emstorage_get_db_connection();
12580 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl ");
12582 EM_DEBUG_LOG("rule->value [%s]", rule->value);
12584 where_pararaph_length = strlen(rule->value) + 100;
12585 where_pararaph = malloc(sizeof(char) * where_pararaph_length);
12587 if (where_pararaph == NULL) {
12588 EM_DEBUG_EXCEPTION("malloc failed for where_pararaph.");
12589 error = EMAIL_ERROR_OUT_OF_MEMORY;
12593 memset(where_pararaph, 0, sizeof(char) * where_pararaph_length);
12595 EM_DEBUG_LOG("rule->type [%d], rule->flag2[%d]", rule->type, rule->flag2);
12597 if (rule->type == EMAIL_FILTER_FROM) {
12598 if (rule->flag2 == RULE_TYPE_INCLUDES)
12599 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (3,5) AND full_address_from like \'%%%s%%\'", account_id, rule->value);
12600 else /* RULE_TYPE_EXACTLY */
12601 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (3,5) AND full_address_from = \'%s\'", account_id, rule->value);
12603 else if (rule->type == EMAIL_FILTER_SUBJECT) {
12604 if (rule->flag2 == RULE_TYPE_INCLUDES)
12605 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (3,5) AND subject like \'%%%s%%\'", account_id, rule->value);
12606 else /* RULE_TYPE_EXACTLY */
12607 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (3,5) AND subject = \'%s\'", account_id, rule->value);
12610 error = EMAIL_ERROR_INVALID_PARAM;
12611 EM_DEBUG_EXCEPTION("rule->type is invald");
12615 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
12616 strcat(sql_query_string, where_pararaph);
12618 EM_DEBUG_LOG("query[%s]", sql_query_string);
12620 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
12621 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
12622 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12623 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12625 EM_DEBUG_LOG("Count of mails [%d]", count);
12628 mail_list = malloc(sizeof(int) * count);
12629 if (mail_list == NULL) {
12630 EM_DEBUG_EXCEPTION("malloc failed for mail_list.");
12631 error = EMAIL_ERROR_OUT_OF_MEMORY;
12637 for (i = 0; i < count; i++)
12638 _get_table_field_data_int(result, &(mail_list[i]), col_index++);
12640 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET mailbox_id = %d, mailbox_type = 5 ", dest_mailbox_id);
12642 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
12643 strcat(sql_query_string, where_pararaph);
12645 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12646 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12647 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12650 *filtered_mail_id_list = mail_list;
12656 sqlite3_free_table(result);
12661 EM_SAFE_FREE(where_pararaph);
12663 if (ret && count_of_mails)
12664 *count_of_mails = count;
12666 if (err_code != NULL)
12669 EM_DEBUG_FUNC_END("ret [%d]", ret);
12673 #define EMAIL_SLOT_UNIT 25
12675 INTERNAL_FUNC int emstorage_set_mail_slot_size(int account_id, int mailbox_id, int new_slot_size, int transaction, int *err_code)
12677 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);
12678 int rc = -1, ret = false, err = EMAIL_ERROR_NONE;
12679 int where_pararaph_length = 0;
12680 char *where_pararaph = NULL;
12681 char sql_query_string[QUERY_SIZE] = {0, };
12683 sqlite3 *local_db_handle = emstorage_get_db_connection();
12685 EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
12687 if (new_slot_size > 0)
12688 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = %d ", new_slot_size);
12689 else if (new_slot_size == 0)
12690 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", EMAIL_SLOT_UNIT);
12692 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", new_slot_size * -1);
12696 where_pararaph_length = 80;
12698 where_pararaph_length = 50;
12700 if (new_slot_size == 0)
12701 where_pararaph_length += 70;
12703 where_pararaph = malloc(sizeof(char) * where_pararaph_length);
12704 if (where_pararaph == NULL) {
12705 EM_DEBUG_EXCEPTION("Memory allocation failed for where_pararaph");
12706 err = EMAIL_ERROR_OUT_OF_MEMORY;
12709 memset(where_pararaph, 0x00, where_pararaph_length);
12711 if (account_id > ALL_ACCOUNT) {
12714 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_id = %d ", mailbox_id);
12716 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d ", account_id);
12719 if (new_slot_size == 0)
12720 SNPRINTF(where_pararaph + strlen(where_pararaph), where_pararaph_length - strlen(where_pararaph), " %s total_mail_count_on_server > mail_slot_size ", (and ? "AND" : "WHERE"));
12722 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
12723 strcat(sql_query_string, where_pararaph);
12725 EM_DEBUG_EXCEPTION("Query buffer overflowed !!!");
12726 err = EMAIL_ERROR_OUT_OF_MEMORY;
12730 EM_DEBUG_LOG("query[%s]", sql_query_string);
12732 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
12733 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12734 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12740 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
12744 EM_SAFE_FREE(where_pararaph);
12746 if (err_code != NULL)
12749 EM_DEBUG_FUNC_END("ret [%d]", ret);
12753 INTERNAL_FUNC int emstorage_add_meeting_request(int account_id, int input_mailbox_id, email_meeting_request_t* meeting_req, int transaction, int *err_code)
12755 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);
12757 if (!meeting_req || meeting_req->mail_id <= 0) {
12759 EM_DEBUG_EXCEPTION("mail_id[%]d", meeting_req->mail_id);
12761 if (err_code != NULL)
12762 *err_code = EMAIL_ERROR_INVALID_PARAM;
12769 int error = EMAIL_ERROR_NONE;
12770 DB_STMT hStmt = NULL;
12771 char sql_query_string[QUERY_SIZE] = {0, };
12773 time_t temp_unix_time = 0;
12775 sqlite3 *local_db_handle = emstorage_get_db_connection();
12776 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
12778 SNPRINTF(sql_query_string, sizeof(sql_query_string),
12779 "INSERT INTO mail_meeting_tbl VALUES "
12780 "( ?" /* mail_id */
12781 ", ?" /* account_id */
12782 ", ?" /* mailbox_id */
12783 ", ?" /* meeting_response */
12784 ", ?" /* start_time */
12785 ", ?" /* end_time */
12786 ", ?" /* location */
12787 ", ?" /* global_object_id */
12789 ", ?" /* standard_name */
12790 ", ?" /* standard_time_start_date */
12791 ", ?" /* standard_biad */
12792 ", ?" /* daylight_name */
12793 ", ?" /* daylight_time_start_date */
12794 ", ?" /* daylight_bias */
12797 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
12798 if (rc != SQLITE_OK) {
12799 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
12800 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
12802 error = EMAIL_ERROR_DB_FAILURE;
12808 EM_DEBUG_LOG(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
12809 EM_DEBUG_LOG(">>>>> account_id[%d]", account_id);
12810 EM_DEBUG_LOG(">>>>> mailbox_name[%s]", mailbox_name);
12811 EM_DEBUG_LOG(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
12812 EM_DEBUG_LOG(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
12813 EM_DEBUG_LOG(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
12814 EM_DEBUG_LOG(">>>>> meeting_req->location[%s]", meeting_req->location);
12815 EM_DEBUG_LOG(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
12816 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
12817 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
12818 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
12819 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
12820 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
12821 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
12822 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
12824 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->mail_id);
12825 _bind_stmt_field_data_int(hStmt, col_index++, account_id);
12826 _bind_stmt_field_data_int(hStmt, col_index++, input_mailbox_id);
12827 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
12829 temp_unix_time = timegm(&(meeting_req->start_time));
12830 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
12831 temp_unix_time = timegm(&(meeting_req->end_time));
12832 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
12834 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->location, 0, LOCATION_LEN_IN_MAIL_MEETING_TBL);
12835 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->global_object_id, 0, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
12837 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
12838 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.standard_name, 0, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
12839 temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
12840 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
12841 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
12843 _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.daylight_name, 0, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
12844 temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
12845 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
12846 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
12849 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12850 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12851 ("sqlite3_step fail:%d", rc));
12852 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12853 ("sqlite3_step fail:%d", rc));
12858 if (hStmt != NULL) {
12859 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
12861 rc = sqlite3_finalize(hStmt);
12862 if (rc != SQLITE_OK) {
12863 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
12864 error = EMAIL_ERROR_DB_FAILURE;
12868 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
12871 if (err_code != NULL)
12874 EM_DEBUG_FUNC_END("ret [%d]", ret);
12878 INTERNAL_FUNC int emstorage_get_meeting_request(int mail_id, email_meeting_request_t ** meeting_req, int transaction, int *err_code)
12880 EM_DEBUG_FUNC_BEGIN();
12885 int error = EMAIL_ERROR_NONE;
12886 email_meeting_request_t *p_temp_meeting_req = NULL;
12888 time_t temp_unix_time;
12890 EM_IF_NULL_RETURN_VALUE(meeting_req, false);
12892 DB_STMT hStmt = NULL;
12893 char sql_query_string[QUERY_SIZE] = {0, };
12895 sqlite3 *local_db_handle = emstorage_get_db_connection();
12896 EMSTORAGE_START_READ_TRANSACTION(transaction);
12898 SNPRINTF(sql_query_string, sizeof(sql_query_string),
12899 "SELECT mail_id, meeting_response, start_time, end_time, location, global_object_id, offset, standard_name, standard_time_start_date, standard_bias, daylight_name, daylight_time_start_date, daylight_bias "
12900 " FROM mail_meeting_tbl "
12901 " WHERE mail_id = %d", mail_id);
12902 EM_DEBUG_LOG("sql : %s ", sql_query_string);
12905 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
12907 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12908 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12911 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12912 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12913 ("sqlite3_step fail:%d", rc));
12915 if (rc == SQLITE_DONE) {
12916 EM_DEBUG_EXCEPTION(" no Meeting request found...");
12919 error= EMAIL_ERROR_DATA_NOT_FOUND;
12923 if (!(p_temp_meeting_req = (email_meeting_request_t*)malloc(sizeof(email_meeting_request_t)))) {
12924 EM_DEBUG_EXCEPTION(" malloc failed...");
12925 error = EMAIL_ERROR_OUT_OF_MEMORY;
12929 memset(p_temp_meeting_req, 0x00, sizeof(email_meeting_request_t));
12933 _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->mail_id), col_index++);
12934 _get_stmt_field_data_int(hStmt, (int *)&(p_temp_meeting_req->meeting_response), col_index++);
12935 _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
12936 gmtime_r(&temp_unix_time, &(p_temp_meeting_req->start_time));
12937 _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
12938 gmtime_r(&temp_unix_time, &(p_temp_meeting_req->end_time));
12939 _get_stmt_field_data_string(hStmt, &p_temp_meeting_req->location, 1, col_index++);
12940 _get_stmt_field_data_string(hStmt, &p_temp_meeting_req->global_object_id, 1, col_index++);
12941 _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->time_zone.offset_from_GMT), col_index++);
12943 _get_stmt_field_data_string_without_allocation(hStmt, p_temp_meeting_req->time_zone.standard_name, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL, 1, col_index++);
12944 _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
12945 gmtime_r(&temp_unix_time, &(p_temp_meeting_req->time_zone.standard_time_start_date));
12946 _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->time_zone.standard_bias), col_index++);
12948 _get_stmt_field_data_string_without_allocation(hStmt, p_temp_meeting_req->time_zone.daylight_name, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL, 1, col_index++);
12949 _get_stmt_field_data_int(hStmt, (int *)(&temp_unix_time), col_index++);
12950 gmtime_r(&temp_unix_time, &(p_temp_meeting_req->time_zone.daylight_time_start_date));
12951 _get_stmt_field_data_int(hStmt, &(p_temp_meeting_req->time_zone.daylight_bias), col_index++);
12954 EM_DEBUG_LOG(">>>>> meeting_req->mail_id[%d]", p_temp_meeting_req->mail_id);
12955 EM_DEBUG_LOG(">>>>> meeting_req->meeting_response[%d]", p_temp_meeting_req->meeting_response);
12956 EM_DEBUG_LOG(">>>>> meeting_req->start_time[%s]", asctime(&(p_temp_meeting_req->start_time)));
12957 EM_DEBUG_LOG(">>>>> meeting_req->end_time[%s]", asctime(&(p_temp_meeting_req->end_time)));
12958 EM_DEBUG_LOG(">>>>> meeting_req->location[%s]", p_temp_meeting_req->location);
12959 EM_DEBUG_LOG(">>>>> meeting_req->global_object_id[%s]", p_temp_meeting_req->global_object_id);
12960 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", p_temp_meeting_req->time_zone.offset_from_GMT);
12961 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_name[%s]", p_temp_meeting_req->time_zone.standard_name);
12962 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(p_temp_meeting_req->time_zone.standard_time_start_date)));
12963 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_bias[%d]", p_temp_meeting_req->time_zone.standard_bias);
12964 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_name[%s]", p_temp_meeting_req->time_zone.daylight_name);
12965 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(p_temp_meeting_req->time_zone.daylight_time_start_date)));
12966 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_bias[%d]", p_temp_meeting_req->time_zone.daylight_bias);
12972 *meeting_req = p_temp_meeting_req;
12974 EM_SAFE_FREE(p_temp_meeting_req);
12977 if (hStmt != NULL) {
12978 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
12980 rc = sqlite3_finalize(hStmt);
12982 if (rc != SQLITE_OK) {
12983 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
12984 error = EMAIL_ERROR_DB_FAILURE;
12988 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12991 if (err_code != NULL)
12994 EM_DEBUG_FUNC_END("ret [%d]", ret);
12999 INTERNAL_FUNC int emstorage_update_meeting_request(email_meeting_request_t* meeting_req, int transaction, int *err_code)
13001 EM_DEBUG_FUNC_BEGIN("meeting_req[%p], transaction[%d], err_code[%p]", meeting_req, transaction, err_code);
13004 int error = EMAIL_ERROR_NONE;
13006 DB_STMT hStmt = NULL;
13007 char sql_query_string[QUERY_SIZE] = {0, };
13008 time_t temp_unix_time = 0;
13010 if (!meeting_req) {
13011 EM_DEBUG_EXCEPTION("Invalid Parameter!");
13012 error = EMAIL_ERROR_INVALID_PARAM;
13016 sqlite3 *local_db_handle = emstorage_get_db_connection();
13017 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13019 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13020 SNPRINTF(sql_query_string, sizeof(sql_query_string),
13021 "UPDATE mail_meeting_tbl "
13023 /* " account_id = ?, " // not update here, this can be changed when move or copy */
13024 /* " mailbox_name = ?, " // not update here, this can be changed when move or copy */
13025 " meeting_response = ?, "
13026 " start_time = ?, "
13029 " global_object_id = ?, "
13031 " standard_name = ?, "
13032 " standard_time_start_date = ?, "
13033 " standard_bias = ?, "
13034 " daylight_name = ?, "
13035 " daylight_time_start_date = ?, "
13036 " daylight_bias = ? "
13037 "WHERE mail_id = %d",
13038 meeting_req->mail_id);
13040 EM_DEBUG_LOG("SQL(%s)", sql_query_string);
13042 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
13043 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13044 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13046 EM_DEBUG_LOG(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
13047 EM_DEBUG_LOG(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
13048 EM_DEBUG_LOG(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
13049 EM_DEBUG_LOG(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
13050 EM_DEBUG_LOG(">>>>> meeting_req->location[%s]", meeting_req->location);
13051 EM_DEBUG_LOG(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
13052 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
13053 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
13054 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
13055 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
13056 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
13057 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
13058 EM_DEBUG_LOG(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
13062 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
13063 temp_unix_time = timegm(&(meeting_req->start_time));
13064 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13065 temp_unix_time = timegm(&(meeting_req->end_time));
13066 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13067 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->location, 1, LOCATION_LEN_IN_MAIL_MEETING_TBL);
13068 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->global_object_id, 1, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
13069 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
13070 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.standard_name, 1, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
13071 temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
13072 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13073 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
13074 _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.daylight_name, 1, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
13075 temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
13076 _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
13077 _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
13080 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13081 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13082 ("sqlite3_step fail:%d", rc));
13087 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13089 if (hStmt != NULL) {
13090 EM_DEBUG_LOG("before sqlite3_finalize hStmt = %p", hStmt);
13092 rc = sqlite3_finalize(hStmt);
13093 if (rc != SQLITE_OK) {
13094 EM_DEBUG_EXCEPTION(" sqlite3_finalize failed - %d", rc);
13096 error = EMAIL_ERROR_DB_FAILURE;
13102 if (err_code != NULL)
13105 EM_DEBUG_FUNC_END("ret [%d]", ret);
13109 INTERNAL_FUNC int emstorage_delete_meeting_request(int account_id, int mail_id, int input_mailbox_id, int transaction, int *err_code)
13111 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);
13113 if (account_id < ALL_ACCOUNT || mail_id < 0) {
13114 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d]", account_id, mail_id);
13116 if (err_code != NULL)
13117 *err_code = EMAIL_ERROR_INVALID_PARAM;
13123 int error = EMAIL_ERROR_NONE;
13125 char sql_query_string[QUERY_SIZE] = {0, };
13127 sqlite3 *local_db_handle = emstorage_get_db_connection();
13128 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13130 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_meeting_tbl ");
13132 if (account_id != ALL_ACCOUNT) { /* NOT '0' means a specific account. '0' means all account */
13133 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " WHERE account_id = %d", account_id);
13137 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " %s mail_id = %d", (and ? "AND" : "WHERE"), mail_id);
13140 if (input_mailbox_id > 0) { /* 0 means all mailbox_id */
13141 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1), " %s mailbox_id = '%d'", (and ? "AND" : "WHERE"), input_mailbox_id);
13144 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13145 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13146 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13151 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13157 EM_DEBUG_FUNC_END("ret [%d]", ret);
13162 INTERNAL_FUNC void emstorage_free_meeting_request(email_meeting_request_t *meeting_req)
13164 EM_DEBUG_FUNC_BEGIN();
13166 if (!meeting_req ) {
13167 EM_DEBUG_EXCEPTION("NULL PARAM");
13171 EM_SAFE_FREE(meeting_req->location);
13172 EM_SAFE_FREE(meeting_req->global_object_id);
13174 EM_DEBUG_FUNC_END();
13178 INTERNAL_FUNC int emstorage_get_overflowed_mail_id_list(int account_id, int input_mailbox_id, int mail_slot_size, int **mail_id_list, int *mail_id_count, int transaction, int *err_code)
13180 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);
13181 EM_PROFILE_BEGIN(profile_emstorage_get_overflowed_mail_id_list);
13182 char sql_query_string[QUERY_SIZE] = {0, };
13183 char **result = NULL;
13184 int rc = -1, ret = false;
13185 int error = EMAIL_ERROR_NONE;
13186 int counter = 0, col_index = 0;
13187 int result_mail_id_count = 0;
13188 int *result_mail_id_list = NULL;
13190 if (input_mailbox_id <= 0 || !mail_id_list || !mail_id_count || account_id < 1) {
13191 EM_DEBUG_EXCEPTION("Invalid Parameter");
13192 error = EMAIL_ERROR_INVALID_PARAM;
13196 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);
13198 EM_DEBUG_LOG("query[%s].", sql_query_string);
13200 sqlite3 *local_db_handle = emstorage_get_db_connection();
13201 EMSTORAGE_START_READ_TRANSACTION(transaction);
13203 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_mail_id_count, 0, NULL), rc);
13204 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13205 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13207 if (!result_mail_id_count) {
13208 EM_DEBUG_LOG("No mail found...");
13210 error= EMAIL_ERROR_MAIL_NOT_FOUND;
13214 EM_DEBUG_LOG("There are [%d] overflowed mails in mailbox_id [%d]", result_mail_id_count, input_mailbox_id);
13216 if (!(result_mail_id_list = (int *)malloc(sizeof(int) * result_mail_id_count))) {
13217 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
13218 error = EMAIL_ERROR_OUT_OF_MEMORY;
13219 sqlite3_free_table(result);
13223 memset(result_mail_id_list, 0x00, sizeof(int) * result_mail_id_count);
13227 for (counter = 0; counter < result_mail_id_count; counter++)
13228 _get_table_field_data_int(result, result_mail_id_list + counter, col_index++);
13233 EM_DEBUG_LOG("finish off [%d]", ret);
13236 sqlite3_free_table(result);
13239 *mail_id_list = result_mail_id_list;
13240 *mail_id_count = result_mail_id_count;
13243 EM_SAFE_FREE(result_mail_id_list);
13245 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13248 if (err_code != NULL)
13251 EM_PROFILE_END(profile_emstorage_get_overflowed_mail_id_list);
13252 EM_DEBUG_FUNC_END("ret [%d]", ret);
13257 INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(int mail_id, int *thread_id, int *err_code)
13259 EM_DEBUG_FUNC_BEGIN("mail_id[%d], thread_id[%p], err_code[%p]", mail_id, thread_id, err_code);
13261 int rc = -1, ret = false;
13262 int err = EMAIL_ERROR_NONE;
13263 char sql_query_string[QUERY_SIZE] = {0, };
13265 int result_count = 0;
13267 if (mail_id == 0 || thread_id == NULL) {
13268 EM_DEBUG_EXCEPTION("Invalid Parameter");
13269 if (err_code != NULL)
13270 *err_code = EMAIL_ERROR_INVALID_PARAM;
13274 sqlite3 *local_db_handle = emstorage_get_db_connection();
13276 memset(sql_query_string, 0, QUERY_SIZE);
13277 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT thread_id FROM mail_tbl WHERE mail_id = %d", mail_id);
13279 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
13280 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
13281 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13282 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13284 if (!result_count) {
13285 EM_DEBUG_EXCEPTION("No mail found...");
13287 err= EMAIL_ERROR_MAIL_NOT_FOUND;
13288 /* sqlite3_free_table(result); */
13292 _get_table_field_data_int(result, thread_id, 1);
13294 sqlite3_free_table(result);
13302 if (err_code != NULL)
13305 EM_DEBUG_FUNC_END("ret [%d]", ret);
13310 INTERNAL_FUNC int emstorage_update_latest_thread_mail(int account_id, int thread_id, int latest_mail_id, int thread_item_count, int transaction, int *err_code)
13312 EM_DEBUG_FUNC_BEGIN("account_id [%d], thread_id[%d], latest_mail_id [%d], thread_item_count[%d], err_code[%p]", account_id, thread_id, latest_mail_id, thread_item_count, err_code);
13314 int rc = -1, ret = false;
13315 int err = EMAIL_ERROR_NONE;
13316 char sql_query_string[QUERY_SIZE] = {0, };
13318 int result_count = 0;
13320 if (thread_id == 0) {
13321 EM_DEBUG_EXCEPTION("Invalid Parameter");
13322 if (err_code != NULL)
13323 *err_code = EMAIL_ERROR_INVALID_PARAM;
13327 sqlite3 *local_db_handle = emstorage_get_db_connection();
13329 if (thread_item_count == 0 || latest_mail_id == 0) {
13330 memset(sql_query_string, 0, QUERY_SIZE);
13331 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id, count(*) FROM (SELECT account_id, mail_id, thread_id, mailbox_type FROM mail_tbl ORDER BY date_time) WHERE account_id = %d AND thread_id = %d AND mailbox_type NOT in (3,5,7)", account_id, thread_id);
13333 /* rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
13334 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
13335 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13336 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13337 EM_DEBUG_LOG("result_count[%d]", result_count);
13338 if (result_count == 0) {
13339 EM_DEBUG_EXCEPTION("No mail found...");
13341 err= EMAIL_ERROR_MAIL_NOT_FOUND;
13342 sqlite3_free_table(result);
13346 _get_table_field_data_int(result, &latest_mail_id, 2);
13347 _get_table_field_data_int(result, &thread_item_count, 3);
13349 EM_DEBUG_LOG("latest_mail_id[%d]", latest_mail_id);
13350 EM_DEBUG_LOG("thread_item_count[%d]", thread_item_count);
13352 sqlite3_free_table(result);
13355 EMSTORAGE_START_WRITE_TRANSACTION(transaction, err);
13357 /* if (thread_item_count > 1) */
13359 memset(sql_query_string, 0, QUERY_SIZE);
13360 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);
13361 EM_DEBUG_LOG("query[%s]", sql_query_string);
13363 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13364 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13365 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13368 memset(sql_query_string, 0, QUERY_SIZE);
13369 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);
13370 EM_DEBUG_LOG("query[%s]", sql_query_string);
13372 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13373 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13374 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13380 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, err);
13384 if (err_code != NULL)
13387 EM_DEBUG_FUNC_END("ret [%d]", ret);
13392 INTERNAL_FUNC void emstorage_flush_db_cache()
13394 sqlite3_release_memory(-1);
13397 #ifdef __FEATURE_LOCAL_ACTIVITY__
13399 * emstorage_add_activity - Add Email Local activity during OFFLINE mode
13402 INTERNAL_FUNC int emstorage_add_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
13404 EM_DEBUG_FUNC_BEGIN();
13406 EM_DEBUG_LOG(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
13408 int rc = -1, ret = false;
13409 int error = EMAIL_ERROR_NONE;
13410 DB_STMT hStmt = NULL;
13411 char sql_query_string[8192] = { 0x00, };
13414 if (!local_activity) {
13415 EM_DEBUG_EXCEPTION(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
13416 if (err_code != NULL)
13417 *err_code = EMAIL_ERROR_INVALID_PARAM;
13421 sqlite3 *local_db_handle = emstorage_get_db_connection();
13422 memset(sql_query_string, 0x00 , sizeof(sql_query_string));
13423 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13425 SNPRINTF(sql_query_string, sizeof(sql_query_string), "INSERT INTO mail_local_activity_tbl VALUES (?, ?, ?, ?, ?, ?, ?)");
13427 EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ] ", local_activity->activity_id);
13428 EM_DEBUG_LOG(">>>>> MAIL ID [ %d ] ", local_activity->mail_id);
13429 EM_DEBUG_LOG(">>>>> ACCOUNT ID [ %d ] ", local_activity->account_id);
13430 EM_DEBUG_LOG(">>>>> ACTIVITY TYPE [ %d ] ", local_activity->activity_type);
13431 EM_DEBUG_LOG(">>>>> SERVER MAIL ID [ %s ] ", local_activity->server_mailid);
13432 EM_DEBUG_LOG(">>>>> SOURCE MAILBOX [ %s ] ", local_activity->src_mbox);
13433 EM_DEBUG_LOG(">>>>> DEST MAILBOX [ %s ] ", local_activity->dest_mbox);
13435 EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
13438 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
13439 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13440 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13442 EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
13447 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
13448 _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
13449 _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
13450 _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
13451 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->server_mailid, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
13452 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->src_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
13453 _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->dest_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
13456 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13458 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
13459 ("sqlite3_step fail:%d", rc));
13460 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13461 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
13467 if (hStmt != NULL) {
13468 rc = sqlite3_finalize(hStmt);
13469 if (rc != SQLITE_OK) {
13470 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
13472 error = EMAIL_ERROR_DB_FAILURE;
13476 EM_DEBUG_LOG(" >>>>>>>>>> hStmt is NULL!!!");
13479 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13483 if (err_code != NULL)
13486 EM_DEBUG_FUNC_END("ret [%d]", ret);
13491 * emstorage_get_activity - Get the Local activity Information
13495 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)
13497 EM_DEBUG_FUNC_BEGIN();
13499 int i = 0, count = 0, rc = -1, ret = false;
13500 int error = EMAIL_ERROR_NONE;
13501 emstorage_activity_tbl_t *p_activity_tbl = NULL;
13502 char sql_query_string[1024] = {0x00, };
13503 char **result = NULL;
13506 EM_IF_NULL_RETURN_VALUE(activity_list, false);
13507 EM_IF_NULL_RETURN_VALUE(select_num, false);
13510 if (!select_num || !activity_list || account_id <= 0 || activityid < 0) {
13511 EM_DEBUG_LOG(" select_num[%p], activity_list[%p] account_id [%d] activityid [%d] ", select_num, activity_list, account_id, activityid);
13512 if (err_code != NULL)
13513 *err_code = EMAIL_ERROR_INVALID_PARAM;
13517 EMSTORAGE_START_READ_TRANSACTION(transaction);
13519 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13521 if (activityid == ALL_ACTIVITIES) {
13522 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d order by activity_id", account_id);
13525 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);
13528 EM_DEBUG_LOG("Query [%s]", sql_query_string);
13532 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
13533 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13534 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13540 if (!(p_activity_tbl = (emstorage_activity_tbl_t*)em_malloc(sizeof(emstorage_activity_tbl_t) * count))) {
13541 EM_DEBUG_EXCEPTION(" em_malloc failed...");
13542 error = EMAIL_ERROR_OUT_OF_MEMORY;
13547 for (i = 0; i < count; i++) {
13548 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
13549 if (result[col_index])
13550 p_activity_tbl[i].activity_id = atoi(result[col_index++]);
13552 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
13553 if (result[col_index])
13554 p_activity_tbl[i].account_id = atoi(result[col_index++]);
13556 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
13557 if (result[col_index])
13558 p_activity_tbl[i].mail_id = atoi(result[col_index++]);
13560 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
13561 if (result[col_index])
13562 p_activity_tbl[i].activity_type = atoi(result[col_index++]);
13565 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
13566 if (result[col_index] && strlen(result[col_index])>0)
13567 p_activity_tbl[i].server_mailid = EM_SAFE_STRDUP(result[col_index++]);
13571 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
13572 if (result[col_index] && strlen(result[col_index])>0)
13573 p_activity_tbl[i].src_mbox = EM_SAFE_STRDUP(result[col_index++]);
13577 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
13578 if (result[col_index] && strlen(result[col_index])>0)
13579 p_activity_tbl[i].dest_mbox = EM_SAFE_STRDUP(result[col_index++]);
13586 sqlite3_free_table(result);
13594 *activity_list = p_activity_tbl;
13595 *select_num = count;
13596 EM_DEBUG_LOG(">>>> COUNT : %d >> ", count);
13598 else if (p_activity_tbl != NULL) {
13599 emstorage_free_local_activity(&p_activity_tbl, count, NULL);
13602 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13603 if (err_code != NULL)
13606 EM_DEBUG_FUNC_END("ret [%d]", ret);
13611 INTERNAL_FUNC int emstorage_get_next_activity_id(int *activity_id, int *err_code)
13614 EM_DEBUG_FUNC_BEGIN();
13617 int err = EMAIL_ERROR_NONE;
13620 char **result = NULL;
13622 if (NULL == activity_id)
13624 EM_DEBUG_EXCEPTION(" activity_id[%p]", activity_id);
13626 err = EMAIL_ERROR_INVALID_PARAM;
13630 /* increase unique id */
13632 sql = "SELECT max(rowid) FROM mail_local_activity_tbl;";
13634 /* 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); */
13635 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13636 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13638 if (NULL==result[1])
13641 rc = atoi(result[1])+1;
13646 sqlite3_free_table(result);
13652 if (NULL != err_code) {
13656 EM_DEBUG_FUNC_END("ret [%d]", ret);
13661 INTERNAL_FUNC int emstorage_get_activity_id_list(int account_id, int ** activity_id_list, int *activity_id_count, int lowest_activity_type, int highest_activity_type, int transaction, int *err_code)
13664 EM_DEBUG_FUNC_BEGIN();
13666 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);
13668 if (account_id <= 0|| NULL == activity_id_list || NULL == activity_id_count ||lowest_activity_type <=0 || highest_activity_type <= 0) {
13669 if (err_code != NULL)
13670 *err_code = EMAIL_ERROR_INVALID_PARAM;
13675 int error = EMAIL_ERROR_NONE;
13676 int i = 0, rc = -1, count = 0;
13677 char sql_query_string[1024] = {0x00, };
13678 int *activity_ids = NULL;
13680 char **result = NULL;
13682 EMSTORAGE_START_READ_TRANSACTION(transaction);
13684 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13686 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);
13688 EM_DEBUG_LOG(" Query [%s]", sql_query_string);
13691 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
13692 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13693 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13697 EM_DEBUG_LOG(" Activity COUNT : %d ... ", count);
13699 if (NULL == (activity_ids = (int *)em_malloc(sizeof(int) * count))) {
13700 EM_DEBUG_EXCEPTION(" em_malloc failed...");
13701 error = EMAIL_ERROR_OUT_OF_MEMORY;
13705 for (i = 0; i < count; i++) {
13706 activity_ids[i] = atoi(result[col_index]);
13708 EM_DEBUG_LOG("activity_id %d", activity_ids[i]);
13717 *activity_id_count = count;
13718 *activity_id_list = activity_ids;
13721 else if (activity_ids != NULL) /* Prevent defect - 216566 */
13722 EM_SAFE_FREE(activity_ids);
13725 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13726 if (err_code != NULL) {
13730 EM_DEBUG_FUNC_END("ret [%d]", ret);
13734 INTERNAL_FUNC int emstorage_free_activity_id_list(int *activity_id_list, int *error_code)
13736 EM_DEBUG_FUNC_BEGIN();
13738 int error = EMAIL_ERROR_NONE;
13741 EM_DEBUG_LOG(" activity_id_list [%p]", activity_id_list);
13743 if (NULL == activity_id_list) {
13744 error = EMAIL_ERROR_INVALID_PARAM;
13748 free(activity_id_list);
13749 activity_id_list = NULL;
13757 if (NULL != error_code) {
13758 *error_code = error;
13761 EM_DEBUG_FUNC_END("ret [%d]", ret);
13766 * emstorage_delete_local_activity - Deletes the Local acitivity Generated based on activity_type
13767 * or based on server mail id
13770 INTERNAL_FUNC int emstorage_delete_local_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
13772 EM_DEBUG_FUNC_BEGIN();
13775 EM_DEBUG_LOG(" local_activity[%p] ", local_activity);
13777 if (!local_activity) {
13778 EM_DEBUG_EXCEPTION(" local_activity[%p] ", local_activity);
13779 if (err_code != NULL)
13780 *err_code = EMAIL_ERROR_INVALID_PARAM;
13784 int rc = -1, ret = false; /* Prevent_FIX */
13785 int err = EMAIL_ERROR_NONE;
13787 int query_where = 0;
13788 char sql_query_string[8192] = { 0x00, };
13789 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
13791 memset(sql_query_string, 0x00, sizeof(sql_query_string));
13793 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_local_activity_tbl ");
13795 EM_DEBUG_LOG(">>> Query [ %s ] ", sql_query_string);
13797 if (local_activity->account_id) {
13798 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1),
13799 " WHERE account_id = %d ", local_activity->account_id);
13804 if (local_activity->server_mailid) {
13805 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1),
13806 " %s %s server_mailid = '%s' ", query_where? "": "WHERE", query_and? "AND":"", local_activity->server_mailid);
13812 if (local_activity->mail_id) {
13813 EM_DEBUG_LOG(">>>> MAIL ID [ %d ] , ACTIVITY TYPE [%d ]", local_activity->mail_id, local_activity->activity_type);
13815 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1),
13816 " %s %s mail_id = %d ", query_where? "": "WHERE", query_and? "AND":"", local_activity->mail_id);
13823 if (local_activity->activity_type > 0) {
13824 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1),
13825 " %s %s activity_type = %d ", query_where? "": "WHERE", query_and? "AND" : "" , local_activity->activity_type);
13828 if (local_activity->activity_id > 0) {
13829 SNPRINTF(sql_query_string + strlen(sql_query_string), sizeof(sql_query_string)-(strlen(sql_query_string)+1),
13830 " %s %s activity_id = %d ", query_where? "": "WHERE", query_and? "AND" : "" , local_activity->activity_id);
13834 EM_DEBUG_LOG(">>>>> Query [ %s ] ", sql_query_string);
13836 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
13837 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13838 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13840 rc = sqlite3_changes(local_db_handle);
13842 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
13843 err = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13849 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
13850 if (err_code != NULL)
13852 EM_DEBUG_FUNC_END("ret [%d]", ret);
13857 * emstorage_free_local_activity - Free the Local Activity data
13859 INTERNAL_FUNC int emstorage_free_local_activity(emstorage_activity_tbl_t **local_activity_list, int count, int *err_code)
13861 EM_DEBUG_FUNC_BEGIN();
13863 EM_DEBUG_LOG(" local_activity_list[%p], count[%d], err_code[%p]", local_activity_list, count, err_code);
13866 int error = EMAIL_ERROR_INVALID_PARAM;
13869 if (!local_activity_list || !*local_activity_list) {
13870 EM_DEBUG_EXCEPTION(" local_activity_list[%p], count[%d]", local_activity_list, count);
13872 error = EMAIL_ERROR_INVALID_PARAM;
13876 emstorage_activity_tbl_t* p = *local_activity_list;
13879 for (; i < count; i++) {
13880 EM_SAFE_FREE(p[i].dest_mbox);
13881 EM_SAFE_FREE(p[i].src_mbox);
13882 EM_SAFE_FREE(p[i].server_mailid);
13885 free(p); *local_activity_list = NULL;
13892 if (err_code != NULL)
13895 EM_DEBUG_FUNC_END("ret [%d]", ret);
13899 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
13901 static char *_mail_field_name_list[] = {
13909 "server_mail_status",
13910 "server_mailbox_name",
13913 "full_address_from",
13916 "full_address_bcc",
13917 "body_download_status",
13921 "flags_seen_field",
13922 "flags_deleted_field",
13923 "flags_flagged_field",
13924 "flags_answered_field",
13925 "flags_recent_field",
13926 "flags_draft_field",
13927 "flags_forwarded_field",
13933 "attachment_count",
13934 "inline_content_count",
13936 "thread_item_count",
13938 "meeting_request_status",
13944 static char*_get_mail_field_name_by_attribute_type(email_mail_attribute_type input_attribute_type)
13946 EM_DEBUG_FUNC_BEGIN("input_attribute_type [%d]", input_attribute_type);
13948 if(input_attribute_type > EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS || input_attribute_type < 0) {
13949 EM_DEBUG_EXCEPTION("Invalid input_attribute_type [%d]", input_attribute_type);
13953 EM_DEBUG_FUNC_END("return [%s]", _mail_field_name_list[input_attribute_type]);
13954 return _mail_field_name_list[input_attribute_type];
13957 static int _get_attribute_type_by_mail_field_name(char *input_mail_field_name, email_mail_attribute_type *output_mail_attribute_type)
13959 EM_DEBUG_FUNC_BEGIN("input_mail_field_name [%p], output_mail_attribute_type [%p]", input_mail_field_name, output_mail_attribute_type);
13961 int err = EMAIL_ERROR_DATA_NOT_FOUND;
13963 if(!input_mail_field_name || !output_mail_attribute_type) {
13964 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
13965 return EMAIL_ERROR_INVALID_PARAM;
13968 for (i = 0; i < EMAIL_MAIL_ATTRIBUTE_END; i++) {
13969 if (EM_SAFE_STRCMP(input_mail_field_name, _mail_field_name_list[i]) == 0) {
13970 *output_mail_attribute_type = i;
13971 err = EMAIL_ERROR_NONE;
13976 EM_DEBUG_FUNC_END("found mail attribute type [%d]", (int)*output_mail_attribute_type);
13980 static int _get_key_value_string_for_list_filter_rule(email_list_filter_rule_t *input_list_filter_rule,char **output_key_value_string)
13982 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_key_value_string [%p]", input_list_filter_rule, output_key_value_string);
13984 int ret = EMAIL_ERROR_NONE;
13985 char key_value_string[QUERY_SIZE] = { 0, };
13986 char *temp_key_value_1 = NULL;
13987 char *temp_key_value_2 = NULL;
13989 if(input_list_filter_rule == NULL || output_key_value_string == NULL) {
13990 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
13991 return EMAIL_ERROR_INVALID_PARAM;
13994 switch(input_list_filter_rule->target_attribute) {
13995 case EMAIL_MAIL_ATTRIBUTE_MAIL_ID :
13996 case EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID :
13997 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID :
13998 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE :
13999 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_STATUS :
14000 case EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS :
14001 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN :
14002 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML :
14003 case EMAIL_MAIL_ATTRIBUTE_FILE_SIZE :
14004 case EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD :
14005 case EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD :
14006 case EMAIL_MAIL_ATTRIBUTE_FLAGS_FLAGGED_FIELD :
14007 case EMAIL_MAIL_ATTRIBUTE_FLAGS_ANSWERED_FIELD :
14008 case EMAIL_MAIL_ATTRIBUTE_FLAGS_RECENT_FIELD :
14009 case EMAIL_MAIL_ATTRIBUTE_FLAGS_DRAFT_FIELD :
14010 case EMAIL_MAIL_ATTRIBUTE_FLAGS_FORWARDED_FIELD :
14011 case EMAIL_MAIL_ATTRIBUTE_DRM_STATUS :
14012 case EMAIL_MAIL_ATTRIBUTE_PRIORITY :
14013 case EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS :
14014 case EMAIL_MAIL_ATTRIBUTE_LOCK_STATUS :
14015 case EMAIL_MAIL_ATTRIBUTE_REPORT_STATUS :
14016 case EMAIL_MAIL_ATTRIBUTE_ATTACHMENT_COUNT :
14017 case EMAIL_MAIL_ATTRIBUTE_INLINE_CONTENT_COUNT :
14018 case EMAIL_MAIL_ATTRIBUTE_THREAD_ID :
14019 case EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT :
14020 case EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS :
14021 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_CLASS :
14022 case EMAIL_MAIL_ATTRIBUTE_DIGEST_TYPE :
14023 case EMAIL_MAIL_ATTRIBUTE_SMIME_TYPE :
14024 SNPRINTF(key_value_string, QUERY_SIZE, "%d", input_list_filter_rule->key_value.integer_type_value);
14027 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME :
14028 case EMAIL_MAIL_ATTRIBUTE_SUBJECT :
14029 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME :
14030 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID :
14031 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID :
14032 case EMAIL_MAIL_ATTRIBUTE_FROM :
14033 case EMAIL_MAIL_ATTRIBUTE_TO :
14034 case EMAIL_MAIL_ATTRIBUTE_CC :
14035 case EMAIL_MAIL_ATTRIBUTE_BCC :
14036 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT :
14037 if(input_list_filter_rule->key_value.string_type_value == NULL) {
14038 EM_DEBUG_EXCEPTION("Invalid string_type_value [%p]", input_list_filter_rule->key_value.string_type_value);
14039 ret = EMAIL_ERROR_INVALID_PARAM;
14043 temp_key_value_1 = input_list_filter_rule->key_value.string_type_value;
14045 temp_key_value_2 = em_replace_all_string(temp_key_value_1, "_", "\\_");
14046 temp_key_value_1 = em_replace_all_string(temp_key_value_2, "%", "\\%");
14048 if(input_list_filter_rule->rule_type == EMAIL_LIST_FILTER_RULE_INCLUDE)
14049 SNPRINTF(key_value_string, QUERY_SIZE, "\'%%%s%%\'", temp_key_value_1);
14051 SNPRINTF(key_value_string, QUERY_SIZE, "\'%s\'", temp_key_value_1);
14054 case EMAIL_MAIL_ATTRIBUTE_DATE_TIME :
14055 SNPRINTF(key_value_string, QUERY_SIZE, "%d", (int)input_list_filter_rule->key_value.datetime_type_value);
14059 ret = EMAIL_ERROR_INVALID_PARAM;
14060 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
14064 if(ret == EMAIL_ERROR_NONE && strlen(key_value_string) > 0) {
14065 *output_key_value_string = strdup(key_value_string);
14070 EM_SAFE_FREE(temp_key_value_1);
14071 EM_SAFE_FREE(temp_key_value_2);
14073 EM_DEBUG_FUNC_END("ret [%d]", ret);
14077 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14078 static int _get_cert_password_file_name(int index, char *cert_password_file_name)
14080 EM_DEBUG_FUNC_BEGIN("index : [%d]", index);
14082 if (index <= 0 || !cert_password_file_name) {
14083 EM_DEBUG_EXCEPTION("Invalid parameter");
14084 return EMAIL_ERROR_INVALID_PARAM;
14087 sprintf(cert_password_file_name, ".email_cert_%d", index);
14089 EM_DEBUG_FUNC_END();
14090 return EMAIL_ERROR_NONE;
14094 static int _make_filter_rule_string(email_list_filter_rule_t *input_list_filter_rule, char **output_string)
14096 EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
14098 int ret = EMAIL_ERROR_NONE;
14100 int length_field_name = 0;
14101 int length_value = 0;
14102 char result_rule_string[QUERY_SIZE] = { 0 , };
14103 char *mod_field_name_string = NULL;
14104 char *mod_value_string = NULL;
14105 char *temp_field_name_string = NULL;
14106 char *temp_key_value_string = NULL;
14108 if(input_list_filter_rule == NULL || output_string == NULL) {
14109 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14110 return EMAIL_ERROR_INVALID_PARAM;
14113 temp_field_name_string = _get_mail_field_name_by_attribute_type(input_list_filter_rule->target_attribute);
14115 if(temp_field_name_string == NULL) {
14116 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
14117 return EMAIL_ERROR_INVALID_PARAM;
14120 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) {
14121 EM_DEBUG_EXCEPTION("_get_key_value_string_for_list_filter_rule failed");
14122 return EMAIL_ERROR_INVALID_PARAM;
14125 length_field_name = strlen(temp_field_name_string);
14126 length_value = strlen(temp_key_value_string);
14128 switch(input_list_filter_rule->target_attribute) {
14129 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME :
14130 case EMAIL_MAIL_ATTRIBUTE_SUBJECT :
14131 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME :
14132 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID :
14133 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID :
14134 case EMAIL_MAIL_ATTRIBUTE_FROM :
14135 case EMAIL_MAIL_ATTRIBUTE_TO :
14136 case EMAIL_MAIL_ATTRIBUTE_CC :
14137 case EMAIL_MAIL_ATTRIBUTE_BCC :
14138 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT :
14146 if(is_alpha == 1 && input_list_filter_rule->case_sensitivity == false) {
14147 length_field_name += strlen("UPPER() ");
14148 length_value += strlen("UPPER() ");
14149 mod_field_name_string = em_malloc(sizeof(char) * length_field_name);
14150 mod_value_string = em_malloc(sizeof(char) * length_value);
14151 SNPRINTF(mod_field_name_string, length_field_name, "UPPER(%s)", temp_field_name_string);
14152 SNPRINTF(mod_value_string, length_value, "UPPER(%s)", temp_key_value_string);
14153 EM_SAFE_FREE(temp_key_value_string);
14156 mod_field_name_string = strdup(temp_field_name_string);
14157 mod_value_string = temp_key_value_string;
14160 switch (input_list_filter_rule->rule_type) {
14161 case EMAIL_LIST_FILTER_RULE_EQUAL :
14162 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = %s ", mod_field_name_string, mod_value_string);
14164 case EMAIL_LIST_FILTER_RULE_NOT_EQUAL :
14165 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = %s ", mod_field_name_string, mod_value_string);
14167 case EMAIL_LIST_FILTER_RULE_LESS_THAN :
14168 SNPRINTF(result_rule_string, QUERY_SIZE, "%s < %s ", mod_field_name_string, mod_value_string);
14170 case EMAIL_LIST_FILTER_RULE_GREATER_THAN :
14171 SNPRINTF(result_rule_string, QUERY_SIZE, "%s > %s ", mod_field_name_string, mod_value_string);
14173 case EMAIL_LIST_FILTER_RULE_LESS_THAN_OR_EQUAL :
14174 SNPRINTF(result_rule_string, QUERY_SIZE, "%s <= %s ", mod_field_name_string, mod_value_string);
14176 case EMAIL_LIST_FILTER_RULE_GREATER_THAN_OR_EQUAL :
14177 SNPRINTF(result_rule_string, QUERY_SIZE, "%s >= %s ", mod_field_name_string, mod_value_string);
14179 case EMAIL_LIST_FILTER_RULE_INCLUDE :
14180 SNPRINTF(result_rule_string, QUERY_SIZE, "%s LIKE %s ", mod_field_name_string, mod_value_string);
14182 case EMAIL_LIST_FILTER_RULE_IN :
14183 SNPRINTF(result_rule_string, QUERY_SIZE, "%s IN (%s) ", mod_field_name_string, mod_value_string);
14185 case EMAIL_LIST_FILTER_RULE_NOT_IN :
14186 SNPRINTF(result_rule_string, QUERY_SIZE, "%s NOT IN (%s) ", mod_field_name_string, mod_value_string);
14189 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
14190 ret = EMAIL_ERROR_INVALID_PARAM;
14194 *output_string = strdup(result_rule_string);
14197 EM_SAFE_FREE(mod_field_name_string);
14198 EM_SAFE_FREE(mod_value_string);
14200 EM_DEBUG_FUNC_END("ret [%d]", ret);
14204 static int _make_order_rule_string(email_list_sorting_rule_t *input_sorting_rule, char **output_string) {
14205 EM_DEBUG_FUNC_BEGIN("input_sorting_rule [%p], output_string [%p]", input_sorting_rule, output_string);
14207 char result_rule_string[QUERY_SIZE] = { 0 , };
14208 int ret = EMAIL_ERROR_NONE;
14210 if(input_sorting_rule->force_boolean_check) {
14211 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = 0 ", _get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
14214 EM_SAFE_STRCPY(result_rule_string, _get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
14216 switch (input_sorting_rule->sort_order) {
14217 case EMAIL_SORT_ORDER_ASCEND :
14218 EM_SAFE_STRCAT(result_rule_string, " ASC ");
14221 case EMAIL_SORT_ORDER_DESCEND :
14222 EM_SAFE_STRCAT(result_rule_string, " DESC ");
14226 EM_DEBUG_EXCEPTION("Invalid sort_order [%d]", input_sorting_rule->sort_order);
14227 ret = EMAIL_ERROR_INVALID_PARAM;
14231 *output_string = strdup(result_rule_string);
14234 EM_DEBUG_FUNC_END("ret [%d]", ret);
14238 INTERNAL_FUNC int emstorage_write_conditional_clause_for_getting_mail_list(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)
14240 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);
14241 int ret = EMAIL_ERROR_NONE;
14243 char conditional_clause_string[QUERY_SIZE] = {0, };
14244 char *result_string_for_a_item = NULL;
14246 if ( (input_filter_count > 0 && !input_filter_list) || (input_sorting_rule_count > 0 && !input_sorting_rule_list) || output_conditional_clause == NULL) {
14247 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14248 return EMAIL_ERROR_INVALID_PARAM;
14251 if(input_filter_count > 0) {
14252 strcpy(conditional_clause_string, " WHERE ");
14254 for ( i = 0; i < input_filter_count; i++) {
14255 switch (input_filter_list[i].list_filter_item_type) {
14256 case EMAIL_LIST_FILTER_ITEM_RULE :
14257 EM_DEBUG_LOG("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE", i);
14258 _make_filter_rule_string(&(input_filter_list[i].list_filter_item.rule), &result_string_for_a_item);
14261 case EMAIL_LIST_FILTER_ITEM_OPERATOR :
14262 EM_DEBUG_LOG("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_OPERATOR", i);
14263 switch(input_filter_list[i].list_filter_item.operator_type) {
14264 case EMAIL_LIST_FILTER_OPERATOR_AND :
14265 result_string_for_a_item = strdup("AND ");
14267 case EMAIL_LIST_FILTER_OPERATOR_OR :
14268 result_string_for_a_item = strdup("OR ");
14270 case EMAIL_LIST_FILTER_OPERATOR_LEFT_PARENTHESIS :
14271 result_string_for_a_item = strdup(" (");
14273 case EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS :
14274 result_string_for_a_item = strdup(") ");
14280 EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", input_filter_list[i].list_filter_item_type);
14281 ret = EMAIL_ERROR_INVALID_PARAM;
14285 if(result_string_for_a_item == NULL) {
14286 EM_DEBUG_EXCEPTION("result_string_for_a_item is null");
14287 ret = EMAIL_ERROR_INVALID_PARAM;
14291 if(strlen(conditional_clause_string) + strlen(result_string_for_a_item) >= QUERY_SIZE) {
14292 EM_DEBUG_EXCEPTION("Query is too long");
14293 ret = EMAIL_ERROR_DATA_TOO_LONG;
14296 strcat(conditional_clause_string, result_string_for_a_item);
14297 EM_SAFE_FREE(result_string_for_a_item);
14301 if(input_sorting_rule_count > 0) {
14302 strcat(conditional_clause_string, "ORDER BY ");
14304 for ( i = 0; i < input_sorting_rule_count; i++) {
14305 if( (ret = _make_order_rule_string(&input_sorting_rule_list[i], &result_string_for_a_item)) != EMAIL_ERROR_NONE) {
14306 EM_DEBUG_EXCEPTION("_make_order_rule_string failed. [%d]", ret);
14310 strcat(conditional_clause_string, ", ");
14311 strcat(conditional_clause_string, result_string_for_a_item);
14312 EM_SAFE_FREE(result_string_for_a_item);
14316 *output_conditional_clause = strdup(conditional_clause_string);
14319 EM_SAFE_FREE(result_string_for_a_item);
14321 EM_DEBUG_FUNC_END("ret [%d]", ret);
14325 INTERNAL_FUNC int emstorage_free_list_filter(email_list_filter_t **input_filter_list, int input_filter_count)
14327 EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d]", input_filter_list, input_filter_count);
14328 int err = EMAIL_ERROR_NONE;
14330 email_list_filter_t *temp_filter_list = NULL;
14332 EM_IF_NULL_RETURN_VALUE(input_filter_list, EMAIL_ERROR_INVALID_PARAM);
14334 for ( i = 0; i < input_filter_count; i++) {
14335 temp_filter_list = (*input_filter_list) + i;
14336 if(temp_filter_list && temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE) {
14337 switch(temp_filter_list->list_filter_item.rule.target_attribute) {
14338 case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME :
14339 case EMAIL_MAIL_ATTRIBUTE_SUBJECT :
14340 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME :
14341 case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID :
14342 case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID :
14343 case EMAIL_MAIL_ATTRIBUTE_FROM :
14344 case EMAIL_MAIL_ATTRIBUTE_TO :
14345 case EMAIL_MAIL_ATTRIBUTE_CC :
14346 case EMAIL_MAIL_ATTRIBUTE_BCC :
14347 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN :
14348 case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML :
14349 case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT :
14350 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule.key_value.string_type_value);
14358 free(*input_filter_list);
14359 *input_filter_list = NULL;
14361 EM_DEBUG_FUNC_END("err [%d]", err);
14365 INTERNAL_FUNC int emstorage_add_certificate(emstorage_certificate_tbl_t *certificate, int transaction, int *err_code)
14367 EM_DEBUG_FUNC_BEGIN("certificate:[%p], transaction:[%d], err_code:[%p]", certificate, transaction, err_code);
14369 if (!certificate) {
14370 EM_DEBUG_EXCEPTION("certificate:[%p], transaction:[%d], err_code:[%p]", certificate, transaction, err_code);
14371 if (err_code != NULL)
14372 *err_code = EMAIL_ERROR_INVALID_PARAM;
14377 int rc = -1, ret = false;
14378 int error = EMAIL_ERROR_NONE;
14379 DB_STMT hStmt = NULL;
14380 char sql_query_string[QUERY_SIZE] = {0, };
14381 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14382 char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
14384 sqlite3 *local_db_handle = emstorage_get_db_connection();
14386 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
14388 char *sql = "SELECT max(rowid) FROM mail_certificate_tbl;";
14389 char **result = NULL;
14391 /* rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
14392 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
14393 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14394 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
14396 if (NULL==result[1]) rc = 1;
14397 else rc = atoi(result[1])+1;
14398 sqlite3_free_table(result);
14400 certificate->certificate_id = rc;
14401 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14402 if ((error = _get_cert_password_file_name(certificate->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
14403 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
14407 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14408 "INSERT INTO mail_certificate_tbl VALUES "
14410 " ? " /* Index of certificate */
14411 " , ? " /* Select the account */
14412 " , ? " /* Year of issue */
14413 " , ? " /* Month of issue */
14414 " , ? " /* Day of issue */
14415 " , ? " /* Year of expiration */
14416 " , ? " /* Month of expiration */
14417 " , ? " /* Day of expiration */
14418 " , ? " /* Organization of issue */
14419 " , ? " /* Email address */
14420 " , ? " /* Subject of certificate */
14421 " , ? " /* Name of saved certificate */
14425 /* rc = sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL); */
14426 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
14427 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14428 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14430 EM_DEBUG_LOG(">>>> SQL STMT [ %s ] ", sql_query_string);
14431 _bind_stmt_field_data_int(hStmt, CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->certificate_id);
14432 _bind_stmt_field_data_int(hStmt, ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_year);
14433 _bind_stmt_field_data_int(hStmt, ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_month);
14434 _bind_stmt_field_data_int(hStmt, ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_day);
14435 _bind_stmt_field_data_int(hStmt, EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_year);
14436 _bind_stmt_field_data_int(hStmt, EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_month);
14437 _bind_stmt_field_data_int(hStmt, EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_day);
14438 _bind_stmt_field_data_string(hStmt, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_organization_name, 0, ISSUE_ORGANIZATION_LEN_IN_MAIL_CERTIFICATE_TBL);
14439 _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->email_address, 0, EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL);
14440 _bind_stmt_field_data_string(hStmt, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->subject_str, 0, SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL);
14441 _bind_stmt_field_data_string(hStmt, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->filepath, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
14442 /* rc = sqlite3_step(hStmt); */
14443 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14445 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
14446 ("sqlite3_step fail:%d", rc));
14447 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14448 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
14449 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14450 if (ssm_write_buffer(certificate->password, strlen(certificate->password), cert_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
14451 EM_DEBUG_EXCEPTION("ssm_write_buffer failed - Private certificate password : [%s]", cert_password_file_name);
14452 error = EMAIL_ERROR_SYSTEM_FAILURE;
14458 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
14460 if (!emstorage_notify_storage_event(NOTI_CERTIFICATE_ADD, certificate->certificate_id, 0, NULL, 0))
14461 EM_DEBUG_EXCEPTION("emstorage_notify_storage_event(NOTI_CERTIFICATE_ADD] : Notification failed");
14465 if (hStmt != NULL) {
14466 rc = sqlite3_finalize(hStmt);
14467 if (rc != SQLITE_OK) {
14468 EM_DEBUG_LOG("sqlite3_finalize failed - %d", rc);
14469 error = EMAIL_ERROR_DB_FAILURE;
14473 EM_DEBUG_LOG("hStmt is NULL!!!");
14475 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
14478 if (err_code != NULL)
14481 EM_DEBUG_FUNC_END("ret [%d]", ret);
14485 INTERNAL_FUNC int emstorage_free_certificate(emstorage_certificate_tbl_t **certificate_list, int count, int *err_code)
14487 EM_DEBUG_FUNC_BEGIN("certificate_list[%p], count[%d], err_code[%p]", certificate_list, count, err_code);
14490 int error = EMAIL_ERROR_NONE;
14493 if (!certificate_list || !*certificate_list) {
14494 EM_DEBUG_EXCEPTION("certificate_list[%p], count[%d]", certificate_list, count);
14495 error = EMAIL_ERROR_INVALID_PARAM;
14499 emstorage_certificate_tbl_t *p = *certificate_list;
14502 for (; i < count; i++) {
14503 EM_SAFE_FREE(p[i].issue_organization_name);
14504 EM_SAFE_FREE(p[i].email_address);
14505 EM_SAFE_FREE(p[i].subject_str);
14506 EM_SAFE_FREE(p[i].filepath);
14507 EM_SAFE_FREE(p[i].password);
14511 *certificate_list = NULL;
14517 if (err_code != NULL)
14520 EM_DEBUG_FUNC_END("ret [%d]", ret);
14524 INTERNAL_FUNC int emstorage_get_certificate_list(int *select_num, emstorage_certificate_tbl_t **certificate_list, int transaction, int with_password, int *err_code)
14526 EM_DEBUG_FUNC_BEGIN();
14528 int i = 0, count = 0, rc = -1, ret = false;
14529 int error = EMAIL_ERROR_NONE;
14530 emstorage_certificate_tbl_t *p_data_tbl = NULL;
14532 DB_STMT hStmt = NULL;
14534 if (!select_num || !certificate_list) {
14535 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, certificate_list);
14536 if (err_code != NULL)
14537 *err_code = EMAIL_ERROR_INVALID_PARAM;
14541 sqlite3 *local_db_handle = emstorage_get_db_connection();
14542 EMSTORAGE_START_READ_TRANSACTION(transaction);
14544 char sql_query_string[QUERY_SIZE] = {0, };
14545 char *sql = "SELECT count(*) FROM mail_certificate_tbl;";
14548 /* rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
14549 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
14550 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14551 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
14553 count = atoi(result[1]);
14554 sqlite3_free_table(result);
14557 EM_DEBUG_EXCEPTION("no account found...");
14558 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
14562 EM_DEBUG_LOG("count = %d", rc);
14563 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_certificate_tbl ORDER BY account_id");
14565 /* rc = sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL); */
14566 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
14568 EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
14569 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14570 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14572 /* rc = sqlite3_step(hStmt); */
14573 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14574 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14575 ("sqlite3_step fail:%d", rc));
14576 if (rc == SQLITE_DONE) {
14577 EM_DEBUG_EXCEPTION("no account found...");
14579 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
14585 if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t) * count))) {
14586 EM_DEBUG_EXCEPTION("malloc failed...");
14587 error = EMAIL_ERROR_OUT_OF_MEMORY;
14590 memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t) * count);
14591 for (i = 0; i < count; i++) {
14592 /* get recordset */
14593 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
14594 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
14595 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
14596 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
14597 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
14598 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
14599 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
14600 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
14601 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
14602 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
14603 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
14604 if (with_password == true) {
14605 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14606 /* get password from the secure storage */
14607 char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
14609 EM_SAFE_FREE(p_data_tbl[i].password);
14611 /* get password file name */
14612 if ((error = _get_cert_password_file_name(p_data_tbl[i].certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
14613 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
14617 /* read password from secure storage */
14618 if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl[i].password))) < 0) {
14619 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage() failed...");
14622 EM_DEBUG_LOG("recv_password_file_name[%s], password[%s]", cert_password_file_name, p_data_tbl[i].password);
14626 /* rc = sqlite3_step(hStmt); */
14627 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14628 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i, rc);
14629 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14630 ("sqlite3_step fail:%d", rc));
14637 *certificate_list = p_data_tbl;
14638 *select_num = count;
14639 EM_DEBUG_LOG("COUNT : %d", count);
14641 else if (p_data_tbl != NULL)
14642 emstorage_free_certificate(&p_data_tbl, count, NULL);
14643 if (hStmt != NULL) {
14644 EM_DEBUG_LOG("Before sqlite3_finalize hStmt = %p", hStmt);
14646 rc = sqlite3_finalize(hStmt);
14648 if (rc != SQLITE_OK) {
14649 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
14650 error = EMAIL_ERROR_DB_FAILURE;
14654 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14657 if (err_code != NULL)
14660 EM_DEBUG_FUNC_END("ret [%d]", ret);
14664 INTERNAL_FUNC int emstorage_get_certificate_by_email_address(char *email_address, emstorage_certificate_tbl_t **certificate, int transaction, int with_password, int *err_code)
14666 EM_DEBUG_FUNC_BEGIN("email_address[%s], certificate[%p], transaction[%d], err_code[%p]", email_address, certificate, transaction, err_code);
14668 if (!certificate) {
14669 EM_DEBUG_EXCEPTION("email_address[%s], certificate[%p]", email_address, certificate);
14670 if (err_code != NULL)
14671 *err_code = EMAIL_ERROR_INVALID_PARAM;
14676 int error = EMAIL_ERROR_NONE;
14677 emstorage_certificate_tbl_t *p_data_tbl = NULL;
14678 DB_STMT hStmt = NULL;
14679 char sql_query_string[QUERY_SIZE] = {0, };
14682 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14683 char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
14685 sqlite3 *local_db_handle = emstorage_get_db_connection();
14686 EMSTORAGE_START_READ_TRANSACTION(transaction);
14688 /* Make query string */
14689 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
14690 sql_len = strlen(sql_query_string);
14692 /* dummy value, FROM WHERE clause */
14693 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "* FROM mail_certificate_tbl WHERE email_address = '%s'", email_address);
14696 EM_DEBUG_LOG("query = [%s]", sql_query_string);
14698 /* execute a sql and count rows */
14699 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
14700 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14701 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14703 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14704 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14705 ("sqlite3_step fail:%d", rc));
14707 if (rc == SQLITE_DONE) {
14708 EM_DEBUG_EXCEPTION("no matched certificate found...");
14709 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
14713 /* Assign query result to structure */
14714 if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t)))) {
14715 EM_DEBUG_EXCEPTION("malloc failed...");
14716 error = EMAIL_ERROR_OUT_OF_MEMORY;
14720 memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t));
14721 _get_stmt_field_data_int(hStmt, &(p_data_tbl->certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
14722 _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
14723 _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
14724 _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
14725 _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
14726 _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
14727 _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
14728 _get_stmt_field_data_string(hStmt, &(p_data_tbl->issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
14729 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
14730 _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
14731 _get_stmt_field_data_string(hStmt, &(p_data_tbl->filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
14733 if (with_password) {
14734 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14735 /* get password file name */
14736 if ((error = _get_cert_password_file_name(p_data_tbl->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
14737 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
14741 /* read password from secure storage */
14742 if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl->password))) != EMAIL_ERROR_NONE) {
14743 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
14746 EM_DEBUG_LOG("cert_password_file_name[%s], password[%s]", cert_password_file_name, p_data_tbl->password);
14753 *certificate = p_data_tbl;
14756 emstorage_free_certificate((emstorage_certificate_tbl_t **)&p_data_tbl, 1, NULL);
14758 if (hStmt != NULL) {
14759 rc = sqlite3_finalize(hStmt);
14760 if (rc != SQLITE_OK) {
14761 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
14762 error = EMAIL_ERROR_DB_FAILURE;
14766 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14769 if (err_code != NULL)
14772 EM_DEBUG_FUNC_END("ret [%d]", ret);
14776 INTERNAL_FUNC int emstorage_get_certificate_by_index(int index, emstorage_certificate_tbl_t **certificate, int transaction, int with_password, int *err_code)
14778 EM_DEBUG_FUNC_BEGIN("index[%d], certificate[%p], transaction[%d], err_code[%p]", index, certificate, transaction, err_code);
14780 if (!certificate) {
14781 EM_DEBUG_EXCEPTION("index[%d], account[%p]", index, certificate);
14782 if (err_code != NULL)
14783 *err_code = EMAIL_ERROR_INVALID_PARAM;
14788 int error = EMAIL_ERROR_NONE;
14789 emstorage_certificate_tbl_t *p_data_tbl = NULL;
14790 DB_STMT hStmt = NULL;
14791 char sql_query_string[QUERY_SIZE] = {0, };
14794 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14795 char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
14797 sqlite3 *local_db_handle = emstorage_get_db_connection();
14798 EMSTORAGE_START_READ_TRANSACTION(transaction);
14800 /* Make query string */
14801 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
14802 sql_len = strlen(sql_query_string);
14804 /* dummy value, FROM WHERE clause */
14805 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "* FROM mail_certificate_tbl WHERE certificate_id = %d", index);
14808 EM_DEBUG_LOG("query = [%s]", sql_query_string);
14810 /* execute a sql and count rows */
14811 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
14812 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14813 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14815 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14816 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14817 ("sqlite3_step fail:%d", rc));
14819 if (rc == SQLITE_DONE) {
14820 EM_DEBUG_EXCEPTION("no matched certificate found...");
14821 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
14825 /* Assign query result to structure */
14826 if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t)))) {
14827 EM_DEBUG_EXCEPTION("malloc failed...");
14828 error = EMAIL_ERROR_OUT_OF_MEMORY;
14832 memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t));
14834 _get_stmt_field_data_int(hStmt, &(p_data_tbl->certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
14835 _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
14836 _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
14837 _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
14838 _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
14839 _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
14840 _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
14841 _get_stmt_field_data_string(hStmt, &(p_data_tbl->issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
14842 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
14843 _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
14844 _get_stmt_field_data_string(hStmt, &(p_data_tbl->filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
14846 if (with_password) {
14847 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14848 /* get password file name */
14849 if ((error = _get_cert_password_file_name(p_data_tbl->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
14850 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
14854 /* read password from secure storage */
14855 if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl->password))) != EMAIL_ERROR_NONE) {
14856 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage() failed...");
14859 EM_DEBUG_LOG("cert_password_file_name[%s], password[%s]", cert_password_file_name, p_data_tbl->password);
14866 *certificate = p_data_tbl;
14869 emstorage_free_certificate((emstorage_certificate_tbl_t **)&p_data_tbl, 1, NULL);
14871 if (hStmt != NULL) {
14872 rc = sqlite3_finalize(hStmt);
14873 if (rc != SQLITE_OK) {
14874 EM_DEBUG_EXCEPTION("sqlite3_finalize failed - %d", rc);
14875 error = EMAIL_ERROR_DB_FAILURE;
14879 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14882 if (err_code != NULL)
14885 EM_DEBUG_FUNC_END("ret [%d]", ret);
14889 INTERNAL_FUNC int emstorage_delete_certificate(int certificate_id, int transaction, int *err_code)
14891 EM_DEBUG_FUNC_BEGIN("certificate_id[%d], transaction[%d], err_code[%p]", certificate_id, transaction, err_code);
14893 if (certificate_id < 1) {
14894 EM_DEBUG_EXCEPTION(" certificate_id[%d]", certificate_id);
14896 if (err_code != NULL)
14897 *err_code = EMAIL_ERROR_INVALID_PARAM;
14901 int rc = -1, ret = false;
14902 int error = EMAIL_ERROR_NONE;
14903 sqlite3 *local_db_handle = emstorage_get_db_connection();
14904 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
14906 /* TODO : delete password files - file names can be obtained from db or a rule that makes a name */
14907 DB_STMT hStmt = NULL;
14908 char sql_query_string[QUERY_SIZE] = {0, };
14909 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14910 char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
14911 /* get password file name */
14912 if ((error = _get_cert_password_file_name(certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
14913 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
14917 /* delete from db */
14918 memset(sql_query_string, 0x00, sizeof(sql_query_string));
14919 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_certificate_tbl WHERE certificate_id = %d", certificate_id);
14921 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, sql_query_string, NULL, NULL, NULL), rc);
14922 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
14923 ("sqlite3_exec fail:%d", rc));
14924 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14925 ("SQL(%s) exec fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14927 /* validate account existence */
14928 rc = sqlite3_changes(local_db_handle);
14930 EM_DEBUG_EXCEPTION(" no matched certificate found...");
14931 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
14934 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14935 /* delete from secure storage */
14936 if (ssm_delete_file(cert_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
14937 EM_DEBUG_EXCEPTION(" ssm_delete_file failed -cert password : file[%s]", cert_password_file_name);
14938 error = EMAIL_ERROR_SYSTEM_FAILURE;
14946 if (hStmt != NULL) {
14947 rc = sqlite3_finalize(hStmt);
14948 if (rc != SQLITE_OK) {
14949 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
14950 error = EMAIL_ERROR_DB_FAILURE;
14954 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
14957 if (err_code != NULL)
14960 EM_DEBUG_FUNC_END("ret [%d]", ret);
14964 INTERNAL_FUNC int emstorage_update_certificate(int certificate_id, emstorage_certificate_tbl_t *certificate, int transaction, int *err_code)
14966 EM_DEBUG_FUNC_BEGIN("certificate_id[%d], certificate[%p], transaction[%d], err_code[%p]", certificate_id, certificate, transaction, err_code);
14968 if (certificate_id < 1) {
14969 EM_DEBUG_EXCEPTION(" certificate_id[%d]", certificate_id);
14971 if (err_code != NULL)
14972 *err_code = EMAIL_ERROR_INVALID_PARAM;
14976 int error = EMAIL_ERROR_NONE;
14977 int rc, ret = false;
14979 DB_STMT hStmt = NULL;
14980 char sql_query_string[QUERY_SIZE] = {0, };
14981 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
14982 char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
14984 sqlite3 *local_db_handle = emstorage_get_db_connection();
14985 EMSTORAGE_START_WRITE_TRANSACTION(transaction, error);
14987 SNPRINTF(sql_query_string, sizeof(sql_query_string),
14988 "UPDATE mail_certificate_tbl SET"
14990 ", issue_month = ?" /* Index of certificate */
14991 ", issue_day = ?" /* Select the account */
14992 ", expiration_year = ?" /* Year of issue */
14993 ", expiration_month = ?" /* Month of issue */
14994 ", expiration_day = ?" /* Day of issue */
14995 ", issue_organization_name = ?" /* Year of expiration */
14996 ", email_address = ?" /* Month of expiration */
14997 ", subject_str = ?" /* Day of expiration */
14998 ", filepath = ?" /* Organization of issue */
15000 " WHERE certificate_id = ?");
15002 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, strlen(sql_query_string), &hStmt, NULL), rc);
15003 EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
15004 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15005 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15007 _bind_stmt_field_data_int(hStmt, ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_year);
15008 _bind_stmt_field_data_int(hStmt, ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_month);
15009 _bind_stmt_field_data_int(hStmt, ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_day);
15010 _bind_stmt_field_data_int(hStmt, EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_year);
15011 _bind_stmt_field_data_int(hStmt, EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_month);
15012 _bind_stmt_field_data_int(hStmt, EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_day);
15013 _bind_stmt_field_data_string(hStmt, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_organization_name, 0, ISSUE_ORGANIZATION_LEN_IN_MAIL_CERTIFICATE_TBL);
15014 _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->email_address, 0, EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL);
15015 _bind_stmt_field_data_string(hStmt, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->subject_str, 0, SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL);
15016 _bind_stmt_field_data_string(hStmt, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->filepath, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
15018 /* rc = sqlite3_step(hStmt); */
15019 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15020 EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
15021 ("sqlite3_step fail:%d", rc));
15022 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15023 ("sqlite3_step fail:%d", rc));
15025 /* validate account existence */
15026 rc = sqlite3_changes(local_db_handle);
15028 EM_DEBUG_EXCEPTION(" no matched account found...");
15030 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
15034 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15035 /* get password file name */
15036 if ((error = _get_cert_password_file_name(certificate->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
15037 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
15041 /* save passwords to the secure storage */
15042 if (ssm_write_buffer(certificate->password, strlen(certificate->password), cert_password_file_name, SSM_FLAG_SECRET_OPERATION, NULL) < 0) {
15043 EM_DEBUG_EXCEPTION("ssm_write_buffer failed - Private certificate password : [%s]", cert_password_file_name);
15044 error = EMAIL_ERROR_SYSTEM_FAILURE;
15048 if (!emstorage_notify_storage_event(NOTI_CERTIFICATE_UPDATE, certificate->certificate_id, 0, NULL, 0))
15049 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event[ NOTI_CERTIFICATE_UPDATE] : Notification Failed >>> ");
15055 if (hStmt != NULL) {
15056 EM_DEBUG_LOG(" Before sqlite3_finalize hStmt = %p", hStmt);
15058 rc = sqlite3_finalize(hStmt);
15059 if (rc != SQLITE_OK) {
15060 EM_DEBUG_LOG(" sqlite3_finalize failed - %d", rc);
15061 error = EMAIL_ERROR_DB_FAILURE;
15065 EMSTORAGE_FINISH_WRITE_TRANSACTION(transaction, ret, error);
15069 if (err_code != NULL)
15072 EM_DEBUG_FUNC_END("ret [%d]", ret);