Fixed the invalid casting and remove the flash noti
[platform/core/messaging/email-service.git] / email-core / email-storage / email-storage.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7 *
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
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
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.
19 *
20 */
21
22
23 /******************************************************************************
24  * File: email-storage.c
25  * Desc: email-service Storage Library on Sqlite3
26  *
27  * Auth:
28  *
29  * History:
30  *      2007.03.02 : created
31  *****************************************************************************/
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <stdbool.h>
37 #include <time.h>
38 #include <sys/types.h>
39 #include <sys/time.h>
40 #include <sys/wait.h>
41 #include <glib.h>
42 #include <sys/stat.h>
43 #include <unistd.h>
44 #include <errno.h>
45 #include <dirent.h>
46 #include <vconf.h>
47 #include <sys/mman.h>
48 #include <fcntl.h>
49 #include <tzplatform_config.h>
50
51 #define __USE_UNIX98
52 #define __USE_GNU
53 #include <pthread.h>
54
55 #include "email-internal-types.h"
56 #include "email-convert.h"
57 #include "email-core-utils.h"
58 #include "email-utilities.h"
59 #include "email-storage.h"
60 #include "email-debug-log.h"
61 #include "email-types.h"
62 #include "email-convert.h"
63 #include "email-core-signal.h"
64 #include "email-core-event.h"
65 #include "email-core-container.h"
66 #include "email-core-key-manager.h"
67
68 #define SETTING_MEMORY_TEMP_FILE_PATH "/tmp/email_tmp_file"
69 #define CONTENT_DATA                  "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset="
70 #define CONTENT_TYPE_DATA             "<meta http-equiv=\"Content-Type\" content=\"text/html; charset="
71
72 #define FLAG_SEEN       0x01
73 #define FLAG_DELETED    0x02
74 #define FLAG_FLAGGED    0x04
75 #define FLAG_ANSWERED   0x08
76 #define FLAG_OLD        0x10
77 #define FLAG_DRAFT      0x20
78
79 #undef close
80
81 #define ISSUE_ORGANIZATION_LEN_IN_MAIL_CERTIFICATE_TBL  256
82 #define EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL       128
83 #define SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL      1027
84 #define FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL           256
85
86 #define ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL            50
87 #define RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL   50
88 #define EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL              128
89 #define USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL               50
90 #define PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL                50
91 #define SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL     50
92 #define SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL            50
93 #define SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL        50
94 #define DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL            30
95 #define REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL           128
96 #define RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL             128
97 #define LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL          256
98 #define DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL       256
99 #define SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL               256
100 #define MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL                128
101 #define ALIAS_LEN_IN_MAIL_BOX_TBL                       128
102 #define LOCAL_MBOX_LEN_IN_MAIL_READ_MAIL_UID_TBL        128
103 #define MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL      128
104 #define S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL             128
105 #define DATA2_LEN_IN_MAIL_READ_MAIL_UID_TBL             256
106 #define FILTER_NAME_LEN_IN_MAIL_RULE_TBL                256
107 #define VALUE_LEN_IN_MAIL_RULE_TBL                      256
108 #define VALUE2_LEN_IN_MAIL_RULE_TBL                     256
109 #define DEST_MAILBOX_LEN_IN_MAIL_RULE_TBL               128
110 #define MAILBOX_NAME_LEN_IN_MAIL_ATTACHMENT_TBL         128
111 #define ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL      256
112 #define ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL      256
113 #define CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL           256
114 #define ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL 128
115 #define MAILBOX_LEN_IN_MAIL_TBL                         128
116 #define SERVER_MAILBOX_LEN_IN_MAIL_TBL                  128
117 #define SERVER_MAIL_ID_LEN_IN_MAIL_TBL                  128
118 #define FROM_LEN_IN_MAIL_TBL                            256
119 #define SENDER_LEN_IN_MAIL_TBL                          256
120 #define REPLY_TO_LEN_IN_MAIL_TBL                        256
121 #define TO_LEN_IN_MAIL_TBL                              3999
122 #define CC_LEN_IN_MAIL_TBL                              3999
123 #define BCC_LEN_IN_MAIL_TBL                             3999
124 #define RETURN_PATH_LEN_IN_MAIL_TBL                     3999
125 #define SUBJECT_LEN_IN_MAIL_TBL                         1027
126 #define THREAD_TOPIC_LEN_IN_MAIL_TBL                    256
127 #define TEXT_1_LEN_IN_MAIL_TBL                          256
128 #define TEXT_2_LEN_IN_MAIL_TBL                          256
129 #define MIME_ENTITY_LEN_IN_MAIL_TBL                     256
130 #define DATETIME_LEN_IN_MAIL_TBL                        128
131 #define MESSAGE_ID_LEN_IN_MAIL_TBL                      256
132 #define FROM_CONTACT_NAME_LEN_IN_MAIL_TBL               3999
133 #define FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL              3999
134 #define TO_CONTACT_NAME_LEN_IN_MAIL_TBL                 3999
135 #define TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL                3999
136 #define MAILBOX_LEN_IN_MAIL_MEETING_TBL                 128
137 #define LOCATION_LEN_IN_MAIL_MEETING_TBL                1024
138 #define GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL        512
139 #define STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL           32
140 #define DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL           32
141 #define PREVIEWBODY_LEN_IN_MAIL_TBL                     512
142 #define CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL        256
143
144 /*  this define is used for query to change data (delete, insert, update) */
145 #define EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction_flag, error_code) \
146         do {\
147                 if (transaction_flag) {\
148                         if (emstorage_begin_transaction(multi_user_name, NULL, NULL, &error_code) == false) {\
149                                 EM_DEBUG_EXCEPTION("emstorage_begin_transaction error [%d]", error_code);\
150                                 goto FINISH_OFF;\
151                         }\
152                 }\
153         } while(0)
154
155 /*  this define is used for query to change data (delete, insert, update) */
156 #define EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction_flag, result_code, error_code) \
157         do {\
158                 if (transaction_flag) {\
159                         if (result_code == true) {\
160                                 if (emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL) == false) {\
161                                         EM_DEBUG_EXCEPTION("emstorage_commit_transaction error");\
162                                         error_code = EMAIL_ERROR_DB_FAILURE;\
163                                         result_code = false;\
164                                 }\
165                         }\
166                         else {\
167                                 if (emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL) == false) {\
168                                         EM_DEBUG_EXCEPTION("emstorage_rollback_transaction error");\
169                                         error_code = EMAIL_ERROR_DB_FAILURE;\
170                                 }\
171                         }\
172                 }\
173         } while(0)
174
175 /*  this define is used for query to read (select) */
176 #define EMSTORAGE_START_READ_TRANSACTION(transaction_flag) \
177         if (transaction_flag)\
178         {\
179                 /*_timedlock_shm_mutex(mapped_for_db_lock, 2);*/\
180         }
181
182 /*  this define is used for query to read (select) */
183 #define EMSTORAGE_FINISH_READ_TRANSACTION(transaction_flag) \
184         if (transaction_flag)\
185         {\
186                 /*_unlockshm_mutex(mapped_for_db_lock);*/\
187         }
188
189 /*  for safety DB operation */
190 static pthread_mutex_t _db_handle_lock = PTHREAD_MUTEX_INITIALIZER;
191
192 /* for safety secure-storage operation */
193 static pthread_mutex_t _ss_handle_lock = PTHREAD_MUTEX_INITIALIZER;
194
195 #define _MULTIPLE_DB_HANDLE
196
197 #ifdef _MULTIPLE_DB_HANDLE
198
199 typedef struct
200 {
201     char *user_name;
202         pthread_t       thread_id;
203         sqlite3 *db_handle;
204 } db_handle_t;
205
206 #define MAX_DB_CLIENT 100
207
208 /* static int _db_handle_count = 0; */
209 db_handle_t _db_handle_list[MAX_DB_CLIENT] = {{NULL, 0, 0}, };
210
211
212 sqlite3 *emstorage_get_db_handle(char *multi_user_name)
213 {
214         EM_DEBUG_FUNC_BEGIN();
215         int i;
216         pthread_t current_thread_id = THREAD_SELF();
217         sqlite3 *result_db_handle = NULL;
218
219         ENTER_CRITICAL_SECTION(_db_handle_lock);
220         for (i = 0; i < MAX_DB_CLIENT; i++) {
221         if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id))
222         {
223             if (_db_handle_list[i].user_name) {
224                 if (EM_SAFE_STRCASECMP(_db_handle_list[i].user_name, multi_user_name ? multi_user_name : "root"))
225                     continue;
226             }
227                         EM_DEBUG_LOG_DEV("found db handle at [%d]", i);
228                         result_db_handle = _db_handle_list[i].db_handle;
229                         break;
230                 }
231         }
232         LEAVE_CRITICAL_SECTION(_db_handle_lock);
233
234         if (!result_db_handle)
235                 EM_DEBUG_LOG("no db_handle for [%d] found", current_thread_id);
236
237         EM_DEBUG_FUNC_END();
238         return result_db_handle;
239 }
240
241 int emstorage_set_db_handle(char *multi_user_name, sqlite3 *db_handle)
242 {
243         EM_DEBUG_FUNC_BEGIN();
244         int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
245         pthread_t current_thread_id = THREAD_SELF();
246
247         ENTER_CRITICAL_SECTION(_db_handle_lock);
248         for (i = 0; i < MAX_DB_CLIENT; i++)     {
249                 if (_db_handle_list[i].thread_id == 0) {
250                         _db_handle_list[i].thread_id = current_thread_id;
251                         _db_handle_list[i].db_handle = db_handle;
252             /* Only distinguished container and host  */
253             _db_handle_list[i].user_name = strdup(multi_user_name ? multi_user_name : "root");
254                         EM_DEBUG_LOG("current_thread_id [%d], index [%d]", current_thread_id, i);
255                         error_code =  EMAIL_ERROR_NONE;
256                         break;
257                 }
258         }
259         LEAVE_CRITICAL_SECTION(_db_handle_lock);
260
261         if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
262                 EM_DEBUG_EXCEPTION("Exceeded the limitation of db client. Can't find empty slot in _db_handle_list.");
263
264         EM_DEBUG_FUNC_END("error_code [%d]", error_code);
265         return error_code;
266 }
267
268 int emstorage_remove_db_handle()
269 {
270         EM_DEBUG_FUNC_BEGIN();
271         int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
272         ENTER_CRITICAL_SECTION(_db_handle_lock);
273         for (i = 0; i < MAX_DB_CLIENT; i++)
274         {
275                 if (_db_handle_list[i].thread_id == THREAD_SELF())
276                 {
277                         _db_handle_list[i].thread_id = 0;
278                         _db_handle_list[i].db_handle = NULL;
279             EM_SAFE_FREE(_db_handle_list[i].user_name);
280
281                         EM_DEBUG_LOG("index [%d]", i);
282                         error_code = EMAIL_ERROR_NONE;
283                         break;
284                 }
285         }
286         LEAVE_CRITICAL_SECTION(_db_handle_lock);
287
288         if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
289                 EM_DEBUG_EXCEPTION("Can't find proper thread_id");
290
291         EM_DEBUG_FUNC_END("error_code [%d]", error_code);
292         return error_code;
293 }
294
295 int emstorage_reset_db_handle_list()
296 {
297         EM_DEBUG_FUNC_BEGIN();
298         int i;
299
300         ENTER_CRITICAL_SECTION(_db_handle_lock);
301         for (i = 0; i < MAX_DB_CLIENT; i++)
302         {
303                 _db_handle_list[i].thread_id = 0;
304                 _db_handle_list[i].db_handle = NULL;
305         EM_SAFE_FREE(_db_handle_list[i].user_name);
306         }
307         LEAVE_CRITICAL_SECTION(_db_handle_lock)
308
309         EM_DEBUG_FUNC_END();
310         return EMAIL_ERROR_NONE;
311 }
312
313
314 sqlite3 *emstorage_get_db_connection(char *multi_user_name)
315 {
316         return emstorage_db_open(multi_user_name, NULL);
317 }
318
319 #else   /*  _MULTIPLE_DB_HANDLE */
320
321 sqlite3 *_db_handle = NULL;
322
323 sqlite3 *emstorage_get_db_connection()
324 {
325         if (NULL == _db_handle)
326                 emstorage_db_open(NULL);
327         return _db_handle;
328 }
329 #endif  /*  _MULTIPLE_DB_HANDLE */
330
331 /* ------------------------------------------------------------------------------ */
332 /*  Mutex using shared memory */
333 typedef struct
334 {
335         pthread_mutex_t mutex;
336         int data;
337 } mmapped_t;
338
339 mmapped_t       *mapped_for_db_lock = NULL;
340 int              shm_fd_for_db_lock = 0;
341
342 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
343 mmapped_t       *mapped_for_generating_mail_id = NULL;
344 int              shm_fd_for_generating_mail_id = 0;
345 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
346
347 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__
348 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
349         do {\
350                 _timedlock_shm_mutex(mapped_for_db_lock, 2);\
351                 return_value = function_call;\
352                 _unlockshm_mutex(mapped_for_db_lock);\
353         } while(0)
354
355 #else /*  __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
356 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
357         {  return_value = function_call; }
358 #endif /*  __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
359
360 static int emstorage_exec_query_by_prepare_v2(sqlite3 *local_db_handle, char *query_string)
361 {
362         EM_DEBUG_FUNC_BEGIN("local_db_handle[%p] query_string[%p]", local_db_handle, query_string);
363         int error = EMAIL_ERROR_NONE;
364         int rc = 0;
365         DB_STMT db_statement = NULL;
366
367         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, query_string, EM_SAFE_STRLEN(query_string), &db_statement, NULL), rc);
368         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
369                 ("sqlite3_prepare failed [%d] [%s]", rc, query_string));
370
371         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(db_statement), rc);
372         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
373         ("sqlite3_step failed [%d] [%s]", rc, query_string));
374
375 FINISH_OFF :
376
377         if (db_statement != NULL)  {
378                 rc = sqlite3_finalize(db_statement);
379                 if (rc != SQLITE_OK) {
380                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
381                         error = EMAIL_ERROR_DB_FAILURE;
382                 }
383         }
384
385         return error;
386 }
387
388 INTERNAL_FUNC int emstorage_shm_file_init(const char *shm_file_name)
389 {
390         EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
391         char errno_buf[ERRNO_BUF_SIZE] = {0};
392
393         if(!shm_file_name) {
394                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
395                 return EMAIL_ERROR_INVALID_PARAM;
396         }
397
398         int fd = shm_open (shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); /*  note: permission is not working */
399         if (fd < 0) {
400                 EM_DEBUG_EXCEPTION("shm_open errno [%d] [%s]", errno, EM_STRERROR(errno_buf));
401                 return EMAIL_ERROR_SYSTEM_FAILURE;
402         }
403
404         fchmod(fd, 0666);
405         EM_DEBUG_LOG("** Create SHM FILE **");
406         if (ftruncate(fd, sizeof(mmapped_t)) != 0) {
407                 EM_DEBUG_EXCEPTION("ftruncate errno [%d]", errno);
408                 return EMAIL_ERROR_SYSTEM_FAILURE;
409         }
410
411         mmapped_t *m = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
412         if (m == MAP_FAILED) {
413                 EM_DEBUG_EXCEPTION("mmap errno [%d]", errno);
414                 EM_SAFE_CLOSE (fd);
415                 return EMAIL_ERROR_SYSTEM_FAILURE;
416         }
417
418         m->data = 0;
419
420         pthread_mutexattr_t mattr;
421         pthread_mutexattr_init(&mattr);
422         pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
423         pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
424         pthread_mutex_init(&(m->mutex), &mattr);
425         pthread_mutexattr_destroy(&mattr);
426
427     pthread_mutex_destroy(&(m->mutex));
428     munmap(m, sizeof(mmapped_t));
429
430         EM_SAFE_CLOSE (fd);
431         EM_DEBUG_FUNC_END();
432         return EMAIL_ERROR_NONE;
433 }
434
435 int emstorage_shm_file_destroy(const char *shm_file_name)
436 {
437         EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
438         char errno_buf[ERRNO_BUF_SIZE] = {0};
439
440         if(!shm_file_name) {
441                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
442                 return EMAIL_ERROR_INVALID_PARAM;
443         }
444
445         if (shm_unlink(shm_file_name) != 0)
446                 EM_DEBUG_EXCEPTION("shm_unlink failed: %s", EM_STRERROR(errno_buf));
447         EM_DEBUG_FUNC_END();
448         return EMAIL_ERROR_NONE;
449 }
450
451 int _initialize_shm_mutex(const char *shm_file_name, int *param_shm_fd, mmapped_t **param_mapped)
452 {
453         EM_DEBUG_FUNC_BEGIN("shm_file_name [%p] param_shm_fd [%p], param_mapped [%p]", shm_file_name, param_shm_fd, param_mapped);
454         char errno_buf[ERRNO_BUF_SIZE] = {0};
455
456         if(!shm_file_name || !param_shm_fd || !param_mapped) {
457                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
458                 return EMAIL_ERROR_INVALID_PARAM;
459         }
460
461         if (!(*param_mapped)) {
462                 EM_DEBUG_LOG("** mapping begin **");
463                 if (!(*param_shm_fd)) { /*  open shm_file_name at first. Otherwise, the num of files in /proc/pid/fd will be increasing  */
464                         *param_shm_fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
465                         if ((*param_shm_fd) == -1) {
466                                 EM_DEBUG_EXCEPTION("FAIL: shm_open(): %s", EM_STRERROR(errno_buf));
467                                 return EMAIL_ERROR_SYSTEM_FAILURE;
468                         }
469                 }
470
471         fchmod((*param_shm_fd), 0666);
472         EM_DEBUG_LOG("** Create SHM FILE **");
473         if (ftruncate((*param_shm_fd), sizeof(mmapped_t)) != 0) {
474             EM_DEBUG_EXCEPTION("ftruncate errno [%d]", errno);
475             return EMAIL_ERROR_SYSTEM_FAILURE;
476         }
477
478                 mmapped_t *tmp = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ|PROT_WRITE, MAP_SHARED, (*param_shm_fd), 0);
479                 if (tmp == MAP_FAILED) {
480                         EM_DEBUG_EXCEPTION("mmap failed: %s", EM_STRERROR(errno_buf));
481                         return EMAIL_ERROR_SYSTEM_FAILURE;
482                 }
483
484         tmp->data = 0;
485
486         pthread_mutexattr_t mattr;
487         pthread_mutexattr_init(&mattr);
488         pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
489         pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
490         pthread_mutex_init(&(tmp->mutex), &mattr);
491         pthread_mutexattr_destroy(&mattr);
492
493                 *param_mapped = tmp;
494         }
495
496         EM_DEBUG_FUNC_END();
497         return EMAIL_ERROR_NONE;
498 }
499
500 int _timedlock_shm_mutex(mmapped_t *param_mapped, int sec)
501 {
502         EM_DEBUG_FUNC_BEGIN("param_mapped [%p], sec [%d]", param_mapped, sec);
503
504         if(!param_mapped) {
505                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
506                 return EMAIL_ERROR_INVALID_PARAM;
507         }
508
509         struct timespec abs_time;
510         clock_gettime(CLOCK_REALTIME, &abs_time);
511         abs_time.tv_sec += sec;
512         char errno_buf[ERRNO_BUF_SIZE] = {0};
513
514         int err = pthread_mutex_timedlock(&(param_mapped->mutex), &abs_time);
515
516         if (err == EOWNERDEAD) {
517                 err = pthread_mutex_consistent(&(param_mapped->mutex));
518                 EM_DEBUG_EXCEPTION("Previous owner is dead with lock. Fix mutex : %s", EM_STRERROR(errno_buf));
519         }
520         else if (err != 0) {
521                 EM_DEBUG_EXCEPTION("ERROR : %s", EM_STRERROR(errno_buf));
522                 return err;
523         }
524
525         EM_DEBUG_FUNC_END();
526         return EMAIL_ERROR_NONE;
527 }
528
529 void _unlockshm_mutex(mmapped_t *param_mapped)
530 {
531         EM_DEBUG_FUNC_BEGIN();
532         pthread_mutex_unlock(&(param_mapped->mutex));
533         EM_DEBUG_FUNC_END();
534 }
535 /* ------------------------------------------------------------------------------ */
536
537
538 static int _open_counter = 0;
539
540 static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name);
541 static int _read_password_from_secure_storage(char *file_name, char **password);
542
543 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
544 static int _get_cert_password_file_name(int index, char *cert_password_file_name);
545 #endif
546
547 typedef struct {
548         const char *object_name;
549         unsigned int data_flag;
550 } email_db_object_t;
551
552 static const email_db_object_t _g_db_tables[] =
553 {
554         { "mail_read_mail_uid_tbl", 1},
555         { "mail_tbl", 1},
556         { "mail_attachment_tbl", 1},
557         { NULL,  0},
558 };
559
560 static const email_db_object_t _g_db_indexes[] =
561 {
562         { "mail_read_mail_uid_idx1", 1},
563         { "mail_idx1", 1},
564         { "mail_attachment_idx1", 1},
565         { NULL,  1},
566 };
567
568 enum
569 {
570         CREATE_TABLE_MAIL_ACCOUNT_TBL,
571         CREATE_TABLE_MAIL_BOX_TBL,
572         CREATE_TABLE_MAIL_READ_MAIL_UID_TBL,
573         CREATE_TABLE_MAIL_RULE_TBL,
574         CREATE_TABLE_MAIL_TBL,
575         CREATE_TABLE_MAIL_ATTACHMENT_TBL,
576 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
577         CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL,
578 #else
579         CREATE_TABLE_DUMMY_INDEX1,
580 #endif
581         CREATE_TABLE_MAIL_MEETING_TBL,
582 #ifdef __FEATURE_LOCAL_ACTIVITY__
583         CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL,
584 #else
585         CREATE_TABLE_DUMMY_INDEX2,
586 #endif
587         CREATE_TABLE_MAIL_CERTIFICATE_TBL,
588         CREATE_TABLE_MAIL_TASK_TBL,
589 #ifdef __FEATURE_BODY_SEARCH__
590         CREATE_TABLE_MAIL_TEXT_TBL,
591 #else
592         CREATE_TABLE_DUMMY_INDEX3,
593 #endif
594
595 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
596         CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
597 #else
598         CREATE_TABLE_DUMMY_INDEX4,
599 #endif
600
601         /*CREATE INDEX*/
602         CREATE_TABLE_MAIL_ACCOUNT_IDX,
603         CREATE_TABLE_MAIL_BOX_IDX,
604         CREATE_TABLE_MAIL_READ_MAIL_UID_IDX,
605         CREATE_TABLE_MAIL_IDX,
606         CREATE_TABLE_MAIL_ATTACHMENT_IDX,
607         CREATE_TABLE_MAIL_MEETING_IDX,
608         CREATE_TABLE_MAIL_TASK_IDX,
609         CREATE_TABLE_MAIL_DATETIME_IDX,
610         CREATE_TABLE_MAIL_THREAD_IDX,
611         CREATE_TABLE_MAX,
612 };
613
614 enum
615 {
616         DATA1_IDX_IN_MAIL_ACTIVITY_TBL = 0,
617         TRANSTYPE_IDX_IN_MAIL_ACTIVITY_TBL,
618         FLAG_IDX_IN_MAIL_ACTIVITY_TBL,
619 };
620
621 enum
622 {
623         CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL = 0,
624         ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
625         ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
626         ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
627         EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
628         EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
629         EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
630         ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL,
631         EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL,
632         SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL,
633         FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL,
634 };
635
636 enum
637 {
638         TO_RECIPIENT = 0,
639         CC_RECIPIENT,
640         BCC_RECIPIENT,
641 };
642 enum
643 {
644         ACCOUNT_ID_IDX_IN_MAIL_BOX_TBL = 0,
645         LOCAL_YN_IDX_IN_MAIL_BOX_TBL,
646         MAILBOX_NAME_IDX_IN_MAIL_BOX_TBL,
647         MAILBOX_TYPE_IDX_IN_MAIL_BOX_TBL,
648         ALIAS_IDX_IN_MAIL_BOX_TBL,
649         SYNC_WITH_SERVER_YN_IDX_IN_MAIL_BOX_TBL,
650         MODIFIABLE_YN_IDX_IN_MAIL_BOX_TBL,
651         TOTAL_MAIL_COUNT_ON_SERVER_IDX_IN_MAIL_BOX_TBL,
652         ARCHIVE_IDX_IN_MAIL_BOX_TBL,
653         MAIL_SLOT_SIZE_IDX_IN_MAIL_BOX_TBL,
654 };
655
656 enum
657 {
658         ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL = 0,
659         LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
660         MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL,
661         LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
662         SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
663         RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL,
664         SYNC_STATUS_IDX_IN_MAIL_READ_MAIL_UID_TBL,
665         FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL,
666         FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL,
667         IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL,          /* unused */
668 };
669
670 #ifdef __FEATURE_BODY_SEARCH__
671 enum
672 {
673         MAIL_ID_IDX_IN_MAIL_TEXT_TBL = 0,
674         ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL,
675         MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL,
676         BODY_TEXT_IDX_IN_MAIL_TEXT_TBL,
677         FIELD_COUNT_OF_MAIL_TEXT_TBL,
678 };
679 #endif
680
681 enum
682 {
683         ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL = 0,
684         RULE_ID_IDX_IN_MAIL_RULE_TBL,
685         FILTER_NAME_IDX_IN_MAIL_RULE_TBL,
686         TYPE_IDX_IN_MAIL_RULE_TBL,
687         VALUE_IDX_IN_MAIL_RULE_TBL,
688         VALUE2_IDX_IN_MAIL_RULE_TBL,
689         ACTION_TYPE_IDX_IN_MAIL_RULE_TBL,
690         TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL,
691         FLAG1_IDX_IN_MAIL_RULE_TBL,
692         FLAG2_IDX_IN_MAIL_RULE_TBL,
693 };
694
695 enum
696 {
697         MAIL_ID_IDX_IN_MAIL_TBL = 0,
698         ACCOUNT_ID_IDX_IN_MAIL_TBL,
699         MAILBOX_ID_IDX_IN_MAIL_TBL,
700         MAILBOX_TYPE_IDX_IN_MAIL_TBL,
701         SUBJECT_IDX_IN_MAIL_TBL,
702         DATETIME_IDX_IN_MAIL_TBL,
703         SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL,
704         SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL,
705         SERVER_MAIL_ID_IDX_IN_MAIL_TBL,
706         MESSAGE_ID_IDX_IN_MAIL_TBL,
707         REFERENCE_ID_IDX_IN_MAIL_TBL,
708         FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL,
709         FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL,
710         FULL_ADDRESS_TO_IDX_IN_MAIL_TBL,
711         FULL_ADDRESS_CC_IDX_IN_MAIL_TBL,
712         FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL,
713         FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL,
714         EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL,
715         EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL,
716         ALIAS_SENDER_IDX_IN_MAIL_TBL,
717         ALIAS_RECIPIENT_IDX_IN_MAIL_TBL,
718         BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL,
719         FILE_PATH_PLAIN_IDX_IN_MAIL_TBL,
720         FILE_PATH_HTML_IDX_IN_MAIL_TBL,
721         FILE_PATH_MIME_ENTITY_IDX_IN_MAIL_TBL,
722         MAIL_SIZE_IDX_IN_MAIL_TBL,
723         FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL,
724         FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL,
725         FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL,
726         FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL,
727         FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL,
728         FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL,
729         FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL,
730         DRM_STATUS_IDX_IN_MAIL_TBL,
731         PRIORITY_IDX_IN_MAIL_TBL,
732         SAVE_STATUS_IDX_IN_MAIL_TBL,
733         LOCK_STATUS_IDX_IN_MAIL_TBL,
734         REPORT_STATUS_IDX_IN_MAIL_TBL,
735         ATTACHMENT_COUNT_IDX_IN_MAIL_TBL,
736         INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL,
737         THREAD_ID_IDX_IN_MAIL_TBL,
738         THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL,
739         PREVIEW_TEXT_IDX_IN_MAIL_TBL,
740         MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL,
741         MESSAGE_CLASS_IDX_IN_MAIL_TBL,
742         DIGEST_TYPE_IDX_IN_MAIL_TBL,
743         SMIME_TYPE_IDX_IN_MAIL_TBL,
744         SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL,
745         REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL,
746         TAG_ID_IDX_IN_MAIL_TBL,
747         REPLIED_TIME_IDX_IN_MAIL_TBL,
748         FORWARDED_TIME_IDX_IN_MAIL_TBL,
749         DEFAULT_CHARSET_IDX_IN_MAIL_TBL,
750         EAS_DATA_LENGTH_IDX_IN_MAIL_TBL,
751         EAS_DATA_IDX_IN_MAIL_TBL,
752         USER_NAME_IDX_IN_MAIL_TBL,
753         FIELD_COUNT_OF_MAIL_TBL,  /* End of mail_tbl */
754 };
755
756 enum
757 {
758         ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL = 0,
759         ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL,
760         ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL,
761         CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
762         ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL,
763         MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
764         ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
765         MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
766         ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
767         ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
768         ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL,
769         ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
770         ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
771 #ifdef __ATTACHMENT_OPTI__
772         ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL,
773         SECTION_IDX_IN_MAIL_ATTACHMENT_TBL,
774 #endif
775 };
776
777 enum {
778         IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL = 0,
779 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
780         MAIL_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
781         ACCOUNT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
782         ADDRESS_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
783         ADDRESS_IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL,
784 #endif
785         ADDRESS_IDX_IN_MAIL_CONTACT_SYNC_TBL,
786         CONTACT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
787         STORAGE_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
788         CONTACT_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
789 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
790         DISPLAY_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
791         FLAG1_IDX_IN_MAIL_CONTACT_SYNC_TBL,
792 #endif
793 };
794
795 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
796 enum
797 {
798         ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL = 0,
799         STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
800         ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
801         MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
802         SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
803         MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
804     MULTI_USER_NAME_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
805 };
806 #endif
807
808 /* sowmya.kr 03032010, changes for get list of mails for given addr list */
809 typedef struct _em_mail_id_list {
810         int mail_id;
811         struct _em_mail_id_list *next;
812 } em_mail_id_list;
813
814 static char *g_test_query[] = {
815                 /*  1. select mail_account_tbl */
816                 "SELECT"
817                 " account_name, "
818                 " incoming_server_type, "
819                 " incoming_server_address, "
820                 " user_email_address, "
821                 " incoming_server_user_name, "
822                 " incoming_server_password, "
823                 " retrieval_mode, "
824                 " incoming_server_port_number, "
825                 " incoming_server_secure_connection, "
826                 " incoming_server_authentication_method,"
827                 " outgoing_server_type, "
828                 " outgoing_server_address, "
829                 " outgoing_server_port_number, "
830                 " outgoing_server_need_authentication, "
831                 " outgoing_server_secure_connection, "
832                 " outgoing_server_user_name, "
833                 " outgoing_server_password, "
834                 " display_name, "
835                 " reply_to_addr, "
836                 " return_addr, "
837                 " account_id, "
838                 " keep_mails_on_pop_server_after_download, "
839                 " auto_resend_times, "
840                 " outgoing_server_size_limit, "
841                 " wifi_auto_download, "
842                 " pop_before_smtp, "
843                 " incoming_server_requires_apop,"
844                 " logo_icon_path, "
845                 " is_preset_account, "
846                 " check_interval, "
847                 " priority, "
848                 " keep_local_copy, "
849                 " req_delivery_receipt, "
850                 " req_read_receipt, "
851                 " download_limit, "
852                 " block_address, "
853                 " block_subject, "
854                 " display_name_from, "
855                 " reply_with_body, "
856                 " forward_with_files, "
857                 " add_myname_card, "
858                 " add_signature, "
859                 " signature"
860                 ", add_my_address_to_bcc"
861                 ", notification_status "
862                 ", vibrate_status "
863                 ", display_content_status "
864                 ", default_ringtone_status "
865                 ", alert_ringtone_path "
866                 ", account_svc_id "
867                 ", index_color "
868                 ", sync_status "
869                 ", sync_disabled "
870                 ", smime_type"
871                 ", certificate_path"
872                 ", cipher_type"
873                 ", digest_type"
874                 " FROM mail_account_tbl",
875                 /*  2. select mail_box_tbl */
876                 "SELECT "
877                 "   mailbox_id, "
878                 "   account_id, "
879                 "   local_yn,  "
880                 "   mailbox_name,  "
881                 "   mailbox_type,   "
882                 "   alias,  "
883                 "   deleted_flag,  "
884                 "   modifiable_yn,  "
885                 "   total_mail_count_on_server,  "
886                 "   has_archived_mails, "
887                 "   mail_slot_size, "
888                 "   no_select, "
889                 "   last_sync_time "
890                 " FROM mail_box_tbl ",
891                 /*  3. select mail_read_mail_uid_tbl */
892                 "SELECT  "
893                 "   account_id,  "
894                 "   mailbox_id,  "
895                 "   mailbox_name,  "
896                 "   local_uid,  "
897                 "   server_uid,  "
898                 "   rfc822_size ,  "
899                 "   sync_status,  "
900                 "   flags_seen_field,  "
901                 "   idx_num "
902                 " FROM mail_read_mail_uid_tbl ",
903                 /*  4. select mail_rule_tbl */
904                 "SELECT "
905                 "   account_id, "
906                 "   rule_id, "
907                 "   filter_name, "
908                 "   type, "
909                 "   value, "
910                 "   value2, "
911                 "   action_type, "
912                 "   target_mailbox_id,  "
913                 "   flag1, "
914                 "   flag2 "
915                 " FROM mail_rule_tbl    ",
916                 /*  5. select mail_tbl */
917                 "SELECT"
918                 "       mail_id, "
919                 "       account_id, "
920                 "       mailbox_id, "
921                 "       mailbox_name, "
922                 "   mailbox_type, "
923                 "   subject, "
924                 "       date_time, "
925                 "       server_mail_status, "
926                 "       server_mailbox_name, "
927                 "       server_mail_id, "
928                 "   message_id, "
929                 "       reference_mail_id, "
930                 "   full_address_from, "
931                 "   full_address_reply, "
932                 "   full_address_to, "
933                 "   full_address_cc, "
934                 "   full_address_bcc, "
935                 "   full_address_return, "
936                 "   email_address_sender, "
937                 "   email_address_recipient, "
938                 "   alias_sender, "
939                 "   alias_recipient, "
940                 "       body_download_status, "
941                 "       file_path_plain, "
942                 "       file_path_html, "
943                 "   file_path_mime_entity, "
944                 "       mail_size, "
945                 "   flags_seen_field     ,"
946                 "   flags_deleted_field  ,"
947                 "   flags_flagged_field  ,"
948                 "   flags_answered_field ,"
949                 "   flags_recent_field   ,"
950                 "   flags_draft_field    ,"
951                 "   flags_forwarded_field,"
952                 "       DRM_status, "
953                 "       priority, "
954                 "       save_status, "
955                 "       lock_status, "
956                 "       report_status, "
957                 "   attachment_count, "
958                 "       inline_content_count, "
959                 "       thread_id, "
960                 "       thread_item_count, "
961                 "   preview_text, "
962                 "       meeting_request_status, "
963                 "   message_class, "
964                 "   digest_type, "
965                 "   smime_type "
966                 " FROM mail_tbl",
967                 /*  6. select mail_attachment_tbl */
968                 "SELECT "
969                 "       attachment_id, "
970                 "       attachment_name, "
971                 "       attachment_path, "
972                 "       attachment_size, "
973                 "       mail_id,  "
974                 "       account_id, "
975                 "       mailbox_id, "
976                 "       attachment_save_status,  "
977                 "       attachment_drm_type,  "
978                 "       attachment_drm_method,  "
979                 "       attachment_inline_content_status,  "
980                 "       attachment_mime_type  "
981                 " FROM mail_attachment_tbl ",
982
983 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
984                 "SELECT  "
985                 "   account_id, "
986                 "   mail_id, "
987                 "   server_mail_id, "
988                 "   activity_id, "
989                 "   activity_type, "
990                 "   mailbox_id, "
991                 "   mailbox_name "
992                 "   multi_user_name "
993                 " FROM mail_partial_body_activity_tbl ",
994 #endif
995
996                 "SELECT  "
997                 "   mail_id, "
998                 "   account_id, "
999                 "   mailbox_id, "
1000                 "   meeting_response, "
1001                 "   start_time, "
1002                 "   end_time, "
1003                 "   location, "
1004                 "   global_object_id, "
1005                 "   offset, "
1006                 "   standard_name, "
1007                 "   standard_time_start_date, "
1008                 "   standard_bias, "
1009                 "   daylight_name, "
1010                 "   daylight_time_start_date, "
1011                 "   daylight_bias "
1012                 " FROM mail_meeting_tbl ",
1013
1014 #ifdef __FEATURE_LOCAL_ACTIVITY__
1015                 "SELECT "
1016                 "       activity_id, "
1017                 "   account_id, "
1018                 "   mail_id, "
1019                 "   activity_type, "
1020                 "   server_mailid, "
1021                 "   src_mbox , "
1022                 "       dest_mbox "
1023                 " FROM mail_local_activity_tbl  ",
1024 #endif
1025                 "SELECT "
1026                 "       certificate_id, "
1027                 "   issue_year, "
1028                 "   issue_month, "
1029                 "   issue_day, "
1030                 "   expiration_year, "
1031                 "   expiration_month, "
1032                 "   expiration_day, "
1033                 "   issue_organization_name, "
1034                 "   email_address, "
1035                 "   subject_str, "
1036                 "   filepath, "
1037                 "   password "
1038                 " FROM mail_certificate_tbl     ",
1039                 "SELECT "
1040                 "       task_id, "
1041                 "   task_type, "
1042                 "   task_status, "
1043                 "   task_priority, "
1044                 "   task_parameter_length, "
1045                 "   task_parameter , "
1046                 "       date_time "
1047                 " FROM mail_task_tbl    ",
1048 #ifdef __FEATURE_BODY_SEARCH__
1049                 "SELECT "
1050                 "       mail_id, "
1051                 "   account_id, "
1052                 "   mailbox_id, "
1053                 "   body_text "
1054                 " FROM mail_text_tbl    ",
1055 #endif
1056 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
1057                 "SELECT  "
1058                 "   activity_id, "
1059                 "   status, "
1060                 "   account_id, "
1061                 "   mail_id, "
1062                 "   server_mail_id, "
1063                 "   mailbox_id, "
1064                 "   multi_user_name, "
1065                 " FROM mail_auto_download_activity_tbl ",
1066 #endif
1067                 NULL,
1068 };
1069
1070 int _field_count_of_table[CREATE_TABLE_MAX] = { 0, };
1071
1072 static int _get_table_field_data_char(char  **table, char *buf, int index)
1073 {
1074         if ((table == NULL) || (buf == NULL) || (index < 0))  {
1075                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1076                 return false;
1077         }
1078
1079         if (table[index] != NULL) {
1080                 *buf = (char)atoi(table[index]);
1081                 return true;
1082         }
1083
1084         /*  EM_DEBUG_LOG("Empty field. Set as zero"); */
1085
1086         *buf = 0;
1087         return false;
1088 }
1089
1090 static int _get_table_field_data_int(char  **table, int *buf, int index)
1091 {
1092         if ((table == NULL) || (buf == NULL) || (index < 0))  {
1093                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1094                 return false;
1095         }
1096
1097         if (table[index] != NULL) {
1098                 *buf = atoi(table[index]);
1099                 return true;
1100         }
1101
1102         /*  EM_DEBUG_LOG("Empty field. Set as zero"); */
1103
1104         *buf = 0;
1105         return false;
1106 }
1107
1108 static int _get_table_field_data_time_t(char  **table, time_t *buf, int index)
1109 {
1110         if ((table == NULL) || (buf == NULL) || (index < 0))  {
1111                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1112                 return false;
1113         }
1114
1115         if (table[index] != NULL) {
1116                 *buf = (time_t)atol(table[index]);
1117                 return true;
1118         }
1119
1120         /*  EM_DEBUG_LOG("Empty field. Set as zero"); */
1121
1122         *buf = 0;
1123         return false;
1124 }
1125
1126 static int _get_table_field_data_string(char **table, char **buf, int ucs2, int index)
1127 {
1128         int ret = false;
1129
1130         if ((table == NULL) || (buf == NULL) || (index < 0))  {
1131                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1132                 return false;
1133         }
1134
1135         char *pTemp = table[index];
1136         int sLen = 0;
1137         if (pTemp == NULL)
1138                 *buf = NULL;
1139         else {
1140                 sLen = EM_SAFE_STRLEN(pTemp);
1141                 if(sLen) {
1142                         *buf = (char *) em_malloc(sLen + 1);
1143                         if (*buf == NULL) {
1144                                 EM_DEBUG_EXCEPTION("malloc is failed");
1145                                 goto FINISH_OFF;
1146                         }
1147                         strncpy(*buf, pTemp, sLen);
1148                 }
1149                 else
1150                         *buf = NULL;
1151         }
1152 #ifdef _PRINT_STORAGE_LOG_
1153         if (*buf)
1154                 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", *buf, index);
1155         else
1156                 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1157 #endif
1158         ret = true;
1159 FINISH_OFF:
1160
1161         return ret;
1162 }
1163
1164 static int _get_table_field_data_string_without_allocation(char **table, char *buf, int buffer_size, int ucs2, int index)
1165 {
1166         if ((table == NULL) || (buf == NULL) || (index < 0))  {
1167                 EM_DEBUG_EXCEPTION(" table[%p], buf[%p], index[%d]", table, buf, index);
1168                         return false;
1169         }
1170
1171         char *pTemp = table[index];
1172
1173         if (pTemp == NULL)
1174                 buf = NULL;
1175         else {
1176                 memset(buf, 0, buffer_size);
1177                 strncpy(buf, pTemp, buffer_size - 1);
1178         }
1179 #ifdef _PRINT_STORAGE_LOG_
1180         if (buf)
1181                 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", buf, index);
1182         else
1183                 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1184 #endif
1185
1186         return true;
1187 }
1188
1189 static int _get_table_field_data_blob(char **table, void **buffer, int buffer_size, int index)
1190 {
1191         if ((table == NULL) || (buffer == NULL) || (index < 0))  {
1192                 EM_DEBUG_EXCEPTION(" table[%p], buffer[%p], buffer_size [%d], index[%d]", table, buffer, buffer_size, index);
1193                 return false;
1194         }
1195
1196         char *temp_buffer = table[index];
1197
1198         if (temp_buffer == NULL)
1199                 buffer = NULL;
1200         else {
1201                 *buffer = malloc(buffer_size);
1202                 if(*buffer == NULL) {
1203                         EM_DEBUG_EXCEPTION("allocation failed.");
1204                         return false;
1205                 }
1206                 memset(*buffer, 0, buffer_size);
1207                 memcpy(*buffer, temp_buffer, buffer_size);
1208         }
1209 #ifdef _PRINT_STORAGE_LOG_
1210         if (buf)
1211                 EM_DEBUG_LOG("_get_table_field_data_string - buffer[%s], index[%d]", buffer, index);
1212         else
1213                 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1214 #endif
1215
1216         return true;
1217 }
1218
1219 static int _get_stmt_field_data_char(DB_STMT hStmt, char *buf, int index)
1220 {
1221         if ((hStmt == NULL) || (buf == NULL) || (index < 0))  {
1222                 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1223                 return false;
1224         }
1225
1226         if (sqlite3_column_text(hStmt, index) != NULL) {
1227                 *buf = (char)sqlite3_column_int(hStmt, index);
1228 #ifdef _PRINT_STORAGE_LOG_
1229                         EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1230 #endif
1231                 return true;
1232         }
1233
1234         EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1235
1236         return false;
1237 }
1238
1239 static int _get_stmt_field_data_int(DB_STMT hStmt, int *buf, int index)
1240 {
1241         if ((hStmt == NULL) || (buf == NULL) || (index < 0))  {
1242                 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1243                 return false;
1244         }
1245
1246         if (sqlite3_column_text(hStmt, index) != NULL) {
1247                 *buf = sqlite3_column_int(hStmt, index);
1248 #ifdef _PRINT_STORAGE_LOG_
1249                         EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1250 #endif
1251                 return true;
1252         }
1253
1254         EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1255
1256         return false;
1257 }
1258
1259 static int _get_stmt_field_data_time_t(DB_STMT hStmt, time_t *buf, int index)
1260 {
1261         if ((hStmt == NULL) || (buf == NULL) || (index < 0))  {
1262                 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1263                 return false;
1264         }
1265
1266         if (sqlite3_column_text(hStmt, index) != NULL) {
1267                 *buf = (time_t)sqlite3_column_int(hStmt, index);
1268 #ifdef _PRINT_STORAGE_LOG_
1269                 EM_DEBUG_LOG("_get_stmt_field_data_time_t [%d]", *buf);
1270 #endif
1271                 return true;
1272         }
1273
1274         EM_DEBUG_LOG("_get_stmt_field_data_time_t fail. index [%d]", index);
1275         return false;
1276 }
1277
1278 static int _get_stmt_field_data_string(DB_STMT hStmt, char **buf, int ucs2, int index)
1279 {
1280         if (!hStmt || !buf || (index < 0))  { /*prevent 39619*/
1281                 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1282                 return false;
1283         }
1284
1285         int sLen = 0;
1286         sLen = sqlite3_column_bytes(hStmt, index);
1287
1288 #ifdef _PRINT_STORAGE_LOG_
1289                 EM_DEBUG_LOG("_get_stmt_field_data_string sqlite3_column_bytes sLen[%d]", sLen);
1290 #endif
1291
1292         if (sLen > 0) {
1293                 *buf = (char *) em_malloc(sLen + 1);
1294                 if (*buf == NULL) {
1295                         EM_DEBUG_EXCEPTION("em_malloc failed");
1296                         return false;
1297                 }
1298
1299                 strncpy(*buf, (char *)sqlite3_column_text(hStmt, index), sLen);
1300         }
1301         else
1302                 *buf = NULL;
1303
1304 #ifdef _PRINT_STORAGE_LOG_
1305         if (*buf)
1306                 EM_DEBUG_LOG("buf[%s], index[%d]", *buf, index);
1307         else
1308                 EM_DEBUG_LOG("_get_stmt_field_data_string - No string got");
1309 #endif
1310
1311         return false;
1312 }
1313
1314 static void _get_stmt_field_data_blob(DB_STMT hStmt, void **buf, int index)
1315 {
1316         if( !hStmt || !buf || (index < 0))  { /*prevent 39618*/
1317                 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1318                 return;
1319         }
1320
1321         int sLen = 0;
1322         sLen = sqlite3_column_bytes(hStmt, index);
1323
1324 #ifdef _PRINT_STORAGE_LOG_
1325         EM_DEBUG_LOG("_get_stmt_field_data_blob sqlite3_column_bytes sLen[%d]", sLen);
1326 #endif
1327
1328         if (sLen > 0) {
1329                 *buf = (char *) em_malloc(sLen);
1330                 if (*buf == NULL) {
1331                         EM_DEBUG_EXCEPTION("em_malloc failed");
1332                         return;
1333                 }
1334
1335                 memcpy(*buf, (void *)sqlite3_column_blob(hStmt, index), sLen);
1336         }
1337         else
1338                 *buf = NULL;
1339
1340 }
1341
1342 static int _get_stmt_field_data_string_without_allocation(DB_STMT hStmt, char *buf, int buffer_size, int ucs2, int index)
1343 {
1344         if (!hStmt || !buf || (index < 0))  { /*prevent 39620*/
1345                 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], buffer_size[%d], index[%d]", hStmt, buf, buffer_size, index);
1346                 return false;
1347         }
1348
1349         int sLen = 0;
1350         sLen = sqlite3_column_bytes(hStmt, index);
1351
1352 #ifdef _PRINT_STORAGE_LOG_
1353                 EM_DEBUG_LOG("_get_stmt_field_data_string_without_allocation sqlite3_column_bytes sLen[%d]", sLen);
1354 #endif
1355
1356         if (sLen > 0) {
1357                 memset(buf, 0, buffer_size);
1358                 strncpy(buf, (char *)sqlite3_column_text(hStmt, index), buffer_size - 1);
1359         }
1360         else
1361                 strcpy(buf, "");
1362
1363 #ifdef _PRINT_STORAGE_LOG_
1364         EM_DEBUG_LOG("buf[%s], index[%d]", buf, index);
1365 #endif
1366
1367         return false;
1368 }
1369
1370 static int _bind_stmt_field_data_char(DB_STMT hStmt, int index, char value)
1371 {
1372         if ((hStmt == NULL) || (index < 0)) {
1373                 EM_DEBUG_EXCEPTION("index[%d]", index);
1374                 return false;
1375         }
1376
1377         int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1378
1379         if (ret != SQLITE_OK)  {
1380                 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1381                 return false;
1382         }
1383
1384         return true;
1385 }
1386
1387 static int _bind_stmt_field_data_int(DB_STMT hStmt, int index, int value)
1388 {
1389         if ((hStmt == NULL) || (index < 0)) {
1390                 EM_DEBUG_EXCEPTION("index[%d]", index);
1391                 return false;
1392         }
1393
1394         int ret = sqlite3_bind_int(hStmt, index+1, value);
1395
1396         if (ret != SQLITE_OK)  {
1397                 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1398                 return false;
1399         }
1400
1401         return true;
1402 }
1403
1404 static int _bind_stmt_field_data_time_t(DB_STMT hStmt, int index, time_t value)
1405 {
1406         if ((hStmt == NULL) || (index < 0)) {
1407                 EM_DEBUG_EXCEPTION("index[%d]", index);
1408                 return false;
1409         }
1410
1411         int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1412
1413         if (ret != SQLITE_OK)  {
1414                 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1415                 return false;
1416         }
1417
1418         return true;
1419 }
1420
1421 static int _bind_stmt_field_data_string(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1422 {
1423         if ((hStmt == NULL) || (index < 0)) {
1424                 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1425                 return false;
1426         }
1427
1428 #ifdef _PRINT_STORAGE_LOG_
1429         EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1430 #endif
1431
1432         int ret = 0;
1433         if (value != NULL)
1434                 ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1435         else
1436                 ret = sqlite3_bind_text(hStmt, index+1, "", -1, NULL);
1437
1438         if (ret != SQLITE_OK)  {
1439                 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1440                 return false;
1441         }
1442         return true;
1443 }
1444
1445 /* destroy function for sqlite3_bind_text */
1446 void _bind_stmt_free_string(void* arg)
1447 {
1448         EM_DEBUG_FUNC_BEGIN();
1449         char* p = (char*) arg;
1450         EM_SAFE_FREE(p);
1451         EM_DEBUG_FUNC_END();
1452 }
1453
1454 static int _bind_stmt_field_data_nstring(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1455 {
1456         if ((hStmt == NULL) || (index < 0)) {
1457                 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1458                 return false;
1459         }
1460
1461 #ifdef _PRINT_STORAGE_LOG_
1462         EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1463 #endif
1464
1465         int ret = 0;
1466         if (value != NULL) {
1467                 if (strlen(value) <= max_len)
1468                         ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1469                 else {
1470                         char *buf = (char*)em_malloc(sizeof(char) * (max_len));
1471                         if (buf == NULL) {
1472                                 EM_DEBUG_EXCEPTION("em_malloc failed");
1473                                 return false;
1474                         }
1475                         snprintf(buf, max_len-1, "%s", value);
1476                         ret = sqlite3_bind_text(hStmt, index+1, buf, -1, _bind_stmt_free_string);
1477                 }
1478         } else
1479                 ret = sqlite3_bind_text(hStmt, index+1, "", -1, NULL);
1480
1481         if (ret != SQLITE_OK)  {
1482                 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1483                 return false;
1484         }
1485         return true;
1486 }
1487
1488
1489 static int _bind_stmt_field_data_blob(DB_STMT hStmt, int index, void *blob, int blob_size)
1490 {
1491         if ((hStmt == NULL) || (index < 0)) {
1492                 EM_DEBUG_EXCEPTION("index[%d], blob_size[%d]", index, blob_size);
1493                 return false;
1494         }
1495
1496 #ifdef _PRINT_STORAGE_LOG_
1497         EM_DEBUG_LOG("hStmt = %p, index = %d, blob_size = %d, blob = [%p]", hStmt, index, blob_size, blob);
1498 #endif
1499
1500         int ret = 0;
1501         if (blob_size>0)
1502                 ret = sqlite3_bind_blob(hStmt, index+1, blob, blob_size, SQLITE_STATIC);
1503         else
1504                 ret = sqlite3_bind_null(hStmt, index+1);
1505
1506         if (ret != SQLITE_OK)  {
1507                 EM_DEBUG_EXCEPTION("sqlite3_bind_blob fail [%d]", ret);
1508                 return false;
1509         }
1510         return true;
1511 }
1512
1513
1514 static int _delete_temp_file(const char *path)
1515 {
1516         EM_DEBUG_FUNC_BEGIN("path[%p]", path);
1517
1518         DIR *dp = NULL;
1519         struct dirent *entry = NULL;
1520
1521         char buf[1024] = {0x00, };
1522
1523         if ((dp = opendir(path)) == NULL)  {
1524                 EM_DEBUG_EXCEPTION("opendir(\"%s\") failed...", path);
1525                 return false;
1526         }
1527
1528         while ((entry = readdir(dp)) != NULL)  {
1529                 SNPRINTF(buf, sizeof(buf), "%s/%s", path, entry->d_name);
1530                 remove(buf);
1531         }
1532
1533         closedir(dp);
1534         EM_DEBUG_FUNC_END();
1535         return true;
1536 }
1537
1538 char *cpy_str(char *src)
1539 {
1540         char *p = NULL;
1541
1542         if (src)  {
1543                 if (!(p = em_malloc((int)EM_SAFE_STRLEN(src) + 1)))  {
1544                         EM_DEBUG_EXCEPTION("mailoc failed...");
1545                         return NULL;
1546                 }
1547                 strncpy(p, src, EM_SAFE_STRLEN(src));
1548         }
1549
1550         return p;
1551 }
1552
1553 static void _emstorage_close_once(void)
1554 {
1555         EM_DEBUG_FUNC_BEGIN();
1556
1557         EM_DEBUG_FUNC_END();
1558 }
1559
1560 INTERNAL_FUNC int emstorage_close(int *err_code)
1561 {
1562         EM_DEBUG_FUNC_BEGIN();
1563
1564         int ret = false;
1565         int error = EMAIL_ERROR_NONE;
1566
1567         if (!emstorage_db_close(NULL, &error)) 
1568
1569         if (--_open_counter == 0)
1570                 _emstorage_close_once();
1571
1572         ret = true;
1573
1574         if (err_code != NULL)
1575                 *err_code = error;
1576
1577         EM_DEBUG_FUNC_END("ret [%d]", ret);
1578         return ret;
1579 }
1580
1581 static void *_emstorage_open_once(char *multi_user_name, int *err_code)
1582 {
1583         EM_DEBUG_FUNC_BEGIN();
1584
1585         int error = EMAIL_ERROR_NONE;
1586
1587     if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1588         char buf[MAX_PATH] = {0};
1589                 char *prefix_path = NULL;
1590
1591                 error = emcore_get_container_path(multi_user_name, &prefix_path);
1592                 if (error != EMAIL_ERROR_NONE) {
1593                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
1594                         goto FINISH_OFF;
1595                 }
1596
1597         memset(buf, 0x00, sizeof(buf));
1598         SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, EMAILPATH);
1599         mkdir(buf, DIRECTORY_PERMISSION);
1600
1601         memset(buf, 0x00, sizeof(buf));
1602         SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAILHOME);
1603         mkdir(buf, DIRECTORY_PERMISSION);
1604
1605         memset(buf, 0x00, sizeof(buf));
1606         SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAILTEMP);
1607         mkdir(buf, DIRECTORY_PERMISSION);
1608
1609         _delete_temp_file(buf);
1610                 EM_SAFE_FREE(prefix_path);
1611     } else {
1612         mkdir(DATA_PATH, DIRECTORY_PERMISSION);
1613         mkdir(EMAILPATH, DIRECTORY_PERMISSION);
1614         mkdir(MAILHOME, DIRECTORY_PERMISSION);
1615         mkdir(MAILTEMP, DIRECTORY_PERMISSION);
1616
1617         _delete_temp_file(MAILTEMP);
1618     }
1619
1620         if (!emstorage_create_table(multi_user_name, EMAIL_CREATE_DB_NORMAL, &error)) {
1621                 EM_DEBUG_EXCEPTION(" emstorage_create_table failed - %d", error);
1622                 goto FINISH_OFF;
1623         }
1624
1625 FINISH_OFF:
1626         
1627
1628         if (err_code != NULL)
1629                 *err_code = error;
1630
1631         return NULL;
1632 }
1633
1634  /*  pData : a parameter which is registered when busy handler is registerd */
1635  /*  count : retry count */
1636 #define EMAIL_STORAGE_MAX_RETRY_COUNT 20
1637
1638 static int _callback_sqlite_busy_handler(void *pData, int retry)
1639 {
1640         if (retry < EMAIL_STORAGE_MAX_RETRY_COUNT) {
1641                 EM_DEBUG_LOG("Busy handler called!!: [%d]", retry);
1642                 sqlite3_sleep(200);
1643                 return 1;
1644         }
1645
1646         EM_DEBUG_EXCEPTION("Invalid Busy handler called:: [%d]", retry);
1647         return 0;
1648 }
1649
1650 static int _callback_collation_utf7_sort(void *data, int length_text_a, const void *text_a,
1651                                                                                                          int length_text_b, const void *text_b)
1652 {
1653     EM_DEBUG_FUNC_BEGIN();
1654     int result = 0;
1655     char *converted_string_a = NULL;
1656     char *converted_string_b = NULL;
1657
1658         EM_DEBUG_LOG_DEV("text_a : [%s]", text_a);
1659         converted_string_a = emcore_convert_mutf7_to_utf8((char *)text_a);
1660         EM_DEBUG_LOG_DEV("Converted text_a : [%s]", converted_string_a);
1661
1662         EM_DEBUG_LOG_DEV("text_b : [%s]", text_b);
1663         converted_string_b = emcore_convert_mutf7_to_utf8((char *)text_b);
1664         EM_DEBUG_LOG_DEV("Converted text_b : [%s]", converted_string_b);
1665
1666         if (converted_string_a && converted_string_b)
1667                 result = strcmp(converted_string_a, converted_string_b);
1668
1669         EM_SAFE_FREE(converted_string_a);
1670         EM_SAFE_FREE(converted_string_b);
1671
1672         EM_DEBUG_FUNC_END();
1673         return result;
1674 }
1675
1676 static int _delete_all_files_and_directories(char *db_file_path, int *err_code)
1677 {
1678         EM_DEBUG_FUNC_BEGIN();
1679
1680         int error = EMAIL_ERROR_NONE;
1681         int ret = false;
1682
1683         if (!emstorage_delete_file(db_file_path, &error)) {
1684                 if (error != EMAIL_ERROR_FILE_NOT_FOUND) {
1685                         EM_DEBUG_EXCEPTION_SEC("remove failed - %s", EMAIL_SERVICE_DB_FILE_PATH);
1686                         goto FINISH_OFF;
1687                 }
1688         }
1689
1690         if (!emstorage_delete_dir(MAILHOME, &error)) {
1691                 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed");
1692                 goto FINISH_OFF;
1693         }
1694
1695         ret = true;
1696
1697 FINISH_OFF:
1698         if (err_code)
1699                 *err_code = error;
1700         EM_DEBUG_FUNC_END();
1701         return ret;
1702 }
1703
1704 static int _recovery_from_malformed_db_file(char *db_file_path, int *err_code)
1705 {
1706         EM_DEBUG_FUNC_BEGIN();
1707
1708         int error = EMAIL_ERROR_NONE;
1709         int ret = false;
1710
1711         /* Delete all files and directories */
1712         if (!_delete_all_files_and_directories(db_file_path, &error)) {
1713                 EM_DEBUG_EXCEPTION("_delete_all_files_and_directories failed [%d]", error);
1714                 goto FINISH_OFF;
1715         }
1716
1717         /* Delete all accounts on MyAccount */
1718
1719         /* Delete all managed connection to DB */
1720         emstorage_reset_db_handle_list();
1721
1722         ret = true;
1723
1724 FINISH_OFF:
1725         if (err_code)
1726                 *err_code = error;
1727         EM_DEBUG_FUNC_END();
1728         return ret;
1729 }
1730
1731 int _xsystem(const char *argv[])
1732 {
1733         int status = 0;
1734         pid_t pid;
1735         pid = fork();
1736
1737         switch (pid) {
1738                 case -1 : 
1739                         perror("fork failed");
1740                         return -1;
1741                 case 0 :
1742                         execvp(argv[0], (char *const *)argv);
1743                         _exit(-1);
1744                 default:
1745                         /* parent */
1746                         break;
1747         }
1748
1749         if (waitpid(pid, &status, 0) == -1) {
1750                 perror("waitpid failed");
1751                 return -1;
1752         }
1753
1754         if (WIFSIGNALED(status)) {
1755                 perror("signal");
1756                 return -1;
1757         }
1758
1759         if (!WIFEXITED(status)) {
1760                 perror("should not happen");
1761                 return -1;
1762         }
1763
1764         return WEXITSTATUS(status);
1765 }
1766
1767 #define SCRIPT_INIT_DB "/usr/bin/email-service_init_db.sh"
1768
1769 INTERNAL_FUNC int em_db_open(char *db_file_path, sqlite3 **sqlite_handle, int *err_code)
1770 {
1771         EM_DEBUG_FUNC_BEGIN();
1772         int rc = 0;
1773         int error = EMAIL_ERROR_NONE;
1774         int ret = false;
1775
1776         EM_DEBUG_LOG_DEV("*sqlite_handle[%p]", *sqlite_handle);
1777
1778         if (*sqlite_handle)  { /*prevent 33351*/
1779                 EM_DEBUG_LOG_DEV(">>>>> DB Already Opened......");
1780                 if (err_code != NULL)
1781                         *err_code = error;
1782                 return true;
1783         }
1784
1785         /* Generate db file */
1786         struct stat sts;
1787         ret = stat(db_file_path, &sts);
1788         if (ret == -1 && errno == ENOENT) {
1789                 const char *argv_script[] = {"/bin/sh", SCRIPT_INIT_DB, NULL};
1790                 ret = _xsystem(argv_script);
1791         }
1792
1793     EM_DEBUG_LOG("DB file path : [%s]", db_file_path);
1794
1795         /*  db open */
1796         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1797         if (SQLITE_OK != rc) {
1798                 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1799                 if (SQLITE_PERM == rc || SQLITE_CANTOPEN == rc) {
1800                         error = EMAIL_ERROR_PERMISSION_DENIED;
1801                 } else {
1802                         error = EMAIL_ERROR_DB_FAILURE;
1803                 }
1804                 sqlite3_close(*sqlite_handle);
1805                 *sqlite_handle = NULL;
1806
1807                 if (SQLITE_CORRUPT == rc) /* SQLITE_CORRUPT : The database disk image is malformed */ {/* Recovery DB file */
1808                         EM_DEBUG_LOG("The database disk image is malformed. Trying to remove and create database disk image and directories");
1809                         if (!_recovery_from_malformed_db_file(db_file_path, &error)) {
1810                                 EM_DEBUG_EXCEPTION("_recovery_from_malformed_db_file failed [%d]", error);
1811                                 goto FINISH_OFF;
1812                         }
1813
1814                         EM_DEBUG_LOG("Open DB again");
1815                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1816                         if (SQLITE_OK != rc) {
1817                                 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1818                                 if (SQLITE_PERM == rc) {
1819                                         error = EMAIL_ERROR_PERMISSION_DENIED;
1820                                 } else {
1821                                         error = EMAIL_ERROR_DB_FAILURE;
1822                                 }
1823                                 sqlite3_close(*sqlite_handle);
1824                                 *sqlite_handle = NULL;
1825                                 goto FINISH_OFF; /*prevent 33351*/
1826                         }
1827                 }
1828                 else
1829                         goto FINISH_OFF;
1830         }
1831
1832         EM_DEBUG_LOG_DEV(">>>>> Register DB Handle to busy handler: *sqlite_handle[%p]", *sqlite_handle);
1833         /* register busy handler */
1834         rc = sqlite3_busy_handler(*sqlite_handle, _callback_sqlite_busy_handler, NULL);  /*  Busy Handler registration, NULL is a parameter which will be passed to handler */
1835         if (SQLITE_OK != rc) {
1836                 EM_DEBUG_EXCEPTION("sqlite3_busy_handler fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1837                 error = EMAIL_ERROR_DB_FAILURE;
1838                 sqlite3_close(*sqlite_handle);
1839                 *sqlite_handle = NULL;
1840                 goto FINISH_OFF;
1841         }
1842
1843         /* Register collation callback function */
1844         rc = sqlite3_create_collation(*sqlite_handle, "CONVERTUTF8", SQLITE_UTF8, NULL, _callback_collation_utf7_sort);
1845         if (SQLITE_OK != rc) {
1846                 EM_DEBUG_EXCEPTION("sqlite3_create_collation failed : [%d][%s]", rc, sqlite3_errmsg(*sqlite_handle));
1847                 error = EMAIL_ERROR_DB_FAILURE;
1848                 sqlite3_close(*sqlite_handle);
1849                 *sqlite_handle = NULL;
1850                 goto FINISH_OFF;
1851         }
1852
1853         ret = true;
1854
1855 FINISH_OFF:
1856         if (err_code != NULL)
1857                 *err_code = error;
1858
1859         EM_DEBUG_FUNC_END("ret [%d]", ret);
1860         return ret;
1861 }
1862
1863 INTERNAL_FUNC sqlite3* emstorage_db_open(char *multi_user_name, int *err_code)
1864 {
1865         EM_DEBUG_FUNC_BEGIN();
1866
1867         sqlite3 *_db_handle = NULL;
1868
1869         int error = EMAIL_ERROR_NONE;
1870         char *prefix_path = NULL;
1871
1872         _db_handle = emstorage_get_db_handle(multi_user_name);
1873
1874         if( _db_handle == NULL) {
1875                 char *output_file_path = NULL;
1876         char temp_file_path[MAX_PATH] = {0};
1877
1878         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1879                         error = emcore_get_container_path(multi_user_name, &prefix_path);
1880                         if (error != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION && error != EMAIL_ERROR_NONE) {
1881                                 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", error);
1882                                 goto FINISH_OFF;
1883                         }
1884         } else {
1885             prefix_path = strdup("");
1886         }
1887
1888                 if (error == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1889                         if ((error = emcore_get_canonicalize_path(EMAIL_SERVICE_DB_FILE_PATH, &output_file_path)) != EMAIL_ERROR_NONE) {
1890                                 EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", error);
1891                                 goto FINISH_OFF;
1892                         }
1893
1894                         SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
1895                         EM_SAFE_FREE(output_file_path);
1896                 } else {
1897                         SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
1898                 }
1899
1900                 if (!em_db_open(temp_file_path, &_db_handle, &error)) {
1901                         EM_DEBUG_EXCEPTION("em_db_open failed[%d]", error);
1902                         goto FINISH_OFF;
1903                 }
1904
1905                 _initialize_shm_mutex(SHM_FILE_FOR_DB_LOCK, &shm_fd_for_db_lock, &mapped_for_db_lock);
1906
1907 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
1908                 _initialize_shm_mutex(SHM_FILE_FOR_MAIL_ID_LOCK, &shm_fd_for_generating_mail_id, &mapped_for_generating_mail_id);
1909 #endif /*__FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
1910
1911                 emstorage_set_db_handle(multi_user_name, _db_handle);
1912
1913                 emstorage_initialize_field_count();
1914         }
1915
1916 FINISH_OFF:
1917
1918         EM_SAFE_FREE(prefix_path);
1919
1920         if (err_code != NULL)
1921                 *err_code = error;
1922
1923         EM_DEBUG_FUNC_END("ret [%p]", _db_handle);
1924         return _db_handle;
1925 }
1926
1927 INTERNAL_FUNC int emstorage_db_close(char *multi_user_name, int *err_code)
1928 {
1929         EM_DEBUG_FUNC_BEGIN();
1930 #ifdef _MULTIPLE_DB_HANDLE
1931         sqlite3 *_db_handle = emstorage_get_db_handle(multi_user_name);
1932 #endif
1933
1934         int error = EMAIL_ERROR_NONE;
1935         int ret = false;
1936
1937         if (_db_handle) {
1938                 ret = sqlite3_close(_db_handle);
1939                 if (ret != SQLITE_OK) {
1940                         EM_DEBUG_EXCEPTION(" sqlite3_close fail - %d", ret);
1941                         error = EMAIL_ERROR_DB_FAILURE;
1942                         ret = false;
1943                         goto FINISH_OFF;
1944                 }
1945 #ifdef _MULTIPLE_DB_HANDLE
1946                 emstorage_remove_db_handle();
1947 #endif
1948                 _db_handle = NULL;
1949         }
1950
1951         ret = true;
1952
1953 FINISH_OFF:
1954         if (err_code != NULL)
1955                 *err_code = error;
1956
1957         EM_DEBUG_FUNC_END("ret [%d]", ret);
1958         return ret;
1959 }
1960
1961 INTERNAL_FUNC int emstorage_open(char *multi_user_name, int *err_code)
1962 {
1963         EM_DEBUG_FUNC_BEGIN();
1964
1965     int ret = false;
1966     int error = EMAIL_ERROR_NONE;
1967     int retValue;
1968         char *prefix_path = NULL;
1969     char buf[MAX_PATH] = {0};
1970
1971     if (EM_SAFE_STRLEN(multi_user_name) <= 0) {
1972         SNPRINTF(buf, sizeof(buf), "%s", DB_PATH);
1973     } else {
1974                 error = emcore_get_container_path(multi_user_name, &prefix_path);
1975                 if (error != EMAIL_ERROR_NONE) {
1976                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
1977                         goto FINISH_OFF;
1978                 }
1979         SNPRINTF(buf, sizeof(buf), "%s/%s", prefix_path, DB_PATH);
1980     }
1981
1982     retValue = mkdir(buf, DIRECTORY_PERMISSION);
1983
1984     EM_DEBUG_LOG("mkdir return- %d", retValue);
1985     EM_DEBUG_LOG("emstorage_open - before sqlite3_open - pid = %d", getpid());
1986
1987     if (emstorage_db_open(multi_user_name, &error) == NULL) {
1988         EM_DEBUG_EXCEPTION("emstorage_db_open failed[%d]", error);
1989         goto FINISH_OFF;
1990     }
1991
1992     if (_open_counter++ == 0) {
1993         _emstorage_open_once(multi_user_name, &error);
1994     }
1995
1996     ret = true;
1997
1998 FINISH_OFF:
1999
2000         EM_SAFE_FREE(prefix_path);
2001
2002     if (err_code != NULL)
2003         *err_code = error;
2004
2005     EM_DEBUG_FUNC_END("ret [%d]", ret);
2006     return ret;
2007 }
2008
2009 static int emstorage_get_field_count_from_create_table_query(char *input_create_table_query, int *output_field_count)
2010 {
2011         EM_DEBUG_FUNC_BEGIN("input_create_table_query[%d], output_field_count[%p]", input_create_table_query, output_field_count);
2012         int err = EMAIL_ERROR_NONE;
2013         int field_count = 0;
2014         char *pos = NULL;
2015
2016         if(input_create_table_query == NULL || output_field_count == NULL) {
2017                 err = EMAIL_ERROR_INVALID_PARAM;
2018                 goto FINISH_OFF;
2019         }
2020
2021         pos = input_create_table_query;
2022
2023         do {
2024                 field_count++;
2025                 if(pos == NULL || *pos == (char)0)
2026                         break;
2027                 pos += 1;
2028                 pos = strchr(pos, ',');
2029         } while(pos);
2030
2031         *output_field_count = field_count;
2032
2033         EM_DEBUG_LOG_DEV("field_count [%d]", field_count);
2034
2035 FINISH_OFF:
2036
2037         EM_DEBUG_FUNC_END("err [%d]", err);
2038         return err;
2039 }
2040
2041 INTERNAL_FUNC int emstorage_initialize_field_count()
2042 {
2043         EM_DEBUG_FUNC_BEGIN();
2044         int err = EMAIL_ERROR_NONE;
2045         int query_len = 0;
2046         char **create_table_query = NULL;
2047
2048         if(_field_count_of_table[CREATE_TABLE_MAIL_ACCOUNT_TBL] != 0) {
2049                 err = EMAIL_ERROR_ALREADY_INITIALIZED;
2050                 goto FINISH_OFF;
2051         }
2052
2053         err = emcore_load_query_from_file(EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2054         if (err != EMAIL_ERROR_NONE) {
2055                 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", err);
2056                 goto FINISH_OFF;
2057         }
2058
2059         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_ACCOUNT_TBL]));
2060         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_BOX_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_BOX_TBL]));
2061         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL]));
2062         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_RULE_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_RULE_TBL]));
2063         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_TBL]));
2064         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_ATTACHMENT_TBL]));
2065         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL]));
2066         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_MEETING_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_MEETING_TBL]));
2067 #ifdef __FEATURE_LOCAL_ACTIVITY__
2068         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL]));
2069 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2070         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_CERTIFICATE_TBL]));
2071         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_TASK_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_TASK_TBL]));
2072         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_TEXT_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_TEXT_TBL]));
2073 FINISH_OFF:
2074
2075         if (create_table_query) {
2076                 int i = 0;
2077                 for (i = 0; i < query_len; i++) {
2078                         if (create_table_query[i]) {
2079                                 EM_SAFE_FREE(create_table_query[i]);
2080                         }
2081                 }
2082                 EM_SAFE_FREE(create_table_query);
2083         }
2084
2085         EM_DEBUG_FUNC_END("err [%d]", err);
2086         return err;
2087 }
2088
2089 INTERNAL_FUNC int emstorage_create_table(char *multi_user_name, emstorage_create_db_type_t type, int *err_code)
2090 {
2091         EM_DEBUG_FUNC_BEGIN();
2092
2093         int error = EMAIL_ERROR_NONE;
2094         int rc = -1, ret = false;
2095         int query_len = 0;
2096         char sql_query_string[QUERY_SIZE] = {0, };
2097         char **create_table_query = NULL;
2098
2099         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2100
2101         error = emcore_load_query_from_file(EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2102         if (error != EMAIL_ERROR_NONE) {
2103                 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
2104                 goto FINISH_OFF2;
2105         }
2106
2107         if (query_len < CREATE_TABLE_MAX) {
2108                 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
2109                 error = EMAIL_ERROR_SYSTEM_FAILURE;
2110                 goto FINISH_OFF2;
2111         }
2112
2113         EM_DEBUG_LOG("local_db_handle = %p.", local_db_handle);
2114
2115         char *sql;
2116         char **result = NULL;
2117
2118         /*  1. create mail_account_tbl */
2119         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_account_tbl';";
2120         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2121         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2122                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2123
2124         EM_DEBUG_LOG("emstorage_create_table - result[1] = %s %c", result[1], result[1]);
2125
2126         if (atoi(result[1]) < 1) {
2127                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2128                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2129                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2130
2131                 EM_DEBUG_LOG("CREATE TABLE mail_account_tbl");
2132                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2133                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2134                 if (error != EMAIL_ERROR_NONE) {
2135                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2136                         goto FINISH_OFF;
2137                 }
2138
2139                 /*  create mail_account_tbl unique index */
2140                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ACCOUNT_IDX]);
2141                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2142                 if (error != EMAIL_ERROR_NONE) {
2143                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2144                         goto FINISH_OFF;
2145                 }
2146
2147                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2148         } /*  mail_account_tbl */
2149         else if (type == EMAIL_CREATE_DB_CHECK)  {
2150                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], NULL, NULL, NULL), rc);
2151                 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)));
2152         }
2153
2154         sqlite3_free_table(result);
2155         result = NULL;
2156
2157         /*  2. create mail_box_tbl */
2158         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_box_tbl';";
2159
2160         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2161         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2162                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2163
2164         if (atoi(result[1]) < 1) {
2165                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2166                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2167                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2168
2169                 EM_DEBUG_LOG("CREATE TABLE mail_box_tbl");
2170
2171                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_BOX_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2172                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2173                 if (error != EMAIL_ERROR_NONE) {
2174                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2175                         goto FINISH_OFF;
2176                 }
2177
2178                 /*  create mail_local_mailbox_tbl unique index */
2179                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_BOX_IDX]);
2180                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2181                 if (error != EMAIL_ERROR_NONE) {
2182                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2183                         goto FINISH_OFF;
2184                 }
2185
2186                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2187         } /*  mail_box_tbl */
2188         else if (type == EMAIL_CREATE_DB_CHECK)  {
2189                 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_BOX_TBL], NULL, NULL, NULL);
2190                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2191                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_BOX_TBL], rc, sqlite3_errmsg(local_db_handle)));
2192         }
2193         sqlite3_free_table(result);
2194         result = NULL;
2195
2196         /*  3. create mail_read_mail_uid_tbl */
2197         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_read_mail_uid_tbl';";
2198         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2199         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2200                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2201
2202
2203         if (atoi(result[1]) < 1) {
2204                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2205                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2206                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2207
2208                 EM_DEBUG_LOG("CREATE TABLE mail_read_mail_uid_tbl");
2209
2210                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2211                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2212                 if (error != EMAIL_ERROR_NONE) {
2213                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2214                         goto FINISH_OFF;
2215                 }
2216
2217                 /*  create mail_read_mail_uid_tbl unique index */
2218                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_IDX]);
2219                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2220                 if (error != EMAIL_ERROR_NONE) {
2221                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2222                         goto FINISH_OFF;
2223                 }
2224
2225                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2226         } /*  mail_read_mail_uid_tbl */
2227         else if (type == EMAIL_CREATE_DB_CHECK)  {
2228                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], NULL, NULL, NULL), rc);
2229                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2230                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], rc, sqlite3_errmsg(local_db_handle)));
2231         }
2232         sqlite3_free_table(result);
2233         result = NULL;
2234
2235         /*  4. create mail_rule_tbl */
2236         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_rule_tbl';";
2237
2238         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2239         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2240                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2241
2242         if (atoi(result[1]) < 1) {
2243                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2244                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2245                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2246
2247                 EM_DEBUG_LOG("CREATE TABLE mail_rule_tbl");
2248
2249                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_RULE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2250                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2251                 if (error != EMAIL_ERROR_NONE) {
2252                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2253                         goto FINISH_OFF;
2254                 }
2255
2256                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2257         } /*  mail_rule_tbl */
2258         else if (type == EMAIL_CREATE_DB_CHECK)  {
2259                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_RULE_TBL], NULL, NULL, NULL), rc);
2260                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2261                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_RULE_TBL], rc, sqlite3_errmsg(local_db_handle)));
2262         }
2263         sqlite3_free_table(result);
2264         result = NULL;
2265
2266         /*  5. create mail_tbl */
2267         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_tbl';";
2268         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2269         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2270                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2271
2272         if (atoi(result[1]) < 1) {
2273                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2274                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2275                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2276                 EM_DEBUG_LOG("CREATE TABLE mail_tbl");
2277
2278                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2279                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2280                 if (error != EMAIL_ERROR_NONE) {
2281                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2282                         goto FINISH_OFF;
2283                 }
2284
2285                 /*  create mail_tbl unique index */
2286                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_IDX]);
2287                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2288                 if (error != EMAIL_ERROR_NONE) {
2289                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2290                         goto FINISH_OFF;
2291                 }
2292
2293                 /*  create mail_tbl index for date_time */
2294                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_DATETIME_IDX]);
2295                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2296                 if (error != EMAIL_ERROR_NONE) {
2297                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2298                         goto FINISH_OFF;
2299                 }
2300
2301                 /*  create mail_tbl index for thread_item_count */
2302                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_THREAD_IDX]);
2303                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2304                 if (error != EMAIL_ERROR_NONE) {
2305                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2306                         goto FINISH_OFF;
2307                 }
2308
2309                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2310                 /*  just one time call */
2311 /*              EFTSInitFTSIndex(FTS_EMAIL_IDX); */
2312         } /*  mail_tbl */
2313         else if (type == EMAIL_CREATE_DB_CHECK)  {
2314                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TBL], NULL, NULL, NULL), rc);
2315                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2316                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TBL], rc, sqlite3_errmsg(local_db_handle)));
2317         }
2318         sqlite3_free_table(result);
2319         result = NULL;
2320
2321         /*  6. create mail_attachment_tbl */
2322         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_attachment_tbl';";
2323         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2324         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2325                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2326
2327         if (atoi(result[1]) < 1) {
2328                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2329                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2330                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2331
2332                 EM_DEBUG_LOG("CREATE TABLE mail_attachment_tbl");
2333
2334                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2335                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2336                 if (error != EMAIL_ERROR_NONE) {
2337                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2338                         goto FINISH_OFF;
2339                 }
2340
2341                 /*  create mail_attachment_tbl unique index */
2342                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_IDX]);
2343                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2344                 if (error != EMAIL_ERROR_NONE) {
2345                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2346                         goto FINISH_OFF;
2347                 }
2348
2349                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2350         } /*  mail_attachment_tbl */
2351         else if (type == EMAIL_CREATE_DB_CHECK)  {
2352                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], NULL, NULL, NULL), rc);
2353                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2354                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], rc, sqlite3_errmsg(local_db_handle)));
2355         }
2356         sqlite3_free_table(result);
2357         result = NULL;
2358
2359 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
2360
2361         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_partial_body_activity_tbl';";
2362         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2363         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2364                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2365
2366         if (atoi(result[1]) < 1) {
2367
2368                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2369                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2370                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2371
2372                 EM_DEBUG_LOG("CREATE TABLE mail_partial_body_activity_tbl");
2373
2374                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2375                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2376                 if (error != EMAIL_ERROR_NONE) {
2377                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2378                         goto FINISH_OFF;
2379                 }
2380
2381                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2382         } /*  mail_rule_tbl */
2383         else if (type == EMAIL_CREATE_DB_CHECK)  {
2384                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2385                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2386                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2387         }
2388         sqlite3_free_table(result);
2389         result = NULL;
2390
2391 #endif /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
2392
2393         /*  create mail_meeting_tbl */
2394         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_meeting_tbl';";
2395         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2396         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2397                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2398
2399         if (atoi(result[1]) < 1) {
2400                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2401                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2402                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2403
2404                 EM_DEBUG_LOG("CREATE TABLE mail_meeting_tbl");
2405
2406                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_MEETING_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2407                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2408                 if (error != EMAIL_ERROR_NONE) {
2409                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2410                         goto FINISH_OFF;
2411                 }
2412
2413                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_MEETING_IDX]);
2414                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2415                 if (error != EMAIL_ERROR_NONE) {
2416                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2417                         goto FINISH_OFF;
2418                 }
2419
2420                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2421         } /*  mail_contact_sync_tbl */
2422         else if (type == EMAIL_CREATE_DB_CHECK)  {
2423                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], NULL, NULL, NULL), rc);
2424                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2425                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], rc, sqlite3_errmsg(local_db_handle)));
2426         }
2427         sqlite3_free_table(result);
2428         result = NULL;
2429
2430 #ifdef __FEATURE_LOCAL_ACTIVITY__
2431
2432                 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_local_activity_tbl';";
2433                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2434                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2435                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2436
2437                 if (atoi(result[1]) < 1) {
2438
2439                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2440                         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2441                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2442
2443                         EM_DEBUG_LOG(" CREATE TABLE mail_local_activity_tbl");
2444
2445                         SNPRINTF(sql_query_string, sizeof(sql_query_string), create_table_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL]);
2446                         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2447                         if (error != EMAIL_ERROR_NONE) {
2448                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2449                                 goto FINISH_OFF;
2450                         }
2451
2452                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2453                 } /*  mail_rule_tbl */
2454                 else if (type == EMAIL_CREATE_DB_CHECK)  {
2455                         rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL);
2456                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2457                         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2458                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2459                 }
2460                 sqlite3_free_table(result);
2461                 result = NULL;
2462 #endif /*  __FEATURE_LOCAL_ACTIVITY__ */
2463         /*  create mail_certificate_tbl */
2464         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_certificate_tbl';";
2465         /*  rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL);   */
2466         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2467         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)));
2468
2469         if (atoi(result[1]) < 1) {
2470                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2471                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2472
2473                 EM_DEBUG_LOG("CREATE TABLE mail_certificate_tbl");
2474
2475                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2476                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2477                 if (error != EMAIL_ERROR_NONE) {
2478                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2479                         goto FINISH_OFF;
2480                 }
2481
2482                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2483         } /*  mail_contact_sync_tbl */
2484         else if (type == EMAIL_CREATE_DB_CHECK)  {
2485                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], NULL, NULL, NULL), rc);
2486                 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)));
2487         }
2488
2489         sqlite3_free_table(result);
2490         result = NULL;
2491
2492         /*  create mail_task_tbl */
2493         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_task_tbl';";
2494         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2495         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)));
2496
2497         if (atoi(result[1]) < 1) {
2498                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2499                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2500
2501                 EM_DEBUG_LOG("CREATE TABLE mail_task_tbl");
2502
2503                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TASK_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2504                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2505                 if (error != EMAIL_ERROR_NONE) {
2506                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2507                         goto FINISH_OFF;
2508                 }
2509
2510                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_TASK_IDX]);
2511                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2512                 if (error != EMAIL_ERROR_NONE) {
2513                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2514                         goto FINISH_OFF;
2515                 }
2516
2517                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2518         } /*  mail_task_tbl */
2519         else if (type == EMAIL_CREATE_DB_CHECK)  {
2520                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TASK_TBL], NULL, NULL, NULL), rc);
2521                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TASK_TBL], rc, sqlite3_errmsg(local_db_handle)));
2522         }
2523
2524         sqlite3_free_table(result);
2525         result = NULL;
2526
2527 #ifdef __FEATURE_BODY_SEARCH__
2528         /*  create mail_text_tbl */
2529         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_text_tbl';";
2530         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2531         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)));
2532
2533         if (atoi(result[1]) < 1) {
2534                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2535                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2536
2537                 EM_DEBUG_LOG("CREATE TABLE mail_text_tbl");
2538
2539                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TEXT_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2540                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2541                 if (error != EMAIL_ERROR_NONE) {
2542                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2543                         goto FINISH_OFF;
2544                 }
2545
2546                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2547         } /*  mail_text_tbl */
2548         else if (type == EMAIL_CREATE_DB_CHECK)  {
2549                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TEXT_TBL], NULL, NULL, NULL), rc);
2550                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TEXT_TBL], rc, sqlite3_errmsg(local_db_handle)));
2551         }
2552
2553         sqlite3_free_table(result);
2554         result = NULL;
2555
2556 #endif
2557
2558 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
2559         /*  create mail_auto_download_activity_tbl */
2560         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_auto_download_activity_tbl';";
2561         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2562         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2563
2564         if (atoi(result[1]) < 1) {
2565                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2566                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2567
2568                 EM_DEBUG_LOG("CREATE TABLE mail_auto_download_activity_tbl");
2569
2570                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], sizeof(sql_query_string)-1);
2571                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2572                 if (error != EMAIL_ERROR_NONE) {
2573                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2574                         goto FINISH_OFF;
2575                 }
2576
2577                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2578         } /*  mail_auto_download_activity_tbl */
2579         else if (type == EMAIL_CREATE_DB_CHECK)  {
2580                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2581                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2582         }
2583
2584         sqlite3_free_table(result);
2585         result = NULL;
2586 #endif
2587
2588
2589         ret = true;
2590
2591 FINISH_OFF:
2592
2593         if(result) sqlite3_free_table(result);
2594
2595         if (ret == true) {
2596                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2597         }
2598         else {
2599                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
2600         }
2601
2602 FINISH_OFF2:
2603         if (create_table_query) {
2604                 int i = 0;
2605                 for (i = 0; i < query_len; i++) {
2606                         if (create_table_query[i]) {
2607                                 EM_SAFE_FREE(create_table_query[i]);
2608                         }
2609                 }
2610                 EM_SAFE_FREE(create_table_query);
2611         }
2612
2613         if (err_code != NULL)
2614                 *err_code = error;
2615
2616         EM_DEBUG_FUNC_END("ret [%d]", ret);
2617         return ret;
2618 }
2619
2620 /* Query series --------------------------------------------------------------*/
2621 INTERNAL_FUNC int emstorage_query_mail_count(char *multi_user_name, const char *input_conditional_clause, int input_transaction, int *output_total_mail_count, int *output_unseen_mail_count)
2622 {
2623         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);
2624         int rc = -1;
2625         int query_size = 0;
2626         int error = EMAIL_ERROR_NONE;
2627         DB_STMT hStmt = NULL;
2628         char *sql_query_string = NULL;
2629         char **result;
2630         sqlite3 *local_db_handle = NULL;
2631
2632         if (!input_conditional_clause || (!output_total_mail_count && !output_unseen_mail_count)) {
2633                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2634                 return EMAIL_ERROR_INVALID_PARAM;
2635         }
2636
2637         query_size = EM_SAFE_STRLEN(input_conditional_clause) + QUERY_SIZE;
2638         sql_query_string = em_malloc(query_size);
2639         if (sql_query_string == NULL) {
2640                 EM_DEBUG_EXCEPTION("em_malloc failed");
2641                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2642                 goto FINISH_OFF;
2643         }
2644
2645         local_db_handle = emstorage_get_db_connection(multi_user_name);
2646
2647         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2648
2649         SNPRINTF(sql_query_string, query_size, "SELECT COUNT(*) FROM mail_tbl");
2650         EM_SAFE_STRCAT(sql_query_string, (char*)input_conditional_clause);
2651
2652         if (output_total_mail_count)  {
2653                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
2654                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2655                         ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
2656
2657                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2658                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
2659                         ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
2660                 _get_stmt_field_data_int(hStmt, output_total_mail_count, 0);
2661         }
2662
2663         if (output_unseen_mail_count)  {
2664                 EM_SAFE_STRCAT(sql_query_string, " AND flags_seen_field = 0 ");
2665
2666                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
2667                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2668                         ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2669
2670                 *output_unseen_mail_count = atoi(result[1]);
2671                 sqlite3_free_table(result);
2672         }
2673
2674 FINISH_OFF:
2675
2676         if (hStmt != NULL)  {
2677                 rc = sqlite3_finalize(hStmt);
2678                 if (rc != SQLITE_OK)  {
2679                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
2680                         error = EMAIL_ERROR_DB_FAILURE;
2681                 }
2682         }
2683
2684         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2685
2686         EM_SAFE_FREE(sql_query_string);
2687
2688         EM_DEBUG_FUNC_END("error [%d]", error);
2689         return error;
2690 }
2691
2692 INTERNAL_FUNC int emstorage_query_mail_id_list(char *multi_user_name, const char *input_conditional_clause, int input_transaction, int **output_mail_id_list, int *output_mail_id_count)
2693 {
2694         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);
2695
2696         int      i = 0;
2697         int      count = 0;
2698         int      rc = -1;
2699         int      cur_query = 0;
2700         int      col_index;
2701         int      query_size = 0;
2702         int      error = EMAIL_ERROR_NONE;
2703         int     *result_mail_id_list = NULL;
2704         char   **result = NULL;
2705         char     *sql_query_string = NULL;
2706         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2707
2708         EM_IF_NULL_RETURN_VALUE(input_conditional_clause, EMAIL_ERROR_INVALID_PARAM);
2709         EM_IF_NULL_RETURN_VALUE(output_mail_id_list, EMAIL_ERROR_INVALID_PARAM);
2710         EM_IF_NULL_RETURN_VALUE(output_mail_id_count, EMAIL_ERROR_INVALID_PARAM);
2711
2712         query_size = strlen(input_conditional_clause) + strlen("SELECT mail_id FROM mail_tbl ") + 10;  // 10 is extra space
2713         sql_query_string = em_malloc(query_size);
2714         if (sql_query_string == NULL) {
2715                 EM_DEBUG_EXCEPTION("em_malloc failed");
2716                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2717                 goto FINISH_OFF;
2718         }
2719
2720         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2721
2722         /* Composing query */
2723         SNPRINTF_OFFSET(sql_query_string, cur_query, query_size, "SELECT mail_id FROM mail_tbl ");
2724         EM_SAFE_STRCAT(sql_query_string, (char*)input_conditional_clause);
2725
2726         EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
2727
2728         /* Performing query */
2729         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2730         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2731                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2732
2733         col_index = 1;
2734
2735         /*  to get mail list */
2736         if (count == 0) {
2737                 EM_DEBUG_LOG("No mail found...");
2738                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2739                 goto FINISH_OFF;
2740         }
2741
2742         EM_DEBUG_LOG("There are [%d] mails.", count);
2743
2744         if (!(result_mail_id_list = (int*)em_malloc(sizeof(int) * count))) {
2745                 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
2746                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2747                 goto FINISH_OFF;
2748         }
2749
2750         EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2751
2752         for (i = 0; i < count; i++)
2753                 _get_table_field_data_int(result, result_mail_id_list + i, col_index++);
2754
2755         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >> ", count);
2756
2757         *output_mail_id_list  = result_mail_id_list;
2758         *output_mail_id_count = count;
2759
2760 FINISH_OFF:
2761
2762         if(result)
2763                 sqlite3_free_table(result);
2764
2765         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2766
2767         EM_SAFE_FREE(sql_query_string);
2768
2769         if(error != EMAIL_ERROR_NONE)
2770                 EM_SAFE_FREE(result_mail_id_list);
2771
2772         EM_DEBUG_FUNC_END("error [%d]", error);
2773         return error;
2774 }
2775
2776 INTERNAL_FUNC int emstorage_query_mail_list(char *multi_user_name, const char *conditional_clause, int transaction, email_mail_list_item_t** result_mail_list,  int *result_count,  int *err_code)
2777 {
2778         EM_DEBUG_FUNC_BEGIN();
2779         EM_PROFILE_BEGIN(emstorage_query_mail_list_func);
2780
2781         int i = 0, count = 0, rc = -1, to_get_count = (result_mail_list)?0:1;
2782         int sql_query_string_length = 0;
2783         int local_inline_content_count = 0, local_attachment_count = 0;
2784         int cur_query = 0, base_count = 0, col_index;
2785         int ret = false, error = EMAIL_ERROR_NONE;
2786         char *date_time_string = NULL;
2787         char **result = NULL;
2788         char *field_mail_id = "mail_id";
2789         char *field_all     = "mail_id, account_id, mailbox_id, mailbox_type, full_address_from, email_address_sender, full_address_to, subject, body_download_status, mail_size, flags_seen_field, flags_deleted_field, flags_flagged_field, flags_answered_field, flags_recent_field, flags_draft_field, flags_forwarded_field, DRM_status, priority, save_status, lock_status, attachment_count, inline_content_count, date_time, preview_text, thread_id, thread_item_count, meeting_request_status, message_class, smime_type, scheduled_sending_time, remaining_resend_times, tag_id, eas_data_length, eas_data ";
2790         char *select_query_form = "SELECT %s FROM mail_tbl ";
2791         char *target_field = NULL;
2792         char *sql_query_string = NULL;
2793         email_mail_list_item_t *mail_list_item_from_tbl = NULL;
2794         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2795
2796         EM_IF_NULL_RETURN_VALUE(conditional_clause, false);
2797         EM_IF_NULL_RETURN_VALUE(result_count, false);
2798
2799         EMSTORAGE_START_READ_TRANSACTION(transaction);
2800
2801         /*  select clause */
2802         if (to_get_count) /*  count only */
2803                 target_field = field_mail_id;
2804         else /* mail list in plain form */
2805                 target_field = field_all;
2806
2807         sql_query_string_length = EM_SAFE_STRLEN(select_query_form) + EM_SAFE_STRLEN(target_field) + EM_SAFE_STRLEN(conditional_clause);
2808
2809         if(sql_query_string_length)
2810                 sql_query_string = em_malloc(sql_query_string_length);
2811
2812         if(sql_query_string == NULL) {
2813                 EM_DEBUG_EXCEPTION("em_malloc failed...");
2814                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2815                 goto FINISH_OFF;
2816         }
2817
2818         cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, sql_query_string_length, select_query_form, target_field);
2819
2820         strncat(sql_query_string, conditional_clause, sql_query_string_length - cur_query);
2821
2822         EM_DEBUG_LOG_DEV ("query[%s]", sql_query_string);
2823
2824         /*  performing query */
2825         EM_PROFILE_BEGIN(emstorage_query_mail_list_performing_query);
2826         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2827         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2828                 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2829         EM_PROFILE_END(emstorage_query_mail_list_performing_query);
2830
2831         if (!base_count)
2832                 base_count = ({ 
2833                                 int i=0; 
2834                                 char *tmp = NULL; 
2835                                 for (tmp = field_all; tmp && *(tmp + 1); tmp = index(tmp + 1, ',')) i++ ;
2836                                 i; 
2837                                 });
2838
2839         col_index = base_count;
2840
2841         EM_DEBUG_LOG_DEV("base_count [%d]", base_count);
2842
2843         if (to_get_count) {
2844                 /*  to get count */
2845                 if (!count) {
2846                         EM_DEBUG_LOG_DEV ("No mail found...");
2847                         ret = false;
2848                         error= EMAIL_ERROR_MAIL_NOT_FOUND;
2849                         goto FINISH_OFF;
2850                 }
2851                 EM_DEBUG_LOG_DEV ("There are [%d] mails.", count);
2852         }
2853         else {
2854                 /*  to get mail list */
2855                 if (!count) {
2856                         EM_DEBUG_LOG_DEV ("No mail found...");
2857                         ret = false;
2858                         error= EMAIL_ERROR_MAIL_NOT_FOUND;
2859                         goto FINISH_OFF;
2860                 }
2861
2862                 EM_DEBUG_LOG_DEV ("There are [%d] mails.", count);
2863                 if (!(mail_list_item_from_tbl = (email_mail_list_item_t*)em_malloc(sizeof(email_mail_list_item_t) * count))) {
2864                         EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
2865                         error = EMAIL_ERROR_OUT_OF_MEMORY;
2866                         goto FINISH_OFF;
2867                 }
2868
2869                 EM_PROFILE_BEGIN(emstorage_query_mail_list_loop);
2870                 EM_DEBUG_LOG_DEV (">>>> DATA ASSIGN START >> ");
2871                 for (i = 0; i < count; i++) {
2872                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_id), col_index++);
2873                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].account_id), col_index++);
2874                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mailbox_id), col_index++);
2875                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].mailbox_type), col_index++);
2876                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].full_address_from, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2877                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_sender, MAX_EMAIL_ADDRESS_LENGTH, 1, col_index++);
2878                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_recipient, STRING_LENGTH_FOR_DISPLAY,  1, col_index++);
2879                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].subject, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2880                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].body_download_status), col_index++);
2881                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_size), col_index++);
2882                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_seen_field), col_index++);
2883                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_deleted_field), col_index++);
2884                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_flagged_field), col_index++);
2885                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_answered_field), col_index++);
2886                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_recent_field), col_index++);
2887                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_draft_field), col_index++);
2888                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_forwarded_field), col_index++);
2889                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].DRM_status), col_index++);
2890                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].priority), col_index++);
2891                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].save_status), col_index++);
2892                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].lock_status), col_index++);
2893                         _get_table_field_data_int(result, &local_attachment_count, col_index++);
2894                         _get_table_field_data_int(result, &local_inline_content_count, col_index++);
2895                         _get_table_field_data_time_t(result, &(mail_list_item_from_tbl[i].date_time), col_index++);
2896                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].preview_text, MAX_PREVIEW_TEXT_LENGTH, 1, col_index++);
2897                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_id), col_index++);
2898                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_item_count), col_index++);
2899                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].meeting_request_status), col_index++);
2900                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].message_class), col_index++);
2901                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].smime_type), col_index++);
2902                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].scheduled_sending_time), col_index++);
2903                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].remaining_resend_times), col_index++);
2904                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].tag_id), col_index++);
2905                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].eas_data_length), col_index++);
2906                         _get_table_field_data_blob(result, (void**)&(mail_list_item_from_tbl[i].eas_data), mail_list_item_from_tbl[i].eas_data_length, col_index++);
2907
2908                         mail_list_item_from_tbl[i].attachment_count = (local_attachment_count > 0) ? 1:0;
2909                 }
2910                 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >> ", count);
2911                 EM_PROFILE_END(emstorage_query_mail_list_loop);
2912         }
2913
2914         ret = true;
2915
2916 FINISH_OFF:
2917         EM_DEBUG_LOG("MAIL_COUNT [%d]", count);
2918
2919         if(result)
2920                 sqlite3_free_table(result);
2921
2922         if (to_get_count)
2923                 *result_count = count;
2924         else {
2925                 if (ret == true)  {
2926                         if (result_mail_list)
2927                                 *result_mail_list = mail_list_item_from_tbl;
2928                         *result_count = count;
2929                 }
2930                 else
2931                         EM_SAFE_FREE(mail_list_item_from_tbl);
2932         }
2933
2934         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2935
2936 //      sqlite3_db_release_memory(local_db_handle);
2937
2938         EM_SAFE_FREE(sql_query_string);
2939         EM_SAFE_FREE(date_time_string);
2940
2941         if (err_code != NULL)
2942                 *err_code = error;
2943
2944         EM_PROFILE_END(emstorage_query_mail_list_func);
2945         EM_DEBUG_FUNC_END("ret [%d]", ret);
2946         return ret;
2947 }
2948
2949
2950 INTERNAL_FUNC int emstorage_query_mail_tbl(char *multi_user_name, const char *conditional_clause, int transaction, emstorage_mail_tbl_t** result_mail_tbl, int *result_count, int *err_code)
2951 {
2952         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);
2953
2954         if (!conditional_clause || !result_mail_tbl || !result_count) {
2955                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: [%p] [%p] [%p]", conditional_clause, result_mail_tbl, result_mail_tbl);
2956                 if (err_code != NULL)
2957                         *err_code = EMAIL_ERROR_INVALID_PARAM;
2958                 return false;
2959         }
2960
2961         int i, col_index = FIELD_COUNT_OF_MAIL_TBL, rc, ret = false, count;
2962         int error = EMAIL_ERROR_NONE;
2963         char **result = NULL, sql_query_string[QUERY_SIZE] = {0, };
2964         emstorage_mail_tbl_t* p_data_tbl = NULL;
2965         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2966
2967         EMSTORAGE_START_READ_TRANSACTION(transaction);
2968
2969         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl %s", conditional_clause);
2970
2971         EM_DEBUG_LOG_DEV ("Query[%s]", sql_query_string);
2972
2973         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2974         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
2975                 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2976
2977         if (!count) {
2978                 EM_DEBUG_LOG("No mail found...");
2979                 ret = false;
2980                 error= EMAIL_ERROR_MAIL_NOT_FOUND;
2981                 goto FINISH_OFF;
2982         }
2983
2984         EM_DEBUG_LOG("There are [%d] mails.", count);
2985         if (!(p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * count))) {
2986                 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
2987                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2988                 goto FINISH_OFF;
2989         }
2990
2991         EM_DEBUG_LOG_DEV (">>>> DATA ASSIGN START >> ");
2992         for (i = 0; i < count; i++) {
2993                 _get_table_field_data_int   (result, &(p_data_tbl[i].mail_id), col_index++);
2994                 _get_table_field_data_int   (result, &(p_data_tbl[i].account_id), col_index++);
2995                 _get_table_field_data_int   (result, &(p_data_tbl[i].mailbox_id), col_index++);
2996                 _get_table_field_data_int   (result, &(p_data_tbl[i].mailbox_type), col_index++);
2997                 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 1, col_index++);
2998                 _get_table_field_data_time_t (result, &(p_data_tbl[i].date_time), col_index++);
2999                 _get_table_field_data_int   (result, &(p_data_tbl[i].server_mail_status), col_index++);
3000                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
3001                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
3002                 _get_table_field_data_string(result, &(p_data_tbl[i].message_id), 0, col_index++);
3003                 _get_table_field_data_int   (result, &(p_data_tbl[i].reference_mail_id), col_index++);
3004                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_from), 1, col_index++);
3005                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_reply), 1, col_index++);
3006                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_to), 1, col_index++);
3007                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_cc), 1, col_index++);
3008                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_bcc), 1, col_index++);
3009                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_return), 1, col_index++);
3010                 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_sender), 1, col_index++);
3011                 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_recipient), 1, col_index++);
3012                 _get_table_field_data_string(result, &(p_data_tbl[i].alias_sender), 1, col_index++);
3013                 _get_table_field_data_string(result, &(p_data_tbl[i].alias_recipient), 1, col_index++);
3014                 _get_table_field_data_int   (result, &(p_data_tbl[i].body_download_status), col_index++);
3015                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
3016                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
3017                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
3018                 _get_table_field_data_int   (result, &(p_data_tbl[i].mail_size), col_index++);
3019                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_seen_field), col_index++);
3020                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_deleted_field), col_index++);
3021                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_flagged_field), col_index++);
3022                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_answered_field), col_index++);
3023                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_recent_field), col_index++);
3024                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_draft_field), col_index++);
3025                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_forwarded_field), col_index++);
3026                 _get_table_field_data_int   (result, &(p_data_tbl[i].DRM_status), col_index++);
3027                 _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].priority), col_index++);
3028                 _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].save_status), col_index++);
3029                 _get_table_field_data_int   (result, &(p_data_tbl[i].lock_status), col_index++);
3030                 _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].report_status), col_index++);
3031                 _get_table_field_data_int   (result, &(p_data_tbl[i].attachment_count), col_index++);
3032                 _get_table_field_data_int   (result, &(p_data_tbl[i].inline_content_count), col_index++);
3033                 _get_table_field_data_int   (result, &(p_data_tbl[i].thread_id), col_index++);
3034                 _get_table_field_data_int   (result, &(p_data_tbl[i].thread_item_count), col_index++);
3035                 _get_table_field_data_string(result, &(p_data_tbl[i].preview_text), 1, col_index++);
3036                 _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].meeting_request_status), col_index++);
3037                 _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].message_class), col_index++);
3038                 _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].digest_type), col_index++);
3039                 _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].smime_type), col_index++);
3040                 _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].scheduled_sending_time), col_index++);
3041                 _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].remaining_resend_times), col_index++);
3042                 _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].tag_id), col_index++);
3043                 _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].replied_time), col_index++);
3044                 _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].forwarded_time), col_index++);
3045                 _get_table_field_data_string(result, &(p_data_tbl[i].default_charset), 0, col_index++);
3046                 _get_table_field_data_int   (result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3047                 _get_table_field_data_blob  (result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3048                 _get_table_field_data_string(result, &(p_data_tbl[i].user_name), 0, col_index++);
3049         }
3050
3051         ret = true;
3052
3053 FINISH_OFF:
3054         if(result)
3055                 sqlite3_free_table(result);
3056
3057         if (ret)  {
3058                 *result_mail_tbl = p_data_tbl;
3059                 *result_count = count;
3060         }
3061         else {
3062                 *result_mail_tbl = NULL;
3063                 *result_count = 0;
3064         }
3065
3066         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3067
3068 //      sqlite3_db_release_memory(local_db_handle);
3069
3070         if (err_code != NULL)
3071                 *err_code = error;
3072
3073         EM_DEBUG_FUNC_END("ret [%d]", ret);
3074         return ret;
3075 }
3076
3077 #ifdef __FEATURE_BODY_SEARCH__
3078 INTERNAL_FUNC int emstorage_query_mail_text_tbl(char *multi_user_name, const char *conditional_clause, int transaction, emstorage_mail_text_tbl_t** result_mail_text_tbl, int *result_count, int *err_code)
3079 {
3080         EM_DEBUG_FUNC_BEGIN("conditional_clause[%s], result_mail_text_tbl[%p], result_count [%p], transaction[%d], err_code[%p]", conditional_clause, result_mail_text_tbl, result_count, transaction, err_code);
3081
3082         if (!conditional_clause || !result_mail_text_tbl || !result_count) { /*prevent 50930*/
3083                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3084                 if (err_code != NULL)
3085                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3086                 return false;
3087         }
3088
3089         int i = 0;
3090         int rc = 0;
3091         int ret = false;
3092         int count = 0;
3093         int col_index = FIELD_COUNT_OF_MAIL_TEXT_TBL;
3094         int error = EMAIL_ERROR_NONE;
3095         char **result = NULL;
3096         char sql_query_string[QUERY_SIZE] = {0, };
3097         emstorage_mail_text_tbl_t* p_data_tbl = NULL;
3098         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3099
3100         EMSTORAGE_START_READ_TRANSACTION(transaction);
3101
3102         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_text_tbl %s", conditional_clause);
3103
3104         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
3105
3106         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3107         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3108                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3109
3110         if (!count) {
3111                 EM_DEBUG_LOG("No mail found...");
3112                 ret = false;
3113                 error= EMAIL_ERROR_MAIL_NOT_FOUND;
3114                 goto FINISH_OFF;
3115         }
3116
3117         EM_DEBUG_LOG("There are [%d] mails.", count);
3118         if (!(p_data_tbl = (emstorage_mail_text_tbl_t *)em_malloc(sizeof(emstorage_mail_text_tbl_t) * count))) {
3119                 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_text_tbl_t failed...");
3120                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3121                 goto FINISH_OFF;
3122         }
3123
3124         EM_DEBUG_LOG_DEV (">>>> DATA ASSIGN START >>");
3125         for (i = 0; i < count; i++) {
3126                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
3127                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3128                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3129                 _get_table_field_data_string(result, &(p_data_tbl[i].body_text), 0, col_index++);
3130         }
3131
3132         ret = true;
3133
3134 FINISH_OFF:
3135         if(result)
3136                 sqlite3_free_table(result);
3137
3138         if (ret == true) {
3139                 *result_mail_text_tbl = p_data_tbl;
3140                 *result_count = count;
3141         }
3142         else
3143                 EM_SAFE_FREE(p_data_tbl);
3144
3145         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3146
3147 //      sqlite3_db_release_memory(local_db_handle);
3148
3149         if (err_code != NULL)
3150                 *err_code = error;
3151
3152         EM_DEBUG_FUNC_END("ret [%d]", ret);
3153         return ret;
3154 }
3155 #endif
3156
3157 INTERNAL_FUNC int emstorage_query_mailbox_tbl(char *multi_user_name, const char *input_conditional_clause, const char *input_ordering_clause, int input_get_mail_count,  int input_transaction, emstorage_mailbox_tbl_t **output_mailbox_list, int *output_mailbox_count)
3158 {
3159         EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_ordering_clause [%p], input_get_mail_count[%d], input_transaction[%d], output_mailbox_list[%p], output_mailbox_count[%d]", input_conditional_clause, input_ordering_clause, input_get_mail_count, input_transaction, output_mailbox_list, output_mailbox_count);
3160
3161         int i = 0;
3162         int rc;
3163         int count = 0;
3164         int col_index = 0;
3165         int error = EMAIL_ERROR_NONE;
3166         int read_count = 0;
3167         int total_count = 0;
3168         char **result;
3169         char sql_query_string[QUERY_SIZE] = {0, };
3170         char *fields = "MBT.mailbox_id, MBT.account_id, local_yn, MBT.mailbox_name, MBT.mailbox_type, alias, deleted_flag, modifiable_yn, total_mail_count_on_server, has_archived_mails, mail_slot_size, no_select, last_sync_time, MBT.eas_data_length, MBT.eas_data ";
3171         emstorage_mailbox_tbl_t* p_data_tbl = NULL;
3172         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3173
3174         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
3175
3176         if (input_get_mail_count == 0) {        /* without mail count */
3177                 col_index = 15;
3178                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT %s FROM mail_box_tbl AS MBT %s %s", fields, input_conditional_clause, input_ordering_clause);
3179         }
3180         else {  /* with read count and total count */
3181                 col_index = 17;
3182                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT %s, total, read  FROM mail_box_tbl AS MBT LEFT OUTER JOIN (SELECT mailbox_id, count(mail_id) AS total, SUM(flags_seen_field) AS read FROM mail_tbl WHERE flags_deleted_field = 0 GROUP BY mailbox_id) AS MT ON MBT.mailbox_id = MT.mailbox_id %s %s", fields, input_conditional_clause, input_ordering_clause);
3183         }
3184
3185         EM_DEBUG_LOG_DEV("query[%s]", sql_query_string);
3186
3187         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
3188         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
3189                 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string))
3190
3191         EM_DEBUG_LOG_DEV("result count [%d]", count);
3192
3193         if(count == 0) {
3194                 EM_DEBUG_LOG_SEC ("Can't find mailbox query[%s]", sql_query_string);
3195                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
3196                 goto FINISH_OFF;
3197         }
3198
3199         if ((p_data_tbl = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t) * count)) == NULL) {
3200                 EM_DEBUG_EXCEPTION("em_malloc failed...");
3201                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3202                 goto FINISH_OFF;
3203         }
3204
3205         for (i = 0; i < count; i++)  {
3206                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3207                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3208                 _get_table_field_data_int(result, &(p_data_tbl[i].local_yn), col_index++);
3209                 _get_table_field_data_string(result, &(p_data_tbl[i].mailbox_name), 0, col_index++);
3210                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].mailbox_type), col_index++);
3211                 _get_table_field_data_string(result, &(p_data_tbl[i].alias), 0, col_index++);
3212                 _get_table_field_data_int(result, &(p_data_tbl[i].deleted_flag), col_index++);
3213                 _get_table_field_data_int(result, &(p_data_tbl[i].modifiable_yn), col_index++);
3214                 _get_table_field_data_int(result, &(p_data_tbl[i].total_mail_count_on_server), col_index++);
3215                 _get_table_field_data_int(result, &(p_data_tbl[i].has_archived_mails), col_index++);
3216                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_slot_size), col_index++);
3217                 _get_table_field_data_int(result, &(p_data_tbl[i].no_select), col_index++);
3218                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].last_sync_time), col_index++);
3219                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3220                 _get_table_field_data_blob(result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3221
3222                 if (input_get_mail_count == 1) {
3223                         _get_table_field_data_int(result, &(total_count), col_index++);
3224                         p_data_tbl[i].total_mail_count_on_local = total_count;
3225                         _get_table_field_data_int(result, &(read_count), col_index++);
3226                         p_data_tbl[i].unread_count = total_count - read_count;                  /*  return unread count, NOT  */
3227                 }
3228         }
3229
3230
3231 FINISH_OFF:
3232         if (result)
3233                 sqlite3_free_table(result);
3234
3235         if (error == EMAIL_ERROR_NONE)  {
3236                 *output_mailbox_list = p_data_tbl;
3237                 *output_mailbox_count = count;
3238                 EM_DEBUG_LOG("Mailbox Count [%d]", count);
3239         }
3240
3241         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
3242
3243 //      sqlite3_db_release_memory(local_db_handle);
3244
3245         EM_DEBUG_FUNC_END("error [%d]", error);
3246         return error;
3247 }
3248
3249 /* Query series --------------------------------------------------------------*/
3250 INTERNAL_FUNC int emstorage_check_duplicated_account(char *multi_user_name, email_account_t* account, int transaction, int *err_code)
3251 {
3252         EM_DEBUG_FUNC_BEGIN();
3253         int rc = -1, ret = false;
3254         int error = EMAIL_ERROR_NONE;
3255         char **result;
3256         int count;
3257         char sql_query_string[QUERY_SIZE] = {0, };
3258         sqlite3 *local_db_handle = NULL;
3259
3260         if (account == NULL || account->user_email_address == NULL || account->incoming_server_user_name == NULL || account->incoming_server_address == NULL||
3261                 account->outgoing_server_user_name == NULL || account->outgoing_server_address == NULL)  {
3262                 if (err_code != NULL)
3263                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3264                 return false;
3265         }
3266
3267         local_db_handle = emstorage_get_db_connection(multi_user_name);
3268
3269         EMSTORAGE_START_READ_TRANSACTION(transaction);
3270
3271         SNPRINTF(sql_query_string, sizeof(sql_query_string),
3272                 "SELECT COUNT(*) FROM mail_account_tbl "
3273                 " WHERE "
3274                 " user_email_address = '%s' AND "
3275                 " incoming_server_user_name = '%s' AND "
3276                 " incoming_server_type = %d AND "
3277                 " incoming_server_address = '%s' AND "
3278                 " outgoing_server_user_name = '%s' AND "
3279                 " outgoing_server_type = %d AND "
3280                 " outgoing_server_address = '%s'; ",
3281                 account->user_email_address,
3282                 account->incoming_server_user_name, account->incoming_server_type, account->incoming_server_address,
3283                 account->outgoing_server_user_name, account->outgoing_server_type, account->outgoing_server_address
3284         );
3285         EM_DEBUG_LOG_SEC("Query[%s]", sql_query_string);
3286         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
3287         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
3288                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3289
3290         count = atoi(result[1]);
3291         sqlite3_free_table(result);
3292
3293         EM_DEBUG_LOG("Count of Duplicated Account Information: [%d]", count);
3294
3295         if (count == 0) {       /*  not duplicated account */
3296                 ret = true;
3297                 EM_DEBUG_LOG_SEC("NOT duplicated account: user_email_address[%s]", account->user_email_address);
3298         }
3299         else {  /*  duplicated account */
3300                 ret = false;
3301                 EM_DEBUG_LOG_SEC("The same account already exists. Duplicated account: user_email_address[%s]", account->user_email_address);
3302                 error = EMAIL_ERROR_ALREADY_EXISTS;
3303         }
3304
3305 FINISH_OFF:
3306
3307         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3308
3309         if (err_code != NULL)
3310                 *err_code = error;
3311
3312         EM_DEBUG_FUNC_END("ret [%d]", ret);
3313         return ret;
3314
3315 }
3316
3317 INTERNAL_FUNC int emstorage_get_account_count(char *multi_user_name, int *count, int transaction, int *err_code)
3318 {
3319         EM_DEBUG_FUNC_BEGIN();
3320
3321         if (!count)  {
3322                 if (err_code != NULL)
3323                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3324                 return false;
3325         }
3326
3327         int rc = -1, ret = false;
3328         int error = EMAIL_ERROR_NONE;
3329
3330         DB_STMT hStmt = NULL;
3331         char sql_query_string[QUERY_SIZE] = {0, };
3332
3333
3334         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3335         EMSTORAGE_START_READ_TRANSACTION(transaction);
3336         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_account_tbl");
3337         EM_DEBUG_LOG_SEC("SQL STMT [%s]", sql_query_string);
3338         EMSTORAGE_PROTECTED_FUNC_CALL (sqlite3_prepare_v2 (local_db_handle, sql_query_string,
3339                                                           EM_SAFE_STRLEN (sql_query_string), &hStmt, NULL), rc);
3340         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
3341         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3342                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3343
3344         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3345         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3346                 ("sqlite3_step fail:%d", rc));
3347
3348         *count = sqlite3_column_int(hStmt, 0);
3349
3350         ret = true;
3351
3352 FINISH_OFF:
3353
3354         if (hStmt != NULL)  {
3355                 rc = sqlite3_finalize(hStmt);
3356                 hStmt=NULL;
3357                 if (rc != SQLITE_OK)  {
3358                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
3359                         error = EMAIL_ERROR_DB_FAILURE;
3360                 }
3361         }
3362
3363         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3364
3365         if (err_code != NULL)
3366                 *err_code = error;
3367
3368         EM_DEBUG_FUNC_END("ret [%d]", ret);
3369         return ret;
3370 }
3371
3372 INTERNAL_FUNC int emstorage_get_account_list(char *multi_user_name, int *select_num, emstorage_account_tbl_t **account_list, int transaction, int with_password, int *err_code)
3373 {
3374         EM_DEBUG_FUNC_BEGIN();
3375
3376         int i = 0, count = 0, rc = -1, ret = false;
3377         int field_index = 0;
3378         int sql_len = 0;
3379         int error = EMAIL_ERROR_NONE;
3380         emstorage_account_tbl_t *p_data_tbl = NULL;
3381         char sql_query_string[QUERY_SIZE] = {0, };
3382         char *sql = "SELECT count(*) FROM mail_account_tbl;";
3383         char **result;
3384         sqlite3 *local_db_handle = NULL;
3385         DB_STMT hStmt = NULL;
3386
3387         if (!select_num || !account_list)  {
3388                 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, account_list);
3389                 if (err_code != NULL)
3390                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3391                 return false;
3392         }
3393
3394         local_db_handle = emstorage_get_db_connection(multi_user_name);
3395
3396         EMSTORAGE_START_READ_TRANSACTION(transaction);
3397
3398         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
3399         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
3400                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
3401
3402         count = atoi(result[1]);
3403         sqlite3_free_table(result);
3404
3405         EM_DEBUG_LOG_DEV("count = %d", rc);
3406
3407         if (count == 0) {
3408                 EM_DEBUG_LOG("no account found...");
3409                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3410                 ret = true;
3411                 goto FINISH_OFF;
3412         }
3413         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
3414         sql_len = EM_SAFE_STRLEN(sql_query_string);
3415
3416         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
3417                 "account_id, "
3418                 "account_name, "
3419                 "logo_icon_path, "
3420                 "user_data, "
3421                 "user_data_length, "
3422                 "account_svc_id, "
3423                 "sync_status, "
3424                 "sync_disabled, "
3425                 "default_mail_slot_size, "
3426                 "roaming_option, "
3427                 "color_label, "
3428                 "user_display_name, "
3429                 "user_email_address, "
3430                 "reply_to_address, "
3431                 "return_address, "
3432                 "incoming_server_type, "
3433                 "incoming_server_address, "
3434                 "incoming_server_port_number, "
3435                 "incoming_server_user_name, "
3436                 "incoming_server_password, "
3437                 "incoming_server_secure_connection, "
3438                 "incoming_server_authentication_method, "
3439                 "retrieval_mode, "
3440                 "keep_mails_on_pop_server_after_download, "
3441                 "check_interval, "
3442                 "auto_download_size, "
3443                 "peak_interval, "
3444                 "peak_days, "
3445                 "peak_start_time, "
3446                 "peak_end_time, "
3447                 "outgoing_server_type, "
3448                 "outgoing_server_address, "
3449                 "outgoing_server_port_number, "
3450                 "outgoing_server_user_name, "
3451                 "outgoing_server_password, "
3452                 "outgoing_server_secure_connection, "
3453                 "outgoing_server_need_authentication, "
3454                 "outgoing_server_use_same_authenticator, "
3455                 "priority, "
3456                 "keep_local_copy, "
3457                 "req_delivery_receipt, "
3458                 "req_read_receipt, "
3459                 "download_limit, "
3460                 "block_address, "
3461                 "block_subject, "
3462                 "display_name_from, "
3463                 "reply_with_body, "
3464                 "forward_with_files, "
3465                 "add_myname_card, "
3466                 "add_signature, "
3467                 "signature, "
3468                 "add_my_address_to_bcc, "
3469                 "notification_status, "
3470                 "vibrate_status, "
3471                 "display_content_status, "
3472                 "default_ringtone_status, "
3473                 "alert_ringtone_path, "
3474                 "auto_resend_times, "
3475                 "outgoing_server_size_limit, "
3476                 "wifi_auto_download, "
3477                 "pop_before_smtp, "
3478                 "incoming_server_requires_apop, "
3479                 "smime_type, "
3480                 "certificate_path, "
3481                 "cipher_type, "
3482                 "digest_type"
3483         );
3484
3485         sql_len = EM_SAFE_STRLEN(sql_query_string);
3486
3487         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " FROM mail_account_tbl ORDER BY account_id");
3488
3489         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3490
3491         EM_DEBUG_LOG_DEV ("After sqlite3_prepare_v2 hStmt = %p", hStmt);
3492         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3493                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3494
3495         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3496         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3497                 ("sqlite3_step fail:%d", rc));
3498         if (rc == SQLITE_DONE)  {
3499                 EM_DEBUG_EXCEPTION("no account found...");
3500
3501                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3502                 count = 0;
3503                 ret = true;
3504                 goto FINISH_OFF;
3505         }
3506
3507         if (!(p_data_tbl = (emstorage_account_tbl_t*)malloc(sizeof(emstorage_account_tbl_t) * count)))  {
3508                 EM_DEBUG_EXCEPTION("malloc failed...");
3509                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3510                 goto FINISH_OFF;
3511         }
3512         memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * count);
3513         for (i = 0; i < count; i++)  {
3514                 /*  get recordset */
3515                 field_index = 0;
3516                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_id), field_index++);
3517                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].account_name), 0, field_index++);
3518                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].logo_icon_path), 0, field_index++);
3519                 _get_stmt_field_data_blob(hStmt, &(p_data_tbl[i].user_data), field_index++);
3520                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].user_data_length), field_index++);
3521                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_svc_id), field_index++);
3522                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_status), field_index++);
3523                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_disabled), field_index++);
3524                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].default_mail_slot_size), field_index++);
3525                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].roaming_option), field_index++);
3526                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].color_label), field_index++);
3527                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_display_name), 0, field_index++);
3528                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_email_address), 0, field_index++);
3529                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].reply_to_address), 0, field_index++);
3530                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].return_address), 0, field_index++);
3531                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].incoming_server_type), field_index++);
3532                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_address), 0, field_index++);
3533                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_port_number), field_index++);
3534                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_user_name), 0, field_index++);
3535                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_password), 0, field_index++);
3536                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_secure_connection), field_index++);
3537                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].incoming_server_authentication_method), field_index++);
3538                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].retrieval_mode), field_index++);
3539                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].keep_mails_on_pop_server_after_download), field_index++);
3540                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].check_interval), field_index++);
3541                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_download_size), field_index++);
3542                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_interval), field_index++);
3543                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_days), field_index++);
3544                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_start_time), field_index++);
3545                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_end_time), field_index++);
3546                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].outgoing_server_type), field_index++);
3547                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_address), 0, field_index++);
3548                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_port_number), field_index++);
3549                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_user_name), 0, field_index++);
3550                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_password), 0, field_index++);
3551                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_secure_connection), field_index++);
3552                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_need_authentication), field_index++);
3553                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_use_same_authenticator), field_index++);
3554                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.priority), field_index++);
3555                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.keep_local_copy), field_index++);
3556                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_delivery_receipt), field_index++);
3557                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_read_receipt), field_index++);
3558                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.download_limit), field_index++);
3559                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_address), field_index++);
3560                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_subject), field_index++);
3561                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.display_name_from), 0, field_index++);
3562                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.reply_with_body), field_index++);
3563                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.forward_with_files), field_index++);
3564                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_myname_card), field_index++);
3565                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_signature), field_index++);
3566                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.signature), 0, field_index++);
3567                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.add_my_address_to_bcc), field_index++);
3568                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.notification_status), field_index++);
3569                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.vibrate_status), field_index++);
3570                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.display_content_status), field_index++);
3571                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.default_ringtone_status), field_index++);
3572                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.alert_ringtone_path), 0, field_index++);
3573                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_resend_times), field_index++);
3574                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_size_limit), field_index++);
3575                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].wifi_auto_download), field_index++);
3576                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].pop_before_smtp), field_index++);
3577                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_requires_apop), field_index++);
3578                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].smime_type), field_index++);
3579                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].certificate_path), 0, field_index++);
3580                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].cipher_type), field_index++);
3581                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].digest_type), field_index++);
3582
3583                 /* EAS passwd is not accessible */
3584                 if (with_password == true && p_data_tbl[i].incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
3585                         /*  get password from the secure storage */
3586                         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3587                         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3588
3589                         EM_SAFE_FREE(p_data_tbl[i].incoming_server_password);
3590                         EM_SAFE_FREE(p_data_tbl[i].outgoing_server_password);
3591
3592                         /*  get password file name */
3593                         error = _get_password_file_name (multi_user_name, p_data_tbl[i].account_id, 
3594                                                    recv_password_file_name, 
3595                                                    send_password_file_name);
3596                         if (error != EMAIL_ERROR_NONE){
3597                                 EM_DEBUG_EXCEPTION("_get_password_file_name error [%d]", error);
3598                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3599                                 goto FINISH_OFF;
3600                         }
3601
3602                         /*  read password from secure storage */
3603                         error = _read_password_from_secure_storage (recv_password_file_name,
3604                                           &(p_data_tbl[i].incoming_server_password));
3605                         if (error < 0 ) {
3606                                 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s] error [%d]",
3607                                                     recv_password_file_name, error);
3608                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3609                                 goto FINISH_OFF;
3610                         }
3611                         error = _read_password_from_secure_storage (send_password_file_name,
3612                                           &(p_data_tbl[i].outgoing_server_password));
3613                         if (error < 0) {
3614                                 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s]  error [%d]",
3615                                                      send_password_file_name, error);
3616                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3617                                 goto FINISH_OFF;
3618                         }
3619                 }
3620
3621                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3622                 EM_DEBUG_LOG_DEV("after sqlite3_step(), i = %d, rc = %d.", i,  rc);
3623                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
3624                         ("sqlite3_step fail:%d", rc));
3625         }
3626
3627         ret = true;
3628
3629 FINISH_OFF:
3630         if (ret == true)  {
3631                 *account_list = p_data_tbl;
3632                 *select_num = count;
3633                 EM_DEBUG_LOG_DEV("COUNT : %d", count);
3634         }
3635         else if (p_data_tbl != NULL)
3636                 emstorage_free_account(&p_data_tbl, count, NULL);
3637
3638         if (hStmt != NULL)  {
3639                 rc = sqlite3_finalize(hStmt);
3640                 hStmt = NULL;
3641                 if (rc != SQLITE_OK)  {
3642                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
3643                         error = EMAIL_ERROR_DB_FAILURE;
3644                 }
3645         }
3646
3647         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3648
3649         if (err_code != NULL)
3650                 *err_code = error;
3651
3652         EM_DEBUG_FUNC_END("ret [%d]", ret);
3653         return ret;
3654 }
3655
3656 INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(char *multi_user_name, int mailbox_id, char *server_mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code)
3657 {
3658         EM_DEBUG_FUNC_BEGIN_SEC("mailbox_id [%d], server_mail_id[%s], mail[%p], transaction[%d], err_code[%p]", mailbox_id, server_mail_id, mail, transaction, err_code);
3659
3660         int ret = false, error = EMAIL_ERROR_NONE, result_count;
3661         char conditional_clause[QUERY_SIZE] = {0, };
3662         emstorage_mail_tbl_t* p_data_tbl = NULL;
3663
3664         if (!server_mail_id || !mail) {
3665                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3666                 error = EMAIL_ERROR_INVALID_PARAM;
3667                 goto FINISH_OFF;
3668         }
3669
3670         if (mailbox_id == 0)
3671                 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE UPPER(server_mail_id) =UPPER('%s')", server_mail_id);
3672         else
3673                 SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE UPPER(server_mail_id) =UPPER('%s') AND mailbox_id = %d", server_mail_id, mailbox_id);
3674
3675         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
3676
3677         if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &result_count, &error)) {
3678                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3679                 goto FINISH_OFF;
3680         }
3681
3682         ret = true;
3683
3684 FINISH_OFF:
3685         if (ret == true)
3686                 *mail = p_data_tbl;
3687
3688         if (err_code != NULL)
3689                 *err_code = error;
3690
3691         EM_DEBUG_FUNC_END("ret [%d]", ret);
3692         return ret;
3693 }
3694
3695
3696 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)
3697 {
3698         int cur_clause = 0, conditional_clause_count = 0, i = 0;
3699
3700         if (account_id < ALL_ACCOUNT) {
3701                 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3702                 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3703         }
3704
3705         /*  where clause */
3706         if (account_id != ALL_ACCOUNT) {
3707                 cur_clause += (conditional_clause_count++ == 0)?
3708                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE account_id = %d", account_id):
3709                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND account_id = %d", account_id);
3710         }
3711
3712         if (mailbox_id > 0) {
3713                 cur_clause += (conditional_clause_count++ == 0)?
3714                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_id = %d", mailbox_id):
3715                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_id = %d", mailbox_id);
3716         }
3717
3718         if (thread_id > 0) {
3719                 cur_clause += (conditional_clause_count++ == 0)?
3720                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_id = %d ", thread_id):
3721                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_id = %d ", thread_id);
3722         }
3723         else if (thread_id == EMAIL_LIST_TYPE_THREAD)   {
3724                 cur_clause += (conditional_clause_count++ == 0)?
3725                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_item_count > 0"):
3726                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_item_count > 0");
3727         }
3728         else if (thread_id == EMAIL_LIST_TYPE_LOCAL) {
3729                 cur_clause += (conditional_clause_count++ == 0)?
3730                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE server_mail_status = 0"):
3731                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND server_mail_status = 0");
3732         }
3733         else if (thread_id == EMAIL_LIST_TYPE_UNREAD) {
3734                 cur_clause += (conditional_clause_count++ == 0)?
3735                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_seen_field == 0"):
3736                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_seen_field == 0");
3737         }
3738
3739         /*  EM_DEBUG_LOG("where clause added [%s]", conditional_clause_string); */
3740         if (addr_list && addr_list->address_count > 0) {
3741                 if (!addr_list->address_type) {
3742                         cur_clause += (conditional_clause_count++ == 0)?
3743                                 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE email_address_sender IN(\"%s\"", addr_list->address_list[0]):
3744                                 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND email_address_sender IN(\"%s\"", addr_list->address_list[0]);
3745
3746                         for (i = 1; i < addr_list->address_count; i++)
3747                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3748
3749                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3750                 } else {
3751                         cur_clause += (conditional_clause_count++ == 0)?
3752                                 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]):
3753                                 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]);
3754
3755                         for (i = 1; i < addr_list->address_count; i++)
3756                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3757
3758                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3759                 }
3760         }
3761
3762         if (input_except_delete_flagged_mails) {
3763                 cur_clause += (conditional_clause_count++ == 0)?
3764                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_deleted_field = 0"):
3765                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_deleted_field = 0");
3766         }
3767
3768         if (search_value) {
3769                 switch (search_type) {
3770                         case EMAIL_SEARCH_FILTER_SUBJECT:
3771                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3772                                         " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3773                                 break;
3774                         case EMAIL_SEARCH_FILTER_SENDER:
3775                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3776                                         " %s  ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3777                                         ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3778                                 break;
3779                         case EMAIL_SEARCH_FILTER_RECIPIENT:
3780                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3781                                         " %s ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3782                                         "       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3783                                         "       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3784                                         ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value);
3785                                 break;
3786                         case EMAIL_SEARCH_FILTER_ALL:
3787                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3788                                         " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\' "
3789                                         "       OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3790                                         "                       OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3791                                         "                       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3792                                         "                       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3793                                         "               ) "
3794                                         "       )"
3795                                         ")", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value, search_value, search_value);
3796                                 break;
3797                 }
3798         }
3799
3800         /*  EM_DEBUG_LOG("where clause [%s]", conditional_clause_string); */
3801         static char sorting_str[][50] = {
3802                          " ORDER BY date_time DESC",                                             /* case EMAIL_SORT_DATETIME_HIGH: */
3803                          " ORDER BY date_time ASC",                                               /* case EMAIL_SORT_DATETIME_LOW: */
3804                          " ORDER BY full_address_from DESC, date_time DESC", /* case EMAIL_SORT_SENDER_HIGH: */
3805                          " ORDER BY full_address_from ASC, date_time DESC",  /* case EMAIL_SORT_SENDER_LOW: */
3806                          " ORDER BY full_address_to DESC, date_time DESC",   /* case EMAIL_SORT_RCPT_HIGH: */
3807                          " ORDER BY full_address_to ASC, date_time DESC",       /* case EMAIL_SORT_RCPT_LOW: */
3808                          " ORDER BY subject DESC, date_time DESC",                 /* case EMAIL_SORT_SUBJECT_HIGH: */
3809                          " ORDER BY subject ASC, date_time DESC",                       /* case EMAIL_SORT_SUBJECT_LOW: */
3810                          " ORDER BY priority DESC, date_time DESC",               /* case EMAIL_SORT_PRIORITY_HIGH: */
3811                          " ORDER BY priority ASC, date_time DESC",                 /* case EMAIL_SORT_PRIORITY_LOW: */
3812                          " ORDER BY attachment_count DESC, date_time DESC",  /* case EMAIL_SORT_ATTACHMENT_HIGH: */
3813                          " ORDER BY attachment_count ASC, date_time DESC",   /* case EMAIL_SORT_ATTACHMENT_LOW: */
3814                          " ORDER BY lock_status DESC, date_time DESC",     /* case EMAIL_SORT_FAVORITE_HIGH: */
3815                          " ORDER BY lock_status ASC, date_time DESC",           /* case EMAIL_SORT_FAVORITE_LOW: */
3816                          " ORDER BY mailbox_id DESC, date_time DESC",     /* case EMAIL_SORT_MAILBOX_ID_HIGH: */
3817                          " ORDER BY mailbox_id ASC, date_time DESC",            /* case EMAIL_SORT_MAILBOX_ID_LOW: */
3818                          " ORDER BY flags_flagged_field DESC, date_time DESC",  /* case EMAIL_SORT_FLAGGED_FLAG_HIGH: */
3819                          " ORDER BY flags_flagged_field ASC, date_time DESC",   /* case EMAIL_SORT_FLAGGED_FLAG_LOW: */
3820                          " ORDER BY flags_seen_field DESC, date_time DESC",     /* case EMAIL_SORT_SEEN_FLAG_HIGH: */
3821                          " ORDER BY flags_seen_field ASC, date_time DESC"       /* case EMAIL_SORT_SEEN_FLAG_LOW: */
3822                          };
3823
3824         if (sorting < EMAIL_SORT_END && sorting >= 0)
3825                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " %s", sorting_str[sorting]);
3826         else
3827                 EM_DEBUG_LOG(" Invalid Sorting order ");
3828
3829         /*  limit clause */
3830         if (start_index != -1 && limit_count != -1)
3831                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " LIMIT %d, %d", start_index, limit_count);
3832
3833         return true;
3834 }
3835
3836
3837 /**
3838   *     emstorage_get_mail_list - Get the mail list information.
3839   *
3840   *
3841   */
3842 INTERNAL_FUNC int emstorage_get_mail_list(char *multi_user_name, int account_id, int mailbox_id, email_email_address_list_t* addr_list, int thread_id, int start_index, int limit_count, int search_type, const char *search_value, email_sort_type_t sorting, int transaction, email_mail_list_item_t** mail_list,  int *result_count,  int *err_code)
3843 {
3844         EM_DEBUG_FUNC_BEGIN();
3845         EM_PROFILE_BEGIN(emstorage_get_mail_list_func);
3846
3847         int ret = false, error = EMAIL_ERROR_NONE;
3848         char conditional_clause_string[QUERY_SIZE] = { 0, };
3849
3850         if (account_id < ALL_ACCOUNT) {
3851                 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3852                 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3853         }
3854         EM_IF_NULL_RETURN_VALUE(result_count, false);
3855
3856         _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);
3857
3858         EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
3859
3860         if(!emstorage_query_mail_list(multi_user_name, conditional_clause_string, transaction, mail_list, result_count, &error)) {
3861                 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
3862                 goto FINISH_OFF;
3863         }
3864
3865         ret = true;
3866
3867 FINISH_OFF:
3868         if (err_code != NULL)
3869                 *err_code = error;
3870
3871         EM_PROFILE_END(emstorage_get_mail_list_func);
3872         EM_DEBUG_FUNC_END("ret [%d]", ret);
3873         return ret;
3874 }
3875
3876
3877 /**
3878   *     emstorage_get_mails - Get the Mail list information based on mailbox_name name
3879   *
3880   *
3881   */
3882 INTERNAL_FUNC int emstorage_get_mails(char *multi_user_name, int account_id, int mailbox_id, email_email_address_list_t* addr_list, int thread_id, int start_index, int limit_count, email_sort_type_t sorting,  int transaction, emstorage_mail_tbl_t** mail_list, int *result_count, int *err_code)
3883 {
3884         EM_DEBUG_FUNC_BEGIN();
3885         EM_PROFILE_BEGIN(emStorageGetMails);
3886
3887         int count = 0, ret = false, error = EMAIL_ERROR_NONE;
3888         emstorage_mail_tbl_t *p_data_tbl = NULL;
3889         char conditional_clause_string[QUERY_SIZE] = {0, };
3890
3891         EM_IF_NULL_RETURN_VALUE(mail_list, false);
3892         EM_IF_NULL_RETURN_VALUE(result_count, false);
3893
3894         if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3895                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3896                 if (err_code != NULL)
3897                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3898                 return false;
3899         }
3900
3901         _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);
3902
3903         EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
3904
3905         if(!emstorage_query_mail_tbl(multi_user_name, conditional_clause_string, transaction, &p_data_tbl, &count,  &error)) {
3906                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3907                 goto FINISH_OFF;
3908         }
3909
3910         ret = true;
3911
3912 FINISH_OFF:
3913         if (ret == true) {
3914                 *mail_list = p_data_tbl;
3915                 *result_count = count;
3916                 EM_DEBUG_LOG("COUNT : %d", count);
3917         }
3918         else if (p_data_tbl != NULL)
3919                 emstorage_free_mail(&p_data_tbl, count, NULL);
3920
3921         if (err_code != NULL)
3922                 *err_code = error;
3923
3924         EM_PROFILE_END(emStorageGetMails);
3925         EM_DEBUG_FUNC_END("ret [%d]", ret);
3926         return ret;
3927 }
3928
3929
3930
3931 /**
3932   *     emstorage_get_searched_mail_list - Get the mail list information after filtering
3933   *
3934   *
3935   */
3936 INTERNAL_FUNC int emstorage_get_searched_mail_list(char *multi_user_name, int account_id, int mailbox_id, int thread_id, int search_type, const char *search_value, int start_index, int limit_count, email_sort_type_t sorting, int transaction, email_mail_list_item_t **mail_list,  int *result_count,  int *err_code)
3937 {
3938         EM_DEBUG_FUNC_BEGIN();
3939
3940         int ret = false, error = EMAIL_ERROR_NONE;
3941         char conditional_clause[QUERY_SIZE] = {0, };
3942         char *temp_search_value = NULL;
3943         char *temp_search_value2 = NULL;
3944
3945         EM_IF_NULL_RETURN_VALUE(mail_list, false);
3946         EM_IF_NULL_RETURN_VALUE(result_count, false);
3947
3948         if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3949                 EM_DEBUG_EXCEPTION("select_num[%p], Mail_list[%p]", result_count, mail_list);
3950                 error = EMAIL_ERROR_INVALID_PARAM;
3951                 goto FINISH_OFF;
3952         }
3953
3954         temp_search_value = em_replace_all_string((char*)search_value, "_", "\\_");
3955         temp_search_value2 = em_replace_all_string(temp_search_value, "%", "\\%");
3956
3957         _write_conditional_clause_for_getting_mail_list(account_id, mailbox_id, NULL, thread_id, start_index, limit_count, search_type, temp_search_value2, sorting, true, conditional_clause, QUERY_SIZE, &error);
3958
3959         EM_DEBUG_LOG("conditional_clause[%s]", conditional_clause);
3960
3961         if(!emstorage_query_mail_list(multi_user_name, conditional_clause, transaction, mail_list, result_count, &error)) {
3962                 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
3963                 goto FINISH_OFF;
3964         }
3965
3966         ret = true;
3967
3968 FINISH_OFF:
3969         EM_DEBUG_LOG("emstorage_get_searched_mail_list finish off");
3970
3971         if (err_code != NULL)
3972                 *err_code = error;
3973
3974         EM_SAFE_FREE(temp_search_value);
3975         EM_SAFE_FREE(temp_search_value2);
3976
3977         EM_DEBUG_FUNC_END("ret [%d]", ret);
3978         return ret;
3979 }
3980
3981
3982 static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name)
3983 {
3984         EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
3985
3986         if (account_id <= 0 || !recv_password_file_name || !send_password_file_name)  {
3987                 EM_DEBUG_EXCEPTION("Invalid parameter");
3988                 return EMAIL_ERROR_INVALID_PARAM;
3989         }
3990
3991     EM_DEBUG_LOG("MULTI_USER_NAME : [%s]", multi_user_name);
3992
3993         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
3994                 sprintf(recv_password_file_name, ".email_account_%d_recv_%s", account_id, multi_user_name);
3995                 sprintf(send_password_file_name, ".email_account_%d_send_%s", account_id, multi_user_name);
3996         } else {
3997                 sprintf(recv_password_file_name, ".email_account_%d_recv", account_id);
3998                 sprintf(send_password_file_name, ".email_account_%d_send", account_id);
3999         }
4000
4001         EM_DEBUG_FUNC_END();
4002         return EMAIL_ERROR_NONE;
4003 }
4004
4005 static int _read_password_from_secure_storage(char *file_name, char **password)
4006 {
4007         EM_DEBUG_FUNC_BEGIN_SEC("file_name[%s]", file_name);
4008
4009         if (!file_name || !password) {
4010                 EM_DEBUG_EXCEPTION("Invalid Parameter");
4011                 return EMAIL_ERROR_INVALID_PARAM;
4012         }
4013
4014         int ret = EMAIL_ERROR_NONE;
4015
4016         ret = emcore_get_password_in_key_manager(file_name, password);
4017         if (ret != EMAIL_ERROR_NONE) {
4018                 EM_DEBUG_EXCEPTION("emcore_get_password_in_key_manager failed : [%d]", ret);
4019                 goto FINISH_OFF;        
4020         }
4021
4022 FINISH_OFF:
4023
4024         EM_DEBUG_FUNC_END("ret [%d]", ret);
4025         return ret;
4026 }
4027
4028 INTERNAL_FUNC int emstorage_get_account_by_id(char *multi_user_name, int account_id, int pulloption, emstorage_account_tbl_t **account, int transaction, int *err_code)
4029 {
4030         EM_DEBUG_FUNC_BEGIN("account_id[%d], pulloption[%d], account[%p], transaction[%d], err_code[%p]", account_id, pulloption, account, transaction, err_code);
4031
4032         if (!account)  {
4033                 EM_DEBUG_EXCEPTION("account_id[%d], account[%p]", account_id, account);
4034                 if (err_code != NULL)
4035                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4036                 return false;
4037         }
4038
4039         int ret = false;
4040         int error = EMAIL_ERROR_NONE;
4041         emstorage_account_tbl_t* p_data_tbl = NULL;
4042         DB_STMT hStmt = NULL;
4043         char sql_query_string[QUERY_SIZE] = {0, };
4044         int rc = -1;
4045         int sql_len = 0;
4046         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4047         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4048
4049         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4050
4051         EMSTORAGE_START_READ_TRANSACTION(transaction);
4052
4053         /*  Make query string */
4054         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
4055         sql_len = EM_SAFE_STRLEN(sql_query_string);
4056
4057         if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4058                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4059                         "incoming_server_type,"
4060                         "incoming_server_address,"
4061                         "user_email_address,"
4062                         "incoming_server_user_name,"
4063                         "retrieval_mode,"
4064                         "incoming_server_port_number,"
4065                         "incoming_server_secure_connection,"
4066                         "incoming_server_authentication_method,"
4067                         "outgoing_server_type,"
4068                         "outgoing_server_address,"
4069                         "outgoing_server_port_number,"
4070                         "outgoing_server_need_authentication,"
4071                         "outgoing_server_secure_connection,"
4072                         "outgoing_server_user_name,"
4073                         "user_display_name,"
4074                         "reply_to_address,"
4075                         "return_address,"
4076                         "account_id,"
4077                         "keep_mails_on_pop_server_after_download,"
4078                         "auto_download_size,"
4079                         "peak_interval,"
4080                         "peak_days,"
4081                         "peak_start_time,"
4082                         "peak_end_time,"
4083                         "outgoing_server_use_same_authenticator,"
4084                         "auto_resend_times,"
4085                         "outgoing_server_size_limit,"
4086                         "wifi_auto_download,"
4087                         "pop_before_smtp,"
4088                         "incoming_server_requires_apop,"
4089                         "logo_icon_path,"
4090                         "user_data,"
4091                         "user_data_length,"
4092                         "color_label,"
4093                         "check_interval,"
4094                         "sync_status,");
4095                 sql_len = EM_SAFE_STRLEN(sql_query_string);
4096         }
4097
4098         if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME) {
4099                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " account_name, ");
4100                 sql_len = EM_SAFE_STRLEN(sql_query_string);
4101         }
4102
4103         /*  get from secure storage, not from db */
4104         if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4105                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4106                         "priority,"
4107                         "keep_local_copy,"
4108                         "req_delivery_receipt,"
4109                         "req_read_receipt,"
4110                         "download_limit,"
4111                         "block_address,"
4112                         "block_subject,"
4113                         "display_name_from,"
4114                         "reply_with_body,"
4115                         "forward_with_files,"
4116                         "add_myname_card,"
4117                         "add_signature,"
4118                         "signature,"
4119                         "add_my_address_to_bcc,"
4120                         "notification_status,"
4121                         "vibrate_status,"
4122                         "display_content_status,"
4123                         "default_ringtone_status,"
4124                         "alert_ringtone_path,"
4125                         "account_svc_id,"
4126                         "sync_disabled,"
4127                         "default_mail_slot_size,"
4128                         "roaming_option,"
4129                         "smime_type,"
4130                         "certificate_path,"
4131                         "cipher_type,"
4132                         "digest_type,");
4133
4134                 sql_len = EM_SAFE_STRLEN(sql_query_string);
4135         }
4136
4137         /*  dummy value, FROM WHERE clause */
4138         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "0 FROM mail_account_tbl WHERE account_id = %d", account_id);
4139
4140         /*  FROM clause */
4141         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
4142
4143         /*  execute a sql and count rows */
4144         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4145         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4146                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4147
4148         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4149         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4150                 ("sqlite3_step fail:%d", rc));
4151
4152         if (rc == SQLITE_DONE)  {
4153                 EM_DEBUG_EXCEPTION("no matched account found...");
4154                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4155                 goto FINISH_OFF;
4156         }
4157
4158         /*  Assign query result to structure */
4159         if (!(p_data_tbl = (emstorage_account_tbl_t *)malloc(sizeof(emstorage_account_tbl_t) * 1)))  {
4160                 EM_DEBUG_EXCEPTION("malloc failed...");
4161                 error = EMAIL_ERROR_OUT_OF_MEMORY;
4162                 goto FINISH_OFF;
4163         }
4164
4165         memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * 1);
4166         int col_index = 0;
4167
4168         if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4169                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_type), col_index++);
4170                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_address),  0, col_index++);
4171                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_email_address), 0, col_index++);
4172                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_user_name), 0, col_index++);
4173                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->retrieval_mode), col_index++);
4174                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_port_number), col_index++);
4175                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_secure_connection), col_index++);
4176                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_authentication_method), col_index++);
4177                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->outgoing_server_type), col_index++);
4178                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_address), 0, col_index++);
4179                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_port_number), col_index++);
4180                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_need_authentication), col_index++);
4181                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_secure_connection), col_index++);
4182                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_user_name), 0, col_index++);
4183                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_display_name), 0, col_index++);
4184                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->reply_to_address), 0, col_index++);
4185                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->return_address), 0, col_index++);
4186                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
4187                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->keep_mails_on_pop_server_after_download), col_index++);
4188                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_download_size), col_index++);
4189                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_interval), col_index++);
4190                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_days), col_index++);
4191                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_start_time), col_index++);
4192                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_end_time), col_index++);
4193                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_use_same_authenticator), col_index++);
4194                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_resend_times), col_index++);
4195                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_size_limit), col_index++);
4196                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->wifi_auto_download), col_index++);
4197                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->pop_before_smtp), col_index++);
4198                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_requires_apop), col_index++);
4199                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->logo_icon_path), 0, col_index++);
4200                 _get_stmt_field_data_blob(hStmt, &p_data_tbl->user_data, col_index++);
4201                 _get_stmt_field_data_int(hStmt, &p_data_tbl->user_data_length, col_index++);
4202                 _get_stmt_field_data_int(hStmt, &p_data_tbl->color_label, col_index++);
4203                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->check_interval), col_index++);
4204                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->sync_status), col_index++);
4205         }
4206
4207         if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME)
4208                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->account_name), 0, col_index++);
4209
4210         if (pulloption & EMAIL_ACC_GET_OPT_PASSWORD) {
4211                 /*  get password file name */
4212                 if ((error = _get_password_file_name(multi_user_name, p_data_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4213                         EM_DEBUG_EXCEPTION("_get_password_file_name failed [%d]", error);
4214                         goto FINISH_OFF;
4215                 }
4216
4217                 /*  read password from secure storage */
4218                 if ((error = _read_password_from_secure_storage(recv_password_file_name, &(p_data_tbl->incoming_server_password))) < 0) {
4219                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4220                         goto FINISH_OFF;
4221                 }
4222
4223                 EM_DEBUG_LOG_DEV ("recv_password_file_name[%s], password[%s]", recv_password_file_name,  p_data_tbl->incoming_server_password);
4224
4225                 if (p_data_tbl->outgoing_server_use_same_authenticator == 0) {
4226                         if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl->outgoing_server_password))) < 0) {
4227                                 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4228                                 goto FINISH_OFF;
4229                         }
4230                         EM_DEBUG_LOG_DEV ("send_password_file_name[%s], password[%s]", send_password_file_name,  p_data_tbl->outgoing_server_password);
4231                 }
4232         }
4233
4234         if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4235                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.priority), col_index++);
4236                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.keep_local_copy), col_index++);
4237                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_delivery_receipt), col_index++);
4238                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_read_receipt), col_index++);
4239                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.download_limit), col_index++);
4240                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_address), col_index++);
4241                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_subject), col_index++);
4242                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.display_name_from), 0, col_index++);
4243                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.reply_with_body), col_index++);
4244                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.forward_with_files), col_index++);
4245                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_myname_card), col_index++);
4246                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_signature), col_index++);
4247                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.signature), 0, col_index++);
4248                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.add_my_address_to_bcc), col_index++);
4249                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.notification_status), col_index++);
4250                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.vibrate_status), col_index++);
4251                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.display_content_status), col_index++);
4252                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.default_ringtone_status), col_index++);
4253                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.alert_ringtone_path), 0, col_index++);
4254                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_svc_id), col_index++);
4255                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->sync_disabled), col_index++);
4256                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->default_mail_slot_size), col_index++);
4257                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->roaming_option), col_index++);
4258                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->smime_type), col_index++);
4259                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->certificate_path), 0, col_index++);
4260                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->cipher_type), col_index++);
4261                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->digest_type), col_index++);
4262         }
4263
4264         ret = true;
4265
4266 FINISH_OFF:
4267         if (ret == true)
4268                 *account = p_data_tbl;
4269         else {
4270                 if (p_data_tbl)
4271                         emstorage_free_account((emstorage_account_tbl_t **)&p_data_tbl, 1, NULL);
4272         }
4273         if (hStmt != NULL)  {
4274                 rc = sqlite3_finalize(hStmt);
4275                 if (rc != SQLITE_OK)  {
4276                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
4277                         error = EMAIL_ERROR_DB_FAILURE;
4278                 }
4279         }
4280
4281         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4282
4283         if (err_code != NULL)
4284                 *err_code = error;
4285
4286         EM_DEBUG_FUNC_END("ret [%d]", ret);
4287         return ret;
4288 }
4289
4290 INTERNAL_FUNC int emstorage_get_password_length_of_account(char *multi_user_name, int account_id, int password_type, int *password_length, int *err_code)
4291 {
4292         EM_DEBUG_FUNC_BEGIN("account_id[%d], password_length[%p], err_code[%p]", account_id, password_length, err_code);
4293
4294         if (account_id <= 0 || password_length == NULL)  {
4295                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4296                 if (err_code != NULL)
4297                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4298                 return false;
4299         }
4300
4301         int ret = false;
4302         int error = EMAIL_ERROR_NONE;
4303         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4304         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4305         char *temp_password = NULL;
4306
4307
4308         /*  get password file name */
4309         if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4310                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4311                 goto FINISH_OFF;
4312         }
4313
4314         /*  read password from secure storage */
4315         if (password_type == EMAIL_GET_INCOMING_PASSWORD_LENGTH) {
4316                 if ((error = _read_password_from_secure_storage(recv_password_file_name, &temp_password)) < 0 || !temp_password) {
4317                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed...");
4318                         goto FINISH_OFF;
4319                 }
4320         } else if (password_type == EMAIL_GET_OUTGOING_PASSWORD_LENGTH) {
4321                 if ((error = _read_password_from_secure_storage(send_password_file_name, &temp_password)) < 0 || !temp_password) {
4322                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed...");
4323                         goto FINISH_OFF;
4324                 }
4325         } else {
4326                 EM_DEBUG_LOG("Invalid password type");
4327                 if (err_code != NULL)
4328                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4329                 goto FINISH_OFF;
4330         }
4331
4332         *password_length = EM_SAFE_STRLEN(temp_password);
4333
4334         EM_DEBUG_LOG_SEC("recv_password_file_name[%s], *password_length[%d]", recv_password_file_name,  *password_length);
4335
4336         ret = true;
4337
4338 FINISH_OFF:
4339         EM_SAFE_FREE(temp_password);
4340
4341         if (err_code != NULL)
4342                 *err_code = error;
4343
4344         EM_DEBUG_FUNC_END("ret [%d]", ret);
4345         return ret;
4346 }
4347
4348 INTERNAL_FUNC int emstorage_update_account_password(char *multi_user_name, int input_account_id, char *input_incoming_server_password, char *input_outgoing_server_password)
4349 {
4350         EM_DEBUG_FUNC_BEGIN("input_account_id[%d], input_incoming_server_password[%p], input_outgoing_server_password[%p]", input_account_id, input_incoming_server_password, input_outgoing_server_password);
4351
4352         int err = EMAIL_ERROR_NONE;
4353         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4354         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4355
4356         if(input_incoming_server_password == NULL && input_outgoing_server_password == NULL) {
4357                 EM_DEBUG_EXCEPTION_SEC("Invalid param");
4358                 err = EMAIL_ERROR_INVALID_PARAM;
4359                 goto FINISH_OFF;
4360         }
4361
4362         /*  get password file name */
4363         if ((err = _get_password_file_name(multi_user_name, 
4364                                                                                 input_account_id, 
4365                                                                                 recv_password_file_name, 
4366                                                                                 send_password_file_name)) != EMAIL_ERROR_NONE) {
4367                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4368                 goto FINISH_OFF;
4369         }
4370
4371         EM_DEBUG_LOG_SEC("recv_password_file_name [%s] input_incoming_server_password [%s]", 
4372                                                 recv_password_file_name, input_incoming_server_password);
4373         EM_DEBUG_LOG_SEC("send_password_file_name [%s] input_outgoing_server_password [%s]", 
4374                                                 send_password_file_name, input_outgoing_server_password);
4375
4376         err = emcore_remove_password_in_key_manager(recv_password_file_name);
4377         if (err != EMAIL_ERROR_NONE) {
4378                 EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", recv_password_file_name);
4379                 goto FINISH_OFF;
4380         }
4381
4382         /*  save recv passwords to the secure storage */
4383         err = emcore_add_password_in_key_manager(recv_password_file_name, input_incoming_server_password);
4384         if (err != EMAIL_ERROR_NONE) {
4385                 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4386                 goto FINISH_OFF;
4387         }
4388
4389         err = emcore_remove_password_in_key_manager(send_password_file_name);
4390         if (err != EMAIL_ERROR_NONE) {
4391                 EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", send_password_file_name);
4392                 goto FINISH_OFF;
4393         }
4394
4395         /*  save send passwords to the secure storage */
4396         err = emcore_add_password_in_key_manager(send_password_file_name, input_outgoing_server_password);
4397         if (err != EMAIL_ERROR_NONE) {
4398                 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4399                 goto FINISH_OFF;
4400         }
4401
4402 FINISH_OFF:
4403
4404         EM_DEBUG_FUNC_END("err [%d]", err);
4405         return err;
4406 }
4407
4408 INTERNAL_FUNC int emstorage_update_account(char *multi_user_name, int account_id, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
4409 {
4410         EM_DEBUG_FUNC_BEGIN("account_id[%d], account[%p], transaction[%d], err_code[%p]", account_id, account_tbl, transaction, err_code);
4411
4412         if (account_id < FIRST_ACCOUNT_ID || !account_tbl)  {
4413                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4414                 if (err_code != NULL)
4415                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4416                 return false;
4417         }
4418
4419         int error = EMAIL_ERROR_NONE;
4420         int rc, ret = false;
4421
4422         DB_STMT hStmt = NULL;
4423         char sql_query_string[QUERY_SIZE] = {0, };
4424
4425         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4426
4427         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4428
4429         SNPRINTF(sql_query_string, sizeof(sql_query_string),
4430                 "UPDATE mail_account_tbl SET"
4431                 "  account_name = ?"
4432                 ", logo_icon_path = ?"
4433                 ", user_data = ?"
4434                 ", user_data_length = ?"
4435                 ", account_svc_id = ?"
4436                 ", sync_status = ?"
4437                 ", sync_disabled = ?"
4438                 ", default_mail_slot_size = ?"
4439                 ", roaming_option = ?"
4440                 ", color_label = ?"
4441                 ", user_display_name = ?"
4442                 ", user_email_address = ?"
4443                 ", reply_to_address = ?"
4444                 ", return_address = ?"
4445                 ", incoming_server_type = ?"
4446                 ", incoming_server_address = ?"
4447                 ", incoming_server_port_number = ?"
4448                 ", incoming_server_user_name = ?"
4449                 ", incoming_server_secure_connection = ?"
4450                 ", incoming_server_authentication_method = ?"
4451                 ", retrieval_mode = ?"
4452                 ", keep_mails_on_pop_server_after_download = ?"
4453                 ", check_interval = ?"
4454                 ", auto_download_size = ?"
4455                 ", peak_interval = ?"
4456                 ", peak_days = ?"
4457                 ", peak_start_time = ?"
4458                 ", peak_end_time = ?"
4459                 ", outgoing_server_type = ?"
4460                 ", outgoing_server_address = ?"
4461                 ", outgoing_server_port_number = ?"
4462                 ", outgoing_server_user_name = ?"
4463                 ", outgoing_server_secure_connection = ?"
4464                 ", outgoing_server_need_authentication = ?"
4465                 ", outgoing_server_use_same_authenticator = ?"
4466                 ", priority = ?"
4467                 ", keep_local_copy = ?"
4468                 ", req_delivery_receipt = ?"
4469                 ", req_read_receipt = ?"
4470                 ", download_limit = ?"
4471                 ", block_address = ?"
4472                 ", block_subject = ?"
4473                 ", display_name_from = ?"
4474                 ", reply_with_body = ?"
4475                 ", forward_with_files = ?"
4476                 ", add_myname_card = ?"
4477                 ", add_signature = ?"
4478                 ", signature = ?"
4479                 ", add_my_address_to_bcc = ?"
4480                 ", notification_status = ?"
4481                 ", vibrate_status = ?"
4482                 ", display_content_status = ?"
4483                 ", default_ringtone_status = ?"
4484                 ", alert_ringtone_path = ?"
4485                 ", auto_resend_times = ?"
4486                 ", outgoing_server_size_limit = ?"
4487                 ", wifi_auto_download = ?"
4488                 ", pop_before_smtp = ?"
4489                 ", incoming_server_requires_apop = ?"
4490                 ", smime_type = ?"
4491                 ", certificate_path = ?"
4492                 ", cipher_type = ?"
4493                 ", digest_type = ?"
4494                 ", user_name = ?"
4495                 " WHERE account_id = ?");
4496
4497         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4498         EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
4499         EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
4500         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4501                 ("sqlite3_prepare fail:(%d) %s", rc, sqlite3_errmsg(local_db_handle)));
4502
4503         int i = 0;
4504
4505         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4506         _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4507         _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4508         _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4509         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4510         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4511         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4512         _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4513         _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
4514         _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
4515         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4516         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4517         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4518         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4519         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4520         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4521         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
4522         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4523         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
4524         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
4525         _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
4526         _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
4527         _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
4528         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
4529         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
4530         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
4531         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
4532         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
4533         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
4534         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4535         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
4536         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
4537         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
4538         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
4539         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
4540         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
4541         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
4542         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
4543         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
4544         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
4545         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
4546         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
4547         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4548         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
4549         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
4550         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
4551         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
4552         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
4553         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
4554         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
4555         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
4556         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
4557         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
4558         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4559         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
4560         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
4561         _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
4562         _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
4563         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
4564         _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
4565         _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4566         _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
4567         _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
4568         _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4569         _bind_stmt_field_data_int(hStmt, i++, account_id);
4570
4571         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4572         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
4573                 ("sqlite3_step fail:%d", rc));
4574         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4575                 ("sqlite3_step fail:%d", rc));
4576
4577         /*  validate account existence */
4578         rc = sqlite3_changes(local_db_handle);
4579         if (rc == 0) {
4580                 EM_DEBUG_EXCEPTION ("no matched account found...");
4581                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4582                 goto FINISH_OFF;
4583         }
4584
4585         if (account_tbl->incoming_server_password || account_tbl->outgoing_server_password) {
4586                 if((error = emstorage_update_account_password(multi_user_name, account_id, account_tbl->incoming_server_password, account_tbl->outgoing_server_password)) != EMAIL_ERROR_NONE) {
4587                         EM_DEBUG_EXCEPTION("emstorage_update_account_password failed [%d]", error);
4588                         goto FINISH_OFF;
4589                 }
4590         }
4591
4592         ret = true;
4593
4594 FINISH_OFF:
4595         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4596
4597         if (hStmt != NULL)  {
4598                 rc = sqlite3_finalize(hStmt);
4599                 if (rc != SQLITE_OK)  {
4600                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
4601                         error = EMAIL_ERROR_DB_FAILURE;
4602                 }
4603         }
4604
4605         if (error == EMAIL_ERROR_NONE) {
4606                 if (!emcore_notify_storage_event (NOTI_ACCOUNT_UPDATE, account_tbl->account_id, 0, NULL, 0))
4607                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_ACCOUNT_UPDATE] : Notification Failed >>> ");
4608         }
4609
4610         if (err_code != NULL)
4611                 *err_code = error;
4612
4613         EM_DEBUG_FUNC_END("ret [%d]", ret);
4614         return ret;
4615 }
4616
4617 INTERNAL_FUNC int emstorage_set_field_of_accounts_with_integer_value(char *multi_user_name, int account_id, char *field_name, int value, int transaction)
4618 {
4619         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], field_name[%s], value[%d], transaction[%d]", account_id, field_name, value, transaction);
4620         int error = EMAIL_ERROR_NONE;
4621         int result = 0;
4622         char sql_query_string[QUERY_SIZE] = {0, };
4623         sqlite3 *local_db_handle = NULL;
4624
4625         if (!account_id  || !field_name) {
4626                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4627                 return EMAIL_ERROR_INVALID_PARAM;
4628         }
4629
4630         local_db_handle = emstorage_get_db_connection(multi_user_name);
4631
4632         /* Write query string */
4633         SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_account_tbl SET %s = %d WHERE account_id = %d", field_name, value, account_id);
4634
4635         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4636
4637         /* Execute query */
4638         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4639
4640         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4641         if (error != EMAIL_ERROR_NONE) {
4642                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4643                 goto FINISH_OFF;
4644         }
4645
4646         if (sqlite3_changes(local_db_handle) == 0)
4647                 EM_DEBUG_LOG("no mail matched...");
4648
4649
4650 FINISH_OFF:
4651         result = (error == EMAIL_ERROR_NONE) ? true : false;
4652         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, error);
4653
4654         if (error == EMAIL_ERROR_NONE) {
4655                 if (!emcore_notify_storage_event (NOTI_ACCOUNT_UPDATE, account_id, 0, field_name, value))
4656                         EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_event failed : NOTI_ACCOUNT_UPDATE [%s,%d]", field_name, value);
4657         }
4658         EM_DEBUG_FUNC_END("error [%d]", error);
4659         return error;
4660 }
4661
4662 INTERNAL_FUNC int emstorage_get_sync_status_of_account(char *multi_user_name, int account_id, int *result_sync_status,int *err_code)
4663 {
4664         EM_DEBUG_FUNC_BEGIN("account_id[%d], result_sync_status [%p], err_code[%p]", account_id, result_sync_status, err_code);
4665
4666         if(!result_sync_status) {
4667                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4668                 if (err_code != NULL)
4669                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4670                 return false;
4671         }
4672
4673         int error = EMAIL_ERROR_NONE, rc, ret = false, sync_status, count, i, col_index;
4674         char sql_query_string[QUERY_SIZE] = {0, };
4675         char **result = NULL;
4676         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4677
4678         if(account_id)
4679                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl WHERE account_id = %d", account_id);
4680         else
4681                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl");
4682
4683         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
4684         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
4685                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4686
4687         if (!count) {
4688                 EM_DEBUG_EXCEPTION("no matched account found...");
4689                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4690                 goto FINISH_OFF;
4691         }
4692
4693         col_index = 1;
4694         *result_sync_status = 0;
4695
4696         for(i = 0; i < count; i++) {
4697                 _get_table_field_data_int(result, &sync_status, col_index++);
4698                 *result_sync_status |= sync_status;
4699         }
4700
4701         EM_DEBUG_LOG("sync_status [%d]", sync_status);
4702
4703         sqlite3_free_table(result);
4704
4705         ret = true;
4706
4707 FINISH_OFF:
4708
4709
4710         if (err_code != NULL)
4711                 *err_code = error;
4712
4713         EM_DEBUG_FUNC_END("ret [%d]", ret);
4714         return ret;
4715 }
4716
4717 INTERNAL_FUNC int emstorage_update_sync_status_of_account(char *multi_user_name, int account_id, email_set_type_t set_operator, int sync_status, int transaction, int *err_code)
4718 {
4719         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);
4720
4721         int error = EMAIL_ERROR_NONE, rc, ret = false, set_value = sync_status, result_sync_status;
4722         char sql_query_string[QUERY_SIZE] = {0, };
4723         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4724
4725         if(set_operator != SET_TYPE_SET && account_id) {
4726                 if(!emstorage_get_sync_status_of_account(multi_user_name, account_id, &result_sync_status, &error)) {
4727                         EM_DEBUG_EXCEPTION("emstorage_get_sync_status_of_account failed [%d]", error);
4728                         if (err_code != NULL)
4729                                 *err_code = error;
4730                         return false;
4731                 }
4732                 switch(set_operator) {
4733                         case SET_TYPE_UNION :
4734                                 set_value = result_sync_status | set_value;
4735                                 break;
4736                         case SET_TYPE_MINUS :
4737                                 set_value = result_sync_status & (~set_value);
4738                                 break;
4739                         default:
4740                                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_NOT_SUPPORTED [%d]", set_operator);
4741                                 error = EMAIL_ERROR_NOT_SUPPORTED;
4742                                 break;
4743                 }
4744                 EM_DEBUG_LOG("set_value [%d]", set_value);
4745         }
4746
4747         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4748
4749         if(account_id)
4750                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE account_id = %d", set_value, account_id);
4751         else
4752                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE incoming_server_type <> 5", set_value);
4753
4754         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4755
4756         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4757         if (error != EMAIL_ERROR_NONE) {
4758                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4759                 goto FINISH_OFF;
4760         }
4761
4762         rc = sqlite3_changes(local_db_handle);
4763
4764         if (rc == 0) {
4765                 EM_DEBUG_EXCEPTION("no matched account found...");
4766                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4767                 goto FINISH_OFF;
4768         }
4769
4770         ret = true;
4771
4772 FINISH_OFF:
4773         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4774
4775         if (error == EMAIL_ERROR_NONE) {
4776                 if (!emcore_notify_storage_event (NOTI_ACCOUNT_UPDATE_SYNC_STATUS, account_id, set_value, NULL, 0))
4777                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_UPDATE_SYNC_STATUS] : Notification failed");
4778         }
4779
4780         if (err_code != NULL)
4781                 *err_code = error;
4782
4783         EM_DEBUG_FUNC_END("ret [%d]", ret);
4784         return ret;
4785 }
4786
4787 INTERNAL_FUNC int emstorage_add_account(char *multi_user_name, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
4788 {
4789         EM_DEBUG_FUNC_BEGIN("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4790
4791         if (!account_tbl)  {
4792                 EM_DEBUG_EXCEPTION("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4793                 if (err_code != NULL)
4794                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4795                 return false;
4796         }
4797
4798         int rc = -1, ret = false;
4799         int error = EMAIL_ERROR_NONE;
4800         DB_STMT hStmt = NULL;
4801         char sql_query_string[QUERY_SIZE] = {0, };
4802         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4803         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4804
4805         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4806
4807         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4808
4809         char *sql = "SELECT max(rowid) FROM mail_account_tbl;";
4810         char **result = NULL;
4811
4812         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
4813         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
4814                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
4815
4816         if (NULL==result[1]) rc = 1;
4817         else rc = atoi(result[1])+1;
4818         sqlite3_free_table(result);
4819         result = NULL;
4820
4821         account_tbl->account_id = rc;
4822
4823         if ((error = _get_password_file_name(multi_user_name, account_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4824                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4825                 goto FINISH_OFF;
4826         }
4827
4828         EM_DEBUG_LOG(" >>>> ACCOUNT_ID [ %d ] ", account_tbl->account_id);
4829         SNPRINTF(sql_query_string, sizeof(sql_query_string),
4830                 "INSERT INTO mail_account_tbl VALUES "
4831                 "(        "
4832                 "    ? "  /*   account_id */
4833                 "  , ? "  /*   account_name */
4834                 "  , ? "  /*   logo_icon_path */
4835                 "  , ? "  /*   user_data */
4836                 "  , ? "  /*   user_data_length */
4837                 "  , ? "  /*   account_svc_id */
4838                 "  , ? "  /*   sync_status */
4839                 "  , ? "  /*   sync_disabled */
4840                 "  , ? "  /*   default_mail_slot_size */
4841                 "  , ? "  /*   roaming_option */
4842                 "  , ? "  /*   color_label */
4843                 "  , ? "  /*   user_display_name */
4844                 "  , ? "  /*   user_email_address */
4845                 "  , ? "  /*   reply_to_address */
4846                 "  , ? "  /*   return_address */
4847                 "  , ? "  /*   incoming_server_type */
4848                 "  , ? "  /*   incoming_server_address */
4849                 "  , ? "  /*   incoming_server_port_number */
4850                 "  , ? "  /*   incoming_server_user_name */
4851                 "  , ? "  /*   incoming_server_password */
4852                 "  , ? "  /*   incoming_server_secure_connection */
4853                 "  , ? "  /*   incoming_server_authentication_method */
4854                 "  , ? "  /*   retrieval_mode */
4855                 "  , ? "  /*   keep_mails_on_pop_server_after_download */
4856                 "  , ? "  /*   check_interval */
4857                 "  , ? "  /*   auto_download_size */
4858                 "  , ? "  /*   peak_interval */
4859                 "  , ? "  /*   peak_days */
4860                 "  , ? "  /*   peak_start_time */
4861                 "  , ? "  /*   peak_end_time */
4862                 "  , ? "  /*   outgoing_server_type */
4863                 "  , ? "  /*   outgoing_server_address */
4864                 "  , ? "  /*   outgoing_server_port_number */
4865                 "  , ? "  /*   outgoing_server_user_name */
4866                 "  , ? "  /*   outgoing_server_password */
4867                 "  , ? "  /*   outgoing_server_secure_connection */
4868                 "  , ? "  /*   outgoing_server_need_authentication */
4869                 "  , ? "  /*   outgoing_server_use_same_authenticator */
4870                 "  , ? "  /*   priority */
4871                 "  , ? "  /*   keep_local_copy */
4872                 "  , ? "  /*   req_delivery_receipt */
4873                 "  , ? "  /*   req_read_receipt */
4874                 "  , ? "  /*   download_limit */
4875                 "  , ? "  /*   block_address */
4876                 "  , ? "  /*   block_subject */
4877                 "  , ? "  /*   display_name_from */
4878                 "  , ? "  /*   reply_with_body */
4879                 "  , ? "  /*   forward_with_files */
4880                 "  , ? "  /*   add_myname_card */
4881                 "  , ? "  /*   add_signature */
4882                 "  , ? "  /*   signature */
4883                 "  , ? "  /*   add_my_address_to_bcc */
4884                 "  , ? "  /*   auto_resend_times */
4885                 "  , ? "  /*   outgoing_server_size_limit */
4886                 "  , ? "  /*   wifi_auto_download */
4887                 "  , ? "  /*   pop_before_smtp */
4888                 "  , ? "  /*   incoming_server_requires_apop */
4889                 "  , ? "  /*   smime_type */
4890                 "  , ? "  /*   certificate_path */
4891                 "  , ? "  /*   cipher_type */
4892                 "  , ? "  /*   digest_type */
4893                 "  , ? "  /*   notification_status */
4894                 "  , ? "  /*   vibrate_status */
4895                 "  , ? "  /*   display_content_status */
4896                 "  , ? "  /*   default_ringtone_status */
4897                 "  , ? "  /*   alert_ringtone_path */
4898                 "  , ? "  /*   user_name */
4899                 ") ");
4900
4901         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4902         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4903                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4904
4905         int i = 0;
4906
4907         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_id);
4908         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4909         _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4910         _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4911         _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4912         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4913         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4914         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4915         _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4916         _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
4917         _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
4918         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4919         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4920         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4921         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4922         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4923         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4924         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
4925         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4926         _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
4927         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
4928         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
4929         _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
4930         _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
4931         _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
4932         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
4933         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
4934         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
4935         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
4936         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
4937         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
4938         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4939         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
4940         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
4941         _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
4942         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
4943         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
4944         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
4945         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
4946         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
4947         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
4948         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
4949         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
4950         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
4951         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
4952         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4953         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
4954         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
4955         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
4956         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
4957         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
4958         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
4959         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
4960         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
4961         _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
4962         _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
4963         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
4964         _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
4965         _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
4966         _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
4967         _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
4968         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
4969         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
4970         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
4971         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
4972         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4973         _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4974
4975         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4976
4977         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
4978                 ("sqlite3_step fail:%d", rc));
4979         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
4980                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
4981
4982
4983         /*  save passwords to the secure storage */
4984         EM_DEBUG_LOG_SEC("save to the secure storage : recv_file[%s], send_file[%s]", recv_password_file_name, send_password_file_name);
4985         error = emcore_add_password_in_key_manager(recv_password_file_name, account_tbl->incoming_server_password);
4986         if (error != EMAIL_ERROR_NONE) {
4987                 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
4988                 goto FINISH_OFF;
4989         }
4990
4991         error = emcore_add_password_in_key_manager(send_password_file_name, account_tbl->outgoing_server_password);
4992         if (error != EMAIL_ERROR_NONE) {
4993                 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
4994                 goto FINISH_OFF;
4995         }
4996
4997         ret = true;
4998
4999 FINISH_OFF:
5000         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5001
5002         if (hStmt != NULL)  {
5003                 rc = sqlite3_finalize(hStmt);
5004                 if (rc != SQLITE_OK)  {
5005                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
5006                         error = EMAIL_ERROR_DB_FAILURE;
5007                 }
5008         }
5009
5010         if (error == EMAIL_ERROR_NONE) {
5011                 if (!emcore_notify_storage_event (NOTI_ACCOUNT_ADD, account_tbl->account_id, 0, NULL, 0))
5012                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_ADD] : Notification failed");
5013         }
5014
5015         if (err_code != NULL)
5016                 *err_code = error;
5017
5018         EM_DEBUG_FUNC_END("ret [%d]", ret);
5019         return ret;
5020 }
5021
5022 INTERNAL_FUNC int emstorage_delete_account(char *multi_user_name, int account_id, int transaction, int *err_code)
5023 {
5024         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
5025
5026         if (account_id < FIRST_ACCOUNT_ID)  {
5027                 EM_DEBUG_EXCEPTION(" account_id[%d]", account_id);
5028
5029                 if (err_code != NULL)
5030                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5031                 return false;
5032         }
5033
5034         int rc = -1, ret = false;
5035         int error = EMAIL_ERROR_NONE;
5036
5037         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5038
5039         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5040
5041         /*  TODO : delete password files - file names can be obtained from db or a rule that makes a name */
5042         char sql_query_string[QUERY_SIZE] = {0, };
5043         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5044         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5045
5046         /*  get password file name */
5047         if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
5048                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
5049                 goto FINISH_OFF;
5050         }
5051
5052         /*  delete from db */
5053         memset(sql_query_string, 0x00, sizeof(sql_query_string));
5054         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_account_tbl WHERE account_id = %d", account_id);
5055         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5056         if (error != EMAIL_ERROR_NONE) {
5057                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5058                         goto FINISH_OFF;
5059         }
5060
5061         /*  validate account existence */
5062         rc = sqlite3_changes(local_db_handle);
5063         if (rc == 0)  {
5064                 EM_DEBUG_EXCEPTION(" no matched account found...");
5065                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
5066                 goto FINISH_OFF;
5067         }
5068
5069                 /*  delete from secure storage */
5070         error = emcore_remove_password_in_key_manager(recv_password_file_name);
5071         if (error != EMAIL_ERROR_NONE) {
5072                 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5073                 goto FINISH_OFF;
5074         }
5075
5076         error = emcore_remove_password_in_key_manager(send_password_file_name);
5077         if (error != EMAIL_ERROR_NONE) {
5078                 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5079                 goto FINISH_OFF;
5080         }
5081
5082         ret = true;
5083
5084 FINISH_OFF:
5085         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5086
5087         if (err_code != NULL)
5088                 *err_code = error;
5089
5090         EM_DEBUG_FUNC_END("ret [%d]", ret);
5091         return ret;
5092 }
5093
5094
5095 INTERNAL_FUNC int emstorage_free_account(emstorage_account_tbl_t** account_list, int count, int *err_code)
5096 {
5097         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%d], err_code[%p]", account_list, count, err_code);
5098
5099         int ret = false;
5100         int error = EMAIL_ERROR_NONE;
5101
5102         if (count > 0)  {
5103                 if (!account_list || !*account_list)  {
5104                         EM_DEBUG_EXCEPTION("account_list[%p], count[%d]", account_list, count);
5105                         error = EMAIL_ERROR_INVALID_PARAM;
5106                         goto FINISH_OFF;
5107                 }
5108
5109                 emstorage_account_tbl_t* p = *account_list;
5110                 int i = 0;
5111
5112                 for (; i < count; i++)  {
5113                         EM_SAFE_FREE(p[i].account_name);
5114                         EM_SAFE_FREE(p[i].incoming_server_address);
5115                         EM_SAFE_FREE(p[i].user_email_address);
5116                         EM_SAFE_FREE(p[i].user_data);
5117                         EM_SAFE_FREE(p[i].incoming_server_user_name);
5118                         EM_SAFE_FREE(p[i].incoming_server_password);
5119                         EM_SAFE_FREE(p[i].outgoing_server_address);
5120                         EM_SAFE_FREE(p[i].outgoing_server_user_name);
5121                         EM_SAFE_FREE(p[i].outgoing_server_password);
5122                         EM_SAFE_FREE(p[i].user_display_name);
5123                         EM_SAFE_FREE(p[i].reply_to_address);
5124                         EM_SAFE_FREE(p[i].return_address);
5125                         EM_SAFE_FREE(p[i].logo_icon_path);
5126                         EM_SAFE_FREE(p[i].options.display_name_from);
5127                         EM_SAFE_FREE(p[i].options.signature);
5128                         EM_SAFE_FREE(p[i].options.alert_ringtone_path);
5129                         EM_SAFE_FREE(p[i].certificate_path);
5130                         EM_SAFE_FREE(p[i].user_name);
5131                 }
5132
5133                 EM_SAFE_FREE(p);
5134                 *account_list = NULL;
5135         }
5136
5137         ret = true;
5138
5139 FINISH_OFF:
5140         if (err_code != NULL)
5141                 *err_code = error;
5142
5143         EM_DEBUG_FUNC_END("ret [%d]", ret);
5144         return ret;
5145 }
5146
5147 INTERNAL_FUNC int emstorage_get_mailbox_count(char *multi_user_name, int account_id, int local_yn, int *count, int transaction, int *err_code)
5148 {
5149         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);
5150
5151         if ((account_id < FIRST_ACCOUNT_ID) || (count == NULL))  {
5152                 EM_DEBUG_EXCEPTION(" account_list[%d], local_yn[%d], count[%p]", account_id, local_yn, count);
5153
5154                 if (err_code != NULL)
5155                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5156                 return false;
5157         }
5158
5159         int rc = -1, ret = false;
5160         int error = EMAIL_ERROR_NONE;
5161         char sql_query_string[QUERY_SIZE] = {0, };
5162
5163         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5164         EMSTORAGE_START_READ_TRANSACTION(transaction);
5165
5166         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);
5167
5168         char **result;
5169
5170         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5171         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
5172                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5173
5174         *count = atoi(result[1]);
5175         sqlite3_free_table(result);
5176
5177
5178         ret = true;
5179
5180 FINISH_OFF:
5181
5182         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5183
5184         if (err_code != NULL)
5185                 *err_code = error;
5186
5187         EM_DEBUG_FUNC_END("ret [%d]", ret);
5188         return ret;
5189 }
5190
5191 INTERNAL_FUNC int emstorage_get_mailbox_list(char *multi_user_name, int account_id, int local_yn, email_mailbox_sort_type_t sort_type, int *select_num, emstorage_mailbox_tbl_t** mailbox_list, int transaction, int *err_code)
5192 {
5193         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);
5194
5195         if (!select_num || !mailbox_list) {
5196                 EM_DEBUG_EXCEPTION("Invalid parameters");
5197
5198                 if (err_code != NULL)
5199                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5200
5201                 return false;
5202         }
5203
5204         int ret = false;
5205         int error = EMAIL_ERROR_NONE;
5206         char conditional_clause_string[QUERY_SIZE] = {0, };
5207         char ordering_clause_string[QUERY_SIZE] = {0, };
5208
5209         if (account_id == ALL_ACCOUNT) {
5210                 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5211                         SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " WHERE local_yn = %d ", local_yn);
5212         }
5213         else {
5214                 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d  ", account_id);
5215                 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5216                         SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " AND local_yn = %d ", local_yn);
5217         }
5218
5219         EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5220
5221         switch (sort_type) {
5222                 case EMAIL_MAILBOX_SORT_BY_NAME_ASC :
5223                         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name ASC");
5224                         break;
5225
5226                 case EMAIL_MAILBOX_SORT_BY_NAME_DSC :
5227                         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name DESC");
5228                         break;
5229
5230                 case EMAIL_MAILBOX_SORT_BY_TYPE_ASC :
5231                         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type ASC");
5232                         break;
5233
5234                 case EMAIL_MAILBOX_SORT_BY_TYPE_DSC :
5235                         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type DEC");
5236                         break;
5237         }
5238
5239         EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5240
5241         if( (error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5242                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5243                 goto FINISH_OFF;
5244         }
5245
5246         ret = true;
5247
5248 FINISH_OFF:
5249
5250         if (err_code != NULL)
5251                 *err_code = error;
5252
5253         EM_DEBUG_FUNC_END("ret [%d]", ret);
5254         return ret;
5255 }
5256
5257 INTERNAL_FUNC int emstorage_get_mailbox_list_ex(char *multi_user_name, int account_id, int local_yn, int with_count, int *select_num, emstorage_mailbox_tbl_t **mailbox_list, int transaction, int *err_code)
5258 {
5259         EM_DEBUG_FUNC_BEGIN("account_id[%d], local_yn[%d], select_num[%p], mailbox_list[%p], transaction[%d], err_code[%p]", account_id, local_yn, select_num, mailbox_list, transaction, err_code);
5260
5261         int ret = false;
5262         int error = EMAIL_ERROR_NONE;
5263         int where_clause_count = 0;
5264         char conditional_clause_string[QUERY_SIZE] = {0, };
5265         char ordering_clause_string[QUERY_SIZE] = {0, };
5266
5267         if (!select_num || !mailbox_list) {
5268                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5269                 error = EMAIL_ERROR_INVALID_PARAM;
5270                 goto FINISH_OFF;
5271         }
5272
5273         if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL) {
5274                 SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), "WHERE local_yn = %d ",  local_yn);
5275                 where_clause_count++;
5276         }
5277
5278         if (account_id > 0) {
5279                 if (where_clause_count == 0) {
5280                         SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " WHERE ");
5281                         SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " account_id = %d ", account_id);
5282                 }
5283                 else
5284                         SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " AND account_id = %d ", account_id);
5285         }
5286
5287         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY CASE WHEN MBT.mailbox_name"
5288                                                                                                  " GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\'"
5289                                                                                                  " THEN 2 ELSE 1 END ASC,"
5290                                                                                                  " MBT.mailbox_name COLLATE CONVERTUTF8 ASC ");
5291         EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5292         EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5293
5294         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 1, 1, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5295                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5296                 goto FINISH_OFF;
5297         }
5298
5299         ret = true;
5300
5301 FINISH_OFF:
5302
5303         if (err_code != NULL)
5304                 *err_code = error;
5305
5306         EM_DEBUG_FUNC_END("ret [%d]", ret);
5307         return ret;
5308 }
5309
5310 INTERNAL_FUNC int emstorage_get_child_mailbox_list(char *multi_user_name, int account_id, char *parent_mailbox_name, int *select_num, emstorage_mailbox_tbl_t **mailbox_list, int transaction, int *err_code)
5311 {
5312         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);
5313
5314         int ret = false;
5315         int error = EMAIL_ERROR_NONE;
5316         char conditional_clause_string[QUERY_SIZE] = {0, };
5317
5318         if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list || !parent_mailbox_name)  {
5319                 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);
5320                 if (err_code != NULL)
5321                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5322                 return false;
5323         }
5324
5325         sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string, "WHERE account_id = %d  AND UPPER(mailbox_name) LIKE UPPER('%q%%')", account_id, parent_mailbox_name);
5326         EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5327
5328         if( (error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, " ORDER BY mailbox_name DESC ", 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5329                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5330                 goto FINISH_OFF;
5331         }
5332
5333         ret = true;
5334
5335 FINISH_OFF:
5336
5337         if (err_code != NULL)
5338                 *err_code = error;
5339
5340         EM_DEBUG_FUNC_END("ret [%d]", ret);
5341         return ret;
5342 }
5343
5344 INTERNAL_FUNC int emstorage_get_mailbox_by_modifiable_yn(char *multi_user_name, int account_id, int local_yn, int *select_num, emstorage_mailbox_tbl_t** mailbox_list, int transaction, int *err_code)
5345 {
5346         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);
5347         if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list)  {
5348                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5349
5350                 if (err_code != NULL)
5351                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5352
5353                 return false;
5354         }
5355
5356         int ret = false;
5357         int error = EMAIL_ERROR_NONE;
5358         char conditional_clause_string[QUERY_SIZE] = {0, };
5359
5360         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND modifiable_yn = 0", account_id);
5361         EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
5362
5363         if( (error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, " ORDER BY mailbox_name", 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5364                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5365                 goto FINISH_OFF;
5366         }
5367
5368         ret = true;
5369
5370 FINISH_OFF:
5371
5372         if (err_code != NULL)
5373                 *err_code = error;
5374
5375         EM_DEBUG_FUNC_END("ret [%d]", ret);
5376         return ret;
5377 }
5378
5379 INTERNAL_FUNC int emstorage_stamp_last_sync_time_of_mailbox(char *multi_user_name, int input_mailbox_id, int input_transaction)
5380 {
5381         EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_transaction [%d]", input_mailbox_id, input_transaction);
5382
5383         int      result_code = false;
5384         int      error = EMAIL_ERROR_NONE;
5385         time_t   current_time = 0;
5386         char     sql_query_string[QUERY_SIZE] = {0, };
5387         sqlite3 *local_db_handle = NULL;
5388
5389         if (!input_mailbox_id )  {
5390                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5391                 return EMAIL_ERROR_INVALID_PARAM;
5392         }
5393
5394         time(&current_time);
5395
5396         local_db_handle = emstorage_get_db_connection(multi_user_name);
5397     EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
5398
5399     SNPRINTF(sql_query_string, sizeof(sql_query_string),
5400         "UPDATE mail_box_tbl SET"
5401                 " last_sync_time = %d"
5402                 " WHERE mailbox_id = %d"
5403                 , (int)current_time
5404                 , input_mailbox_id);
5405
5406         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
5407         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5408         if (error != EMAIL_ERROR_NONE) {
5409                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5410                         goto FINISH_OFF;
5411         }
5412
5413 FINISH_OFF:
5414
5415         if(error == EMAIL_ERROR_NONE)
5416                 result_code = true;
5417
5418         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, result_code, error);
5419
5420         EM_DEBUG_FUNC_END("error [%d]", error);
5421         return error;
5422 }
5423
5424 INTERNAL_FUNC int emstorage_get_mailbox_by_name(char *multi_user_name, int account_id, int local_yn, char *mailbox_name, emstorage_mailbox_tbl_t **result_mailbox, int transaction, int *err_code)
5425 {
5426         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], local_yn[%d], mailbox_name[%s], result_mailbox[%p], transaction[%d], err_code[%p]", account_id, local_yn, mailbox_name, result_mailbox, transaction, err_code);
5427         EM_PROFILE_BEGIN(profile_emstorage_get_mailbox_by_name);
5428
5429         if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !result_mailbox)  {
5430                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], mailbox_name[%s], result_mailbox[%p]", account_id, local_yn, mailbox_name, result_mailbox);
5431
5432                 if (err_code != NULL)
5433                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5434                 return false;
5435         }
5436
5437         int ret = false;
5438         int error = EMAIL_ERROR_NONE;
5439         int result_count = 0;
5440         char conditional_clause_string[QUERY_SIZE] = {0, };
5441
5442         if(strcmp(mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
5443                 if (!(*result_mailbox = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t))))  {
5444                         EM_DEBUG_EXCEPTION("malloc failed...");
5445                         error = EMAIL_ERROR_OUT_OF_MEMORY;
5446                         goto FINISH_OFF;
5447                 }
5448
5449                 (*result_mailbox)->mailbox_id                 = 0;
5450                 (*result_mailbox)->account_id                 = account_id;
5451                 (*result_mailbox)->local_yn                   = 1;
5452                 (*result_mailbox)->mailbox_name               = EM_SAFE_STRDUP(mailbox_name);
5453                 (*result_mailbox)->mailbox_type               = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;
5454                 (*result_mailbox)->alias                      = EM_SAFE_STRDUP(mailbox_name);
5455                 (*result_mailbox)->deleted_flag               = 0;
5456                 (*result_mailbox)->modifiable_yn              = 1;
5457                 (*result_mailbox)->total_mail_count_on_server = 1;
5458                 (*result_mailbox)->has_archived_mails         = 0;
5459                 (*result_mailbox)->mail_slot_size             = 0x0FFFFFFF;
5460                 (*result_mailbox)->no_select                  = 0;
5461         }
5462         else {
5463
5464                 if (local_yn == -1)
5465                         sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string, "WHERE account_id = %d AND mailbox_name = '%q'", account_id, mailbox_name);
5466                 else
5467                         sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string, "WHERE account_id = %d AND local_yn = %d AND mailbox_name = '%q'", account_id, local_yn, mailbox_name);
5468
5469                 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5470
5471                 if( (error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5472                         EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5473                         goto FINISH_OFF;
5474                 }
5475         }
5476
5477         ret = true;
5478
5479 FINISH_OFF:
5480
5481         if (err_code != NULL)
5482                 *err_code = error;
5483
5484         EM_PROFILE_END(profile_emstorage_get_mailbox_by_name);
5485         EM_DEBUG_FUNC_END("ret [%d]", ret);
5486         return ret;
5487 }
5488
5489 INTERNAL_FUNC int emstorage_get_mailbox_by_mailbox_type(char *multi_user_name, int account_id, email_mailbox_type_e mailbox_type, emstorage_mailbox_tbl_t **output_mailbox, int transaction, int *err_code)
5490 {
5491         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_type[%d], output_mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox_type, output_mailbox, transaction, err_code);
5492
5493         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_USER_DEFINED) || !output_mailbox) {
5494                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_type[%d], output_mailbox[%p]", account_id, mailbox_type, output_mailbox);
5495                 if (err_code != NULL)
5496                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5497                 return false;
5498         }
5499
5500         int ret = false;
5501         int error = EMAIL_ERROR_NONE;
5502         int result_count = 0;
5503         emstorage_mailbox_tbl_t *result_mailbox = NULL;
5504         char conditional_clause_string[QUERY_SIZE] = {0,};
5505
5506
5507         SNPRINTF(conditional_clause_string, QUERY_SIZE, "WHERE account_id = %d AND mailbox_type = %d ", account_id, mailbox_type);
5508
5509         EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5510
5511         if( (error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", true, false, &result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5512                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl error [%d]", error);
5513                 goto FINISH_OFF;
5514         }
5515
5516         ret = true;
5517
5518 FINISH_OFF:
5519         if (ret == true)
5520                 *output_mailbox = result_mailbox;
5521
5522         if (err_code != NULL)
5523                 *err_code = error;
5524
5525         EM_DEBUG_FUNC_END("ret [%d]", ret);
5526         return ret;
5527 }
5528
5529 INTERNAL_FUNC int emstorage_get_mailbox_by_id(char *multi_user_name, int input_mailbox_id, emstorage_mailbox_tbl_t** output_mailbox)
5530 {
5531         EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], output_mailbox[%p]", input_mailbox_id, output_mailbox);
5532
5533         if (input_mailbox_id <= 0 || !output_mailbox)  {
5534                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: input_mailbox_id[%d] output_mailbox[%p]", input_mailbox_id, output_mailbox);
5535                 return  EMAIL_ERROR_INVALID_PARAM;
5536         }
5537
5538         int  ret = EMAIL_ERROR_NONE;
5539         int  result_count = 0;
5540         char conditional_clause_string[QUERY_SIZE] = {0, };
5541
5542         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE MBT.mailbox_id = %d", input_mailbox_id);
5543
5544         EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5545
5546         if( (ret = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", true, false, output_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5547                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", ret);
5548                 goto FINISH_OFF;
5549         }
5550
5551 FINISH_OFF:
5552
5553         EM_DEBUG_FUNC_END("ret [%d]", ret);
5554         return ret;
5555 }
5556
5557 INTERNAL_FUNC int emstorage_get_mailbox_by_keyword(char *multi_user_name, int account_id, char *keyword, emstorage_mailbox_tbl_t** result_mailbox, int * result_count, int transaction, int *err_code)
5558 {
5559         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], keyword[%s], result_mailbox[%p], transaction[%d], err_code[%p]", account_id, keyword, result_mailbox, transaction, err_code);
5560
5561         if (account_id < 0 || !keyword || !result_mailbox) {
5562                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], keyword[%s], result_mailbox[%p]", account_id, keyword, result_mailbox);
5563
5564                 if (err_code != NULL)
5565                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5566                 return false;
5567         }
5568
5569         int ret = false;
5570         int error = EMAIL_ERROR_NONE;
5571         char conditional_clause_string[QUERY_SIZE] = {0, };
5572
5573         if (account_id == 0)
5574                 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5575                                 "WHERE alias LIKE \'%%%q%%\'", keyword);
5576         else if (account_id > 0)
5577                 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5578                                 "WHERE account_id = %d AND alias LIKE \'%%%q%%\'", account_id, keyword);
5579
5580         EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5581
5582         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, result_count)) != EMAIL_ERROR_NONE) {
5583                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5584                 goto FINISH_OFF;
5585         }
5586
5587         ret = true;
5588
5589 FINISH_OFF:
5590
5591         if (err_code != NULL)
5592                 *err_code = error;
5593
5594         EM_DEBUG_FUNC_END("ret [%d]", ret);
5595         return ret;
5596 }
5597
5598 INTERNAL_FUNC int emstorage_get_mailbox_id_by_mailbox_type(char *multi_user_name, int account_id, email_mailbox_type_e mailbox_type, int *mailbox_id, int transaction, int *err_code)
5599 {
5600         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);
5601         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_id)  {
5602                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_id[%p]", account_id, mailbox_type, mailbox_id);
5603                 if (err_code != NULL)
5604                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5605                 return false;
5606         }
5607
5608         int rc, ret = false;
5609         int error = EMAIL_ERROR_NONE;
5610         emstorage_account_tbl_t* account = NULL;
5611         DB_STMT hStmt = NULL;
5612         char sql_query_string[QUERY_SIZE] = {0, };
5613
5614         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5615         EMSTORAGE_START_READ_TRANSACTION(transaction);
5616
5617         /*  Check whether the account exists. */
5618         if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
5619                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5620                 goto FINISH_OFF;
5621         }
5622
5623         if (account)
5624                 emstorage_free_account(&account, 1, NULL);
5625
5626         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);
5627
5628         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5629
5630
5631         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5632
5633         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5634                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5635
5636
5637         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5638         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5639                 ("sqlite3_step fail:%d", rc));
5640
5641         if (rc == SQLITE_DONE) {
5642                 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5643                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5644                 goto FINISH_OFF;
5645         }
5646
5647         _get_stmt_field_data_int(hStmt, mailbox_id, 0);
5648
5649         ret = true;
5650
5651 FINISH_OFF:
5652         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5653         if (hStmt != NULL)  {
5654                 rc = sqlite3_finalize(hStmt);
5655                 if (rc != SQLITE_OK)  {
5656                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
5657                         error = EMAIL_ERROR_DB_FAILURE;
5658                 }
5659         }
5660
5661         if (err_code != NULL)
5662                 *err_code = error;
5663
5664         EM_DEBUG_FUNC_END("ret [%d]", ret);
5665         return ret;
5666 }
5667
5668 INTERNAL_FUNC int emstorage_get_mailbox_name_by_mailbox_type(char *multi_user_name, int account_id, email_mailbox_type_e mailbox_type, char **mailbox_name, int transaction, int *err_code)
5669 {
5670         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);
5671         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_name)  {
5672                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
5673                 if (err_code != NULL)
5674                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5675                 return false;
5676         }
5677
5678         int rc, ret = false;
5679         int error = EMAIL_ERROR_NONE;
5680         emstorage_account_tbl_t* account = NULL;
5681         DB_STMT hStmt = NULL;
5682         char sql_query_string[QUERY_SIZE] = {0, };
5683
5684         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5685         EMSTORAGE_START_READ_TRANSACTION(transaction);
5686
5687         /*  Check whether the account exists. */
5688         if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
5689                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5690                 goto FINISH_OFF;
5691         }
5692
5693         if (account)
5694                 emstorage_free_account(&account, 1, NULL);
5695
5696         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);
5697
5698         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5699
5700
5701         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5702
5703         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5704                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5705
5706
5707         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5708         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5709                 ("sqlite3_step fail:%d", rc));
5710
5711         if (rc == SQLITE_DONE) {
5712                 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5713                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5714                 goto FINISH_OFF;
5715         }
5716
5717         _get_stmt_field_data_string(hStmt, mailbox_name, 0, 0);
5718
5719         ret = true;
5720
5721 FINISH_OFF:
5722         if (hStmt != NULL)  {
5723                 rc = sqlite3_finalize(hStmt);
5724                 if (rc != SQLITE_OK)  {
5725                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
5726                         error = EMAIL_ERROR_DB_FAILURE;
5727                 }
5728         }
5729
5730         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5731
5732         if (err_code != NULL)
5733                 *err_code = error;
5734
5735         EM_DEBUG_FUNC_END("ret [%d]", ret);
5736         return ret;
5737 }
5738
5739 INTERNAL_FUNC int emstorage_update_mailbox_modifiable_yn(char *multi_user_name, int account_id, int local_yn, char *mailbox_name, int modifiable_yn, int transaction, int *err_code)
5740 {
5741         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);
5742         int ret = false;
5743         int error = EMAIL_ERROR_NONE;
5744         char sql_query_string[QUERY_SIZE] = {0, };
5745         char *replaced_mailbox_name = NULL;
5746
5747         if (mailbox_name) {
5748                 if (strstr(mailbox_name, "'")) {
5749                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5750                 } else {
5751                         replaced_mailbox_name = strdup(mailbox_name);
5752                 }
5753         }
5754
5755         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5756
5757         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5758
5759         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5760                 "UPDATE mail_box_tbl SET"
5761                 " modifiable_yn = %d"
5762                 " WHERE account_id = %d"
5763                 " AND local_yn = %d"
5764                 " AND mailbox_name = '%s'"
5765                 , modifiable_yn
5766                 , account_id
5767                 , local_yn
5768                 , replaced_mailbox_name);
5769
5770         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5771         if (error != EMAIL_ERROR_NONE) {
5772                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5773                         goto FINISH_OFF;
5774         }
5775
5776         ret = true;
5777
5778 FINISH_OFF:
5779         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5780
5781         EM_SAFE_FREE(replaced_mailbox_name);
5782
5783         if (err_code != NULL)
5784                 *err_code = error;
5785
5786         EM_DEBUG_FUNC_END("ret [%d]", ret);
5787         return ret;
5788
5789 }
5790
5791 INTERNAL_FUNC int emstorage_update_mailbox_total_count(char *multi_user_name, int account_id, int input_mailbox_id, int total_count_on_server, int transaction, int *err_code)
5792 {
5793         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);
5794         int ret = false;
5795         int error = EMAIL_ERROR_NONE;
5796         char sql_query_string[QUERY_SIZE] = {0, };
5797
5798         if (account_id <= 0 || input_mailbox_id <= 0)  {
5799                 EM_DEBUG_EXCEPTION("account_id[%d], input_mailbox_id[%d]", account_id, input_mailbox_id);
5800
5801                 if (err_code != NULL)
5802                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5803                 EM_DEBUG_FUNC_END("ret [%d]", ret);
5804                 return false;
5805         }
5806         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5807
5808         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5809
5810         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5811                 "UPDATE mail_box_tbl SET"
5812                 " total_mail_count_on_server = %d"
5813                 " WHERE account_id = %d"
5814                 " AND mailbox_id = %d"
5815                 , total_count_on_server
5816                 , account_id
5817                 , input_mailbox_id);
5818
5819         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
5820         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5821         if (error != EMAIL_ERROR_NONE) {
5822                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5823                         goto FINISH_OFF;
5824         }
5825
5826         ret = true;
5827
5828 FINISH_OFF:
5829         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5830
5831         if (err_code != NULL)
5832                 *err_code = error;
5833
5834         EM_DEBUG_FUNC_END("ret [%d]", ret);
5835         return ret;
5836
5837 }
5838
5839 INTERNAL_FUNC int emstorage_update_mailbox(char *multi_user_name, int account_id, int local_yn, int input_mailbox_id, emstorage_mailbox_tbl_t *result_mailbox, int transaction, int *err_code)
5840 {
5841         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);
5842
5843         if (account_id < FIRST_ACCOUNT_ID || input_mailbox_id <= 0 || !result_mailbox)  {
5844                 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);
5845
5846                 if (err_code != NULL)
5847                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5848                 EM_DEBUG_FUNC_END("ret [%d]", EMAIL_ERROR_INVALID_PARAM);
5849                 return false;
5850         }
5851
5852         int rc, ret = false;
5853         int error = EMAIL_ERROR_NONE;
5854         char sql_query_string[QUERY_SIZE] = {0, };
5855         DB_STMT hStmt = NULL;
5856         int i = 0;
5857
5858         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5859         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5860
5861         if (local_yn != -1) {
5862                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5863                         "UPDATE mail_box_tbl SET"
5864                         "  mailbox_id = ?"
5865                         ", mailbox_name = ?"
5866                         ", mailbox_type = ?"
5867                         ", alias = ?"
5868                         ", deleted_flag = ?"
5869                         ", modifiable_yn= ?"
5870                         ", mail_slot_size= ?"
5871                         ", total_mail_count_on_server = ?"
5872                         " WHERE account_id = %d"
5873                         " AND local_yn = %d"
5874                         " AND mailbox_id = '%d'"
5875                         , account_id
5876                         , local_yn
5877                         , input_mailbox_id);
5878         }
5879         else {
5880                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5881                         "UPDATE mail_box_tbl SET"
5882                         "  mailbox_id = ?"
5883                         ", mailbox_name = ?"
5884                         ", mailbox_type = ?"
5885                         ", alias = ?"
5886                         ", deleted_flag = ?"
5887                         ", modifiable_yn= ?"
5888                         ", mail_slot_size= ?"
5889                         ", total_mail_count_on_server = ?"
5890                         " WHERE account_id = %d"
5891                         " AND mailbox_id = '%d'"
5892                         , account_id
5893                         , input_mailbox_id);
5894         }
5895
5896
5897
5898         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5899         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5900                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5901
5902         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_id);
5903         _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);
5904         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_type);
5905         _bind_stmt_field_data_string(hStmt, i++, (char *)result_mailbox->alias ? result_mailbox->alias : "", 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
5906         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->deleted_flag);
5907         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->modifiable_yn);
5908         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mail_slot_size);
5909         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->total_mail_count_on_server);
5910
5911
5912         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5913         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
5914                 ("sqlite3_step fail:%d", rc));
5915         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
5916                 ("sqlite3_step fail:%d", rc));
5917
5918         ret = true;
5919
5920 FINISH_OFF:
5921         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5922
5923         if (hStmt != NULL)  {
5924                 rc = sqlite3_finalize(hStmt);
5925                 if (rc != SQLITE_OK)  {
5926                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
5927                         error = EMAIL_ERROR_DB_FAILURE;
5928                 }
5929         }
5930
5931         if (err_code != NULL)
5932                 *err_code = error;
5933
5934         EM_DEBUG_FUNC_END("ret [%d]", ret);
5935         return ret;
5936 }
5937
5938 INTERNAL_FUNC int emstorage_update_mailbox_type(char *multi_user_name, int account_id, int local_yn, int input_mailbox_id, email_mailbox_type_e new_mailbox_type, int transaction, int *err_code)
5939 {
5940         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], local_yn[%d], input_mailbox_id[%d], new_mailbox_type[%d], transaction[%d], err_code[%p]", account_id, local_yn, input_mailbox_id, new_mailbox_type, transaction, err_code);
5941
5942         if (account_id < FIRST_ACCOUNT_ID)  {
5943                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
5944
5945                 if (err_code != NULL)
5946                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5947                 return false;
5948         }
5949
5950         int rc, ret = false;
5951         int error = EMAIL_ERROR_NONE;
5952         char sql_query_string[QUERY_SIZE] = {0, };
5953
5954         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5955         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5956
5957         EM_DEBUG_LOG("emstorage_update_mailbox_type");
5958
5959         DB_STMT hStmt_box_tbl = NULL;
5960         DB_STMT hStmt_mail_tbl = NULL;
5961         int i = 0;
5962
5963         /*  Update mail_box_tbl */
5964         if (local_yn != -1) {
5965                 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
5966                         "UPDATE mail_box_tbl SET"
5967                         " mailbox_type = ?"
5968                         " WHERE account_id = %d"
5969                         " AND local_yn = %d"
5970                         " AND mailbox_id = '%d'"
5971                         , account_id
5972                         , local_yn
5973                         , input_mailbox_id);
5974         }
5975         else {
5976                 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
5977                         "UPDATE mail_box_tbl SET"
5978                         " mailbox_type = ?"
5979                         " WHERE account_id = %d"
5980                         " AND mailbox_id = '%d'"
5981                         , account_id
5982                         , input_mailbox_id);
5983         }
5984
5985         EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
5986
5987         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_box_tbl, NULL), rc);
5988
5989         if(SQLITE_OK != rc) {
5990                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
5991                 error = EMAIL_ERROR_DB_FAILURE;
5992                 goto FINISH_OFF;
5993         }
5994
5995         _bind_stmt_field_data_int(hStmt_box_tbl, i++, new_mailbox_type);
5996
5997
5998         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_box_tbl), rc);
5999
6000         if(rc == SQLITE_FULL) {
6001                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6002                 error   = EMAIL_ERROR_MAIL_MEMORY_FULL;
6003                 goto FINISH_OFF;
6004         }
6005
6006         if(rc != SQLITE_ROW && rc != SQLITE_DONE) {
6007                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6008                 error = EMAIL_ERROR_DB_FAILURE;
6009                 goto FINISH_OFF;
6010         }
6011
6012
6013         /*  Update mail_tbl */
6014         i = 0;
6015         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6016                         "UPDATE mail_tbl SET"
6017                         " mailbox_type = ?"
6018                         " WHERE account_id = %d"
6019                         " AND mailbox_id = '%d'"
6020                         , account_id
6021                         , input_mailbox_id);
6022
6023         EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
6024
6025         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_mail_tbl, NULL), rc);
6026         if(SQLITE_OK != rc) {
6027                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6028                 error = EMAIL_ERROR_DB_FAILURE;
6029                 goto FINISH_OFF;
6030         }
6031
6032         _bind_stmt_field_data_int(hStmt_mail_tbl, i++, new_mailbox_type);
6033
6034         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_mail_tbl), rc);
6035         if(rc == SQLITE_FULL) {
6036                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6037                 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
6038                 goto FINISH_OFF;
6039         }
6040
6041         if(rc != SQLITE_ROW && rc != SQLITE_DONE){
6042                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6043                 error = EMAIL_ERROR_DB_FAILURE;
6044                 goto FINISH_OFF;
6045         }
6046
6047         ret = true;
6048
6049 FINISH_OFF:
6050         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6051
6052         if (hStmt_box_tbl != NULL)  {
6053                 rc = sqlite3_finalize(hStmt_box_tbl);
6054                 if (rc != SQLITE_OK)  {
6055                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
6056                         error = EMAIL_ERROR_DB_FAILURE;
6057                 }
6058         }
6059
6060         if (hStmt_mail_tbl != NULL)  {
6061                 rc = sqlite3_finalize(hStmt_mail_tbl);
6062                 if (rc != SQLITE_OK)  {
6063                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
6064                         error = EMAIL_ERROR_DB_FAILURE;
6065                 }
6066         }
6067
6068         if (err_code != NULL)
6069                 *err_code = error;
6070
6071         EM_DEBUG_FUNC_END("ret [%d]", ret);
6072         return ret;
6073 }
6074
6075 INTERNAL_FUNC int emstorage_set_local_mailbox(char *multi_user_name, int input_mailbox_id, int input_is_local_mailbox, int transaction)
6076 {
6077         EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], new_mailbox_type[%d], transaction[%d], err_code[%p]", input_mailbox_id, input_is_local_mailbox, transaction);
6078
6079         int rc, ret = false;
6080         int error = EMAIL_ERROR_NONE;
6081         char sql_query_string[QUERY_SIZE] = {0, };
6082
6083         if (input_mailbox_id < 0)  {
6084                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6085                 return EMAIL_ERROR_INVALID_PARAM;
6086         }
6087
6088         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6089
6090         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6091
6092         EM_DEBUG_LOG("emstorage_update_mailbox_type");
6093
6094         DB_STMT hStmt = NULL;
6095         int i = 0;
6096
6097         /*  Update mail_box_tbl */
6098         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6099                 "UPDATE mail_box_tbl SET"
6100                 " local_yn = ?"
6101                 " WHERE mailbox_id = %d"
6102                 , input_mailbox_id);
6103
6104         EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
6105
6106         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6107         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6108                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6109
6110         _bind_stmt_field_data_int(hStmt, i++, input_is_local_mailbox);
6111
6112         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6113         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
6114                 ("sqlite3_step fail:%d", rc));
6115         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6116                 ("sqlite3_step fail:%d", rc));
6117 /*
6118         if (hStmt != NULL)  {
6119                 rc = sqlite3_finalize(hStmt);
6120                 if (rc != SQLITE_OK)  {
6121                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
6122                         error = EMAIL_ERROR_DB_FAILURE;
6123                 }
6124                 hStmt = NULL;
6125         }
6126 */
6127         ret = true;
6128
6129 FINISH_OFF:
6130         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6131
6132         if (hStmt != NULL)  {
6133                 rc = sqlite3_finalize(hStmt);
6134                 if (rc != SQLITE_OK)  {
6135                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
6136                         error = EMAIL_ERROR_DB_FAILURE;
6137                 }
6138         }
6139
6140         EM_DEBUG_FUNC_END("error [%d]", error);
6141         return error;
6142 }
6143
6144 INTERNAL_FUNC int emstorage_set_field_of_mailbox_with_integer_value(char *multi_user_name, int input_account_id, int *input_mailbox_id_array, int input_mailbox_id_count, char *input_field_name, int input_value, int transaction)
6145 {
6146         EM_DEBUG_FUNC_BEGIN("input_account_id [%d] input_mailbox_id_array[%p] input_mailbox_id_count[%d] input_field_name[%p] input_value[%d] err_code[%p]", input_account_id, input_mailbox_id_array, input_mailbox_id_count, input_field_name, input_value, transaction);
6147         int i = 0;
6148         int err = EMAIL_ERROR_NONE;
6149         int result = false;
6150         int cur_mailbox_id_string = 0;
6151         int mailbox_id_string_buffer_length = 0;
6152         char  sql_query_string[QUERY_SIZE] = {0, };
6153         char *mailbox_id_string_buffer = NULL;
6154         char *parameter_string = NULL;
6155         sqlite3 *local_db_handle = NULL;
6156
6157         if (input_mailbox_id_array == NULL || input_mailbox_id_count == 0 || input_field_name == NULL) {
6158                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6159                 return EMAIL_ERROR_INVALID_PARAM;
6160         }
6161
6162         local_db_handle = emstorage_get_db_connection(multi_user_name);
6163
6164         /* Generating mail id list string */
6165         mailbox_id_string_buffer_length = MAILBOX_ID_STRING_LENGTH * input_mailbox_id_count;
6166
6167         mailbox_id_string_buffer = em_malloc(mailbox_id_string_buffer_length);
6168
6169         if(!mailbox_id_string_buffer) {
6170                 EM_DEBUG_EXCEPTION("em_malloc failed");
6171                 err = EMAIL_ERROR_OUT_OF_MEMORY;
6172                 goto FINISH_OFF;
6173         }
6174
6175         for(i = 0; i < input_mailbox_id_count; i++)
6176                 cur_mailbox_id_string += SNPRINTF_OFFSET(mailbox_id_string_buffer, cur_mailbox_id_string, mailbox_id_string_buffer_length, "%d,", input_mailbox_id_array[i]);
6177
6178         if(EM_SAFE_STRLEN(mailbox_id_string_buffer) > 1)
6179                 mailbox_id_string_buffer[EM_SAFE_STRLEN(mailbox_id_string_buffer) - 1] = NULL_CHAR;
6180
6181         /* Generating notification parameter string */
6182         parameter_string = em_malloc(mailbox_id_string_buffer_length + EM_SAFE_STRLEN(input_field_name) + 2);
6183
6184         if(!parameter_string) {
6185                 EM_DEBUG_EXCEPTION("em_malloc failed");
6186                 err = EMAIL_ERROR_OUT_OF_MEMORY;
6187                 goto FINISH_OFF;
6188         }
6189
6190         SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", input_field_name, 0x01, mailbox_id_string_buffer);
6191
6192         /* Write query string */
6193         SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_box_tbl SET %s = %d WHERE mailbox_id in (%s) ", input_field_name, input_value, mailbox_id_string_buffer);
6194
6195         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
6196
6197         /* Execute query */
6198         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
6199         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6200         if (err != EMAIL_ERROR_NONE) {
6201                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
6202                         goto FINISH_OFF;
6203         }
6204
6205         if (sqlite3_changes(local_db_handle) == 0)
6206                 EM_DEBUG_LOG("no mail matched...");
6207
6208         result = true;
6209
6210 FINISH_OFF:
6211         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, err);
6212
6213         if (err == EMAIL_ERROR_NONE && parameter_string) {
6214                 if (!emcore_notify_storage_event (NOTI_MAILBOX_FIELD_UPDATE, input_account_id, 0, parameter_string, input_value))
6215                         EM_DEBUG_EXCEPTION_SEC ("emcore_notify_storage_event failed : NOTI_MAILBOX_FIELD_UPDATE [%s,%d]",
6216                                                                                          input_field_name, input_value);
6217         }
6218
6219         EM_SAFE_FREE(mailbox_id_string_buffer);
6220         EM_SAFE_FREE(parameter_string);
6221
6222         EM_DEBUG_FUNC_END("err [%d]", err);
6223         return err;
6224 }
6225
6226 INTERNAL_FUNC int emstorage_add_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox_tbl, int transaction, int *err_code)
6227 {
6228         EM_DEBUG_FUNC_BEGIN("mailbox_tbl[%p], transaction[%d], err_code[%p]", mailbox_tbl, transaction, err_code);
6229
6230         if (!mailbox_tbl)  {
6231                 if (err_code != NULL)
6232                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6233                 return false;
6234         }
6235
6236         int rc, ret = false;
6237         int error = EMAIL_ERROR_NONE;
6238         DB_STMT hStmt = NULL;
6239         char sql_query_string[QUERY_SIZE] = {0,};
6240         char **result = NULL;
6241         time_t current_time;
6242         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6243         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6244
6245         EM_SAFE_STRCPY(sql_query_string, "SELECT max(rowid) FROM mail_box_tbl;");
6246
6247         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6248         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6249                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6250
6251         time(&current_time);
6252
6253         if (NULL == result[1])
6254                 rc = 1;
6255         else
6256                 rc = atoi(result[1]) + 1;
6257         sqlite3_free_table(result);
6258
6259         memset(sql_query_string, 0, sizeof(char) * QUERY_SIZE);
6260
6261         mailbox_tbl->mailbox_id = rc;
6262
6263         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6264                 "INSERT INTO mail_box_tbl VALUES "
6265                 "( ?"    /* mailbox_id */
6266                 ", ?"    /* account_id */
6267                 ", ?"    /* local_yn */
6268                 ", ?"    /* mailbox_name */
6269                 ", ?"    /* mailbox_type */
6270                 ", ?"    /* alias */
6271                 ", ?"    /* deleted_flag */
6272                 ", ?"    /* modifiable_yn */
6273                 ", ?"    /* total_mail_count_on_server */
6274                 ", ?"    /* has_archived_mails */
6275                 ", ?"    /* mail_slot_size */
6276                 ", ?"    /* no_select */
6277                 ", ?"    /* last_sync_time */
6278                 ", ?"    /* eas_data_length */
6279                 ", ?"    /* eas_data */
6280                 ")");
6281
6282
6283         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6284         EM_DEBUG_LOG_DEV ("After sqlite3_prepare hStmt = %p", hStmt);
6285         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6286                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6287
6288         int col_index = 0;
6289
6290         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_id);
6291         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->account_id);
6292         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->local_yn);
6293         _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
6294         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_type);
6295         _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->alias, 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
6296         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->deleted_flag);
6297         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->modifiable_yn);
6298         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->total_mail_count_on_server);
6299         _bind_stmt_field_data_int(hStmt, col_index++, 0);
6300         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mail_slot_size);
6301         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->no_select);
6302         _bind_stmt_field_data_int(hStmt, col_index++, current_time);
6303         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->eas_data_length);
6304         _bind_stmt_field_data_blob(hStmt, col_index++, (void*)mailbox_tbl->eas_data, mailbox_tbl->eas_data_length);
6305
6306
6307         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6308         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6309                 ("sqlite3_step fail:%dn", rc));
6310
6311         ret = true;
6312
6313 FINISH_OFF:
6314         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6315         if (hStmt != NULL)  {
6316                 rc = sqlite3_finalize(hStmt);
6317                 if (rc != SQLITE_OK)  {
6318                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
6319                         error = EMAIL_ERROR_DB_FAILURE;
6320                 }
6321         }
6322
6323         if (error == EMAIL_ERROR_NONE) {
6324                 if (!emcore_notify_storage_event (NOTI_MAILBOX_ADD, mailbox_tbl->account_id, mailbox_tbl->mailbox_id,
6325                                                                   mailbox_tbl->mailbox_name, mailbox_tbl->mailbox_type))
6326                         EM_DEBUG_EXCEPTION ("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6327         }
6328
6329         if (err_code != NULL)
6330                 *err_code = error;
6331
6332         EM_DEBUG_FUNC_END("ret [%d]", ret);
6333         return ret;
6334 }
6335
6336 INTERNAL_FUNC int emstorage_set_all_mailbox_modifiable_yn(char *multi_user_name, int account_id, int modifiable_yn, int transaction, int *err_code)
6337 {
6338         EM_DEBUG_FUNC_BEGIN("account_id[%d], modifiable_yn[%d], err_code[%p]", account_id, modifiable_yn, err_code);
6339
6340         if (account_id < FIRST_ACCOUNT_ID)  {
6341
6342                 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
6343
6344                 if (err_code != NULL)
6345                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6346                 return false;
6347         }
6348         int rc, ret = false;
6349         int error = EMAIL_ERROR_NONE;
6350         char sql_query_string[QUERY_SIZE] = {0,};
6351         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6352         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6353
6354
6355         SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET modifiable_yn = %d WHERE account_id = %d", modifiable_yn, account_id);
6356
6357         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6358         if (error != EMAIL_ERROR_NONE) {
6359                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6360                         goto FINISH_OFF;
6361         }
6362
6363         rc = sqlite3_changes(local_db_handle);
6364         if (rc == 0)
6365                 EM_DEBUG_EXCEPTION("All mailbox_name modifiable_yn set to 0 already");
6366
6367
6368         ret = true;
6369
6370 FINISH_OFF:
6371         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6372
6373         if (err_code != NULL)
6374                 *err_code = error;
6375         EM_DEBUG_FUNC_END("ret [%d]", ret);
6376         return ret;
6377
6378
6379 }
6380
6381 INTERNAL_FUNC int emstorage_delete_mailbox(char *multi_user_name, int account_id, int local_yn, int input_mailbox_id, int transaction, int *err_code)
6382 {
6383         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);
6384
6385         if (account_id < FIRST_ACCOUNT_ID)  {
6386                 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
6387
6388                 if (err_code != NULL)
6389                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6390                 return false;
6391         }
6392
6393         int rc, ret = false;
6394         int error = EMAIL_ERROR_NONE;
6395         char sql_query_string[QUERY_SIZE] = {0, };
6396         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6397         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6398
6399         if (local_yn == -1)
6400                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_box_tbl WHERE account_id = %d ", account_id);
6401         else
6402                 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);
6403
6404         if (input_mailbox_id > 0)  {            /* 0 means all mailbox */
6405                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(1+ EM_SAFE_STRLEN(sql_query_string)), "AND mailbox_id = %d", input_mailbox_id);
6406         }
6407
6408         EM_DEBUG_LOG_SEC("mailbox sql_query_string [%s]", sql_query_string);
6409         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6410         if (error != EMAIL_ERROR_NONE) {
6411                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6412                         goto FINISH_OFF;
6413         }
6414
6415         rc = sqlite3_changes(local_db_handle);
6416         if (rc == 0)  {
6417                 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
6418                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
6419                 ret = true;
6420         }
6421         ret = true;
6422
6423 FINISH_OFF:
6424
6425         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6426
6427         if(error == EMAIL_ERROR_NONE) {
6428                 if (!emcore_notify_storage_event (NOTI_MAILBOX_DELETE, account_id, input_mailbox_id, NULL, 0))
6429                         EM_DEBUG_EXCEPTION ("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6430         }
6431
6432         if (err_code != NULL)
6433                 *err_code = error;
6434
6435         EM_DEBUG_FUNC_END("ret [%d]", ret);
6436         return ret;
6437 }
6438
6439 INTERNAL_FUNC int emstorage_free_mailbox(emstorage_mailbox_tbl_t** mailbox_list, int count, int *err_code)
6440 {
6441         EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d], err_code[%p]", mailbox_list, count, err_code);
6442
6443         int ret = false;
6444         int error = EMAIL_ERROR_NONE;
6445
6446         if (count > 0)  {
6447                 if (!mailbox_list || !*mailbox_list)  {
6448                         EM_DEBUG_EXCEPTION(" mailbox_list[%p], count[%d]", mailbox_list, count);
6449
6450                         error = EMAIL_ERROR_INVALID_PARAM;
6451                         goto FINISH_OFF;
6452                 }
6453
6454                 emstorage_mailbox_tbl_t* p = *mailbox_list;
6455                 int i = 0;
6456
6457                 for (; i < count; i++) {
6458                         EM_SAFE_FREE(p[i].mailbox_name);
6459                         EM_SAFE_FREE(p[i].alias);
6460                         EM_SAFE_FREE(p[i].eas_data); /*valgrind*/
6461                 }
6462
6463                 EM_SAFE_FREE(p); *mailbox_list = NULL;
6464         }
6465
6466         ret = true;
6467
6468 FINISH_OFF:
6469         if (err_code != NULL)
6470                 *err_code = error;
6471
6472         EM_DEBUG_FUNC_END("ret [%d]", ret);
6473         return ret;
6474 }
6475
6476 INTERNAL_FUNC int emstorage_get_count_read_mail_uid(char *multi_user_name, int account_id, char *mailbox_name, int *count, int transaction, int *err_code)
6477 {
6478         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);
6479
6480         if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !count)  {
6481                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], count[%p], exist[%p]", account_id, mailbox_name, count);
6482
6483                 if (err_code != NULL)
6484                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6485                 return false;
6486         }
6487
6488         int rc = -1, ret = false;
6489         int error = EMAIL_ERROR_NONE;
6490         char sql_query_string[QUERY_SIZE] = {0, };
6491         char *replaced_mailbox_name = NULL;
6492
6493         if (strstr(mailbox_name, "'")) {
6494                 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6495         } else {
6496                 replaced_mailbox_name = EM_SAFE_STRDUP(mailbox_name);
6497         }
6498
6499         EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6500
6501
6502         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6503         EMSTORAGE_START_READ_TRANSACTION(transaction);
6504         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_read_mail_uid_tbl WHERE account_id = %d AND mailbox_name = '%s'  ", account_id, replaced_mailbox_name);
6505         EM_DEBUG_LOG_SEC(">>> SQL [ %s ] ", sql_query_string);
6506
6507         char **result;
6508
6509         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6510         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6511                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6512
6513         *count = atoi(result[1]);
6514         sqlite3_free_table(result);
6515
6516         ret = true;
6517
6518 FINISH_OFF:
6519         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6520
6521         EM_SAFE_FREE(replaced_mailbox_name);
6522
6523         if (err_code != NULL)
6524                 *err_code = error;
6525
6526         EM_DEBUG_FUNC_END("ret [%d]", ret);
6527         return ret;
6528 }
6529
6530 INTERNAL_FUNC int emstorage_check_read_mail_uid(char *multi_user_name, int account_id, char *mailbox_name, char *uid, int *exist, int transaction, int *err_code)
6531 {
6532         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);
6533
6534         if (account_id < FIRST_ACCOUNT_ID || !uid || !exist)  {
6535                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], uid[%p], exist[%p]", account_id, mailbox_name , uid, exist);
6536
6537                 if (err_code != NULL)
6538                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6539                 return false;
6540         }
6541
6542         int rc = -1, ret = false;
6543         int error = EMAIL_ERROR_NONE;
6544         char sql_query_string[QUERY_SIZE] = {0, };
6545         char *replaced_mailbox_name = NULL;
6546
6547         EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6548
6549         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6550         EMSTORAGE_START_READ_TRANSACTION(transaction);
6551
6552         if (mailbox_name)  {
6553                 if (strstr(mailbox_name, "'")) {
6554                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6555                 } else {
6556                         replaced_mailbox_name = strdup(mailbox_name);
6557                 }
6558
6559                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_read_mail_uid_tbl WHERE account_id = %d AND mailbox_name = '%s' AND server_uid = '%s' ", account_id, replaced_mailbox_name, uid);
6560         }
6561         else  {
6562                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_read_mail_uid_tbl WHERE account_id = %d AND server_uid = '%s' ", account_id, uid);
6563         }
6564
6565         char **result = NULL;
6566
6567         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6568         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6569                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6570
6571         *exist = atoi(result[1]);
6572         sqlite3_free_table(result);
6573
6574         if (*exist > 0)
6575                 *exist = 1;
6576         else
6577                 *exist = 0;
6578
6579         ret = true;
6580
6581 FINISH_OFF:
6582         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6583
6584         EM_SAFE_FREE(replaced_mailbox_name);
6585
6586         if (err_code != NULL)
6587                 *err_code = error;
6588
6589         EM_DEBUG_FUNC_END("ret [%d]", ret);
6590         return ret;
6591 }
6592
6593 INTERNAL_FUNC int emstorage_get_downloaded_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code)
6594 {
6595         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], err_code[%p]", mail_id, mail, err_code);
6596
6597         if (!mail || mail_id <= 0) {
6598                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
6599                 if (err_code != NULL)
6600                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6601                 return false;
6602         }
6603
6604         int rc, ret = false;
6605         int error = EMAIL_ERROR_NONE;
6606         DB_STMT hStmt = NULL;
6607         char sql_query_string[QUERY_SIZE] = {0, };
6608
6609         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6610         EMSTORAGE_START_READ_TRANSACTION(transaction);
6611
6612         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE local_uid = %d", mail_id);
6613
6614         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6615         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6616
6617         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6618                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6619
6620
6621         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6622         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6623                 ("sqlite3_step fail:%d", rc));
6624
6625         *mail = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
6626         if (*mail == NULL) {
6627                 error = EMAIL_ERROR_OUT_OF_MEMORY;
6628                 EM_DEBUG_EXCEPTION("Memory allocation for mail failed.");
6629                 goto FINISH_OFF;
6630
6631         }
6632         memset(*mail, 0x00, sizeof(emstorage_mail_tbl_t));
6633
6634         _get_stmt_field_data_int(hStmt, &((*mail)->account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6635         _get_stmt_field_data_int(hStmt, &((*mail)->mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6636         _get_stmt_field_data_string(hStmt, &((*mail)->server_mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6637         _get_stmt_field_data_int(hStmt, &((*mail)->mail_id), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6638         _get_stmt_field_data_string(hStmt, &((*mail)->server_mail_id), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6639         _get_stmt_field_data_int(hStmt, &((*mail)->mail_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6640         _get_stmt_field_data_char(hStmt, &((*mail)->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6641
6642         (*mail)->server_mail_status = 1;
6643
6644         ret = true;
6645
6646 FINISH_OFF:
6647         if (hStmt != NULL)  {
6648                 rc = sqlite3_finalize(hStmt);
6649                 if (rc != SQLITE_OK)  {
6650                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
6651                         error = EMAIL_ERROR_DB_FAILURE;
6652                 }
6653         }
6654
6655         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6656
6657         if (err_code != NULL)
6658                 *err_code = error;
6659
6660         EM_DEBUG_FUNC_END("ret [%d]", ret);
6661         return ret;
6662 }
6663
6664 INTERNAL_FUNC int emstorage_get_downloaded_list(char *multi_user_name, int account_id, int mailbox_id, emstorage_read_mail_uid_tbl_t **read_mail_uid, int *count, int transaction, int *err_code)
6665 {
6666         EM_PROFILE_BEGIN(emStorageGetDownloadList);
6667         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);
6668         if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid || !count)  {
6669                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_id[%s], read_mail_uid[%p], count[%p]", account_id, mailbox_id, read_mail_uid, count);
6670
6671                 if (err_code != NULL)
6672                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6673                 return false;
6674         }
6675
6676         int rc, ret = false;
6677         int error = EMAIL_ERROR_NONE;
6678
6679         DB_STMT hStmt = NULL;
6680         char sql_query_string[QUERY_SIZE] = {0, };
6681
6682         emstorage_read_mail_uid_tbl_t* p_data_tbl = NULL;
6683         int i = 0;
6684
6685         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6686         EMSTORAGE_START_READ_TRANSACTION(transaction);
6687
6688         if (mailbox_id)
6689                 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);
6690         else
6691                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
6692
6693         EM_DEBUG_LOG_SEC(" sql_query_string : %s", sql_query_string);
6694
6695
6696
6697         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6698         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6699         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6700                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6701
6702
6703         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6704         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6705                 ("sqlite3_step fail:%d", rc));
6706
6707         char **result;
6708         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL); */
6709         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL), rc);
6710         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6711                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6712
6713         sqlite3_free_table(result);
6714         if (*count == 0)  {
6715                 EM_DEBUG_LOG("No mail found in mail_read_mail_uid_tbl");
6716                 ret = true;
6717                 goto FINISH_OFF;
6718         }
6719
6720
6721         if (!(p_data_tbl = (emstorage_read_mail_uid_tbl_t*)malloc(sizeof(emstorage_read_mail_uid_tbl_t) * *count)))  {
6722                 EM_DEBUG_EXCEPTION(" malloc failed...");
6723                 error = EMAIL_ERROR_OUT_OF_MEMORY;
6724                 goto FINISH_OFF;
6725         }
6726
6727         memset(p_data_tbl, 0x00, sizeof(emstorage_read_mail_uid_tbl_t)* *count);
6728
6729         for (i = 0; i < *count; ++i)  {
6730                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6731                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id),LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6732                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6733                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].local_uid), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6734                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].server_uid), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6735                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rfc822_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6736                 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6737                 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_flagged_field), FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6738
6739                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6740                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6741                         ("sqlite3_step fail:%d", rc));
6742         }
6743
6744         ret = true;
6745
6746 FINISH_OFF:
6747         if (ret == true)
6748                 *read_mail_uid = p_data_tbl;
6749         else if (p_data_tbl)
6750                 emstorage_free_read_mail_uid(&p_data_tbl, *count, NULL);
6751
6752         if (hStmt != NULL)  {
6753                 rc = sqlite3_finalize(hStmt);
6754                 if (rc != SQLITE_OK)  {
6755                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
6756                         error = EMAIL_ERROR_DB_FAILURE;
6757                 }
6758         }
6759
6760         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6761
6762         if (err_code != NULL)
6763                 *err_code = error;
6764
6765         EM_PROFILE_END(emStorageGetDownloadList);
6766         EM_DEBUG_FUNC_END("ret [%d]", ret);
6767         return ret;
6768 }
6769
6770 INTERNAL_FUNC int emstorage_get_downloaded_mail_size(char *multi_user_name, int account_id, char *mailbox_id, int local_uid, char *mailbox_name, char *uid, int *mail_size, int transaction, int *err_code)
6771 {
6772         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);
6773
6774         if (account_id < FIRST_ACCOUNT_ID || !mail_size)  {
6775                 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);
6776
6777                 if (err_code != NULL)
6778                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6779                 return false;
6780         }
6781
6782         int rc, ret = false;
6783         int error = EMAIL_ERROR_NONE;
6784         DB_STMT hStmt = NULL;
6785         char sql_query_string[QUERY_SIZE] = {0, };
6786         char *replaced_mailbox_name = NULL;
6787
6788         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6789         EMSTORAGE_START_READ_TRANSACTION(transaction);
6790
6791         if (mailbox_name) {
6792                 if (strstr(mailbox_name, "'")) {
6793                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6794                 } else {
6795                         replaced_mailbox_name = strdup(mailbox_name);
6796                 }
6797
6798                 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6799
6800                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6801                         "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6802                         "WHERE account_id = %d "
6803                         "AND mailbox_id = '%s' "
6804                         "AND local_uid = %d "
6805                         "AND mailbox_name = '%s' "
6806                         "AND server_uid = '%s'",
6807                         account_id, mailbox_id, local_uid, replaced_mailbox_name, uid);
6808         }
6809         else {
6810                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6811                         "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6812                         "WHERE account_id = %d "
6813                         "AND mailbox_id = '%s' "
6814                         "AND local_uid = %d "
6815                         "AND server_uid = '%s'",
6816                         account_id, mailbox_id, local_uid, uid);
6817         }
6818
6819
6820         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6821         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6822                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6823
6824
6825         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6826         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6827                 ("sqlite3_step fail:%d", rc));
6828
6829         if (rc == SQLITE_DONE) {
6830                 EM_DEBUG_LOG("no matched mail found....");
6831                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
6832                 goto FINISH_OFF;
6833         }
6834
6835         _get_stmt_field_data_int(hStmt, mail_size, 0);
6836
6837         ret = true;
6838
6839 FINISH_OFF:
6840         EM_SAFE_FREE(replaced_mailbox_name);
6841
6842         if (hStmt != NULL)  {
6843                 rc = sqlite3_finalize(hStmt);
6844                 if (rc != SQLITE_OK)  {
6845                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
6846                         error = EMAIL_ERROR_DB_FAILURE;
6847                 }
6848         }
6849
6850         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6851
6852         if (err_code != NULL)
6853                 *err_code = error;
6854
6855         EM_DEBUG_FUNC_END("ret [%d]", ret);
6856         return ret;
6857 }
6858
6859 INTERNAL_FUNC int emstorage_add_downloaded_mail(char *multi_user_name, emstorage_read_mail_uid_tbl_t *read_mail_uid, int transaction, int *err_code)
6860 {
6861         EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], transaction[%d], err_code[%p]", read_mail_uid, transaction, err_code);
6862
6863         if (!read_mail_uid)  {
6864                 EM_DEBUG_EXCEPTION("read_mail_uid[%p]", read_mail_uid);
6865                 if (err_code != NULL)
6866                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6867                 return false;
6868         }
6869
6870         int rc, rc2,  ret = false;
6871         int error = EMAIL_ERROR_NONE;
6872         DB_STMT hStmt = NULL;
6873         char sql_query_string[QUERY_SIZE] = {0, };
6874
6875         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6876         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6877
6878         char *sql = "SELECT max(rowid) FROM mail_read_mail_uid_tbl;";
6879         char **result = NULL;
6880
6881
6882         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6883         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
6884                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6885
6886         if (NULL==result[1]) rc = 1;
6887         else rc = atoi(result[1])+1;
6888         sqlite3_free_table(result);
6889
6890         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6891                 "INSERT INTO mail_read_mail_uid_tbl VALUES "
6892                 "( ?"  /* account_id */
6893                 ", ?"  /* mailbox_id */
6894                 ", ?"  /* mailbox_name */
6895                 ", ?"  /* local_uid */
6896                 ", ?"  /* server_uid */
6897                 ", ?"  /* rfc822_size */
6898                 ", ?"  /* sync_status */
6899                 ", ?"  /* flags_seen_field */
6900                 ", ?"  /* flags_flagged_field */
6901                 ", ? )");
6902
6903
6904         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
6905         if (rc2 != SQLITE_OK)  {
6906                 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6907                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6908
6909                 error = EMAIL_ERROR_DB_FAILURE;
6910                 goto FINISH_OFF;
6911         }
6912
6913         EM_DEBUG_LOG("account_id[%d] mailbox_id[%d] local_uid [%d]"
6914                    "server_uid[%s] rfc822_size[%d] rc[%d]",
6915          read_mail_uid->account_id, read_mail_uid->mailbox_id,read_mail_uid->local_uid,
6916          read_mail_uid->server_uid, read_mail_uid->rfc822_size, rc);
6917
6918         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->account_id);
6919         _bind_stmt_field_data_int(hStmt, LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->mailbox_id);
6920         _bind_stmt_field_data_int(hStmt, LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->local_uid);
6921         _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);
6922         _bind_stmt_field_data_string(hStmt, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, (char *)read_mail_uid->server_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6923         _bind_stmt_field_data_int(hStmt, RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->rfc822_size);
6924         _bind_stmt_field_data_int(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_seen_field);
6925         _bind_stmt_field_data_int(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_flagged_field);
6926         _bind_stmt_field_data_int(hStmt, IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, rc);
6927
6928
6929         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6930         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
6931                 ("sqlite3_step fail:%d", rc));
6932         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
6933                 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
6934
6935
6936         ret = true;
6937
6938 FINISH_OFF:
6939         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6940         if (hStmt != NULL)  {
6941                 rc = sqlite3_finalize(hStmt);
6942                 if (rc != SQLITE_OK)  {
6943                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
6944                         error = EMAIL_ERROR_DB_FAILURE;
6945                 }
6946         }
6947
6948         if (err_code != NULL)
6949                 *err_code = error;
6950
6951         EM_DEBUG_FUNC_END("ret [%d]", ret);
6952         return ret;
6953 }
6954
6955 #ifdef __FEATURE_BODY_SEARCH__
6956 INTERNAL_FUNC int emstorage_add_mail_text(char *multi_user_name, emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
6957 {
6958         EM_DEBUG_FUNC_BEGIN("mail_text[%p], transaction[%d], err_code[%p]", mail_text, transaction, err_code);
6959
6960         if (!mail_text) {
6961                 EM_DEBUG_EXCEPTION("mail_text[%p]", mail_text);
6962                 if (err_code != NULL)
6963                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6964                 return false;
6965         }
6966
6967         int rc, rc2,  ret = false;
6968         int error = EMAIL_ERROR_NONE;
6969         DB_STMT hStmt = NULL;
6970         char sql_query_string[QUERY_SIZE] = {0, };
6971
6972         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6973         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6974
6975         char *sql = "SELECT max(rowid) FROM mail_text_tbl;";
6976         char **result = NULL;
6977
6978         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6979         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6980                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6981         sqlite3_free_table(result);
6982
6983         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6984                 "INSERT INTO mail_text_tbl VALUES "
6985                 "( ?"
6986                 ", ?"
6987                 ", ?"
6988                 ", ? )");
6989
6990         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
6991         if (rc2 != SQLITE_OK)  {
6992                 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6993                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc2, sqlite3_errmsg(local_db_handle));
6994
6995                 error = EMAIL_ERROR_DB_FAILURE;
6996                 goto FINISH_OFF;
6997         }
6998
6999         EM_DEBUG_LOG ("mail_id[%d] account_id[%d] mailbox_id[%d]", mail_text->mail_id,
7000                                        mail_text->account_id, mail_text->mailbox_id);
7001         EM_DEBUG_LOG_DEV ("body_text VALUE [%s] ", mail_text->body_text);
7002
7003         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mail_id);
7004         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->account_id);
7005         _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mailbox_id);
7006         _bind_stmt_field_data_string(hStmt, BODY_TEXT_IDX_IN_MAIL_TEXT_TBL, (char *)mail_text->body_text, 0, -1);
7007
7008         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7009         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
7010                 ("sqlite3_step fail:%d", rc));
7011         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7012                 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
7013
7014         ret = true;
7015
7016 FINISH_OFF:
7017         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7018         if (hStmt != NULL) {
7019                 rc = sqlite3_finalize(hStmt);
7020                 if (rc != SQLITE_OK)  {
7021                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
7022                         error = EMAIL_ERROR_DB_FAILURE;
7023                 }
7024         }
7025
7026         if (err_code != NULL)
7027                 *err_code = error;
7028
7029         EM_DEBUG_FUNC_END("ret [%d]", ret);
7030         return ret;
7031 }
7032 #endif
7033
7034 INTERNAL_FUNC int emstorage_change_read_mail_uid(char *multi_user_name, int account_id, int mailbox_id, int local_uid, char *mailbox_name, char *uid, emstorage_read_mail_uid_tbl_t* read_mail_uid, int transaction, int *err_code)
7035 {
7036         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);
7037
7038         if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid)  {
7039                 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);
7040
7041                 if (err_code != NULL)
7042                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7043                 return false;
7044         }
7045
7046         int rc, ret = false;
7047         int error = EMAIL_ERROR_NONE;
7048         DB_STMT hStmt = NULL;
7049         char sql_query_string[QUERY_SIZE] = {0, };
7050
7051         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7052
7053         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7054
7055         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7056                 "UPDATE mail_read_mail_uid_tbl SET"
7057                 "  account_id = ?"
7058                 ", mailbox_id = ?"
7059                 ", mailbox_name = ?"
7060                 ", local_uid  = ?"
7061                 ", server_uid = ?"
7062                 ", rfc822_size = ?"
7063                 ", flags_seen_field  = ?"
7064                 ", flags_flagged_field  = ?"
7065                 " WHERE account_id = ?"
7066                 " AND mailbox_id  = ?"
7067                 " AND local_uid   = ?"
7068                 " AND mailbox_name= ?"
7069                 " AND server_uid = ?");
7070
7071
7072         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7073         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7074         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7075                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7076
7077
7078         int i = 0;
7079
7080         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->account_id);
7081         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->mailbox_id);
7082         _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7083         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->local_uid);
7084         _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->server_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7085         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->rfc822_size);
7086         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_seen_field);
7087         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_flagged_field);
7088         _bind_stmt_field_data_int(hStmt, i++, account_id);
7089         _bind_stmt_field_data_int(hStmt, i++, mailbox_id);
7090         _bind_stmt_field_data_int(hStmt, i++, local_uid);
7091         _bind_stmt_field_data_string(hStmt, i++, (char*)mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7092         _bind_stmt_field_data_string(hStmt, i++, (char*)uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7093
7094
7095         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7096         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7097                 ("sqlite3_step fail:%d", rc));
7098
7099         ret = true;
7100
7101 FINISH_OFF:
7102         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7103         if (hStmt != NULL)  {
7104                 rc = sqlite3_finalize(hStmt);
7105                 if (rc != SQLITE_OK)  {
7106                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
7107                         error = EMAIL_ERROR_DB_FAILURE;
7108                 }
7109         }
7110
7111         if (err_code != NULL)
7112                 *err_code = error;
7113
7114         EM_DEBUG_FUNC_END("ret [%d]", ret);
7115         return ret;
7116 }
7117
7118 INTERNAL_FUNC int emstorage_remove_downloaded_mail(char *multi_user_name, int account_id, char *mailbox_name, char *uid, int transaction, int *err_code)
7119 {
7120         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], mailbox_name[%s], uid[%s], transaction[%d], err_code[%p]", account_id, mailbox_name, uid, transaction, err_code);
7121
7122         if (account_id < FIRST_ACCOUNT_ID)  {
7123                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_name[%s], uid[%s]", account_id, mailbox_name, uid);
7124
7125                 if (err_code != NULL)
7126                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7127                 return false;
7128         }
7129
7130         int ret = false;
7131         int error = EMAIL_ERROR_NONE;
7132         char sql_query_string[QUERY_SIZE] = {0, };
7133         char *replaced_mailbox_name = NULL;
7134         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7135
7136         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7137
7138         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d ", account_id);
7139
7140         if (mailbox_name) {             /*  NULL means all mailbox_name */
7141                 if (strstr(mailbox_name, "'")) {
7142                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
7143                 } else {
7144                         replaced_mailbox_name = strdup(mailbox_name);
7145                 }
7146
7147                 SNPRINTF(sql_query_string+EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)), "AND mailbox_name = '%s' ", replaced_mailbox_name);
7148         }
7149
7150         if (uid) {              /*  NULL means all mail */
7151                 SNPRINTF(sql_query_string+EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)), "AND server_uid='%s' ", uid);
7152         }
7153
7154         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7155         if (error != EMAIL_ERROR_NONE) {
7156                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7157                         goto FINISH_OFF;
7158         }
7159
7160         ret = true;
7161
7162 FINISH_OFF:
7163         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7164
7165         EM_SAFE_FREE(replaced_mailbox_name);
7166
7167         if (err_code != NULL)
7168                 *err_code = error;
7169
7170         EM_DEBUG_FUNC_END("ret [%d]", ret);
7171         return ret;
7172 }
7173
7174 INTERNAL_FUNC int emstorage_free_read_mail_uid(emstorage_read_mail_uid_tbl_t** read_mail_uid, int count, int *err_code)
7175 {
7176         EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], count[%d], err_code[%p]", read_mail_uid, count, err_code);
7177
7178         int ret = false;
7179         int error = EMAIL_ERROR_NONE;
7180
7181         if (count > 0)  {
7182                 if (!read_mail_uid || !*read_mail_uid)  {
7183                         EM_DEBUG_EXCEPTION(" read_mail_uid[%p], count[%d]", read_mail_uid, count);
7184
7185                         error = EMAIL_ERROR_INVALID_PARAM;
7186                         goto FINISH_OFF;
7187                 }
7188
7189                 emstorage_read_mail_uid_tbl_t* p = *read_mail_uid;
7190                 int i;
7191
7192                 for (i = 0; i < count; i++)  {
7193                         EM_SAFE_FREE(p[i].mailbox_name);
7194                         EM_SAFE_FREE(p[i].server_uid);
7195                 }
7196
7197                 EM_SAFE_FREE(p); *read_mail_uid = NULL;
7198         }
7199
7200         ret = true;
7201
7202 FINISH_OFF:
7203         if (err_code != NULL)
7204                 *err_code = error;
7205
7206         EM_DEBUG_FUNC_END("ret [%d]", ret);
7207         return ret;
7208 }
7209
7210 INTERNAL_FUNC int emstorage_get_rule_count_by_account_id(char *multi_user_name, int account_id, int *count, int transaction, int *err_code)
7211 {
7212         EM_DEBUG_FUNC_BEGIN("account_id [%d], count[%p], transaction[%d], err_code[%p]", count, transaction, err_code);
7213
7214         if (!count) {
7215                 EM_DEBUG_EXCEPTION("count[%p]", count);
7216
7217                 if (err_code != NULL)
7218                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7219                 return false;
7220         }
7221
7222         int rc = -1, ret = false;
7223         int error =  EMAIL_ERROR_NONE;
7224         char sql_query_string[QUERY_SIZE] = {0, };
7225
7226         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7227         EMSTORAGE_START_READ_TRANSACTION(transaction);
7228
7229         if (account_id != ALL_ACCOUNT)
7230                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl where account_id = %d", account_id);
7231         else
7232                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl");
7233
7234         char **result;
7235
7236         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7237         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
7238                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7239
7240         *count = atoi(result[1]);
7241         sqlite3_free_table(result);
7242
7243         ret = true;
7244
7245 FINISH_OFF:
7246         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7247
7248         if (err_code != NULL)
7249                 *err_code = error;
7250
7251         EM_DEBUG_FUNC_END("ret [%d]", ret);
7252         return ret;
7253 }
7254
7255 INTERNAL_FUNC int emstorage_get_rule(char *multi_user_name, int account_id, int type, int start_idx, int *select_num, int *is_completed, emstorage_rule_tbl_t** rule_list, int transaction, int *err_code)
7256 {
7257         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);
7258
7259         if (!select_num || !is_completed || !rule_list) {               /*  only global rule supported. */
7260                 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);
7261
7262                 if (err_code != NULL)
7263                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7264                 return false;
7265         }
7266
7267         int ret = false;
7268         int error = EMAIL_ERROR_NONE;
7269
7270         emstorage_rule_tbl_t* p_data_tbl = NULL;
7271         int i = 0, count = 0;
7272         DB_STMT hStmt = NULL;
7273         char sql_query_string[QUERY_SIZE] = {0, };
7274
7275         int rc;
7276
7277         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7278         EMSTORAGE_START_READ_TRANSACTION(transaction);
7279
7280         if (account_id != ALL_ACCOUNT) {
7281                 if (type)
7282                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d AND type = %d", account_id, type);
7283                 else
7284                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d ORDER BY type", account_id);
7285         } else {
7286                 if (type)
7287                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE type = %d", type);
7288                 else
7289                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl ORDER BY type");
7290         }
7291
7292         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7293 /*      EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt); */
7294         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7295                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7296
7297
7298         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7299         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7300                 ("sqlite3_step fail:%d", rc));
7301
7302         char **result;
7303         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL); */
7304         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
7305         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
7306                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7307
7308         sqlite3_free_table(result);
7309
7310         if (count == 0)  {
7311                 EM_DEBUG_LOG_DEV ("No matching rule found...");
7312                 ret = true;
7313                 error = EMAIL_ERROR_FILTER_NOT_FOUND; /*there is no matched rule*/
7314                 goto FINISH_OFF;
7315         }
7316
7317
7318         if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t) * count))) {
7319                 EM_DEBUG_EXCEPTION(" malloc failed...");
7320
7321                 error = EMAIL_ERROR_OUT_OF_MEMORY;
7322                 goto FINISH_OFF;
7323         }
7324
7325         memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t) * count);
7326
7327         for (i = 0; i < count; i++)  {
7328                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7329                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7330                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7331                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].type), TYPE_IDX_IN_MAIL_RULE_TBL);
7332                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7333                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7334                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7335                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7336                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7337                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7338
7339                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7340                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7341                         ("sqlite3_step fail:%d", rc));
7342         }
7343
7344         ret = true;
7345
7346 FINISH_OFF:
7347
7348         EM_DEBUG_LOG("[%d] rules found.", count);
7349
7350         if (ret == true)  {
7351                 *rule_list = p_data_tbl;
7352                 *select_num = count;
7353         }
7354         else if (p_data_tbl != NULL)
7355                 emstorage_free_rule(&p_data_tbl, count, NULL); /* CID FIX */
7356
7357         if (hStmt != NULL)  {
7358                 rc = sqlite3_finalize(hStmt);
7359                 if (rc != SQLITE_OK)  {
7360                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
7361                         error = EMAIL_ERROR_DB_FAILURE;
7362                 }
7363         }
7364
7365         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7366
7367         if (err_code != NULL)
7368                 *err_code = error;
7369
7370         EM_DEBUG_FUNC_END("ret [%d]", ret);
7371         return ret;
7372 }
7373
7374 INTERNAL_FUNC int emstorage_get_rule_by_id(char *multi_user_name, int rule_id, emstorage_rule_tbl_t** rule, int transaction, int *err_code)
7375 {
7376         EM_DEBUG_FUNC_BEGIN("rule_id[%d], rule[%p], transaction[%d], err_code[%p]", rule_id, rule, transaction, err_code);
7377         int error = EMAIL_ERROR_NONE;
7378         int ret = false;
7379         DB_STMT hStmt = NULL;
7380
7381         if (rule_id <= 0) {
7382                 EM_DEBUG_EXCEPTION("Invalid parameter");
7383                 error = EMAIL_ERROR_INVALID_PARAM;
7384                 goto FINISH_OFF;
7385         }
7386
7387         if (!rule) {
7388                 EM_DEBUG_EXCEPTION("rule_id[%d], rule[%p]", rule_id, rule);
7389                 error = EMAIL_ERROR_INVALID_PARAM;
7390                 goto FINISH_OFF;
7391         }
7392
7393         emstorage_rule_tbl_t* p_data_tbl = NULL;
7394         int rc;
7395
7396         char sql_query_string[QUERY_SIZE] = {0, };
7397         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7398         EMSTORAGE_START_READ_TRANSACTION(transaction);
7399
7400         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7401
7402         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_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)));
7405
7406
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));
7410
7411         if (rc == SQLITE_DONE)  {
7412                 EM_DEBUG_EXCEPTION(" no matched rule found...");
7413                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7414                 goto FINISH_OFF;
7415         }
7416
7417         if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t))))  {
7418                 EM_DEBUG_EXCEPTION(" malloc failed...");
7419                 error = EMAIL_ERROR_OUT_OF_MEMORY;
7420                 goto FINISH_OFF;
7421         }
7422
7423         memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t));
7424         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7425         _get_stmt_field_data_int(hStmt, &(p_data_tbl->rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7426         _get_stmt_field_data_string(hStmt, &(p_data_tbl->filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7427         _get_stmt_field_data_int(hStmt, &(p_data_tbl->type), TYPE_IDX_IN_MAIL_RULE_TBL);
7428         _get_stmt_field_data_string(hStmt, &(p_data_tbl->value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7429         _get_stmt_field_data_string(hStmt, &(p_data_tbl->value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7430         _get_stmt_field_data_int(hStmt, &(p_data_tbl->action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7431         _get_stmt_field_data_int(hStmt, &(p_data_tbl->target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7432         _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7433         _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7434
7435         ret = true;
7436
7437 FINISH_OFF:
7438
7439         if (ret == true)
7440                 *rule = p_data_tbl;
7441
7442         if (hStmt != NULL)  {
7443                 rc = sqlite3_finalize(hStmt);
7444                 if (rc != SQLITE_OK)  {
7445                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
7446                         error = EMAIL_ERROR_DB_FAILURE;
7447                 }
7448         }
7449
7450         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7451
7452         if (err_code != NULL)
7453                 *err_code = error;
7454
7455         EM_DEBUG_FUNC_END("ret [%d]", ret);
7456         return ret;
7457 }
7458
7459 INTERNAL_FUNC int emstorage_change_rule(char *multi_user_name, int rule_id, emstorage_rule_tbl_t* new_rule, int transaction, int *err_code)
7460 {
7461         EM_DEBUG_FUNC_BEGIN("rule_id[%d], new_rule[%p], transaction[%d], err_code[%p]", rule_id, new_rule, transaction, err_code);
7462
7463         if (!new_rule) {                /*  only global rule supported. */
7464                 EM_DEBUG_EXCEPTION("rule_id[%d], new_rule[%p]", rule_id, new_rule);
7465
7466                 if (err_code != NULL)
7467                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7468                 return false;
7469         }
7470
7471         int rc, ret = false;
7472         int error = EMAIL_ERROR_NONE;
7473
7474         DB_STMT hStmt = NULL;
7475         char sql_query_string[QUERY_SIZE] = {0, };
7476         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7477         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7478
7479         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7480                 "UPDATE mail_rule_tbl SET"
7481                 "  filter_name = ?"
7482                 ", type = ?"
7483                 ", value = ?"
7484                 ", value2 = ?"
7485                 ", action_type = ?"
7486                 ", target_mailbox_id = ?"
7487                 ", flag1 = ?"
7488                 ", flag2 = ?"
7489                 ", account_id = ?"
7490                 ", rule_id = ?"
7491                 " WHERE rule_id = %d"
7492                 , rule_id);
7493
7494
7495         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7496         EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
7497         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7498                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7499
7500         int i = 0;
7501
7502         _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->filter_name, 0, FILTER_NAME_LEN_IN_MAIL_RULE_TBL);
7503         _bind_stmt_field_data_int(hStmt, i++, new_rule->type);
7504         _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7505         _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7506         _bind_stmt_field_data_int(hStmt, i++, new_rule->action_type);
7507         _bind_stmt_field_data_int(hStmt, i++, new_rule->target_mailbox_id);
7508         _bind_stmt_field_data_int(hStmt, i++, new_rule->flag1);
7509         _bind_stmt_field_data_int(hStmt, i++, new_rule->flag2);
7510         _bind_stmt_field_data_int(hStmt, i++, new_rule->account_id);
7511         _bind_stmt_field_data_int(hStmt, i++, rule_id);
7512
7513
7514         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7515         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7516                 ("sqlite3_step fail:%d", rc));
7517
7518         ret = true;
7519
7520 FINISH_OFF:
7521         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7522         if (hStmt != NULL)  {
7523                 rc = sqlite3_finalize(hStmt);
7524                 if (rc != SQLITE_OK)  {
7525                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
7526                         error = EMAIL_ERROR_DB_FAILURE;
7527                 }
7528         }
7529
7530         if (err_code != NULL)
7531                 *err_code = error;
7532
7533         EM_DEBUG_FUNC_END("ret [%d]", ret);
7534         return ret;
7535 }
7536
7537 INTERNAL_FUNC int emstorage_find_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7538 {
7539         EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7540
7541         if (!rule) {
7542                 EM_DEBUG_LOG("rule is NULL");
7543                 if (err_code != NULL)
7544                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7545                 return false;
7546         }
7547
7548         DB_STMT hStmt = NULL;
7549         char sql_query_string[QUERY_SIZE] = {0,};
7550         int error = EMAIL_ERROR_NONE;
7551         int rc = 0;
7552         int ret = false;
7553
7554         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7555         EMSTORAGE_START_READ_TRANSACTION(transaction);
7556
7557         switch(rule->action_type) {
7558         case EMAIL_FILTER_MOVE:
7559                 if (rule->type == EMAIL_PRIORITY_SENDER) {
7560                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7561                                 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7562                                 rule->action_type, rule->type, rule->value2);
7563                 } else {
7564                                 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7565                                         "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(filter_name) = UPPER(\'%q\')", rule->action_type, rule->type, rule->filter_name);
7566                 }
7567                 break;
7568         case EMAIL_FILTER_BLOCK:
7569                 if (rule->type == EMAIL_FILTER_FROM)
7570                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7571                                 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7572                                 rule->action_type, rule->type, rule->value2);
7573                 else if (rule->type == EMAIL_FILTER_SUBJECT)
7574                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7575                                 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value) = UPPER(\'%q\')",
7576                                 rule->action_type, rule->type, rule->value);
7577                 else if (rule->type == (EMAIL_FILTER_SUBJECT | EMAIL_FILTER_FROM))
7578                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7579                                 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND (type = %d AND UPPER(value) = UPPER(\'%q\')) OR (type = %d AND UPPER(value2) = UPPER(\'%q\'))",
7580                                 rule->action_type, EMAIL_FILTER_SUBJECT, rule->value, EMAIL_FILTER_FROM, rule->value2);
7581                 break;
7582
7583         default:
7584                 EM_DEBUG_EXCEPTION("Invalid parameter : rule->action_type[%d]", rule->action_type);
7585                 error = EMAIL_ERROR_INVALID_PARAM;
7586                 goto FINISH_OFF;
7587                 break;
7588         }
7589
7590         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7591         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF;},
7592                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7593
7594         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7595         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF;},
7596                 ("sqlite3_step fail:%d", rc));
7597
7598         if (rc == SQLITE_DONE) {
7599                 EM_DEBUG_EXCEPTION(" no matched rule found...");
7600                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7601         }
7602
7603         ret = true;
7604
7605 FINISH_OFF:
7606
7607         if (hStmt != NULL) {
7608                 rc = sqlite3_finalize(hStmt);
7609                 if (rc != SQLITE_OK) {
7610                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
7611                         error = EMAIL_ERROR_DB_FAILURE;
7612                 }
7613         }
7614
7615         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7616
7617         if (err_code)
7618                 *err_code = error;
7619
7620         EM_DEBUG_FUNC_END("ret [%d]", ret);
7621         return ret;
7622 }
7623
7624 INTERNAL_FUNC int emstorage_add_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7625 {
7626         EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7627
7628         if (!rule) {    /*  only global rule supported. */
7629                 EM_DEBUG_LOG("rule is NULL");
7630                 if (err_code != NULL)
7631                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7632                 return false;
7633         }
7634
7635         int rc, rc_2, ret = false;
7636         int error = EMAIL_ERROR_NONE;
7637         DB_STMT hStmt = NULL;
7638         char sql_query_string[QUERY_SIZE] = {0, };
7639
7640         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7641
7642         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7643
7644         char *sql;
7645         char **result;
7646         sql = "SELECT max(rowid) FROM mail_rule_tbl;";
7647
7648         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7649         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
7650                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7651
7652         if (NULL==result[1])
7653                 rc = 1;
7654         else
7655                 rc = atoi(result[1])+1;
7656
7657         sqlite3_free_table(result);
7658
7659         rule->rule_id = rc;
7660
7661         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7662                 "INSERT INTO mail_rule_tbl VALUES "
7663                 "( ?"           /*  account id */
7664                 ", ?"           /*  rule_id */
7665                 ", ?"           /*  filter_name */
7666                 ", ?"           /*  type */
7667                 ", ?"           /*  value */
7668                 ", ?"           /*  value2 */
7669                 ", ?"           /*  action_type */
7670                 ", ?"           /*  target_mailbox_id */
7671                 ", ?"           /*  flag1 */
7672                 ", ?)");        /*  flag2 */
7673
7674         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc_2);
7675         if (rc_2 != SQLITE_OK)  {
7676                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc_2, sqlite3_errmsg(local_db_handle));
7677                 error = EMAIL_ERROR_DB_FAILURE;
7678                 goto FINISH_OFF;
7679         }
7680
7681         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL, rule->account_id);
7682         _bind_stmt_field_data_int(hStmt, RULE_ID_IDX_IN_MAIL_RULE_TBL, rule->rule_id);
7683         _bind_stmt_field_data_string(hStmt, FILTER_NAME_IDX_IN_MAIL_RULE_TBL, (char*)rule->filter_name, 0, FILTER_NAME_LEN_IN_MAIL_RULE_TBL);
7684         _bind_stmt_field_data_int(hStmt, TYPE_IDX_IN_MAIL_RULE_TBL, rule->type);
7685         _bind_stmt_field_data_string(hStmt, VALUE_IDX_IN_MAIL_RULE_TBL, (char*)rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7686         _bind_stmt_field_data_string(hStmt, VALUE2_IDX_IN_MAIL_RULE_TBL, (char*)rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7687         _bind_stmt_field_data_int(hStmt, ACTION_TYPE_IDX_IN_MAIL_RULE_TBL, rule->action_type);
7688         _bind_stmt_field_data_int(hStmt, TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL, rule->target_mailbox_id);
7689         _bind_stmt_field_data_int(hStmt, FLAG1_IDX_IN_MAIL_RULE_TBL, rule->flag1);
7690         _bind_stmt_field_data_int(hStmt, FLAG2_IDX_IN_MAIL_RULE_TBL, rule->flag2);
7691
7692         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7693         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7694                 ("sqlite3_step fail:%d", rc));
7695
7696         ret = true;
7697
7698 FINISH_OFF:
7699         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7700         if (hStmt != NULL)  {
7701                 rc = sqlite3_finalize(hStmt);
7702                 if (rc != SQLITE_OK) {
7703                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
7704                         error = EMAIL_ERROR_DB_FAILURE;
7705                 }
7706         }
7707
7708         if (err_code != NULL)
7709                 *err_code = error;
7710
7711         EM_DEBUG_FUNC_END("ret [%d]", ret);
7712         return ret;
7713 }
7714
7715 INTERNAL_FUNC int emstorage_delete_rule(char *multi_user_name, int rule_id, int transaction, int *err_code)
7716 {
7717         EM_DEBUG_FUNC_BEGIN("rule_id[%d], transaction[%d], err_code[%p]", rule_id, transaction, err_code);
7718
7719         if (rule_id <= 0) {             /*  only global rule supported. */
7720                 EM_DEBUG_EXCEPTION("rule_id[%d]", rule_id);
7721
7722                 if (err_code != NULL)
7723                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7724                 return false;
7725         }
7726
7727         int rc, ret = false;
7728         int error = EMAIL_ERROR_NONE;
7729         char sql_query_string[QUERY_SIZE] = {0, };
7730         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7731         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7732
7733         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7734         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7735         if (error != EMAIL_ERROR_NONE) {
7736                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7737                         goto FINISH_OFF;
7738         }
7739
7740         rc = sqlite3_changes(local_db_handle);
7741         if (rc == 0) {
7742                 EM_DEBUG_EXCEPTION(" no matched rule found...");
7743
7744                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7745                 goto FINISH_OFF;
7746         }
7747
7748         ret = true;
7749
7750 FINISH_OFF:
7751         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7752
7753         if (err_code != NULL)
7754                 *err_code = error;
7755
7756         EM_DEBUG_FUNC_END("ret [%d]", ret);
7757         return ret;
7758 }
7759
7760 INTERNAL_FUNC int emstorage_free_rule(emstorage_rule_tbl_t** rule_list, int count, int *err_code)
7761 {
7762         EM_DEBUG_FUNC_BEGIN("rule_list[%p], conut[%d], err_code[%p]", rule_list, count, err_code);
7763
7764         int ret = false;
7765         int error = EMAIL_ERROR_NONE;
7766
7767         if (count > 0) {
7768                 if (!rule_list || !*rule_list) {
7769                         EM_DEBUG_EXCEPTION(" rule_list[%p], conut[%d]", rule_list, count);
7770
7771                         error = EMAIL_ERROR_INVALID_PARAM;
7772                         goto FINISH_OFF;
7773                 }
7774
7775                 emstorage_rule_tbl_t* p = *rule_list;
7776                 int i = 0;
7777
7778                 for (; i < count; i++) {
7779                         EM_SAFE_FREE(p[i].value);
7780                 }
7781
7782                 EM_SAFE_FREE(p); *rule_list = NULL;
7783         }
7784
7785 FINISH_OFF:
7786         if (err_code != NULL)
7787                 *err_code = error;
7788
7789         EM_DEBUG_FUNC_END("ret [%d]", ret);
7790         return ret;
7791 }
7792
7793 INTERNAL_FUNC int emstorage_get_mail_count(char *multi_user_name, int account_id, int mailbox_id, int *total, int *unseen, int transaction, int *err_code)
7794 {
7795         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], total[%p], unseen[%p], transaction[%d], err_code[%p]", account_id, mailbox_id, total, unseen, transaction, err_code);
7796
7797         if (!total && !unseen) {
7798                 EM_DEBUG_EXCEPTION(" accoun_id[%d], mailbox_id[%d], total[%p], unseen[%p]", account_id, mailbox_id, total, unseen);
7799                 if (err_code != NULL)
7800                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7801                 return false;
7802         }
7803
7804         int rc = -1, ret = false;
7805         int error = EMAIL_ERROR_NONE;
7806         DB_STMT hStmt = NULL;
7807         char sql_query_string[QUERY_SIZE] = {0, };
7808         char *replaced_mailbox_name = NULL;
7809
7810         memset(&sql_query_string, 0x00, sizeof(sql_query_string));
7811         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7812         EMSTORAGE_START_READ_TRANSACTION(transaction);
7813
7814         if (total)  {
7815                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl");
7816
7817                 if (account_id != ALL_ACCOUNT)  {
7818                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE account_id = %d", account_id);
7819                         if (mailbox_id)
7820                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND mailbox_id = %d", mailbox_id);
7821                 }
7822                 else if (mailbox_id)
7823                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE mailbox_id = %d", mailbox_id);
7824
7825 #ifdef USE_GET_RECORD_COUNT_API
7826                 char **result;
7827
7828                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7829                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF2; },
7830                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7831
7832                 *total = atoi(result[1]);
7833                 sqlite3_free_table(result);
7834 #else
7835
7836                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7837                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF2; },
7838                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7839
7840                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7841                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF2; },
7842                         ("sqlite3_step fail:%d", rc));
7843                 _get_stmt_field_data_int(hStmt, total, 0);
7844 #endif          /*  USE_GET_RECORD_COUNT_API */
7845         }
7846
7847         if (unseen)  {
7848                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl WHERE flags_seen_field = 0");               /*  fSEEN = 0x01 */
7849
7850                 if (account_id != ALL_ACCOUNT)
7851                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND account_id = %d", account_id);
7852
7853                 if (mailbox_id) {
7854                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND mailbox_id = %d", mailbox_id);
7855                 }
7856                 else
7857                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND mailbox_type NOT IN (3, 5)");
7858
7859                 char **result;
7860                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7861                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
7862                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7863
7864                 *unseen = atoi(result[1]);
7865                 sqlite3_free_table(result);
7866
7867         }
7868 FINISH_OFF:
7869         ret = true;
7870
7871 FINISH_OFF2:
7872
7873 #ifndef USE_PREPARED_QUERY_
7874         if (hStmt != NULL)  {
7875                 rc = sqlite3_finalize(hStmt);
7876                 if (rc != SQLITE_OK)  {
7877                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
7878                         error = EMAIL_ERROR_DB_FAILURE;
7879                 }
7880         }
7881 #endif
7882
7883         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7884
7885         EM_SAFE_FREE(replaced_mailbox_name);
7886
7887         if (err_code != NULL)
7888                 *err_code = error;
7889
7890         EM_DEBUG_FUNC_END("ret [%d]", ret);
7891         return ret;
7892 }
7893
7894 INTERNAL_FUNC int emstorage_get_mail_field_by_id(char *multi_user_name, int mail_id, int type, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
7895 {
7896         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
7897
7898         if (mail_id <= 0 || !mail)  {
7899                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
7900                 if (err_code != NULL)
7901                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7902                 return false;
7903         }
7904
7905         int col_index = 0;
7906         emstorage_mail_tbl_t* p_data_tbl = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
7907
7908         if (p_data_tbl == NULL)  {
7909                 EM_DEBUG_EXCEPTION("malloc failed...");
7910                 if (err_code != NULL)
7911                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
7912                 return false;
7913         }
7914
7915         memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
7916         DB_STMT hStmt = NULL;
7917         char sql_query_string[QUERY_SIZE] = {0, };
7918
7919         int rc, ret = false;
7920         int error = EMAIL_ERROR_NONE;
7921
7922         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7923         EMSTORAGE_START_READ_TRANSACTION(transaction);
7924
7925         switch (type)  {
7926                 case RETRIEVE_SUMMARY:
7927                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7928                                 "SELECT account_id, "
7929                                 "mail_id, "
7930                                 "mailbox_id, "
7931                                 "server_mail_status, "
7932                                 "server_mailbox_name, "
7933                                 "server_mail_id, "
7934                                 "file_path_plain, "
7935                                 "file_path_html,"
7936                                 "file_path_mime_entity, "
7937                                 "flags_seen_field, "
7938                                 "save_status, "
7939                                 "lock_status, "
7940                                 "thread_id, "
7941                                 "thread_item_count "
7942                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
7943                         break;
7944
7945                 case RETRIEVE_FIELDS_FOR_DELETE:
7946                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7947                                 "SELECT account_id, "
7948                                 "mail_id, "
7949                                 "server_mail_status, "
7950                                 "server_mailbox_name, "
7951                                 "server_mail_id "
7952                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
7953                         break;
7954
7955                 case RETRIEVE_ACCOUNT:
7956                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7957                                 "SELECT account_id "
7958                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
7959                         break;
7960
7961                 case RETRIEVE_FLAG:
7962                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7963                                 "SELECT account_id, "
7964                                 "flags_seen_field, "
7965                                 "thread_id, "
7966                                 "mailbox_id "
7967                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
7968                         break;
7969
7970                 default :
7971                         EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
7972                         error = EMAIL_ERROR_INVALID_PARAM;
7973                         goto FINISH_OFF;
7974         }
7975
7976         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
7977
7978         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7979         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7980                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7981
7982
7983         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7984         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
7985                 ("sqlite3_step fail:%d", rc));
7986
7987         if (rc == SQLITE_DONE)  {
7988                 EM_DEBUG_LOG("no matched mail found...");
7989                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7990                 goto FINISH_OFF;
7991         }
7992         switch (type)  {
7993                 case RETRIEVE_SUMMARY:
7994                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
7995                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
7996                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
7997                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
7998                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
7999                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8000                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, col_index++);
8001                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, col_index++);
8002                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, col_index++);
8003                         _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8004                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), col_index++);
8005                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), col_index++);
8006                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8007                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_item_count), col_index++);
8008                         break;
8009
8010                 case RETRIEVE_FIELDS_FOR_DELETE:
8011                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8012                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
8013                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
8014                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
8015                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8016                         break;
8017
8018                 case RETRIEVE_ACCOUNT:
8019                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8020                         break;
8021
8022                 case RETRIEVE_FLAG:
8023                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8024                         _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8025                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8026                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
8027                         break;
8028         }
8029
8030         ret = true;
8031
8032 FINISH_OFF:
8033         if (ret == true)
8034                 *mail = p_data_tbl;
8035         else if (p_data_tbl != NULL)
8036                 emstorage_free_mail(&p_data_tbl,  1, NULL);
8037
8038         if (hStmt != NULL)  {
8039                 rc = sqlite3_finalize(hStmt);
8040                 if (rc != SQLITE_OK)  {
8041                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
8042                         error = EMAIL_ERROR_DB_FAILURE;
8043                 }
8044         }
8045
8046
8047         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8048
8049         if (err_code != NULL)
8050                 *err_code = error;
8051
8052         EM_DEBUG_FUNC_END("ret [%d]", ret);
8053         return ret;
8054 }
8055
8056 INTERNAL_FUNC int emstorage_get_mail_field_by_multiple_mail_id(char *multi_user_name, int mail_ids[], int number_of_mails, int type, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
8057 {
8058         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);
8059
8060         int ret = false;
8061         int error = EMAIL_ERROR_NONE;
8062         int query_string_length = 0;
8063         int i = 0, item_count = 0, rc = -1, field_count, col_index, cur_sql_query_string = 0;
8064         char **result = NULL;
8065         char *sql_query_string = NULL;
8066         emstorage_mail_tbl_t* p_data_tbl = NULL;
8067         sqlite3 *local_db_handle = NULL;
8068
8069         if (number_of_mails <= 0 || !mail_ids)  {
8070                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8071                 if (err_code != NULL)
8072                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8073                 return false;
8074         }
8075
8076         p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * number_of_mails);
8077
8078         query_string_length = (sizeof(char) * 8 * number_of_mails) + 512;
8079         sql_query_string = (char*)em_malloc(query_string_length);
8080
8081         if (p_data_tbl == NULL || sql_query_string == NULL)  {
8082                 EM_DEBUG_EXCEPTION("malloc failed...");
8083
8084                 EM_SAFE_FREE(p_data_tbl);
8085                 EM_SAFE_FREE(sql_query_string);
8086
8087                 if (err_code != NULL)
8088                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8089                 return false;
8090         }
8091
8092         local_db_handle = emstorage_get_db_connection(multi_user_name);
8093
8094         EMSTORAGE_START_READ_TRANSACTION(transaction);
8095
8096         switch (type) {
8097                 case RETRIEVE_SUMMARY:
8098                         cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8099                                 "SELECT account_id, "
8100                                 "mail_id, "
8101                                 "mailbox_id, "
8102                                 "server_mail_status, "
8103                                 "server_mailbox_name, "
8104                                 "server_mail_id, "
8105                                 "file_path_plain, "
8106                                 "file_path_html, "
8107                                 "file_path_mime_entity, "
8108                                 "subject, "
8109                                 "flags_seen_field, "
8110                                 "save_status, "
8111                                 "lock_status, "
8112                                 "thread_id, "
8113                                 "thread_item_count "
8114                                 "FROM mail_tbl WHERE mail_id in (");
8115                         field_count = 15;
8116                         break;
8117
8118                 case RETRIEVE_FIELDS_FOR_DELETE:
8119                         cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8120                                 "SELECT account_id, "
8121                                 "mail_id, "
8122                                 "server_mail_status, "
8123                                 "server_mailbox_name, "
8124                                 "server_mail_id "
8125                                 "FROM mail_tbl WHERE mail_id in (");
8126                         field_count = 5;
8127                         break;
8128
8129                 case RETRIEVE_ACCOUNT:
8130                         cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8131                                 "SELECT account_id FROM mail_tbl WHERE mail_id in (");
8132                         field_count = 1;
8133                         break;
8134
8135                 case RETRIEVE_FLAG:
8136                         cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8137                                 "SELECT account_id, "
8138                                 "mail_id, "
8139                                 "mailbox_id, "
8140                                 "flags_seen_field, "
8141                                 "thread_id "
8142                                 "FROM mail_tbl WHERE mail_id in (");
8143                         field_count = 5;
8144                         break;
8145
8146                 default :
8147                         EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
8148                         error = EMAIL_ERROR_INVALID_PARAM;
8149                         goto FINISH_OFF;
8150         }
8151
8152         for(i = 0; i < number_of_mails; i++)
8153                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_string_length, "%d,", mail_ids[i]);
8154         sql_query_string[EM_SAFE_STRLEN(sql_query_string) - 1] = ')';
8155
8156         EM_DEBUG_LOG_SEC("Query [%s], Length [%d]", sql_query_string, EM_SAFE_STRLEN(sql_query_string));
8157
8158         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &item_count, 0, NULL), rc);
8159         if (SQLITE_OK != rc && -1 != rc) {
8160                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
8161                 error = EMAIL_ERROR_DB_FAILURE;
8162                 goto FINISH_OFF;
8163         }
8164
8165         if (rc == SQLITE_DONE)  {
8166                 EM_DEBUG_LOG("no matched mail found...");
8167                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8168                 goto FINISH_OFF;
8169         }
8170
8171         EM_DEBUG_LOG("item_count [%d]", item_count);
8172
8173         if(number_of_mails != item_count) {
8174                 EM_DEBUG_EXCEPTION("Can't find all emails");
8175                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8176                 goto FINISH_OFF;
8177         }
8178
8179         col_index = field_count;
8180
8181         for(i = 0; i < item_count; i++) {
8182                 switch (type) {
8183                         case RETRIEVE_SUMMARY:
8184                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8185                                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8186                                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8187                                 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8188                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8189                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8190                                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
8191                                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
8192                                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
8193                                 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 0, col_index++);
8194                                 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8195                                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
8196                                 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
8197                                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8198                                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
8199                                 break;
8200
8201                         case RETRIEVE_FIELDS_FOR_DELETE:
8202                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8203                                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8204                                 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8205                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8206                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8207                                 break;
8208
8209                         case RETRIEVE_ACCOUNT:
8210                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8211                                 break;
8212
8213                         case RETRIEVE_FLAG:
8214                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8215                                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8216                                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8217                                 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8218                                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8219                                 break;
8220                 }
8221         }
8222
8223         ret = true;
8224
8225 FINISH_OFF:
8226         if (ret == true)
8227                 *mail = p_data_tbl;
8228         else
8229                 emstorage_free_mail(&p_data_tbl, number_of_mails, NULL);
8230
8231         if (result)
8232                 sqlite3_free_table(result);
8233
8234         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8235
8236         EM_SAFE_FREE(sql_query_string);
8237
8238         if (err_code != NULL)
8239                 *err_code = error;
8240
8241         EM_DEBUG_FUNC_END("ret [%d]", ret);
8242         return ret;
8243 }
8244
8245 INTERNAL_FUNC int emstorage_get_mail_by_id(char *multi_user_name, int mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code)
8246 {
8247         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8248
8249         if (mail_id <= 0 || !mail) {
8250                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
8251                 if (err_code != NULL)
8252                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8253                 return false;
8254         }
8255
8256         int ret = false, error = EMAIL_ERROR_NONE, count;
8257         char conditional_clause[QUERY_SIZE] = {0, };
8258         emstorage_mail_tbl_t* p_data_tbl = NULL;
8259
8260         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8261         EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8262
8263         if(!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8264                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8265                 goto FINISH_OFF;
8266         }
8267
8268         ret = true;
8269
8270 FINISH_OFF:
8271         if (ret == true)
8272                 *mail = p_data_tbl;
8273         else if (p_data_tbl != NULL)
8274                 emstorage_free_mail(&p_data_tbl, 1, &error);
8275
8276         if (err_code != NULL)
8277                 *err_code = error;
8278
8279         EM_DEBUG_FUNC_END("ret [%d]", ret);
8280         return ret;
8281 }
8282
8283 #ifdef __FEATURE_BODY_SEARCH__
8284 INTERNAL_FUNC int emstorage_get_mail_text_by_id(char *multi_user_name, int mail_id, emstorage_mail_text_tbl_t **mail_text, int transaction, int *err_code)
8285 {
8286         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
8287
8288         if (mail_id <= 0 || !mail_text) {
8289                 EM_DEBUG_EXCEPTION("mail_id[%d], mail_text[%p]", mail_id, mail_text);
8290                 if (err_code != NULL)
8291                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8292                 return false;
8293         }
8294
8295         int ret = false;
8296         int error = EMAIL_ERROR_NONE;
8297         int count = 0;
8298         char conditional_clause[QUERY_SIZE] = {0, };
8299         emstorage_mail_text_tbl_t *p_data_tbl = NULL;
8300
8301         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8302         EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8303
8304         if(!emstorage_query_mail_text_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8305                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8306                 goto FINISH_OFF;
8307         }
8308
8309         ret = true;
8310
8311 FINISH_OFF:
8312         if (ret == true)
8313                 *mail_text = p_data_tbl;
8314         else if (p_data_tbl != NULL)
8315                 emstorage_free_mail_text(&p_data_tbl, 1, &error);
8316
8317         if (err_code != NULL)
8318                 *err_code = error;
8319
8320         EM_DEBUG_FUNC_END("ret [%d]", ret);
8321         return ret;
8322 }
8323 #endif
8324
8325 INTERNAL_FUNC int emstorage_mail_search_start(char *multi_user_name, 
8326                                                                                         emstorage_search_filter_t *search, 
8327                                                                                         int account_id, 
8328                                                                                         int mailbox_id, 
8329                                                                                         int sorting, 
8330                                                                                         DB_STMT *search_handle, 
8331                                                                                         int *searched, 
8332                                                                                         int transaction, 
8333                                                                                         int *err_code)
8334 {
8335         EM_DEBUG_FUNC_BEGIN("search[%p], account_id[%d], mailbox_id[%d], sorting[%d], " 
8336                                                 "search_handle[%p], searched[%p], transaction[%d], err_code[%p]", 
8337                                                 search, account_id, mailbox_id, sorting, search_handle, 
8338                                                 searched, transaction, err_code);
8339
8340         if (!search_handle || !searched)  {
8341                 if (err_code != NULL)
8342                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8343                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8344                 EM_DEBUG_FUNC_END("false");
8345                 return false;
8346         }
8347
8348         emstorage_search_filter_t* p = search;
8349         int error = EMAIL_ERROR_NONE;
8350         DB_STMT hStmt = NULL;
8351         char sql_query_string[QUERY_SIZE] = {0, };
8352         int rc, ret = false;
8353         int and = false, mail_count = 0;
8354
8355         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8356         EMSTORAGE_START_READ_TRANSACTION(transaction);
8357
8358         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl");
8359
8360         if (account_id != ALL_ACCOUNT)  {
8361                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE account_id = %d", account_id);
8362                 and = true;
8363         }
8364
8365         if (mailbox_id)  {
8366                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s mailbox_id = %d", and ? "AND" : "WHERE", mailbox_id);
8367                 and = true;
8368         }
8369
8370         while (p)  {
8371
8372                 if (p->key_type) {
8373                         if (!strncmp(p->key_type, "subject", strlen("subject"))) {
8374                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s subject LIKE '%%%%%s%%%%'", and ? "AND" : "WHERE", p->key_value);
8375                                 and = true;
8376                         }
8377                         else if (!strncmp(p->key_type, "full_address_from", strlen("full_address_from"))) {
8378                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s full_address_from LIKE '%%%%%s%%%%'", and ? "AND" : "WHERE", p->key_value);
8379                                 and = true;
8380                         }
8381                         else if (!strncmp(p->key_type, "full_address_to", strlen("full_address_to"))) {
8382                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s full_address_to LIKE '%%%%%s%%%%'", and ? "AND" : "WHERE", p->key_value);
8383                                 and = true;
8384                         }
8385                         else if (!strncmp(p->key_type, "email_address", strlen("email_address"))) {
8386                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s email_address_sender = '%s' OR email_address_recipient = '%s'", and ? "AND" : "WHERE", p->key_value, p->key_value);
8387                                 and = true;
8388                         }
8389                         p = p->next;
8390                 }
8391         }
8392
8393         if (sorting)
8394                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " ORDER BY date_time");
8395
8396         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
8397
8398
8399         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8400         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8401                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8402
8403
8404         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8405         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8406                 ("sqlite3_step fail:%d", rc));
8407
8408         char **result;
8409
8410         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &mail_count, NULL, NULL), rc);
8411         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
8412                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8413
8414         sqlite3_free_table(result);
8415
8416         ret = true;
8417
8418 FINISH_OFF:
8419         if (ret == true)  {
8420                 *search_handle = hStmt;
8421                 *searched = mail_count;
8422                 EM_DEBUG_LOG("mail_count [%d]", mail_count);
8423         }
8424         else  {
8425                 if (hStmt != NULL)  {
8426                         rc = sqlite3_finalize(hStmt);
8427                         if (rc != SQLITE_OK)  {
8428                                 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
8429                                 error = EMAIL_ERROR_DB_FAILURE;
8430                         }
8431                 }
8432
8433                 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8434         }
8435
8436         if (err_code != NULL)
8437                 *err_code = error;
8438
8439         EM_DEBUG_FUNC_END("ret [%d]", ret);
8440         return ret;
8441 }
8442
8443 INTERNAL_FUNC int emstorage_mail_search_result(DB_STMT search_handle, emstorage_mail_field_type_t type, void** data, int transaction, int *err_code)
8444 {
8445         EM_DEBUG_FUNC_BEGIN("search_handle[%d], type[%d], data[%p], transaction[%d], err_code[%p]", search_handle, type, data, transaction, err_code);
8446
8447         if (search_handle == 0 || !data) {
8448                 EM_DEBUG_EXCEPTION(" search_handle[%d], type[%d], data[%p]", search_handle, type, data);
8449
8450                 if (err_code != NULL)
8451                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8452                 return false;
8453         }
8454
8455         emstorage_mail_tbl_t* p_data_tbl = NULL;
8456         DB_STMT hStmt = search_handle;
8457         int rc, ret = false;
8458         int error = EMAIL_ERROR_NONE;
8459
8460         switch (type)  {
8461                 case RETRIEVE_ID:
8462                         _get_stmt_field_data_int(hStmt, (int *)data, MAIL_ID_IDX_IN_MAIL_TBL);
8463                         break;
8464
8465                 case RETRIEVE_ENVELOPE:
8466                 case RETRIEVE_ALL:
8467                         if (!(p_data_tbl = em_malloc(sizeof(emstorage_mail_tbl_t)))) {
8468                                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
8469                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
8470                                 goto FINISH_OFF;
8471                         }
8472
8473                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8474                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8475                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->mail_size), MAIL_SIZE_IDX_IN_MAIL_TBL);
8476                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8477                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_from), 1, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL);
8478                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_to), 1, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL);
8479                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject), 1, SUBJECT_IDX_IN_MAIL_TBL);
8480                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->body_download_status), BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL);
8481                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL);
8482                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8483                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8484                         _get_stmt_field_data_time_t(hStmt, &(p_data_tbl->date_time), DATETIME_IDX_IN_MAIL_TBL);
8485                         _get_stmt_field_data_char  (hStmt, &(p_data_tbl->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL);
8486                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->DRM_status), DRM_STATUS_IDX_IN_MAIL_TBL);
8487                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->priority), PRIORITY_IDX_IN_MAIL_TBL);
8488                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->save_status), SAVE_STATUS_IDX_IN_MAIL_TBL);
8489                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->lock_status), LOCK_STATUS_IDX_IN_MAIL_TBL);
8490                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->report_status), REPORT_STATUS_IDX_IN_MAIL_TBL);
8491                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8492                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->meeting_request_status), MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL);
8493                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->message_class), MESSAGE_CLASS_IDX_IN_MAIL_TBL);
8494                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->digest_type), DIGEST_TYPE_IDX_IN_MAIL_TBL);
8495                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->smime_type), SMIME_TYPE_IDX_IN_MAIL_TBL);
8496                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->scheduled_sending_time), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8497                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->remaining_resend_times), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8498                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->tag_id), TAG_ID_IDX_IN_MAIL_TBL);
8499                         _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->eas_data_length), EAS_DATA_LENGTH_IDX_IN_MAIL_TBL);
8500                         _get_stmt_field_data_blob   (hStmt, (void**)&(p_data_tbl->eas_data), EAS_DATA_IDX_IN_MAIL_TBL);
8501
8502                         if (type == RETRIEVE_ALL)  {
8503                                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8504                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8505                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_reply), 1, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL);
8506                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_cc), 1, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL);
8507                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_bcc), 1, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL);
8508                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_return), 1, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL);
8509                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->message_id), 0, MESSAGE_ID_IDX_IN_MAIL_TBL);
8510                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8511                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8512                                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl->attachment_count), ATTACHMENT_COUNT_IDX_IN_MAIL_TBL);
8513                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8514                                 _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->replied_time), REPLIED_TIME_IDX_IN_MAIL_TBL);
8515                                 _get_stmt_field_data_int   (hStmt, (int*)&(p_data_tbl->forwarded_time), FORWARDED_TIME_IDX_IN_MAIL_TBL);
8516                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->default_charset), 0, DEFAULT_CHARSET_IDX_IN_MAIL_TBL);
8517                         }
8518
8519                         if (p_data_tbl->body_download_status)  {
8520                                 struct stat buf;
8521
8522                                 if (p_data_tbl->file_path_html)  {
8523                                         if (stat(p_data_tbl->file_path_html, &buf) == -1)
8524                                                 p_data_tbl->body_download_status = 0;
8525                                 }
8526                                 else if (p_data_tbl->file_path_plain)  {
8527                                         if (stat(p_data_tbl->file_path_plain, &buf) == -1)
8528                                                 p_data_tbl->body_download_status = 0;
8529                                 }
8530                                 else
8531                                         p_data_tbl->body_download_status = 0;
8532                         }
8533
8534                         *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8535                         break;
8536
8537                 case RETRIEVE_SUMMARY:
8538                         if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t))))  {
8539                                 EM_DEBUG_EXCEPTION(" malloc failed...");
8540
8541                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
8542                                 goto FINISH_OFF;
8543                         }
8544
8545                         memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8546
8547                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8548                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8549                         _get_stmt_field_data_int   (hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8550                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8551                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8552
8553                         *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8554                         break;
8555
8556                 case RETRIEVE_ADDRESS:
8557                         if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t))))  {
8558                                 EM_DEBUG_EXCEPTION(" malloc failed...");
8559                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
8560                                 goto FINISH_OFF;
8561                         }
8562
8563                         memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8564                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8565                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8566                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8567                         *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8568                         break;
8569
8570                 default:
8571                         break;
8572         }
8573
8574         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8575         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8576                 ("sqlite3_step fail:%d", rc));
8577
8578         ret = true;
8579
8580 FINISH_OFF:
8581
8582         if (err_code != NULL)
8583                 *err_code = error;
8584
8585         if (ret == false && p_data_tbl)
8586                 emstorage_free_mail(&p_data_tbl, 1, NULL);
8587
8588         EM_DEBUG_FUNC_END("ret [%d]", ret);
8589         return ret;
8590 }
8591
8592 INTERNAL_FUNC int emstorage_mail_search_end(DB_STMT search_handle, int transaction, int *err_code)
8593 {
8594         EM_DEBUG_FUNC_BEGIN("search_handle[%d], transaction[%d], err_code[%p]", search_handle, transaction, err_code);
8595
8596         int error = EMAIL_ERROR_NONE;
8597         int rc, ret = false;
8598
8599         if (search_handle == 0)  {
8600                 EM_DEBUG_EXCEPTION(" search_handle[%d]", search_handle);
8601                 error = EMAIL_ERROR_INVALID_PARAM;
8602                 goto FINISH_OFF;
8603         }
8604
8605         DB_STMT hStmt = search_handle;
8606
8607         rc = sqlite3_finalize(hStmt);
8608         if (rc != SQLITE_OK)  {
8609                 EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
8610                 error = EMAIL_ERROR_DB_FAILURE;
8611         }
8612
8613         ret = true;
8614
8615 FINISH_OFF:
8616         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8617
8618         if (err_code != NULL)
8619                 *err_code = error;
8620
8621         EM_DEBUG_FUNC_END("ret [%d]", ret);
8622         return ret;
8623 }
8624
8625 INTERNAL_FUNC int emstorage_change_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t *mail, int transaction, int *err_code)
8626 {
8627         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8628
8629         if (mail_id <= 0 || !mail) {
8630                 EM_DEBUG_EXCEPTION(" mail_id[%d], mail[%p]", mail_id, mail);
8631
8632                 if (err_code != NULL)
8633                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8634                 return false;
8635         }
8636
8637         DB_STMT hStmt = NULL;
8638         char sql_query_string[QUERY_SIZE] = {0, };
8639         int rc = -1;
8640         int ret = false;
8641         int error = EMAIL_ERROR_NONE;
8642         int i = 0;
8643         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8644         char mailbox_id_param_string[10] = {0,};
8645
8646         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
8647
8648         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8649                 "UPDATE mail_tbl SET"
8650                 "  mail_id = ?"
8651                 ", account_id = ?"
8652                 ", mailbox_id = ?"
8653                 ", mail_size = ?"
8654                 ", server_mail_status = ?"
8655                 ", server_mailbox_name = ?"
8656                 ", server_mail_id = ?"
8657                 ", reference_mail_id = ?"
8658                 ", full_address_from = ?"
8659                 ", full_address_reply = ?"  /* 10 */
8660                 ", full_address_to = ?"
8661                 ", full_address_cc = ?"
8662                 ", full_address_bcc = ?"
8663                 ", full_address_return = ?"
8664                 ", subject = ?"
8665                 ", body_download_status = ?"
8666                 ", file_path_plain = ?"
8667                 ", file_path_html = ?"
8668                 ", file_path_mime_entity = ?"
8669                 ", date_time = ?"
8670                 ", flags_seen_field      = ?"
8671                 ", flags_deleted_field   = ?"
8672                 ", flags_flagged_field   = ?"
8673                 ", flags_answered_field  = ?"
8674                 ", flags_recent_field    = ?"
8675                 ", flags_draft_field     = ?"
8676                 ", flags_forwarded_field = ?"
8677                 ", DRM_status = ?"
8678                 ", priority = ?"
8679                 ", save_status = ?"
8680                 ", lock_status = ?"
8681                 ", message_id = ?"
8682                 ", report_status = ?"
8683                 ", preview_text = ?"
8684                 ", smime_type = ?"
8685                 ", scheduled_sending_time = ?"
8686                 ", remaining_resend_times = ?"
8687                 ", tag_id = ?"
8688                 ", replied_time = ?"
8689                 ", forwarded_time = ?"
8690                 ", default_charset = ?"
8691                 ", eas_data_length = ?"
8692                 ", eas_data = ?"
8693                 " WHERE mail_id = %d AND account_id != 0 "
8694                 , mail_id);
8695
8696
8697         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8698         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8699                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8700
8701         _bind_stmt_field_data_int   (hStmt, i++, mail->mail_id);
8702         _bind_stmt_field_data_int   (hStmt, i++, mail->account_id);
8703         _bind_stmt_field_data_int   (hStmt, i++, mail->mailbox_id);
8704         _bind_stmt_field_data_int   (hStmt, i++, mail->mail_size);
8705         _bind_stmt_field_data_int   (hStmt, i++, mail->server_mail_status);
8706         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mailbox_name, 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
8707         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mail_id, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
8708         _bind_stmt_field_data_int   (hStmt, i++, mail->reference_mail_id);
8709         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
8710         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
8711         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
8712         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
8713         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
8714         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
8715         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8716         _bind_stmt_field_data_int   (hStmt, i++, mail->body_download_status);
8717         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8718         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8719         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
8720         _bind_stmt_field_data_int   (hStmt, i++, mail->date_time);
8721         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_seen_field);
8722         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_deleted_field);
8723         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_flagged_field);
8724         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_answered_field);
8725         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_recent_field);
8726         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_draft_field);
8727         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_forwarded_field);
8728         _bind_stmt_field_data_int   (hStmt, i++, mail->DRM_status);
8729         _bind_stmt_field_data_int   (hStmt, i++, mail->priority);
8730         _bind_stmt_field_data_int   (hStmt, i++, mail->save_status);
8731         _bind_stmt_field_data_int   (hStmt, i++, mail->lock_status);
8732         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->message_id, 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
8733         _bind_stmt_field_data_int   (hStmt, i++, mail->report_status);
8734         _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8735         _bind_stmt_field_data_int   (hStmt, i++, mail->smime_type);
8736         _bind_stmt_field_data_int   (hStmt, i++, mail->scheduled_sending_time);
8737         _bind_stmt_field_data_int   (hStmt, i++, mail->remaining_resend_times);
8738         _bind_stmt_field_data_int   (hStmt, i++, mail->tag_id);
8739         _bind_stmt_field_data_int   (hStmt, i++, mail->replied_time);
8740         _bind_stmt_field_data_int   (hStmt, i++, mail->forwarded_time);
8741         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
8742         _bind_stmt_field_data_int   (hStmt, i++, mail->eas_data_length);
8743         _bind_stmt_field_data_blob  (hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
8744
8745         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8746         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
8747                 ("sqlite3_step fail:%d", rc));
8748
8749         rc = sqlite3_changes(local_db_handle);
8750         if (rc == 0)  {
8751                 EM_DEBUG_LOG(" no matched mail found...");
8752                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8753                 goto FINISH_OFF;
8754         }
8755
8756         ret = true;
8757
8758 FINISH_OFF:
8759         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
8760         if (hStmt != NULL)  {
8761                 rc = sqlite3_finalize(hStmt);
8762                 if (rc != SQLITE_OK)  {
8763                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
8764                         error = EMAIL_ERROR_DB_FAILURE;
8765                 }
8766         }
8767
8768
8769         if(error == EMAIL_ERROR_NONE && mail) {
8770                 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
8771                 if (!emcore_notify_storage_event (NOTI_MAIL_UPDATE, mail->account_id, mail->mail_id, mailbox_id_param_string, 0))
8772                         EM_DEBUG_EXCEPTION ("emcore_notify_storage_event failed [NOTI_MAIL_UPDATE]");
8773         }
8774
8775         if (err_code != NULL)
8776                 *err_code = error;
8777
8778         EM_DEBUG_FUNC_END("ret [%d]", ret);
8779         return ret;
8780 }
8781
8782 /**
8783   *  emstorage_clean_save_status(int save_status, int  *err_code) - set the all mail status to the set value
8784   *
8785   *
8786   **/
8787 INTERNAL_FUNC int emstorage_clean_save_status(char *multi_user_name, int save_status, int  *err_code)
8788 {
8789         EM_DEBUG_FUNC_BEGIN("save_status[%d], err_code[%p]", save_status, err_code);
8790
8791         EM_IF_NULL_RETURN_VALUE(err_code, false);
8792
8793         int ret = false;
8794         int error = EMAIL_ERROR_NONE;
8795         int rc = 0;
8796         char sql_query_string[QUERY_SIZE] = {0, };
8797         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8798
8799         memset(sql_query_string, 0x00, sizeof(sql_query_string));
8800         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);
8801
8802         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
8803         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
8804         if (error != EMAIL_ERROR_NONE) {
8805                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
8806                         goto FINISH_OFF;
8807         }
8808
8809         rc = sqlite3_changes(local_db_handle);
8810         if (rc == 0) {
8811                 EM_DEBUG_LOG(" No Matched Mail Exists ");
8812                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8813         }
8814
8815         ret = true;
8816
8817 FINISH_OFF:
8818
8819         if (err_code != NULL)
8820                 *err_code = error;
8821
8822         EM_DEBUG_FUNC_END("ret [%d]", ret);
8823         return ret;
8824 }
8825
8826 INTERNAL_FUNC int emstorage_set_field_of_mails_with_integer_value(char *multi_user_name, int account_id, int mail_ids[], int mail_ids_count, char *field_name, int value, int transaction, int *err_code)
8827 {
8828         EM_DEBUG_FUNC_BEGIN_SEC("account_id [%d], mail_ids[%p], mail_ids_count[%d], field_name[%s], value[%d], transaction[%d], err_code[%p]", account_id, mail_ids, mail_ids_count, field_name, value, transaction, err_code);
8829         int i = 0;
8830         int error = EMAIL_ERROR_NONE;
8831         int ret = false;
8832         int query_size = 0;
8833         int cur_mail_id_string = 0;
8834         int mail_id_string_buffer_length = 0;
8835         int parameter_string_length = 0;
8836         char  *sql_query_string = NULL;
8837         char *mail_id_string_buffer = NULL;
8838         char *parameter_string = NULL;
8839         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8840         email_mail_attribute_type target_mail_attribute_type = 0;
8841
8842         if (!mail_ids  || !field_name || account_id == 0) {
8843                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8844                 if (err_code != NULL)
8845                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8846                 return false;
8847         }
8848
8849         if( (error = emcore_get_attribute_type_by_mail_field_name(field_name, &target_mail_attribute_type)) != EMAIL_ERROR_NONE) {
8850                 EM_DEBUG_EXCEPTION("emstorageemcore_get_attribute_type_by_mail_field_name failed [%d]", error);
8851                 if (err_code != NULL)
8852                         *err_code = error;
8853                 return false;
8854         }
8855
8856         /* Generating mail id list string */
8857         mail_id_string_buffer_length = MAIL_ID_STRING_LENGTH * mail_ids_count;
8858
8859         mail_id_string_buffer = em_malloc(mail_id_string_buffer_length);
8860
8861         if(!mail_id_string_buffer) {
8862                 EM_DEBUG_EXCEPTION("em_malloc failed");
8863                 if (err_code != NULL)
8864                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8865                 return false;
8866         }
8867
8868         for(i = 0; i < mail_ids_count; i++)
8869                 cur_mail_id_string += SNPRINTF_OFFSET(mail_id_string_buffer, cur_mail_id_string, mail_id_string_buffer_length, "%d,", mail_ids[i]);
8870
8871         if(EM_SAFE_STRLEN(mail_id_string_buffer) > 1)
8872                 mail_id_string_buffer[EM_SAFE_STRLEN(mail_id_string_buffer) - 1] = NULL_CHAR;
8873
8874         /* Generating notification parameter string */
8875         parameter_string_length = mail_id_string_buffer_length + EM_SAFE_STRLEN(field_name) + 2;
8876         parameter_string = em_malloc(parameter_string_length);
8877
8878         if(!parameter_string) {
8879                 EM_DEBUG_EXCEPTION("em_malloc failed");
8880                 if (err_code != NULL)
8881                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8882                 EM_SAFE_FREE (mail_id_string_buffer);
8883                 return false;
8884         }
8885
8886         SNPRINTF(parameter_string, parameter_string_length, "%s%c%s", field_name, 0x01, mail_id_string_buffer);
8887         query_size = EM_SAFE_STRLEN(mail_id_string_buffer) + EM_SAFE_STRLEN(field_name) + 250;
8888
8889         sql_query_string = em_malloc(query_size);
8890         if (sql_query_string == NULL) {
8891                 EM_DEBUG_EXCEPTION("em_malloc failed");
8892                 error = EMAIL_ERROR_OUT_OF_MEMORY;
8893                 goto FINISH_OFF;
8894         }
8895         /* Write query string */
8896         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);
8897
8898         EM_DEBUG_LOG_DEV ("sql_query_string [%s]", sql_query_string);
8899
8900         /* Execute query */
8901         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
8902         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
8903         if (error != EMAIL_ERROR_NONE) {
8904                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
8905                         goto FINISH_OFF;
8906         }
8907
8908         if (sqlite3_changes(local_db_handle) == 0)
8909                 EM_DEBUG_LOG("no mail matched...");
8910
8911         ret = true;
8912
8913 FINISH_OFF:
8914         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
8915
8916         if (error == EMAIL_ERROR_NONE && parameter_string) {
8917                 if (!emcore_notify_storage_event (NOTI_MAIL_FIELD_UPDATE, account_id, target_mail_attribute_type, parameter_string, value))
8918                         EM_DEBUG_EXCEPTION_SEC ("emcore_notify_storage_event failed : NOTI_MAIL_FIELD_UPDATE [%s,%d]", field_name, value);
8919         }
8920
8921         EM_SAFE_FREE(mail_id_string_buffer);
8922         EM_SAFE_FREE(parameter_string);
8923         EM_SAFE_FREE(sql_query_string);
8924
8925
8926         if (err_code != NULL)
8927                 *err_code = error;
8928
8929         EM_DEBUG_FUNC_END("error [%d]", error);
8930         return ret;
8931 }
8932
8933 #ifdef __FEATURE_BODY_SEARCH__
8934 INTERNAL_FUNC int emstorage_change_mail_text_field(char *multi_user_name, int mail_id, emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
8935 {
8936         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
8937
8938         if (mail_id <= 0 || !mail_text) {
8939                 EM_DEBUG_EXCEPTION(" mail_id[%d], mail_text[%p]", mail_id, mail_text);
8940                 if (err_code != NULL)
8941                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8942                 return false;
8943         }
8944
8945         int ret = false;
8946         int error = EMAIL_ERROR_NONE;
8947         DB_STMT hStmt = NULL;
8948         char sql_query_string[QUERY_SIZE] = {0, };
8949
8950         int i = 0;
8951         int rc = 0;
8952
8953         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8954         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
8955
8956         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8957                 "UPDATE mail_text_tbl SET"
8958                 " body_text = ?"
8959                 " WHERE mail_id = %d AND account_id != 0"
8960                 , mail_id);
8961         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
8962
8963         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8964         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8965                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8966
8967         i = 0;
8968         _bind_stmt_field_data_string(hStmt, i++, (char *)mail_text->body_text, 0, -1);
8969
8970         if (hStmt != NULL) {
8971                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8972                 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF;}, ("sqlite3_step fail:%d", rc));
8973                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF;}, ("sqlite3_step fail:%d", rc));
8974
8975                 rc = sqlite3_changes(local_db_handle);
8976                 if (rc == 0)  {
8977                         EM_DEBUG_LOG(" no matched mail found...");
8978                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
8979                         goto FINISH_OFF;
8980                 }
8981         }
8982
8983         ret = true;
8984
8985 FINISH_OFF:
8986         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
8987
8988         if (hStmt != NULL) {
8989                 rc = sqlite3_finalize(hStmt);
8990                 if (rc != SQLITE_OK) {
8991                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
8992                         error = EMAIL_ERROR_DB_FAILURE;
8993                 }
8994                 hStmt = NULL;
8995         }
8996
8997         if (err_code != NULL)
8998                 *err_code = error;
8999
9000         EM_DEBUG_FUNC_END("ret [%d]", ret);
9001         return ret;
9002 }
9003 #endif
9004
9005 INTERNAL_FUNC int emstorage_change_mail_field(char *multi_user_name, int mail_id, email_mail_change_type_t type, emstorage_mail_tbl_t *mail, int transaction, int *err_code)
9006 {
9007         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
9008
9009         int ret = false;
9010         int error = EMAIL_ERROR_NONE;
9011         int move_flag = 0;
9012         int rc = 0;
9013         int i = 0;
9014         int mailbox_id = 0;
9015         DB_STMT hStmt = NULL;
9016         char sql_query_string[QUERY_SIZE] = {0, };
9017         char mailbox_id_param_string[10] = {0,};
9018         sqlite3 *local_db_handle = NULL;
9019
9020         if (mail_id <= 0 || !mail)  {
9021                 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], mail[%p]", mail_id, type, mail);
9022                 if (err_code != NULL)
9023                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9024                 return false;
9025         }
9026
9027         local_db_handle = emstorage_get_db_connection(multi_user_name);
9028
9029         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9030
9031         switch (type) {
9032                 case APPEND_BODY:
9033                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9034                                 "UPDATE mail_tbl SET"
9035                                 "  body_download_status = ?"
9036                                 ", file_path_plain = ?"
9037                                 ", file_path_html = ?"
9038                                 ", file_path_mime_entity = ?"
9039                                 ", flags_seen_field      = ?"
9040                                 ", flags_deleted_field   = ?"
9041                                 ", flags_flagged_field   = ?"
9042                                 ", flags_answered_field  = ?"
9043                                 ", flags_recent_field    = ?"
9044                                 ", flags_draft_field     = ?"
9045                                 ", flags_forwarded_field = ?"
9046                                 ", DRM_status = ?"
9047                                 ", attachment_count = ?"
9048                                 ", preview_text= ?"
9049                                 ", meeting_request_status = ? "
9050                                 ", message_class = ? "
9051                                 ", digest_type = ? "
9052                                 ", smime_type = ? "
9053                                 " WHERE mail_id = %d AND account_id != 0"
9054                                 , mail_id);
9055
9056
9057                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9058                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9059                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9060                          i = 0;
9061
9062                         _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9063                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9064                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9065                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9066                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_seen_field);
9067                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_deleted_field);
9068                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_flagged_field);
9069                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_answered_field);
9070                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_recent_field);
9071                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_draft_field);
9072                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_forwarded_field);
9073                         _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
9074                         _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9075                         _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9076                         _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
9077                         _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
9078                         _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9079                         _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9080                         break;
9081
9082                 case UPDATE_MAILBOX: {
9083                                 int err;
9084                                 emstorage_mailbox_tbl_t *mailbox_tbl;
9085
9086                                 if ((err = emstorage_get_mailbox_by_id(multi_user_name, mail->mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
9087                                         EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_id failed [%d]", err);
9088                                         goto FINISH_OFF;
9089                                 }
9090
9091                                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9092                                         "UPDATE mail_tbl SET"
9093                                         " mailbox_id = '%d'"
9094                                         ",mailbox_type = '%d'"
9095                                         " WHERE mail_id = %d AND account_id != 0"
9096                                         , mailbox_tbl->mailbox_id
9097                                         , mailbox_tbl->mailbox_type
9098                                         , mail_id);
9099                                         move_flag = 1;
9100
9101
9102                                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9103                                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9104                                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9105
9106                                 i = 0;
9107                                 _bind_stmt_field_data_string(hStmt, i++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
9108
9109                                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL); /*prevent 26251*/
9110
9111                         }
9112                         break;
9113
9114                 case UPDATE_FLAG:
9115                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9116                                 "UPDATE mail_tbl SET"
9117                                 " flags_seen_field      = %d"
9118                                 ",flags_deleted_field   = %d"
9119                                 ",flags_flagged_field   = %d"
9120                                 ",flags_answered_field  = %d"
9121                                 ",flags_recent_field    = %d"
9122                                 ",flags_draft_field     = %d"
9123                                 ",flags_forwarded_field = %d"
9124                                 "  WHERE mail_id = %d AND account_id != 0"
9125                                 , mail->flags_seen_field
9126                                 , mail->flags_deleted_field
9127                                 , mail->flags_flagged_field
9128                                 , mail->flags_answered_field
9129                                 , mail->flags_recent_field
9130                                 , mail->flags_draft_field
9131                                 , mail->flags_forwarded_field
9132                                 , mail_id);
9133                         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9134
9135
9136                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9137                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9138                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9139
9140                         break;
9141
9142                 case UPDATE_EXTRA_FLAG:
9143                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9144                                 "UPDATE mail_tbl SET"
9145                                 "  priority = %d"
9146                                 ", save_status = %d"
9147                                 ", lock_status = %d"
9148                                 ", report_status = %d"
9149                                 ", DRM_status = %d"
9150                                 " WHERE mail_id = %d AND account_id != 0"
9151                                 , mail->priority
9152                                 , mail->save_status
9153                                 , mail->lock_status
9154                                 , mail->report_status
9155                                 , mail->DRM_status
9156                                 , mail_id);
9157                         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9158
9159
9160                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9161                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9162                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9163                         break;
9164
9165                 case UPDATE_STICKY_EXTRA_FLAG:
9166                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9167                                 "UPDATE mail_tbl SET"
9168                                 "  lock_status = %d"
9169                                 "  WHERE mail_id = %d AND account_id != 0"
9170                                 , mail->lock_status
9171                                 , mail_id);
9172                         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9173
9174
9175                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9176                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9177                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9178                         break;
9179
9180                 case UPDATE_MAIL:
9181                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9182                                 "UPDATE mail_tbl SET"
9183                                 "  full_address_from = ?"
9184                                 ", full_address_reply = ?"
9185                                 ", full_address_to = ?"
9186                                 ", full_address_cc = ?"
9187                                 ", full_address_bcc = ?"
9188                                 ", full_address_return = ?"
9189                                 ", subject = ?"
9190                                 ", file_path_plain = ?"
9191                                 ", date_time = ?"
9192                                 ", flags_seen_field = ?"
9193                                 ", flags_deleted_field = ?"
9194                                 ", flags_flagged_field = ?"
9195                                 ", flags_answered_field = ?"
9196                                 ", flags_recent_field = ?"
9197                                 ", flags_draft_field = ?"
9198                                 ", flags_forwarded_field = ?"
9199                                 ", priority = ?"
9200                                 ", save_status = ?"
9201                                 ", lock_status = ?"
9202                                 ", report_status = ?"
9203                                 ", DRM_status = ?"
9204                                 ", file_path_html = ?"
9205                                 ", file_path_mime_entity = ?"
9206                                 ", mail_size = ?"
9207                                 ", preview_text = ?"
9208                                 ", body_download_status = ?"
9209                                 ", attachment_count = ?"
9210                                 ", inline_content_count = ?"
9211                                 ", meeting_request_status = ?"
9212                                 ", message_class = ?"
9213                                 ", digest_type = ?"
9214                                 ", smime_type = ?"
9215                                 ", scheduled_sending_time = ?"
9216                                 ", remaining_resend_times = ?"
9217                                 ", tag_id = ?"
9218                                 ", replied_time = ?"
9219                                 ", forwarded_time = ?"
9220                                 ", default_charset = ?"
9221                                 ", eas_data_length = ?"
9222                                 ", eas_data = ?"
9223                                 " WHERE mail_id = %d AND account_id != 0"
9224                                 , mail_id);
9225
9226
9227                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9228                         EM_DEBUG_LOG_DEV (" before sqlite3_prepare hStmt = %p", hStmt);
9229                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9230                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9231                         i = 0;
9232                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
9233                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
9234                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
9235                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
9236                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
9237                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
9238                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9239                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9240                         _bind_stmt_field_data_time_t(hStmt, i++, mail->date_time);
9241                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_seen_field);
9242                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_deleted_field);
9243                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_flagged_field);
9244                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_answered_field);
9245                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_recent_field);
9246                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_draft_field);
9247                         _bind_stmt_field_data_char  (hStmt, i++, mail->flags_forwarded_field);
9248                         _bind_stmt_field_data_int   (hStmt, i++, mail->priority);
9249                         _bind_stmt_field_data_int   (hStmt, i++, mail->save_status);
9250                         _bind_stmt_field_data_int   (hStmt, i++, mail->lock_status);
9251                         _bind_stmt_field_data_int   (hStmt, i++, mail->report_status);
9252                         _bind_stmt_field_data_int   (hStmt, i++, mail->DRM_status);
9253                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9254                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9255                         _bind_stmt_field_data_int   (hStmt, i++, mail->mail_size);
9256                         _bind_stmt_field_data_nstring(hStmt, i++, (char*)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9257                         _bind_stmt_field_data_int   (hStmt, i++, mail->body_download_status);
9258                         _bind_stmt_field_data_int   (hStmt, i++, mail->attachment_count);
9259                         _bind_stmt_field_data_int   (hStmt, i++, mail->inline_content_count);
9260                         _bind_stmt_field_data_int   (hStmt, i++, mail->meeting_request_status);
9261                         _bind_stmt_field_data_int   (hStmt, i++, mail->message_class);
9262                         _bind_stmt_field_data_int   (hStmt, i++, mail->digest_type);
9263                         _bind_stmt_field_data_int   (hStmt, i++, mail->smime_type);
9264                         _bind_stmt_field_data_int   (hStmt, i++, mail->scheduled_sending_time);
9265                         _bind_stmt_field_data_int   (hStmt, i++, mail->remaining_resend_times);
9266                         _bind_stmt_field_data_int   (hStmt, i++, mail->tag_id);
9267                         _bind_stmt_field_data_int   (hStmt, i++, mail->replied_time);
9268                         _bind_stmt_field_data_int   (hStmt, i++, mail->forwarded_time);
9269                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
9270                         _bind_stmt_field_data_int   (hStmt, i++, mail->eas_data_length);
9271                         _bind_stmt_field_data_blob  (hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
9272                         break;
9273
9274                 case UPDATE_DATETIME:  {
9275                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9276                                 "UPDATE mail_tbl SET"
9277                                 " date_time = '%ld'"
9278                                 " WHERE mail_id = %d AND account_id != 0"
9279                                 , mail->date_time
9280                                 , mail_id);
9281
9282                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9283                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9284                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9285                         break;
9286                 }
9287
9288                 case UPDATE_FROM_CONTACT_INFO:
9289                         EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_FROM_CONTACT_INFO");
9290                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9291                                 "UPDATE mail_tbl SET"
9292                                 " email_address_sender = ?,"
9293                                 " WHERE mail_id = %d",
9294                                 mail_id);
9295
9296                         hStmt = NULL;
9297
9298                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9299                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9300                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9301                         i = 0;
9302                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9303                         break;
9304
9305                 case UPDATE_TO_CONTACT_INFO:
9306                         EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_TO_CONTACT_INFO");
9307                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9308                                 "UPDATE mail_tbl SET"
9309                                 " email_address_recipient = ?,"
9310                                 " WHERE mail_id = %d",
9311                                 mail_id);
9312
9313                         hStmt = NULL;
9314
9315                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9316                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9317                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9318                         i = 0;
9319                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9320                         break;
9321
9322                         case UPDATE_ALL_CONTACT_INFO:
9323                                 EM_DEBUG_LOG("emstorage_change_mail_field - mail change type is UPDATE_ALL_CONTACT_INFO");
9324                                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9325                                         "UPDATE mail_tbl SET"
9326                                         " email_address_sender = ?,"
9327                                         " email_address_recipient = ?,"
9328                                         " WHERE mail_id = %d",
9329                                         mail_id);
9330
9331                                 hStmt = NULL;
9332
9333                                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9334                                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9335                                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9336                                 i = 0;
9337                                 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9338                                 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9339                                 break;
9340
9341
9342 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
9343                         case UPDATE_PARTIAL_BODY_DOWNLOAD:
9344
9345                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9346                         "UPDATE mail_tbl SET"
9347                         "  body_download_status = ?"
9348                         ", file_path_plain = ?"
9349                         ", file_path_html = ?"
9350                         ", file_path_mime_entity = ?"
9351                         ", attachment_count = ?"
9352                         ", inline_content_count = ?"
9353                         ", preview_text = ?"
9354                         ", digest_type = ?"
9355                         ", smime_type = ?"
9356                         " WHERE mail_id = %d"
9357                         , mail_id);
9358
9359
9360                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9361                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9362                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9363                          i = 0;
9364
9365                         _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9366                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9367                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html,  0, TEXT_2_LEN_IN_MAIL_TBL);
9368                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9369                         _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9370                         _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
9371                         _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text,    0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9372                         _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9373                         _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9374
9375                         break;
9376
9377 #endif
9378                 case UPDATE_FILE_PATH:
9379                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9380                                 "UPDATE mail_tbl SET"
9381                                 ", file_path_plain = ?"
9382                                 ", file_path_html = ?"
9383                                 ", file_path_mime_entity = ?"
9384                                 " WHERE mail_id = %d"
9385                                 , mail_id);
9386
9387
9388                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9389                         EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
9390                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9391                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9392                         i = 0;
9393                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9394                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9395                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9396                         break;
9397
9398                 default:
9399                         EM_DEBUG_LOG(" type[%d]", type);
9400
9401                         error = EMAIL_ERROR_INVALID_PARAM;
9402                         goto FINISH_OFF;
9403         }
9404
9405         if (hStmt != NULL)  {
9406
9407         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9408                 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
9409                 ("sqlite3_step fail:%d", rc));
9410                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
9411                         ("sqlite3_step fail:%d", rc));
9412                 rc = sqlite3_changes(local_db_handle);
9413                 if (rc == 0)  {
9414                         EM_DEBUG_LOG(" no matched mail found...");
9415                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
9416                         goto FINISH_OFF;
9417                 }
9418         }
9419
9420         if (mail->account_id == 0) {
9421                 emstorage_mail_tbl_t* mail_for_account_tbl = NULL;
9422                 if (!emstorage_get_mail_field_by_id(multi_user_name, mail_id, RETRIEVE_ACCOUNT, &mail_for_account_tbl, true, &error) || !mail_for_account_tbl) {
9423                         EM_DEBUG_EXCEPTION ("emstorage_get_mail_field_by_id error [%d]", error);
9424                         goto FINISH_OFF;
9425                 }
9426                 mail->account_id = mail_for_account_tbl->account_id;
9427                 if (mail_for_account_tbl)
9428                         emstorage_free_mail(&mail_for_account_tbl, 1, NULL);
9429         }
9430
9431         ret = true;
9432
9433 FINISH_OFF:
9434         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9435
9436         if (hStmt != NULL)  {
9437                 rc = sqlite3_finalize(hStmt);
9438                 if (rc != SQLITE_OK)  {
9439                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
9440                         error = EMAIL_ERROR_DB_FAILURE;
9441                 }
9442                 hStmt = NULL;
9443         }
9444         if (error == EMAIL_ERROR_NONE &&  move_flag != 1 && transaction) {
9445                 if (!emstorage_get_mailbox_id_by_mailbox_type(multi_user_name, mail->account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &mailbox_id, false, &error))
9446                         EM_DEBUG_EXCEPTION("emstorage_get_mailbox_id_by_mailbox_type error [%d]", error);
9447
9448                 if (mail->mailbox_id == mailbox_id) {
9449                         SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
9450                         if (!emcore_notify_storage_event (NOTI_MAIL_UPDATE, mail->account_id, mail_id, mailbox_id_param_string, type))
9451                                 EM_DEBUG_EXCEPTION ("emcore_notify_storage_event error [ NOTI_MAIL_UPDATE ] >>>> ");
9452                 }
9453                 else {
9454                         /* h.gahlaut@samsung.com: Jan 10, 2011 Publishing noti to refresh outbox when email sending status changes */
9455                         if (!emcore_notify_storage_event (NOTI_MAIL_UPDATE, mail->account_id, mail_id, NULL, type))
9456                                 EM_DEBUG_EXCEPTION (" emcore_notify_storage_event error [ NOTI_MAIL_UPDATE ]");
9457                 }
9458         }
9459
9460         if (err_code != NULL)
9461                 *err_code = error;
9462
9463         EM_DEBUG_FUNC_END("ret [%d]", ret);
9464         return ret;
9465 }
9466
9467 INTERNAL_FUNC int emstorage_increase_mail_id(char *multi_user_name, int *mail_id, int transaction, int *err_code)
9468 {
9469         EM_DEBUG_FUNC_BEGIN("mail_id[%p], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9470
9471         int rc, ret = false;
9472         int error = EMAIL_ERROR_NONE;
9473         int latest_mail_id = 0;
9474         sqlite3 *local_db_handle = NULL;
9475         char *sql = "SELECT MAX(mail_id) FROM mail_tbl;";
9476         char **result = NULL;
9477
9478 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9479         _timedlock_shm_mutex(mapped_for_generating_mail_id, 2);
9480 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9481
9482         ret = vconf_get_int(VCONF_KEY_LATEST_MAIL_ID, &latest_mail_id);
9483         if (ret < 0 || latest_mail_id == 0) {
9484                 EM_DEBUG_LOG("vconf_get_int() failed [%d] or latest_mail_id is zero", ret);
9485
9486         local_db_handle = emstorage_get_db_connection(multi_user_name);
9487
9488                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9489                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9490                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9491                 if (NULL == result[1])
9492                         rc = 1;
9493                 else
9494                         rc = atoi(result[1]) + 1;
9495
9496                 sqlite3_free_table(result);
9497                 latest_mail_id = rc;
9498         }
9499
9500         latest_mail_id++;
9501
9502         ret = vconf_set_int(VCONF_KEY_LATEST_MAIL_ID, latest_mail_id);
9503
9504         if (mail_id)
9505                 *mail_id = latest_mail_id;
9506
9507 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9508         _unlockshm_mutex(mapped_for_generating_mail_id);
9509 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9510
9511         ret = true;
9512
9513 FINISH_OFF:
9514
9515         if (err_code != NULL)
9516                 *err_code = error;
9517
9518         EM_DEBUG_FUNC_END("ret [%d]", ret);
9519         return ret;
9520 }
9521
9522 INTERNAL_FUNC int emstorage_add_mail(char *multi_user_name, emstorage_mail_tbl_t *mail_tbl_data, int get_id, int transaction, int *err_code)
9523 {
9524         EM_PROFILE_BEGIN(profile_emstorage_add_mail);
9525         EM_DEBUG_FUNC_BEGIN("mail_tbl_data[%p], get_id[%d], transaction[%d], err_code[%p]", mail_tbl_data, get_id, transaction, err_code);
9526
9527         if (!mail_tbl_data)  {
9528                 EM_DEBUG_EXCEPTION("mail_tbl_data[%p], get_id[%d]", mail_tbl_data, get_id);
9529                 if (err_code != NULL)
9530                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9531                 return false;
9532         }
9533
9534         int rc, ret = false;
9535         int error = EMAIL_ERROR_NONE;
9536         char sql_query_string[QUERY_SIZE] = {0, };
9537         DB_STMT hStmt = NULL;
9538
9539         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9540
9541         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9542
9543         if (get_id)  {
9544                 /*  increase unique id */
9545                 char *sql = "SELECT max(rowid) FROM mail_tbl;";
9546                 char **result;
9547
9548                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9549 /*              EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
9550                         ("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle))); */
9551                 if (rc != SQLITE_OK) {
9552                         EM_DEBUG_EXCEPTION ("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle));
9553                         error = EMAIL_ERROR_DB_FAILURE;
9554                         sqlite3_free_table(result);
9555                         goto FINISH_OFF;
9556                 }
9557
9558                 if (NULL == result[1])
9559                         rc = 1;
9560                 else
9561                         rc = atoi(result[1])+1;
9562
9563                 sqlite3_free_table(result);
9564
9565                 mail_tbl_data->mail_id   = rc;
9566                 mail_tbl_data->thread_id = rc;
9567         }
9568
9569         if (mail_tbl_data->date_time == 0)
9570                 mail_tbl_data->date_time = time(NULL);
9571
9572         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9573                 "INSERT INTO mail_tbl VALUES "
9574                 "( ?" /*  mail_id */
9575                 ", ?" /*  account_id */
9576                 ", ?" /*  mailbox_id */
9577                 ", ?" /*  mailbox_type */
9578                 ", ?" /*  subject */
9579
9580                 ", ?" /*  date_time */
9581                 ", ?" /*  server_mail_status */
9582                 ", ?" /*  server_mailbox_name */
9583                 ", ?" /*  server_mail_id */
9584                 ", ?" /*  message_id */
9585
9586                 ", ?" /*  reference_mail_id */
9587                 ", ?" /*  full_address_from */
9588                 ", ?" /*  full_address_reply */
9589                 ", ?" /*  full_address_to */
9590                 ", ?" /*  full_address_cc */
9591
9592                 ", ?" /*  full_address_bcc */
9593                 ", ?" /*  full_address_return */
9594                 ", ?" /*  email_address_sender */
9595                 ", ?" /*  email_address_recipient */
9596                 ", ?" /*  alias_sender */
9597
9598                 ", ?" /*  alias_recipient */
9599                 ", ?" /*  body_download_status */
9600                 ", ?" /*  file_path_plain */
9601                 ", ?" /*  file_path_html */
9602                 ", ?" /*  file_path_mime_entity */
9603
9604                 ", ?" /*  mail_size */
9605                 ", ?" /*  flags_seen_field */
9606                 ", ?" /*  flags_deleted_field */
9607                 ", ?" /*  flags_flagged_field */
9608                 ", ?" /*  flags_answered_field */
9609
9610                 ", ?" /*  flags_recent_field */
9611                 ", ?" /*  flags_draft_field */
9612                 ", ?" /*  flags_forwarded_field */
9613                 ", ?" /*  DRM_status */
9614                 ", ?" /*  priority */
9615
9616                 ", ?" /*  save_status */
9617                 ", ?" /*  lock_status */
9618                 ", ?" /*  report_status */
9619                 ", ?" /*  attachment_count */
9620                 ", ?" /*  inline_content_count */
9621
9622                 ", ?" /*  thread_id */
9623                 ", ?" /*  thread_item_count */
9624                 ", ?" /*  preview_text */
9625                 ", ?" /*  meeting_request_status */
9626                 ", ?" /*  message_class */
9627
9628                 ", ?" /*  digest_type */
9629                 ", ?" /*  smime_type */
9630                 ", ?" /*  scheduled_sending_time */
9631                 ", ?" /*  remaining_resend_times */
9632                 ", ?" /*  tag_id */
9633
9634                 ", ?" /*  replied_time */
9635                 ", ?" /*  forwarded_time */
9636                 ", ?" /*  default charset */
9637                 ", ?" /*  eas_data_length */
9638                 ", ?" /*  eas_data */
9639                 ", ?" /*  user_name */
9640                 ")");
9641
9642         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle,
9643                                                    sql_query_string,
9644                                                    EM_SAFE_STRLEN(sql_query_string),
9645                                                    &hStmt,
9646                                                    NULL),
9647                                     rc);
9648         if (rc != SQLITE_OK) {
9649                 EM_DEBUG_EXCEPTION ("sqlite3_prepare error [%d] [%s] SQL(%s) ", 
9650                              rc, sql_query_string, sqlite3_errmsg(local_db_handle));
9651                 error = EMAIL_ERROR_DB_FAILURE;
9652                 goto FINISH_OFF;
9653         }
9654
9655         _bind_stmt_field_data_int   (hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mail_id);
9656         _bind_stmt_field_data_int   (hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, mail_tbl_data->account_id);
9657         _bind_stmt_field_data_int   (hStmt, MAILBOX_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_id);
9658         _bind_stmt_field_data_int   (hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_type);
9659         _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9660
9661         _bind_stmt_field_data_int   (hStmt, DATETIME_IDX_IN_MAIL_TBL, mail_tbl_data->date_time);
9662         _bind_stmt_field_data_int   (hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->server_mail_status);
9663         _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);
9664         _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);
9665         _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);
9666
9667         _bind_stmt_field_data_int   (hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, mail_tbl_data->reference_mail_id);
9668         _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);
9669         _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);
9670         _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);
9671         _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);
9672
9673         _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);
9674         _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);
9675         _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);
9676         _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);
9677         _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);
9678
9679         _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);
9680         _bind_stmt_field_data_int   (hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->body_download_status);
9681         _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);
9682         _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);
9683         _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);
9684
9685         _bind_stmt_field_data_int   (hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, mail_tbl_data->mail_size);
9686         _bind_stmt_field_data_int   (hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_seen_field);
9687         _bind_stmt_field_data_int   (hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_deleted_field);
9688         _bind_stmt_field_data_int   (hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_flagged_field);
9689         _bind_stmt_field_data_int   (hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_answered_field);
9690
9691         _bind_stmt_field_data_int   (hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_recent_field);
9692         _bind_stmt_field_data_int   (hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_draft_field);
9693         _bind_stmt_field_data_int   (hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_forwarded_field);
9694         _bind_stmt_field_data_int   (hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->DRM_status);
9695         _bind_stmt_field_data_int   (hStmt, PRIORITY_IDX_IN_MAIL_TBL, mail_tbl_data->priority);
9696
9697         _bind_stmt_field_data_int   (hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->save_status);
9698         _bind_stmt_field_data_int   (hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->lock_status);
9699         _bind_stmt_field_data_int   (hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->report_status);
9700         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->attachment_count);
9701         _bind_stmt_field_data_int   (hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->inline_content_count);
9702
9703         _bind_stmt_field_data_int   (hStmt, THREAD_ID_IDX_IN_MAIL_TBL, mail_tbl_data->thread_id);
9704         _bind_stmt_field_data_int   (hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->thread_item_count);
9705         _bind_stmt_field_data_nstring(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9706         _bind_stmt_field_data_int   (hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->meeting_request_status);
9707         _bind_stmt_field_data_int   (hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, mail_tbl_data->message_class);
9708
9709         _bind_stmt_field_data_int   (hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->digest_type);
9710         _bind_stmt_field_data_int   (hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->smime_type);
9711         _bind_stmt_field_data_int   (hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->scheduled_sending_time);
9712         _bind_stmt_field_data_int   (hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, mail_tbl_data->remaining_resend_times);
9713         _bind_stmt_field_data_int   (hStmt, TAG_ID_IDX_IN_MAIL_TBL, mail_tbl_data->tag_id);
9714
9715         _bind_stmt_field_data_int   (hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->replied_time);
9716         _bind_stmt_field_data_int   (hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->forwarded_time);
9717         _bind_stmt_field_data_string(hStmt, DEFAULT_CHARSET_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
9718         _bind_stmt_field_data_int   (hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, mail_tbl_data->eas_data_length);
9719         _bind_stmt_field_data_blob  (hStmt, EAS_DATA_IDX_IN_MAIL_TBL, (void*)mail_tbl_data->eas_data, mail_tbl_data->eas_data_length);
9720         _bind_stmt_field_data_string(hStmt, USER_NAME_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->user_name, 0, TEXT_2_LEN_IN_MAIL_TBL);
9721
9722         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9723         if (rc == SQLITE_FULL) {
9724                 EM_DEBUG_EXCEPTION ("sqlite3_step error [%d]", rc);
9725                 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
9726                 goto FINISH_OFF;
9727         }
9728         if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
9729                 EM_DEBUG_EXCEPTION ("sqlite3_step error [%d]", rc);
9730                 error = EMAIL_ERROR_DB_FAILURE;
9731                 goto FINISH_OFF;
9732         }
9733         ret = true;
9734
9735 FINISH_OFF:
9736         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9737
9738         if (hStmt != NULL)  {
9739                 rc = sqlite3_finalize(hStmt);
9740                 if (rc != SQLITE_OK)  {
9741                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
9742                         error = EMAIL_ERROR_DB_FAILURE;
9743                 }
9744         }
9745
9746         if (err_code != NULL)
9747                 *err_code = error;
9748
9749         EM_PROFILE_END(profile_emstorage_add_mail);
9750         EM_DEBUG_FUNC_END("ret [%d]", ret);
9751         return ret;
9752 }
9753
9754 INTERNAL_FUNC int emstorage_move_multiple_mails_on_db(char *multi_user_name, int input_source_account_id, int input_mailbox_id, int mail_ids[], int number_of_mails, int transaction, int *err_code)
9755 {
9756         EM_DEBUG_FUNC_BEGIN("input_source_account_id [%d], input_mailbox_id [%d], mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", input_source_account_id, input_mailbox_id, mail_ids, number_of_mails, transaction, err_code);
9757
9758         int ret = false, i, cur_conditional_clause = 0;
9759         int error = EMAIL_ERROR_NONE;
9760         int target_account_id;
9761         int conditional_clause_len = 0;
9762         char *sql_query_string = NULL, *conditional_clause = NULL;
9763         emstorage_mailbox_tbl_t *result_mailbox = NULL;
9764         email_mailbox_type_e target_mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED;
9765         char* target_mailbox_name = NULL;
9766
9767         if (!mail_ids || input_mailbox_id <= 0) {
9768                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9769                 if (err_code != NULL)
9770                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9771                 return false;
9772         }
9773
9774         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9775
9776         if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &result_mailbox)) != EMAIL_ERROR_NONE || !result_mailbox) {
9777                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", error);
9778                 if (err_code != NULL)
9779                         *err_code = error;
9780                 return false;
9781         }
9782
9783         if (result_mailbox->mailbox_name) {
9784                 if (strstr(result_mailbox->mailbox_name, "'")) {
9785                         target_mailbox_name = em_replace_all_string(result_mailbox->mailbox_name, "'", "''");
9786                 } else {
9787                         target_mailbox_name = strdup(result_mailbox->mailbox_name);
9788                 }
9789         }
9790
9791         target_mailbox_type = result_mailbox->mailbox_type;
9792         target_account_id   = result_mailbox->account_id;
9793         emstorage_free_mailbox(&result_mailbox, 1, NULL);
9794
9795         conditional_clause_len =  (sizeof(char) * 8 * number_of_mails) + 512;
9796         conditional_clause = em_malloc(conditional_clause_len);
9797         if (conditional_clause == NULL) {
9798                 EM_DEBUG_EXCEPTION("em_malloc failed");
9799                 error = EMAIL_ERROR_OUT_OF_MEMORY;
9800                 goto FINISH_OFF;
9801         }
9802         cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE mail_id in (");
9803
9804         for(i = 0; i < number_of_mails; i++)
9805                 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
9806
9807         /* prevent 34415 */
9808         char *last_comma = rindex(conditional_clause, ',');
9809         if(last_comma) *last_comma = ')'; /* replace , with ) */
9810
9811         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9812
9813         /* Updating a mail_tbl */
9814
9815         sql_query_string = em_malloc(conditional_clause_len);
9816         if (sql_query_string == NULL) {
9817                 EM_DEBUG_EXCEPTION("em_malloc failed");
9818                 error = EMAIL_ERROR_OUT_OF_MEMORY;
9819                 goto FINISH_OFF;
9820         }
9821         SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_tbl SET mailbox_type = %d, mailbox_id = %d, account_id = %d %s", target_mailbox_type, input_mailbox_id, target_account_id, conditional_clause);
9822         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9823
9824         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9825         if (error != EMAIL_ERROR_NONE) {
9826                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9827                         goto FINISH_OFF;
9828         }
9829
9830         /* Updating a mail_attachment_tbl */
9831         memset(sql_query_string, 0x00, conditional_clause_len);
9832         SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_attachment_tbl SET mailbox_id = '%d', account_id = %d %s", input_mailbox_id, target_account_id, conditional_clause);
9833         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9834         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9835         if (error != EMAIL_ERROR_NONE) {
9836                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9837                         goto FINISH_OFF;
9838         }
9839
9840         /* Updating a mail_meeting_tbl */
9841         memset(sql_query_string, 0x00, conditional_clause_len);
9842         SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_meeting_tbl SET mailbox_id = %d, account_id = %d %s", input_mailbox_id, target_account_id, conditional_clause);
9843         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9844         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9845         if (error != EMAIL_ERROR_NONE) {
9846                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9847                         goto FINISH_OFF;
9848         }
9849
9850 #ifdef __FEATURE_BODY_SEARCH__
9851         /* Updating mail_text_tbl */
9852         memset(sql_query_string, 0x00, conditional_clause_len);
9853         SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_text_tbl SET mailbox_id = %d, account_id = %d %s", input_mailbox_id, target_account_id, conditional_clause);
9854         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9855         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9856         if (error != EMAIL_ERROR_NONE) {
9857                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9858                         goto FINISH_OFF;
9859         }
9860 #endif
9861
9862         /* Updating a mail_read_mail_uid_tbl */
9863         memset(conditional_clause, 0x00, conditional_clause_len);
9864         cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE local_uid in (");
9865
9866         for(i = 0; i < number_of_mails; i++)
9867                 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
9868
9869         /* prevent 34415 */
9870         last_comma = rindex(conditional_clause, ',');
9871         if(last_comma) *last_comma = ')'; /* replace , with ) */
9872
9873         memset(sql_query_string, 0x00, conditional_clause_len);
9874         SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_read_mail_uid_tbl SET mailbox_name = '%s', mailbox_id = %d, account_id = %d %s", target_mailbox_name, input_mailbox_id, target_account_id, conditional_clause);
9875         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9876         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9877         if (error != EMAIL_ERROR_NONE) {
9878                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9879                         goto FINISH_OFF;
9880         }
9881
9882         ret = true;
9883
9884 FINISH_OFF:
9885         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9886
9887         EM_SAFE_FREE(target_mailbox_name);
9888         EM_SAFE_FREE(conditional_clause);
9889         EM_SAFE_FREE(sql_query_string);
9890
9891         if (err_code != NULL)
9892                 *err_code = error;
9893
9894         EM_DEBUG_FUNC_END("ret [%d]", ret);
9895         return ret;
9896 }
9897
9898 INTERNAL_FUNC int emstorage_delete_mail(char *multi_user_name, int mail_id, int from_server, int transaction, int *err_code)
9899 {
9900         EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9901
9902         if (!mail_id)  {
9903                 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
9904                 if (err_code != NULL)
9905                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9906                 return false;
9907         }
9908
9909         int ret = false;
9910         int error = EMAIL_ERROR_NONE;
9911         char sql_query_string[QUERY_SIZE] = {0, };
9912         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9913         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9914
9915         memset(sql_query_string, 0x00, sizeof(sql_query_string));
9916
9917         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id = %d ", mail_id);
9918         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9919         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9920         if (error != EMAIL_ERROR_NONE) {
9921                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9922                         goto FINISH_OFF;
9923         }
9924
9925         ret = true;
9926
9927 FINISH_OFF:
9928         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9929
9930         if (err_code != NULL)
9931                 *err_code = error;
9932
9933         EM_DEBUG_FUNC_END("ret [%d]", ret);
9934         return ret;
9935 }
9936
9937 INTERNAL_FUNC int emstorage_delete_multiple_mails(char *multi_user_name, int mail_ids[], int number_of_mails, int transaction, int *err_code)
9938 {
9939         EM_DEBUG_FUNC_BEGIN("mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", mail_ids, number_of_mails, transaction, err_code);
9940
9941         int ret = false, i, cur_sql_query_string = 0;
9942         int error = EMAIL_ERROR_NONE;
9943         int query_size = 0;
9944         char *sql_query_string = NULL;
9945
9946         if (!mail_ids) {
9947                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9948                 if (err_code != NULL)
9949                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9950                 return false;
9951         }
9952
9953         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9954         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9955
9956         query_size = (sizeof(char) * 8 * number_of_mails) + 512;
9957         sql_query_string =  em_malloc(query_size);
9958         if (sql_query_string == NULL) {
9959                 EM_DEBUG_EXCEPTION("em_malloc failed");
9960                 error = EMAIL_ERROR_OUT_OF_MEMORY;
9961                 goto FINISH_OFF;
9962         }
9963
9964         cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_tbl WHERE mail_id in (");
9965
9966         for(i = 0; i < number_of_mails; i++)
9967                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
9968
9969         /* prevent 34414 */
9970         char *last_comma = rindex(sql_query_string, ',');
9971         if (last_comma != NULL) *last_comma = ')'; /* replace , with ) */
9972
9973         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9974         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9975         if (error != EMAIL_ERROR_NONE) {
9976                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9977                         goto FINISH_OFF;
9978         }
9979
9980 #ifdef __FEATURE_BODY_SEARCH__
9981         /* delete mail_text from mail_text_tbl */
9982         cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_text_tbl WHERE mail_id in (");
9983
9984         for(i = 0; i < number_of_mails; i++)
9985                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
9986
9987         last_comma = rindex(sql_query_string, ',');
9988         *last_comma = ')'; /* replace , with ) */
9989
9990         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9991         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9992         if (error != EMAIL_ERROR_NONE) {
9993                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9994                         goto FINISH_OFF;
9995         }
9996 #endif
9997
9998         ret = true;
9999
10000 FINISH_OFF:
10001         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10002
10003         EM_SAFE_FREE(sql_query_string);
10004         if (err_code != NULL)
10005                 *err_code = error;
10006
10007         EM_DEBUG_FUNC_END("ret [%d]", ret);
10008         return ret;
10009 }
10010
10011 INTERNAL_FUNC int emstorage_delete_mail_by_account(char *multi_user_name, int account_id, int transaction, int *err_code)
10012 {
10013         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
10014
10015         if (account_id < FIRST_ACCOUNT_ID)  {
10016                 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
10017
10018                 if (err_code != NULL)
10019                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10020                 return false;
10021         }
10022
10023         int rc, ret = false;
10024         int error = EMAIL_ERROR_NONE;
10025         char sql_query_string[QUERY_SIZE] = {0, };
10026
10027         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10028         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10029
10030         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d", account_id);
10031         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10032         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10033         if (error != EMAIL_ERROR_NONE) {
10034                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10035                         goto FINISH_OFF;
10036         }
10037
10038         rc = sqlite3_changes(local_db_handle);
10039         if (rc == 0)  {
10040                 EM_DEBUG_LOG(" no mail found...");
10041                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10042         }
10043
10044         /* Delete all mails  mail_read_mail_uid_tbl table based on account id */
10045         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
10046         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10047         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10048         if (error != EMAIL_ERROR_NONE) {
10049                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10050                         goto FINISH_OFF;
10051         }
10052
10053         rc = sqlite3_changes(local_db_handle);
10054         if (rc == 0)  {
10055                 EM_DEBUG_LOG("No mail found...");
10056                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10057         }
10058
10059 #ifdef __FEATURE_BODY_SEARCH__
10060         /* Delete all mail_text in mail_text_tbl table based on account id */
10061         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE account_id = %d", account_id);
10062         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10063         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10064         if (error != EMAIL_ERROR_NONE) {
10065                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10066                         goto FINISH_OFF;
10067         }
10068
10069         rc = sqlite3_changes(local_db_handle);
10070         if (rc == 0) {
10071                 EM_DEBUG_LOG("No mail found...");
10072                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10073         }
10074 #endif
10075
10076         ret = true;
10077
10078 FINISH_OFF:
10079         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10080
10081         if (error == EMAIL_ERROR_NONE) {
10082                 if (!emcore_notify_storage_event (NOTI_MAIL_DELETE_WITH_ACCOUNT, account_id, 0 , NULL, 0))
10083                         EM_DEBUG_EXCEPTION ("emcore_notify_storage_event Failed [ NOTI_MAIL_DELETE_ALL ]");
10084         }
10085
10086         if (err_code != NULL)
10087                 *err_code = error;
10088
10089         EM_DEBUG_FUNC_END("ret [%d]", ret);
10090         return ret;
10091 }
10092
10093 INTERNAL_FUNC int emstorage_delete_mail_by_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox, int transaction, int *err_code)
10094 {
10095         EM_DEBUG_FUNC_BEGIN("mailbox[%p], transaction[%d], err_code[%p]", mailbox, transaction, err_code);
10096
10097         if (mailbox == NULL)  {
10098                 EM_DEBUG_EXCEPTION("mailbox [%p]", mailbox);
10099                 if (err_code != NULL)
10100                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10101                 return false;
10102         }
10103
10104         int ret = false;
10105         int error = EMAIL_ERROR_NONE;
10106         char sql_query_string[QUERY_SIZE] = {0, };
10107         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10108         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10109
10110         if (strcmp(mailbox->mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
10111                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d AND mailbox_type = %d", mailbox->account_id, mailbox->mailbox_type);
10112                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10113                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10114                 if (error != EMAIL_ERROR_NONE) {
10115                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10116                         goto FINISH_OFF;
10117                 }
10118
10119                 /* Delete Mails from mail_read_mail_uid_tbl */
10120                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d AND mailbox_name = '%s'", mailbox->account_id, mailbox->mailbox_name);
10121                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10122                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10123                 if (error != EMAIL_ERROR_NONE) {
10124                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10125                         goto FINISH_OFF;
10126                 }
10127         } else {
10128                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d AND mailbox_id = %d", mailbox->account_id, mailbox->mailbox_id);
10129                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10130                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10131                 if (error != EMAIL_ERROR_NONE) {
10132                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10133                         goto FINISH_OFF;
10134                 }
10135
10136                 /* Delete Mails from mail_read_mail_uid_tbl */
10137                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d AND mailbox_id = %d", mailbox->account_id, mailbox->mailbox_id);
10138                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10139                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10140                 if (error != EMAIL_ERROR_NONE) {
10141                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10142                         goto FINISH_OFF;
10143                 }
10144
10145 #ifdef __FEATURE_BODY_SEARCH__
10146                 /* Delete Mails from mail_text_tbl */
10147                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE account_id = %d AND mailbox_id = %d", mailbox->account_id, mailbox->mailbox_id);
10148                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10149                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10150                 if (error != EMAIL_ERROR_NONE) {
10151                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10152                         goto FINISH_OFF;
10153                 }
10154 #endif
10155         }
10156         ret = true;
10157
10158 FINISH_OFF:
10159         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10160
10161         if (error == EMAIL_ERROR_NONE) {
10162                 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_ALL, mailbox->account_id, mailbox->mailbox_id , mailbox->mailbox_name, 0))
10163                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [ NOTI_MAIL_DELETE_ALL ] >>>> ");
10164         }
10165
10166         if (err_code != NULL)
10167                 *err_code = error;
10168
10169         EM_DEBUG_FUNC_END("ret [%d]", ret);
10170         return ret;
10171 }
10172
10173 INTERNAL_FUNC int emstorage_free_mail(emstorage_mail_tbl_t** mail_list, int count, int *err_code)
10174 {
10175         EM_DEBUG_FUNC_BEGIN("mail_list[%p], count[%d], err_code[%p]", mail_list, count, err_code);
10176
10177         if (count > 0)  {
10178                 if ((mail_list == NULL) || (*mail_list == NULL))  {
10179                         EM_DEBUG_EXCEPTION("mail_ilst[%p], count[%d]", mail_list, count);
10180
10181                         if (err_code)
10182                                 *err_code = EMAIL_ERROR_INVALID_PARAM;
10183                         return false;
10184                 }
10185
10186                 emstorage_mail_tbl_t* p = *mail_list;
10187                 int i = 0;
10188
10189                 for (; i < count; i++, p++) {
10190                         EM_SAFE_FREE(p->server_mailbox_name);
10191                         EM_SAFE_FREE(p->server_mail_id);
10192                         EM_SAFE_FREE(p->full_address_from);
10193                         EM_SAFE_FREE(p->full_address_reply);
10194                         EM_SAFE_FREE(p->full_address_to);
10195                         EM_SAFE_FREE(p->full_address_cc);
10196                         EM_SAFE_FREE(p->full_address_bcc);
10197                         EM_SAFE_FREE(p->full_address_return);
10198                         EM_SAFE_FREE(p->subject);
10199                         EM_SAFE_FREE(p->file_path_plain);
10200                         EM_SAFE_FREE(p->file_path_html);
10201                         EM_SAFE_FREE(p->file_path_mime_entity);
10202                         EM_SAFE_FREE(p->message_id);
10203                         EM_SAFE_FREE(p->email_address_sender);
10204                         EM_SAFE_FREE(p->email_address_recipient);
10205                         EM_SAFE_FREE(p->preview_text);
10206                         EM_SAFE_FREE(p->alias_sender);
10207                         EM_SAFE_FREE(p->alias_recipient);
10208                         EM_SAFE_FREE(p->default_charset);
10209                         EM_SAFE_FREE(p->pgp_password);
10210                         EM_SAFE_FREE(p->eas_data);
10211                         EM_SAFE_FREE(p->user_name);
10212                 }
10213                 EM_SAFE_FREE(*mail_list);
10214         }
10215
10216         if (err_code != NULL)
10217                 *err_code = EMAIL_ERROR_NONE;
10218
10219         EM_DEBUG_FUNC_END();
10220         return true;
10221 }
10222
10223 #ifdef __FEATURE_BODY_SEARCH__
10224 INTERNAL_FUNC void emstorage_free_mail_text(emstorage_mail_text_tbl_t** mail_text_list, int count, int *err_code)
10225 {
10226         EM_DEBUG_FUNC_BEGIN("mail_text_list[%p], count[%d], err_code[%p]", mail_text_list, count, err_code);
10227
10228         if (count > 0) {
10229                 if ((mail_text_list == NULL) || (*mail_text_list == NULL)) {
10230                         EM_DEBUG_LOG("Nothing to free: mail_text_list[%p]", mail_text_list);
10231                         return;
10232                 }
10233
10234                 emstorage_mail_text_tbl_t *p = *mail_text_list;
10235                 int i = 0;
10236
10237                 for (; i < count; i++, p++) {
10238                         EM_SAFE_FREE(p->body_text);
10239                 }
10240                 EM_SAFE_FREE(*mail_text_list);
10241         }
10242
10243         EM_DEBUG_FUNC_END();
10244 }
10245 #endif
10246
10247 INTERNAL_FUNC int emstorage_get_attachment_count(char *multi_user_name, int mail_id, int *count, int transaction, int *err_code)
10248 {
10249         EM_DEBUG_FUNC_BEGIN("mail_id[%d], count[%p], transaction[%d], err_code[%p]", mail_id, count, transaction, err_code);
10250
10251         if (mail_id <= 0 || !count)  {
10252                 EM_DEBUG_EXCEPTION("mail_id[%d], count[%p]", mail_id, count);
10253                 if (err_code != NULL)
10254                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10255                 return false;
10256         }
10257
10258         int rc = -1, ret = false;
10259         int error = EMAIL_ERROR_NONE;
10260         char sql_query_string[QUERY_SIZE] = {0, };
10261
10262         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10263         EMSTORAGE_START_READ_TRANSACTION(transaction);
10264
10265         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
10266
10267         char **result;
10268
10269         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10270         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
10271                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10272
10273         *count = atoi(result[1]);
10274         sqlite3_free_table(result);
10275
10276         ret = true;
10277
10278 FINISH_OFF:
10279         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10280
10281         if (err_code != NULL)
10282                 *err_code = error;
10283
10284         EM_DEBUG_FUNC_END("ret [%d]", ret);
10285         return ret;
10286 }
10287
10288 INTERNAL_FUNC int emstorage_get_attachment_list(char *multi_user_name, int input_mail_id, int input_transaction, emstorage_attachment_tbl_t** output_attachment_list, int *output_attachment_count)
10289 {
10290         EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], input_transaction[%d], output_attachment_list[%p], output_attachment_count[%p]", input_mail_id, input_transaction, output_attachment_list, output_attachment_count);
10291
10292         if (input_mail_id <= 0 || !output_attachment_list || !output_attachment_count)  {
10293                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10294                 return EMAIL_ERROR_INVALID_PARAM;
10295         }
10296
10297         int                         error = EMAIL_ERROR_NONE;
10298         int                         i = 0;
10299         int                         rc = -1;
10300         char                      **result = NULL;
10301         char                        sql_query_string[QUERY_SIZE] = {0, };
10302         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10303         DB_STMT hStmt = NULL;
10304         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10305
10306         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
10307         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", input_mail_id);
10308         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10309         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
10310                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10311
10312         *output_attachment_count = atoi(result[1]);
10313         sqlite3_free_table(result);
10314
10315         if(*output_attachment_count == 0) {
10316                 error = EMAIL_ERROR_NONE;
10317                 goto FINISH_OFF;
10318         }
10319
10320         p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * (*output_attachment_count));
10321
10322         if (!p_data_tbl)  {
10323                 EM_DEBUG_EXCEPTION("em_malloc failed...");
10324                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10325                 goto FINISH_OFF;
10326         }
10327
10328         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE mail_id = %d ORDER BY attachment_id", input_mail_id);
10329         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
10330
10331         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10332         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)));
10333
10334         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10335         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },   ("sqlite3_step fail:%d", rc));
10336
10337         if (rc == SQLITE_DONE)  {
10338                 EM_DEBUG_EXCEPTION("no matched attachment found...");
10339                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10340                 goto FINISH_OFF;
10341         }
10342         for (i = 0; i < *output_attachment_count; i++)  {
10343                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10344                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10345                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10346                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10347                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10348                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10349                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10350                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10351                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10352                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10353                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10354                 _get_stmt_field_data_int   (hStmt, &(p_data_tbl[i].attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10355                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10356                 EM_DEBUG_LOG("attachment[%d].attachment_id : %d", i, p_data_tbl[i].attachment_id);
10357                 EM_DEBUG_LOG("attachment_mime_type : %s", p_data_tbl[i].attachment_mime_type);
10358                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10359                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
10360         }
10361
10362 FINISH_OFF:
10363
10364         if (error == EMAIL_ERROR_NONE)
10365                 *output_attachment_list = p_data_tbl;
10366         else if (p_data_tbl != NULL)
10367                 emstorage_free_attachment(&p_data_tbl, *output_attachment_count, NULL);
10368
10369         if (hStmt) {
10370                 rc = sqlite3_finalize(hStmt);
10371                 if (rc != SQLITE_OK)  {
10372                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
10373                         error = EMAIL_ERROR_DB_FAILURE;
10374                         if (*output_attachment_list)
10375                                 emstorage_free_attachment(output_attachment_list, *output_attachment_count, NULL); /* prevent */
10376                 }
10377         }
10378
10379         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
10380
10381         EM_DEBUG_FUNC_END("error [%d]", error);
10382         return error;
10383 }
10384
10385 INTERNAL_FUNC int emstorage_get_attachment(char *multi_user_name, int attachment_id, emstorage_attachment_tbl_t **attachment, int transaction, int *err_code)
10386 {
10387         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], transaction[%d], err_code[%p]", attachment_id, attachment, transaction, err_code);
10388
10389         if (attachment_id <= 0 || !attachment)  {
10390                 EM_DEBUG_EXCEPTION("attachment_id[%d], attachment[%p]", attachment_id, attachment);
10391                 if (err_code != NULL)
10392                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10393                 return false;
10394         }
10395
10396         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10397         int rc, ret = false;
10398         int error = EMAIL_ERROR_NONE;
10399         char sql_query_string[QUERY_SIZE] = {0, };
10400
10401         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10402         EMSTORAGE_START_READ_TRANSACTION(transaction);
10403
10404         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_id = %d",  attachment_id);
10405
10406         sqlite3_stmt* hStmt = NULL;
10407
10408         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10409         EM_DEBUG_LOG_DEV (" before sqlite3_prepare hStmt = %p", hStmt);
10410
10411         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10412                 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10413
10414
10415         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10416         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10417                 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10418
10419         if (rc == SQLITE_DONE)  {
10420                 EM_DEBUG_LOG ("no matched attachment found...");
10421                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10422                 goto FINISH_OFF;
10423         }
10424
10425         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1)))  {
10426                 EM_DEBUG_EXCEPTION("malloc failed...");
10427                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10428                 goto FINISH_OFF;
10429         }
10430
10431         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10432         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10433         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10434         _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10435         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10436         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10437         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10438         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10439         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10440         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10441         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10442         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10443         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10444
10445 #ifdef __ATTACHMENT_OPTI__
10446                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10447                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10448 #endif
10449
10450         ret = true;
10451
10452 FINISH_OFF:
10453         if (ret == true)
10454                 *attachment = p_data_tbl;
10455
10456         if (hStmt != NULL)  {
10457                 rc = sqlite3_finalize(hStmt);
10458                 if (rc != SQLITE_OK)  {
10459                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10460                         error = EMAIL_ERROR_DB_FAILURE;
10461                 }
10462         }
10463
10464         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10465
10466         if (err_code != NULL)
10467                 *err_code = error;
10468
10469         EM_DEBUG_FUNC_END("ret [%d]", ret);
10470         return ret;
10471 }
10472
10473 INTERNAL_FUNC int emstorage_get_attachment_nth(char *multi_user_name, int mail_id, int nth, emstorage_attachment_tbl_t **attachment_tbl, int transaction, int *err_code)
10474 {
10475         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);
10476
10477         if (mail_id <= 0 || nth <= 0 || !attachment_tbl)  {
10478                 EM_DEBUG_EXCEPTION(" mail_id[%d], nth[%d], attachment[%p]", mail_id, nth, attachment_tbl);
10479
10480                 if (err_code != NULL)
10481                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10482                 return false;
10483         }
10484
10485         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10486         char *p = NULL;
10487         int rc, ret = false;
10488         int error = EMAIL_ERROR_NONE;
10489         char sql_query_string[QUERY_SIZE] = {0, };
10490
10491         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10492         EMSTORAGE_START_READ_TRANSACTION(transaction);
10493
10494         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));
10495         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10496
10497         DB_STMT hStmt = NULL;
10498
10499         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10500         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10501                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10502
10503
10504         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10505         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10506                 ("sqlite3_step fail:%d", rc));
10507
10508         if (rc == SQLITE_DONE)  {
10509                 EM_DEBUG_EXCEPTION("no matched attachment found: mail_id[%d] nth[%d]", mail_id, nth);
10510                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10511                 goto FINISH_OFF;
10512         }
10513
10514         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1)))  {
10515                 EM_DEBUG_EXCEPTION(" malloc failed...");
10516                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10517                 goto FINISH_OFF;
10518         }
10519
10520         p_data_tbl->attachment_id = sqlite3_column_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10521         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10522                 p_data_tbl->attachment_name = cpy_str(p);
10523         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10524                 p_data_tbl->attachment_path = cpy_str(p);
10525         if ((p = (char *)sqlite3_column_text(hStmt, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10526                 p_data_tbl->content_id = cpy_str(p);
10527         p_data_tbl->attachment_size = sqlite3_column_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10528         p_data_tbl->mail_id = sqlite3_column_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10529         p_data_tbl->account_id = sqlite3_column_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10530         p_data_tbl->mailbox_id = sqlite3_column_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10531         p_data_tbl->attachment_save_status = sqlite3_column_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10532         p_data_tbl->attachment_drm_type = sqlite3_column_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10533         p_data_tbl->attachment_drm_method = sqlite3_column_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10534         p_data_tbl->attachment_inline_content_status = sqlite3_column_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10535         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10536                 p_data_tbl->attachment_mime_type = cpy_str(p);
10537 #ifdef __ATTACHMENT_OPTI__
10538                 p_data_tbl->encoding = sqlite3_column_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10539                 if ((p = (char *)sqlite3_column_text(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10540                         p_data_tbl->section= cpy_str(p);
10541 #endif
10542         ret = true;
10543
10544 FINISH_OFF:
10545         if (ret == true)
10546                 *attachment_tbl = p_data_tbl;
10547
10548         if (hStmt != NULL)  {
10549                 rc = sqlite3_finalize(hStmt);
10550                 if (rc != SQLITE_OK)  {
10551                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10552                         error = EMAIL_ERROR_DB_FAILURE;
10553                 }
10554         }
10555
10556         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10557
10558         if (err_code != NULL)
10559                 *err_code = error;
10560
10561         EM_DEBUG_FUNC_END("ret [%d]", ret);
10562         return ret;
10563 }
10564
10565 INTERNAL_FUNC int emstorage_get_attachment_by_attachment_path(char *multi_user_name, char *attachment_path, emstorage_attachment_tbl_t **attachment, int transaction, int *err_code)
10566 {
10567         EM_DEBUG_FUNC_BEGIN("attachment_path[%p], attachment[%p], transaction[%d], err_code[%p]", attachment_path, attachment, transaction, err_code);
10568
10569         if (attachment_path == NULL || !attachment)  {
10570                 EM_DEBUG_EXCEPTION("attachment_path[%p], attachment[%p]", attachment_path, attachment);
10571                 if (err_code != NULL)
10572                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10573                 return false;
10574         }
10575
10576         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10577         int rc, ret = false;
10578         int error = EMAIL_ERROR_NONE;
10579         char sql_query_string[QUERY_SIZE] = {0, };
10580
10581         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10582         EMSTORAGE_START_READ_TRANSACTION(transaction);
10583
10584         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_path = '%s'", attachment_path);
10585
10586         sqlite3_stmt* hStmt = NULL;
10587
10588         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10589         EM_DEBUG_LOG_DEV ("before sqlite3_prepare hStmt = %p", hStmt);
10590
10591         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10592                 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10593
10594
10595         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10596         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10597                 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10598
10599         if (rc == SQLITE_DONE)  {
10600                 EM_DEBUG_LOG ("no matched attachment found...");
10601                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10602                 goto FINISH_OFF;
10603         }
10604
10605         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1)))  {
10606                 EM_DEBUG_EXCEPTION("malloc failed...");
10607                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10608                 goto FINISH_OFF;
10609         }
10610
10611         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10612         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10613         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10614         _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10615         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10616         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10617         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10618         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10619         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10620         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10621         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10622         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10623         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10624
10625 #ifdef __ATTACHMENT_OPTI__
10626                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10627                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10628 #endif
10629
10630         ret = true;
10631
10632 FINISH_OFF:
10633         if (ret == true)
10634                 *attachment = p_data_tbl;
10635
10636         if (hStmt != NULL)  {
10637                 rc = sqlite3_finalize(hStmt);
10638                 if (rc != SQLITE_OK)  {
10639                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10640                         error = EMAIL_ERROR_DB_FAILURE;
10641                 }
10642         }
10643
10644         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10645
10646         if (err_code != NULL)
10647                 *err_code = error;
10648
10649         EM_DEBUG_FUNC_END("ret [%d]", ret);
10650         return ret;
10651 }
10652
10653 INTERNAL_FUNC int emstorage_change_attachment_field(char *multi_user_name, int mail_id, email_mail_change_type_t type, emstorage_attachment_tbl_t *attachment, int transaction, int *err_code)
10654 {
10655         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], attachment[%p], transaction[%d], err_code[%p]", mail_id, type, attachment, transaction, err_code);
10656
10657         if (mail_id <= 0 || !attachment)  {
10658                 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], attachment[%p]", mail_id, type, attachment);
10659                 if (err_code != NULL)
10660                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10661                 return false;;
10662         }
10663
10664         int rc, ret = false;
10665         int error = EMAIL_ERROR_NONE;
10666         DB_STMT hStmt = NULL;
10667         char sql_query_string[QUERY_SIZE] = {0, };
10668
10669         int i = 0;
10670
10671         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10672
10673         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10674
10675         switch (type)  {
10676                 case UPDATE_MAILBOX:
10677                                 EM_DEBUG_LOG("UPDATE_MAILBOX");
10678                         if (!attachment->mailbox_id)  {
10679                                 EM_DEBUG_EXCEPTION(" attachment->mailbox_id[%d]", attachment->mailbox_id);
10680                                 error = EMAIL_ERROR_INVALID_PARAM;
10681                                 goto FINISH_OFF;
10682                         }
10683                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
10684                                 "UPDATE mail_attachment_tbl SET mailbox_id = ? WHERE mail_id = %d", mail_id);
10685
10686                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10687                         EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10688                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10689                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10690
10691                         _bind_stmt_field_data_int(hStmt, i++, attachment->mailbox_id);
10692                         break;
10693
10694                 case UPDATE_SAVENAME:
10695                         EM_DEBUG_LOG("UPDATE_SAVENAME");
10696                         if (!attachment->attachment_path)  {
10697                                 EM_DEBUG_EXCEPTION(" attachment->attachment_path[%p]", attachment->attachment_path);
10698                                 error = EMAIL_ERROR_INVALID_PARAM;
10699                                 goto FINISH_OFF;
10700                         }
10701
10702                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
10703                                 "UPDATE mail_attachment_tbl SET"
10704                                 "  attachment_size = ?"
10705                                 ", attachment_save_status = ?"
10706                                 ", attachment_path = ?"
10707                                 " WHERE mail_id = %d"
10708                                 " AND attachment_id = %d"
10709                                 , attachment->mail_id
10710                                 , attachment->attachment_id);
10711
10712
10713                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10714                         EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10715                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10716                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10717
10718                         _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_size);
10719                         _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_save_status);
10720                         _bind_stmt_field_data_string(hStmt, i++, (char *)attachment->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10721                         break;
10722
10723                 default:
10724                         EM_DEBUG_LOG("type[%d]", type);
10725                         error = EMAIL_ERROR_INVALID_PARAM;
10726                         goto FINISH_OFF;
10727         }
10728         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10729
10730         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10731         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10732                 ("sqlite3_step fail:%d", rc));
10733         ret = true;
10734
10735 FINISH_OFF:
10736
10737         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10738
10739         if (hStmt != NULL)  {
10740                 rc = sqlite3_finalize(hStmt);
10741                 if (rc != SQLITE_OK)  {
10742                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10743                         error = EMAIL_ERROR_DB_FAILURE;
10744                 }
10745         }
10746
10747         if (err_code != NULL)
10748                 *err_code = error;
10749         EM_DEBUG_FUNC_END("ret [%d]", ret);
10750         return ret;
10751 }
10752
10753 INTERNAL_FUNC int emstorage_rename_mailbox(char *multi_user_name, int input_mailbox_id, char *input_new_mailbox_name, char *input_new_mailbox_alias, void *input_eas_data, int input_eas_data_length, int input_transaction)
10754 {
10755         EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d] input_new_mailbox_name[%p] input_new_mailbox_alias[%p] input_eas_data[%p] input_eas_data_length[%d] input_transaction[%d]", input_mailbox_id, input_new_mailbox_name, input_new_mailbox_alias, input_eas_data, input_eas_data_length, input_transaction);
10756
10757         int rc = 0;
10758         int ret = false;
10759         int error = EMAIL_ERROR_NONE;
10760         int field_idx = 0;
10761         int account_id = 0;
10762         char sql_query_string[QUERY_SIZE] = {0, };
10763         char *replaced_mailbox_name = NULL;
10764         char *replaced_alias = NULL;
10765         sqlite3 *local_db_handle = NULL;
10766         DB_STMT hStmt = NULL;
10767         emstorage_mailbox_tbl_t *old_mailbox_data = NULL;
10768
10769         if (input_mailbox_id <= 0 || !input_new_mailbox_name || !input_new_mailbox_alias)  {
10770                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10771                 return EMAIL_ERROR_INVALID_PARAM;
10772         }
10773
10774         if (strstr(input_new_mailbox_name, "'")) {
10775                 replaced_mailbox_name = em_replace_all_string(input_new_mailbox_name, "'", "''");
10776         } else {
10777                 replaced_mailbox_name = strdup(input_new_mailbox_name);
10778         }
10779
10780         if (strstr(input_new_mailbox_alias, "'")) {
10781                 replaced_alias = em_replace_all_string(input_new_mailbox_alias, "'", "''");
10782         } else {
10783                 replaced_alias = strdup(input_new_mailbox_alias);
10784         }
10785
10786         local_db_handle = emstorage_get_db_connection(multi_user_name);
10787
10788         if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &old_mailbox_data)) != EMAIL_ERROR_NONE) {
10789                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", error);
10790                 EM_SAFE_FREE (replaced_mailbox_name);
10791                 EM_SAFE_FREE (replaced_alias);
10792                 return error;
10793         }
10794
10795     if (old_mailbox_data == NULL) {
10796         EM_DEBUG_LOG("old_mailbox_data is NULL");
10797         error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
10798         goto FINISH_OFF;
10799     }
10800
10801         account_id = old_mailbox_data->account_id;
10802
10803         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
10804
10805         if(input_eas_data && input_eas_data_length > 0) {
10806                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10807                         "UPDATE mail_box_tbl SET"
10808                         " mailbox_name = ?"
10809                         ",alias = ?"
10810                         ",eas_data = ?"
10811                         ",eas_data_length = ?"
10812                         " WHERE mailbox_id = ?");
10813
10814                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10815
10816                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10817                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10818
10819                 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10820                 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10821                 _bind_stmt_field_data_blob  (hStmt, field_idx++ , input_eas_data, input_eas_data_length);
10822                 _bind_stmt_field_data_int   (hStmt, field_idx++ , input_eas_data_length);
10823                 _bind_stmt_field_data_int   (hStmt, field_idx++ , input_mailbox_id);
10824         }
10825         else {
10826                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10827                                 "UPDATE mail_box_tbl SET"
10828                                 " mailbox_name = ?"
10829                                 ",alias = ?"
10830                                 " WHERE mailbox_id = ?");
10831
10832                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10833
10834                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10835                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10836
10837                 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10838                 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10839                 _bind_stmt_field_data_int   (hStmt, field_idx++ , input_mailbox_id);
10840         }
10841
10842
10843         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10844         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
10845                 ("sqlite3_step fail:%d", rc));
10846         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10847                 ("sqlite3_step fail:%d", rc));
10848
10849         if (sqlite3_changes(local_db_handle) == 0)
10850                 EM_DEBUG_LOG("no mail_meeting_tbl matched...");
10851
10852         ret = true;
10853
10854 FINISH_OFF:
10855         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, error);
10856         if (error == EMAIL_ERROR_NONE) {
10857                 if (!emcore_notify_storage_event (NOTI_MAILBOX_RENAME, account_id, input_mailbox_id, input_new_mailbox_name, 0))
10858                         EM_DEBUG_EXCEPTION ("emcore_notify_storage_event Failed [ NOTI_MAILBOX_RENAME ] >>>> ");
10859         }
10860         else {
10861                 if (!emcore_notify_storage_event (NOTI_MAILBOX_RENAME_FAIL, account_id, input_mailbox_id, input_new_mailbox_name, error))
10862                         EM_DEBUG_EXCEPTION ("emcore_notify_storage_event Failed [ NOTI_MAILBOX_RENAME_FAIL ] >>>> ");
10863         }
10864
10865         EM_SAFE_FREE(replaced_mailbox_name);
10866         EM_SAFE_FREE(replaced_alias);
10867
10868         if (old_mailbox_data)
10869                 emstorage_free_mailbox(&old_mailbox_data, 1, NULL);
10870
10871         if (hStmt != NULL)  {
10872                 rc = sqlite3_finalize(hStmt);
10873                 if (rc != SQLITE_OK)  {
10874                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10875                         error = EMAIL_ERROR_DB_FAILURE;
10876                 }
10877         }
10878
10879         EM_DEBUG_FUNC_END("error [%d]", error);
10880         return error;
10881 }
10882
10883 INTERNAL_FUNC int emstorage_get_new_attachment_no(char *multi_user_name, int *attachment_no, int *err_code)
10884 {
10885         EM_DEBUG_FUNC_BEGIN("attachment_no [%p], err_code[%p]", attachment_no, err_code);
10886         int rc, ret = false;
10887         int error = EMAIL_ERROR_NONE;
10888         char *sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
10889         char **result;
10890
10891         if (!attachment_no)  {
10892                 EM_DEBUG_EXCEPTION("Invalid attachment");
10893                 if (err_code != NULL)
10894                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10895                 return false;
10896         }
10897
10898         *attachment_no = -1;
10899
10900         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10901
10902
10903         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
10904         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
10905                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
10906
10907         if (NULL == result[1])
10908                 rc = 1;
10909         else
10910                 rc = atoi(result[1])+1;
10911
10912         sqlite3_free_table(result);
10913
10914         *attachment_no = rc;
10915         EM_DEBUG_LOG("attachment_no [%d]", *attachment_no);
10916         ret = true;
10917
10918 FINISH_OFF:
10919
10920         if (err_code != NULL)
10921                 *err_code = error;
10922
10923         EM_DEBUG_FUNC_END("ret [%d]", ret);
10924         return ret;
10925 }
10926
10927 INTERNAL_FUNC int emstorage_add_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int iscopy, int transaction, int *err_code)
10928 {
10929         EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], iscopy[%d], transaction[%d], err_code[%p]", attachment_tbl, iscopy, transaction, err_code);
10930
10931         char *sql = NULL;
10932         char **result;
10933         int rc, ret = false;
10934         int error = EMAIL_ERROR_NONE;
10935         char sql_query_string[QUERY_SIZE] = {0, };
10936
10937         DB_STMT hStmt = NULL;
10938         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10939
10940         if (!attachment_tbl)  {
10941                 EM_DEBUG_EXCEPTION("attachment_tbl[%p], iscopy[%d]", attachment_tbl, iscopy);
10942                 if (err_code != NULL)
10943                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10944                 return false;
10945         }
10946
10947         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10948
10949         sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
10950
10951         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
10952         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
10953                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
10954
10955         if (NULL==result[1]) rc = 1;
10956         else rc = atoi(result[1]) + 1;
10957         sqlite3_free_table(result);
10958
10959         attachment_tbl->attachment_id = rc;
10960
10961         SNPRINTF(sql_query_string, sizeof(sql_query_string),
10962                 "INSERT INTO mail_attachment_tbl VALUES "
10963                 "( ?"   /* attachment_id */
10964                 ", ?"   /* attachment_name */
10965                 ", ?"   /* attachment_path */
10966                 ", ?"   /* content_id */
10967                 ", ?"   /* attachment_size */
10968                 ", ?"   /* mail_id */
10969                 ", ?"   /* account_id */
10970                 ", ?"   /* mailbox_id */
10971                 ", ?"   /* attachment_save_status */
10972                 ", ?"   /* attachment_drm_type */
10973                 ", ?"   /* attachment_drm_method */
10974                 ", ?"   /* attachment_inline_content_status */
10975                 ", ?"   /* attachment_mime_type */
10976 #ifdef __ATTACHMENT_OPTI__
10977                 ", ?"
10978                 ", ?"
10979 #endif
10980                 ")");
10981
10982
10983         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10984         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
10985                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10986
10987         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_id);
10988         _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);
10989         _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);
10990         _bind_stmt_field_data_string(hStmt, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->content_id, 0, CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL);
10991         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_size);
10992         _bind_stmt_field_data_int   (hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mail_id);
10993         _bind_stmt_field_data_int   (hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->account_id);
10994         _bind_stmt_field_data_int   (hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mailbox_id);
10995         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_save_status);
10996         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_type);
10997         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_method);
10998         _bind_stmt_field_data_int   (hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_inline_content_status);
10999         _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);
11000 #ifdef __ATTACHMENT_OPTI__
11001         _bind_stmt_field_data_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->encoding);
11002         _bind_stmt_field_data_string(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->section, 0, ATTACHMENT_LEN_IN_MAIL_ATTACHMENT_TBL);
11003 #endif
11004
11005
11006         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11007         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
11008                 ("sqlite3_step fail:%d", rc));
11009         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11010                 ("sqlite3_step fail:%d", rc));
11011
11012         rc = sqlite3_changes(local_db_handle);
11013         if (rc == 0)  {
11014                 EM_DEBUG_LOG(" no matched mail found...");
11015                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11016                 goto FINISH_OFF;
11017         }
11018
11019         ret = true;
11020
11021 FINISH_OFF:
11022         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11023
11024         if (hStmt != NULL)  {
11025                 rc = sqlite3_finalize(hStmt);
11026                 if (rc != SQLITE_OK)  {
11027                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11028                         error = EMAIL_ERROR_DB_FAILURE;
11029                 }
11030         }
11031
11032         if (err_code != NULL)
11033         *err_code = error;
11034
11035         EM_DEBUG_FUNC_END("ret [%d]", ret);
11036         return ret;
11037 }
11038
11039 INTERNAL_FUNC int emstorage_update_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int transaction, int *err_code)
11040 {
11041         EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], transaction[%d], err_code[%p]", attachment_tbl, transaction, err_code);
11042
11043         int rc, ret = false, field_idx = 0;
11044         int error = EMAIL_ERROR_NONE;
11045         DB_STMT hStmt = NULL;
11046         char sql_query_string[QUERY_SIZE] = {0, };
11047
11048         if (!attachment_tbl)  {
11049                 EM_DEBUG_EXCEPTION(" attachment_tbl[%p] ", attachment_tbl);
11050                 if (err_code)
11051                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11052                 return false;
11053         }
11054
11055         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11056
11057         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11058
11059         SNPRINTF(sql_query_string, sizeof(sql_query_string),
11060                 "UPDATE mail_attachment_tbl SET  "
11061                 "  attachment_name = ?"
11062                 ", attachment_path =  ?"
11063                 ", content_id = ?"
11064                 ", attachment_size = ?"
11065                 ", mail_id = ?"
11066                 ", account_id = ?"
11067                 ", mailbox_id = ?"
11068                 ", attachment_save_status = ?"
11069                 ", attachment_drm_type = ?"
11070                 ", attachment_drm_method = ?"
11071                 ", attachment_inline_content_status = ? "
11072                 ", attachment_mime_type = ? "
11073                 " WHERE attachment_id = ?;");
11074
11075
11076         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11077
11078         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11079                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11080
11081         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
11082         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
11083         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->content_id, 0, CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL);
11084         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_size);
11085         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->mail_id);
11086         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->account_id);
11087         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->mailbox_id);
11088         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_save_status);
11089         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_drm_type);
11090         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_drm_method);
11091         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_inline_content_status);
11092         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_mime_type, 0, ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL);
11093         _bind_stmt_field_data_int   (hStmt, field_idx++ , attachment_tbl->attachment_id);
11094
11095
11096         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11097         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
11098                 ("sqlite3_step fail:%d", rc));
11099         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
11100                 ("sqlite3_step fail:%d", rc));
11101
11102         rc = sqlite3_changes(local_db_handle);
11103         if (rc == 0)  {
11104                 EM_DEBUG_LOG(" no matched mail found...");
11105                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11106                 goto FINISH_OFF;
11107         }
11108
11109         ret = true;
11110
11111 FINISH_OFF:
11112         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11113         if (hStmt != NULL)  {
11114                 rc = sqlite3_finalize(hStmt);
11115                 if (rc != SQLITE_OK)  {
11116                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11117                         error = EMAIL_ERROR_DB_FAILURE;
11118                 }
11119         }
11120
11121         if (err_code != NULL)
11122         *err_code = error;
11123
11124         EM_DEBUG_FUNC_END("ret [%d]", ret);
11125         return ret;
11126 }
11127
11128 INTERNAL_FUNC int emstorage_delete_attachment_on_db(char *multi_user_name, int attachment_id, int transaction, int *err_code)
11129 {
11130         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], transaction[%d], err_code[%p]", attachment_id, transaction, err_code);
11131
11132         if (attachment_id < 0)  {
11133                 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
11134                 if (err_code != NULL)
11135                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11136                 return false;
11137         }
11138
11139         int ret = false;
11140         int error = EMAIL_ERROR_NONE;
11141         char sql_query_string[QUERY_SIZE] = {0, };
11142         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11143
11144         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11145
11146         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
11147
11148         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11149         if (error != EMAIL_ERROR_NONE) {
11150                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11151                         goto FINISH_OFF;
11152         }
11153
11154         ret = true;
11155
11156 FINISH_OFF:
11157         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11158
11159         if (err_code)
11160                 *err_code = error;
11161
11162         EM_DEBUG_FUNC_END("ret [%d]", ret);
11163         return ret;
11164 }
11165
11166 INTERNAL_FUNC int emstorage_delete_all_attachments_of_mail(char *multi_user_name, int mail_id, int transaction, int *err_code)
11167 {
11168         EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
11169         int ret = false;
11170         int error = EMAIL_ERROR_NONE;
11171         char sql_query_string[QUERY_SIZE] = {0, };
11172         sqlite3 *local_db_handle = NULL;
11173
11174         if (mail_id <= 0)  {
11175                 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
11176                 if (err_code != NULL)
11177                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11178                 return false;
11179         }
11180
11181         local_db_handle = emstorage_get_db_connection(multi_user_name);
11182
11183         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11184
11185         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
11186         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11187         if (error != EMAIL_ERROR_NONE) {
11188                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11189                         goto FINISH_OFF;
11190         }
11191
11192         ret = true;
11193
11194 FINISH_OFF:
11195         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11196
11197         if (err_code)
11198                 *err_code = error;
11199
11200         EM_DEBUG_FUNC_END("ret [%d]", ret);
11201         return ret;
11202 }
11203
11204 INTERNAL_FUNC int emstorage_delete_attachment_all_on_db(char *multi_user_name, int account_id, char *mailbox, int transaction, int *err_code)
11205 {
11206         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox, transaction, err_code);
11207
11208         int error = EMAIL_ERROR_NONE;
11209         int ret = false;
11210         char sql_query_string[QUERY_SIZE] = {0, };
11211         char *replaced_mailbox = NULL;
11212         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11213
11214         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11215
11216         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl");
11217
11218         if (account_id != ALL_ACCOUNT) /*  '0' means all account */
11219                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE account_id = %d", account_id);
11220
11221         if (mailbox)    /*  NULL means all mailbox_name */ {
11222                 if (strstr(mailbox, "'")) {
11223                         replaced_mailbox = em_replace_all_string(mailbox, "'", "''");
11224                 } else {
11225                         replaced_mailbox = strdup(mailbox);
11226                 }
11227
11228                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s mailbox_name = '%s'", account_id != ALL_ACCOUNT ? "AND" : "WHERE", replaced_mailbox);
11229                 EM_SAFE_FREE(replaced_mailbox); /*prevent 49434*/
11230         }
11231
11232         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11233         if (error != EMAIL_ERROR_NONE) {
11234                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11235                         goto FINISH_OFF;
11236         }
11237
11238         ret = true;
11239
11240 FINISH_OFF:
11241         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11242
11243         if (err_code != NULL)
11244                 *err_code = error;
11245
11246         EM_DEBUG_FUNC_END("ret [%d]", ret);
11247         return ret;
11248 }
11249
11250 INTERNAL_FUNC int emstorage_free_attachment(emstorage_attachment_tbl_t** attachment_tbl_list, int count, int *err_code)
11251 {
11252         EM_DEBUG_FUNC_BEGIN("attachment_tbl_list[%p], count[%d], err_code[%p]", attachment_tbl_list, count, err_code);
11253
11254         if (count > 0)  {
11255                 if ((attachment_tbl_list == NULL) || (*attachment_tbl_list == NULL))  {
11256                         EM_DEBUG_LOG("Nothing to free: attachment_tbl_list[%p], count[%d]", attachment_tbl_list, count);
11257                         if (err_code != NULL)
11258                                 *err_code = EMAIL_ERROR_INVALID_PARAM;
11259                         return false;
11260                 }
11261
11262                 emstorage_attachment_tbl_t* p = *attachment_tbl_list;
11263                 int i;
11264
11265                 for (i = 0; i < count; i++)  {
11266                         EM_SAFE_FREE(p[i].attachment_name);
11267                         EM_SAFE_FREE(p[i].attachment_path);
11268                         EM_SAFE_FREE(p[i].content_id);
11269                         EM_SAFE_FREE(p[i].attachment_mime_type);
11270 #ifdef __ATTACHMENT_OPTI__
11271                         EM_SAFE_FREE(p[i].section);
11272 #endif
11273                 }
11274
11275                 EM_SAFE_FREE(p);
11276                 *attachment_tbl_list = NULL;
11277         }
11278
11279         if (err_code != NULL)
11280                 *err_code = EMAIL_ERROR_NONE;
11281         EM_DEBUG_FUNC_END();
11282         return true;
11283 }
11284
11285 INTERNAL_FUNC int emstorage_begin_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11286 {
11287         EM_PROFILE_BEGIN(emStorageBeginTransaction);
11288         int ret = true;
11289
11290         _timedlock_shm_mutex(mapped_for_db_lock, 2);
11291
11292         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11293
11294         int rc;
11295         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN immediate;", NULL, NULL, NULL), rc);
11296         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11297                 ("SQL(BEGIN) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11298
11299         if (ret == false) {
11300                 if (err_code != NULL) *err_code = EMAIL_ERROR_DB_FAILURE;
11301         }
11302
11303         EM_PROFILE_END(emStorageBeginTransaction);
11304         EM_DEBUG_FUNC_END("ret [%d]", ret);
11305         return ret;
11306 }
11307
11308 INTERNAL_FUNC int emstorage_commit_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11309 {
11310         EM_DEBUG_FUNC_BEGIN();
11311         int ret = true;
11312         int rc;
11313
11314         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11315
11316         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
11317         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; }, ("SQL(END) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11318
11319         if (ret == false && err_code != NULL)
11320                 *err_code = EMAIL_ERROR_DB_FAILURE;
11321
11322         _unlockshm_mutex(mapped_for_db_lock);
11323
11324         EM_DEBUG_FUNC_END("ret [%d]", ret);
11325         return ret;
11326 }
11327
11328 INTERNAL_FUNC int emstorage_rollback_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11329 {
11330         EM_DEBUG_FUNC_BEGIN();
11331         int ret = true;
11332         int rc;
11333         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11334
11335         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "ROLLBACK;", NULL, NULL, NULL), rc);
11336         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11337                 ("SQL(ROLLBACK) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11338
11339         if (ret == false && err_code != NULL)
11340                 *err_code = EMAIL_ERROR_DB_FAILURE;
11341
11342         _unlockshm_mutex(mapped_for_db_lock);
11343
11344         EM_DEBUG_FUNC_END("ret [%d]", ret);
11345         return ret;
11346 }
11347
11348 INTERNAL_FUNC int emstorage_is_mailbox_full(char *multi_user_name, int account_id, email_mailbox_t *mailbox, int *result, int *err_code)
11349 {
11350         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], result[%p], err_code[%p]", account_id, mailbox, result, err_code);
11351
11352         if (account_id < FIRST_ACCOUNT_ID || !mailbox || !result)  {
11353                 if (mailbox)
11354                         EM_DEBUG_EXCEPTION("Invalid Parameter. accoun_id[%d], mailbox[%p]", account_id, mailbox);
11355
11356                 if (err_code != NULL)
11357                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11358
11359                 return false;
11360         }
11361
11362         int ret = false;
11363         int error = EMAIL_ERROR_NONE;
11364         int mail_count = 0;
11365
11366         if (!emstorage_get_mail_count(multi_user_name, account_id, mailbox->mailbox_id, &mail_count, NULL, true, &error)) {
11367                 EM_DEBUG_EXCEPTION("emstorage_get_mail_count failed [%d]", error);
11368                 goto FINISH_OFF;
11369         }
11370
11371         if (mailbox) {
11372                 EM_DEBUG_LOG("mail_count[%d] mail_slot_size[%d]", mail_count, mailbox->mail_slot_size);
11373                 if (mail_count >= mailbox->mail_slot_size)
11374                         *result = true;
11375                 else
11376                         *result = false;
11377
11378                 ret = true;
11379         }
11380
11381         ret = true;
11382 FINISH_OFF:
11383
11384         if (err_code != NULL)
11385                 *err_code = error;
11386
11387         EM_DEBUG_FUNC_END("ret [%d]", ret);
11388         return ret;
11389 }
11390
11391 INTERNAL_FUNC int emstorage_clear_mail_data(char *multi_user_name, int transaction, int *err_code)
11392 {
11393         EM_DEBUG_FUNC_BEGIN("transaction[%d], err_code[%p]", transaction, err_code);
11394
11395         int ret = false;
11396         int error = EMAIL_ERROR_NONE;
11397         char sql_query_string[QUERY_SIZE] = {0, };
11398         const email_db_object_t* tables = _g_db_tables;
11399         const email_db_object_t* indexes = _g_db_indexes;
11400
11401         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11402         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11403
11404         if (!emstorage_delete_dir(MAILHOME, &error)) {
11405                 EM_DEBUG_EXCEPTION(" emstorage_delete_dir failed - %d", error);
11406
11407                 goto FINISH_OFF;
11408         }
11409
11410         mkdir(MAILHOME, DIRECTORY_PERMISSION);
11411         mkdir(MAILTEMP, DIRECTORY_PERMISSION);
11412         chmod(MAILTEMP, 0777);
11413
11414         /*  first clear index. */
11415         while (indexes->object_name)  {
11416                 if (indexes->data_flag)  {
11417                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP index %s", indexes->object_name);
11418                         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11419                         if (error != EMAIL_ERROR_NONE) {
11420                                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11421                                         goto FINISH_OFF;
11422                         }
11423                 }
11424                 indexes++;
11425         }
11426
11427         while (tables->object_name)  {
11428                 if (tables->data_flag)  {
11429                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP table %s", tables->object_name);
11430                         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11431                         if (error != EMAIL_ERROR_NONE) {
11432                                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11433                                         goto FINISH_OFF;
11434                         }
11435                 }
11436
11437                 tables++;
11438         }
11439         ret = true;
11440
11441 FINISH_OFF:
11442         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11443
11444         if (err_code != NULL)
11445                 *err_code = error;
11446
11447         EM_DEBUG_FUNC_END("ret [%d]", ret);
11448         return ret;
11449 }
11450 /*======================= DB File Utils =============================================*/
11451 #include <dirent.h>
11452 #include <sys/types.h>
11453 #define  DIR_SEPERATOR "/"
11454
11455 INTERNAL_FUNC char *emstorage_make_directory_path_from_file_path(char *file_name)
11456 {
11457         EM_DEBUG_FUNC_BEGIN("Filename [ %p ]", file_name);
11458         char delims[] = "/";
11459         char *result = NULL;
11460         gchar **token = NULL;
11461
11462         token = g_strsplit_set(file_name, delims, 1);
11463
11464         if (token && token[0]) {
11465                 EM_DEBUG_LOG_SEC(">>>> Directory_name [ %s ]", token[0]);
11466                 result = EM_SAFE_STRDUP(token[0]);
11467         } else
11468                 EM_DEBUG_LOG(">>>> No Need to create Directory");
11469
11470         g_strfreev(token);
11471
11472         return result;
11473 }
11474
11475 INTERNAL_FUNC int emstorage_get_save_name(char *multi_user_name, int account_id, int mail_id, int atch_id, 
11476                                                                                         char *fname, char *move_buf, char *path_buf, int maxlen, int *err_code)
11477 {
11478         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], mail_id[%d], atch_id[%d], fname[%s], move_buf[%p], path_buf[%p], err_code[%p]", account_id, mail_id, atch_id, fname, move_buf, path_buf, err_code);
11479         EM_PROFILE_BEGIN(profile_emstorage_get_save_name);
11480
11481         int ret = false;
11482         int error = EMAIL_ERROR_NONE;
11483         char *dir_name = NULL;
11484         char create_dir[1024]={0};
11485         char *temp_file = NULL;
11486         char *prefix_path = NULL;
11487
11488         if (!move_buf || !path_buf || account_id < FIRST_ACCOUNT_ID || mail_id < 0 || atch_id < 0)  {
11489                 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d], fname[%p], move_buf[%p], path_buf[%p]", account_id, mail_id, atch_id, fname, move_buf, path_buf);
11490                 error = EMAIL_ERROR_INVALID_PARAM;
11491                 goto FINISH_OFF;
11492         }
11493
11494         sprintf(path_buf, "%s", MAILHOME);
11495         sprintf(path_buf+EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, account_id);
11496
11497         if (mail_id > 0)
11498                 sprintf(path_buf+EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, mail_id);
11499
11500         if (atch_id > 0)
11501                 sprintf(path_buf+EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, atch_id);
11502
11503         if (fname) {
11504                 temp_file = EM_SAFE_STRDUP(fname);
11505                 if (temp_file && strstr(temp_file, "/")) {
11506                         dir_name = emstorage_make_directory_path_from_file_path(temp_file);
11507                 }
11508         }
11509
11510         if (dir_name) {
11511                 snprintf(create_dir, sizeof(create_dir), "%s%s%s", path_buf, DIR_SEPERATOR, dir_name);
11512                 EM_DEBUG_LOG(">>>>> DIR PATH [%s]", create_dir);
11513                 mkdir(create_dir, DIRECTORY_PERMISSION);
11514                 EM_SAFE_FREE(dir_name);
11515         }
11516
11517         if (fname) {
11518                 EM_DEBUG_LOG_DEV(">>>>> fname [%s]", fname);
11519                 if (EM_SAFE_STRLEN(fname) + EM_SAFE_STRLEN(path_buf) + strlen(DIR_SEPERATOR) > maxlen - 1) {
11520                         char *modified_name = NULL;
11521                         int remain_len  = (maxlen - 1) - EM_SAFE_STRLEN(path_buf) - strlen(DIR_SEPERATOR);
11522
11523                         if (remain_len <= 0) {
11524                                 error = EMAIL_ERROR_MAX_EXCEEDED;
11525                                 goto FINISH_OFF;
11526                         }
11527
11528                         if (remain_len > MAX_FILENAME) {
11529                                 remain_len = MAX_FILENAME;
11530                         }
11531
11532                         modified_name = em_shrink_filename(fname, remain_len);
11533
11534                         if (!modified_name) {
11535                                 error = EMAIL_ERROR_MAX_EXCEEDED;
11536                                 goto FINISH_OFF;
11537                         }
11538
11539                         sprintf(path_buf+EM_SAFE_STRLEN(path_buf), "%s%s", DIR_SEPERATOR, modified_name);
11540                         EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11541                         EM_SAFE_FREE(modified_name);
11542                 } else {
11543                         if (EM_SAFE_STRLEN(fname) > MAX_FILENAME - 1) {
11544                                 char *modified_name = NULL;
11545
11546                                 modified_name = em_shrink_filename(fname, MAX_FILENAME);
11547                                 if (!modified_name) {
11548                                         error = EMAIL_ERROR_MAX_EXCEEDED;
11549                                         goto FINISH_OFF;
11550                                 }
11551
11552                                 sprintf(path_buf+EM_SAFE_STRLEN(path_buf), "%s%s", DIR_SEPERATOR, modified_name);
11553                                 EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11554                                 EM_SAFE_FREE(modified_name);
11555                         } else {
11556                                 sprintf(path_buf+EM_SAFE_STRLEN(path_buf), "%s%s", DIR_SEPERATOR, fname);
11557                         }
11558                 }
11559         }
11560
11561         EM_DEBUG_LOG_SEC(">>>>> path_buf [%s]", path_buf);
11562
11563     if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11564                 error = emcore_get_container_path(multi_user_name, &prefix_path);
11565                 if (error != EMAIL_ERROR_NONE) {
11566                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11567                         goto FINISH_OFF;
11568                 }
11569         sprintf(move_buf, "%s/%s", prefix_path, path_buf);
11570         EM_DEBUG_LOG("move_buf : [%s]", move_buf);
11571     } else {
11572         sprintf(move_buf, "%s", path_buf);
11573         EM_DEBUG_LOG("move_buf : [%s]", move_buf);
11574     }
11575
11576         ret = true;
11577
11578 FINISH_OFF:
11579
11580         EM_SAFE_FREE(temp_file);
11581         EM_SAFE_FREE(prefix_path);
11582
11583         if (err_code != NULL)
11584                 *err_code = error;
11585
11586         EM_PROFILE_END(profile_emstorage_get_save_name);
11587         EM_DEBUG_FUNC_END("ret [%d]", ret);
11588         return ret;
11589 }
11590
11591 INTERNAL_FUNC int emstorage_get_dele_name(char *multi_user_name, int account_id, int mail_id, int atch_id, char *fname, char *name_buf, int *err_code)
11592 {
11593         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);
11594
11595         if (!name_buf || account_id < FIRST_ACCOUNT_ID)  {
11596                 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);
11597                 if (err_code != NULL)
11598                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11599                 return false;
11600         }
11601
11602         sprintf(name_buf+EM_SAFE_STRLEN(name_buf),      "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
11603
11604         if (mail_id > 0)
11605                 sprintf(name_buf+EM_SAFE_STRLEN(name_buf),      "%s%d", DIR_SEPERATOR, mail_id);
11606         else
11607                 goto FINISH_OFF;
11608
11609         if (atch_id > 0)
11610                 sprintf(name_buf+EM_SAFE_STRLEN(name_buf),      "%s%d", DIR_SEPERATOR, atch_id);
11611         else
11612                 goto FINISH_OFF;
11613
11614 FINISH_OFF:
11615         sprintf(name_buf+EM_SAFE_STRLEN(name_buf),      ".DELE");
11616
11617         EM_DEBUG_FUNC_END();
11618         return true;
11619 }
11620
11621 INTERNAL_FUNC int emstorage_create_dir(char *multi_user_name, int account_id, int mail_id, int atch_id, int *err_code)
11622 {
11623         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], err_code[%p]", account_id, mail_id, atch_id, err_code);
11624         EM_PROFILE_BEGIN(profile_emcore_save_create_dir);
11625         int ret = false;
11626         int error = EMAIL_ERROR_NONE;
11627         char buf[512];
11628         struct stat sbuf;
11629         char *prefix_path = NULL;
11630         char errno_buf[ERRNO_BUF_SIZE] = {0};
11631
11632         memset(buf, 0x00, sizeof(buf));
11633
11634     if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11635                 error = emcore_get_container_path(multi_user_name, &prefix_path);
11636                 if (error != EMAIL_ERROR_NONE) {
11637                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11638                         goto FINISH_OFF;
11639                 }
11640         } else {
11641                 prefix_path = strdup("");
11642         }
11643
11644         if (account_id >= FIRST_ACCOUNT_ID)  {
11645                 SNPRINTF(buf, sizeof(buf), "%s%s%s%s%d", prefix_path, 
11646                                                                                                 DIR_SEPERATOR, 
11647                                                                                                 MAILHOME, 
11648                                                                                                 DIR_SEPERATOR, 
11649                                                                                                 account_id);
11650
11651                 if (stat(buf, &sbuf) == 0) {
11652                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR)  {
11653                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11654                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11655                                 goto FINISH_OFF;
11656                         }
11657                 }
11658                 else  {
11659                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11660                                 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11661                                 EM_DEBUG_EXCEPTION("mkdir failed: %s", EM_STRERROR(errno_buf));
11662                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11663                                 if(errno == 28)
11664                                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11665                                 goto FINISH_OFF;
11666                         }
11667
11668                         if (account_id == EML_FOLDER) {
11669                                 chmod(buf, 0777);
11670                         }
11671                 }
11672         }
11673
11674         if (mail_id > 0)  {
11675                 int space_left_in_buffer = sizeof(buf) - EM_SAFE_STRLEN(buf);
11676
11677                 if (account_id < FIRST_ACCOUNT_ID)  {
11678                         EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11679                         error = EMAIL_ERROR_INVALID_PARAM;
11680                         goto FINISH_OFF;
11681                 }
11682
11683                 if (space_left_in_buffer + 10 > sizeof(buf)) {
11684                         EM_DEBUG_EXCEPTION("Buffer overflowed");
11685                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11686                         goto FINISH_OFF;
11687                 }
11688
11689                 SNPRINTF(buf+EM_SAFE_STRLEN(buf), space_left_in_buffer, "%s%d", DIR_SEPERATOR, mail_id);
11690
11691                 if (stat(buf, &sbuf) == 0) {
11692                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR)  {
11693                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11694                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11695                                 goto FINISH_OFF;
11696                         }
11697                 }
11698                 else  {
11699                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11700                                 EM_DEBUG_EXCEPTION("mkdir failed [%s]", buf);
11701                                 EM_DEBUG_EXCEPTION("mkdir failed [%d][%s]", errno, EM_STRERROR(errno_buf));
11702                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11703                                 if(errno == 28)
11704                                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11705                                 goto FINISH_OFF;
11706                         }
11707
11708                         if (account_id == EML_FOLDER) {
11709                                 chmod(buf, 0777);
11710                         }
11711                 }
11712         }
11713
11714         if (atch_id > 0)  {
11715                 if (account_id < FIRST_ACCOUNT_ID || mail_id <= 0)  {
11716                         EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11717
11718                         error = EMAIL_ERROR_INVALID_PARAM;
11719                         goto FINISH_OFF;
11720                 }
11721
11722                 SNPRINTF(buf+EM_SAFE_STRLEN(buf), sizeof(buf)-(EM_SAFE_STRLEN(buf)+1), "%s%d", DIR_SEPERATOR, atch_id);
11723
11724                 if (stat(buf, &sbuf) == 0) {
11725                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR)  {
11726                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11727
11728                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11729                                 goto FINISH_OFF;
11730                         }
11731                 }
11732                 else  {
11733                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11734                                 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11735                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11736                                 if(errno == 28)
11737                                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11738                                 goto FINISH_OFF;
11739                         }
11740
11741                         if (account_id == EML_FOLDER) {
11742                                 chmod(buf, 0777);
11743                         }
11744                 }
11745         }
11746
11747         ret = true;
11748
11749 FINISH_OFF:
11750
11751         EM_SAFE_FREE(prefix_path);
11752
11753         if (err_code != NULL)
11754                 *err_code = error;
11755
11756         EM_PROFILE_END(profile_emcore_save_create_dir);
11757         EM_DEBUG_FUNC_END("ret [%d]", ret);
11758         return ret;
11759 }
11760
11761 INTERNAL_FUNC int emstorage_copy_file(char *src_file, char *dst_file, int sync_status, int *err_code)
11762 {
11763         EM_DEBUG_FUNC_BEGIN("src_file[%s], dst_file[%s], err_code[%p]", src_file, dst_file, err_code);
11764         EM_DEBUG_LOG("Using the fsync function");
11765         int ret = false;
11766         int error = EMAIL_ERROR_NONE;
11767         struct stat st_buf;
11768
11769         int fp_src = 0;
11770         int fp_dst = 0;
11771         int nread = 0;
11772         char buf[FILE_MAX_BUFFER_SIZE] = {0};
11773         char errno_buf[ERRNO_BUF_SIZE] = {0};
11774
11775         if (!src_file || !dst_file)  {
11776                 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
11777
11778                 error = EMAIL_ERROR_INVALID_PARAM;
11779                 goto FINISH_OFF;
11780         }
11781
11782         if (stat(src_file, &st_buf) < 0) {
11783                 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", src_file);
11784
11785                 error = EMAIL_ERROR_SYSTEM_FAILURE;             /* EMAIL_ERROR_INVALID_PATH; */
11786                 goto FINISH_OFF;
11787         }
11788
11789         error = em_open(src_file, O_RDONLY, 0, &fp_src);
11790         if (error != EMAIL_ERROR_NONE) {
11791                         EM_DEBUG_EXCEPTION(">>>> Source Fail em_open %s Failed: %d", src_file, error);
11792                         goto FINISH_OFF;
11793         }
11794
11795         error = em_open(dst_file, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, &fp_dst); /*prevent 24474*/
11796         if (error != EMAIL_ERROR_NONE) {
11797                         EM_DEBUG_EXCEPTION(">>>> Destination Fail em_open %s:  %d", dst_file, error);
11798                         goto FINISH_OFF;
11799         }
11800
11801         memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
11802
11803         while ((nread = read(fp_src, buf, FILE_MAX_BUFFER_SIZE)) > 0) {
11804                 if (nread > 0 && nread <= FILE_MAX_BUFFER_SIZE)  {
11805                         EM_DEBUG_LOG("Nread Value [%d]", nread);
11806                         char *buf_ptr;
11807                         ssize_t byte_written = 0;
11808                         size_t remain_byte = nread;
11809                         buf_ptr = buf;
11810                         errno = 0;
11811
11812                         while(remain_byte > 0 && buf_ptr && errno == 0) {
11813                                 byte_written = write(fp_dst, buf_ptr, remain_byte);
11814
11815                                 if (byte_written < 0) {
11816                                         /* interrupted by a signal */
11817                                         if (errno == EINTR) {
11818                                                 errno = 0;
11819                                                 continue;
11820                                         }
11821
11822                                         EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
11823                                         error = EMAIL_ERROR_UNKNOWN;
11824                                         goto FINISH_OFF;
11825                                 }
11826                                 EM_DEBUG_LOG("NWRITTEN [%d]", byte_written);
11827                                 remain_byte -= byte_written;
11828                                 buf_ptr += byte_written;
11829                         }
11830                 }
11831
11832                 memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
11833         }
11834
11835         ret = true;
11836
11837 FINISH_OFF:
11838         EM_SAFE_CLOSE (fp_src);
11839
11840         if (fp_dst >=0) { /*prevent 24474*/
11841                 if (sync_status) {
11842                         EM_DEBUG_LOG("Before fsync");
11843                         fsync(fp_dst);
11844                 }
11845                 close (fp_dst);
11846         }
11847
11848         if (nread < 0 || error == EMAIL_ERROR_UNKNOWN)
11849                 remove(dst_file);
11850
11851         if (err_code != NULL)
11852                 *err_code = error;
11853         EM_DEBUG_FUNC_END("ret [%d]", ret);
11854         return ret;
11855 }
11856 /* create Directory if user has deleted [deepam.p@samsung.com] */
11857 INTERNAL_FUNC void emstorage_create_dir_if_delete()
11858 {
11859         EM_DEBUG_FUNC_BEGIN();
11860
11861         mkdir(DATA_PATH, DIRECTORY_PERMISSION);
11862         mkdir(EMAILPATH, DIRECTORY_PERMISSION);
11863         mkdir(MAILHOME, DIRECTORY_PERMISSION);
11864         mkdir(MAILTEMP, DIRECTORY_PERMISSION);
11865         chmod(MAILTEMP, 0777);
11866
11867         EM_DEBUG_FUNC_END();
11868 }
11869
11870 static int _get_temp_file_name(char **filename, int *err_code)
11871 {
11872         EM_DEBUG_FUNC_BEGIN("filename[%p], err_code[%p]", filename, err_code);
11873
11874         int ret = false;
11875         int error = EMAIL_ERROR_NONE;
11876
11877         if (filename == NULL) {
11878                 EM_DEBUG_EXCEPTION(" filename[%p]", filename);
11879                 error = EMAIL_ERROR_INVALID_PARAM;
11880                 goto FINISH_OFF;
11881         }
11882
11883         char tempname[512] = {0x00, };
11884         struct timeval tv;
11885
11886         gettimeofday(&tv, NULL);
11887         srand(tv.tv_usec);
11888
11889         SNPRINTF(tempname, sizeof(tempname), "%s%c%d", MAILTEMP, '/', rand());
11890
11891         char *p = EM_SAFE_STRDUP(tempname);
11892         if (p == NULL)  {
11893                 EM_DEBUG_EXCEPTION(" strdup failed...");
11894                 error = EMAIL_ERROR_OUT_OF_MEMORY;
11895                 goto FINISH_OFF;
11896         }
11897
11898         *filename = p;
11899
11900         ret = true;
11901
11902 FINISH_OFF:
11903         if (err_code != NULL)
11904                 *err_code = error;
11905
11906         EM_DEBUG_FUNC_END("ret [%d]", ret);
11907         return ret;
11908 }
11909
11910 INTERNAL_FUNC int emstorage_add_content_type(char *file_path, char *char_set, int *err_code)
11911 {
11912         EM_DEBUG_FUNC_BEGIN("File path [ %p ]  Character Set [ %p ] err_code [ %p]", file_path, char_set, err_code);
11913
11914         EM_IF_NULL_RETURN_VALUE(file_path, false);
11915         EM_IF_NULL_RETURN_VALUE(char_set, false);
11916         EM_IF_NULL_RETURN_VALUE(err_code, false);
11917
11918         char *buf =  NULL;
11919         char *buf1 = NULL;
11920         struct stat st_buf;
11921         int buf_size = 0;
11922         char *low_char_set = NULL;
11923         char *match_str = NULL;
11924         int nwritten = 0;
11925         int ret = false;
11926         int error = EMAIL_ERROR_NONE;
11927         int data_count_to_written = 0;
11928         char *temp_file_name = NULL;
11929         int err = 0;
11930         FILE* fp_src = NULL;
11931         FILE* fp_dest = NULL;
11932         int nread = 0;
11933
11934
11935         if (stat(file_path, &st_buf) < 0) {
11936                 EM_DEBUG_EXCEPTION_SEC(" stat(\"%s\") failed...", file_path);
11937                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11938                 goto FINISH_OFF;
11939         }
11940
11941         buf_size =  st_buf.st_size;
11942         EM_DEBUG_LOG(">>>> File Size [ %d ] ", buf_size);
11943         buf = (char *)calloc(1, buf_size+1);
11944
11945         if (!buf) {
11946                 EM_DEBUG_LOG(">>> Memory cannot be allocated ");
11947                 goto FINISH_OFF;
11948         }
11949
11950         error = em_fopen(file_path, "rb", &fp_src);
11951         if (error != EMAIL_ERROR_NONE || fp_src == NULL) {
11952                 EM_DEBUG_EXCEPTION_SEC(" file_path fopen failed - %s [%d]", file_path, error);
11953                 goto FINISH_OFF;
11954         }
11955
11956         if ((nread = fread(buf, 1, buf_size, fp_src)) > 0) {
11957                 if (nread > 0 && nread <= buf_size)  {
11958                         EM_DEBUG_LOG(">>>> Nread Value [ %d ] ", nread);
11959
11960                         /**
11961                           *   1.Add check for whether content type is there.
11962                           *   2. If not based on the character set, Append it in File
11963                           **/
11964
11965                         low_char_set = calloc(1, EM_SAFE_STRLEN(char_set) + strlen(" \" /></head>") +1); /*prevent 34359*/
11966                         if (low_char_set == NULL) {
11967                                 EM_DEBUG_EXCEPTION("calloc failed");
11968                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
11969                                 goto FINISH_OFF;
11970                         }
11971
11972                         strncat(low_char_set, char_set, EM_SAFE_STRLEN(char_set));
11973                         EM_DEBUG_LOG(">>>> CHAR SET [ %s ] ", low_char_set);
11974                         strncat(low_char_set, " \" /></head>", strlen(" \" /></head>")); /*prevent 34359*/
11975                         EM_DEBUG_LOG(">>> CHARSET [ %s ] ", low_char_set);
11976                         match_str = strstr(buf, CONTENT_TYPE_DATA);
11977
11978                         if (match_str == NULL) {
11979                                 EM_DEBUG_LOG(">>>>emstorage_add_content_type 3 ");
11980                                 if (fp_src !=NULL) {
11981                                         fclose(fp_src);fp_src = NULL;
11982                                 }
11983                                 data_count_to_written = EM_SAFE_STRLEN(low_char_set)+strlen(CONTENT_DATA)+1; /*prevent 34359*/
11984                                 buf1 = (char *)calloc(1, data_count_to_written);
11985
11986                                 if (buf1) {
11987                                         strncat(buf1, CONTENT_DATA, strlen(CONTENT_DATA)); /*prevent 34359*/
11988                                         EM_DEBUG_LOG(">>>>> BUF 1 [ %s ] ", buf1);
11989                                         strncat(buf1, low_char_set, EM_SAFE_STRLEN(low_char_set));
11990                                         EM_DEBUG_LOG(">>>> HTML TAG DATA  [ %s ] ", buf1);
11991
11992                                         /* 1. Create a temporary file name */
11993                                         if (!_get_temp_file_name(&temp_file_name, &err)) {
11994                                                         EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed - %d", err);
11995                                                         if (err_code != NULL) *err_code = err;
11996                                                         goto FINISH_OFF;
11997                                         }
11998                                         EM_DEBUG_LOG_SEC(">>>>>>> TEMP APPEND FILE PATH [ %s ] ", temp_file_name);
11999
12000                                         /* Open the Temp file in Append mode */
12001                                         error = em_fopen(temp_file_name, "ab", &fp_dest);
12002                                         if (error != EMAIL_ERROR_NONE) {
12003                                                 EM_DEBUG_EXCEPTION_SEC(" fopen failed - %s [%d]", temp_file_name, error);
12004                                                 goto FINISH_OFF;
12005                                         }
12006
12007                                         /* 2. write the Latest data */
12008                                         nwritten = fwrite(buf1, data_count_to_written-1, 1, fp_dest);
12009
12010                                         if (nwritten > 0) {
12011                                                 EM_DEBUG_LOG(" Latest Data  : [%d ] bytes written ", nwritten);
12012                                                 nwritten = 0;
12013                                                 /* 3. Append old data */
12014                                                 nwritten = fwrite(buf, nread-1, 1, fp_dest);
12015
12016                                                 if (nwritten <= 0) {
12017                                                         EM_DEBUG_EXCEPTION(" Error Occured while writing Old data : [%d ] bytes written ", nwritten);
12018                                                         error = EMAIL_ERROR_SYSTEM_FAILURE;
12019                                                         goto FINISH_OFF;
12020                                                 }
12021                                                 else {
12022                                                         EM_DEBUG_LOG(">>>> OLD data appended [ %d ] ", nwritten);
12023
12024                                                         if (!emstorage_move_file(temp_file_name, file_path, false, &err)) {
12025                                                                 EM_DEBUG_EXCEPTION(" emstorage_move_file failed - %d", err);
12026                                                                 goto FINISH_OFF;
12027                                                         }
12028                                                 }
12029
12030                                         }
12031                                         else {
12032                                                 EM_DEBUG_EXCEPTION(" Error Occured while writing New data : [%d ] bytes written ", nwritten);
12033                                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12034                                                 goto FINISH_OFF;
12035                                         }
12036                                 }
12037                         }
12038                 }
12039         }
12040
12041         ret = true;
12042 FINISH_OFF:
12043
12044         EM_SAFE_FREE(buf);
12045         EM_SAFE_FREE(buf1);
12046         EM_SAFE_FREE(low_char_set);
12047         EM_SAFE_FREE(temp_file_name);
12048
12049         if (fp_src != NULL) {
12050                 fclose(fp_src);
12051                 fp_src = NULL;
12052         }
12053
12054         if (fp_dest != NULL) {
12055                 fclose(fp_dest);
12056                 fp_dest = NULL;
12057         }
12058         if (err_code)
12059                 *err_code = error;
12060         EM_DEBUG_FUNC_END("ret [%d]", ret);
12061         return ret;
12062
12063 }
12064
12065 INTERNAL_FUNC int emstorage_move_file(char *src_file, char *dst_file, int sync_status, int *err_code)
12066 {
12067         EM_DEBUG_FUNC_BEGIN("src_file[%p], dst_file[%p], err_code[%p]", src_file, dst_file, err_code);
12068
12069         int ret = false;
12070         int error = EMAIL_ERROR_NONE;
12071         char errno_buf[ERRNO_BUF_SIZE] = {0};
12072
12073         if (src_file == NULL || dst_file == NULL)  {
12074                 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
12075                 error = EMAIL_ERROR_INVALID_PARAM;
12076                 goto FINISH_OFF;
12077         }
12078
12079         EM_DEBUG_LOG_SEC("src_file[%s], dst_file[%s]", src_file, dst_file);
12080
12081         if (strcmp(src_file, dst_file) != 0) {
12082                 if (rename(src_file, dst_file) != 0) {
12083                         /* EM_DEBUG_EXCEPTION("%s", strerror(errno) ); */
12084                         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() */
12085                                 /*  does not work across different mount points, even if the same file system is mounted on both.)       */
12086                                 EM_DEBUG_LOG("oldpath and newpath are not on the same mounted file system.");
12087                                 if (!emstorage_copy_file(src_file, dst_file, sync_status, &error)) {
12088                                         EM_DEBUG_EXCEPTION("emstorage_copy_file failed - %d", error);
12089                                         goto FINISH_OFF;
12090                                 }
12091                                 remove(src_file);
12092                                 EM_DEBUG_LOG("src[%s] removed", src_file);
12093
12094                         }
12095                         else  {
12096                                 if (errno == ENOENT)  {
12097                                         struct stat temp_file_stat;
12098                                         if (stat(src_file, &temp_file_stat) < 0) {
12099                                                 EM_DEBUG_EXCEPTION("no src file found [%s] : %s", src_file, EM_STRERROR(errno_buf));
12100                                         }
12101                                         if (stat(dst_file, &temp_file_stat) < 0)
12102                                                 EM_DEBUG_EXCEPTION("no dst file found [%s] : %s", dst_file, EM_STRERROR(errno_buf));
12103
12104                                         error = EMAIL_ERROR_FILE_NOT_FOUND;
12105                                         goto FINISH_OFF;
12106
12107                                 }
12108                                 else  {
12109                                         EM_DEBUG_EXCEPTION("rename failed: %s", EM_STRERROR(errno_buf));
12110                                         error = EMAIL_ERROR_SYSTEM_FAILURE;
12111                                         goto FINISH_OFF;
12112                                 }
12113                         }
12114                 }
12115         }
12116         else {
12117                 EM_DEBUG_LOG("src[%s] = dst[%s]", src_file, dst_file);
12118         }
12119
12120         ret = true;
12121
12122 FINISH_OFF:
12123         if (err_code != NULL)
12124                 *err_code = error;
12125
12126         EM_DEBUG_FUNC_END("ret [%d]", ret);
12127         return ret;
12128 }
12129
12130 INTERNAL_FUNC int emstorage_delete_file(char *src_file, int *err_code)
12131 {
12132         EM_DEBUG_FUNC_BEGIN("src_file[%p], err_code[%p]", src_file, err_code);
12133
12134         int ret = false;
12135         int error = EMAIL_ERROR_NONE;
12136
12137         if (src_file == NULL) {
12138                 EM_DEBUG_EXCEPTION(" src_file[%p]", src_file);
12139
12140                 error = EMAIL_ERROR_INVALID_PARAM;
12141                 goto FINISH_OFF;
12142         }
12143
12144         if (remove(src_file) != 0) {
12145                 if (errno != ENOENT) {
12146                         EM_DEBUG_EXCEPTION(" remove failed - %d", errno);
12147
12148                         error = EMAIL_ERROR_SYSTEM_FAILURE;
12149                         goto FINISH_OFF;
12150                 }
12151                 else {
12152                         EM_DEBUG_EXCEPTION(" no file found...");
12153
12154                         error = EMAIL_ERROR_FILE_NOT_FOUND;
12155                 }
12156         }
12157
12158         ret = true;
12159
12160 FINISH_OFF:
12161         if (err_code != NULL)
12162                 *err_code = error;
12163
12164         EM_DEBUG_FUNC_END("ret [%d]", ret);
12165         return ret;
12166 }
12167
12168
12169 INTERNAL_FUNC int emstorage_delete_dir(char *src_dir, int *err_code)
12170 {
12171         EM_DEBUG_FUNC_BEGIN("src_dir[%p], err_code[%p]", src_dir, err_code);
12172
12173         if (src_dir == NULL) {
12174                 EM_DEBUG_EXCEPTION("src_dir[%p]", src_dir);
12175
12176                 if (err_code != NULL)
12177                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12178                 return false;
12179         }
12180
12181         int error = EMAIL_ERROR_NONE;
12182
12183         DIR *dirp;
12184         struct dirent *dp;
12185         struct stat sbuf;
12186         char buf[512];
12187
12188         dirp = opendir(src_dir);
12189
12190         if (dirp == NULL)  {
12191                 if (errno == ENOENT)  {
12192                         EM_DEBUG_EXCEPTION("directory[%s] does not exist...", src_dir);
12193                         if (err_code != NULL)
12194                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12195                         return true;
12196                 }
12197                 else  {
12198                         EM_DEBUG_EXCEPTION("opendir failed [%s] [%d]", src_dir, errno);
12199                         if (err_code != NULL)
12200                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12201                         return false;
12202                 }
12203         }
12204
12205         while ((dp=readdir(dirp)))  {
12206                 if (strncmp(dp->d_name, ".", 1) == 0 || strncmp(dp->d_name, "..", 2) == 0) /* prevent 34360 */
12207                         continue;
12208
12209                 SNPRINTF(buf, sizeof(buf), "%s/%s", src_dir, dp->d_name);
12210
12211                 if (lstat(buf, &sbuf) == 0 || stat(buf, &sbuf) == 0) {
12212                         /*  check directory */
12213                         if ((sbuf.st_mode & S_IFMT) == S_IFDIR)  {      /*  directory */
12214                                 /*  recursive call */
12215                                 if (!emstorage_delete_dir(buf, &error)) {
12216                                         closedir(dirp);
12217                                         if (err_code != NULL)
12218                                                 *err_code = error;
12219                                         return false;
12220                                 }
12221                         }
12222                         else  { /*  file */
12223                                 if (remove(buf) < 0)  {
12224                                         EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", buf, errno);
12225                                         closedir(dirp);
12226                                         if (err_code != NULL)
12227                                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12228                                         return false;
12229                                 }
12230                         }
12231                 }
12232                 else
12233                         EM_DEBUG_EXCEPTION("content does not exist...");
12234         }
12235
12236         closedir(dirp);
12237
12238         EM_DEBUG_LOG_DEV ("remove direcotory [%s]", src_dir);
12239
12240         /* EM_DEBUG_FUNC_BEGIN(); */
12241
12242         if (remove(src_dir) < 0)  {
12243                 EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", src_dir, errno);
12244                 if (err_code != NULL)
12245                         *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12246                 return false;
12247         }
12248
12249         if (err_code != NULL)
12250                 *err_code = error;
12251
12252         return true;
12253 }
12254
12255 /* faizan.h@samsung.com */
12256 INTERNAL_FUNC int emstorage_update_server_uid(char *multi_user_name, char *old_server_uid, char *new_server_uid, int *err_code)
12257 {
12258         EM_DEBUG_FUNC_BEGIN("new_server_uid[%s], old_server_uid[%s]", new_server_uid, old_server_uid);
12259         int ret = false;
12260         int error = EMAIL_ERROR_NONE;
12261         char sql_query_string[QUERY_SIZE] = {0, };
12262         int transaction = true;
12263
12264         if (!old_server_uid || !new_server_uid) {
12265                 EM_DEBUG_EXCEPTION("Invalid parameters");
12266                 if (err_code)
12267                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12268                 return false;
12269         }
12270
12271         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12272
12273         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12274
12275         SNPRINTF(sql_query_string, sizeof(sql_query_string),
12276                  "UPDATE mail_tbl SET server_mail_id=\'%s\' WHERE server_mail_id=%s ", new_server_uid, old_server_uid);
12277
12278         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12279         if (error != EMAIL_ERROR_NONE) {
12280                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12281                         goto FINISH_OFF;
12282         }
12283
12284         ret = true;
12285
12286 FINISH_OFF:
12287         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12288
12289         if (err_code != NULL)
12290                 *err_code = error;
12291
12292         EM_DEBUG_FUNC_END("ret [%d]", ret);
12293         return ret;
12294
12295 }
12296
12297 INTERNAL_FUNC int emstorage_update_read_mail_uid(char *multi_user_name, int mail_id, char *new_server_uid, char *mbox_name, int *err_code)
12298 {
12299         EM_DEBUG_FUNC_BEGIN_SEC("mail_id[%d], new_server_uid[%s], mbox_name[%s]", mail_id, new_server_uid, mbox_name);
12300
12301         int ret = false;
12302         int error = EMAIL_ERROR_NONE;
12303         char sql_query_string[QUERY_SIZE] = {0, };
12304         int transaction = true;
12305
12306         if (!mail_id || !new_server_uid || !mbox_name)  {
12307                 EM_DEBUG_EXCEPTION("Invalid parameters");
12308                 if (err_code != NULL)
12309                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12310                 return false;
12311         }
12312
12313         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12314         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12315
12316
12317         SNPRINTF(sql_query_string, sizeof(sql_query_string),
12318                  "UPDATE mail_read_mail_uid_tbl SET server_uid=\'%s\', mailbox_id=\'%s\', mailbox_name=\'%s\' WHERE local_uid=%d ", new_server_uid, mbox_name, mbox_name, mail_id);
12319
12320         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12321         if (error != EMAIL_ERROR_NONE) {
12322                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12323                         goto FINISH_OFF;
12324         }
12325
12326         ret     = true;
12327
12328 FINISH_OFF:
12329
12330         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12331
12332         if (err_code != NULL)
12333                 *err_code = error;
12334
12335         EM_DEBUG_FUNC_END("ret [%d]", ret);
12336         return ret;
12337
12338 }
12339
12340 INTERNAL_FUNC int emstorage_update_save_status(char *multi_user_name, int account_id, int *err_code)
12341 {
12342         EM_DEBUG_FUNC_BEGIN();
12343
12344         int ret = false;
12345         int transaction = true;
12346         int error = EMAIL_ERROR_NONE;
12347         char sql_query_string[QUERY_SIZE] = {0,};
12348
12349         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12350
12351         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12352
12353         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12354
12355         if (account_id <= ALL_ACCOUNT)
12356                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET save_status = %d WHERE (save_status = %d or save_status = %d)", EMAIL_MAIL_STATUS_NONE, EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12357         else
12358                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET save_status = %d WHERE (save_status = %d or save_status = %d) and account_id = %d ", EMAIL_MAIL_STATUS_NONE, EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED, account_id);
12359
12360         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12361         if (error != EMAIL_ERROR_NONE) {
12362                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12363                         goto FINISH_OFF;
12364         }
12365
12366         ret = true;
12367
12368 FINISH_OFF:
12369         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12370
12371         if (err_code != NULL)
12372                 *err_code = error;
12373
12374         EM_DEBUG_FUNC_END("ret [%d]", ret);
12375         return ret;
12376
12377 }
12378
12379 int emstorage_get_unread_mailid(char *multi_user_name, int account_id, int vip_mode, int **mail_ids, int *mail_number, int *err_code)
12380 {
12381         EM_DEBUG_FUNC_BEGIN();
12382
12383         if ((!mail_ids) ||(account_id <= 0 &&  account_id != -1)) {
12384                 EM_DEBUG_EXCEPTION(" mail_id[%p], account_id[%d] ", mail_ids, account_id);
12385                 if (err_code != NULL)
12386                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12387                 return false;
12388         }
12389
12390         int ret = false;
12391         int rc = -1;
12392         int error = EMAIL_ERROR_NONE;
12393         int count = 0;
12394         int i = 0;
12395         int col_index = 0;
12396         int *p_mail_ids = NULL;
12397         int transaction = false;
12398         char **result = NULL;
12399         char sql_query_string[QUERY_SIZE] = {0, };
12400         char temp_query_string[QUERY_SIZE] = {0,};
12401         char sql_select_query_string[QUERY_SIZE] = {0, };
12402
12403         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12404         EMSTORAGE_START_READ_TRANSACTION(transaction);
12405
12406         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12407
12408         if (account_id == -1)
12409                 SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string), "SELECT mail_id FROM mail_tbl WHERE flags_seen_field = 0 AND (save_status = %d or save_status = %d)", EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12410         else
12411                 SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string), "SELECT mail_id FROM mail_tbl WHERE account_id = %d AND flags_seen_field = 0 AND (save_status = %d or save_status = %d)", account_id, EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12412
12413         if (vip_mode) {
12414                 SNPRINTF(temp_query_string, sizeof(temp_query_string), "%s AND tag_id < 0", sql_select_query_string);
12415         } else {
12416                 SNPRINTF(temp_query_string, sizeof(temp_query_string), "%s", sql_select_query_string);
12417         }
12418
12419         SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s ORDER BY date_time ASC", temp_query_string);
12420
12421         EM_DEBUG_LOG_SEC("query: [%s]", sql_query_string);
12422
12423         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
12424         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12425                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12426
12427         EM_DEBUG_LOG("Count : %d", count);
12428
12429         if (count == 0)  {
12430                 EM_DEBUG_EXCEPTION("no Mails found...");
12431                 ret = false;
12432                 error= EMAIL_ERROR_MAIL_NOT_FOUND;
12433                 goto FINISH_OFF;
12434         }
12435
12436         p_mail_ids = em_malloc(count * sizeof(int));
12437         if (p_mail_ids == NULL) {
12438                 EM_DEBUG_EXCEPTION("em_malloc failed...");
12439                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12440                 goto FINISH_OFF;
12441         }
12442
12443         col_index = 1;
12444
12445         for (i = 0; i < count; i++) {
12446                 _get_table_field_data_int(result, &(p_mail_ids[i]), col_index++);
12447         }
12448
12449         ret = true;
12450
12451 FINISH_OFF:
12452
12453         if (result)
12454                 sqlite3_free_table(result);
12455
12456         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12457
12458 //      sqlite3_db_release_memory(local_db_handle);
12459
12460         if (ret == true) {
12461                 if (mail_ids != NULL)
12462                         *mail_ids = p_mail_ids;
12463
12464                 if (mail_number != NULL)
12465                         *mail_number = count;
12466         } else {
12467                 EM_SAFE_FREE(p_mail_ids);
12468         }
12469
12470         if (err_code != NULL)
12471                 *err_code = error;
12472
12473         EM_DEBUG_FUNC_END("ret [%d]", ret);
12474         return ret;
12475 }
12476
12477 int setting_system_command(const char *command)
12478
12479  {
12480         int pid = 0, status = 0;
12481         char *const environ[] = { NULL };
12482
12483         if (command == 0)
12484                 return 1;
12485
12486         pid = fork();
12487
12488         if (pid == -1)
12489                 return -1;
12490
12491         if (pid == 0) {
12492                 char *argv[4];
12493
12494                 argv[0] = "sh";
12495                 argv[1] = "-c";
12496                 argv[2] = (char *)command;
12497                 argv[3] = 0;
12498
12499                 execve("/bin/sh", argv, environ);
12500                 abort();
12501         }
12502         do{
12503                 if (waitpid(pid, &status, 0) == -1) {
12504                         if (errno != EINTR)
12505                                 return -1;
12506                 }
12507                 else {
12508                         return status;
12509                 }
12510         } while (1);
12511
12512         return 0;
12513 }
12514
12515
12516 INTERNAL_FUNC int emstorage_mail_get_total_diskspace_usage(unsigned long *total_usage,  int transaction, int *err_code)
12517 {
12518         EM_DEBUG_FUNC_BEGIN("total_usage[%p],  transaction[%d], err_code[%p]", total_usage, transaction, err_code);
12519
12520         if (!total_usage) {
12521                 EM_DEBUG_EXCEPTION("total_usage[%p]", total_usage);
12522
12523                 if (err_code != NULL)
12524                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12525                 return false;
12526         }
12527
12528         int   ret = false;
12529         int   error = EMAIL_ERROR_NONE;
12530         char  syscmd[256] = {0, };
12531         char  line[256] = {0, };
12532         char *line_from_file = NULL;
12533         FILE *fp = NULL;
12534         unsigned long total_diskusage = 0;
12535
12536         SNPRINTF(syscmd, sizeof(syscmd), "touch %s", SETTING_MEMORY_TEMP_FILE_PATH);
12537         if (setting_system_command(syscmd) == -1) {
12538                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
12539
12540                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12541                 goto FINISH_OFF;
12542         }
12543
12544         SNPRINTF(syscmd, sizeof(syscmd), "du -hsk %s > %s", EMAILPATH, SETTING_MEMORY_TEMP_FILE_PATH);
12545         EM_DEBUG_LOG(" cmd : %s", syscmd);
12546         if (setting_system_command(syscmd) == -1) {
12547                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : Setting > Memory] System Command [%s] is failed", syscmd);
12548
12549                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12550                 goto FINISH_OFF;
12551         }
12552
12553         error = em_fopen(SETTING_MEMORY_TEMP_FILE_PATH, "r", &fp);
12554         if (error != EMAIL_ERROR_NONE) {
12555                 perror(SETTING_MEMORY_TEMP_FILE_PATH);
12556                 goto FINISH_OFF;
12557         }
12558
12559         line_from_file = fgets(line, sizeof(line), fp);
12560
12561         if(line_from_file == NULL) {
12562                 EM_DEBUG_EXCEPTION("fgets failed");
12563                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12564                 goto FINISH_OFF;
12565         }
12566         total_diskusage = strtoul(line, NULL, 10);
12567
12568         memset(syscmd, 0, sizeof(syscmd));
12569         SNPRINTF(syscmd, sizeof(syscmd), "rm -f %s", SETTING_MEMORY_TEMP_FILE_PATH);
12570         if (setting_system_command(syscmd) == -1) {
12571                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage :  [Setting > Memory] System Command [%s] is failed", syscmd);
12572                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12573                 goto FINISH_OFF;
12574         }
12575
12576         EM_DEBUG_LOG("[Setting > Memory] @@@@@ Size of Directory [%s] is %ld KB", EMAILPATH, total_diskusage);
12577
12578         ret = true;
12579
12580 FINISH_OFF:
12581         if (err_code != NULL)
12582                 *err_code = error;
12583
12584         if (ret)
12585                 *total_usage = total_diskusage;
12586         else
12587                 *total_usage = 0;
12588
12589         if(fp) fclose(fp); /* prevent 32730 */
12590
12591         EM_DEBUG_FUNC_END("ret [%d]", ret);
12592         return ret;
12593 }
12594 #define MAILHOME_UTF8    tzplatform_mkpath(TZ_USER_DATA,"email/.email_data/7/348/UTF-8")
12595
12596 INTERNAL_FUNC int emstorage_test(char *multi_user_name, int mail_id, int account_id, char *full_address_to, char *full_address_cc, char *full_address_bcc, int *err_code)
12597 {
12598         DB_STMT hStmt = NULL;
12599         int ret = false;
12600         int error = EMAIL_ERROR_NONE;
12601         int rc = 0;
12602         char sql_query_string[QUERY_SIZE] = {0, };
12603
12604         SNPRINTF(sql_query_string, sizeof(sql_query_string),
12605                 "INSERT INTO mail_tbl VALUES "
12606                 "( ?" /*  mail_id */
12607                 ", ?" /*  account_id */
12608                 ", ?" /*  mail_size */
12609                 ", ?" /*  server_mail_status */
12610                 ", ?" /*  server_mailbox_name */
12611                 ", ?" /*  server_mail_id */
12612                 ", ?" /*  reference_mail_id */
12613                 ", ?" /*  full_address_from */
12614                 ", ?" /*  full_address_reply */
12615                 ", ?" /*  full_address_to */
12616                 ", ?" /*  full_address_cc */
12617                 ", ?" /*  full_address_bcc */
12618                 ", ?" /*  full_address_return */
12619                 ", ?" /*  subject */
12620                 ", ?" /*  body_download_status */
12621                 ", ?" /*  file_path_plain */
12622                 ", ?" /*  file_path_html */
12623                 ", ?" /*  date_time */
12624                 ", ?" /*  flags_seen_field */
12625                 ", ?" /*  flags_deleted_field */
12626                 ", ?" /*  flags_flagged_field */
12627                 ", ?" /*  flags_answered_field */
12628                 ", ?" /*  flags_recent_field */
12629                 ", ?" /*  flags_draft_field */
12630                 ", ?" /*  flags_forwarded_field */
12631                 ", ?" /*  DRM_status */
12632                 ", ?" /*  priority */
12633                 ", ?" /*  save_status */
12634                 ", ?" /*  lock_status */
12635                 ", ?" /*  message_id */
12636                 ", ?" /*  report_status */
12637                 ", ?" /*  email_address_sender */
12638                 ", ?" /*  email_address_recipient */
12639                 ", ?" /*  attachment_count */
12640                 ", ?" /*  inline_content_count */
12641                 ", ?" /*  preview_text */
12642                 ", ?" /*  thread_id */
12643                 ", ?" /*  mailbox_type */
12644                 ", ?" /*  alias_sender */
12645                 ", ?" /*  alias_recipient */
12646                 ", ?" /*  thread_item_count */
12647                 ", ?" /*  meeting_request_status */
12648                 ", ?" /*  message_class */
12649                 ", ?" /*  digest_type */
12650                 ", ?" /*  smime_type */
12651                 ", ?" /*  scheduled_sending_time */
12652                 ", ?" /*  remaining_resend_times */
12653                 ", ?" /*  tag_id   */
12654                 ", ?" /*  replied_time */
12655                 ", ?" /*  forwarded_time */
12656                 ", ?" /*  default_charset */
12657                 ", ?" /*  eas_data_length */
12658                 ", ?" /*  eas_data */
12659                 ")");
12660
12661         int transaction = true;
12662         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12663
12664         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12665
12666
12667         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12668         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12669                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12670
12671         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_id);
12672         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, account_id);
12673         _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, EMAIL_MAILBOX_TYPE_OUTBOX);
12674         _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, "save test - long", 1, SUBJECT_LEN_IN_MAIL_TBL);
12675         _bind_stmt_field_data_string(hStmt, DATETIME_IDX_IN_MAIL_TBL, "20100316052908", 0, DATETIME_LEN_IN_MAIL_TBL);
12676         _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, 0);
12677         _bind_stmt_field_data_string(hStmt, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL, "", 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
12678         _bind_stmt_field_data_string(hStmt, SERVER_MAIL_ID_IDX_IN_MAIL_TBL, "", 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
12679         _bind_stmt_field_data_string(hStmt, MESSAGE_ID_IDX_IN_MAIL_TBL, "", 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
12680         _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, 0);
12681         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL, "<test08@streaming.s3glab.net>", 1, FROM_LEN_IN_MAIL_TBL);
12682         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL, "", 1, REPLY_TO_LEN_IN_MAIL_TBL);
12683         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL, full_address_to, 1, TO_LEN_IN_MAIL_TBL);
12684         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL, full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
12685         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL, full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
12686         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL, "", 1, RETURN_PATH_LEN_IN_MAIL_TBL);
12687         _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);
12688         _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);
12689         _bind_stmt_field_data_string(hStmt, ALIAS_SENDER_IDX_IN_MAIL_TBL, "send_alias", 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12690         _bind_stmt_field_data_string(hStmt, ALIAS_RECIPIENT_IDX_IN_MAIL_TBL, "recipient_alias", 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12691         _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, 1);
12692         _bind_stmt_field_data_string(hStmt, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL, MAILHOME_UTF8, 0, TEXT_1_LEN_IN_MAIL_TBL);
12693         _bind_stmt_field_data_string(hStmt, FILE_PATH_HTML_IDX_IN_MAIL_TBL, "", 0, TEXT_2_LEN_IN_MAIL_TBL);
12694         _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, 4);
12695         _bind_stmt_field_data_char(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, 0);
12696         _bind_stmt_field_data_char(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, 0);
12697         _bind_stmt_field_data_char(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, 0);
12698         _bind_stmt_field_data_char(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, 0);
12699         _bind_stmt_field_data_char(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, 0);
12700         _bind_stmt_field_data_char(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, 0);
12701         _bind_stmt_field_data_char(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, 0);
12702         _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, 0);
12703         _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, 0);
12704         _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, 0);
12705         _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, 0);
12706         _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, 0);
12707         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12708         _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, 0);
12709         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12710         _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, 0);
12711         _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, 0);
12712         _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, "preview body", 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
12713         _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, 0);
12714         _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, 0);
12715         _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, 0);
12716         _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, 0);
12717         _bind_stmt_field_data_int(hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, 0);
12718         _bind_stmt_field_data_int(hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, 0);
12719         _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, 0);
12720         _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, 0);
12721         _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, 0);
12722         _bind_stmt_field_data_string(hStmt, DEFAULT_CHARSET_IDX_IN_MAIL_TBL, "UTF-8", 0, TEXT_2_LEN_IN_MAIL_TBL);
12723         _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, 0);
12724         _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, NULL, 0);
12725
12726         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12727         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
12728                 ("sqlite3_step fail:%d", rc));
12729         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
12730                 ("sqlite3_step fail:%d", rc));
12731         ret = true;
12732
12733 FINISH_OFF:
12734         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12735         if (hStmt != NULL)  {
12736                 rc = sqlite3_finalize(hStmt);
12737                 if (rc != SQLITE_OK)  {
12738                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
12739                         error = EMAIL_ERROR_DB_FAILURE;
12740                 }
12741         }
12742
12743         if (err_code != NULL)
12744                 *err_code = error;
12745
12746         EM_DEBUG_FUNC_END("ret [%d]", ret);
12747         return ret;
12748 }
12749
12750
12751 INTERNAL_FUNC int emstorage_get_max_mail_count()
12752 {
12753         return EMAIL_MAIL_MAX_COUNT;
12754 }
12755
12756 #define STRIPPED_SUBJECT_BUFFER_SIZE 4086
12757
12758 INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(char *multi_user_name, emstorage_mail_tbl_t *mail_tbl, int *thread_id, int *result_latest_mail_id_in_thread, int *thread_item_count)
12759 {
12760         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);
12761         EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
12762         int      rc = 0, query_size = 0, query_size_account = 0;
12763         int      account_id = 0;
12764         int      err_code = EMAIL_ERROR_NONE;
12765         int      count = 0, result_thread_id = -1, latest_mail_id_in_thread = -1;
12766         time_t   latest_date_time = 0;
12767         char    *subject = NULL;
12768         char    *sql_query_string = NULL, *sql_account = NULL;
12769         char    *sql_format = "SELECT thread_id, date_time, mail_id FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
12770         char    *sql_format_account = " AND account_id = %d ";
12771         char    *sql_format_order_by = " ORDER BY date_time DESC ";
12772         char   **result = NULL;
12773         char     stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
12774
12775         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12776
12777         EM_DEBUG_LOG("subject: [%p], mail_id: [%d]", subject, mail_tbl->mail_id);
12778
12779         EM_IF_NULL_RETURN_VALUE(mail_tbl, EMAIL_ERROR_INVALID_PARAM);
12780         EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
12781         EM_IF_NULL_RETURN_VALUE(result_latest_mail_id_in_thread, EMAIL_ERROR_INVALID_PARAM);
12782         EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
12783
12784         account_id   = mail_tbl->account_id;
12785         subject      = mail_tbl->subject;
12786
12787         EM_DEBUG_LOG_SEC("subject: [%s]", subject);
12788
12789         if (EM_SAFE_STRLEN(subject) == 0 && mail_tbl->mail_id != 0) {
12790                 result_thread_id = mail_tbl->mail_id;
12791                 count = 1;
12792                 goto FINISH_OFF;
12793         }
12794
12795         if (em_find_pos_stripped_subject_for_thread_view(subject, stripped_subject, STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE)  {
12796                 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
12797                 err_code =  EMAIL_ERROR_UNKNOWN;
12798                 result_thread_id = -1;
12799                 goto FINISH_OFF;
12800         }
12801
12802         EM_DEBUG_LOG_SEC("stripped_subject: [%s]", stripped_subject);
12803
12804         if (EM_SAFE_STRLEN(stripped_subject) == 0) {
12805                 result_thread_id = -1;
12806                 goto FINISH_OFF;
12807         }
12808
12809         EM_DEBUG_LOG_SEC("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s", EM_SAFE_STRLEN(stripped_subject), stripped_subject);
12810
12811         if (account_id > 0)     {
12812                 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
12813                 sql_account = malloc(query_size_account);
12814                 if (sql_account == NULL) {
12815                         EM_DEBUG_EXCEPTION("malloc for sql_account  is failed %d", query_size_account);
12816                         err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
12817                         goto FINISH_OFF;
12818                 }
12819                 snprintf(sql_account, query_size_account, sql_format_account, account_id);
12820         }
12821
12822         /* prevent 34362 */
12823         query_size = strlen(sql_format) + strlen(stripped_subject)*2 + 50 + query_size_account + strlen(sql_format_order_by); /*  + query_size_mailbox; */
12824         sql_query_string = malloc(query_size);
12825
12826         if (sql_query_string == NULL) {
12827                 EM_DEBUG_EXCEPTION("malloc for sql  is failed %d", query_size);
12828                 err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
12829                 goto FINISH_OFF;
12830         }
12831
12832         sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mail_tbl->mailbox_id);
12833
12834         if (account_id > 0)
12835                 strcat(sql_query_string, sql_account);
12836
12837         strcat(sql_query_string, sql_format_order_by);
12838         strcat(sql_query_string, ";");
12839
12840         EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
12841
12842         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
12843
12844         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12845                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12846
12847         EM_DEBUG_LOG("Result rows count : %d", count);
12848
12849         if (count == 0)
12850                 result_thread_id = -1;
12851         else {
12852                 _get_table_field_data_int   (result, &result_thread_id, 3);
12853                 _get_table_field_data_time_t(result, &latest_date_time, 4);
12854                 _get_table_field_data_int   (result, &latest_mail_id_in_thread, 5);
12855
12856                 if (latest_date_time < mail_tbl->date_time)
12857                         *result_latest_mail_id_in_thread = latest_mail_id_in_thread;
12858                 else
12859                         *result_latest_mail_id_in_thread = mail_tbl->mail_id;
12860                 EM_DEBUG_LOG("latest_mail_id_in_thread [%d], mail_id [%d]", latest_mail_id_in_thread, mail_tbl->mail_id);
12861         }
12862
12863 FINISH_OFF:
12864         *thread_id = result_thread_id;
12865         *thread_item_count = count;
12866
12867         EM_DEBUG_LOG("Result thread id : %d", *thread_id);
12868         EM_DEBUG_LOG("Result count : %d", *thread_item_count);
12869         EM_DEBUG_LOG("err_code : %d", err_code);
12870
12871         EM_SAFE_FREE(sql_account);
12872         EM_SAFE_FREE(sql_query_string);
12873
12874         sqlite3_free_table(result);
12875
12876         EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
12877
12878         return err_code;
12879 }
12880
12881 INTERNAL_FUNC int emstorage_get_thread_id_from_mailbox(char *multi_user_name, int account_id, int mailbox_id, char *mail_subject, int *thread_id, int *thread_item_count)
12882 {
12883         EM_DEBUG_FUNC_BEGIN("mailbox_id [%d], subject [%p], thread_id [%p], thread_item_count [%p]", mailbox_id, mail_subject, thread_id, thread_item_count);
12884         EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
12885
12886         int rc = 0;
12887         int query_size = 0;
12888         int query_size_account = 0;
12889         int err_code = EMAIL_ERROR_NONE;
12890         int count = 0;
12891         int result_thread_id = -1;
12892         char *sql_query_string = NULL;
12893         char *sql_account = NULL;
12894         char *sql_format = "SELECT thread_id FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
12895         char *sql_format_account = " AND account_id = %d ";
12896         char *sql_format_order_by = " ORDER BY date_time DESC ";
12897         char **result = NULL;
12898         char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
12899         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12900
12901         EM_IF_NULL_RETURN_VALUE(mail_subject, EMAIL_ERROR_INVALID_PARAM);
12902         EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
12903         EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
12904
12905         EM_DEBUG_LOG_SEC("subject: [%s]", mail_subject);
12906
12907         if (em_find_pos_stripped_subject_for_thread_view(mail_subject, stripped_subject, STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE)     {
12908                 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view  is failed");
12909                 err_code =  EMAIL_ERROR_UNKNOWN;
12910                 result_thread_id = -1;
12911                 goto FINISH_OFF;
12912         }
12913
12914         if (EM_SAFE_STRLEN(stripped_subject) == 0) {
12915                 result_thread_id = -1;
12916                 goto FINISH_OFF;
12917         }
12918
12919         EM_DEBUG_LOG("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s", EM_SAFE_STRLEN(stripped_subject), stripped_subject);
12920
12921         if (account_id > 0) {
12922                 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
12923                 sql_account = malloc(query_size_account);
12924                 if (sql_account == NULL) {
12925                         EM_DEBUG_EXCEPTION("malloc for sql_account  is failed %d", query_size_account);
12926                         err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
12927                         goto FINISH_OFF;
12928                 }
12929                 snprintf(sql_account, query_size_account, sql_format_account, account_id);
12930         }
12931
12932         query_size = EM_SAFE_STRLEN(sql_format) + EM_SAFE_STRLEN(stripped_subject)*2 + 50 + query_size_account + EM_SAFE_STRLEN(sql_format_order_by); /*  + query_size_mailbox; */
12933         sql_query_string = malloc(query_size);
12934
12935         if (sql_query_string == NULL) {
12936                 EM_DEBUG_EXCEPTION("malloc for sql  is failed %d", query_size);
12937                 err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
12938                 goto FINISH_OFF;
12939         }
12940
12941         sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mailbox_id);
12942
12943         if (account_id > 0)
12944                 strcat(sql_query_string, sql_account);
12945
12946         strcat(sql_query_string, sql_format_order_by);
12947         strcat(sql_query_string, ";");
12948
12949         EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
12950
12951         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
12952
12953         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
12954                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12955
12956         EM_DEBUG_LOG("Result rows count : %d", count);
12957
12958         if (count == 0)
12959                 result_thread_id = -1;
12960         else {
12961                 _get_table_field_data_int   (result, &result_thread_id, 1);
12962         }
12963
12964 FINISH_OFF:
12965         *thread_id = result_thread_id;
12966         *thread_item_count = count;
12967
12968         EM_DEBUG_LOG("Result thread id : %d", *thread_id);
12969         EM_DEBUG_LOG("Result count : %d", *thread_item_count);
12970         EM_DEBUG_LOG("err_code : %d", err_code);
12971
12972         EM_SAFE_FREE(sql_account);
12973         EM_SAFE_FREE(sql_query_string);
12974
12975         sqlite3_free_table(result);
12976
12977         EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
12978
12979         return err_code;
12980 }
12981
12982 INTERNAL_FUNC int emstorage_get_thread_information(char *multi_user_name, int thread_id, emstorage_mail_tbl_t** mail_tbl, int transaction, int *err_code)
12983 {
12984         EM_DEBUG_FUNC_BEGIN();
12985
12986         int count = 0, ret = false;
12987         int error = EMAIL_ERROR_NONE;
12988         emstorage_mail_tbl_t *p_data_tbl = NULL;
12989         char conditional_clause[QUERY_SIZE] = {0, };
12990
12991         EM_IF_NULL_RETURN_VALUE(mail_tbl, false);
12992
12993         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE thread_id = %d AND thread_item_count > 0", thread_id);
12994         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
12995
12996         if(!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
12997                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
12998                 goto FINISH_OFF;
12999         }
13000
13001         if(p_data_tbl)
13002                 EM_DEBUG_LOG("thread_id : %d, thread_item_count : %d", p_data_tbl[0].thread_id, p_data_tbl[0].thread_item_count);
13003
13004         ret = true;
13005
13006 FINISH_OFF:
13007         if (ret == true)
13008                 *mail_tbl = p_data_tbl;
13009         else if (p_data_tbl != NULL)
13010                 emstorage_free_mail(&p_data_tbl, 1, NULL);
13011
13012         if (err_code != NULL)
13013                 *err_code = error;
13014
13015         EM_DEBUG_FUNC_END("ret [%d]", ret);
13016         return ret;
13017 }
13018
13019 INTERNAL_FUNC int emstorage_get_sender_list(char *multi_user_name, int account_id, int mailbox_id, int search_type, const char *search_value, email_sort_type_t sorting, email_sender_list_t** sender_list, int *sender_count,  int *err_code)
13020 {
13021         EM_DEBUG_FUNC_BEGIN_SEC("account_id [%d], mailbox_id [%d], search_type [%d], search_value [%p], sorting [%d], sender_list[%p], sender_count[%p] err_code[%p]"
13022                 , account_id , mailbox_id , search_type , search_value , sorting , sender_list, sender_count, err_code);
13023
13024         if ((!sender_list) ||(!sender_count)) {
13025                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
13026                 if (err_code != NULL)
13027                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13028                 return false;
13029         }
13030
13031         int rc = -1, ret = false;
13032         int error = EMAIL_ERROR_NONE;
13033         int count = 0;
13034         int i, col_index = 0;
13035         int read_count = 0;
13036         email_sender_list_t *p_sender_list = NULL;
13037         char sql_query_string[QUERY_SIZE] = {0, };
13038         char **result = NULL;
13039         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13040
13041         SNPRINTF(sql_query_string, sizeof(sql_query_string),
13042                 "SELECT email_address_sender, alias_sender, COUNT(email_address_sender), SUM(flags_seen_field = 1) "
13043                 "FROM mail_tbl ");
13044
13045         /*  mailbox_id */
13046         if (mailbox_id)
13047                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE mailbox_id = %d ", mailbox_id);
13048         else    /*  NULL  means all mailbox_name. but except for trash(3), spambox(5), all emails(for GMail, 7) */
13049                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE mailbox_type not in (3, 5, 7, 8) ");
13050
13051         /*  account id */
13052         /*  '0' (ALL_ACCOUNT) means all account */
13053         if (account_id > ALL_ACCOUNT)
13054                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1), " AND account_id = %d ", account_id);
13055
13056         if (search_value) {
13057                 switch (search_type) {
13058                         case EMAIL_SEARCH_FILTER_SUBJECT:
13059                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13060                                         " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\')) ", search_value);
13061                                 break;
13062                         case EMAIL_SEARCH_FILTER_SENDER:
13063                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13064                                         " AND  ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13065                                         ") ", search_value);
13066                                 break;
13067                         case EMAIL_SEARCH_FILTER_RECIPIENT:
13068                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13069                                         " AND ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13070                                         "       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13071                                         "       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13072                                         ") ", search_value, search_value, search_value);
13073                                 break;
13074                         case EMAIL_SEARCH_FILTER_ALL:
13075                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13076                                         " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') "
13077                                         "       OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13078                                         "                       OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13079                                         "                       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13080                                         "                       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13081                                         "               ) "
13082                                         "       )"
13083                                         ")", search_value, search_value, search_value, search_value, search_value);
13084                                 break;
13085                 }
13086         }
13087
13088
13089         /*  sorting option is not available now. The order of sender list is ascending order by display name */
13090         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
13091                 "GROUP BY email_address_sender "
13092                 "ORDER BY UPPER(alias_sender) ");
13093
13094         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
13095
13096         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
13097         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13098                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13099
13100         EM_DEBUG_LOG("Count of Sender [%d]", count);
13101
13102         if (!(p_sender_list = (email_sender_list_t*)em_malloc(sizeof(email_sender_list_t) * count))) {
13103                 EM_DEBUG_EXCEPTION("em_malloc failed...");
13104                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13105                 goto FINISH_OFF;
13106         }
13107
13108         col_index = 4;
13109
13110         EM_DEBUG_LOG(">>>> DATA ASSIGN START >>");
13111         for (i = 0; i < count; i++)  {
13112                 _get_table_field_data_string(result, &(p_sender_list[i].address), 1, col_index++);
13113                 _get_table_field_data_string(result, &(p_sender_list[i].display_name), 1, col_index++);
13114                 _get_table_field_data_int(result, &(p_sender_list[i].total_count), col_index++);
13115                 _get_table_field_data_int(result, &(read_count), col_index++);
13116                 p_sender_list[i].unread_count = p_sender_list[i].total_count - read_count;              /*  unread count = total - read          */
13117         }
13118         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >>", count);
13119
13120         sqlite3_free_table(result);
13121         result = NULL;
13122
13123         ret = true;
13124
13125 FINISH_OFF:
13126         if (ret == true)  {
13127                 *sender_list = p_sender_list;
13128                 *sender_count = count;
13129                 EM_DEBUG_LOG(">>>> COUNT : %d >>", count);
13130         }
13131
13132
13133         if (err_code != NULL)
13134                 *err_code = error;
13135
13136         EM_DEBUG_FUNC_END("ret [%d]", ret);
13137         return ret;
13138 }
13139
13140 INTERNAL_FUNC int emstorage_free_sender_list(email_sender_list_t **sender_list, int count)
13141 {
13142         EM_DEBUG_FUNC_BEGIN("sender_list[%p], count[%d]", sender_list, count);
13143
13144         int err = EMAIL_ERROR_NONE;
13145
13146         if (count > 0)  {
13147                 if (!sender_list || !*sender_list)  {
13148                         EM_DEBUG_EXCEPTION("sender_list[%p], count[%d]", sender_list, count);
13149                         err = EMAIL_ERROR_INVALID_PARAM;
13150                         return err;
13151                 }
13152
13153                 email_sender_list_t* p = *sender_list;
13154                 int i = 0;
13155
13156                 for (; i < count; i++)  {
13157                         EM_SAFE_FREE(p[i].address);
13158                         EM_SAFE_FREE(p[i].display_name);
13159                 }
13160
13161                 EM_SAFE_FREE(p);
13162                 *sender_list = NULL;
13163         }
13164
13165         return err;
13166 }
13167
13168
13169 INTERNAL_FUNC int emstorage_free_address_info_list(email_address_info_list_t **address_info_list)
13170 {
13171         EM_DEBUG_FUNC_BEGIN("address_info_list[%p]", address_info_list);
13172
13173         int err = EMAIL_ERROR_NONE;
13174         email_address_info_t *p_address_info = NULL;
13175         GList *list = NULL;
13176         GList *node = NULL;
13177         int i = 0;
13178
13179         if (!address_info_list || !*address_info_list)  {
13180                 EM_DEBUG_EXCEPTION("address_info_list[%p]", address_info_list);
13181                 err = EMAIL_ERROR_INVALID_PARAM;
13182                 return err;
13183         }
13184
13185         /*  delete GLists */
13186         for (i = EMAIL_ADDRESS_TYPE_FROM; i <= EMAIL_ADDRESS_TYPE_BCC; i++) {
13187                 switch (i) {
13188                         case EMAIL_ADDRESS_TYPE_FROM:
13189                                 list = (*address_info_list)->from;
13190                                 break;
13191                         case EMAIL_ADDRESS_TYPE_TO:
13192                                 list = (*address_info_list)->to;
13193                                 break;
13194                         case EMAIL_ADDRESS_TYPE_CC:
13195                                 list = (*address_info_list)->cc;
13196                                 break;
13197                         case EMAIL_ADDRESS_TYPE_BCC:
13198                                 list = (*address_info_list)->bcc;
13199                                 break;
13200                 }
13201
13202                 /*  delete dynamic-allocated memory for each item */
13203                 node = g_list_first(list);
13204                 while (node != NULL) {
13205                         p_address_info = (email_address_info_t*)node->data;
13206                         EM_SAFE_FREE(p_address_info->address);
13207                         EM_SAFE_FREE(p_address_info->display_name);
13208                         EM_SAFE_FREE(node->data);
13209
13210                         node = g_list_next(node);
13211                 }
13212                 g_list_free(list);
13213         }
13214
13215         EM_SAFE_FREE(*address_info_list);
13216         *address_info_list = NULL;
13217
13218         EM_DEBUG_FUNC_END("err [%d]", err);
13219         return err;
13220 }
13221 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
13222
13223 INTERNAL_FUNC int emstorage_add_pbd_activity(char *multi_user_name, email_event_partial_body_thd* local_activity, int *activity_id, int transaction, int *err_code)
13224 {
13225         EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
13226
13227         if (!local_activity || !activity_id) {
13228                 EM_DEBUG_EXCEPTION("local_activity[%p], transaction[%d], activity_id[%p], err_code[%p]", local_activity, activity_id, transaction, err_code);
13229                 if (err_code != NULL)
13230                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13231                 return false;
13232         }
13233
13234         int rc = -1;
13235         int ret = false;
13236         int error = EMAIL_ERROR_NONE;
13237         int i = 0;
13238
13239         char sql_query_string[QUERY_SIZE] = {0, };
13240         DB_STMT hStmt = NULL;
13241         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13242
13243         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13244         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13245         SNPRINTF(sql_query_string, sizeof(sql_query_string),
13246                 "INSERT INTO mail_partial_body_activity_tbl VALUES "
13247                 "( "
13248                 "? "  /* Account ID */
13249                 ",?"  /* Local Mail ID */
13250                 ",?"  /* Server mail ID */
13251                 ",?"  /* Activity ID */
13252                 ",?"  /* Activity type*/
13253                 ",?"  /* Mailbox ID*/
13254                 ",?"  /* Mailbox name*/
13255                 ",?"  /* Multi User Name */
13256                 ") ");
13257
13258         char *sql = "SELECT max(rowid) FROM mail_partial_body_activity_tbl;";
13259         char **result = NULL;
13260
13261
13262         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13263
13264         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13265                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13266
13267         if (NULL==result[1]) rc = 1;
13268         else rc = atoi(result[1])+1;
13269         sqlite3_free_table(result);
13270         result = NULL;
13271
13272         *activity_id = local_activity->activity_id = rc;
13273
13274         EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [ %d ], MAIL ID [ %d ], ACTIVITY TYPE [ %d ], SERVER MAIL ID [ %lu ]", \
13275                 local_activity->activity_id, local_activity->mail_id, local_activity->activity_type, local_activity->server_mail_id);
13276
13277         if (local_activity->mailbox_id)
13278                 EM_DEBUG_LOG(" MAILBOX ID [ %d ]", local_activity->mailbox_id);
13279
13280
13281         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13282         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13283                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13284
13285
13286         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
13287         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
13288         _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
13289         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
13290         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
13291         _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
13292         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->mailbox_name, 0, 3999);
13293         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
13294
13295
13296         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13297
13298         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
13299                 ("sqlite3_step fail:%d", rc));
13300         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13301                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
13302
13303         ret = true;
13304
13305 FINISH_OFF:
13306         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13307         if (hStmt != NULL) {
13308                 rc = sqlite3_finalize(hStmt);
13309                 hStmt = NULL;
13310                 if (rc != SQLITE_OK) {
13311                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13312                         error = EMAIL_ERROR_DB_FAILURE;
13313                 }
13314         }
13315
13316         if (err_code != NULL)
13317                 *err_code = error;
13318
13319         EM_DEBUG_FUNC_END("ret [%d]", ret);
13320         return ret;
13321 }
13322
13323 INTERNAL_FUNC int emstorage_get_pbd_mailbox_list(char *multi_user_name, int account_id, int **mailbox_list, int *count, int transaction, int *err_code)
13324 {
13325         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13326
13327         if (account_id < FIRST_ACCOUNT_ID || NULL == mailbox_list || *mailbox_list == NULL|| NULL == count) {
13328                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13329                 if (err_code != NULL)
13330                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13331                 return false;
13332         }
13333
13334         int ret = false;
13335         int error = EMAIL_ERROR_NONE;
13336         char **result;
13337         int i = 0, rc = -1;
13338         int *mbox_list = NULL;
13339         DB_STMT hStmt = NULL;
13340         char sql_query_string[QUERY_SIZE] = {0, };
13341
13342         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13343         EMSTORAGE_START_READ_TRANSACTION(transaction);
13344
13345         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13346         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);
13347
13348
13349         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13350         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13351                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13352
13353         *count = atoi(result[1]);
13354         sqlite3_free_table(result);
13355
13356         if (!*count) {
13357                 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
13358                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13359                 ret = true;
13360                 goto FINISH_OFF;
13361         }
13362         EM_DEBUG_LOG("Mailbox count = %d", *count);
13363
13364         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13365
13366         /* 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); */
13367         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);
13368
13369         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13370
13371
13372         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13373
13374
13375         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13376                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13377
13378
13379         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13380         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13381                 ("sqlite3_step fail:%d", rc));
13382
13383         mbox_list = (int *)em_malloc(sizeof(int) * (*count));
13384         if (NULL == mbox_list) {
13385                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
13386                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13387                 goto FINISH_OFF;
13388         }
13389
13390         memset(mbox_list, 0x00, sizeof(int) * (*count));
13391
13392         for (i = 0; i < (*count); i++) {
13393                 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
13394                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13395                 /* EM_DEBUG_LOG("In emstorage_get_pdb_mailbox_list() loop, After sqlite3_step(), , i = %d, rc = %d.", i,  rc); */
13396                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13397                         ("sqlite3_step fail:%d", rc));
13398                 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
13399         }
13400
13401         ret = true;
13402
13403 FINISH_OFF:
13404         if (ret == true)
13405                 *mailbox_list = mbox_list;
13406         else
13407                 EM_SAFE_FREE(mbox_list);
13408
13409         if (hStmt != NULL) {
13410                 rc = sqlite3_finalize(hStmt);
13411                 hStmt = NULL;
13412                 if (rc != SQLITE_OK) {
13413                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13414                         error = EMAIL_ERROR_DB_FAILURE;
13415                 }
13416         }
13417
13418         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13419         if (err_code != NULL)
13420                 *err_code = error;
13421         EM_DEBUG_FUNC_END("ret [%d]", ret);
13422         return ret;
13423 }
13424
13425 INTERNAL_FUNC int emstorage_get_pbd_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
13426 {
13427         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13428
13429         if (NULL == account_list || NULL == count) {
13430                 EM_DEBUG_EXCEPTION("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13431                 if (err_code != NULL)
13432                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13433                 return false;
13434         }
13435
13436         int ret = false;
13437         int error = EMAIL_ERROR_NONE;
13438         char *sql = "SELECT count(distinct account_id) FROM mail_partial_body_activity_tbl";
13439         char **result;
13440         int i = 0, rc = -1;
13441         int *result_account_list = NULL;
13442         DB_STMT hStmt = NULL;
13443         char sql_query_string[QUERY_SIZE] = {0, };
13444
13445         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13446
13447         EMSTORAGE_START_READ_TRANSACTION(transaction);
13448
13449         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13450         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13451                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13452
13453         *count = atoi(result[1]);
13454         sqlite3_free_table(result);
13455
13456         if (!*count) {
13457                 EM_DEBUG_EXCEPTION("no account found...");
13458                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13459                 ret = true;
13460                 goto FINISH_OFF;
13461         }
13462
13463         EM_DEBUG_LOG("Account count [%d]", *count);
13464
13465         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13466
13467         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_partial_body_activity_tbl");
13468
13469         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13470
13471
13472         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13473
13474         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
13475         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13476                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13477
13478
13479         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13480         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13481                 ("sqlite3_step fail:%d", rc));
13482
13483         if (NULL == (result_account_list = (int *)em_malloc(sizeof(int) * (*count)))) {
13484                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
13485                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13486                 goto FINISH_OFF;
13487         }
13488
13489         memset(result_account_list, 0x00, sizeof(int) * (*count));
13490
13491         for (i = 0; i < (*count); i++) {
13492                 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
13493
13494                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13495                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13496                         ("sqlite3_step fail:%d", rc));
13497                 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
13498         }
13499
13500         ret = true;
13501
13502 FINISH_OFF:
13503         if (ret == true)
13504                 *account_list = result_account_list;
13505         else
13506                 EM_SAFE_FREE(result_account_list);
13507
13508         if (hStmt != NULL) {
13509                 rc = sqlite3_finalize(hStmt);
13510                 hStmt = NULL;
13511                 if (rc != SQLITE_OK) {
13512                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13513                         error = EMAIL_ERROR_DB_FAILURE;
13514                 }
13515         }
13516
13517         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13518         if (err_code != NULL)
13519                 *err_code = error;
13520         EM_DEBUG_FUNC_END("ret [%d]", ret);
13521         return ret;
13522 }
13523
13524 INTERNAL_FUNC int emstorage_get_pbd_activity_data(char *multi_user_name, int account_id, int input_mailbox_id, email_event_partial_body_thd** event_start, int *count, int transaction, int *err_code)
13525 {
13526         EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
13527
13528         if (account_id < FIRST_ACCOUNT_ID || NULL == event_start || 0 == input_mailbox_id || NULL == count) {
13529                 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);
13530
13531                 if (err_code != NULL)
13532                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13533                 return false;
13534         }
13535
13536         int rc = -1;
13537         int ret = false;
13538         char **result;
13539         int error = EMAIL_ERROR_NONE;
13540         int i = 0;
13541         DB_STMT hStmt = NULL;
13542         email_event_partial_body_thd* event_list = NULL;
13543         char sql_query_string[QUERY_SIZE] = {0, };
13544
13545         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13546
13547         EMSTORAGE_START_READ_TRANSACTION(transaction);
13548
13549         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13550         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);
13551
13552
13553         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13554         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
13555                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13556
13557         *count = atoi(result[1]);
13558         sqlite3_free_table(result);
13559
13560         EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
13561
13562         if (!*count) {
13563                 EM_DEBUG_LOG("No matched activity found in mail_partial_body_activity_tbl");
13564                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
13565                 ret = true;
13566                 goto FINISH_OFF;
13567         }
13568         EM_DEBUG_LOG("Activity Count = %d", *count);
13569
13570         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13571         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);
13572
13573         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13574
13575
13576         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13577
13578         EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
13579         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13580                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13581
13582
13583         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13584         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13585                 ("sqlite3_step fail:%d", rc));
13586
13587         if (!(event_list = (email_event_partial_body_thd*)em_malloc(sizeof(email_event_partial_body_thd) * (*count)))) {
13588                 EM_DEBUG_EXCEPTION("Malloc failed");
13589
13590                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13591                 goto FINISH_OFF;
13592         }
13593         memset(event_list, 0x00, sizeof(email_event_partial_body_thd) * (*count));
13594
13595         for (i=0; i < (*count); i++) {
13596                 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_IDX_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13597                 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13598                 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13599                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13600                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_type), ACTIVITY_TYPE_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13601                 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13602                 _get_stmt_field_data_string(hStmt, &(event_list[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13603                 _get_stmt_field_data_string(hStmt, &(event_list[i].multi_user_name), 0, MULTI_USER_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13604
13605                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13606                 /* EM_DEBUG_LOG("In emstorage_get_pbd_activity_data() loop, After sqlite3_step(), , i = %d, rc = %d.", i,  rc); */
13607                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13608                         ("sqlite3_step fail:%d", rc));
13609
13610                 event_list[i].event_type = 0;
13611         }
13612
13613         ret = true;
13614
13615 FINISH_OFF:
13616         if (true == ret)
13617           *event_start = event_list;
13618         else {
13619                 for (i=0; i < (*count); i++)
13620                         emcore_free_partial_body_thd_event(event_list, NULL);
13621                 EM_SAFE_FREE(event_list); /*prevent 54559*/
13622                 *event_start = NULL;
13623                 *count = 0;
13624         }
13625
13626         if (hStmt != NULL) {
13627                 rc = sqlite3_finalize(hStmt);
13628                 hStmt = NULL;
13629                 if (rc != SQLITE_OK) {
13630                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13631                         error = EMAIL_ERROR_DB_FAILURE;
13632                 }
13633         }
13634
13635         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13636         if (err_code != NULL)
13637                 *err_code = error;
13638
13639         EM_DEBUG_FUNC_END("ret [%d]", ret);
13640         return ret;
13641 }
13642
13643 INTERNAL_FUNC int emstorage_delete_pbd_activity(char *multi_user_name, int account_id, int mail_id, int activity_id, int transaction, int *err_code)
13644 {
13645         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);
13646
13647
13648         if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
13649                 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);
13650
13651                 if (err_code != NULL)
13652                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13653                 return false;
13654         }
13655
13656         int rc = -1;
13657         int ret = false;
13658         int error = EMAIL_ERROR_NONE;
13659         char sql_query_string[QUERY_SIZE] = {0, };
13660         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13661
13662         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13663         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13664
13665         if (activity_id == 0)
13666                 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);
13667         else
13668                 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);
13669
13670         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13671         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
13672         if (error != EMAIL_ERROR_NONE) {
13673                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
13674                         goto FINISH_OFF;
13675         }
13676
13677         /*  validate activity existence */
13678         rc = sqlite3_changes(local_db_handle);
13679         if (rc == 0)  {
13680                 EM_DEBUG_EXCEPTION("No matching activity found");
13681                 error = EMAIL_ERROR_DATA_NOT_FOUND;
13682                 ret = true;
13683                 goto FINISH_OFF;
13684         }
13685
13686         ret = true;
13687
13688 FINISH_OFF:
13689         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13690
13691         if (err_code != NULL)
13692                 *err_code = error;
13693
13694         EM_DEBUG_FUNC_END("ret [%d]", ret);
13695         return ret;
13696 }
13697
13698 INTERNAL_FUNC int emstorage_get_mailbox_pbd_activity_count(char *multi_user_name, int account_id, int input_mailbox_id, int *activity_count, int transaction, int *err_code)
13699 {
13700         EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
13701
13702         if (account_id < FIRST_ACCOUNT_ID || NULL == activity_count || NULL == err_code) {
13703                 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
13704                 if (err_code != NULL)
13705                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13706                 return false;
13707         }
13708         int rc = -1;
13709         int ret = false;
13710         int error = EMAIL_ERROR_NONE;
13711         char sql_query_string[QUERY_SIZE] = {0, };
13712
13713         DB_STMT hStmt = NULL;
13714
13715         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13716
13717         EMSTORAGE_START_READ_TRANSACTION(transaction);
13718         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13719
13720         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);
13721
13722         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13723
13724
13725         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13726         EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
13727         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13728                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13729
13730
13731         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13732         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13733                 ("sqlite3_step fail:%d", rc));
13734
13735         _get_stmt_field_data_int(hStmt, activity_count, 0);
13736
13737         EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
13738
13739         ret = true;
13740
13741 FINISH_OFF:
13742
13743         if (hStmt != NULL) {
13744                 rc = sqlite3_finalize(hStmt);
13745                 hStmt=NULL;
13746                 if (rc != SQLITE_OK) {
13747                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13748                         error = EMAIL_ERROR_DB_FAILURE;
13749                 }
13750         }
13751
13752         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13753
13754         if (err_code != NULL)
13755                 *err_code = error;
13756
13757         EM_DEBUG_FUNC_END("ret [%d]", ret);
13758         return ret;
13759 }
13760
13761 INTERNAL_FUNC int emstorage_get_pbd_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
13762 {
13763         EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
13764
13765         if (NULL == activity_count || NULL == err_code) {
13766                 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
13767                 if (err_code != NULL)
13768                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13769                 return false;
13770         }
13771         int rc = -1;
13772         int ret = false;
13773         int error = EMAIL_ERROR_NONE;
13774         DB_STMT hStmt = NULL;
13775         char sql_query_string[QUERY_SIZE] = {0, };
13776
13777         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13778
13779         EMSTORAGE_START_READ_TRANSACTION(transaction);
13780         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13781
13782         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl;");
13783
13784         EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
13785
13786         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13787         EM_DEBUG_LOG_DEV ("  before sqlite3_prepare hStmt = %p", hStmt);
13788         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13789                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13790
13791         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13792         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
13793                 ("sqlite3_step fail:%d", rc));
13794
13795         _get_stmt_field_data_int(hStmt, activity_count, 0);
13796
13797         EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
13798
13799         ret = true;
13800
13801 FINISH_OFF:
13802
13803
13804         if (hStmt != NULL) {
13805                 rc = sqlite3_finalize(hStmt);
13806                 hStmt=NULL;
13807                 if (rc != SQLITE_OK) {
13808                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13809                         error = EMAIL_ERROR_DB_FAILURE;
13810                 }
13811         }
13812
13813         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13814         if (err_code != NULL)
13815                 *err_code = error;
13816
13817         EM_DEBUG_FUNC_END("ret [%d]", ret);
13818         return ret;
13819 }
13820
13821 INTERNAL_FUNC int emstorage_delete_full_pbd_activity_data(char *multi_user_name, int account_id, int transaction, int *err_code)
13822 {
13823         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
13824         if (account_id < FIRST_ACCOUNT_ID) {
13825                 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
13826                 if (err_code != NULL)
13827                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13828                 return false;
13829         }
13830
13831         int rc = -1;
13832         int ret = false;
13833         int error = EMAIL_ERROR_NONE;
13834         char sql_query_string[QUERY_SIZE] = {0, };
13835
13836         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13837
13838         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13839         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13840         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d", account_id);
13841
13842         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13843         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
13844         if (error != EMAIL_ERROR_NONE) {
13845                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
13846                         goto FINISH_OFF;
13847         }
13848
13849         rc = sqlite3_changes(local_db_handle);
13850         if (rc == 0) {
13851                 EM_DEBUG_EXCEPTION("No matching activities found in mail_partial_body_activity_tbl");
13852                 error = EMAIL_ERROR_DATA_NOT_FOUND;
13853                 ret = true;
13854                 goto FINISH_OFF;
13855         }
13856
13857         ret = true;
13858
13859 FINISH_OFF:
13860
13861         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13862
13863         if (err_code != NULL)
13864                 *err_code = error;
13865
13866         EM_DEBUG_FUNC_END("ret [%d]", ret);
13867         return ret;
13868 }
13869
13870 /*Himanshu[h.gahlaut]-> Added below API to update mail_partial_body_activity_tbl
13871 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*/
13872
13873 INTERNAL_FUNC int emstorage_update_pbd_activity(char *multi_user_name, char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code)
13874 {
13875         EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
13876
13877         int rc = -1, ret = false;
13878         int error = EMAIL_ERROR_NONE;
13879         char sql_query_string[QUERY_SIZE] = {0, };
13880
13881         int transaction = true;
13882
13883         if (!old_server_uid || !new_server_uid || !mbox_name)  {
13884                 EM_DEBUG_EXCEPTION("Invalid parameters");
13885                 error = EMAIL_ERROR_INVALID_PARAM;
13886                 return false;
13887         }
13888
13889         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13890
13891         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13892         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13893         SNPRINTF(sql_query_string, sizeof(sql_query_string),
13894                  "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);
13895
13896         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13897         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
13898         if (error != EMAIL_ERROR_NONE) {
13899                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
13900                         goto FINISH_OFF;
13901         }
13902
13903         rc = sqlite3_changes(local_db_handle);
13904         if (rc == 0) {
13905                 EM_DEBUG_LOG("No matching found in mail_partial_body_activity_tbl");
13906         }
13907
13908         ret = true;
13909
13910 FINISH_OFF:
13911         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13912
13913         if (err_code != NULL)
13914                 *err_code = error;
13915
13916         EM_DEBUG_FUNC_END("ret [%d]", ret);
13917         return ret;
13918 }
13919
13920
13921 INTERNAL_FUNC int emstorage_create_file(char *data_string, size_t file_size, char *dst_file_name, int *err_code)
13922 {
13923         EM_DEBUG_FUNC_BEGIN_SEC("file_size[%d] , dst_file_name[%s], err_code[%p]", file_size, dst_file_name, err_code);
13924
13925         int ret = false;
13926         int error = EMAIL_ERROR_NONE;
13927         FILE* fp_dst = NULL;
13928         char errno_buf[ERRNO_BUF_SIZE] = {0};
13929
13930         if (!data_string || !dst_file_name)  {
13931                 EM_DEBUG_LOG("data_string[%p], dst_file_name[%p]", data_string, dst_file_name);
13932                 error = EMAIL_ERROR_INVALID_PARAM;
13933                 goto FINISH_OFF;
13934         }
13935
13936         error = em_fopen(dst_file_name, "w", &fp_dst);
13937         if (error != EMAIL_ERROR_NONE) {
13938                 EM_DEBUG_EXCEPTION_SEC("em_fopen failed - %s: %d", dst_file_name, error);
13939                 goto FINISH_OFF;
13940         }
13941
13942         if (fwrite(data_string, 1, file_size, fp_dst) == 0) {
13943                 EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
13944                 error = EMAIL_ERROR_UNKNOWN;
13945                 goto FINISH_OFF;
13946         }
13947
13948         ret = true;
13949
13950 FINISH_OFF:
13951
13952         if (fp_dst != NULL)
13953                 fclose(fp_dst);
13954
13955         if (err_code != NULL)
13956                 *err_code = error;
13957
13958         EM_DEBUG_FUNC_END("ret [%d]", ret);
13959         return ret;
13960 }
13961
13962 #endif /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
13963
13964
13965
13966 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
13967 INTERNAL_FUNC int emstorage_update_read_mail_uid_by_server_uid(char *multi_user_name, char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code)
13968 {
13969         EM_DEBUG_FUNC_BEGIN();
13970         int rc = -1;
13971         int ret = false;
13972         int error = EMAIL_ERROR_NONE;
13973         char sql_query_string[QUERY_SIZE] = {0, };
13974
13975         int transaction = true;
13976
13977         if (!old_server_uid || !new_server_uid || !mbox_name)  {
13978                 EM_DEBUG_EXCEPTION("Invalid parameters");
13979                 if (err_code != NULL)
13980                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13981                 return false;
13982         }
13983
13984         EM_DEBUG_LOG_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
13985
13986         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13987         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13988
13989
13990         SNPRINTF(sql_query_string, sizeof(sql_query_string),
13991                  "UPDATE mail_read_mail_uid_tbl SET server_uid=\'%s\' , mailbox_name=\'%s\' WHERE server_uid=%s ", new_server_uid, mbox_name, old_server_uid);
13992
13993         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13994         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
13995         if (error != EMAIL_ERROR_NONE) {
13996                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
13997                         goto FINISH_OFF;
13998         }
13999
14000         rc = sqlite3_changes(local_db_handle);
14001         if (rc == 0)
14002         {
14003                 EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
14004                 error = EMAIL_ERROR_DATA_NOT_FOUND;
14005                 goto FINISH_OFF;
14006         }
14007
14008         ret = true;
14009
14010 FINISH_OFF:
14011         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14012
14013         if (err_code != NULL)
14014                 *err_code = error;
14015
14016         EM_DEBUG_FUNC_END("ret [%d]", ret);
14017         return ret;
14018
14019 }
14020
14021
14022 /**
14023  * @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);
14024  * Prepare an array of mail_id and corresponding server mail id.
14025  *
14026  *@author                                       h.gahlaut@samsung.com
14027  * @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)
14028  *                                                      where 88 is the length of fixed keywords including ending null character in the QUERY to be formed
14029  * @param[out] idset                    Returns the array of mail_id and corresponding server_mail_id sorted by server_mail_ids
14030  * @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
14031  * @param[out] err_code         Returns the error code.
14032  * @remarks                                     An Example of Query to be exexuted in this API:
14033  *                                                      SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (12, 13, 56, 78);
14034  * @return This function returns true on success or false on failure.
14035  */
14036 INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *multi_user_name, char *mail_ids, email_id_set_t** idset, int *id_set_count, int *err_code)
14037 {
14038         EM_DEBUG_FUNC_BEGIN();
14039         EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds);
14040
14041         int error = EMAIL_ERROR_NONE;
14042         int ret = false;
14043         email_id_set_t* p_id_set = NULL;
14044         int count = 0;
14045         const int buf_size = QUERY_SIZE;
14046         char sql_query_string[QUERY_SIZE] = {0, };
14047         int space_left_in_query_buffer = buf_size;
14048         int i = 0;
14049         int rc = -1;
14050         char *server_mail_id = NULL;
14051         char **result = NULL;
14052         int col_index = 0;
14053
14054
14055         if (NULL == mail_ids || NULL == idset || NULL == id_set_count) {
14056                 EM_DEBUG_EXCEPTION("Invalid Parameters mail_ids[%p] idset[%p]  id_set_count [%p]", mail_ids, idset, id_set_count);
14057                 if (err_code != NULL)
14058                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14059                 return false;
14060         }
14061
14062         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14063
14064         SNPRINTF(sql_query_string, space_left_in_query_buffer, "SELECT local_uid, server_uid FROM mail_read_mail_uid_tbl WHERE local_uid in (%s) ORDER BY server_uid", mail_ids);
14065
14066         EM_DEBUG_LOG_SEC("SQL Query formed [%s] ", sql_query_string);
14067
14068         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14069         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14070         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14071                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14072
14073         EM_DEBUG_LOG(" Count of mails [%d ]", count);
14074
14075         if (count <= 0) {
14076                 EM_DEBUG_EXCEPTION("Can't find proper mail");
14077                 error = EMAIL_ERROR_DATA_NOT_FOUND;
14078                 goto FINISH_OFF;
14079         }
14080
14081
14082         if (NULL == (p_id_set = (email_id_set_t*)em_malloc(sizeof(email_id_set_t) * count)))  {
14083                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
14084                 error = EMAIL_ERROR_OUT_OF_MEMORY;
14085                 goto FINISH_OFF;
14086         }
14087
14088         col_index = 2;
14089
14090         EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds_Loop);
14091         EM_DEBUG_LOG(">>>> DATA ASSIGN START");
14092         for (i = 0; i < count; i++)  {
14093                 _get_table_field_data_int(result, &(p_id_set[i].mail_id), col_index++);
14094                 _get_table_field_data_string(result, &server_mail_id, 1, col_index++);
14095                 if (server_mail_id) {
14096                         p_id_set[i].server_mail_id = strtoul(server_mail_id, NULL, 10);
14097                         EM_SAFE_FREE(server_mail_id);
14098                 }
14099         }
14100         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d]", count);
14101         EM_PROFILE_END(EmStorageGetIdSetFromMailIds_Loop);
14102
14103         sqlite3_free_table(result);
14104         result = NULL;
14105
14106         ret = true;
14107
14108 FINISH_OFF:
14109
14110         if (ret == true)  {
14111                 *idset = p_id_set;
14112                 *id_set_count = count;
14113                 EM_DEBUG_LOG(" idset[%p] id_set_count [%d]", *idset, *id_set_count);
14114         }
14115         else
14116                 EM_SAFE_FREE(p_id_set);
14117
14118
14119         if (err_code != NULL)
14120                 *err_code = error;
14121
14122         EM_PROFILE_END(EmStorageGetIdSetFromMailIds);
14123
14124         EM_DEBUG_FUNC_END("ret [%d]", ret);
14125         return ret;
14126 }
14127
14128
14129
14130 #endif
14131
14132 INTERNAL_FUNC int emstorage_delete_triggers_from_lucene(char *multi_user_name)
14133 {
14134         EM_DEBUG_FUNC_BEGIN();
14135         int ret = true, transaction = true;
14136         int error = EMAIL_ERROR_NONE;
14137         char sql_query_string[QUERY_SIZE] = {0, };
14138         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14139         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14140
14141         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerDelete;");
14142         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14143         if (error != EMAIL_ERROR_NONE) {
14144                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14145                         goto FINISH_OFF;
14146         }
14147
14148         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerInsert;");
14149         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14150         if (error != EMAIL_ERROR_NONE) {
14151                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14152                         goto FINISH_OFF;
14153         }
14154
14155         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerUpdate;");
14156         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14157         if (error != EMAIL_ERROR_NONE) {
14158                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14159                         goto FINISH_OFF;
14160         }
14161
14162         ret = true;
14163
14164 FINISH_OFF:
14165         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14166
14167         EM_DEBUG_FUNC_END("ret [%d]", ret);
14168         return ret;
14169 }
14170
14171 INTERNAL_FUNC int emstorage_update_tag_id(char *multi_user_name, int old_filter_id, int new_filter_id, int *err_code)
14172 {
14173         EM_DEBUG_FUNC_BEGIN("new_filter_id[%d], old_filter_id[%d]", new_filter_id, old_filter_id);
14174         int ret = false;
14175         int error = EMAIL_ERROR_NONE;
14176         char sql_query_string[QUERY_SIZE] = {0, };
14177         int transaction = true;
14178
14179         if (old_filter_id < 0 || new_filter_id < 0) {
14180                 EM_DEBUG_EXCEPTION("Invalid parameters");
14181                 if (err_code != NULL)
14182                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14183                 return false;
14184         }
14185
14186         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14187
14188         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14189
14190         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14191                  "UPDATE mail_tbl SET tag_id=%d WHERE tag_id=%d ", new_filter_id, old_filter_id);
14192
14193         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14194         if (error != EMAIL_ERROR_NONE) {
14195                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14196                         goto FINISH_OFF;
14197         }
14198
14199         ret = true;
14200
14201 FINISH_OFF:
14202         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14203
14204         if (err_code != NULL)
14205                 *err_code = error;
14206
14207         EM_DEBUG_FUNC_END("ret [%d]", ret);
14208         return ret;
14209 }
14210
14211 INTERNAL_FUNC int emstorage_filter_mails_by_rule(char *multi_user_name, int account_id, int dest_mailbox_id, int dest_mailbox_type, int reset, emstorage_rule_tbl_t *rule, int ** filtered_mail_id_list, int *count_of_mails, int *err_code)
14212 {
14213         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);
14214
14215         if ((account_id < 0) || (dest_mailbox_id < 0) || (!rule)) {
14216                 EM_DEBUG_EXCEPTION("Invalid Parameter");
14217
14218                 if (err_code != NULL)
14219                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14220                 return false;
14221         }
14222
14223         int rc = -1, ret = false, error = EMAIL_ERROR_NONE;
14224         int count = 0, col_index = 0, i = 0, where_pararaph_length = 0, *mail_list = NULL;
14225         int tag_id = rule->rule_id;
14226         char **result = NULL, *where_pararaph = NULL;
14227         char sql_query_string[QUERY_SIZE] = {0, };
14228         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14229
14230         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl ");
14231
14232         where_pararaph_length = EM_SAFE_STRLEN(rule->value) + 2 * (EM_SAFE_STRLEN(rule->value2)) + 100;
14233         where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
14234         if (where_pararaph == NULL) {
14235                 EM_DEBUG_EXCEPTION("malloc failed for where_pararaph.");
14236                 error = EMAIL_ERROR_OUT_OF_MEMORY;
14237                 goto FINISH_OFF;
14238         }
14239
14240         if (account_id != ALL_ACCOUNT)
14241                 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (0)", account_id);
14242         else
14243                 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_type NOT in (0)");
14244
14245         if (rule->type & EMAIL_FILTER_SUBJECT) {
14246                 if (rule->flag2 == RULE_TYPE_INCLUDES)
14247                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject like \'%%%q%%\'", rule->value);
14248                 else /*  RULE_TYPE_EXACTLY */
14249                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject = \'%q\'", rule->value);
14250         }
14251
14252         if (rule->type & EMAIL_FILTER_FROM) {
14253                 if (rule->flag2 == RULE_TYPE_INCLUDES)
14254                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND full_address_from like \'%%%q%%\'", rule->value2);
14255 #ifdef __FEATURE_COMPARE_DOMAIN__
14256                 else if (rule->flag2 == RULE_TYPE_COMPARE_DOMAIN)
14257                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND (full_address_from like \'@%%%q\' OR full_address_from like \'@%%%q>%%\')", rule->value2, rule->value2);
14258 #endif /*__FEATURE_COMPARE_DOMAIN__ */
14259                 else /*  RULE_TYPE_EXACTLY */
14260                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND full_address_from = \'%q\'", rule->value2);
14261         }
14262
14263         if (rule->type == EMAIL_PRIORITY_SENDER) {
14264                 if (rule->flag2 == RULE_TYPE_INCLUDES)
14265                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND full_address_from like \'%%%q%%\'", rule->value2);
14266                 else /*  RULE_TYPE_EXACTLY */
14267                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND full_address_from = \'%q\' OR email_address_sender = \'%q\'", rule->value2, rule->value2);
14268
14269                 tag_id = PRIORITY_SENDER_TAG_ID;
14270         }
14271
14272         /* prevent 34361 */
14273         if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14274                 strcat(sql_query_string, where_pararaph);
14275
14276         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14277
14278         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14279         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14280         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14281                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14282
14283         EM_DEBUG_LOG("Count of mails [%d]", count);
14284
14285         if (count) {
14286                 mail_list = malloc(sizeof(int) * count);
14287                 if (mail_list == NULL) {
14288                         EM_DEBUG_EXCEPTION("malloc failed for mail_list.");
14289                         error = EMAIL_ERROR_OUT_OF_MEMORY;
14290                         goto FINISH_OFF;
14291                 }
14292
14293                 col_index = 1;
14294
14295                 for (i = 0; i < count; i++)
14296                         _get_table_field_data_int(result, &(mail_list[i]), col_index++);
14297
14298                 memset(sql_query_string, 0x00, QUERY_SIZE);
14299                 if (reset) {
14300                         switch (rule->action_type) {
14301                         case EMAIL_FILTER_MOVE :
14302                                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = 0 ");
14303                                 break;
14304                         case EMAIL_FILTER_BLOCK:
14305                         default:
14306                                 EM_DEBUG_LOG("Not support : action_type[%d]", rule->action_type);
14307                                 ret = true;
14308                                 goto FINISH_OFF;
14309                         }
14310                 } else {
14311                         switch (rule->action_type) {
14312                         case EMAIL_FILTER_MOVE :
14313                                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = %d ", tag_id);
14314                                 break;
14315                         case EMAIL_FILTER_BLOCK :
14316                                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET mailbox_id = %d, mailbox_type = %d ", dest_mailbox_id, dest_mailbox_type);
14317                                 break;
14318                         default:
14319                                 EM_DEBUG_LOG("Not support");
14320                                 ret = true;
14321                                 goto FINISH_OFF;
14322                         }
14323                 }
14324                 /* prevent 34361 */
14325                 if(strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14326                         strcat(sql_query_string, where_pararaph);
14327
14328                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14329                 if (error != EMAIL_ERROR_NONE) {
14330                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14331                                 goto FINISH_OFF;
14332                 }
14333
14334 #ifdef __FEATURE_BODY_SEARCH__
14335                 /* Updating mail_text_tbl */
14336                 if (rule->action_type == EMAIL_FILTER_BLOCK) {
14337                         memset(sql_query_string, 0x00, QUERY_SIZE);
14338                         SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_text_tbl SET mailbox_id = %d ", dest_mailbox_id);
14339                         if(strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14340                                 strcat(sql_query_string, where_pararaph);
14341
14342                         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14343                         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14344                         if (error != EMAIL_ERROR_NONE) {
14345                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14346                                 goto FINISH_OFF;
14347                         }
14348                 }
14349 #endif
14350         }
14351
14352         ret = true;
14353
14354 FINISH_OFF:
14355
14356         if (ret) {
14357                 if (filtered_mail_id_list)
14358                         *filtered_mail_id_list = mail_list;
14359
14360                 if (count_of_mails)
14361                         *count_of_mails = count;
14362         } else
14363                 EM_SAFE_FREE(mail_list);
14364
14365         sqlite3_free_table(result);
14366         result = NULL;
14367
14368
14369         EM_SAFE_FREE(where_pararaph);
14370
14371         if (err_code != NULL)
14372                 *err_code = error;
14373
14374         EM_DEBUG_FUNC_END("ret [%d]", ret);
14375         return ret;
14376 }
14377
14378 #define EMAIL_SLOT_UNIT 25
14379 INTERNAL_FUNC int emstorage_set_mail_slot_size(char *multi_user_name, int account_id, int mailbox_id, int new_slot_size, int transaction, int *err_code)
14380 {
14381         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);
14382         int ret = false, err = EMAIL_ERROR_NONE;
14383         int where_pararaph_length = 0;
14384         char *where_pararaph = NULL;
14385         char sql_query_string[QUERY_SIZE] = {0, };
14386         int and = 0;
14387         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14388
14389         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
14390
14391         if (new_slot_size > 0)
14392                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = %d ", new_slot_size);
14393         else if (new_slot_size == 0)
14394                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", EMAIL_SLOT_UNIT);
14395         else
14396                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", new_slot_size * -1);
14397
14398
14399         if (mailbox_id)
14400                 where_pararaph_length = 80;
14401         else
14402                 where_pararaph_length = 50;
14403
14404         if (new_slot_size == 0)
14405                 where_pararaph_length += 70;
14406
14407         where_pararaph = malloc(sizeof(char) * where_pararaph_length);
14408         if (where_pararaph == NULL) {
14409                 EM_DEBUG_EXCEPTION("Memory allocation failed for where_pararaph");
14410                 err = EMAIL_ERROR_OUT_OF_MEMORY;
14411                 goto FINISH_OFF;
14412         }
14413         memset(where_pararaph, 0x00, where_pararaph_length);
14414
14415         if (account_id > ALL_ACCOUNT) {
14416                 and = 1;
14417                 if (mailbox_id)
14418                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_id = %d ", mailbox_id);
14419                 else
14420                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d ", account_id);
14421         }
14422
14423         if (new_slot_size == 0)
14424                 SNPRINTF(where_pararaph + EM_SAFE_STRLEN(where_pararaph), where_pararaph_length - EM_SAFE_STRLEN(where_pararaph), " %s total_mail_count_on_server > mail_slot_size ", (and ? "AND" : "WHERE"));
14425
14426         if (strlen(sql_query_string) + EM_SAFE_STRLEN(where_pararaph) < QUERY_SIZE) /* prevent 34363 */
14427                 strcat(sql_query_string, where_pararaph);
14428         else {
14429                 EM_DEBUG_EXCEPTION("Query buffer overflowed !!!");
14430                 err = EMAIL_ERROR_OUT_OF_MEMORY;
14431                 goto FINISH_OFF;
14432         }
14433
14434         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14435         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14436         if (err != EMAIL_ERROR_NONE) {
14437                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
14438                         goto FINISH_OFF;
14439         }
14440
14441         ret = true;
14442
14443 FINISH_OFF:
14444         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
14445
14446         EM_SAFE_FREE(where_pararaph);
14447
14448         if (err_code != NULL)
14449                 *err_code = err;
14450
14451         EM_DEBUG_FUNC_END("ret [%d]", ret);
14452         return ret;
14453 }
14454
14455 INTERNAL_FUNC int emstorage_add_meeting_request(char *multi_user_name, int account_id, int input_mailbox_id, email_meeting_request_t* meeting_req, int transaction, int *err_code)
14456 {
14457         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);
14458
14459         if (!meeting_req || meeting_req->mail_id <= 0) {
14460                 if (meeting_req)
14461                 EM_DEBUG_EXCEPTION("mail_id[%]d", meeting_req->mail_id);
14462
14463                 if (err_code != NULL)
14464                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14465
14466                 return false;
14467         }
14468
14469         int rc = -1;
14470         int ret = false;
14471         int error = EMAIL_ERROR_NONE;
14472         DB_STMT hStmt = NULL;
14473         char sql_query_string[QUERY_SIZE] = {0, };
14474         int col_index = 0;
14475         time_t temp_unix_time = 0;
14476
14477         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14478         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14479
14480         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14481                 "INSERT INTO mail_meeting_tbl VALUES "
14482                 "( ?"           /*  mail_id */
14483                 ", ?"           /*  account_id */
14484                 ", ?"           /*  mailbox_id */
14485                 ", ?"           /*  meeting_response */
14486                 ", ?"           /*  start_time */
14487                 ", ?"           /*  end_time */
14488                 ", ?"           /*  location */
14489                 ", ?"           /*  global_object_id */
14490                 ", ?"           /*  offset */
14491                 ", ?"           /*  standard_name */
14492                 ", ?"           /*  standard_time_start_date */
14493                 ", ?"           /*  standard_biad */
14494                 ", ?"           /*  daylight_name */
14495                 ", ?"           /*  daylight_time_start_date */
14496                 ", ?"           /*  daylight_bias */
14497                 " )");
14498
14499         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14500         if (rc != SQLITE_OK)  {
14501                 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
14502                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
14503
14504                 error = EMAIL_ERROR_DB_FAILURE;
14505                 goto FINISH_OFF;
14506         }
14507
14508         col_index = 0;
14509         /*
14510         EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
14511         EM_DEBUG_LOG_SEC(">>>>> account_id[%d]", account_id);
14512         EM_DEBUG_LOG_SEC(">>>>> mailbox_name[%s]", mailbox_name);
14513         EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
14514         EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
14515         EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
14516         EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
14517         EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
14518         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
14519         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
14520         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
14521         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
14522         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
14523         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
14524         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
14525         */
14526         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->mail_id);
14527         _bind_stmt_field_data_int(hStmt, col_index++, account_id);
14528         _bind_stmt_field_data_int(hStmt, col_index++, input_mailbox_id);
14529         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
14530
14531         temp_unix_time = timegm(&(meeting_req->start_time));
14532         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14533         temp_unix_time = timegm(&(meeting_req->end_time));
14534         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14535
14536         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->location, 0, LOCATION_LEN_IN_MAIL_MEETING_TBL);
14537         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->global_object_id, 0, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
14538
14539         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
14540         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.standard_name, 0, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
14541         temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
14542         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14543         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
14544
14545         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.daylight_name, 0, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
14546         temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
14547         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14548         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
14549
14550
14551         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14552         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
14553                 ("sqlite3_step fail:%d", rc));
14554         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14555                 ("sqlite3_step fail:%d", rc));
14556
14557         ret = true;
14558
14559 FINISH_OFF:
14560         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14561         if (hStmt != NULL)  {
14562                 rc = sqlite3_finalize(hStmt);
14563                 if (rc != SQLITE_OK)  {
14564                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14565                         error = EMAIL_ERROR_DB_FAILURE;
14566                 }
14567         }
14568
14569         if (err_code != NULL)
14570                 *err_code = error;
14571
14572         EM_DEBUG_FUNC_END("ret [%d]", ret);
14573         return ret;
14574 }
14575
14576 INTERNAL_FUNC int emstorage_query_meeting_request(char *multi_user_name, const char *conditional_clause, email_meeting_request_t **output_meeting_req, int *output_result_count, int transaction)
14577 {
14578         EM_DEBUG_FUNC_BEGIN("conditional_clause[%s] output_meeting_req[%p] output_result_count[%p] transaction[%d]", conditional_clause, output_meeting_req, output_result_count, transaction);
14579
14580         int i = 0;
14581         int col_index = 0;
14582         int rc;
14583         int count = 0;
14584         int dummy = 0;
14585         int err = EMAIL_ERROR_NONE;
14586         char **result = NULL;
14587         char sql_query_string[QUERY_SIZE] = {0, };
14588         email_meeting_request_t* p_temp_meeting_req = NULL;
14589         sqlite3 *local_db_handle = NULL;
14590         time_t temp_unix_time;
14591
14592         if (conditional_clause == NULL || output_meeting_req == NULL || output_result_count == NULL) {
14593                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14594                 err = EMAIL_ERROR_INVALID_PARAM;
14595                 goto FINISH_OFF;
14596         }
14597
14598         col_index = _field_count_of_table[CREATE_TABLE_MAIL_MEETING_TBL];
14599         EM_DEBUG_LOG("col_index [%d]", col_index);
14600
14601         local_db_handle = emstorage_get_db_connection(multi_user_name);
14602
14603         EMSTORAGE_START_READ_TRANSACTION(transaction);
14604
14605         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_meeting_tbl %s", conditional_clause);
14606
14607         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14608
14609         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14610         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14611                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14612
14613         if (!count) {
14614                 EM_DEBUG_EXCEPTION("No meeting_request found...");
14615                 err = EMAIL_ERROR_DATA_NOT_FOUND;
14616                 goto FINISH_OFF;
14617         }
14618
14619         EM_DEBUG_LOG("There are [%d] meeting requests.", count);
14620         if (!(p_temp_meeting_req = (email_meeting_request_t*)em_malloc(sizeof(email_meeting_request_t) * count))) {
14621                 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
14622                 err = EMAIL_ERROR_OUT_OF_MEMORY;
14623                 goto FINISH_OFF;
14624         }
14625
14626         EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
14627
14628         for (i = 0; i < count; i++) {
14629                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].mail_id), col_index++);
14630                 _get_table_field_data_int(result, &dummy, col_index++); /* account_id. but why should this field exist in DB table? */
14631                 _get_table_field_data_int(result, &dummy, col_index++); /* mailbox_id */
14632                 _get_table_field_data_int(result, (int*)&(p_temp_meeting_req[i].meeting_response), col_index++);
14633                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* start time */
14634                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].start_time));
14635                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* end time */
14636                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].end_time));
14637                 _get_table_field_data_string(result, &p_temp_meeting_req[i].location, 1, col_index++);
14638                 _get_table_field_data_string(result, &p_temp_meeting_req[i].global_object_id, 1, col_index++);
14639                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.offset_from_GMT), col_index++);
14640                 _get_table_field_data_string_without_allocation(result, p_temp_meeting_req[i].time_zone.standard_name, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL, 1, col_index++);
14641                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14642                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.standard_time_start_date));
14643                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.standard_bias), col_index++);
14644                 _get_table_field_data_string_without_allocation(result, p_temp_meeting_req[i].time_zone.daylight_name, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL, 1, col_index++);
14645                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14646                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.daylight_time_start_date));
14647                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.daylight_bias), col_index++);
14648         }
14649
14650
14651 FINISH_OFF:
14652         if(result)
14653                 sqlite3_free_table(result);
14654
14655         if (err == EMAIL_ERROR_NONE)  {
14656                 if (p_temp_meeting_req)
14657                         *output_meeting_req = p_temp_meeting_req;
14658                 *output_result_count = count;
14659         }
14660         else
14661                 EM_SAFE_FREE(p_temp_meeting_req);
14662
14663         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14664
14665 //      sqlite3_db_release_memory(local_db_handle);
14666
14667         EM_DEBUG_FUNC_END("err [%d]", err);
14668         return err;
14669 }
14670
14671 INTERNAL_FUNC int emstorage_get_meeting_request(char *multi_user_name, int mail_id, email_meeting_request_t ** meeting_req, int transaction, int *err_code)
14672 {
14673         EM_DEBUG_FUNC_BEGIN();
14674
14675         int count = 0;
14676         int ret = false;
14677         int error = EMAIL_ERROR_NONE;
14678         char conditional_clause[QUERY_SIZE] = {0, };
14679
14680         EM_IF_NULL_RETURN_VALUE(meeting_req, false);
14681
14682
14683         SNPRINTF(conditional_clause, QUERY_SIZE, " WHERE mail_id = %d", mail_id);
14684         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
14685
14686         if((error = emstorage_query_meeting_request(multi_user_name, conditional_clause, meeting_req, &count, transaction)) != EMAIL_ERROR_NONE) {
14687                 EM_DEBUG_EXCEPTION("emstorage_query_meeting_request failed. [%d]", error);
14688                 goto FINISH_OFF;
14689         }
14690
14691         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->mail_id[%d]", (*meeting_req)->mail_id);
14692         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->meeting_response[%d]", (*meeting_req)->meeting_response);
14693         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->start_time[%s]", asctime(&((*meeting_req)->start_time)));
14694         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->end_time[%s]", asctime(&((*meeting_req)->end_time)));
14695         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->location[%s]", (*meeting_req)->location);
14696         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->global_object_id[%s]", (*meeting_req)->global_object_id);
14697         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.offset_from_GMT[%d]", (*meeting_req)->time_zone.offset_from_GMT);
14698         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_name[%s]", (*meeting_req)->time_zone.standard_name);
14699         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.standard_time_start_date)));
14700         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_bias[%d]", (*meeting_req)->time_zone.standard_bias);
14701         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_name[%s]", (*meeting_req)->time_zone.daylight_name);
14702         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.daylight_time_start_date)));
14703         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_bias[%d]", (*meeting_req)->time_zone.daylight_bias);
14704         ret = true;
14705
14706 FINISH_OFF:
14707
14708         if (err_code != NULL)
14709                 *err_code = error;
14710
14711         EM_DEBUG_FUNC_END("ret [%d]", ret);
14712         return ret;
14713 }
14714
14715 INTERNAL_FUNC int emstorage_update_meeting_request(char *multi_user_name, email_meeting_request_t* meeting_req, int transaction, int *err_code)
14716 {
14717         EM_DEBUG_FUNC_BEGIN("meeting_req[%p], transaction[%d], err_code[%p]", meeting_req, transaction, err_code);
14718
14719         int ret = false;
14720         int error = EMAIL_ERROR_NONE;
14721         int rc;
14722         DB_STMT hStmt = NULL;
14723         char sql_query_string[QUERY_SIZE] = {0, };
14724         time_t temp_unix_time = 0;
14725
14726         if (!meeting_req) {
14727                 EM_DEBUG_EXCEPTION("Invalid Parameter!");
14728                 if (err_code != NULL)
14729                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14730                 return false;
14731         }
14732
14733         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14734         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14735
14736         memset(sql_query_string, 0x00, sizeof(sql_query_string));
14737         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14738                 "UPDATE mail_meeting_tbl "
14739                 "SET "
14740                 /* "  account_id = ?, "         //  not update here, this can be changed when move or copy */
14741                 /* "  mailbox_name = ?, "               //  not update here, this can be changed when move or copy */
14742                 "  meeting_response = ?, "
14743                 "  start_time = ?, "
14744                 "  end_time = ?, "
14745                 "  location = ?, "
14746                 "  global_object_id = ?, "
14747                 "  offset = ?, "
14748                 "  standard_name = ?, "
14749                 "  standard_time_start_date = ?, "
14750                 "  standard_bias = ?, "
14751                 "  daylight_name = ?, "
14752                 "  daylight_time_start_date = ?, "
14753                 "  daylight_bias = ? "
14754                 "WHERE mail_id = %d",
14755                 meeting_req->mail_id);
14756
14757         EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
14758
14759         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14760         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14761                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14762 /*
14763         EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
14764         EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
14765         EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
14766         EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
14767         EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
14768         EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
14769         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
14770         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
14771         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
14772         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
14773         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
14774         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
14775         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
14776 */
14777         int col_index = 0;
14778
14779         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
14780         temp_unix_time = timegm(&(meeting_req->start_time));
14781         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14782         temp_unix_time = timegm(&(meeting_req->end_time));
14783         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14784         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->location, 1, LOCATION_LEN_IN_MAIL_MEETING_TBL);
14785         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->global_object_id, 1, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
14786         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
14787         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.standard_name, 1, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
14788         temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
14789         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14790         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
14791         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.daylight_name, 1, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
14792         temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
14793         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14794         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
14795
14796
14797         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14798         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
14799                 ("sqlite3_step fail:%d", rc));
14800         ret = true;
14801
14802
14803 FINISH_OFF:
14804         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14805
14806         if (hStmt != NULL)  {
14807                 rc = sqlite3_finalize(hStmt);
14808                 if (rc != SQLITE_OK)  {
14809                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14810                         error = EMAIL_ERROR_DB_FAILURE;
14811                 }
14812         }
14813
14814         if (err_code != NULL)
14815                 *err_code = error;
14816
14817         EM_DEBUG_FUNC_END("ret [%d]", ret);
14818         return ret;
14819 }
14820
14821 INTERNAL_FUNC int emstorage_delete_meeting_request(char *multi_user_name, int account_id, int mail_id, int input_mailbox_id, int transaction, int *err_code)
14822 {
14823         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);
14824
14825         if (account_id < ALL_ACCOUNT || mail_id < 0) {
14826                 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d]", account_id, mail_id);
14827
14828                 if (err_code != NULL)
14829                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14830                 return false;
14831         }
14832
14833         int ret = false;
14834         int error = EMAIL_ERROR_NONE;
14835         int and = false;
14836         char sql_query_string[QUERY_SIZE] = {0, };
14837
14838         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14839         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14840
14841         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_meeting_tbl ");
14842
14843         if (account_id != ALL_ACCOUNT) {                /*  NOT '0' means a specific account. '0' means all account */
14844                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE account_id = %d", account_id);
14845                 and = true;
14846         }
14847         if (mail_id > 0) {
14848                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s mail_id = %d", (and ? "AND" : "WHERE"), mail_id);
14849                 and = true;
14850         }
14851         if (input_mailbox_id > 0) {             /*  0 means all mailbox_id */
14852                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s mailbox_id = '%d'",  (and ? "AND" : "WHERE"), input_mailbox_id);
14853         }
14854
14855         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14856         if (error != EMAIL_ERROR_NONE) {
14857                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14858                         goto FINISH_OFF;
14859         }
14860
14861         ret = true;
14862
14863 FINISH_OFF:
14864         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14865
14866         if (err_code)
14867                 *err_code = error;
14868
14869         EM_DEBUG_FUNC_END("ret [%d]", ret);
14870         return ret;
14871 }
14872
14873
14874 INTERNAL_FUNC void emstorage_free_meeting_request(email_meeting_request_t *meeting_req)
14875 {
14876         EM_DEBUG_FUNC_BEGIN();
14877
14878         if (!meeting_req ) return;
14879
14880         EM_SAFE_FREE(meeting_req->location);
14881         EM_SAFE_FREE(meeting_req->global_object_id);
14882
14883         EM_DEBUG_FUNC_END();
14884 }
14885
14886 INTERNAL_FUNC int emstorage_get_overflowed_mail_id_list(char *multi_user_name, int account_id, int input_mailbox_id, int mail_slot_size, int **mail_id_list, int *mail_id_count, int transaction, int *err_code)
14887 {
14888         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);
14889         EM_PROFILE_BEGIN(profile_emstorage_get_overflowed_mail_id_list);
14890         char sql_query_string[QUERY_SIZE] = {0, };
14891         char **result = NULL;
14892         int rc = -1, ret = false;
14893         int error = EMAIL_ERROR_NONE;
14894         int counter = 0, col_index = 0;
14895         int result_mail_id_count = 0;
14896         int *result_mail_id_list = NULL;
14897
14898         if (input_mailbox_id <= 0 || !mail_id_list || !mail_id_count || account_id < 1) {
14899                 EM_DEBUG_EXCEPTION("Invalid Parameter");
14900                 if (err_code)
14901                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14902                 return false;
14903         }
14904
14905         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);
14906
14907         EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
14908
14909         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14910         EMSTORAGE_START_READ_TRANSACTION(transaction);
14911
14912         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_mail_id_count, 0, NULL), rc);
14913         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14914                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14915
14916         if (!result_mail_id_count) {
14917                 EM_DEBUG_LOG("No mail found...");
14918                 ret = false;
14919                 error= EMAIL_ERROR_MAIL_NOT_FOUND;
14920                 goto FINISH_OFF;
14921         }
14922
14923         EM_DEBUG_LOG("There are [%d] overflowed mails in mailbox_id [%d]", result_mail_id_count, input_mailbox_id);
14924
14925         if (!(result_mail_id_list = (int *)malloc(sizeof(int) * result_mail_id_count))) {
14926                 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
14927                 error = EMAIL_ERROR_OUT_OF_MEMORY;
14928                 sqlite3_free_table(result);
14929                 goto FINISH_OFF;
14930         }
14931
14932         memset(result_mail_id_list, 0x00, sizeof(int) * result_mail_id_count);
14933
14934         col_index = 1;
14935
14936         for (counter = 0; counter < result_mail_id_count; counter++)
14937                 _get_table_field_data_int(result, result_mail_id_list + counter, col_index++);
14938
14939         ret = true;
14940
14941 FINISH_OFF:
14942         EM_DEBUG_LOG("finish off [%d]", ret);
14943
14944         if (result)
14945                 sqlite3_free_table(result);
14946
14947         if (ret == true)  {
14948                 *mail_id_list = result_mail_id_list;
14949                 *mail_id_count = result_mail_id_count;
14950         }
14951         else
14952                 EM_SAFE_FREE(result_mail_id_list);
14953
14954         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14955
14956         if (err_code != NULL)
14957                 *err_code = error;
14958
14959         EM_PROFILE_END(profile_emstorage_get_overflowed_mail_id_list);
14960         EM_DEBUG_FUNC_END("ret [%d]", ret);
14961         return ret;
14962 }
14963
14964 INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(char *multi_user_name, int mail_id, int *thread_id, int *err_code)
14965 {
14966         EM_DEBUG_FUNC_BEGIN("mail_id[%d], thread_id[%p], err_code[%p]", mail_id, thread_id, err_code);
14967
14968         int rc = -1, ret = false;
14969         int err = EMAIL_ERROR_NONE;
14970         char sql_query_string[QUERY_SIZE] = {0, };
14971         char **result;
14972         int result_count = 0;
14973
14974         if (mail_id == 0 || thread_id == NULL) {
14975                 EM_DEBUG_EXCEPTION("Invalid Parameter");
14976                 if (err_code)
14977                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14978                 return false;
14979         }
14980
14981         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14982
14983         memset(sql_query_string, 0, QUERY_SIZE);
14984         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT thread_id FROM mail_tbl WHERE mail_id = %d", mail_id);
14985
14986         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
14987         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
14988         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
14989                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14990
14991         if (!result_count) {
14992                 EM_DEBUG_LOG("No mail found...");
14993                 ret = false;
14994                 err= EMAIL_ERROR_MAIL_NOT_FOUND;
14995                 /* sqlite3_free_table(result); */
14996                 goto FINISH_OFF;
14997         }
14998
14999         _get_table_field_data_int(result, thread_id, 1);
15000
15001         sqlite3_free_table(result);
15002
15003         ret = true;
15004
15005 FINISH_OFF:
15006
15007         if (err_code != NULL)
15008                 *err_code = err;
15009
15010         EM_DEBUG_FUNC_END("ret [%d]", ret);
15011         return ret;
15012 }
15013
15014 INTERNAL_FUNC int emstorage_update_latest_thread_mail(char *multi_user_name, int account_id, int mailbox_id, int thread_id, int *updated_thread_id, int latest_mail_id, int thread_item_count, int noti_type, int transaction, int *err_code)
15015 {
15016         EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id [%d], thread_id[%d], updated_thread_id[%p], latest_mail_id [%d], thread_item_count[%d], err_code[%p]", account_id, mailbox_id, thread_id, updated_thread_id, latest_mail_id, thread_item_count, err_code);
15017
15018         int rc = -1, ret = false;
15019         int err = EMAIL_ERROR_NONE;
15020         char sql_query_string[QUERY_SIZE] = {0, };
15021         char **result = NULL;
15022         int result_count = 0;
15023
15024         if (thread_id == 0) {
15025                 EM_DEBUG_EXCEPTION("Invalid Parameter");
15026                 if (err_code)
15027                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15028                 return false;
15029         }
15030
15031         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15032
15033         if (thread_item_count == 0 && latest_mail_id == 0) {
15034                 memset(sql_query_string, 0, QUERY_SIZE);
15035                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id, count(*) FROM (SELECT account_id, mail_id, thread_id, mailbox_id FROM mail_tbl ORDER BY date_time) WHERE account_id = %d AND thread_id = %d AND mailbox_id = %d", account_id, thread_id, mailbox_id);
15036
15037                 /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
15038                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
15039                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
15040                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15041                 EM_DEBUG_LOG("result_count[%d]", result_count);
15042                 if (result_count == 0) {
15043                         EM_DEBUG_LOG("No mail found...");
15044                         ret = false;
15045                         if (err_code)
15046                                 *err_code =  EMAIL_ERROR_MAIL_NOT_FOUND;
15047                         sqlite3_free_table(result);
15048                         return false;
15049                 }
15050
15051                 _get_table_field_data_int(result, &latest_mail_id, 2);
15052                 _get_table_field_data_int(result, &thread_item_count, 3);
15053
15054                 EM_DEBUG_LOG("latest_mail_id[%d]", latest_mail_id);
15055                 EM_DEBUG_LOG("thread_item_count[%d]", thread_item_count);
15056
15057                 sqlite3_free_table(result);
15058         }
15059
15060         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15061
15062         if (thread_item_count < 0)
15063         {
15064                 memset(sql_query_string, 0, QUERY_SIZE);
15065                 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);
15066                 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15067                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15068                 if (err != EMAIL_ERROR_NONE) {
15069                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15070                                 goto FINISH_OFF;
15071                 }
15072         } else if (thread_id != latest_mail_id) {
15073                 /* Initialize the thread id */
15074                 memset(sql_query_string, 0, QUERY_SIZE);
15075                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = 0, thread_id = %d WHERE account_id = %d AND mailbox_id = %d AND thread_id = %d", latest_mail_id, account_id, mailbox_id, thread_id);
15076                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15077                 if (err != EMAIL_ERROR_NONE) {
15078                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15079                                 goto FINISH_OFF;
15080                 }
15081
15082                 /* update the thread item count */
15083                 memset(sql_query_string, 0, QUERY_SIZE);
15084                 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);
15085                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15086                 if (err != EMAIL_ERROR_NONE) {
15087                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15088                                 goto FINISH_OFF;
15089                 }
15090
15091
15092         } else {
15093                 memset(sql_query_string, 0, QUERY_SIZE);
15094                 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);
15095                 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15096                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15097                 if (err != EMAIL_ERROR_NONE) {
15098                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15099                                 goto FINISH_OFF;
15100                 }
15101         }
15102         ret = true;
15103
15104 FINISH_OFF:
15105         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15106
15107         if (thread_id != latest_mail_id)
15108         {
15109                 if(err == EMAIL_ERROR_NONE)
15110                 {
15111                         EM_DEBUG_LOG("noti_type[%d]", noti_type);
15112
15113                         if (latest_mail_id > 0 && thread_id > 0 && noti_type > 0) {
15114                                 char mailbox_id_str[25] = {0,};
15115                                 snprintf(mailbox_id_str, sizeof(mailbox_id_str), "%d", mailbox_id);
15116                                 if (!emcore_notify_storage_event(noti_type, thread_id, latest_mail_id, mailbox_id_str, account_id))
15117                                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_event failed [NOTI_THREAD_ID_CHANGED] >>>> ");
15118
15119                                 if (updated_thread_id) *updated_thread_id = latest_mail_id;
15120                         }
15121                 }
15122         }else if(thread_item_count >= 0)
15123         {
15124                 if(err == EMAIL_ERROR_NONE)
15125                 {
15126                         char parameter_string[500] = {0,};
15127                         SNPRINTF(parameter_string, sizeof(parameter_string), "%s%c%d", "thread_item_count", 0x01, latest_mail_id);
15128                         if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT, parameter_string, thread_item_count))
15129                                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event failed [NOTI_MAIL_FIELD_UPDATE] >>>> ");
15130                 }
15131         }
15132
15133         if (err_code != NULL)
15134                 *err_code = err;
15135
15136         EM_DEBUG_FUNC_END("ret [%d]", ret);
15137         return ret;
15138 }
15139
15140 INTERNAL_FUNC int emstorage_update_thread_id_of_mail(char *multi_user_name, int account_id, int mailbox_id, int mail_id, int thread_id, int thread_item_count, int transaction, int *err_code)
15141 {
15142         EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id [%d], mail_id[%d], thread_id[%d], thread_item_count[%d], err_code[%p]", account_id, mailbox_id, mail_id, thread_id, thread_item_count, err_code);
15143
15144         int ret = false;
15145         int err = EMAIL_ERROR_NONE;
15146         char sql_query_string[QUERY_SIZE] = {0, };
15147
15148         if (thread_id == 0) {
15149                 EM_DEBUG_EXCEPTION("Invalid Parameter");
15150                 if (err_code)
15151                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15152                 return false;
15153         }
15154
15155         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15156
15157         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15158
15159         memset(sql_query_string, 0, QUERY_SIZE);
15160         SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = %d, thread_id = %d WHERE account_id = %d AND mail_id = %d", thread_item_count, thread_id, account_id, mail_id);
15161         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15162         if (err != EMAIL_ERROR_NONE) {
15163                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15164                 goto FINISH_OFF;
15165         }
15166
15167         ret = true;
15168
15169 FINISH_OFF:
15170         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15171
15172         if (err_code != NULL)
15173                 *err_code = err;
15174
15175         EM_DEBUG_FUNC_END("ret [%d]", ret);
15176         return ret;
15177 }
15178
15179 #ifdef __FEATURE_LOCAL_ACTIVITY__
15180 /**
15181   *     emstorage_add_activity - Add Email Local activity during OFFLINE mode
15182   *
15183   */
15184 INTERNAL_FUNC int emstorage_add_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15185 {
15186         EM_DEBUG_FUNC_BEGIN();
15187
15188         EM_DEBUG_LOG(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15189
15190         int rc = -1, ret = false;
15191         int error = EMAIL_ERROR_NONE;
15192         DB_STMT hStmt = NULL;
15193         char sql_query_string[8192] = { 0x00, };
15194         int i = 0;
15195
15196         if (!local_activity) {
15197                 EM_DEBUG_EXCEPTION(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15198                 if (err_code != NULL)
15199                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15200                 return false;
15201         }
15202
15203         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15204
15205         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15206
15207         memset(sql_query_string, 0x00 , sizeof(sql_query_string));
15208         SNPRINTF(sql_query_string, sizeof(sql_query_string), "INSERT INTO mail_local_activity_tbl VALUES (?, ?, ?, ?, ?, ?, ?)");
15209
15210         EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ] ", local_activity->activity_id);
15211         EM_DEBUG_LOG(">>>>> MAIL ID [ %d ] ", local_activity->mail_id);
15212         EM_DEBUG_LOG(">>>>> ACCOUNT ID [ %d ] ", local_activity->account_id);
15213         EM_DEBUG_LOG(">>>>> ACTIVITY TYPE [ %d ] ", local_activity->activity_type);
15214         EM_DEBUG_LOG_SEC(">>>>> SERVER MAIL ID [ %s ] ", local_activity->server_mailid);
15215         EM_DEBUG_LOG(">>>>> SOURCE MAILBOX [ %s ] ", local_activity->src_mbox);
15216         EM_DEBUG_LOG(">>>>> DEST MAILBOX   [ %s ] ", local_activity->dest_mbox);
15217
15218         EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15219
15220
15221         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15222         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15223                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15224
15225         EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15226
15227         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
15228         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
15229         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
15230         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
15231         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->server_mailid, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
15232         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->src_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15233         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->dest_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15234
15235
15236         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15237
15238         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
15239                 ("sqlite3_step fail:%d", rc));
15240         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
15241                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
15242
15243         ret = true;
15244
15245 FINISH_OFF:
15246         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15247
15248         if (hStmt != NULL)  {
15249                 rc = sqlite3_finalize(hStmt);
15250                 if (rc != SQLITE_OK)  {
15251                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15252                         error = EMAIL_ERROR_DB_FAILURE;
15253                 }
15254         }
15255
15256         if (err_code != NULL)
15257                 *err_code = error;
15258
15259         EM_DEBUG_FUNC_END("ret [%d]", ret);
15260         return ret;
15261 }
15262
15263 /**
15264   *     emstorage_get_activity - Get the Local activity Information
15265   *
15266   *
15267   */
15268 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)
15269 {
15270         EM_DEBUG_FUNC_BEGIN();
15271
15272         int i = 0, count = 0, rc = -1, ret = false;
15273         int error = EMAIL_ERROR_NONE;
15274         emstorage_activity_tbl_t *p_activity_tbl = NULL;
15275         char sql_query_string[1024] = {0x00, };
15276         char **result = NULL;
15277         int col_index ;
15278
15279         EM_IF_NULL_RETURN_VALUE(activity_list, false);
15280         EM_IF_NULL_RETURN_VALUE(select_num, false);
15281
15282
15283         if (!select_num || !activity_list || account_id <= 0 || activityid < 0) {
15284                 EM_DEBUG_LOG(" select_num[%p], activity_list[%p] account_id [%d] activityid [%d] ", select_num, activity_list, account_id, activityid);
15285                 if (err_code)
15286                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15287                 return false;
15288         }
15289
15290         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15291
15292         EMSTORAGE_START_READ_TRANSACTION(transaction);
15293
15294         memset(sql_query_string, 0x00, sizeof(sql_query_string));
15295
15296         if (activityid == ALL_ACTIVITIES) {
15297                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d order by activity_id", account_id);
15298         }
15299         else {
15300                 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);
15301         }
15302
15303         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
15304
15305
15306
15307         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15308         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
15309                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15310
15311         col_index = 7;
15312
15313         if (!(p_activity_tbl = (emstorage_activity_tbl_t*)em_malloc(sizeof(emstorage_activity_tbl_t) * count))) {
15314                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
15315                 error = EMAIL_ERROR_OUT_OF_MEMORY;
15316                 goto FINISH_OFF;
15317         }
15318
15319
15320         for (i = 0; i < count; i++)  {
15321                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15322                 if (result[col_index])
15323                         p_activity_tbl[i].activity_id = atoi(result[col_index++]);
15324
15325                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15326                 if (result[col_index])
15327                         p_activity_tbl[i].account_id = atoi(result[col_index++]);
15328
15329                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15330                 if (result[col_index])
15331                         p_activity_tbl[i].mail_id = atoi(result[col_index++]);
15332
15333                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15334                 if (result[col_index])
15335                         p_activity_tbl[i].activity_type = atoi(result[col_index++]);
15336
15337
15338                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15339                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index])>0)
15340                         p_activity_tbl[i].server_mailid = EM_SAFE_STRDUP(result[col_index++]);
15341                 else
15342                         col_index++;
15343
15344                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15345                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index])>0)
15346                         p_activity_tbl[i].src_mbox = EM_SAFE_STRDUP(result[col_index++]);
15347                 else
15348                         col_index++;
15349
15350                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15351                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index])>0)
15352                         p_activity_tbl[i].dest_mbox = EM_SAFE_STRDUP(result[col_index++]);
15353                 else
15354                         col_index++;
15355
15356         }
15357
15358         if (result)
15359                 sqlite3_free_table(result);
15360
15361         ret = true;
15362
15363 FINISH_OFF:
15364         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15365
15366         if (ret == true)  {
15367                 *activity_list = p_activity_tbl;
15368                 *select_num = count;
15369                 EM_DEBUG_LOG(">>>> COUNT : %d >> ", count);
15370         }
15371         else if (p_activity_tbl != NULL) {
15372                 emstorage_free_local_activity(&p_activity_tbl, count, NULL);
15373         }
15374
15375
15376         if (err_code != NULL)
15377                 *err_code = error;
15378
15379         EM_DEBUG_FUNC_END("ret [%d]", ret);
15380         return ret;
15381 }
15382
15383
15384 INTERNAL_FUNC int emstorage_get_next_activity_id(int *activity_id, int *err_code)
15385 {
15386
15387         EM_DEBUG_FUNC_BEGIN();
15388
15389         int ret = false;
15390         int err = EMAIL_ERROR_NONE;
15391         int rc = -1;
15392         char *sql = NULL;
15393         char **result = NULL;
15394
15395         if (NULL == activity_id) {
15396                 EM_DEBUG_EXCEPTION(" activity_id[%p]", activity_id);
15397                 if (err_code)
15398                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15399                 return false;
15400         }
15401
15402         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15403
15404         /*  increase unique id */
15405
15406         sql = "SELECT max(rowid) FROM mail_local_activity_tbl;";
15407
15408         /*  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); */
15409         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
15410                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
15411
15412         if (NULL==result[1])
15413                 rc = 1;
15414         else
15415                 rc = atoi(result[1])+1;
15416
15417         *activity_id = rc;
15418
15419         if (result)
15420                 sqlite3_free_table(result);
15421
15422         ret = true;
15423
15424         FINISH_OFF:
15425
15426         if (err_code)
15427                 *err_code = err;
15428
15429         EM_DEBUG_FUNC_END("ret [%d]", ret);
15430         return ret;
15431
15432 }
15433
15434 INTERNAL_FUNC int emstorage_get_activity_id_list(char *multi_user_name, int account_id, int ** activity_id_list, int *activity_id_count, int lowest_activity_type, int highest_activity_type, int transaction, int *err_code)
15435 {
15436
15437         EM_DEBUG_FUNC_BEGIN();
15438
15439         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);
15440
15441         if (account_id <= 0|| NULL == activity_id_list || NULL == activity_id_count ||lowest_activity_type <=0 || highest_activity_type <= 0)  {
15442                 if (err_code != NULL)
15443                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15444                 return false;
15445         }
15446
15447         int ret = false;
15448         int error = EMAIL_ERROR_NONE;
15449         int i = 0, rc = -1, count = 0;
15450         char sql_query_string[1024] = {0x00, };
15451         int *activity_ids = NULL;
15452         int col_index = 0;
15453         char **result = NULL;
15454         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15455         EMSTORAGE_START_READ_TRANSACTION(transaction);
15456
15457         memset(sql_query_string, 0x00, sizeof(sql_query_string));
15458
15459         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);
15460
15461         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
15462
15463
15464         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15465         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
15466                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15467
15468         col_index = 1;
15469
15470         EM_DEBUG_LOG(" Activity COUNT : %d ... ", count);
15471
15472         if (NULL == (activity_ids = (int *)em_malloc(sizeof(int) * count))) {
15473                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
15474                 error = EMAIL_ERROR_OUT_OF_MEMORY;
15475                 goto FINISH_OFF;
15476         }
15477
15478         for (i = 0; i < count; i++)  {
15479                 activity_ids[i] = atoi(result[col_index]);
15480                 col_index++;
15481                 EM_DEBUG_LOG("activity_id %d", activity_ids[i]);
15482         }
15483
15484         ret = true;
15485
15486 FINISH_OFF:
15487
15488
15489         if (ret == true)  {
15490                 *activity_id_count = count;
15491                 *activity_id_list = activity_ids;
15492
15493         }
15494         else if (activity_ids != NULL) /* Prevent defect - 216566 */
15495                 EM_SAFE_FREE(activity_ids);
15496
15497
15498         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15499         if (err_code != NULL) {
15500                 *err_code = error;
15501         }
15502
15503         EM_DEBUG_FUNC_END("ret [%d]", ret);
15504         return ret;
15505 }
15506
15507 INTERNAL_FUNC int emstorage_free_activity_id_list(int *activity_id_list, int *error_code)
15508 {
15509         EM_DEBUG_FUNC_BEGIN();
15510
15511         int error = EMAIL_ERROR_NONE;
15512         int ret = false;
15513
15514         EM_DEBUG_LOG(" activity_id_list [%p]", activity_id_list);
15515
15516         if (NULL == activity_id_list) {
15517                 error = EMAIL_ERROR_INVALID_PARAM;
15518                 goto FINISH_OFF;
15519         }
15520         else {
15521                 EM_SAFE_FREE (activity_id_list);
15522         }
15523
15524
15525         ret= true;
15526
15527         FINISH_OFF:
15528
15529         if (NULL != error_code) {
15530                 *error_code = error;
15531         }
15532
15533         EM_DEBUG_FUNC_END("ret [%d]", ret);
15534         return ret;
15535 }
15536
15537 /**
15538  * emstorage_delete_local_activity - Deletes the Local acitivity Generated based on activity_type
15539  * or based on server mail id
15540  *
15541  */
15542 INTERNAL_FUNC int emstorage_delete_local_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15543 {
15544         EM_DEBUG_FUNC_BEGIN();
15545
15546
15547         EM_DEBUG_LOG(" local_activity[%p] ", local_activity);
15548
15549         if (!local_activity)  {
15550                 EM_DEBUG_EXCEPTION(" local_activity[%p] ", local_activity);
15551                 if (err_code != NULL)
15552                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15553                 return false;
15554         }
15555
15556         int rc = -1, ret = false;                       /* Prevent_FIX  */
15557         int err = EMAIL_ERROR_NONE;
15558         int query_and = 0;
15559         int query_where = 0;
15560         char sql_query_string[8192] = { 0x00, };
15561         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15562         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15563
15564         memset(sql_query_string, 0x00, sizeof(sql_query_string));
15565
15566         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_local_activity_tbl ");
15567
15568         EM_DEBUG_LOG_SEC(">>> Query [ %s ] ", sql_query_string);
15569
15570         if (local_activity->account_id) {
15571                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15572                 " WHERE account_id = %d ", local_activity->account_id);
15573                 query_and = 1;
15574                 query_where = 1;
15575         }
15576
15577         if (local_activity->server_mailid) {
15578                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15579                 " %s %s server_mailid = '%s' ", query_where? "": "WHERE", query_and? "AND":"", local_activity->server_mailid);
15580                 query_and = 1;
15581                 query_where = 1;
15582         }
15583
15584
15585         if (local_activity->mail_id) {
15586                 EM_DEBUG_LOG(">>>> MAIL ID [ %d ] , ACTIVITY TYPE [%d ]", local_activity->mail_id, local_activity->activity_type);
15587
15588                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15589                 " %s %s mail_id = %d  ", query_where? "": "WHERE", query_and? "AND":"", local_activity->mail_id);
15590
15591                 query_and = 1;
15592                 query_where = 1;
15593
15594         }
15595
15596         if (local_activity->activity_type > 0) {
15597                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15598                 " %s %s activity_type = %d ", query_where? "": "WHERE", query_and? "AND" : "" , local_activity->activity_type);
15599         }
15600
15601         if (local_activity->activity_id > 0) {
15602                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15603                 " %s %s activity_id = %d ", query_where? "": "WHERE", query_and? "AND" : "" , local_activity->activity_id);
15604
15605         }
15606
15607         EM_DEBUG_LOG_SEC(">>>>> Query [ %s ] ", sql_query_string);
15608         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15609         if (error != EMAIL_ERROR_NONE) {
15610                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
15611                         goto FINISH_OFF;
15612         }
15613
15614         rc = sqlite3_changes(local_db_handle);
15615         if (rc == 0)  {
15616                 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
15617                 err = EMAIL_ERROR_MAILBOX_NOT_FOUND;
15618         }
15619
15620         ret = true;
15621
15622 FINISH_OFF:
15623         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15624
15625         if (hStmt != NULL)  {
15626                 rc = sqlite3_finalize(hStmt);
15627                 if (rc != SQLITE_OK) {
15628                         EM_DEBUG_EXCEPTION ("sqlite3_finalize error [%d]", rc);
15629                         error = EMAIL_ERROR_DB_FAILURE;
15630                 }
15631         }
15632
15633         if (err_code != NULL)
15634                 *err_code = err;
15635         EM_DEBUG_FUNC_END("ret [%d]", ret);
15636         return ret;
15637 }
15638
15639 /**
15640 *       emstorage_free_local_activity - Free the Local Activity data
15641 */
15642 INTERNAL_FUNC int emstorage_free_local_activity(emstorage_activity_tbl_t **local_activity_list, int count, int *err_code)
15643 {
15644         EM_DEBUG_FUNC_BEGIN();
15645
15646         EM_DEBUG_LOG(" local_activity_list[%p], count[%d], err_code[%p]", local_activity_list, count, err_code);
15647
15648         int ret = false;
15649         int error = EMAIL_ERROR_INVALID_PARAM;
15650
15651         if (count > 0) {
15652                 if (!local_activity_list || !*local_activity_list) {
15653                         EM_DEBUG_EXCEPTION(" local_activity_list[%p], count[%d]", local_activity_list, count);
15654
15655                         error = EMAIL_ERROR_INVALID_PARAM;
15656                         goto FINISH_OFF;
15657                 }
15658
15659                 emstorage_activity_tbl_t* p = *local_activity_list;
15660                 int i = 0;
15661                 if (p) {
15662                         for (; i < count; i++)  {
15663                                 EM_SAFE_FREE (p[i].dest_mbox);
15664                                 EM_SAFE_FREE (p[i].src_mbox);
15665                                 EM_SAFE_FREE (p[i].server_mailid);
15666                         }
15667
15668                         EM_SAFE_FREE (p);
15669                         *local_activity_list = NULL;
15670                 }
15671         }
15672
15673         ret = true;
15674
15675 FINISH_OFF:
15676         if (err_code != NULL)
15677                 *err_code = error;
15678
15679         EM_DEBUG_FUNC_END("ret [%d]", ret);
15680         return ret;
15681
15682 }
15683 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
15684
15685
15686 static int _get_key_value_string_for_list_filter_rule(email_list_filter_rule_t *input_list_filter_rule,char **output_key_value_string)
15687 {
15688         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_key_value_string [%p]", input_list_filter_rule, output_key_value_string);
15689
15690         int  ret = EMAIL_ERROR_NONE;
15691         char key_value_string[QUERY_SIZE] = { 0, };
15692         char *temp_key_value_1 = NULL;
15693         char *temp_key_value_2 = NULL;
15694
15695         if(input_list_filter_rule == NULL || output_key_value_string == NULL) {
15696                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15697                 return EMAIL_ERROR_INVALID_PARAM;
15698         }
15699
15700         switch(input_list_filter_rule->target_attribute) {
15701         case EMAIL_MAIL_ATTRIBUTE_MAIL_ID                 :
15702         case EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID              :
15703         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID              :
15704         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE            :
15705         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_STATUS      :
15706         case EMAIL_MAIL_ATTRIBUTE_REFERENCE_MAIL_ID       :
15707         case EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS    :
15708         case EMAIL_MAIL_ATTRIBUTE_MAIL_SIZE               :
15709         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN         :
15710         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML          :
15711         case EMAIL_MAIL_ATTRIBUTE_FILE_SIZE               :
15712         case EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD        :
15713         case EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD     :
15714         case EMAIL_MAIL_ATTRIBUTE_FLAGS_FLAGGED_FIELD     :
15715         case EMAIL_MAIL_ATTRIBUTE_FLAGS_ANSWERED_FIELD    :
15716         case EMAIL_MAIL_ATTRIBUTE_FLAGS_RECENT_FIELD      :
15717         case EMAIL_MAIL_ATTRIBUTE_FLAGS_DRAFT_FIELD       :
15718         case EMAIL_MAIL_ATTRIBUTE_FLAGS_FORWARDED_FIELD   :
15719         case EMAIL_MAIL_ATTRIBUTE_DRM_STATUS              :
15720         case EMAIL_MAIL_ATTRIBUTE_PRIORITY                :
15721         case EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS             :
15722         case EMAIL_MAIL_ATTRIBUTE_LOCK_STATUS             :
15723         case EMAIL_MAIL_ATTRIBUTE_REPORT_STATUS           :
15724         case EMAIL_MAIL_ATTRIBUTE_ATTACHMENT_COUNT        :
15725         case EMAIL_MAIL_ATTRIBUTE_INLINE_CONTENT_COUNT    :
15726         case EMAIL_MAIL_ATTRIBUTE_THREAD_ID               :
15727         case EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT       :
15728         case EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS  :
15729         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_CLASS           :
15730         case EMAIL_MAIL_ATTRIBUTE_DIGEST_TYPE             :
15731         case EMAIL_MAIL_ATTRIBUTE_SMIME_TYPE              :
15732         case EMAIL_MAIL_ATTRIBUTE_REMAINING_RESEND_TIMES  :
15733         case EMAIL_MAIL_ATTRIBUTE_TAG_ID                  :
15734         case EMAIL_MAIL_ATTRIBUTE_EAS_DATA_LENGTH_TYPE    :
15735                 SNPRINTF(key_value_string, QUERY_SIZE, "%d", input_list_filter_rule->key_value.integer_type_value);
15736                 break;
15737
15738         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME            :
15739         case EMAIL_MAIL_ATTRIBUTE_SUBJECT                 :
15740         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME     :
15741         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID          :
15742         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID              :
15743         case EMAIL_MAIL_ATTRIBUTE_FROM                    :
15744         case EMAIL_MAIL_ATTRIBUTE_TO                      :
15745         case EMAIL_MAIL_ATTRIBUTE_CC                      :
15746         case EMAIL_MAIL_ATTRIBUTE_BCC                     :
15747         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT            :
15748                 if(input_list_filter_rule->key_value.string_type_value == NULL) {
15749                         EM_DEBUG_EXCEPTION("Invalid string_type_value [%p]", input_list_filter_rule->key_value.string_type_value);
15750                         ret = EMAIL_ERROR_INVALID_PARAM;
15751                         goto FINISH_OFF;
15752                 }
15753
15754                 temp_key_value_1 = input_list_filter_rule->key_value.string_type_value;
15755
15756                 temp_key_value_2 = em_replace_all_string(temp_key_value_1, "_", "\\_");
15757                 temp_key_value_1 = em_replace_all_string(temp_key_value_2, "%", "\\%");
15758
15759                 if(input_list_filter_rule->rule_type == EMAIL_LIST_FILTER_RULE_INCLUDE)
15760                         SNPRINTF(key_value_string, QUERY_SIZE, "\'%%%s%%\'", temp_key_value_1);
15761                 else
15762                         SNPRINTF(key_value_string, QUERY_SIZE, "\'%s\'", temp_key_value_1);
15763                 break;
15764
15765         case EMAIL_MAIL_ATTRIBUTE_DATE_TIME               :
15766         case EMAIL_MAIL_ATTRIBUTE_SCHEDULED_SENDING_TIME  :
15767         case EMAIL_MAIL_ATTRIBUTE_REPLIED_TIME            :
15768         case EMAIL_MAIL_ATTRIBUTE_FORWARDED_TIME          :
15769                 SNPRINTF(key_value_string, QUERY_SIZE, "%d", (int)input_list_filter_rule->key_value.datetime_type_value);
15770                 break;
15771
15772         default :
15773                 ret = EMAIL_ERROR_INVALID_PARAM;
15774                 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
15775                 break;
15776         }
15777
15778         if(ret == EMAIL_ERROR_NONE && EM_SAFE_STRLEN(key_value_string) > 0) {
15779                 *output_key_value_string = strdup(key_value_string);
15780         }
15781
15782 FINISH_OFF:
15783
15784         EM_SAFE_FREE(temp_key_value_1);
15785         EM_SAFE_FREE(temp_key_value_2);
15786
15787         EM_DEBUG_FUNC_END("ret [%d]", ret);
15788         return ret;
15789 }
15790
15791 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15792 static int _get_cert_password_file_name(int index, char *cert_password_file_name)
15793 {
15794         EM_DEBUG_FUNC_BEGIN("index : [%d]", index);
15795
15796         if (index <= 0 || !cert_password_file_name) {
15797                 EM_DEBUG_EXCEPTION("Invalid parameter");
15798                 return EMAIL_ERROR_INVALID_PARAM;
15799         }
15800
15801         sprintf(cert_password_file_name, ".email_cert_%d", index);
15802
15803         EM_DEBUG_FUNC_END();
15804         return EMAIL_ERROR_NONE;
15805 }
15806 #endif
15807
15808 static int _make_filter_rule_string(email_list_filter_rule_t *input_list_filter_rule, char **output_string)
15809 {
15810         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
15811
15812         int   ret = EMAIL_ERROR_NONE;
15813         int   is_alpha = 0;
15814         int   length_field_name = 0;
15815         int   length_value = 0;
15816         char  result_rule_string[QUERY_SIZE] = { 0 , };
15817         char *mod_field_name_string = NULL;
15818         char *mod_value_string = NULL;
15819         char *temp_field_name_string = NULL;
15820         char *temp_key_value_string = NULL;
15821
15822         if(input_list_filter_rule == NULL || output_string == NULL) {
15823                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15824                 return  EMAIL_ERROR_INVALID_PARAM;
15825         }
15826
15827         temp_field_name_string = emcore_get_mail_field_name_by_attribute_type(input_list_filter_rule->target_attribute);
15828
15829         if(temp_field_name_string == NULL) {
15830                 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
15831                 return EMAIL_ERROR_INVALID_PARAM;
15832         }
15833
15834         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) {
15835                 EM_DEBUG_EXCEPTION("_get_key_value_string_for_list_filter_rule failed");
15836                 return EMAIL_ERROR_INVALID_PARAM;
15837         }
15838
15839         length_field_name = EM_SAFE_STRLEN(temp_field_name_string);
15840         length_value      = EM_SAFE_STRLEN(temp_key_value_string);
15841
15842         switch(input_list_filter_rule->target_attribute) {
15843         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME            :
15844         case EMAIL_MAIL_ATTRIBUTE_SUBJECT                 :
15845         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME     :
15846         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID          :
15847         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID              :
15848         case EMAIL_MAIL_ATTRIBUTE_FROM                    :
15849         case EMAIL_MAIL_ATTRIBUTE_TO                      :
15850         case EMAIL_MAIL_ATTRIBUTE_CC                      :
15851         case EMAIL_MAIL_ATTRIBUTE_BCC                     :
15852         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT            :
15853                 is_alpha = 1;
15854                 break;
15855         default :
15856                 is_alpha = 0;
15857                 break;
15858         }
15859
15860         if(is_alpha == 1 && input_list_filter_rule->case_sensitivity == false) {
15861                 length_field_name += strlen("UPPER() ");
15862                 length_value      += strlen("UPPER() ");
15863                 mod_field_name_string = em_malloc(sizeof(char) * length_field_name);
15864                 if (mod_field_name_string == NULL) {
15865                         EM_DEBUG_EXCEPTION("em_malloc failed");
15866                         EM_SAFE_FREE(temp_field_name_string);
15867                         ret = EMAIL_ERROR_OUT_OF_MEMORY;
15868                         goto FINISH_OFF;
15869                 }
15870
15871                 mod_value_string = em_malloc(sizeof(char) * length_value);
15872                 if (mod_value_string == NULL) {
15873                         EM_DEBUG_EXCEPTION("em_malloc failed");
15874                         EM_SAFE_FREE(temp_field_name_string);
15875                         ret = EMAIL_ERROR_OUT_OF_MEMORY;
15876                         goto FINISH_OFF;
15877                 }
15878
15879                 SNPRINTF(mod_field_name_string, length_field_name, "UPPER(%s)", temp_field_name_string);
15880                 SNPRINTF(mod_value_string,      length_value, "UPPER(%s)", temp_key_value_string);
15881                 EM_SAFE_FREE(temp_key_value_string);
15882         }
15883         else {
15884                 mod_field_name_string = strdup(temp_field_name_string);
15885                 mod_value_string      = temp_key_value_string;
15886         }
15887
15888         switch (input_list_filter_rule->rule_type) {
15889         case EMAIL_LIST_FILTER_RULE_EQUAL     :
15890                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = %s ", mod_field_name_string, mod_value_string);
15891                 break;
15892         case EMAIL_LIST_FILTER_RULE_NOT_EQUAL :
15893                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s != %s ", mod_field_name_string, mod_value_string);
15894                 break;
15895         case EMAIL_LIST_FILTER_RULE_LESS_THAN :
15896                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s < %s ", mod_field_name_string, mod_value_string);
15897                 break;
15898         case EMAIL_LIST_FILTER_RULE_GREATER_THAN :
15899                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s > %s ", mod_field_name_string, mod_value_string);
15900                 break;
15901         case EMAIL_LIST_FILTER_RULE_LESS_THAN_OR_EQUAL :
15902                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s <= %s ", mod_field_name_string, mod_value_string);
15903                 break;
15904         case EMAIL_LIST_FILTER_RULE_GREATER_THAN_OR_EQUAL :
15905                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s >= %s ", mod_field_name_string, mod_value_string);
15906                 break;
15907         case EMAIL_LIST_FILTER_RULE_INCLUDE   :
15908                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s LIKE %s ", mod_field_name_string, mod_value_string);
15909                 break;
15910         case EMAIL_LIST_FILTER_RULE_IN        :
15911                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s IN (%s) ", mod_field_name_string, mod_value_string);
15912                 break;
15913         case EMAIL_LIST_FILTER_RULE_NOT_IN    :
15914                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s NOT IN (%s) ", mod_field_name_string, mod_value_string);
15915                 break;
15916         default :
15917                 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
15918                 ret = EMAIL_ERROR_INVALID_PARAM;
15919                 goto FINISH_OFF;
15920         }
15921
15922         *output_string = strdup(result_rule_string);
15923
15924 FINISH_OFF:
15925         EM_SAFE_FREE(mod_field_name_string);
15926         EM_SAFE_FREE(mod_value_string);
15927
15928         EM_DEBUG_FUNC_END("ret [%d]", ret);
15929         return ret;
15930 }
15931
15932 static int _make_filter_attach_rule_string(char *multi_user_name, email_list_filter_rule_attach_t *input_list_filter_rule, char **output_string)
15933 {
15934         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
15935
15936         char *field_name_string = NULL;
15937         char  key_value_string[QUERY_SIZE] = {0,};
15938         char  result_rule_string[QUERY_SIZE] = {0,};
15939         int rc = -1;
15940         int count = 0;
15941         int query_size = 0;
15942         int cur_query = 0;
15943         int col_index = 0;
15944         int error = EMAIL_ERROR_NONE;
15945         char **result = NULL;
15946         char sql_query_string[QUERY_SIZE] = {0,};
15947         char *sql_query_string2 = NULL;
15948         sqlite3 *local_db_handle = NULL;
15949         int *mail_ids = NULL;
15950
15951         if(input_list_filter_rule == NULL || output_string == NULL) {
15952                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15953                 return  EMAIL_ERROR_INVALID_PARAM;
15954         }
15955
15956         field_name_string = EM_SAFE_STRDUP("attachment_name");
15957         SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
15958
15959         switch (input_list_filter_rule->rule_type) {
15960
15961         case EMAIL_LIST_FILTER_RULE_INCLUDE:
15962                 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
15963                 break;
15964
15965         case EMAIL_LIST_FILTER_RULE_MATCH:
15966                 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
15967                 break;
15968
15969         default :
15970                 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
15971                 error = EMAIL_ERROR_INVALID_PARAM;
15972                 goto FINISH_OFF;
15973         }
15974
15975         local_db_handle = emstorage_get_db_connection(multi_user_name);
15976
15977         EMSTORAGE_START_READ_TRANSACTION(true);
15978         SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_attachment_tbl %s", result_rule_string);
15979
15980         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
15981         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF;},
15982                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15983
15984         col_index = 1;
15985
15986         if (!count) {
15987                 EM_DEBUG_LOG("No mail found...");
15988                 error= EMAIL_ERROR_MAIL_NOT_FOUND;
15989                 *output_string = strdup("mail_id IN ( ) ");
15990                 goto FINISH_OFF;
15991         }
15992
15993         EM_DEBUG_LOG_DEV (">>>> DATA ASSIGN START >>");
15994         int i = 0;
15995         if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
15996                 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
15997                 error = EMAIL_ERROR_OUT_OF_MEMORY;
15998                 goto FINISH_OFF;
15999         }
16000
16001         for (i = 0; i < count; i++) {
16002                 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16003                 EM_DEBUG_LOG_DEV (">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16004         }
16005
16006         EM_DEBUG_LOG_DEV (">>>> DATA ASSIGN END [count : %d] >>", count);
16007         sqlite3_free_table(result);
16008         EMSTORAGE_FINISH_READ_TRANSACTION(true);
16009
16010 //      sqlite3_db_release_memory(local_db_handle);
16011
16012
16013         query_size = (10 * count) + strlen("mail_id IN ( )  ");
16014
16015         sql_query_string2 = em_malloc(query_size);
16016         if (sql_query_string2 == NULL) {
16017                 EM_DEBUG_EXCEPTION("em_malloc failed");
16018                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16019                 goto FINISH_OFF;
16020         }
16021
16022         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN ( ");
16023         for (i = 0; i < count-1; i++) {
16024                 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16025         }
16026         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d ) ", mail_ids[count-1]);
16027
16028         *output_string = strdup(sql_query_string2);
16029 FINISH_OFF:
16030
16031         EM_SAFE_FREE(mail_ids); /* prevent */
16032         EM_SAFE_FREE(sql_query_string2);
16033         EM_SAFE_FREE(field_name_string);
16034         EM_DEBUG_FUNC_END("error [%d]", error);
16035         return error;
16036 }
16037
16038 static int _make_filter_fts_rule_string(char *multi_user_name, email_list_filter_rule_fts_t *input_list_filter_rule, char **output_string)
16039 {
16040         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16041         char *field_name_string = NULL;
16042         char key_value_string[QUERY_SIZE] = {0,};
16043         char  result_rule_string[QUERY_SIZE] = {0,};
16044         int rc = -1;
16045         int count = 0;
16046         int col_index = 0;
16047         int query_size = 0;
16048         int error = EMAIL_ERROR_NONE;
16049         char **result = NULL;
16050         char sql_query_string[QUERY_SIZE] = {0,};
16051         char *sql_query_string2 = NULL;
16052         sqlite3 *local_db_handle = NULL;
16053         int *mail_ids = NULL;
16054
16055         if(input_list_filter_rule == NULL || output_string == NULL) {
16056                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16057                 return  EMAIL_ERROR_INVALID_PARAM;
16058         }
16059
16060         field_name_string = EM_SAFE_STRDUP("body_text");
16061         SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
16062
16063         switch (input_list_filter_rule->rule_type) {
16064
16065         case EMAIL_LIST_FILTER_RULE_INCLUDE:
16066                 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
16067                 break;
16068
16069         case EMAIL_LIST_FILTER_RULE_MATCH:
16070                 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
16071                 break;
16072
16073         default :
16074                 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16075                 error = EMAIL_ERROR_INVALID_PARAM;
16076                 goto FINISH_OFF;
16077         }
16078
16079         local_db_handle = emstorage_get_db_connection(multi_user_name);
16080
16081         EMSTORAGE_START_READ_TRANSACTION(true);
16082         SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_text_tbl %s", result_rule_string);
16083
16084         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16085         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF;},
16086                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16087
16088         col_index = 1;
16089
16090         if (!count) {
16091                 EM_DEBUG_LOG("No mail found...");
16092                 error= EMAIL_ERROR_MAIL_NOT_FOUND;
16093                 *output_string = strdup("mail_id IN ( ) ");
16094                 goto FINISH_OFF;
16095         }
16096
16097         EM_DEBUG_LOG_DEV (">>>> DATA ASSIGN START >>");
16098         int i = 0;
16099
16100         if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
16101                 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
16102                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16103                 goto FINISH_OFF;
16104         }
16105
16106         for (i = 0; i < count; i++) {
16107                 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16108                 EM_DEBUG_LOG_DEV (">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16109         }
16110
16111         EM_DEBUG_LOG_DEV (">>>> DATA ASSIGN END [count : %d] >>", count);
16112         sqlite3_free_table(result);
16113         EMSTORAGE_FINISH_READ_TRANSACTION(true);
16114
16115 //      sqlite3_db_release_memory(local_db_handle);
16116
16117
16118         query_size = (10 * count) + strlen("mail_id IN ( )  ");
16119         sql_query_string2 = em_malloc(query_size);
16120         if (sql_query_string2 == NULL) {
16121                 EM_DEBUG_EXCEPTION("em_malloc failed");
16122                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16123                 goto FINISH_OFF;
16124         }
16125         int cur_query = 0;
16126         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN ( ");
16127         for (i = 0; i < count-1; i++) {
16128                 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16129         }
16130         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d ) ", mail_ids[count-1]);
16131
16132         *output_string = strdup(sql_query_string2);
16133
16134 FINISH_OFF:
16135         EM_SAFE_FREE(mail_ids); /* prevent */
16136         EM_SAFE_FREE(sql_query_string2);
16137         EM_SAFE_FREE(field_name_string);
16138         EM_DEBUG_FUNC_END("error [%d]", error);
16139         return error;
16140 }
16141
16142 static int _make_order_rule_string(char *multi_user_name, email_list_sorting_rule_t *input_sorting_rule, char **output_string) {
16143         EM_DEBUG_FUNC_BEGIN("input_sorting_rule [%p], output_string [%p]", input_sorting_rule, output_string);
16144
16145         char  result_rule_string[QUERY_SIZE] = { 0 , };
16146         int   ret = EMAIL_ERROR_NONE;
16147
16148         emstorage_account_tbl_t *account_tbl_array = NULL;
16149         int count = 0;
16150         int i = 0;
16151         char *result_str = NULL;
16152         char *tmp_str1 = NULL;
16153         char *tmp_str2 = NULL;
16154         char query_per_account[QUERY_SIZE] = { 0 , };
16155
16156         if(input_sorting_rule->force_boolean_check) {
16157                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = 0 ", emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16158         }
16159         else
16160                 EM_SAFE_STRCPY(result_rule_string, emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16161
16162         switch (input_sorting_rule->sort_order) {
16163                 case EMAIL_SORT_ORDER_ASCEND :
16164                         EM_SAFE_STRCAT(result_rule_string, " ASC ");
16165                         break;
16166
16167                 case EMAIL_SORT_ORDER_DESCEND :
16168                         EM_SAFE_STRCAT(result_rule_string, " DESC ");
16169                         break;
16170
16171                 case EMAIL_SORT_ORDER_NOCASE_ASCEND :
16172                         EM_SAFE_STRCAT(result_rule_string, " COLLATE NOCASE ASC ");
16173                         break;
16174
16175                 case EMAIL_SORT_ORDER_NOCASE_DESCEND :
16176                         EM_SAFE_STRCAT(result_rule_string, " COLLATE NOCASE DESC ");
16177                         break;
16178
16179                 case EMAIL_SORT_ORDER_TO_CCBCC :
16180                         memset(result_rule_string, 0, QUERY_SIZE);
16181                         if (input_sorting_rule->key_value.string_type_value)
16182                                 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16183                                                 " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 ELSE 2 END ",
16184                                                 input_sorting_rule->key_value.string_type_value);
16185                         break;
16186
16187                 case EMAIL_SORT_ORDER_TO_CC_BCC :
16188                         memset(result_rule_string, 0, QUERY_SIZE);
16189                         if (input_sorting_rule->key_value.string_type_value)
16190                                 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16191                                                 " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 WHEN full_address_cc LIKE \'%%%q%%\' THEN 2 ELSE 3 END ",
16192                                                 input_sorting_rule->key_value.string_type_value, input_sorting_rule->key_value.string_type_value);
16193                         break;
16194
16195                 case EMAIL_SORT_ORDER_TO_CCBCC_ALL :
16196                         if (!emstorage_get_account_list(multi_user_name, &count, &account_tbl_array, true, false, NULL)) {
16197                                 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed");
16198                                 goto FINISH_OFF;
16199                         }
16200
16201                         if (!count) {
16202                                 EM_DEBUG_LOG("No account exist");
16203                                 ret = EMAIL_ERROR_INVALID_PARAM;
16204                                 goto FINISH_OFF;
16205                         }
16206
16207                         for (i = 0; i < count; i++) {
16208                                 if (i > 0 && result_str) {
16209                                         tmp_str2 = result_str;
16210                                         result_str = g_strconcat(tmp_str2, " OR ", NULL);
16211                                         EM_SAFE_FREE(tmp_str2);
16212                                 }
16213
16214                                 memset(query_per_account, 0, QUERY_SIZE);
16215                                 snprintf(query_per_account, QUERY_SIZE,
16216                                                 "(account_id = %d AND full_address_to LIKE \'%%%s%%\')",
16217                                                 account_tbl_array[i].account_id, account_tbl_array[i].user_email_address);
16218
16219                                 tmp_str1 = result_str;
16220                                 if (tmp_str1)
16221                                         result_str = g_strconcat(tmp_str1, query_per_account, NULL);
16222                                 else
16223                                         result_str = g_strdup(query_per_account);
16224                                 EM_SAFE_FREE(tmp_str1);
16225                         }
16226
16227                         snprintf(result_rule_string, QUERY_SIZE,
16228                                         " CASE WHEN %s THEN 1 ELSE 2 END ", result_str);
16229
16230                         EM_SAFE_FREE(result_str);
16231                         if (account_tbl_array)
16232                                 emstorage_free_account(&account_tbl_array, count, NULL);
16233                         break;
16234
16235                 case EMAIL_SORT_ORDER_LOCALIZE_ASCEND :
16236                         memset(result_rule_string, 0, QUERY_SIZE);
16237                         sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16238                                 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END ASC, %s COLLATE NOCASE ASC ",
16239                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16240                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16241                         break;
16242
16243                 case EMAIL_SORT_ORDER_LOCALIZE_DESCEND :
16244                         memset(result_rule_string, 0, QUERY_SIZE);
16245                         sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16246                                 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END DESC, %s COLLATE NOCASE DESC ",
16247                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16248                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16249                         break;
16250
16251                 default :
16252                         EM_DEBUG_EXCEPTION("Invalid sort_order [%d]", input_sorting_rule->sort_order);
16253                         ret = EMAIL_ERROR_INVALID_PARAM;
16254                         goto FINISH_OFF;
16255         }
16256
16257         *output_string = strdup(result_rule_string);
16258
16259 FINISH_OFF:
16260         EM_DEBUG_FUNC_END("ret [%d]", ret);
16261         return ret;
16262 }
16263
16264 INTERNAL_FUNC int emstorage_write_conditional_clause_for_getting_mail_list(char *multi_user_name, email_list_filter_t *input_filter_list, int input_filter_count, email_list_sorting_rule_t *input_sorting_rule_list, int input_sorting_rule_count, int input_start_index, int input_limit_count, char **output_conditional_clause)
16265 {
16266         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);
16267         int ret = EMAIL_ERROR_NONE;
16268         int i = 0;
16269         int string_offset = 0;
16270         int query_size = 0;
16271         int new_query_size = 0;
16272         char *conditional_clause_string = NULL;
16273         char *result_string_for_a_item = NULL;
16274
16275         if ( (input_filter_count > 0 && !input_filter_list) || (input_sorting_rule_count > 0 && !input_sorting_rule_list) || output_conditional_clause == NULL) {
16276                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16277                 return EMAIL_ERROR_INVALID_PARAM;
16278         }
16279
16280         conditional_clause_string = em_malloc(QUERY_SIZE);
16281         if (conditional_clause_string == NULL) {
16282                 EM_DEBUG_EXCEPTION("Memory is full");
16283                 return EMAIL_ERROR_OUT_OF_MEMORY;
16284         }
16285
16286         if(input_filter_count > 0) {
16287                 query_size = QUERY_SIZE;
16288                 strcpy(conditional_clause_string,  " WHERE ");
16289
16290                 for ( i = 0; i < input_filter_count; i++) {
16291                         switch (input_filter_list[i].list_filter_item_type) {
16292                         case EMAIL_LIST_FILTER_ITEM_RULE :
16293                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE", i);
16294                                 _make_filter_rule_string(&(input_filter_list[i].list_filter_item.rule), &result_string_for_a_item);
16295                                 break;
16296
16297                         case EMAIL_LIST_FILTER_ITEM_RULE_FTS :
16298                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_FTS", i);
16299                                 _make_filter_fts_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_fts), &result_string_for_a_item);
16300                                 break;
16301
16302                         case EMAIL_LIST_FILTER_ITEM_RULE_ATTACH :
16303                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_ATTACH", i);
16304                                 _make_filter_attach_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_attach), &result_string_for_a_item);
16305                                 break;
16306
16307                         case EMAIL_LIST_FILTER_ITEM_OPERATOR :
16308                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_OPERATOR", i);
16309                                 switch(input_filter_list[i].list_filter_item.operator_type) {
16310                                 case EMAIL_LIST_FILTER_OPERATOR_AND :
16311                                         result_string_for_a_item = strdup("AND ");
16312                                         break;
16313                                 case EMAIL_LIST_FILTER_OPERATOR_OR :
16314                                         result_string_for_a_item = strdup("OR ");
16315                                         break;
16316                                 case EMAIL_LIST_FILTER_OPERATOR_LEFT_PARENTHESIS :
16317                                         result_string_for_a_item = strdup(" (");
16318                                         break;
16319                                 case EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS :
16320                                         result_string_for_a_item = strdup(") ");
16321                                         break;
16322                                 }
16323                                 break;
16324
16325                         default :
16326                                 EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", input_filter_list[i].list_filter_item_type);
16327                                 ret = EMAIL_ERROR_INVALID_PARAM;
16328                                 goto FINISH_OFF;
16329                         }
16330
16331                         if(result_string_for_a_item == NULL) {
16332                                 EM_DEBUG_EXCEPTION("result_string_for_a_item is null");
16333                                 ret = EMAIL_ERROR_INVALID_PARAM;
16334                                 goto FINISH_OFF;
16335                         }
16336
16337                         if(strlen(conditional_clause_string) + EM_SAFE_STRLEN(result_string_for_a_item) >= query_size) { /* prevent 34364 */
16338                                 EM_DEBUG_LOG("QUERY is too long");
16339                                 new_query_size = EM_SAFE_STRLEN(result_string_for_a_item) + EM_SAFE_STRLEN(conditional_clause_string) + QUERY_SIZE;
16340                                 conditional_clause_string = realloc(conditional_clause_string, new_query_size);
16341                                 if (conditional_clause_string == NULL) {
16342                                         EM_DEBUG_EXCEPTION("realloc failed");
16343                                         ret = EMAIL_ERROR_OUT_OF_MEMORY;
16344                                         goto FINISH_OFF;
16345                                 }
16346
16347                                 query_size = new_query_size;
16348                         }
16349
16350                         strcat(conditional_clause_string, result_string_for_a_item);
16351                         EM_SAFE_FREE(result_string_for_a_item);
16352                 }
16353         }
16354
16355         if(input_sorting_rule_count > 0) {
16356                 strcat(conditional_clause_string, "ORDER BY ");
16357
16358                 for ( i = 0; i < input_sorting_rule_count; i++) {
16359                         if( (ret = _make_order_rule_string(multi_user_name, &input_sorting_rule_list[i], &result_string_for_a_item)) != EMAIL_ERROR_NONE) {
16360                                 EM_DEBUG_EXCEPTION("_make_order_rule_string failed. [%d]", ret);
16361                                 goto FINISH_OFF;
16362                         }
16363                         if(i > 0)
16364                                 strcat(conditional_clause_string, ", ");
16365                         strcat(conditional_clause_string, result_string_for_a_item);
16366                         EM_SAFE_FREE(result_string_for_a_item);
16367                 }
16368         }
16369
16370         if (input_start_index != -1 && input_limit_count != -1) {
16371                 string_offset = strlen(conditional_clause_string);
16372                 SNPRINTF_OFFSET(conditional_clause_string, string_offset, query_size, " LIMIT %d, %d", input_start_index, input_limit_count);
16373         }
16374
16375         *output_conditional_clause = strdup(conditional_clause_string);
16376
16377 FINISH_OFF:
16378         EM_SAFE_FREE(result_string_for_a_item);
16379         EM_SAFE_FREE(conditional_clause_string);
16380
16381         EM_DEBUG_FUNC_END("ret [%d]", ret);
16382         return ret;
16383 }
16384
16385 INTERNAL_FUNC int emstorage_free_list_filter(email_list_filter_t **input_filter_list, int input_filter_count)
16386 {
16387         EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d]", input_filter_list, input_filter_count);
16388         int err = EMAIL_ERROR_NONE;
16389         int i = 0;
16390         email_list_filter_t *temp_filter_list = NULL;
16391
16392         EM_IF_NULL_RETURN_VALUE(input_filter_list, EMAIL_ERROR_INVALID_PARAM);
16393
16394         for ( i = 0; i < input_filter_count; i++) {
16395                 temp_filter_list = (*input_filter_list) + i;
16396                 if (!temp_filter_list) {
16397                         continue;
16398                 }
16399
16400                 if(temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE) {
16401                         switch(temp_filter_list->list_filter_item.rule.target_attribute) {
16402                         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME :
16403                         case EMAIL_MAIL_ATTRIBUTE_SUBJECT :
16404                         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME :
16405                         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID :
16406                         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID :
16407                         case EMAIL_MAIL_ATTRIBUTE_FROM :
16408                         case EMAIL_MAIL_ATTRIBUTE_TO :
16409                         case EMAIL_MAIL_ATTRIBUTE_CC :
16410                         case EMAIL_MAIL_ATTRIBUTE_BCC :
16411                         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN :
16412                         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML :
16413                         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT :
16414                                 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule.key_value.string_type_value);
16415                                 break;
16416                         default :
16417                                 break;
16418                         }
16419                 }
16420                 else if(temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE_FTS && temp_filter_list->list_filter_item.rule_fts.target_attribute == EMAIL_MAIL_TEXT_ATTRIBUTE_FULL_TEXT) {
16421                                 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_fts.key_value.string_type_value);
16422                 }
16423                 else if(temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE_ATTACH && temp_filter_list->list_filter_item.rule_attach.target_attribute == EMAIL_MAIL_ATTACH_ATTRIBUTE_ATTACHMENT_NAME) {
16424                                 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_attach.key_value.string_type_value);
16425                 }
16426         }
16427
16428         EM_SAFE_FREE (*input_filter_list);
16429
16430         EM_DEBUG_FUNC_END("err [%d]", err);
16431         return err;
16432 }
16433
16434 INTERNAL_FUNC int emstorage_add_certificate(char *multi_user_name, emstorage_certificate_tbl_t *certificate, int transaction, int *err_code)
16435 {
16436         EM_DEBUG_FUNC_BEGIN("certificate:[%p], transaction:[%d], err_code:[%p]", certificate, transaction, err_code);
16437
16438         if (!certificate) {
16439                 EM_DEBUG_EXCEPTION("certificate:[%p], transaction:[%d], err_code:[%p]", certificate, transaction, err_code);
16440                 if (err_code)
16441                         *err_code = EMAIL_ERROR_INVALID_PARAM;
16442                 return false;
16443         }
16444
16445         int rc = -1, ret = false;
16446         int error = EMAIL_ERROR_NONE;
16447         DB_STMT hStmt = NULL;
16448         char sql_query_string[QUERY_SIZE] = {0, };
16449 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
16450         char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
16451 #endif
16452
16453         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16454
16455         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
16456
16457         char *sql = "SELECT max(rowid) FROM mail_certificate_tbl;";
16458         char **result = NULL;
16459
16460         /*  rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
16461         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
16462         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
16463                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
16464
16465         if (NULL==result[1]) rc = 1;
16466         else rc = atoi(result[1])+1;
16467         sqlite3_free_table(result);
16468
16469         certificate->certificate_id = rc;
16470 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
16471         if ((error = _get_cert_password_file_name(certificate->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
16472                 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
16473                 goto FINISH_OFF;
16474         }
16475 #endif
16476         SNPRINTF(sql_query_string, sizeof(sql_query_string),
16477                 "INSERT INTO mail_certificate_tbl VALUES "
16478                 "(        "
16479                 "   ?  "                /* Index of certificate */
16480                 "  , ? "                /* Select the account */
16481                 "  , ? "                /* Year of issue */
16482                 "  , ? "                /* Month of issue */
16483                 "  , ? "                /* Day of issue */
16484                 "  , ? "                /* Year of expiration */
16485                 "  , ? "                /* Month of expiration */
16486                 "  , ? "                /* Day of expiration */
16487                 "  , ? "                /* Organization of issue */
16488                 "  , ? "                /* Email address */
16489                 "  , ? "                /* Subject of certificate */
16490                 "  , ? "                /* Name of saved certificate */
16491                 ") ");
16492
16493
16494         /*  rc = sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL); */
16495         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16496         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
16497                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16498
16499         EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
16500         _bind_stmt_field_data_int(hStmt, CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->certificate_id);
16501         _bind_stmt_field_data_int(hStmt, ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_year);
16502         _bind_stmt_field_data_int(hStmt, ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_month);
16503         _bind_stmt_field_data_int(hStmt, ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_day);
16504         _bind_stmt_field_data_int(hStmt, EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_year);
16505         _bind_stmt_field_data_int(hStmt, EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_month);
16506         _bind_stmt_field_data_int(hStmt, EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_day);
16507         _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);
16508         _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->email_address, 0, EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL);
16509         _bind_stmt_field_data_string(hStmt, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->subject_str, 0, SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL);
16510         _bind_stmt_field_data_string(hStmt, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->filepath, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
16511         /*  rc = sqlite3_step(hStmt); */
16512         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16513
16514         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
16515                 ("sqlite3_step fail:%d", rc));
16516         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
16517                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
16518
16519         ret = true;
16520
16521 FINISH_OFF:
16522         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
16523         if (hStmt != NULL)  {
16524                 rc = sqlite3_finalize(hStmt);
16525                 if (rc != SQLITE_OK)  {
16526                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16527                         error = EMAIL_ERROR_DB_FAILURE;
16528                 }
16529         }
16530
16531         if (error == EMAIL_ERROR_NONE) {
16532                 if (!emcore_notify_storage_event (NOTI_CERTIFICATE_ADD, certificate->certificate_id, 0, NULL, 0))
16533                         EM_DEBUG_EXCEPTION ("emcore_notify_storage_event(NOTI_CERTIFICATE_ADD] : Notification failed");
16534         }
16535
16536         if (err_code != NULL)
16537                 *err_code = error;
16538
16539         EM_DEBUG_FUNC_END("ret [%d]", ret);
16540         return ret;
16541 }
16542
16543 INTERNAL_FUNC int emstorage_free_certificate(emstorage_certificate_tbl_t **certificate_list, int count, int *err_code)
16544 {
16545         EM_DEBUG_FUNC_BEGIN("certificate_list[%p], count[%d], err_code[%p]", certificate_list, count, err_code);
16546
16547         int ret = false;
16548         int error = EMAIL_ERROR_NONE;
16549
16550         if (count > 0)  {
16551                 if (!certificate_list || !*certificate_list)  {
16552                         EM_DEBUG_EXCEPTION("certificate_list[%p], count[%d]", certificate_list, count);
16553                         error = EMAIL_ERROR_INVALID_PARAM;
16554                         goto FINISH_OFF;
16555                 }
16556
16557                 emstorage_certificate_tbl_t *p = *certificate_list;
16558                 int i = 0;
16559
16560                 for (; i < count; i++)  {
16561                         EM_SAFE_FREE(p[i].issue_organization_name);
16562                         EM_SAFE_FREE(p[i].email_address);
16563                         EM_SAFE_FREE(p[i].subject_str);
16564                         EM_SAFE_FREE(p[i].filepath);
16565                         EM_SAFE_FREE(p[i].password);
16566                 }
16567
16568                 EM_SAFE_FREE(p);
16569                 *certificate_list = NULL;
16570         }
16571
16572         ret = true;
16573
16574 FINISH_OFF:
16575         if (err_code != NULL)
16576                 *err_code = error;
16577
16578         EM_DEBUG_FUNC_END("ret [%d]", ret);
16579         return ret;
16580 }
16581
16582 INTERNAL_FUNC int emstorage_get_certificate_list(char *multi_user_name, int *select_num, emstorage_certificate_tbl_t **certificate_list, int transaction, int with_password, int *err_code)
16583 {
16584         EM_DEBUG_FUNC_BEGIN();
16585
16586         int i = 0, count = 0, rc = -1, ret = false;
16587         int error = EMAIL_ERROR_NONE;
16588         emstorage_certificate_tbl_t *p_data_tbl = NULL;
16589
16590         DB_STMT hStmt = NULL;
16591
16592         if (!select_num || !certificate_list)  {
16593                 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, certificate_list);
16594                 if (err_code != NULL)
16595                         *err_code = EMAIL_ERROR_INVALID_PARAM;
16596                 return false;
16597         }
16598
16599         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16600         EMSTORAGE_START_READ_TRANSACTION(transaction);
16601
16602         char sql_query_string[QUERY_SIZE] = {0, };
16603         char *sql = "SELECT count(*) FROM mail_certificate_tbl;";
16604         char **result;
16605
16606         /*  rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); */
16607         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
16608         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
16609                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
16610
16611         count = atoi(result[1]);
16612         sqlite3_free_table(result);
16613
16614         if (!count) {
16615                 EM_DEBUG_EXCEPTION("no account found...");
16616                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
16617                 ret = true;
16618                 goto FINISH_OFF;
16619         }
16620         EM_DEBUG_LOG("count = %d", rc);
16621         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_certificate_tbl ORDER BY account_id");
16622
16623         /*  rc = sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL);   */
16624         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16625
16626         EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
16627         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
16628                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16629
16630         /*  rc = sqlite3_step(hStmt); */
16631         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16632         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
16633                 ("sqlite3_step fail:%d", rc));
16634         if (rc == SQLITE_DONE)  {
16635                 EM_DEBUG_EXCEPTION("no account found...");
16636
16637                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
16638                 count = 0;
16639                 ret = true;
16640                 goto FINISH_OFF;
16641         }
16642
16643         if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t) * count)))  {
16644                 EM_DEBUG_EXCEPTION("malloc failed...");
16645                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16646                 goto FINISH_OFF;
16647         }
16648         memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t) * count);
16649         for (i = 0; i < count; i++)  {
16650                 /*  get recordset */
16651                 _get_stmt_field_data_int(hStmt,  &(p_data_tbl[i].certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
16652                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
16653                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
16654                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
16655                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
16656                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
16657                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
16658                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
16659                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
16660                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
16661                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
16662                 if (with_password == true) {
16663 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
16664                         /*  get password from the secure storage */
16665                         char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
16666
16667                         EM_SAFE_FREE(p_data_tbl[i].password);
16668
16669                         /*  get password file name */
16670                         if ((error = _get_cert_password_file_name(p_data_tbl[i].certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
16671                                 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
16672                                 goto FINISH_OFF;
16673                         }
16674
16675                         /*  read password from secure storage */
16676                         if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl[i].password))) < 0) {
16677                                 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage() failed...");
16678                                 goto FINISH_OFF;
16679                         }
16680                         EM_DEBUG_LOG_SEC("recv_password_file_name[%s], password[%s]", cert_password_file_name,  p_data_tbl[i].password);
16681 #endif
16682                 }
16683
16684                 /*  rc = sqlite3_step(hStmt); */
16685                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16686                 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i,  rc);
16687                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
16688                         ("sqlite3_step fail:%d", rc));
16689         }
16690
16691         ret = true;
16692
16693 FINISH_OFF:
16694         if (ret == true)  {
16695                 *certificate_list = p_data_tbl;
16696                 *select_num = count;
16697                 EM_DEBUG_LOG("COUNT : %d", count);
16698         }
16699         else if (p_data_tbl != NULL)
16700                 emstorage_free_certificate(&p_data_tbl, count, NULL);
16701         if (hStmt != NULL)  {
16702                 rc = sqlite3_finalize(hStmt);
16703                 hStmt = NULL;
16704                 if (rc != SQLITE_OK)  {
16705                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16706                         error = EMAIL_ERROR_DB_FAILURE;
16707                 }
16708         }
16709
16710         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
16711
16712         if (err_code != NULL)
16713                 *err_code = error;
16714
16715         EM_DEBUG_FUNC_END("ret [%d]", ret);
16716         return ret;
16717 }
16718
16719 INTERNAL_FUNC int emstorage_get_certificate_by_email_address(char *multi_user_name, char *email_address, emstorage_certificate_tbl_t **certificate, int transaction, int with_password, int *err_code)
16720 {
16721         EM_DEBUG_FUNC_BEGIN_SEC("email_address[%s], certificate[%p], transaction[%d], err_code[%p]", email_address, certificate, transaction, err_code);
16722
16723         if (!certificate)  {
16724                 EM_DEBUG_EXCEPTION_SEC("email_address[%s], certificate[%p]", email_address, certificate);
16725                 if (err_code != NULL)
16726                         *err_code = EMAIL_ERROR_INVALID_PARAM;
16727                 return false;
16728         }
16729
16730         int ret = false;
16731         int error = EMAIL_ERROR_NONE;
16732         emstorage_certificate_tbl_t *p_data_tbl = NULL;
16733         DB_STMT hStmt = NULL;
16734         char sql_query_string[QUERY_SIZE] = {0, };
16735         int rc = -1;
16736         int sql_len = 0;
16737 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
16738         char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
16739 #endif
16740
16741         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16742         EMSTORAGE_START_READ_TRANSACTION(transaction);
16743
16744         /*  Make query string */
16745         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
16746         sql_len = EM_SAFE_STRLEN(sql_query_string);
16747
16748         /*  dummy value, FROM WHERE clause */
16749         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "* FROM mail_certificate_tbl WHERE email_address = '%s'", email_address);
16750
16751         /*  FROM clause */
16752         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
16753
16754         /*  execute a sql and count rows */
16755         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16756         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
16757                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16758
16759         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16760         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
16761                 ("sqlite3_step fail:%d", rc));
16762
16763         if (rc == SQLITE_DONE)  {
16764                 EM_DEBUG_EXCEPTION("no matched certificate found...");
16765                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
16766                 goto FINISH_OFF;
16767         }
16768
16769         /*  Assign query result to structure */
16770         if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t))))  {
16771                 EM_DEBUG_EXCEPTION("malloc failed...");
16772                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16773                 goto FINISH_OFF;
16774         }
16775
16776         memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t));
16777         _get_stmt_field_data_int(hStmt,  &(p_data_tbl->certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
16778         _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
16779         _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
16780         _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
16781         _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
16782         _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
16783         _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
16784         _get_stmt_field_data_string(hStmt, &(p_data_tbl->issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
16785         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
16786         _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
16787         _get_stmt_field_data_string(hStmt, &(p_data_tbl->filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
16788
16789         if (with_password) {
16790 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
16791                 /*  get password file name */
16792                 if ((error = _get_cert_password_file_name(p_data_tbl->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
16793                         EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
16794                         goto FINISH_OFF;
16795                 }
16796
16797                 /*  read password from secure storage */
16798                 if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl->password))) != EMAIL_ERROR_NONE) {
16799                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed...");
16800                         goto FINISH_OFF;
16801                 }
16802                 EM_DEBUG_LOG_SEC("cert_password_file_name[%s], password[%s]", cert_password_file_name,  p_data_tbl->password);
16803 #endif
16804         }
16805         ret = true;
16806
16807 FINISH_OFF:
16808         if (ret == true)
16809                 *certificate = p_data_tbl;
16810
16811         if (hStmt != NULL)  {
16812                 rc = sqlite3_finalize(hStmt);
16813                 if (rc != SQLITE_OK)  {
16814                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16815                         error = EMAIL_ERROR_DB_FAILURE;
16816                 }
16817         }
16818
16819         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
16820
16821         if (err_code != NULL)
16822                 *err_code = error;
16823
16824         EM_DEBUG_FUNC_END("ret [%d]", ret);
16825         return ret;
16826 }
16827
16828 INTERNAL_FUNC int emstorage_get_certificate_by_index(char *multi_user_name, int index, emstorage_certificate_tbl_t **certificate, int transaction, int with_password, int *err_code)
16829 {
16830         EM_DEBUG_FUNC_BEGIN("index[%d], certificate[%p], transaction[%d], err_code[%p]", index, certificate, transaction, err_code);
16831
16832         if (!certificate)  {
16833                 EM_DEBUG_EXCEPTION("index[%d], account[%p]", index, certificate);
16834                 if (err_code != NULL)
16835                         *err_code = EMAIL_ERROR_INVALID_PARAM;
16836                 return false;
16837         }
16838
16839         int ret = false;
16840         int error = EMAIL_ERROR_NONE;
16841         emstorage_certificate_tbl_t *p_data_tbl = NULL;
16842         DB_STMT hStmt = NULL;
16843         char sql_query_string[QUERY_SIZE] = {0, };
16844         int rc = -1;
16845         int sql_len = 0;
16846 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
16847         char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
16848 #endif
16849
16850         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16851         EMSTORAGE_START_READ_TRANSACTION(transaction);
16852
16853         /*  Make query string */
16854         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
16855         sql_len = EM_SAFE_STRLEN(sql_query_string);
16856
16857         /*  dummy value, FROM WHERE clause */
16858         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "* FROM mail_certificate_tbl WHERE certificate_id = %d", index);
16859
16860         /*  FROM clause */
16861         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
16862
16863         /*  execute a sql and count rows */
16864         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16865         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
16866                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16867
16868         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16869         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
16870                 ("sqlite3_step fail:%d", rc));
16871
16872         if (rc == SQLITE_DONE)  {
16873                 EM_DEBUG_EXCEPTION("no matched certificate found...");
16874                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
16875                 goto FINISH_OFF;
16876         }
16877
16878         /*  Assign query result to structure */
16879         if (!(p_data_tbl = (emstorage_certificate_tbl_t *)malloc(sizeof(emstorage_certificate_tbl_t))))  {
16880                 EM_DEBUG_EXCEPTION("malloc failed...");
16881                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16882                 goto FINISH_OFF;
16883         }
16884
16885         memset(p_data_tbl, 0x00, sizeof(emstorage_certificate_tbl_t));
16886
16887         _get_stmt_field_data_int(hStmt,  &(p_data_tbl->certificate_id), CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL);
16888         _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_year), ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
16889         _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_month), ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
16890         _get_stmt_field_data_int(hStmt, &(p_data_tbl->issue_day), ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
16891         _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_year), EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL);
16892         _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_month), EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL);
16893         _get_stmt_field_data_int(hStmt, &(p_data_tbl->expiration_day), EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL);
16894         _get_stmt_field_data_string(hStmt, &(p_data_tbl->issue_organization_name), 0, ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL);
16895         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address), 0, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL);
16896         _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject_str), 0, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL);
16897         _get_stmt_field_data_string(hStmt, &(p_data_tbl->filepath), 0, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL);
16898
16899         if (with_password) {
16900 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
16901                 /*  get password file name */
16902                 if ((error = _get_cert_password_file_name(p_data_tbl->certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
16903                         EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
16904                         goto FINISH_OFF;
16905                 }
16906
16907                 /*  read password from secure storage */
16908                 if ((error = _read_password_from_secure_storage(cert_password_file_name, &(p_data_tbl->password))) != EMAIL_ERROR_NONE) {
16909                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed...");
16910                         goto FINISH_OFF;
16911                 }
16912                 EM_DEBUG_LOG_SEC("cert_password_file_name[%s], password[%s]", cert_password_file_name,  p_data_tbl->password);
16913 #endif
16914         }
16915         ret = true;
16916
16917 FINISH_OFF:
16918         if (ret == true)
16919                 *certificate = p_data_tbl;
16920
16921         if (hStmt != NULL)  {
16922                 rc = sqlite3_finalize(hStmt);
16923                 if (rc != SQLITE_OK)  {
16924                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16925                         error = EMAIL_ERROR_DB_FAILURE;
16926                 }
16927         }
16928
16929         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
16930
16931         if (err_code != NULL)
16932                 *err_code = error;
16933
16934         EM_DEBUG_FUNC_END("ret [%d]", ret);
16935         return ret;
16936 }
16937
16938 INTERNAL_FUNC int emstorage_delete_certificate(char *multi_user_name, int certificate_id, int transaction, int *err_code)
16939 {
16940         EM_DEBUG_FUNC_BEGIN("certificate_id[%d], transaction[%d], err_code[%p]", certificate_id, transaction, err_code);
16941
16942         if (certificate_id < 1)  {
16943                 EM_DEBUG_EXCEPTION(" certificate_id[%d]", certificate_id);
16944                 if (err_code)
16945                         *err_code = EMAIL_ERROR_INVALID_PARAM;
16946                 return false;
16947         }
16948
16949         int rc = -1, ret = false;
16950         int error = EMAIL_ERROR_NONE;
16951         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16952         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
16953
16954         /*  TODO : delete password files - file names can be obtained from db or a rule that makes a name */
16955         char sql_query_string[QUERY_SIZE] = {0, };
16956 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
16957         char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
16958         /*  get password file name */
16959         if ((error = _get_cert_password_file_name(certificate_id, cert_password_file_name)) != EMAIL_ERROR_NONE) {
16960                 EM_DEBUG_EXCEPTION("em_storage_get_password_file_name failed.");
16961                 goto FINISH_OFF;
16962         }
16963 #endif
16964         /*  delete from db */
16965         memset(sql_query_string, 0x00, sizeof(sql_query_string));
16966         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_certificate_tbl WHERE certificate_id = %d", certificate_id);
16967         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16968         if (error != EMAIL_ERROR_NONE) {
16969                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
16970                         goto FINISH_OFF;
16971         }
16972
16973         /*  validate account existence */
16974         rc = sqlite3_changes(local_db_handle);
16975         if (rc == 0)  {
16976                 EM_DEBUG_EXCEPTION(" no matched certificate found...");
16977                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
16978                 goto FINISH_OFF;
16979         }
16980
16981         ret = true;
16982
16983 FINISH_OFF:
16984         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
16985
16986         if (err_code != NULL)
16987                 *err_code = error;
16988
16989         EM_DEBUG_FUNC_END("ret [%d]", ret);
16990         return ret;
16991 }
16992
16993 INTERNAL_FUNC int emstorage_update_certificate(char *multi_user_name, int certificate_id, emstorage_certificate_tbl_t *certificate, int transaction, int *err_code)
16994 {
16995         EM_DEBUG_FUNC_BEGIN("certificate_id[%d], certificate[%p], transaction[%d], err_code[%p]", certificate_id, certificate, transaction, err_code);
16996
16997         if (certificate_id < 1)  {
16998                 EM_DEBUG_EXCEPTION(" certificate_id[%d]", certificate_id);
16999                 if (err_code)
17000                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17001                 return false;
17002         }
17003
17004         int error = EMAIL_ERROR_NONE;
17005         int rc, ret = false;
17006
17007         DB_STMT hStmt = NULL;
17008         char sql_query_string[QUERY_SIZE] = {0, };
17009 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
17010         char cert_password_file_name[MAX_PW_FILE_NAME_LENGTH];
17011 #endif
17012
17013         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17014         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17015
17016         SNPRINTF(sql_query_string, sizeof(sql_query_string),
17017                 "UPDATE mail_certificate_tbl SET"
17018                 " issue_year = ?"
17019                 ", issue_month = ?"             /* Index of certificate */
17020                 ", issue_day = ?"               /* Select the account */
17021                 ", expiration_year = ?"         /* Year of issue */
17022                 ", expiration_month = ?"                /* Month of issue */
17023                 ", expiration_day = ?"          /* Day of issue */
17024                 ", issue_organization_name = ?"         /* Year of expiration */
17025                 ", email_address = ?"           /* Month of expiration */
17026                 ", subject_str = ?"             /* Day of expiration */
17027                 ", filepath = ?"                /* Organization of issue */
17028                 ", password = ?"
17029                 " WHERE certificate_id = ?");
17030
17031         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17032         EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
17033         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
17034                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17035
17036         _bind_stmt_field_data_int(hStmt, ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_year);
17037         _bind_stmt_field_data_int(hStmt, ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_month);
17038         _bind_stmt_field_data_int(hStmt, ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->issue_day);
17039         _bind_stmt_field_data_int(hStmt, EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_year);
17040         _bind_stmt_field_data_int(hStmt, EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_month);
17041         _bind_stmt_field_data_int(hStmt, EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->expiration_day);
17042         _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);
17043         _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->email_address, 0, EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL);
17044         _bind_stmt_field_data_string(hStmt, SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->subject_str, 0, SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL);
17045         _bind_stmt_field_data_string(hStmt, FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL, certificate->filepath, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
17046
17047         /*  rc = sqlite3_step(hStmt); */
17048         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17049         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
17050                 ("sqlite3_step fail:%d", rc));
17051         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
17052                 ("sqlite3_step fail:%d", rc));
17053
17054         /*  validate account existence */
17055         rc = sqlite3_changes(local_db_handle);
17056         if (rc == 0) {
17057                 EM_DEBUG_EXCEPTION(" no matched account found...");
17058
17059                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
17060                 goto FINISH_OFF;
17061         }
17062
17063         ret = true;
17064
17065 FINISH_OFF:
17066         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17067
17068         if (hStmt != NULL)  {
17069                 rc = sqlite3_finalize(hStmt);
17070                 if (rc != SQLITE_OK)  {
17071                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17072                         error = EMAIL_ERROR_DB_FAILURE;
17073                 }
17074         }
17075
17076         if (error == EMAIL_ERROR_NONE) {
17077                 if (!emcore_notify_storage_event (NOTI_CERTIFICATE_UPDATE, certificate->certificate_id, 0, NULL, 0))
17078                         EM_DEBUG_EXCEPTION ("emcore_notify_storage_event[ NOTI_CERTIFICATE_UPDATE] : Notification Failed >>> ");
17079         }
17080
17081         if (err_code != NULL)
17082                 *err_code = error;
17083
17084         EM_DEBUG_FUNC_END("ret [%d]", ret);
17085         return ret;
17086 }
17087
17088 /* Tasks --------------------------------------------------------------------------*/
17089 INTERNAL_FUNC int emstorage_add_task(char *multi_user_name, email_task_type_t input_task_type, email_task_priority_t input_task_priority, char *input_task_parameter, int input_task_parameter_length, int input_transaction, int *output_task_id)
17090 {
17091         EM_DEBUG_FUNC_BEGIN("input_task_type [%d] input_task_priority[%p], input_task_parameter[%p] input_task_parameter_length[%d] input_transaction[%d] output_task_id[%p]", input_task_type, input_task_priority, input_task_parameter, input_task_parameter_length, input_transaction, output_task_id);
17092         int ret = 0;
17093         int i = 0;
17094         int task_id = 0;
17095         int err = EMAIL_ERROR_NONE;
17096         int rc = -1;
17097         DB_STMT hStmt = NULL;
17098         char sql_query_string[QUERY_SIZE] = {0, };
17099         sqlite3 *local_db_handle = NULL;
17100         char *sql = "SELECT max(rowid) FROM mail_task_tbl;";
17101         char **result = NULL;
17102
17103         if (input_task_parameter == NULL || output_task_id == NULL)  {
17104                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17105                 return EMAIL_ERROR_INVALID_PARAM;
17106         }
17107
17108         local_db_handle = emstorage_get_db_connection(multi_user_name);
17109         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, err);
17110
17111         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
17112         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
17113                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
17114
17115         if (NULL == result[1])
17116                 task_id = 1;
17117         else
17118                 task_id = atoi(result[1])+1;
17119
17120         *output_task_id = task_id;
17121
17122         sqlite3_free_table(result);
17123         result = NULL;
17124
17125         SNPRINTF(sql_query_string, sizeof(sql_query_string),
17126                 "INSERT INTO mail_task_tbl VALUES "
17127                 "(        "
17128                 "    ? "  /*   task_id */
17129                 "  , ? "  /*   task_type */
17130                 "  , ? "  /*   task_status */
17131                 "  , ? "  /*   task_priority */
17132                 "  , ? "  /*   task_parameter_length */
17133                 "  , ? "  /*   task_parameter */
17134                 "  , ? "  /*   date_time */
17135                 ") ");
17136
17137         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17138         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
17139                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17140
17141         EM_DEBUG_LOG_SEC(">>>> SQL STMT [%s] ", sql_query_string);
17142
17143
17144         _bind_stmt_field_data_int(hStmt, i++, task_id);
17145         _bind_stmt_field_data_int(hStmt, i++, input_task_type);
17146         _bind_stmt_field_data_int(hStmt, i++, EMAIL_TASK_STATUS_WAIT);
17147         _bind_stmt_field_data_int(hStmt, i++, input_task_priority);
17148         _bind_stmt_field_data_int(hStmt, i++, input_task_parameter_length);
17149         _bind_stmt_field_data_blob(hStmt, i++, input_task_parameter, input_task_parameter_length);
17150         _bind_stmt_field_data_int(hStmt, i++, time(NULL));
17151
17152         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17153
17154         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {err = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
17155                 ("sqlite3_step fail:%d", rc));
17156         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
17157                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
17158
17159         ret = (err == EMAIL_ERROR_NONE);
17160
17161 FINISH_OFF:
17162         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, err);
17163
17164         if (hStmt != NULL)  {
17165                 rc = sqlite3_finalize(hStmt);
17166                 if (rc != SQLITE_OK)  {
17167                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17168                         err = EMAIL_ERROR_DB_FAILURE;
17169                 }
17170         }
17171
17172         EM_DEBUG_FUNC_END("err [%d]", err);
17173         return err;
17174 }
17175
17176 INTERNAL_FUNC int emstorage_delete_task(char *multi_user_name, int task_id, int transaction)
17177 {
17178         EM_DEBUG_FUNC_BEGIN("task_id[%d], transaction[%d]", task_id, transaction);
17179         int ret = false;
17180         int err = EMAIL_ERROR_NONE;
17181         char sql_query_string[QUERY_SIZE] = {0, };
17182
17183         sqlite3 *local_db_handle = NULL;
17184
17185         if (task_id < 0)  {
17186                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17187                 return EMAIL_ERROR_INVALID_PARAM;
17188         }
17189
17190         local_db_handle = emstorage_get_db_connection(multi_user_name);
17191
17192         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
17193
17194         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_task_tbl WHERE task_id = %d", task_id);
17195         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17196         if (err != EMAIL_ERROR_NONE) {
17197                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
17198                         goto FINISH_OFF;
17199         }
17200
17201         ret = true;
17202
17203 FINISH_OFF:
17204         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
17205
17206         EM_DEBUG_FUNC_END("err [%d]", err);
17207         return err;
17208 }
17209
17210 INTERNAL_FUNC int emstorage_update_task_status(char *multi_user_name, int task_id, email_task_status_type_t task_status, int transaction)
17211 {
17212         EM_DEBUG_FUNC_BEGIN("task_id[%d] task_status[%d] transaction[%d]", task_id, task_status, transaction);
17213         int ret = false;
17214         int err = EMAIL_ERROR_NONE;
17215         char sql_query_string[QUERY_SIZE] = {0, };
17216
17217         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17218         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
17219
17220         SNPRINTF(sql_query_string, sizeof(sql_query_string),
17221                 "UPDATE mail_task_tbl SET"
17222                 " task_status = %d"
17223                 " WHERE task_id = %d"
17224                 , task_status
17225                 , task_id);
17226         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17227         if (err != EMAIL_ERROR_NONE) {
17228                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
17229                         goto FINISH_OFF;
17230         }
17231
17232         ret = true;
17233
17234 FINISH_OFF:
17235         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
17236
17237         EM_DEBUG_FUNC_END("err [%d]", err);
17238         return err;
17239 }
17240
17241 INTERNAL_FUNC int emstorage_query_task(char *multi_user_name, 
17242                                                                         const char *input_conditional_clause, 
17243                                                                         const char *input_ordering_clause, 
17244                                                                         email_task_t **output_task_list, 
17245                                                                         int *output_task_count)
17246 {
17247         EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_ordering_clause [%p], "
17248                                                 "output_task_list[%p], output_task_count[%d]", 
17249                                                 input_conditional_clause, input_ordering_clause, output_task_list, output_task_count);
17250         int i = 0, count = 0, rc = -1;
17251         int cur_query = 0;
17252         int field_index = 0;
17253         int err = EMAIL_ERROR_NONE;
17254         email_task_t *task_item_from_tbl = NULL;
17255         char sql_query_string[QUERY_SIZE] = {0, };
17256         char *field_list = "task_id, task_type, task_status, task_priority, task_parameter_length, task_parameter ";
17257         char **result;
17258         sqlite3 *local_db_handle = NULL;
17259         DB_STMT hStmt = NULL;
17260
17261         EM_IF_NULL_RETURN_VALUE(input_conditional_clause, false);
17262         EM_IF_NULL_RETURN_VALUE(output_task_count, false);
17263
17264         local_db_handle = emstorage_get_db_connection(multi_user_name);
17265
17266         SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, 
17267                                         "SELECT COUNT(*) FROM mail_task_tbl %s", input_conditional_clause);
17268         EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
17269
17270         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17271         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
17272                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17273
17274         count = atoi(result[1]);
17275         sqlite3_free_table(result);
17276
17277         EM_DEBUG_LOG("count = %d", rc);
17278
17279         if (count == 0) {
17280                 EM_DEBUG_EXCEPTION("no task found...");
17281                 err = EMAIL_ERROR_TASK_NOT_FOUND;
17282                 goto FINISH_OFF;
17283         }
17284
17285         SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE, 
17286                                         "SELECT %s FROM mail_task_tbl %s %s", field_list, input_conditional_clause, input_ordering_clause);
17287         EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
17288
17289         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17290
17291         EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
17292         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
17293                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17294
17295         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17296         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
17297                 ("sqlite3_step fail:%d", rc));
17298
17299         if (rc == SQLITE_DONE) {
17300                 EM_DEBUG_EXCEPTION("no task found...");
17301                 err = EMAIL_ERROR_TASK_NOT_FOUND;
17302                 count = 0;
17303                 goto FINISH_OFF;
17304         }
17305
17306         if (!(task_item_from_tbl = (email_task_t*)em_malloc(sizeof(email_task_t) * count))) {
17307                 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
17308                 err = EMAIL_ERROR_OUT_OF_MEMORY;
17309                 goto FINISH_OFF;
17310         }
17311
17312         for (i = 0; i < count; i++)  {
17313                 /*  get recordset */
17314                 field_index = 0;
17315
17316                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_id), field_index++);
17317                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_type), field_index++);
17318                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_status), field_index++);
17319                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_priority), field_index++);
17320                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_parameter_length), field_index++);
17321                 _get_stmt_field_data_blob(hStmt, (void**)&(task_item_from_tbl[i].task_parameter), field_index++);
17322
17323                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17324                 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i,  rc);
17325                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
17326                         ("sqlite3_step fail:%d", rc));
17327         }
17328
17329 FINISH_OFF:
17330
17331         if (err == EMAIL_ERROR_NONE)  {
17332                 if (output_task_list)
17333                         *output_task_list = task_item_from_tbl;
17334                 *output_task_count = count;
17335         } else {
17336                 for (i = 0; i < count; i++) {
17337                         EM_SAFE_FREE(task_item_from_tbl[i].task_parameter);
17338                 }
17339                 EM_SAFE_FREE(task_item_from_tbl);
17340         }
17341
17342         if (hStmt != NULL)  {
17343                 rc = sqlite3_finalize(hStmt);
17344                 hStmt = NULL;
17345                 if (rc != SQLITE_OK)  {
17346                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17347                         err = EMAIL_ERROR_DB_FAILURE;
17348                 }
17349         }
17350
17351         EM_DEBUG_FUNC_END("err [%d]", err);
17352         return err;
17353 }
17354
17355 INTERNAL_FUNC int emstorage_check_and_update_server_uid_by_message_id(char *multi_user_name, int account_id, email_mailbox_type_e input_mailbox_type, char *message_id, char *server_uid, int *mail_id)
17356 {
17357         EM_DEBUG_FUNC_BEGIN("account_id:[%d], mailbox_type:[%d], message_id:[%s], server_uid:[%s]", account_id, input_mailbox_type, message_id, server_uid);
17358         int err = EMAIL_ERROR_NONE;
17359
17360         if (message_id == NULL) {
17361                 EM_DEBUG_EXCEPTION("Invalid parameter");
17362                 err = EMAIL_ERROR_INVALID_PARAM;
17363                 return err;
17364         }
17365
17366         int rc = -1;
17367         int count = 0;
17368         int temp_mail_id = 0;
17369         int where_pararaph_length = 0;
17370         char *where_pararaph = NULL;
17371         char sql_query_string[QUERY_SIZE] = {0, };
17372         char **result = NULL;
17373         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17374
17375         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id from mail_tbl ");
17376
17377         where_pararaph_length = EM_SAFE_STRLEN(message_id) + 100;
17378         where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
17379         if (where_pararaph == NULL) {
17380                 EM_DEBUG_EXCEPTION("em_malloc failed");
17381                 err = EMAIL_ERROR_OUT_OF_MEMORY;
17382                 goto FINISH_OFF;
17383         }
17384
17385         if (account_id != ALL_ACCOUNT)
17386                 sqlite3_snprintf(where_pararaph_length, where_pararaph, "WHERE account_id = %d AND mailbox_type = %d AND message_id like '%q'", account_id, input_mailbox_type, message_id);
17387         else
17388                 sqlite3_snprintf(where_pararaph_length, where_pararaph, "WHERE mailbox_type = %d AND message_id like '%q'", input_mailbox_type, message_id);
17389
17390         if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
17391                 strcat(sql_query_string, where_pararaph);
17392
17393         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
17394
17395         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
17396         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
17397                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17398
17399
17400         EM_DEBUG_LOG("Count of mails [%d]", count);
17401
17402         if (count) {
17403                 _get_table_field_data_int(result, &temp_mail_id, 1);
17404                 EM_DEBUG_LOG("Searched mail_id [%d]", temp_mail_id);
17405
17406                 memset(sql_query_string, 0x00, QUERY_SIZE);
17407                 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string, "UPDATE mail_tbl set server_mail_id = '%q'", server_uid);
17408
17409                 if(strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
17410                         strcat(sql_query_string, where_pararaph);
17411
17412                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17413                 if (err != EMAIL_ERROR_NONE) {
17414                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
17415                         goto FINISH_OFF;
17416                 }
17417
17418         } else {
17419                 err = EMAIL_ERROR_MAIL_NOT_FOUND;
17420         }
17421
17422 FINISH_OFF:
17423
17424         sqlite3_free_table(result);
17425         result = NULL;
17426
17427
17428         EM_SAFE_FREE(where_pararaph);
17429
17430         if (mail_id != NULL)
17431                 *mail_id = temp_mail_id;
17432
17433         EM_DEBUG_FUNC_END("err : [%d]", err);
17434         return err;
17435 }
17436 /* Tasks --------------------------------------------------------------------------*/
17437
17438 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
17439 INTERNAL_FUNC int emstorage_add_auto_download_activity(char *multi_user_name, email_event_auto_download *local_activity, int *activity_id, int transaction, int *err_code)
17440 {
17441         EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
17442
17443         if (!local_activity || !activity_id) {
17444                 EM_DEBUG_EXCEPTION("local_activity[%p], activity_id[%p]", local_activity, activity_id);
17445                 if (err_code != NULL)
17446                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17447                 return false;
17448         }
17449
17450         int rc = -1;
17451         int ret = false;
17452         int error = EMAIL_ERROR_NONE;
17453         int i = 0;
17454
17455         char sql_query_string[QUERY_SIZE] = {0, };
17456         DB_STMT hStmt = NULL;
17457
17458         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17459         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17460
17461         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17462         SNPRINTF(sql_query_string, sizeof(sql_query_string),
17463                 "INSERT INTO mail_auto_download_activity_tbl VALUES "
17464                 "( "
17465                 "? "  /* Activity ID */
17466                 ",?"  /* Status */
17467                 ",?"  /* Account ID */
17468                 ",?"  /* Local Mail ID */
17469                 ",?"  /* Server mail ID */
17470                 ",?"  /* Mailbox ID*/
17471                 ",?"  /* Multi USER NAME */
17472                 ") ");
17473
17474         char *sql = "SELECT max(rowid) FROM mail_auto_download_activity_tbl;";
17475         char **result = NULL;
17476
17477
17478         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
17479
17480         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
17481                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
17482
17483         if (NULL==result[1]) rc = 1;
17484         else rc = atoi(result[1])+1;
17485         sqlite3_free_table(result);
17486         result = NULL;
17487
17488         *activity_id = local_activity->activity_id = rc;
17489
17490         EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [%d], MAIL ID [%d], SERVER MAIL ID [%lu]",
17491                         local_activity->activity_id, local_activity->mail_id, local_activity->server_mail_id);
17492
17493         if (local_activity->mailbox_id)
17494                 EM_DEBUG_LOG(" MAILBOX ID [%d]", local_activity->mailbox_id);
17495
17496
17497         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17498         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
17499                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17500
17501
17502         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
17503         _bind_stmt_field_data_int(hStmt, i++, local_activity->status);
17504         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
17505         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
17506         _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
17507         _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
17508         _bind_stmt_field_data_string(hStmt, i++, (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
17509
17510
17511         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17512
17513         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL;goto FINISH_OFF; },
17514                 ("sqlite3_step fail:%d", rc));
17515         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
17516                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
17517
17518         ret = true;
17519
17520 FINISH_OFF:
17521         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17522         if (hStmt != NULL) {
17523                 rc = sqlite3_finalize(hStmt);
17524                 hStmt = NULL;
17525                 if (rc != SQLITE_OK) {
17526                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17527                         error = EMAIL_ERROR_DB_FAILURE;
17528                 }
17529         }
17530
17531         if (err_code != NULL)
17532                 *err_code = error;
17533
17534         EM_DEBUG_FUNC_END("ret [%d]", ret);
17535         return ret;
17536 }
17537
17538
17539 INTERNAL_FUNC int emstorage_delete_auto_download_activity(char *multi_user_name, int account_id, int mail_id, int activity_id, int transaction, int *err_code)
17540 {
17541         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);
17542
17543         if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
17544                 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);
17545
17546                 if (err_code != NULL)
17547                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17548                 return false;
17549         }
17550
17551         int rc = -1;
17552         int ret = false;
17553         int error = EMAIL_ERROR_NONE;
17554         char sql_query_string[QUERY_SIZE] = {0, };
17555         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17556
17557         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17558         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17559
17560         if (activity_id == 0)
17561                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d AND mail_id = %d", account_id, mail_id);
17562         else
17563                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d AND activity_id = %d", account_id, activity_id);
17564
17565         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17566         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17567         if (error != EMAIL_ERROR_NONE) {
17568                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17569                 goto FINISH_OFF;
17570         }
17571
17572         /*  validate activity existence */
17573         rc = sqlite3_changes(local_db_handle);
17574         if (rc == 0)  {
17575                 EM_DEBUG_EXCEPTION("No matching activity found");
17576                 error = EMAIL_ERROR_DATA_NOT_FOUND;
17577                 ret = true;
17578                 goto FINISH_OFF;
17579         }
17580
17581         ret = true;
17582
17583 FINISH_OFF:
17584         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17585
17586         if (err_code != NULL)
17587                 *err_code = error;
17588
17589         EM_DEBUG_FUNC_END("ret [%d]", ret);
17590         return ret;
17591 }
17592
17593 INTERNAL_FUNC int emstorage_delete_all_auto_download_activity(char *multi_user_name, int account_id, int transaction, int *err_code)
17594 {
17595         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17596
17597         if (account_id < FIRST_ACCOUNT_ID) {
17598                 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17599                 if (err_code != NULL)
17600                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17601                 return false;
17602         }
17603
17604         int rc = -1;
17605         int ret = false;
17606         int error = EMAIL_ERROR_NONE;
17607         char sql_query_string[QUERY_SIZE] = {0, };
17608
17609         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17610
17611         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17612         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17613         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d", account_id);
17614
17615         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17616         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17617         if (error != EMAIL_ERROR_NONE) {
17618                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17619                         goto FINISH_OFF;
17620         }
17621
17622         rc = sqlite3_changes(local_db_handle);
17623         if (rc == 0) {
17624                 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17625                 error = EMAIL_ERROR_DATA_NOT_FOUND;
17626                 ret = true;
17627                 goto FINISH_OFF;
17628         }
17629
17630         ret = true;
17631
17632 FINISH_OFF:
17633         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17634
17635         if (err_code != NULL)
17636                 *err_code = error;
17637
17638         EM_DEBUG_FUNC_END("ret [%d]", ret);
17639         return ret;
17640 }
17641
17642
17643 INTERNAL_FUNC int emstorage_delete_auto_download_activity_by_mailbox(char *multi_user_name, int account_id, int mailbox_id, int transaction, int *err_code)
17644 {
17645         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17646
17647         if (account_id < FIRST_ACCOUNT_ID || mailbox_id < 0) {
17648                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17649                 if (err_code != NULL)
17650                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17651                 return false;
17652         }
17653
17654         int rc = -1;
17655         int ret = false;
17656         int error = EMAIL_ERROR_NONE;
17657         char sql_query_string[QUERY_SIZE] = {0, };
17658
17659         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17660
17661         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17662         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17663         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d AND mailbox_id = %d", account_id, mailbox_id);
17664
17665         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17666         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17667         if (error != EMAIL_ERROR_NONE) {
17668                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17669                         goto FINISH_OFF;
17670         }
17671
17672         rc = sqlite3_changes(local_db_handle);
17673         if (rc == 0) {
17674                 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17675                 error = EMAIL_ERROR_DATA_NOT_FOUND;
17676                 ret = true;
17677                 goto FINISH_OFF;
17678         }
17679
17680         ret = true;
17681
17682 FINISH_OFF:
17683         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17684
17685         if (err_code != NULL)
17686                 *err_code = error;
17687
17688         EM_DEBUG_FUNC_END("ret [%d]", ret);
17689         return ret;
17690 }
17691
17692
17693 INTERNAL_FUNC int emstorage_get_auto_download_activity(char *multi_user_name, int account_id, int input_mailbox_id, email_event_auto_download **event_start, int *count, int transaction, int *err_code)
17694 {
17695         EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
17696
17697         if (account_id < FIRST_ACCOUNT_ID || !event_start || input_mailbox_id <= 0 || !count) {
17698                 EM_DEBUG_EXCEPTION("account_id[%d], event_start[%p], input_mailbox_id[%d], count[%p], err_code[%p]", account_id, event_start, input_mailbox_id, count, err_code);
17699
17700                 if (err_code != NULL)
17701                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17702                 return false;
17703         }
17704
17705         int rc = -1;
17706         int ret = false;
17707         char **result;
17708         int error = EMAIL_ERROR_NONE;
17709         int i = 0;
17710         DB_STMT hStmt = NULL;
17711         email_event_auto_download *event_list = NULL;
17712         char sql_query_string[QUERY_SIZE] = {0, };
17713
17714         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17715
17716         EMSTORAGE_START_READ_TRANSACTION(transaction);
17717
17718         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17719         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl WHERE account_id = %d AND mailbox_id = '%d' order by activity_id", account_id, input_mailbox_id);
17720
17721
17722         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17723         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
17724                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17725
17726         *count = atoi(result[1]);
17727         sqlite3_free_table(result);
17728
17729         EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
17730
17731         if (!*count) {
17732                 EM_DEBUG_LOG("No matched activity found in mail_auto_download_activity_tbl");
17733                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
17734                 ret = true;
17735                 goto FINISH_OFF;
17736         }
17737         EM_DEBUG_LOG("Activity Count = %d", *count);
17738
17739         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17740         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_auto_download_activity_tbl WHERE account_id = %d AND mailbox_id = '%d' order by activity_id", account_id, input_mailbox_id);
17741
17742         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17743
17744
17745         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17746
17747         EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
17748         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
17749                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17750
17751
17752         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17753         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
17754                 ("sqlite3_step fail:%d", rc));
17755
17756         if (!(event_list = (email_event_auto_download *)em_malloc(sizeof(email_event_auto_download)*(*count)))) {
17757                 EM_DEBUG_EXCEPTION("Malloc failed");
17758
17759                 error = EMAIL_ERROR_OUT_OF_MEMORY;
17760                 goto FINISH_OFF;
17761         }
17762
17763         for (i=0; i < (*count); i++) {
17764                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17765                 _get_stmt_field_data_int(hStmt, &(event_list[i].status), STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17766                 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17767                 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17768                 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17769                 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17770
17771                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17772                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF;},
17773                         ("sqlite3_step fail:%d", rc));
17774         }
17775
17776         ret = true;
17777
17778 FINISH_OFF:
17779
17780         if (true == ret)
17781           *event_start = event_list;
17782         else {
17783                 EM_SAFE_FREE(event_list);
17784                 *event_start = NULL;
17785                 *count = 0;
17786         }
17787
17788         if (hStmt != NULL) {
17789                 rc = sqlite3_finalize(hStmt);
17790                 hStmt = NULL;
17791                 if (rc != SQLITE_OK) {
17792                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17793                         error = EMAIL_ERROR_DB_FAILURE;
17794                 }
17795         }
17796
17797         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17798         if (err_code != NULL)
17799                 *err_code = error;
17800
17801         EM_DEBUG_FUNC_END("ret [%d]", ret);
17802         return ret;
17803 }
17804
17805
17806 INTERNAL_FUNC int emstorage_get_auto_download_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
17807 {
17808         EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
17809
17810         if (!activity_count || !err_code) {
17811                 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
17812                 if (err_code != NULL)
17813                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17814                 return false;
17815         }
17816
17817         int rc = -1;
17818         int ret = false;
17819         int error = EMAIL_ERROR_NONE;
17820         DB_STMT hStmt = NULL;
17821         char sql_query_string[QUERY_SIZE] = {0, };
17822
17823         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17824
17825         EMSTORAGE_START_READ_TRANSACTION(transaction);
17826         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17827
17828         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl;");
17829
17830         EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
17831
17832
17833         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17834         EM_DEBUG_LOG_DEV ("before sqlite3_prepare hStmt = %p", hStmt);
17835         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF;},
17836                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17837
17838
17839         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17840         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF;},
17841                 ("sqlite3_step fail:%d", rc));
17842
17843         _get_stmt_field_data_int(hStmt, activity_count, 0);
17844
17845         EM_DEBUG_LOG("counts of activities in activity table [%d]", *activity_count);
17846
17847         ret = true;
17848
17849 FINISH_OFF:
17850
17851         if (hStmt != NULL) {
17852                 rc = sqlite3_finalize(hStmt);
17853                 hStmt=NULL;
17854                 if (rc != SQLITE_OK) {
17855                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17856                         error = EMAIL_ERROR_DB_FAILURE;
17857                 }
17858         }
17859
17860         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17861         if (err_code != NULL)
17862                 *err_code = error;
17863
17864         EM_DEBUG_FUNC_END("ret [%d]", ret);
17865         return ret;
17866 }
17867
17868
17869 INTERNAL_FUNC int emstorage_get_auto_download_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
17870 {
17871         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
17872
17873         if (!account_list || !count) {
17874                 EM_DEBUG_EXCEPTION("account_list[%p], count[%p]", account_list, count);
17875                 if (err_code != NULL)
17876                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17877                 return false;
17878         }
17879
17880         int ret = false;
17881         int error = EMAIL_ERROR_NONE;
17882         char *sql = "SELECT count(distinct account_id) FROM mail_auto_download_activity_tbl";
17883         char **result;
17884         int i = 0, rc = -1;
17885         int *result_account_list = NULL;
17886         DB_STMT hStmt = NULL;
17887         char sql_query_string[QUERY_SIZE] = {0, };
17888         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17889
17890         EMSTORAGE_START_READ_TRANSACTION(transaction);
17891
17892         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
17893         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
17894                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
17895
17896         *count = atoi(result[1]);
17897         sqlite3_free_table(result);
17898
17899         if (!*count) {
17900                 EM_DEBUG_EXCEPTION("no account found...");
17901                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
17902                 ret = true;
17903                 goto FINISH_OFF;
17904         }
17905
17906         EM_DEBUG_LOG("Account count [%d]", *count);
17907
17908         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17909
17910         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_auto_download_activity_tbl");
17911
17912         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17913
17914
17915         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17916
17917         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
17918         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF;},
17919                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17920
17921
17922         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17923         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
17924                 ("sqlite3_step fail:%d", rc));
17925
17926         if (NULL == (result_account_list = (int *)em_malloc(sizeof(int)*(*count)))) {
17927                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
17928                 error = EMAIL_ERROR_OUT_OF_MEMORY;
17929                 goto FINISH_OFF;
17930         }
17931
17932         for (i = 0; i < (*count); i++) {
17933                 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
17934
17935                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17936                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
17937                         ("sqlite3_step fail:%d", rc));
17938                 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
17939         }
17940
17941         ret = true;
17942
17943 FINISH_OFF:
17944
17945         if (ret == true)
17946                 *account_list = result_account_list;
17947         else
17948                 EM_SAFE_FREE(result_account_list);
17949
17950         if (hStmt != NULL) {
17951                 rc = sqlite3_finalize(hStmt);
17952                 hStmt = NULL;
17953                 if (rc != SQLITE_OK) {
17954                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17955                         error = EMAIL_ERROR_DB_FAILURE;
17956                 }
17957         }
17958
17959         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17960         if (err_code != NULL)
17961                 *err_code = error;
17962         EM_DEBUG_FUNC_END("ret [%d]", ret);
17963         return ret;
17964 }
17965
17966
17967 INTERNAL_FUNC int emstorage_get_auto_download_mailbox_list(char *multi_user_name, int account_id, int **mailbox_list, int *count, int transaction, int *err_code)
17968 {
17969         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
17970
17971         if (account_id < FIRST_ACCOUNT_ID || !mailbox_list || !count) {
17972                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p]", account_id, mailbox_list, count);
17973                 if (err_code != NULL)
17974                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17975                 return false;
17976         }
17977
17978         int ret = false;
17979         int error = EMAIL_ERROR_NONE;
17980         char **result;
17981         int i = 0, rc = -1;
17982         int *mbox_list = NULL;
17983         DB_STMT hStmt = NULL;
17984         char sql_query_string[QUERY_SIZE] = {0, };
17985
17986         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17987
17988         EMSTORAGE_START_READ_TRANSACTION(transaction);
17989
17990         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17991         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(distinct mailbox_id) FROM mail_auto_download_activity_tbl WHERE account_id = %d order by mailbox_id", account_id);
17992
17993
17994         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17995         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE;sqlite3_free_table(result);goto FINISH_OFF; },
17996                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17997
17998         *count = atoi(result[1]);
17999         sqlite3_free_table(result);
18000
18001         if (!*count) {
18002                 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
18003                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
18004                 ret = true;
18005                 goto FINISH_OFF;
18006         }
18007         EM_DEBUG_LOG("Mailbox count = %d", *count);
18008
18009         memset(sql_query_string, 0x00, sizeof(sql_query_string));
18010
18011         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct mailbox_id FROM mail_auto_download_activity_tbl WHERE account_id = %d order by mailbox_id", account_id);
18012
18013         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
18014
18015
18016         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
18017
18018
18019         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
18020                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
18021
18022
18023         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
18024         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
18025                 ("sqlite3_step fail:%d", rc));
18026
18027         mbox_list = (int *)em_malloc(sizeof(int)*(*count)); /* prevent */
18028         if (mbox_list == NULL) {
18029                 EM_DEBUG_EXCEPTION(" em_malloc failed...");
18030                 error = EMAIL_ERROR_OUT_OF_MEMORY;
18031                 goto FINISH_OFF;
18032         }
18033
18034         for (i = 0; i < (*count); i++) {
18035                 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
18036                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
18037
18038                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
18039                         ("sqlite3_step fail:%d", rc));
18040                 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
18041         }
18042
18043         ret = true;
18044
18045 FINISH_OFF:
18046
18047         if (ret == true)
18048                 *mailbox_list = mbox_list;
18049         else
18050                 EM_SAFE_FREE(mbox_list);
18051
18052         if (hStmt != NULL) {
18053                 rc = sqlite3_finalize(hStmt);
18054                 hStmt = NULL;
18055                 if (rc != SQLITE_OK) {
18056                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
18057                         error = EMAIL_ERROR_DB_FAILURE;
18058                 }
18059         }
18060
18061         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
18062         if (err_code != NULL)
18063                 *err_code = error;
18064         EM_DEBUG_FUNC_END("ret [%d]", ret);
18065         return ret;
18066 }
18067
18068
18069 INTERNAL_FUNC int emstorage_get_auto_download_activity_count_by_mailbox(char *multi_user_name, int account_id, int input_mailbox_id, int *activity_count, int transaction, int *err_code)
18070 {
18071         EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
18072
18073         if (account_id < FIRST_ACCOUNT_ID || !activity_count || !err_code) {
18074                 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
18075                 if (err_code != NULL)
18076                         *err_code = EMAIL_ERROR_INVALID_PARAM;
18077                 return false;
18078         }
18079
18080         int rc = -1;
18081         int ret = false;
18082         int error = EMAIL_ERROR_NONE;
18083         char sql_query_string[QUERY_SIZE] = {0, };
18084         DB_STMT hStmt = NULL;
18085
18086         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
18087
18088         EMSTORAGE_START_READ_TRANSACTION(transaction);
18089         memset(sql_query_string, 0x00, sizeof(sql_query_string));
18090         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl WHERE account_id = %d and mailbox_id = '%d'", account_id, input_mailbox_id);
18091
18092         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
18093
18094         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
18095         EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
18096         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
18097                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
18098
18099         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
18100         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE;goto FINISH_OFF; },
18101                 ("sqlite3_step fail:%d", rc));
18102
18103         _get_stmt_field_data_int(hStmt, activity_count, 0);
18104
18105         EM_DEBUG_LOG("count of activities in activity table [%d]", *activity_count);
18106
18107         ret = true;
18108
18109 FINISH_OFF:
18110
18111         if (hStmt != NULL) {
18112                 rc = sqlite3_finalize(hStmt);
18113                 hStmt=NULL;
18114                 if (rc != SQLITE_OK) {
18115                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
18116                         error = EMAIL_ERROR_DB_FAILURE;
18117                 }
18118         }
18119
18120         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
18121
18122         if (err_code != NULL)
18123                 *err_code = error;
18124
18125         EM_DEBUG_FUNC_END("ret [%d]", ret);
18126         return ret;
18127 }
18128
18129
18130 INTERNAL_FUNC int emstorage_update_auto_download_activity(char *multi_user_name, char *old_server_uid, char *new_server_uid, char *mailbox_name, int mailbox_id, int *err_code)
18131 {
18132         EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mailbox_id[%d]", old_server_uid, new_server_uid, mailbox_id);
18133
18134         int rc = -1, ret = false;
18135         int error = EMAIL_ERROR_NONE;
18136         char sql_query_string[QUERY_SIZE] = {0, };
18137         int transaction = true;
18138
18139         if (!old_server_uid || !new_server_uid || (!mailbox_name && mailbox_id < 0)) {
18140                 EM_DEBUG_EXCEPTION("Invalid parameters");
18141                 error = EMAIL_ERROR_INVALID_PARAM;
18142                 return false;
18143         }
18144
18145         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
18146
18147         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
18148         memset(sql_query_string, 0x00, sizeof(sql_query_string));
18149
18150         if (mailbox_id > 0)
18151                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
18152                          "UPDATE mail_auto_download_activity_tbl SET server_mail_id = %s , mailbox_id ='%d' WHERE server_mail_id = %s ", new_server_uid, mailbox_id, old_server_uid);
18153         else if (mailbox_name)
18154                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
18155                          "UPDATE mail_auto_download_activity_tbl SET server_mail_id = %s WHERE server_mail_id = %s ", new_server_uid, old_server_uid);
18156
18157         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
18158         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
18159         if (error != EMAIL_ERROR_NONE) {
18160                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
18161                         goto FINISH_OFF;
18162         }
18163
18164         rc = sqlite3_changes(local_db_handle);
18165         if (rc == 0) {
18166                 EM_DEBUG_LOG("No matching found in mail_auto_download_activity_tbl");
18167         }
18168
18169         ret = true;
18170
18171 FINISH_OFF:
18172
18173         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
18174
18175         if (err_code != NULL)
18176                 *err_code = error;
18177
18178         EM_DEBUG_FUNC_END("ret [%d]", ret);
18179         return ret;
18180 }
18181
18182 #endif
18183
18184 #ifdef __FEATURE_UPDATE_DB_TABLE_SCHEMA__
18185
18186 typedef struct {
18187         char *column_name;
18188         char *column_type;
18189 } email_column_info_t;
18190
18191 static int get_column_information_from_table_callback(void *arg1, int argc, char **argv, char **input_column_name)
18192 {
18193         EM_DEBUG_FUNC_BEGIN("arg1[%p] argc[%d] argv[%p] column_name[%p]", arg1, argc, argv, input_column_name);
18194
18195         int i = 0;
18196         int validated = 0;
18197         char *column_name = NULL;
18198         char *column_type = NULL;
18199         GList *new_list = *((GList**)arg1);
18200         email_column_info_t *column_info_item = NULL;
18201
18202         for (i = 0; i < argc; ++i)  {
18203                 /* EM_DEBUG_LOG("%s = %s", input_column_name[i], argv[i]); */
18204                 if (EM_SAFE_STRCMP(input_column_name[i], "name") == 0) {
18205                         if (column_name)
18206                                 EM_SAFE_FREE(column_name);
18207
18208                         column_name = EM_SAFE_STRDUP(argv[i]);
18209                         validated = 1;
18210                 }
18211                 else if (EM_SAFE_STRCMP(input_column_name[i], "type") == 0) {
18212                         if (column_type)
18213                                 EM_SAFE_FREE(column_type);
18214
18215                         column_type = EM_SAFE_STRDUP(argv[i]);
18216                 }
18217         }
18218
18219         if (validated) {
18220                 EM_DEBUG_LOG("column_name[%s] column_type[%s]", column_name, column_type);
18221                 column_info_item = em_malloc(sizeof(email_column_info_t));
18222         if (column_info_item == NULL) {
18223             EM_DEBUG_EXCEPTION("em_malloc failed");
18224             goto FINISH_OFF;
18225         }
18226
18227                 column_info_item->column_name = EM_SAFE_STRDUP(column_name);
18228                 column_info_item->column_type = EM_SAFE_STRDUP(column_type);
18229                 new_list = g_list_append(new_list, (gpointer)column_info_item);
18230                 *((GList**)arg1) = new_list;
18231         }
18232
18233 FINISH_OFF:
18234
18235     EM_SAFE_FREE(column_name);
18236     EM_SAFE_FREE(column_type);
18237
18238         EM_DEBUG_FUNC_END();
18239         return 0;
18240 }
18241
18242 static int emstorage_get_column_information_from_table(char *multi_user_name, const char *input_table_name, GList **output_column_info)
18243 {
18244         EM_DEBUG_FUNC_BEGIN("input_table_name[%p] output_column_info[%p]", input_table_name, output_column_info);
18245         int err = EMAIL_ERROR_NONE;
18246         int result_from_sqlite = 0;
18247         char *error_message_from_sqlite = NULL;
18248         char sql_query_string[QUERY_SIZE] = {0, };
18249         GList *new_list = NULL;
18250         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
18251
18252         SNPRINTF(sql_query_string, QUERY_SIZE, "pragma table_info(%s);", input_table_name);
18253
18254         result_from_sqlite = sqlite3_exec(local_db_handle, sql_query_string, get_column_information_from_table_callback, &new_list, &error_message_from_sqlite);
18255
18256         if (result_from_sqlite != SQLITE_OK)
18257                 EM_DEBUG_EXCEPTION("sqlite3_exec returns [%d]", result_from_sqlite);
18258
18259         EM_DEBUG_LOG("new_list[%p] output_column_info[%p]", new_list, output_column_info);
18260
18261         if (new_list && output_column_info) {
18262                 EM_DEBUG_LOG("g_list_length[%d]", g_list_length(new_list));
18263                 *output_column_info = new_list;
18264         }
18265
18266         EM_DEBUG_FUNC_END("err [%d]", err);
18267         return err;
18268 }
18269
18270 static  int emstorage_create_renamed_table(char *multi_user_name, char **input_full_query, int input_query_index, char *input_source_table_name, char *input_new_table_name)
18271 {
18272         EM_DEBUG_FUNC_BEGIN("input_full_query [%p] input_query_index[%d] input_source_table_name[%p] input_new_table_name[%p]", input_full_query, input_query_index, input_source_table_name, input_new_table_name);
18273         int error = EMAIL_ERROR_NONE;
18274         int rc = -1;
18275         sqlite3 *local_db_handle = NULL;
18276         char sql_query_string[QUERY_SIZE] = {0, };
18277
18278         if (input_full_query == NULL || input_source_table_name == NULL || input_new_table_name == NULL) {
18279                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
18280                 error = EMAIL_ERROR_INVALID_PARAM;
18281                 goto FINISH_OFF;
18282         }
18283
18284         local_db_handle = emstorage_get_db_connection(multi_user_name);
18285
18286         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
18287         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
18288
18289         EM_DEBUG_LOG("[%s] will be replaced by [%s]", input_source_table_name, input_new_table_name);
18290
18291         EM_SAFE_STRNCPY(sql_query_string, input_full_query[input_query_index], sizeof(sql_query_string)-1); /*prevent 21984*/
18292         reg_replace(sql_query_string, input_source_table_name, input_new_table_name);
18293
18294         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
18295 FINISH_OFF:
18296
18297         if (error == EMAIL_ERROR_NONE) {
18298                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
18299         }
18300         else {
18301                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
18302         }
18303
18304         EM_DEBUG_FUNC_END("error [%d]", error);
18305         return error;
18306 }
18307
18308 static int emstorage_add_column(char *multi_user_name, char *input_table_name, email_column_info_t *input_new_column)
18309 {
18310         EM_DEBUG_FUNC_BEGIN("input_table_name[%p] input_new_column[%p]", input_table_name, input_new_column);
18311         int error = EMAIL_ERROR_NONE;
18312         int rc = -1;
18313         sqlite3 *local_db_handle = NULL;
18314         char sql_query_string[QUERY_SIZE] = {0, };
18315
18316         if (input_table_name == NULL || input_new_column == NULL) {
18317                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
18318                 error = EMAIL_ERROR_INVALID_PARAM;
18319                 goto FINISH_OFF;
18320         }
18321
18322         local_db_handle = emstorage_get_db_connection(multi_user_name);
18323
18324         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
18325         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
18326         SNPRINTF(sql_query_string, QUERY_SIZE, "ALTER TABLE %s ADD COLUMN %s %s;", input_table_name, input_new_column->column_name, input_new_column->column_type);
18327         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
18328 FINISH_OFF:
18329
18330         if (error == EMAIL_ERROR_NONE) {
18331                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
18332         }
18333         else {
18334                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
18335         }
18336
18337         EM_DEBUG_FUNC_END("error [%d]", error);
18338         return error;
18339 }
18340
18341 static int emstorage_drop_table(char *multi_user_name, char *input_table_name)
18342 {
18343         EM_DEBUG_FUNC_BEGIN("input_table_name[%p]", input_table_name);
18344         int error = EMAIL_ERROR_NONE;
18345         int rc = -1;
18346         sqlite3 *local_db_handle = NULL;
18347         char sql_query_string[QUERY_SIZE] = {0, };
18348
18349         if (input_table_name == NULL) {
18350                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
18351                 error = EMAIL_ERROR_INVALID_PARAM;
18352                 goto FINISH_OFF;
18353         }
18354
18355         local_db_handle = emstorage_get_db_connection(multi_user_name);
18356
18357         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
18358         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
18359         SNPRINTF(sql_query_string, QUERY_SIZE, "DROP TABLE %s;", input_table_name);
18360         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
18361 FINISH_OFF:
18362
18363         if (error == EMAIL_ERROR_NONE) {
18364                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
18365         }
18366         else {
18367                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
18368         }
18369
18370         EM_DEBUG_FUNC_END("error [%d]", error);
18371         return error;
18372 }
18373
18374 gint glist_compare_column_name(gconstpointer old_column_info, gconstpointer new_column_info)
18375 {
18376         EM_DEBUG_FUNC_BEGIN("old_column_info[%p] new_column_info[%p]", old_column_info, new_column_info);
18377         email_column_info_t *left_one  = (email_column_info_t*)old_column_info;
18378         email_column_info_t *right_one = (email_column_info_t*)new_column_info;
18379
18380         if (old_column_info == NULL || new_column_info == NULL)
18381                 return -1;
18382
18383         return EM_SAFE_STRCMP((char*)left_one->column_name, (char*)right_one->column_name);
18384 }
18385
18386 INTERNAL_FUNC int emstorage_update_db_table_schema(char *multi_user_name)
18387 {
18388         EM_DEBUG_FUNC_BEGIN();
18389         int i = 0;
18390         int j = 0;
18391         int error = EMAIL_ERROR_NONE;
18392         int query_len = 0;
18393         email_column_info_t *new_column_info = NULL;
18394         email_column_info_t *p_column_info = NULL;
18395         char **create_table_query = NULL;
18396         GList *found_data = NULL;
18397         GList *column_list_of_old_table = NULL;
18398         GList *column_list_of_new_table = NULL;
18399         char table_names[CREATE_TABLE_MAX][2][50] = { { "mail_account_tbl", "mail_account_tbl_new" },
18400                 { "mail_box_tbl", "mail_box_tbl_new" },
18401                 { "mail_read_mail_uid_tbl", "mail_read_mail_uid_tbl_new" },
18402                 { "mail_rule_tbl", "mail_rule_tbl_new" },
18403                 { "mail_tbl", "mail_tbl_new" },
18404                 { "mail_attachment_tbl", "mail_attachment_tbl_new" },
18405 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
18406                 { "mail_partial_body_activity_tbl", "mail_partial_body_activity_tbl_new" },
18407 #else
18408                 { "", "" },
18409 #endif
18410                 { "mail_meeting_tbl", "mail_meeting_tbl_new" },
18411 #ifdef __FEATURE_LOCAL_ACTIVITY__
18412                 { "mail_local_activity_tbl", "mail_local_activity_tbl_new" },
18413 #else
18414                 { "", "" },
18415 #endif
18416                 { "mail_certificate_tbl", "mail_certificate_tbl_new" },
18417                 { "mail_task_tbl", "mail_task_tbl_new" },
18418 #ifdef __FEATURE_BODY_SEARCH__
18419                 { "mail_text_tbl", "mail_text_tbl_new" },
18420 #else
18421                 { "", "" },
18422 #endif
18423
18424 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
18425                 { "mail_auto_download_activity_tbl", "mail_auto_download_activity_tbl_new" }
18426 #else
18427                 { "", "" }
18428 #endif
18429
18430         };
18431
18432         error = emcore_load_query_from_file(EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
18433
18434         if (error != EMAIL_ERROR_NONE) {
18435                 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
18436                 goto FINISH_OFF;
18437         }
18438
18439         if (query_len < CREATE_TABLE_MAX) {
18440                 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
18441                 error = EMAIL_ERROR_SYSTEM_FAILURE;
18442                 goto FINISH_OFF;
18443         }
18444
18445         for (i = CREATE_TABLE_MAIL_ACCOUNT_TBL; i < CREATE_TABLE_MAX; i++) {
18446                 EM_DEBUG_LOG("table [%s] new_table [%s]", table_names[i][0], table_names[i][1]);
18447                 if (EM_SAFE_STRLEN(table_names[i][0]) && EM_SAFE_STRLEN(table_names[i][1])) {
18448                         /* Check existing of _new table */
18449                         emstorage_drop_table(multi_user_name, table_names[i][1]);
18450                         error = emstorage_create_renamed_table(multi_user_name, create_table_query, i, table_names[i][0], table_names[i][1]);
18451                         if (error != EMAIL_ERROR_NONE) {
18452                                 EM_DEBUG_EXCEPTION("emstorage_create_renamed_table failed [%d]", error);
18453                                 goto FINISH_OFF;
18454                         }
18455
18456                         emstorage_get_column_information_from_table(multi_user_name, table_names[i][0], &column_list_of_old_table);
18457                         emstorage_get_column_information_from_table(multi_user_name, table_names[i][1], &column_list_of_new_table);
18458
18459                         /* Compare fields and add new field */
18460                         for (j = 0; j < g_list_length(column_list_of_new_table); j++) {
18461                                 new_column_info = (email_column_info_t*)g_list_nth_data(column_list_of_new_table, j);
18462                                 found_data = g_list_find_custom(column_list_of_old_table, (gconstpointer)new_column_info, glist_compare_column_name);
18463                                 if (found_data == NULL) {
18464                                         /* add new field*/
18465                                         emstorage_add_column(multi_user_name, table_names[i][0], new_column_info);
18466                                 }
18467                         }
18468
18469                         emstorage_drop_table(multi_user_name, table_names[i][1]);
18470                 }
18471                 else
18472                         EM_DEBUG_LOG("Skipped");
18473         }
18474
18475 FINISH_OFF:
18476         if (create_table_query) {
18477                 int i = 0;
18478                 for (i = 0; i < query_len; i++) {
18479                         if (create_table_query[i]) {
18480                                 EM_SAFE_FREE(create_table_query[i]);
18481                         }
18482                 }
18483                 EM_SAFE_FREE(create_table_query);
18484         }
18485
18486         found_data = g_list_first(column_list_of_old_table);
18487         while(found_data != NULL) {
18488                 p_column_info = (email_column_info_t *)found_data->data;
18489                 EM_SAFE_FREE(p_column_info->column_name);
18490                 EM_SAFE_FREE(p_column_info->column_type);
18491                 EM_SAFE_FREE(p_column_info);
18492
18493                 found_data = g_list_next(found_data);
18494         }
18495         g_list_free(column_list_of_old_table);
18496
18497         found_data = g_list_first(column_list_of_new_table);
18498         while(found_data != NULL) {
18499                 p_column_info = (email_column_info_t *)found_data->data;
18500                 EM_SAFE_FREE(p_column_info->column_name);
18501                 EM_SAFE_FREE(p_column_info->column_type);
18502                 EM_SAFE_FREE(p_column_info);
18503
18504                 found_data = g_list_next(found_data);
18505         }
18506         g_list_free(column_list_of_new_table);
18507
18508         EM_DEBUG_FUNC_END("error [%d]", error);
18509         return error;
18510 }
18511 #endif /* __FEATURE_UPDATE_DB_TABLE_SCHEMA__ */
18512
18513 /*EOF*/