7dfec6dc8c24a152db0985a46c2079de7999e62b
[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 #include <grp.h>
55 #include <pwd.h>
56
57 #include "email-internal-types.h"
58 #include "email-convert.h"
59 #include "email-core-utils.h"
60 #include "email-utilities.h"
61 #include "email-storage.h"
62 #include "email-debug-log.h"
63 #include "email-types.h"
64 #include "email-convert.h"
65 #include "email-core-signal.h"
66 #include "email-core-event.h"
67 #include "email-core-container.h"
68 #include "email-core-gmime.h"
69 #include "email-core-key-manager.h"
70
71 #define SETTING_MEMORY_TEMP_FILE_PATH "/tmp/email_tmp_file"
72 #define CONTENT_DATA                  "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset="
73 #define CONTENT_TYPE_DATA             "<meta http-equiv=\"Content-Type\" content=\"text/html; charset="
74
75 #define FLAG_SEEN       0x01
76 #define FLAG_DELETED    0x02
77 #define FLAG_FLAGGED    0x04
78 #define FLAG_ANSWERED   0x08
79 #define FLAG_OLD        0x10
80 #define FLAG_DRAFT      0x20
81
82 #undef close
83
84 #define ISSUE_ORGANIZATION_LEN_IN_MAIL_CERTIFICATE_TBL  256
85 #define EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL       128
86 #define SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL      1027
87 #define FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL           256
88
89 #define ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL            50
90 #define RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL   50
91 #define EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL              128
92 #define USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL               50
93 #define PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL                50
94 #define SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL     50
95 #define SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL            50
96 #define SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL        50
97 #define DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL            30
98 #define REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL           128
99 #define RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL             128
100 #define LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL          256
101 #define DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL       256
102 #define SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL               256
103 #define MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL                128
104 #define ALIAS_LEN_IN_MAIL_BOX_TBL                       128
105 #define LOCAL_MBOX_LEN_IN_MAIL_READ_MAIL_UID_TBL        128
106 #define MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL      128
107 #define S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL             128
108 #define DATA2_LEN_IN_MAIL_READ_MAIL_UID_TBL             256
109 #define FILTER_NAME_LEN_IN_MAIL_RULE_TBL                256
110 #define VALUE_LEN_IN_MAIL_RULE_TBL                      256
111 #define VALUE2_LEN_IN_MAIL_RULE_TBL                     256
112 #define DEST_MAILBOX_LEN_IN_MAIL_RULE_TBL               128
113 #define MAILBOX_NAME_LEN_IN_MAIL_ATTACHMENT_TBL         128
114 #define ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL      256
115 #define ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL      256
116 #define CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL           256
117 #define ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL 128
118 #define MAILBOX_LEN_IN_MAIL_TBL                         128
119 #define SERVER_MAILBOX_LEN_IN_MAIL_TBL                  128
120 #define SERVER_MAIL_ID_LEN_IN_MAIL_TBL                  128
121 #define FROM_LEN_IN_MAIL_TBL                            256
122 #define SENDER_LEN_IN_MAIL_TBL                          256
123 #define REPLY_TO_LEN_IN_MAIL_TBL                        256
124 #define TO_LEN_IN_MAIL_TBL                              3999
125 #define CC_LEN_IN_MAIL_TBL                              3999
126 #define BCC_LEN_IN_MAIL_TBL                             3999
127 #define RETURN_PATH_LEN_IN_MAIL_TBL                     3999
128 #define SUBJECT_LEN_IN_MAIL_TBL                         1027
129 #define THREAD_TOPIC_LEN_IN_MAIL_TBL                    256
130 #define TEXT_1_LEN_IN_MAIL_TBL                          256
131 #define TEXT_2_LEN_IN_MAIL_TBL                          256
132 #define MIME_ENTITY_LEN_IN_MAIL_TBL                     256
133 #define DATETIME_LEN_IN_MAIL_TBL                        128
134 #define MESSAGE_ID_LEN_IN_MAIL_TBL                      256
135 #define FROM_CONTACT_NAME_LEN_IN_MAIL_TBL               3999
136 #define FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL              3999
137 #define TO_CONTACT_NAME_LEN_IN_MAIL_TBL                 3999
138 #define TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL                3999
139 #define MAILBOX_LEN_IN_MAIL_MEETING_TBL                 128
140 #define LOCATION_LEN_IN_MAIL_MEETING_TBL                1024
141 #define GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL        512
142 #define STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL           32
143 #define DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL           32
144 #define PREVIEWBODY_LEN_IN_MAIL_TBL                     512
145 #define CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL        256
146 #define UID                                             "root"          /* UID = root */
147 #define UID_DEFAULT                                     0               /* UID = root */
148 #define GID                                             "priv_email"    /* GID = priv_email */
149 #define GID_DEFAULT                                     10901           /* GID = priv_email */
150 #define MAX_GRP_BUF_SIZE (1024 * 4)
151
152 /*  this define is used for query to change data (delete, insert, update) */
153 #define EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction_flag, error_code) \
154         do { \
155                 if (transaction_flag) { \
156                         if (emstorage_begin_transaction(multi_user_name, NULL, NULL, &error_code) == false) { \
157                                 EM_DEBUG_EXCEPTION("emstorage_begin_transaction error [%d]", error_code); \
158                                 goto FINISH_OFF; \
159                         } \
160                 } \
161         } while (0)
162
163 /*  this define is used for query to change data (delete, insert, update) */
164 #define EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction_flag, result_code, error_code) \
165         do { \
166                 if (transaction_flag) { \
167                         if (result_code == true) { \
168                                 if (emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL) == false) { \
169                                         EM_DEBUG_EXCEPTION("emstorage_commit_transaction error"); \
170                                         error_code = EMAIL_ERROR_DB_FAILURE; \
171                                         result_code = false; \
172                                 } \
173                         } \
174                         else { \
175                                 if (emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL) == false) { \
176                                         EM_DEBUG_EXCEPTION("emstorage_rollback_transaction error"); \
177                                         error_code = EMAIL_ERROR_DB_FAILURE; \
178                                 } \
179                         } \
180                 } \
181         } while (0)
182
183 /*  this define is used for query to read (select) */
184 #define EMSTORAGE_START_READ_TRANSACTION(transaction_flag) \
185         if (transaction_flag) { \
186                 /*_timedlock_shm_mutex(mapped_for_db_lock, 2);*/\
187         }
188
189 /*  this define is used for query to read (select) */
190 #define EMSTORAGE_FINISH_READ_TRANSACTION(transaction_flag) \
191         if (transaction_flag) { \
192                 /*_unlockshm_mutex(mapped_for_db_lock);*/\
193         }
194
195 /*  for safety DB operation */
196 static pthread_mutex_t _db_handle_lock = PTHREAD_MUTEX_INITIALIZER;
197
198
199 #define _MULTIPLE_DB_HANDLE
200
201 #ifdef _MULTIPLE_DB_HANDLE
202
203 typedef struct {
204         char *user_name;
205         pthread_t thread_id;
206         sqlite3 *db_handle;
207 } db_handle_t;
208
209 #define MAX_DB_CLIENT 100
210
211 /* static int _db_handle_count = 0; */
212 db_handle_t _db_handle_list[MAX_DB_CLIENT] = {{NULL, 0, 0}, };
213
214
215 sqlite3 *emstorage_get_db_handle(char *multi_user_name)
216 {
217         EM_DEBUG_FUNC_BEGIN();
218         int i;
219         pthread_t current_thread_id = THREAD_SELF();
220         sqlite3 *result_db_handle = NULL;
221
222         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
223                 ENTER_CRITICAL_SECTION(_db_handle_lock);
224                 for (i = 0; i < MAX_DB_CLIENT; i++) {
225                         if (EM_SAFE_STRCASECMP(_db_handle_list[i].user_name, multi_user_name) != 0)
226                                 continue;
227
228                         if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id)) {
229                                 EM_DEBUG_LOG_DEV("found db handle at [%d]", i);
230                                 result_db_handle = _db_handle_list[i].db_handle;
231                                 break;
232                         }
233                 }
234                 LEAVE_CRITICAL_SECTION(_db_handle_lock);
235         } else {
236                 ENTER_CRITICAL_SECTION(_db_handle_lock);
237                 for (i = 0; i < MAX_DB_CLIENT; i++) {
238                         if (EM_SAFE_STRLEN(_db_handle_list[i].user_name) > 0)
239                                 continue;
240
241                         if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id)) {
242                                 EM_DEBUG_LOG_DEV("found db handle at [%d]", i);
243                                 result_db_handle = _db_handle_list[i].db_handle;
244                                 break;
245                         }
246                 }
247                 LEAVE_CRITICAL_SECTION(_db_handle_lock);
248         }
249
250         if (!result_db_handle)
251                 EM_DEBUG_LOG("no db_handle for [%d] found", current_thread_id);
252
253         EM_DEBUG_FUNC_END();
254         return result_db_handle;
255 }
256
257 int emstorage_set_db_handle(char *multi_user_name, sqlite3 *db_handle)
258 {
259         EM_DEBUG_FUNC_BEGIN();
260         int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
261         pthread_t current_thread_id = THREAD_SELF();
262
263         ENTER_CRITICAL_SECTION(_db_handle_lock);
264         for (i = 0; i < MAX_DB_CLIENT; i++)     {
265                 if (_db_handle_list[i].thread_id == 0) {
266                         _db_handle_list[i].thread_id = current_thread_id;
267                         _db_handle_list[i].db_handle = db_handle;
268                         /* Only distinguished container and host  */
269                         _db_handle_list[i].user_name = EM_SAFE_STRDUP(multi_user_name);
270                         EM_DEBUG_LOG("current_thread_id [%d], index [%d]", current_thread_id, i);
271                         error_code =  EMAIL_ERROR_NONE;
272                         break;
273                 }
274         }
275         LEAVE_CRITICAL_SECTION(_db_handle_lock);
276
277         if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
278                 EM_DEBUG_EXCEPTION("Exceeded the limitation of db client. Can't find empty slot in _db_handle_list.");
279
280         EM_DEBUG_FUNC_END("error_code [%d]", error_code);
281         return error_code;
282 }
283
284 int emstorage_remove_db_handle()
285 {
286         EM_DEBUG_FUNC_BEGIN();
287         int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
288         ENTER_CRITICAL_SECTION(_db_handle_lock);
289         for (i = 0; i < MAX_DB_CLIENT; i++) {
290                 if (_db_handle_list[i].thread_id == THREAD_SELF()) {
291                         _db_handle_list[i].thread_id = 0;
292                         _db_handle_list[i].db_handle = NULL;
293                         EM_SAFE_FREE(_db_handle_list[i].user_name);
294
295                         EM_DEBUG_LOG("index [%d]", i);
296                         error_code = EMAIL_ERROR_NONE;
297                         break;
298                 }
299         }
300         LEAVE_CRITICAL_SECTION(_db_handle_lock);
301
302         if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
303                 EM_DEBUG_EXCEPTION("Can't find proper thread_id");
304
305         EM_DEBUG_FUNC_END("error_code [%d]", error_code);
306         return error_code;
307 }
308
309 int emstorage_reset_db_handle_list()
310 {
311         EM_DEBUG_FUNC_BEGIN();
312         int i;
313
314         ENTER_CRITICAL_SECTION(_db_handle_lock);
315         for (i = 0; i < MAX_DB_CLIENT; i++) {
316                 _db_handle_list[i].thread_id = 0;
317                 _db_handle_list[i].db_handle = NULL;
318                 EM_SAFE_FREE(_db_handle_list[i].user_name);
319         }
320         LEAVE_CRITICAL_SECTION(_db_handle_lock)
321
322                 EM_DEBUG_FUNC_END();
323         return EMAIL_ERROR_NONE;
324 }
325
326
327 sqlite3 *emstorage_get_db_connection(char *multi_user_name)
328 {
329         return emstorage_db_open(multi_user_name, NULL);
330 }
331
332 #else   /*  _MULTIPLE_DB_HANDLE */
333
334 sqlite3 *_db_handle = NULL;
335
336 sqlite3 *emstorage_get_db_connection()
337 {
338         if (NULL == _db_handle)
339                 emstorage_db_open(NULL);
340         return _db_handle;
341 }
342 #endif  /*  _MULTIPLE_DB_HANDLE */
343
344 /* ------------------------------------------------------------------------------ */
345 /*  Mutex using shared memory */
346 typedef struct {
347         pthread_mutex_t mutex;
348         int data;
349 } mmapped_t;
350
351 mmapped_t       *mapped_for_db_lock = NULL;
352 int              shm_fd_for_db_lock = 0;
353
354 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
355 mmapped_t       *mapped_for_generating_mail_id = NULL;
356 int              shm_fd_for_generating_mail_id = 0;
357 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
358
359 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__
360 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
361         do {\
362                 _timedlock_shm_mutex(mapped_for_db_lock, 2);\
363                 return_value = function_call;\
364                 _unlockshm_mutex(mapped_for_db_lock);\
365         } while (0)
366
367 #else /*  __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
368 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
369 {  return_value = function_call; }
370 #endif /*  __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
371
372 static int emstorage_exec_query_by_prepare_v2(sqlite3 *local_db_handle, char *query_string)
373 {
374         EM_DEBUG_FUNC_BEGIN("local_db_handle[%p] query_string[%p]", local_db_handle, query_string);
375         int error = EMAIL_ERROR_NONE;
376         int rc = 0;
377         DB_STMT db_statement = NULL;
378
379         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, query_string, EM_SAFE_STRLEN(query_string), &db_statement, NULL), rc);
380         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
381                         ("sqlite3_prepare failed [%d] [%s]", rc, query_string));
382
383         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(db_statement), rc);
384         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
385                         ("sqlite3_step failed [%d] [%s]", rc, query_string));
386
387 FINISH_OFF:
388
389         if (db_statement != NULL) {
390                 rc = sqlite3_finalize(db_statement);
391                 if (rc != SQLITE_OK) {
392                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
393                         error = EMAIL_ERROR_DB_FAILURE;
394                 }
395         }
396
397         return error;
398 }
399
400 INTERNAL_FUNC int emstorage_shm_file_init(const char *shm_file_name)
401 {
402         EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
403         char errno_buf[ERRNO_BUF_SIZE] = {0};
404
405         if (!shm_file_name) {
406                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
407                 return EMAIL_ERROR_INVALID_PARAM;
408         }
409
410         int fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); /*  note: permission is not working */
411         if (fd < 0) {
412                 EM_DEBUG_EXCEPTION("shm_open errno [%d] [%s]", errno, EM_STRERROR(errno_buf));
413                 return EMAIL_ERROR_SYSTEM_FAILURE;
414         }
415
416         fchmod(fd, 0666);
417         EM_DEBUG_LOG("** Create SHM FILE **");
418         if (ftruncate(fd, sizeof(mmapped_t)) != 0) {
419                 EM_DEBUG_EXCEPTION("ftruncate errno [%d]", errno);
420                 return EMAIL_ERROR_SYSTEM_FAILURE;
421         }
422
423         mmapped_t *m = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
424         if (m == MAP_FAILED) {
425                 EM_DEBUG_EXCEPTION("mmap errno [%d]", errno);
426                 EM_SAFE_CLOSE(fd);
427                 return EMAIL_ERROR_SYSTEM_FAILURE;
428         }
429
430         m->data = 0;
431
432         pthread_mutexattr_t mattr;
433         pthread_mutexattr_init(&mattr);
434         pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
435         pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
436         pthread_mutex_init(&(m->mutex), &mattr);
437         pthread_mutexattr_destroy(&mattr);
438
439         pthread_mutex_destroy(&(m->mutex));
440         munmap(m, sizeof(mmapped_t));
441
442         EM_SAFE_CLOSE(fd);
443         EM_DEBUG_FUNC_END();
444         return EMAIL_ERROR_NONE;
445 }
446
447 int emstorage_shm_file_destroy(const char *shm_file_name)
448 {
449         EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
450         char errno_buf[ERRNO_BUF_SIZE] = {0};
451
452         if (!shm_file_name) {
453                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
454                 return EMAIL_ERROR_INVALID_PARAM;
455         }
456
457         if (shm_unlink(shm_file_name) != 0)
458                 EM_DEBUG_EXCEPTION("shm_unlink failed: %s", EM_STRERROR(errno_buf));
459         EM_DEBUG_FUNC_END();
460         return EMAIL_ERROR_NONE;
461 }
462
463 int _initialize_shm_mutex(const char *shm_file_name, int *param_shm_fd, mmapped_t **param_mapped)
464 {
465         EM_DEBUG_FUNC_BEGIN("shm_file_name [%p] param_shm_fd [%p], param_mapped [%p]", shm_file_name, param_shm_fd, param_mapped);
466         char errno_buf[ERRNO_BUF_SIZE] = {0};
467
468         if (!shm_file_name || !param_shm_fd || !param_mapped) {
469                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
470                 return EMAIL_ERROR_INVALID_PARAM;
471         }
472
473         if (!(*param_mapped)) {
474                 EM_DEBUG_LOG("** mapping begin **");
475                 if (!(*param_shm_fd)) { /*  open shm_file_name at first. Otherwise, the num of files in /proc/pid/fd will be increasing  */
476                         *param_shm_fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
477                         if ((*param_shm_fd) == -1) {
478                                 EM_DEBUG_EXCEPTION("FAIL: shm_open(): %s", EM_STRERROR(errno_buf));
479                                 return EMAIL_ERROR_SYSTEM_FAILURE;
480                         }
481                 }
482
483                 fchmod((*param_shm_fd), 0666);
484                 EM_DEBUG_LOG("** Create SHM FILE **");
485                 if (ftruncate((*param_shm_fd), sizeof(mmapped_t)) != 0) {
486                         EM_DEBUG_EXCEPTION("ftruncate errno [%d]", errno);
487                         return EMAIL_ERROR_SYSTEM_FAILURE;
488                 }
489
490                 mmapped_t *tmp = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ|PROT_WRITE, MAP_SHARED, (*param_shm_fd), 0);
491                 if (tmp == MAP_FAILED) {
492                         EM_DEBUG_EXCEPTION("mmap failed: %s", EM_STRERROR(errno_buf));
493                         return EMAIL_ERROR_SYSTEM_FAILURE;
494                 }
495
496                 tmp->data = 0;
497
498                 pthread_mutexattr_t mattr;
499                 pthread_mutexattr_init(&mattr);
500                 pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
501                 pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
502                 pthread_mutex_init(&(tmp->mutex), &mattr);
503                 pthread_mutexattr_destroy(&mattr);
504
505                 *param_mapped = tmp;
506         }
507
508         EM_DEBUG_FUNC_END();
509         return EMAIL_ERROR_NONE;
510 }
511
512 int _timedlock_shm_mutex(mmapped_t *param_mapped, int sec)
513 {
514         EM_DEBUG_FUNC_BEGIN("param_mapped [%p], sec [%d]", param_mapped, sec);
515
516         if (!param_mapped) {
517                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
518                 return EMAIL_ERROR_INVALID_PARAM;
519         }
520
521         struct timespec abs_time;
522         clock_gettime(CLOCK_REALTIME, &abs_time);
523         abs_time.tv_sec += sec;
524         char errno_buf[ERRNO_BUF_SIZE] = {0};
525
526         int err = pthread_mutex_timedlock(&(param_mapped->mutex), &abs_time);
527
528         if (err == EOWNERDEAD) {
529                 err = pthread_mutex_consistent(&(param_mapped->mutex));
530                 EM_DEBUG_EXCEPTION("Previous owner is dead with lock. Fix mutex : %s", EM_STRERROR(errno_buf));
531         } else if (err != 0) {
532                 EM_DEBUG_EXCEPTION("ERROR : %s", EM_STRERROR(errno_buf));
533                 return err;
534         }
535
536         EM_DEBUG_FUNC_END();
537         return EMAIL_ERROR_NONE;
538 }
539
540 void _unlockshm_mutex(mmapped_t *param_mapped)
541 {
542         EM_DEBUG_FUNC_BEGIN();
543         pthread_mutex_unlock(&(param_mapped->mutex));
544         EM_DEBUG_FUNC_END();
545 }
546 /* ------------------------------------------------------------------------------ */
547
548
549 static int _open_counter = 0;
550
551 static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name);
552 static int _read_password_from_secure_storage(char *file_name, char **password);
553
554 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
555 static int _get_cert_password_file_name(int index, char *cert_password_file_name);
556 #endif
557
558 typedef struct {
559         const char *object_name;
560         unsigned int data_flag;
561 } email_db_object_t;
562
563 static const email_db_object_t _g_db_tables[] = {
564         { "mail_read_mail_uid_tbl", 1},
565         { "mail_tbl", 1},
566         { "mail_attachment_tbl", 1},
567         { NULL,  0},
568 };
569
570 static const email_db_object_t _g_db_indexes[] = {
571         { "mail_read_mail_uid_idx1", 1},
572         { "mail_idx1", 1},
573         { "mail_attachment_idx1", 1},
574         { NULL,  1},
575 };
576
577 enum {
578         CREATE_TABLE_MAIL_ACCOUNT_TBL,
579         CREATE_TABLE_MAIL_BOX_TBL,
580         CREATE_TABLE_MAIL_READ_MAIL_UID_TBL,
581         CREATE_TABLE_MAIL_RULE_TBL,
582         CREATE_TABLE_MAIL_TBL,
583         CREATE_TABLE_MAIL_ATTACHMENT_TBL,
584 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
585         CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL,
586 #else
587         CREATE_TABLE_DUMMY_INDEX1,
588 #endif
589         CREATE_TABLE_MAIL_MEETING_TBL,
590 #ifdef __FEATURE_LOCAL_ACTIVITY__
591         CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL,
592 #else
593         CREATE_TABLE_DUMMY_INDEX2,
594 #endif
595         CREATE_TABLE_MAIL_CERTIFICATE_TBL,
596         CREATE_TABLE_MAIL_TASK_TBL,
597 #ifdef __FEATURE_BODY_SEARCH__
598         CREATE_TABLE_MAIL_TEXT_TBL,
599 #else
600         CREATE_TABLE_DUMMY_INDEX3,
601 #endif
602
603 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
604         CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
605 #else
606         CREATE_TABLE_DUMMY_INDEX4,
607 #endif
608
609         /*CREATE INDEX*/
610         CREATE_TABLE_MAIL_ACCOUNT_IDX,
611         CREATE_TABLE_MAIL_BOX_IDX,
612         CREATE_TABLE_MAIL_READ_MAIL_UID_IDX,
613         CREATE_TABLE_MAIL_IDX,
614         CREATE_TABLE_MAIL_ATTACHMENT_IDX,
615         CREATE_TABLE_MAIL_MEETING_IDX,
616         CREATE_TABLE_MAIL_TASK_IDX,
617         CREATE_TABLE_MAIL_DATETIME_IDX,
618         CREATE_TABLE_MAIL_THREAD_IDX,
619         CREATE_TABLE_MAX,
620 };
621
622 enum {
623         DATA1_IDX_IN_MAIL_ACTIVITY_TBL = 0,
624         TRANSTYPE_IDX_IN_MAIL_ACTIVITY_TBL,
625         FLAG_IDX_IN_MAIL_ACTIVITY_TBL,
626 };
627
628 enum {
629         CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL = 0,
630         ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
631         ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
632         ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
633         EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
634         EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
635         EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
636         ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL,
637         EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL,
638         SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL,
639         FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL,
640 };
641
642 enum {
643         TO_RECIPIENT = 0,
644         CC_RECIPIENT,
645         BCC_RECIPIENT,
646 };
647 enum {
648         ACCOUNT_ID_IDX_IN_MAIL_BOX_TBL = 0,
649         LOCAL_YN_IDX_IN_MAIL_BOX_TBL,
650         MAILBOX_NAME_IDX_IN_MAIL_BOX_TBL,
651         MAILBOX_TYPE_IDX_IN_MAIL_BOX_TBL,
652         ALIAS_IDX_IN_MAIL_BOX_TBL,
653         SYNC_WITH_SERVER_YN_IDX_IN_MAIL_BOX_TBL,
654         MODIFIABLE_YN_IDX_IN_MAIL_BOX_TBL,
655         TOTAL_MAIL_COUNT_ON_SERVER_IDX_IN_MAIL_BOX_TBL,
656         ARCHIVE_IDX_IN_MAIL_BOX_TBL,
657         MAIL_SLOT_SIZE_IDX_IN_MAIL_BOX_TBL,
658 };
659
660 enum {
661         ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL = 0,
662         LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
663         MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL,
664         LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
665         SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
666         RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL,
667         SYNC_STATUS_IDX_IN_MAIL_READ_MAIL_UID_TBL,
668         FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL,
669         FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL,
670         IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL,  /* unused */
671 };
672
673 #ifdef __FEATURE_BODY_SEARCH__
674 enum {
675         MAIL_ID_IDX_IN_MAIL_TEXT_TBL = 0,
676         ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL,
677         MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL,
678         BODY_TEXT_IDX_IN_MAIL_TEXT_TBL,
679         FIELD_COUNT_OF_MAIL_TEXT_TBL,
680 };
681 #endif
682
683 enum {
684         ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL = 0,
685         RULE_ID_IDX_IN_MAIL_RULE_TBL,
686         FILTER_NAME_IDX_IN_MAIL_RULE_TBL,
687         TYPE_IDX_IN_MAIL_RULE_TBL,
688         VALUE_IDX_IN_MAIL_RULE_TBL,
689         VALUE2_IDX_IN_MAIL_RULE_TBL,
690         ACTION_TYPE_IDX_IN_MAIL_RULE_TBL,
691         TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL,
692         FLAG1_IDX_IN_MAIL_RULE_TBL,
693         FLAG2_IDX_IN_MAIL_RULE_TBL,
694 };
695
696 enum {
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         ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL = 0,
758         ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL,
759         ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL,
760         CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
761         ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL,
762         MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
763         ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
764         MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
765         ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
766         ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
767         ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL,
768         ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
769         ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
770 #ifdef __ATTACHMENT_OPTI__
771         ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL,
772         SECTION_IDX_IN_MAIL_ATTACHMENT_TBL,
773 #endif
774 };
775
776 enum {
777         IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL = 0,
778 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
779         MAIL_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
780         ACCOUNT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
781         ADDRESS_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
782         ADDRESS_IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL,
783 #endif
784         ADDRESS_IDX_IN_MAIL_CONTACT_SYNC_TBL,
785         CONTACT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
786         STORAGE_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
787         CONTACT_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
788 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
789         DISPLAY_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
790         FLAG1_IDX_IN_MAIL_CONTACT_SYNC_TBL,
791 #endif
792 };
793
794 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
795 enum {
796         ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL = 0,
797         STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
798         ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
799         MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
800         SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
801         MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
802         MULTI_USER_NAME_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
803 };
804 #endif
805
806 /* sowmya.kr 03032010, changes for get list of mails for given addr list */
807 typedef struct _em_mail_id_list {
808         int mail_id;
809         struct _em_mail_id_list *next;
810 } em_mail_id_list;
811
812 static char *g_test_query[] = {
813         /*  1. select mail_account_tbl */
814         "SELECT"
815                 " account_name, "
816                 " incoming_server_type, "
817                 " incoming_server_address, "
818                 " user_email_address, "
819                 " incoming_server_user_name, "
820                 " incoming_server_password, "
821                 " retrieval_mode, "
822                 " incoming_server_port_number, "
823                 " incoming_server_secure_connection, "
824                 " incoming_server_authentication_method,"
825                 " outgoing_server_type, "
826                 " outgoing_server_address, "
827                 " outgoing_server_port_number, "
828                 " outgoing_server_need_authentication, "
829                 " outgoing_server_secure_connection, "
830                 " outgoing_server_user_name, "
831                 " outgoing_server_password, "
832                 " display_name, "
833                 " reply_to_addr, "
834                 " return_addr, "
835                 " account_id, "
836                 " keep_mails_on_pop_server_after_download, "
837                 " auto_resend_times, "
838                 " outgoing_server_size_limit, "
839                 " wifi_auto_download, "
840                 " pop_before_smtp, "
841                 " incoming_server_requires_apop,"
842                 " logo_icon_path, "
843                 " is_preset_account, "
844                 " check_interval, "
845                 " priority, "
846                 " keep_local_copy, "
847                 " req_delivery_receipt, "
848                 " req_read_receipt, "
849                 " download_limit, "
850                 " block_address, "
851                 " block_subject, "
852                 " display_name_from, "
853                 " reply_with_body, "
854                 " forward_with_files, "
855                 " add_myname_card, "
856                 " add_signature, "
857                 " signature"
858                 ", add_my_address_to_bcc"
859                 ", notification_status "
860                 ", vibrate_status "
861                 ", display_content_status "
862                 ", default_ringtone_status "
863                 ", alert_ringtone_path "
864                 ", account_svc_id "
865                 ", index_color "
866                 ", sync_status "
867                 ", sync_disabled "
868                 ", smime_type"
869                 ", certificate_path"
870                 ", cipher_type"
871                 ", digest_type"
872                 " FROM mail_account_tbl",
873         /*  2. select mail_box_tbl */
874         "SELECT "
875                 "   mailbox_id, "
876                 "   account_id, "
877                 "   local_yn,  "
878                 "   mailbox_name,  "
879                 "   mailbox_type,   "
880                 "   alias,  "
881                 "   deleted_flag,  "
882                 "   modifiable_yn,  "
883                 "   total_mail_count_on_server,  "
884                 "   has_archived_mails, "
885                 "   mail_slot_size, "
886                 "   no_select, "
887                 "   last_sync_time "
888                 " FROM mail_box_tbl ",
889         /*  3. select mail_read_mail_uid_tbl */
890         "SELECT  "
891                 "   account_id,  "
892                 "   mailbox_id,  "
893                 "   mailbox_name,  "
894                 "   local_uid,  "
895                 "   server_uid,  "
896                 "   rfc822_size ,  "
897                 "   sync_status,  "
898                 "   flags_seen_field,  "
899                 "   idx_num "
900                 " FROM mail_read_mail_uid_tbl ",
901         /*  4. select mail_rule_tbl */
902         "SELECT "
903                 "   account_id, "
904                 "   rule_id, "
905                 "   filter_name, "
906                 "   type, "
907                 "   value, "
908                 "   value2, "
909                 "   action_type, "
910                 "   target_mailbox_id,  "
911                 "   flag1, "
912                 "   flag2 "
913                 " FROM mail_rule_tbl    ",
914         /*  5. select mail_tbl */
915         "SELECT"
916                 "       mail_id, "
917                 "       account_id, "
918                 "       mailbox_id, "
919                 "       mailbox_name, "
920                 "   mailbox_type, "
921                 "   subject, "
922                 "       date_time, "
923                 "       server_mail_status, "
924                 "       server_mailbox_name, "
925                 "       server_mail_id, "
926                 "   message_id, "
927                 "       reference_mail_id, "
928                 "   full_address_from, "
929                 "   full_address_reply, "
930                 "   full_address_to, "
931                 "   full_address_cc, "
932                 "   full_address_bcc, "
933                 "   full_address_return, "
934                 "   email_address_sender, "
935                 "   email_address_recipient, "
936                 "   alias_sender, "
937                 "   alias_recipient, "
938                 "       body_download_status, "
939                 "       file_path_plain, "
940                 "       file_path_html, "
941                 "   file_path_mime_entity, "
942                 "       mail_size, "
943                 "   flags_seen_field     ,"
944                 "   flags_deleted_field  ,"
945                 "   flags_flagged_field  ,"
946                 "   flags_answered_field ,"
947                 "   flags_recent_field   ,"
948                 "   flags_draft_field    ,"
949                 "   flags_forwarded_field,"
950                 "       DRM_status, "
951                 "       priority, "
952                 "       save_status, "
953                 "       lock_status, "
954                 "       report_status, "
955                 "   attachment_count, "
956                 "       inline_content_count, "
957                 "       thread_id, "
958                 "       thread_item_count, "
959                 "   preview_text, "
960                 "       meeting_request_status, "
961                 "   message_class, "
962                 "   digest_type, "
963                 "   smime_type "
964                 " FROM mail_tbl",
965         /*  6. select mail_attachment_tbl */
966         "SELECT "
967                 "       attachment_id, "
968                 "       attachment_name, "
969                 "       attachment_path, "
970                 "       attachment_size, "
971                 "       mail_id,  "
972                 "       account_id, "
973                 "       mailbox_id, "
974                 "       attachment_save_status,  "
975                 "       attachment_drm_type,  "
976                 "       attachment_drm_method,  "
977                 "       attachment_inline_content_status,  "
978                 "       attachment_mime_type  "
979                 " FROM mail_attachment_tbl ",
980
981 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
982         "SELECT  "
983                 "   account_id, "
984                 "   mail_id, "
985                 "   server_mail_id, "
986                 "   activity_id, "
987                 "   activity_type, "
988                 "   mailbox_id, "
989                 "   mailbox_name "
990                 "   multi_user_name "
991                 " FROM mail_partial_body_activity_tbl ",
992 #endif
993
994         "SELECT  "
995                 "   mail_id, "
996                 "   account_id, "
997                 "   mailbox_id, "
998                 "   meeting_response, "
999                 "   start_time, "
1000                 "   end_time, "
1001                 "   location, "
1002                 "   global_object_id, "
1003                 "   offset, "
1004                 "   standard_name, "
1005                 "   standard_time_start_date, "
1006                 "   standard_bias, "
1007                 "   daylight_name, "
1008                 "   daylight_time_start_date, "
1009                 "   daylight_bias "
1010                 " FROM mail_meeting_tbl ",
1011
1012 #ifdef __FEATURE_LOCAL_ACTIVITY__
1013         "SELECT "
1014                 "       activity_id, "
1015                 "   account_id, "
1016                 "   mail_id, "
1017                 "   activity_type, "
1018                 "   server_mailid, "
1019                 "   src_mbox , "
1020                 "       dest_mbox "
1021                 " FROM mail_local_activity_tbl  ",
1022 #endif
1023         "SELECT "
1024                 "       certificate_id, "
1025                 "   issue_year, "
1026                 "   issue_month, "
1027                 "   issue_day, "
1028                 "   expiration_year, "
1029                 "   expiration_month, "
1030                 "   expiration_day, "
1031                 "   issue_organization_name, "
1032                 "   email_address, "
1033                 "   subject_str, "
1034                 "   filepath, "
1035                 "   password "
1036                 " FROM mail_certificate_tbl     ",
1037         "SELECT "
1038                 "       task_id, "
1039                 "   task_type, "
1040                 "   task_status, "
1041                 "   task_priority, "
1042                 "   task_parameter_length, "
1043                 "   task_parameter , "
1044                 "       date_time "
1045                 " FROM mail_task_tbl    ",
1046 #ifdef __FEATURE_BODY_SEARCH__
1047         "SELECT "
1048                 "       mail_id, "
1049                 "   account_id, "
1050                 "   mailbox_id, "
1051                 "   body_text "
1052                 " FROM mail_text_tbl    ",
1053 #endif
1054 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
1055         "SELECT  "
1056                 "   activity_id, "
1057                 "   status, "
1058                 "   account_id, "
1059                 "   mail_id, "
1060                 "   server_mail_id, "
1061                 "   mailbox_id, "
1062                 "   multi_user_name, "
1063                 " FROM mail_auto_download_activity_tbl ",
1064 #endif
1065         NULL,
1066 };
1067
1068 int _field_count_of_table[CREATE_TABLE_MAX] = { 0, };
1069
1070 static int _get_table_field_data_char(char  **table, char *buf, int index)
1071 {
1072         if ((table == NULL) || (buf == NULL) || (index < 0)) {
1073                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1074                 return false;
1075         }
1076
1077         if (table[index] != NULL) {
1078                 *buf = (char)atoi(table[index]);
1079                 return true;
1080         }
1081
1082         /*  EM_DEBUG_LOG("Empty field. Set as zero"); */
1083
1084         *buf = 0;
1085         return false;
1086 }
1087
1088 static int _get_table_field_data_int(char  **table, int *buf, int index)
1089 {
1090         if ((table == NULL) || (buf == NULL) || (index < 0)) {
1091                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1092                 return false;
1093         }
1094
1095         if (table[index] != NULL) {
1096                 *buf = atoi(table[index]);
1097                 return true;
1098         }
1099
1100         /*  EM_DEBUG_LOG("Empty field. Set as zero"); */
1101
1102         *buf = 0;
1103         return false;
1104 }
1105
1106 static int _get_table_field_data_time_t(char  **table, time_t *buf, int index)
1107 {
1108         if ((table == NULL) || (buf == NULL) || (index < 0)) {
1109                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1110                 return false;
1111         }
1112
1113         if (table[index] != NULL) {
1114                 *buf = (time_t)atol(table[index]);
1115                 return true;
1116         }
1117
1118         /*  EM_DEBUG_LOG("Empty field. Set as zero"); */
1119
1120         *buf = 0;
1121         return false;
1122 }
1123
1124 static int _get_table_field_data_string(char **table, char **buf, int ucs2, int index)
1125 {
1126         int ret = false;
1127
1128         if ((table == NULL) || (buf == NULL) || (index < 0)) {
1129                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1130                 return false;
1131         }
1132
1133         char *pTemp = table[index];
1134         int sLen = 0;
1135         if (pTemp == NULL)
1136                 *buf = NULL;
1137         else {
1138                 sLen = EM_SAFE_STRLEN(pTemp);
1139                 if (sLen) {
1140                         *buf = (char *) em_malloc(sLen + 1);
1141                         if (*buf == NULL) {
1142                                 EM_DEBUG_EXCEPTION("malloc is failed");
1143                                 goto FINISH_OFF;
1144                         }
1145                         strncpy(*buf, pTemp, sLen+1);
1146                         (*buf)[sLen] = '\0';
1147                 } else
1148                         *buf = NULL;
1149         }
1150 #ifdef _PRINT_STORAGE_LOG_
1151         if (*buf)
1152                 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", *buf, index);
1153         else
1154                 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1155 #endif
1156         ret = true;
1157 FINISH_OFF:
1158
1159         return ret;
1160 }
1161
1162 static int _get_table_field_data_string_without_allocation(char **table, char *buf, int buffer_size, int ucs2, int index)
1163 {
1164         if ((table == NULL) || (buf == NULL) || (index < 0)) {
1165                 EM_DEBUG_EXCEPTION(" table[%p], buf[%p], index[%d]", table, buf, index);
1166                 return false;
1167         }
1168
1169         char *pTemp = table[index];
1170
1171         if (pTemp == NULL)
1172                 buf = NULL;
1173         else {
1174                 memset(buf, 0, buffer_size);
1175                 strncpy(buf, pTemp, buffer_size - 1);
1176         }
1177 #ifdef _PRINT_STORAGE_LOG_
1178         if (buf)
1179                 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", buf, index);
1180         else
1181                 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1182 #endif
1183
1184         return true;
1185 }
1186
1187 static int _get_table_field_data_blob(char **table, void **buffer, int buffer_size, int index)
1188 {
1189         if ((table == NULL) || (buffer == NULL) || (index < 0)) {
1190                 EM_DEBUG_EXCEPTION(" table[%p], buffer[%p], buffer_size [%d], index[%d]", table, buffer, buffer_size, index);
1191                 return false;
1192         }
1193
1194         char *temp_buffer = table[index];
1195
1196         if (temp_buffer == NULL)
1197                 buffer = NULL;
1198         else {
1199                 *buffer = malloc(buffer_size);
1200                 if (*buffer == NULL) {
1201                         EM_DEBUG_EXCEPTION("allocation failed.");
1202                         return false;
1203                 }
1204                 memset(*buffer, 0, buffer_size);
1205                 memcpy(*buffer, temp_buffer, buffer_size);
1206         }
1207 #ifdef _PRINT_STORAGE_LOG_
1208         if (buf)
1209                 EM_DEBUG_LOG("_get_table_field_data_string - buffer[%s], index[%d]", buffer, index);
1210         else
1211                 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1212 #endif
1213
1214         return true;
1215 }
1216
1217 static int _get_stmt_field_data_char(DB_STMT hStmt, char *buf, int index)
1218 {
1219         if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1220                 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1221                 return false;
1222         }
1223
1224         if (sqlite3_column_text(hStmt, index) != NULL) {
1225                 *buf = (char)sqlite3_column_int(hStmt, index);
1226 #ifdef _PRINT_STORAGE_LOG_
1227                 EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1228 #endif
1229                 return true;
1230         }
1231
1232         EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1233
1234         return false;
1235 }
1236
1237 static int _get_stmt_field_data_int(DB_STMT hStmt, int *buf, int index)
1238 {
1239         if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1240                 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1241                 return false;
1242         }
1243
1244         if (sqlite3_column_text(hStmt, index) != NULL) {
1245                 *buf = sqlite3_column_int(hStmt, index);
1246 #ifdef _PRINT_STORAGE_LOG_
1247                 EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1248 #endif
1249                 return true;
1250         }
1251
1252         EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1253
1254         return false;
1255 }
1256
1257 static int _get_stmt_field_data_time_t(DB_STMT hStmt, time_t *buf, int index)
1258 {
1259         if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1260                 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1261                 return false;
1262         }
1263
1264         if (sqlite3_column_text(hStmt, index) != NULL) {
1265                 *buf = (time_t)sqlite3_column_int(hStmt, index);
1266 #ifdef _PRINT_STORAGE_LOG_
1267                 EM_DEBUG_LOG("_get_stmt_field_data_time_t [%d]", *buf);
1268 #endif
1269                 return true;
1270         }
1271
1272         EM_DEBUG_LOG("_get_stmt_field_data_time_t fail. index [%d]", index);
1273         return false;
1274 }
1275
1276 static int _get_stmt_field_data_string(DB_STMT hStmt, char **buf, int ucs2, int index)
1277 {
1278         if (!hStmt || !buf || (index < 0)) { /*prevent 39619*/
1279                 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1280                 return false;
1281         }
1282
1283         int sLen = 0;
1284         sLen = sqlite3_column_bytes(hStmt, index);
1285
1286 #ifdef _PRINT_STORAGE_LOG_
1287         EM_DEBUG_LOG("_get_stmt_field_data_string sqlite3_column_bytes sLen[%d]", sLen);
1288 #endif
1289
1290         if (sLen > 0) {
1291                 *buf = (char *) em_malloc(sLen + 1);
1292                 if (*buf == NULL) {
1293                         EM_DEBUG_EXCEPTION("em_mallocfailed");
1294                         return false;
1295                 }
1296
1297                 strncpy(*buf, (char *)sqlite3_column_text(hStmt, index), sLen);
1298         } else
1299                 *buf = NULL;
1300
1301 #ifdef _PRINT_STORAGE_LOG_
1302         if (*buf)
1303                 EM_DEBUG_LOG("buf[%s], index[%d]", *buf, index);
1304         else
1305                 EM_DEBUG_LOG("_get_stmt_field_data_string - No string got");
1306 #endif
1307
1308         return false;
1309 }
1310
1311 static void _get_stmt_field_data_blob(DB_STMT hStmt, void **buf, int index)
1312 {
1313         if (!hStmt || !buf || (index < 0)) { /*prevent 39618*/
1314                 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1315                 return;
1316         }
1317
1318         int sLen = 0;
1319         sLen = sqlite3_column_bytes(hStmt, index);
1320
1321 #ifdef _PRINT_STORAGE_LOG_
1322         EM_DEBUG_LOG("_get_stmt_field_data_blob sqlite3_column_bytes sLen[%d]", sLen);
1323 #endif
1324
1325         if (sLen > 0) {
1326                 *buf = (char *) em_malloc(sLen);
1327                 if (*buf == NULL) {
1328                         EM_DEBUG_EXCEPTION("em_mallocfailed");
1329                         return;
1330                 }
1331
1332                 memcpy(*buf, (void *)sqlite3_column_blob(hStmt, index), sLen);
1333         } else
1334                 *buf = NULL;
1335
1336 }
1337
1338 static int _bind_stmt_field_data_char(DB_STMT hStmt, int index, char value)
1339 {
1340         if ((hStmt == NULL) || (index < 0)) {
1341                 EM_DEBUG_EXCEPTION("index[%d]", index);
1342                 return false;
1343         }
1344
1345         int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1346
1347         if (ret != SQLITE_OK) {
1348                 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1349                 return false;
1350         }
1351
1352         return true;
1353 }
1354
1355 static int _bind_stmt_field_data_int(DB_STMT hStmt, int index, int value)
1356 {
1357         if ((hStmt == NULL) || (index < 0)) {
1358                 EM_DEBUG_EXCEPTION("index[%d]", index);
1359                 return false;
1360         }
1361
1362         int ret = sqlite3_bind_int(hStmt, index+1, value);
1363
1364         if (ret != SQLITE_OK) {
1365                 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1366                 return false;
1367         }
1368
1369         return true;
1370 }
1371
1372 static int _bind_stmt_field_data_time_t(DB_STMT hStmt, int index, time_t value)
1373 {
1374         if ((hStmt == NULL) || (index < 0)) {
1375                 EM_DEBUG_EXCEPTION("index[%d]", index);
1376                 return false;
1377         }
1378
1379         int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1380
1381         if (ret != SQLITE_OK) {
1382                 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1383                 return false;
1384         }
1385
1386         return true;
1387 }
1388
1389 static int _bind_stmt_field_data_string(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1390 {
1391         if ((hStmt == NULL) || (index < 0)) {
1392                 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1393                 return false;
1394         }
1395
1396 #ifdef _PRINT_STORAGE_LOG_
1397         EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1398 #endif
1399
1400         int ret = 0;
1401         if (value != NULL)
1402                 ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1403         else
1404                 ret = sqlite3_bind_text(hStmt, index+1, "", -1, SQLITE_STATIC);
1405
1406         if (ret != SQLITE_OK) {
1407                 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1408                 return false;
1409         }
1410         return true;
1411 }
1412
1413 static int _bind_stmt_field_data_nstring(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1414 {
1415         if ((hStmt == NULL) || (index < 0)) {
1416                 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1417                 return false;
1418         }
1419
1420 #ifdef _PRINT_STORAGE_LOG_
1421         EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1422 #endif
1423
1424         int ret = 0;
1425         if (value != NULL) {
1426                 if (strlen(value) <= max_len)
1427                         ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1428                 else {
1429                         char *buf = (char*)em_malloc(sizeof(char) * (max_len));
1430                         if (buf == NULL) {
1431                                 EM_DEBUG_EXCEPTION("em_mallocfailed");
1432                                 return false;
1433                         }
1434                         snprintf(buf, max_len-1, "%s", value);
1435                         ret = sqlite3_bind_text(hStmt, index+1, buf, -1, SQLITE_TRANSIENT);
1436                         g_free(buf);
1437                 }
1438         } else
1439                 ret = sqlite3_bind_text(hStmt, index+1, "", -1, SQLITE_STATIC);
1440
1441         if (ret != SQLITE_OK) {
1442                 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1443                 return false;
1444         }
1445         return true;
1446 }
1447
1448
1449 static int _bind_stmt_field_data_blob(DB_STMT hStmt, int index, void *blob, int blob_size)
1450 {
1451         if ((hStmt == NULL) || (index < 0)) {
1452                 EM_DEBUG_EXCEPTION("index[%d], blob_size[%d]", index, blob_size);
1453                 return false;
1454         }
1455
1456 #ifdef _PRINT_STORAGE_LOG_
1457         EM_DEBUG_LOG("hStmt = %p, index = %d, blob_size = %d, blob = [%p]", hStmt, index, blob_size, blob);
1458 #endif
1459
1460         int ret = 0;
1461         if (blob_size > 0)
1462                 ret = sqlite3_bind_blob(hStmt, index+1, blob, blob_size, SQLITE_STATIC);
1463         else
1464                 ret = sqlite3_bind_null(hStmt, index+1);
1465
1466         if (ret != SQLITE_OK) {
1467                 EM_DEBUG_EXCEPTION("sqlite3_bind_blob fail [%d]", ret);
1468                 return false;
1469         }
1470         return true;
1471 }
1472
1473
1474 static int _delete_temp_file(const char *path)
1475 {
1476         EM_DEBUG_FUNC_BEGIN("path[%p]", path);
1477
1478         DIR *dp = NULL;
1479         struct dirent *entry = NULL;
1480
1481         char buf[1024] = {0x00, };
1482
1483         if ((dp = opendir(path)) == NULL) {
1484                 EM_DEBUG_EXCEPTION("opendir(\"%s\") failed...", path);
1485                 return false;
1486         }
1487
1488         while ((entry = readdir(dp)) != NULL) {
1489                 SNPRINTF(buf, sizeof(buf), "%s/%s", path, entry->d_name);
1490                 remove(buf);
1491         }
1492
1493         closedir(dp);
1494         EM_DEBUG_FUNC_END();
1495         return true;
1496 }
1497
1498 char *cpy_str(char *src)
1499 {
1500         char *p = NULL;
1501
1502         if (src) {
1503                 if (!(p = em_malloc((int)EM_SAFE_STRLEN(src) + 1))) {
1504                         EM_DEBUG_EXCEPTION("mailoc failed...");
1505                         return NULL;
1506                 }
1507                 strncpy(p, src, EM_SAFE_STRLEN(src)+1);
1508         }
1509
1510         return p;
1511 }
1512
1513 static void _emstorage_close_once(void)
1514 {
1515         EM_DEBUG_FUNC_BEGIN();
1516
1517         EM_DEBUG_FUNC_END();
1518 }
1519
1520 INTERNAL_FUNC int emstorage_close(int *err_code)
1521 {
1522         EM_DEBUG_FUNC_BEGIN();
1523
1524         int ret = false;
1525         int error = EMAIL_ERROR_NONE;
1526
1527         if (!emstorage_db_close(NULL, &error))
1528
1529                 if (--_open_counter == 0)
1530                         _emstorage_close_once();
1531
1532         ret = true;
1533
1534         if (err_code != NULL)
1535                 *err_code = error;
1536
1537         EM_DEBUG_FUNC_END("ret [%d]", ret);
1538         return ret;
1539 }
1540
1541 static void *_emstorage_open_once(char *multi_user_name, int *err_code)
1542 {
1543         EM_DEBUG_FUNC_BEGIN();
1544
1545         int error = EMAIL_ERROR_NONE;
1546
1547         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1548                 char buf[MAX_PATH] = {0};
1549                 char *prefix_path = NULL;
1550
1551                 error = emcore_get_container_path(multi_user_name, &prefix_path);
1552                 if (error != EMAIL_ERROR_NONE) {
1553                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
1554                         goto FINISH_OFF;
1555                 }
1556
1557                 memset(buf, 0x00, sizeof(buf));
1558                 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, EMAIL_PATH);
1559                 mkdir(buf, DIRECTORY_PERMISSION);
1560
1561                 memset(buf, 0x00, sizeof(buf));
1562                 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAIL_HOME);
1563                 mkdir(buf, DIRECTORY_PERMISSION);
1564
1565                 memset(buf, 0x00, sizeof(buf));
1566                 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAIL_TEMP);
1567                 mkdir(buf, DIRECTORY_PERMISSION);
1568
1569                 _delete_temp_file(buf);
1570                 EM_SAFE_FREE(prefix_path);
1571         } else {
1572                 mkdir(DATA_PATH, DIRECTORY_PERMISSION);
1573                 mkdir(EMAIL_PATH, DIRECTORY_PERMISSION);
1574                 mkdir(MAIL_HOME, DIRECTORY_PERMISSION);
1575                 mkdir(MAIL_TEMP, DIRECTORY_PERMISSION);
1576
1577                 _delete_temp_file(MAIL_TEMP);
1578         }
1579
1580         if (!emstorage_create_table(multi_user_name, EMAIL_CREATE_DB_NORMAL, &error)) {
1581                 EM_DEBUG_EXCEPTION(" emstorage_create_table failed - %d", error);
1582                 goto FINISH_OFF;
1583         }
1584
1585 FINISH_OFF:
1586
1587
1588         if (err_code != NULL)
1589                 *err_code = error;
1590
1591         return NULL;
1592 }
1593
1594 /*  pData : a parameter which is registered when busy handler is registerd */
1595 /*  count : retry count */
1596 static int _callback_sqlite_busy_handler(void *pData, int count)
1597 {
1598         if (10 - count > 0) {
1599                 struct timespec time = {
1600                         .tv_sec = 0,
1601                         .tv_nsec = (count + 1) * 100 * 1000 * 1000
1602                 };
1603                 EM_DEBUG_LOG("Busy handler called!!: PID[%d] / CNT [%d]", getpid(), count);
1604                 nanosleep(&time, NULL);
1605                 return 1;
1606         } else {
1607                 EM_DEBUG_EXCEPTION("Busy handler will be returned SQLITE_BUSY error PID[%d] / CNT[%d]", getpid(), count);
1608                 return 0;
1609         }
1610 }
1611
1612 static int _callback_collation_utf7_sort(void *data, int length_text_a, const void *text_a,
1613                 int length_text_b, const void *text_b)
1614 {
1615         EM_DEBUG_FUNC_BEGIN();
1616         int result = 0;
1617         char *converted_string_a = NULL;
1618         char *converted_string_b = NULL;
1619
1620         EM_DEBUG_LOG_DEV("text_a : [%s]", text_a);
1621         converted_string_a = emcore_convert_mutf7_to_utf8((char *)text_a);
1622         EM_DEBUG_LOG_DEV("Converted text_a : [%s]", converted_string_a);
1623
1624         EM_DEBUG_LOG_DEV("text_b : [%s]", text_b);
1625         converted_string_b = emcore_convert_mutf7_to_utf8((char *)text_b);
1626         EM_DEBUG_LOG_DEV("Converted text_b : [%s]", converted_string_b);
1627
1628         if (converted_string_a && converted_string_b)
1629                 result = strcmp(converted_string_a, converted_string_b);
1630
1631         EM_SAFE_FREE(converted_string_a);
1632         EM_SAFE_FREE(converted_string_b);
1633
1634         EM_DEBUG_FUNC_END();
1635         return result;
1636 }
1637
1638 static int _delete_all_files_and_directories(char *db_file_path, int *err_code)
1639 {
1640         EM_DEBUG_FUNC_BEGIN();
1641
1642         int error = EMAIL_ERROR_NONE;
1643         int ret = false;
1644
1645         if (!emstorage_delete_file(db_file_path, &error)) {
1646                 if (error != EMAIL_ERROR_FILE_NOT_FOUND) {
1647                         EM_DEBUG_EXCEPTION_SEC("remove failed - %s", EMAIL_SERVICE_DB_FILE_PATH);
1648                         goto FINISH_OFF;
1649                 }
1650         }
1651
1652         if (!emstorage_delete_dir((char *)MAIL_HOME, &error)) {
1653                 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed");
1654                 goto FINISH_OFF;
1655         }
1656
1657         ret = true;
1658
1659 FINISH_OFF:
1660         if (err_code)
1661                 *err_code = error;
1662         EM_DEBUG_FUNC_END();
1663         return ret;
1664 }
1665
1666 static int _recovery_from_malformed_db_file(char *db_file_path, int *err_code)
1667 {
1668         EM_DEBUG_FUNC_BEGIN();
1669
1670         int error = EMAIL_ERROR_NONE;
1671         int ret = false;
1672
1673         /* Delete all files and directories */
1674         if (!_delete_all_files_and_directories(db_file_path, &error)) {
1675                 EM_DEBUG_EXCEPTION("_delete_all_files_and_directories failed [%d]", error);
1676                 goto FINISH_OFF;
1677         }
1678
1679         /* Delete all accounts on MyAccount */
1680
1681         /* Delete all managed connection to DB */
1682         emstorage_reset_db_handle_list();
1683
1684         ret = true;
1685
1686 FINISH_OFF:
1687         if (err_code)
1688                 *err_code = error;
1689         EM_DEBUG_FUNC_END();
1690         return ret;
1691 }
1692
1693 int _xsystem(const char *argv[])
1694 {
1695         int status = 0;
1696         pid_t pid;
1697         pid = fork();
1698
1699         switch (pid) {
1700         case -1:
1701                 perror("fork failed");
1702                 return -1;
1703         case 0:
1704                 if (execvp(argv[0], (char *const *)argv) == -1) {
1705                         perror("execute init db script");
1706                         _exit(-1);
1707                 }
1708                 _exit(2);
1709         default:
1710                 /* parent */
1711                 break;
1712         }
1713
1714         if (waitpid(pid, &status, 0) == -1) {
1715                 perror("waitpid failed");
1716                 return -1;
1717         }
1718
1719         if (WIFSIGNALED(status)) {
1720                 perror("signal");
1721                 return -1;
1722         }
1723
1724         if (!WIFEXITED(status)) {
1725                 perror("should not happen");
1726                 return -1;
1727         }
1728
1729         return WEXITSTATUS(status);
1730 }
1731
1732 #define SCRIPT_INIT_DB "/usr/bin/email-service_init_db.sh"
1733
1734 INTERNAL_FUNC int emstorage_init_db(char *multi_user_name)
1735 {
1736         EM_DEBUG_FUNC_BEGIN();
1737         int err = EMAIL_ERROR_NONE;
1738         char *prefix_path = NULL;
1739         char *output_file_path = NULL;
1740         char temp_file_path[MAX_PATH] = {0};
1741         gid_t email_gid = -1;
1742         uid_t email_uid = -1;
1743         struct group *gr;
1744         struct group buf_group;
1745         struct passwd *pw;
1746         struct passwd buf_passwd;
1747         char buf_gr[MAX_GRP_BUF_SIZE];
1748         char buf_pw[MAX_GRP_BUF_SIZE];
1749         int result = 0;
1750
1751         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1752                 err = emcore_get_container_path(multi_user_name, &prefix_path);
1753                 if (err != EMAIL_ERROR_NONE) {
1754                         if (err != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1755                                 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", err);
1756                                 goto FINISH_OFF;
1757                         }
1758                 }
1759         } else {
1760                 prefix_path = strdup("");
1761         }
1762
1763         if (err == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1764                 err = emcore_get_canonicalize_path((char *)EMAIL_SERVICE_DB_FILE_PATH, &output_file_path);
1765                 if (err != EMAIL_ERROR_NONE) {
1766                         EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", err);
1767                         goto FINISH_OFF;
1768                 }
1769
1770                 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
1771         } else {
1772                 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
1773         }
1774
1775         EM_DEBUG_LOG("db file path : [%s]", temp_file_path);
1776
1777         if (!g_file_test(temp_file_path, G_FILE_TEST_EXISTS)) {
1778                 int ret = true;
1779                 const char *argv_script[] = {"/bin/sh", SCRIPT_INIT_DB, NULL};
1780                 ret = _xsystem(argv_script);
1781
1782                 if (ret == -1) {
1783                         EM_DEBUG_EXCEPTION("_xsystem failed");
1784                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1785                 }
1786
1787                 result = getgrnam_r(GID, &buf_group, buf_gr, sizeof(buf_gr), &gr);
1788                 if (result == 0 && NULL != gr)
1789                         email_gid = gr->gr_gid;
1790                 else
1791                         email_gid = GID_DEFAULT;
1792
1793
1794                 result = getpwnam_r(UID, &buf_passwd, buf_pw, sizeof(buf_pw), &pw);
1795                 if (result == 0 && NULL != pw)
1796                         email_uid = pw->pw_uid;
1797                 else
1798                         email_uid = UID_DEFAULT;
1799
1800
1801                 chmod(EMAIL_SERVICE_DB_FILE_PATH, 0660);
1802                 chmod(EMAIL_SERVICE_DB_JOURNAL_FILE_PATH, 0660);
1803                 chmod(EMAIL_SQL_PATH, 0660);
1804
1805                 ret = chown(EMAIL_SERVICE_DB_FILE_PATH, email_uid, email_gid);
1806                 if (0 != ret)
1807                         EM_DEBUG_EXCEPTION("chown() Fail[errno=%d]", errno);
1808                 ret = chown(EMAIL_SERVICE_DB_JOURNAL_FILE_PATH, email_uid, email_gid);
1809                 if (0 != ret)
1810                         EM_DEBUG_EXCEPTION("chown() Fail[errno=%d]", errno);
1811                 ret = chown(EMAIL_SQL_PATH, email_uid, email_gid);
1812                 if (0 != ret)
1813                         EM_DEBUG_EXCEPTION("chown() Fail[errno=%d]", errno);
1814
1815         }
1816
1817 FINISH_OFF:
1818
1819         EM_SAFE_FREE(prefix_path);
1820         EM_SAFE_FREE(output_file_path);
1821
1822         EM_DEBUG_FUNC_END();
1823         return err;
1824 }
1825
1826 INTERNAL_FUNC int em_db_open(char *db_file_path, sqlite3 **sqlite_handle, int *err_code)
1827 {
1828         EM_DEBUG_FUNC_BEGIN();
1829         int rc = 0;
1830         int error = EMAIL_ERROR_NONE;
1831         int ret = false;
1832
1833         EM_DEBUG_LOG_DEV("*sqlite_handle[%p]", *sqlite_handle);
1834
1835         if (*sqlite_handle) { /*prevent 33351*/
1836                 EM_DEBUG_LOG_DEV(">>>>> DB Already Opened......");
1837                 if (err_code != NULL)
1838                         *err_code = error;
1839                 return true;
1840         }
1841
1842         EM_DEBUG_LOG("DB file path : [%s]", db_file_path);
1843
1844         /*  db open */
1845         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1846         if (SQLITE_OK != rc) {
1847                 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1848                 if (SQLITE_PERM == rc || SQLITE_CANTOPEN == rc)
1849                         error = EMAIL_ERROR_PERMISSION_DENIED;
1850                 else
1851                         error = EMAIL_ERROR_DB_FAILURE;
1852
1853                 sqlite3_close(*sqlite_handle);
1854                 *sqlite_handle = NULL;
1855
1856                 if (SQLITE_CORRUPT == rc) /* SQLITE_CORRUPT : The database disk image is malformed */ {/* Recovery DB file */
1857                         EM_DEBUG_LOG("The database disk image is malformed. Trying to remove and create database disk image and directories");
1858                         if (!_recovery_from_malformed_db_file(db_file_path, &error)) {
1859                                 EM_DEBUG_EXCEPTION("_recovery_from_malformed_db_file failed [%d]", error);
1860                                 goto FINISH_OFF;
1861                         }
1862
1863                         EM_DEBUG_LOG("Open DB again");
1864                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1865                         if (SQLITE_OK != rc) {
1866                                 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1867                                 if (SQLITE_PERM == rc)
1868                                         error = EMAIL_ERROR_PERMISSION_DENIED;
1869                                 else
1870                                         error = EMAIL_ERROR_DB_FAILURE;
1871
1872                                 sqlite3_close(*sqlite_handle);
1873                                 *sqlite_handle = NULL;
1874                                 goto FINISH_OFF; /*prevent 33351*/
1875                         }
1876                 } else
1877                         goto FINISH_OFF;
1878         }
1879
1880         /* register busy handler */
1881         EM_DEBUG_LOG_DEV(">>>>> Register DB Handle to busy handler: *sqlite_handle[%p]", *sqlite_handle);
1882         rc = sqlite3_busy_handler(*sqlite_handle, _callback_sqlite_busy_handler, NULL);  /*  Busy Handler registration, NULL is a parameter which will be passed to handler */
1883         if (SQLITE_OK != rc) {
1884                 EM_DEBUG_EXCEPTION("sqlite3_busy_handler fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1885                 error = EMAIL_ERROR_DB_FAILURE;
1886                 sqlite3_close(*sqlite_handle);
1887                 *sqlite_handle = NULL;
1888                 goto FINISH_OFF;
1889         }
1890
1891         /* Register collation callback function */
1892         rc = sqlite3_create_collation(*sqlite_handle, "CONVERTUTF8", SQLITE_UTF8, NULL, _callback_collation_utf7_sort);
1893         if (SQLITE_OK != rc) {
1894                 EM_DEBUG_EXCEPTION("sqlite3_create_collation failed : [%d][%s]", rc, sqlite3_errmsg(*sqlite_handle));
1895                 error = EMAIL_ERROR_DB_FAILURE;
1896                 sqlite3_close(*sqlite_handle);
1897                 *sqlite_handle = NULL;
1898                 goto FINISH_OFF;
1899         }
1900
1901         ret = true;
1902
1903 FINISH_OFF:
1904         if (err_code != NULL)
1905                 *err_code = error;
1906
1907         EM_DEBUG_FUNC_END("ret [%d]", ret);
1908         return ret;
1909 }
1910
1911 INTERNAL_FUNC sqlite3* emstorage_db_open(char *multi_user_name, int *err_code)
1912 {
1913         EM_DEBUG_FUNC_BEGIN();
1914
1915         sqlite3 *_db_handle = NULL;
1916
1917         int error = EMAIL_ERROR_NONE;
1918         char *prefix_path = NULL;
1919
1920         _db_handle = emstorage_get_db_handle(multi_user_name);
1921
1922         if (_db_handle == NULL) {
1923                 char *output_file_path = NULL;
1924                 char temp_file_path[MAX_PATH] = {0};
1925
1926                 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1927                         error = emcore_get_container_path(multi_user_name, &prefix_path);
1928                         if (error != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION && error != EMAIL_ERROR_NONE) {
1929                                 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", error);
1930                                 goto FINISH_OFF;
1931                         }
1932                 } else {
1933                         prefix_path = strdup("");
1934                 }
1935
1936                 if (error == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1937                         if ((error = emcore_get_canonicalize_path((char *)EMAIL_SERVICE_DB_FILE_PATH, &output_file_path)) != EMAIL_ERROR_NONE) {
1938                                 EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", error);
1939                                 goto FINISH_OFF;
1940                         }
1941
1942                         SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
1943                         EM_SAFE_FREE(output_file_path);
1944                 } else {
1945                         SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
1946                 }
1947
1948                 if (!em_db_open(temp_file_path, &_db_handle, &error)) {
1949                         EM_DEBUG_EXCEPTION("em_db_open failed[%d]", error);
1950                         goto FINISH_OFF;
1951                 }
1952
1953                 _initialize_shm_mutex(SHM_FILE_FOR_DB_LOCK, &shm_fd_for_db_lock, &mapped_for_db_lock);
1954
1955 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
1956                 _initialize_shm_mutex(SHM_FILE_FOR_MAIL_ID_LOCK, &shm_fd_for_generating_mail_id, &mapped_for_generating_mail_id);
1957 #endif /*__FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
1958
1959                 emstorage_set_db_handle(multi_user_name, _db_handle);
1960
1961                 emstorage_initialize_field_count();
1962         }
1963
1964 FINISH_OFF:
1965
1966         EM_SAFE_FREE(prefix_path);
1967
1968         if (err_code != NULL)
1969                 *err_code = error;
1970
1971         EM_DEBUG_FUNC_END("ret [%p]", _db_handle);
1972         return _db_handle;
1973 }
1974
1975 INTERNAL_FUNC int emstorage_db_close(char *multi_user_name, int *err_code)
1976 {
1977         EM_DEBUG_FUNC_BEGIN();
1978 #ifdef _MULTIPLE_DB_HANDLE
1979         sqlite3 *_db_handle = emstorage_get_db_handle(multi_user_name);
1980 #endif
1981
1982         int error = EMAIL_ERROR_NONE;
1983         int ret = false;
1984
1985         if (_db_handle) {
1986                 ret = sqlite3_close(_db_handle);
1987                 if (ret != SQLITE_OK) {
1988                         EM_DEBUG_EXCEPTION(" sqlite3_close fail - %d", ret);
1989                         error = EMAIL_ERROR_DB_FAILURE;
1990                         ret = false;
1991                         goto FINISH_OFF;
1992                 }
1993 #ifdef _MULTIPLE_DB_HANDLE
1994                 emstorage_remove_db_handle();
1995 #endif
1996                 _db_handle = NULL;
1997         }
1998
1999         ret = true;
2000
2001 FINISH_OFF:
2002         if (err_code != NULL)
2003                 *err_code = error;
2004
2005         EM_DEBUG_FUNC_END("ret [%d]", ret);
2006         return ret;
2007 }
2008
2009 INTERNAL_FUNC int emstorage_open(char *multi_user_name, int *err_code)
2010 {
2011         EM_DEBUG_FUNC_BEGIN();
2012
2013         int ret = false;
2014         int error = EMAIL_ERROR_NONE;
2015         int retValue;
2016         char *prefix_path = NULL;
2017         char buf[MAX_PATH] = {0};
2018
2019         if (EM_SAFE_STRLEN(multi_user_name) <= 0) {
2020                 SNPRINTF(buf, sizeof(buf), "%s", DB_PATH);
2021         } else {
2022                 error = emcore_get_container_path(multi_user_name, &prefix_path);
2023                 if (error != EMAIL_ERROR_NONE) {
2024                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
2025                         goto FINISH_OFF;
2026                 }
2027                 SNPRINTF(buf, sizeof(buf), "%s/%s", prefix_path, DB_PATH);
2028         }
2029
2030         if (!g_file_test(buf, G_FILE_TEST_EXISTS)) {
2031                 retValue = mkdir(buf, DIRECTORY_PERMISSION);
2032
2033                 EM_DEBUG_LOG("mkdir return- %d", retValue);
2034                 EM_DEBUG_LOG("emstorage_open - before sqlite3_open - pid = %d", getpid());
2035         }
2036
2037         if (emstorage_db_open(multi_user_name, &error) == NULL) {
2038                 EM_DEBUG_EXCEPTION("emstorage_db_open failed[%d]", error);
2039                 goto FINISH_OFF;
2040         }
2041
2042         if (_open_counter++ == 0)
2043                 _emstorage_open_once(multi_user_name, &error);
2044
2045         ret = true;
2046
2047 FINISH_OFF:
2048
2049         EM_SAFE_FREE(prefix_path);
2050
2051         if (err_code != NULL)
2052                 *err_code = error;
2053
2054         EM_DEBUG_FUNC_END("ret [%d]", ret);
2055         return ret;
2056 }
2057
2058 static int emstorage_get_field_count_from_create_table_query(char *input_create_table_query, int *output_field_count)
2059 {
2060         EM_DEBUG_FUNC_BEGIN("input_create_table_query[%d], output_field_count[%p]", input_create_table_query, output_field_count);
2061         int err = EMAIL_ERROR_NONE;
2062         int field_count = 0;
2063         char *pos = NULL;
2064
2065         if (input_create_table_query == NULL || output_field_count == NULL) {
2066                 err = EMAIL_ERROR_INVALID_PARAM;
2067                 goto FINISH_OFF;
2068         }
2069
2070         pos = input_create_table_query;
2071
2072         do {
2073                 field_count++;
2074                 if (pos == NULL || *pos == (char)0)
2075                         break;
2076                 pos += 1;
2077                 pos = strchr(pos, ',');
2078         } while (pos);
2079
2080         *output_field_count = field_count;
2081
2082         EM_DEBUG_LOG_DEV("field_count [%d]", field_count);
2083
2084 FINISH_OFF:
2085
2086         EM_DEBUG_FUNC_END("err [%d]", err);
2087         return err;
2088 }
2089
2090 INTERNAL_FUNC int emstorage_initialize_field_count()
2091 {
2092         EM_DEBUG_FUNC_BEGIN();
2093         int err = EMAIL_ERROR_NONE;
2094         int query_len = 0;
2095         char **create_table_query = NULL;
2096
2097         if (_field_count_of_table[CREATE_TABLE_MAIL_ACCOUNT_TBL] != 0) {
2098                 err = EMAIL_ERROR_ALREADY_INITIALIZED;
2099                 goto FINISH_OFF;
2100         }
2101
2102         err = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2103         if (err != EMAIL_ERROR_NONE) {
2104                 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", err);
2105                 goto FINISH_OFF;
2106         }
2107
2108         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_ACCOUNT_TBL]));
2109         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_BOX_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_BOX_TBL]));
2110         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL]));
2111         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_RULE_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_RULE_TBL]));
2112         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_TBL]));
2113         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_ATTACHMENT_TBL]));
2114         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL]));
2115         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_MEETING_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_MEETING_TBL]));
2116 #ifdef __FEATURE_LOCAL_ACTIVITY__
2117         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL]));
2118 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2119         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_CERTIFICATE_TBL]));
2120         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_TASK_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_TASK_TBL]));
2121         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_TEXT_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_TEXT_TBL]));
2122 FINISH_OFF:
2123
2124         if (create_table_query) {
2125                 int i = 0;
2126                 for (i = 0; i < query_len; i++) {
2127                         if (create_table_query[i])
2128                                 EM_SAFE_FREE(create_table_query[i]);
2129                 }
2130                 EM_SAFE_FREE(create_table_query);
2131         }
2132
2133         EM_DEBUG_FUNC_END("err [%d]", err);
2134         return err;
2135 }
2136
2137 INTERNAL_FUNC int emstorage_create_table(char *multi_user_name, emstorage_create_db_type_t type, int *err_code)
2138 {
2139         EM_DEBUG_FUNC_BEGIN();
2140
2141         int error = EMAIL_ERROR_NONE;
2142         int rc = -1, ret = false;
2143         int query_len = 0;
2144         char sql_query_string[QUERY_SIZE] = {0, };
2145         char **create_table_query = NULL;
2146
2147         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2148
2149         error = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2150         if (error != EMAIL_ERROR_NONE) {
2151                 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
2152                 goto FINISH_OFF2;
2153         }
2154
2155         if (query_len < CREATE_TABLE_MAX) {
2156                 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
2157                 error = EMAIL_ERROR_SYSTEM_FAILURE;
2158                 goto FINISH_OFF2;
2159         }
2160
2161         EM_DEBUG_LOG("local_db_handle = %p.", local_db_handle);
2162
2163         char *sql;
2164         char **result = NULL;
2165
2166         /*  1. create mail_account_tbl */
2167         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_account_tbl';";
2168         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2169         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2170                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2171
2172         EM_DEBUG_LOG("emstorage_create_table - result[1] = %s %c", result[1], result[1]);
2173
2174         if (atoi(result[1]) < 1) {
2175                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2176                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2177                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2178
2179                 EM_DEBUG_LOG("CREATE TABLE mail_account_tbl");
2180                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2181                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2182                 if (error != EMAIL_ERROR_NONE) {
2183                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2184                         goto FINISH_OFF;
2185                 }
2186
2187                 /*  create mail_account_tbl unique index */
2188                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ACCOUNT_IDX]);
2189                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2190                 if (error != EMAIL_ERROR_NONE) {
2191                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2192                         goto FINISH_OFF;
2193                 }
2194
2195                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2196         } /*  mail_account_tbl */
2197         else if (type == EMAIL_CREATE_DB_CHECK) {
2198                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], NULL, NULL, NULL), rc);
2199                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], rc, sqlite3_errmsg(local_db_handle)));
2200         }
2201
2202         sqlite3_free_table(result);
2203         result = NULL;
2204
2205         /*  2. create mail_box_tbl */
2206         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_box_tbl';";
2207
2208         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2209         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2210                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2211
2212         if (atoi(result[1]) < 1) {
2213                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2214                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2215                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2216
2217                 EM_DEBUG_LOG("CREATE TABLE mail_box_tbl");
2218
2219                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_BOX_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2220                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2221                 if (error != EMAIL_ERROR_NONE) {
2222                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2223                         goto FINISH_OFF;
2224                 }
2225
2226                 /*  create mail_local_mailbox_tbl unique index */
2227                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_BOX_IDX]);
2228                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2229                 if (error != EMAIL_ERROR_NONE) {
2230                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2231                         goto FINISH_OFF;
2232                 }
2233
2234                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2235         } /*  mail_box_tbl */
2236         else if (type == EMAIL_CREATE_DB_CHECK) {
2237                 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_BOX_TBL], NULL, NULL, NULL);
2238                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2239                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_BOX_TBL], rc, sqlite3_errmsg(local_db_handle)));
2240         }
2241         sqlite3_free_table(result);
2242         result = NULL;
2243
2244         /*  3. create mail_read_mail_uid_tbl */
2245         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_read_mail_uid_tbl';";
2246         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2247         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2248                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2249
2250
2251         if (atoi(result[1]) < 1) {
2252                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2253                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2254                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2255
2256                 EM_DEBUG_LOG("CREATE TABLE mail_read_mail_uid_tbl");
2257
2258                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2259                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2260                 if (error != EMAIL_ERROR_NONE) {
2261                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2262                         goto FINISH_OFF;
2263                 }
2264
2265                 /*  create mail_read_mail_uid_tbl unique index */
2266                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_IDX]);
2267                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2268                 if (error != EMAIL_ERROR_NONE) {
2269                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2270                         goto FINISH_OFF;
2271                 }
2272
2273                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2274         } /*  mail_read_mail_uid_tbl */
2275         else if (type == EMAIL_CREATE_DB_CHECK) {
2276                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], NULL, NULL, NULL), rc);
2277                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2278                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], rc, sqlite3_errmsg(local_db_handle)));
2279         }
2280         sqlite3_free_table(result);
2281         result = NULL;
2282
2283         /*  4. create mail_rule_tbl */
2284         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_rule_tbl';";
2285
2286         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2287         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2288                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2289
2290         if (atoi(result[1]) < 1) {
2291                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2292                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2293                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2294
2295                 EM_DEBUG_LOG("CREATE TABLE mail_rule_tbl");
2296
2297                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_RULE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2298                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2299                 if (error != EMAIL_ERROR_NONE) {
2300                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2301                         goto FINISH_OFF;
2302                 }
2303
2304                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2305         } /*  mail_rule_tbl */
2306         else if (type == EMAIL_CREATE_DB_CHECK) {
2307                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_RULE_TBL], NULL, NULL, NULL), rc);
2308                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2309                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_RULE_TBL], rc, sqlite3_errmsg(local_db_handle)));
2310         }
2311         sqlite3_free_table(result);
2312         result = NULL;
2313
2314         /*  5. create mail_tbl */
2315         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_tbl';";
2316         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2317         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2318                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2319
2320         if (atoi(result[1]) < 1) {
2321                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2322                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2323                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2324                 EM_DEBUG_LOG("CREATE TABLE mail_tbl");
2325
2326                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2327                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2328                 if (error != EMAIL_ERROR_NONE) {
2329                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2330                         goto FINISH_OFF;
2331                 }
2332
2333                 /*  create mail_tbl unique index */
2334                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_IDX]);
2335                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2336                 if (error != EMAIL_ERROR_NONE) {
2337                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2338                         goto FINISH_OFF;
2339                 }
2340
2341                 /*  create mail_tbl index for date_time */
2342                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_DATETIME_IDX]);
2343                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2344                 if (error != EMAIL_ERROR_NONE) {
2345                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2346                         goto FINISH_OFF;
2347                 }
2348
2349                 /*  create mail_tbl index for thread_item_count */
2350                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_THREAD_IDX]);
2351                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2352                 if (error != EMAIL_ERROR_NONE) {
2353                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2354                         goto FINISH_OFF;
2355                 }
2356
2357                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2358                 /*  just one time call */
2359                 /*      EFTSInitFTSIndex(FTS_EMAIL_IDX); */
2360         } /* mail_tbl */
2361         else if (type == EMAIL_CREATE_DB_CHECK) {
2362                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TBL], NULL, NULL, NULL), rc);
2363                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2364                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TBL], rc, sqlite3_errmsg(local_db_handle)));
2365         }
2366         sqlite3_free_table(result);
2367         result = NULL;
2368
2369         /*  6. create mail_attachment_tbl */
2370         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_attachment_tbl';";
2371         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2372         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2373                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2374
2375         if (atoi(result[1]) < 1) {
2376                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2377                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2378                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2379
2380                 EM_DEBUG_LOG("CREATE TABLE mail_attachment_tbl");
2381
2382                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2383                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2384                 if (error != EMAIL_ERROR_NONE) {
2385                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2386                         goto FINISH_OFF;
2387                 }
2388
2389                 /*  create mail_attachment_tbl unique index */
2390                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_IDX]);
2391                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2392                 if (error != EMAIL_ERROR_NONE) {
2393                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2394                         goto FINISH_OFF;
2395                 }
2396
2397                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2398         } /*  mail_attachment_tbl */
2399         else if (type == EMAIL_CREATE_DB_CHECK) {
2400                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], NULL, NULL, NULL), rc);
2401                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2402                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], rc, sqlite3_errmsg(local_db_handle)));
2403         }
2404         sqlite3_free_table(result);
2405         result = NULL;
2406
2407 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
2408
2409         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_partial_body_activity_tbl';";
2410         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2411         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2412                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2413
2414         if (atoi(result[1]) < 1) {
2415
2416                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2417                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2418                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2419
2420                 EM_DEBUG_LOG("CREATE TABLE mail_partial_body_activity_tbl");
2421
2422                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2423                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2424                 if (error != EMAIL_ERROR_NONE) {
2425                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2426                         goto FINISH_OFF;
2427                 }
2428
2429                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2430         } /*  mail_rule_tbl */
2431         else if (type == EMAIL_CREATE_DB_CHECK) {
2432                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2433                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2434                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2435         }
2436         sqlite3_free_table(result);
2437         result = NULL;
2438
2439 #endif /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
2440
2441         /*  create mail_meeting_tbl */
2442         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_meeting_tbl';";
2443         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2444         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2445                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2446
2447         if (atoi(result[1]) < 1) {
2448                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2449                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2450                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2451
2452                 EM_DEBUG_LOG("CREATE TABLE mail_meeting_tbl");
2453
2454                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_MEETING_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2455                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2456                 if (error != EMAIL_ERROR_NONE) {
2457                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2458                         goto FINISH_OFF;
2459                 }
2460
2461                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_MEETING_IDX]);
2462                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2463                 if (error != EMAIL_ERROR_NONE) {
2464                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2465                         goto FINISH_OFF;
2466                 }
2467
2468                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2469         } /*  mail_contact_sync_tbl */
2470         else if (type == EMAIL_CREATE_DB_CHECK) {
2471                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], NULL, NULL, NULL), rc);
2472                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2473                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], rc, sqlite3_errmsg(local_db_handle)));
2474         }
2475         sqlite3_free_table(result);
2476         result = NULL;
2477
2478 #ifdef __FEATURE_LOCAL_ACTIVITY__
2479
2480         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_local_activity_tbl';";
2481         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2482         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2483                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2484
2485         if (atoi(result[1]) < 1) {
2486
2487                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2488                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2489                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2490
2491                 EM_DEBUG_LOG(" CREATE TABLE mail_local_activity_tbl");
2492
2493                 SNPRINTF(sql_query_string, sizeof(sql_query_string), create_table_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL]);
2494                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2495                 if (error != EMAIL_ERROR_NONE) {
2496                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2497                         goto FINISH_OFF;
2498                 }
2499
2500                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2501         } /*  mail_rule_tbl */
2502         else if (type == EMAIL_CREATE_DB_CHECK) {
2503                 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL);
2504                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2505                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2506                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2507         }
2508         sqlite3_free_table(result);
2509         result = NULL;
2510 #endif /*  __FEATURE_LOCAL_ACTIVITY__ */
2511         /*  create mail_certificate_tbl */
2512         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_certificate_tbl';";
2513         /*  rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL);   */
2514         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2515         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2516
2517         if (atoi(result[1]) < 1) {
2518                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2519                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2520
2521                 EM_DEBUG_LOG("CREATE TABLE mail_certificate_tbl");
2522
2523                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2524                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2525                 if (error != EMAIL_ERROR_NONE) {
2526                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2527                         goto FINISH_OFF;
2528                 }
2529
2530                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2531         } /*  mail_contact_sync_tbl */
2532         else if (type == EMAIL_CREATE_DB_CHECK) {
2533                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], NULL, NULL, NULL), rc);
2534                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], rc, sqlite3_errmsg(local_db_handle)));
2535         }
2536
2537         sqlite3_free_table(result);
2538         result = NULL;
2539
2540         /*  create mail_task_tbl */
2541         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_task_tbl';";
2542         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2543         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2544
2545         if (atoi(result[1]) < 1) {
2546                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2547                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2548
2549                 EM_DEBUG_LOG("CREATE TABLE mail_task_tbl");
2550
2551                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TASK_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2552                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2553                 if (error != EMAIL_ERROR_NONE) {
2554                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2555                         goto FINISH_OFF;
2556                 }
2557
2558                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_TASK_IDX]);
2559                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2560                 if (error != EMAIL_ERROR_NONE) {
2561                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2562                         goto FINISH_OFF;
2563                 }
2564
2565                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2566         } /*  mail_task_tbl */
2567         else if (type == EMAIL_CREATE_DB_CHECK) {
2568                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TASK_TBL], NULL, NULL, NULL), rc);
2569                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TASK_TBL], rc, sqlite3_errmsg(local_db_handle)));
2570         }
2571
2572         sqlite3_free_table(result);
2573         result = NULL;
2574
2575 #ifdef __FEATURE_BODY_SEARCH__
2576         /*  create mail_text_tbl */
2577         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_text_tbl';";
2578         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2579         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2580
2581         if (atoi(result[1]) < 1) {
2582                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2583                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2584
2585                 EM_DEBUG_LOG("CREATE TABLE mail_text_tbl");
2586
2587                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TEXT_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2588                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2589                 if (error != EMAIL_ERROR_NONE) {
2590                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2591                         goto FINISH_OFF;
2592                 }
2593
2594                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2595         } /*  mail_text_tbl */
2596         else if (type == EMAIL_CREATE_DB_CHECK) {
2597                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TEXT_TBL], NULL, NULL, NULL), rc);
2598                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TEXT_TBL], rc, sqlite3_errmsg(local_db_handle)));
2599         }
2600
2601         sqlite3_free_table(result);
2602         result = NULL;
2603
2604 #endif
2605
2606 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
2607         /*  create mail_auto_download_activity_tbl */
2608         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_auto_download_activity_tbl';";
2609         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2610         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2611
2612         if (atoi(result[1]) < 1) {
2613                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2614                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2615
2616                 EM_DEBUG_LOG("CREATE TABLE mail_auto_download_activity_tbl");
2617
2618                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], sizeof(sql_query_string)-1);
2619                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2620                 if (error != EMAIL_ERROR_NONE) {
2621                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2622                         goto FINISH_OFF;
2623                 }
2624
2625                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2626         } /*  mail_auto_download_activity_tbl */
2627         else if (type == EMAIL_CREATE_DB_CHECK) {
2628                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2629                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2630         }
2631
2632         sqlite3_free_table(result);
2633         result = NULL;
2634 #endif
2635
2636
2637         ret = true;
2638
2639 FINISH_OFF:
2640
2641         if (result) sqlite3_free_table(result);
2642
2643         if (ret == true) {
2644                 EMSTORAGE_PROTECTED_FUNC_CALL(
2645                         sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2646         } else {
2647                 EMSTORAGE_PROTECTED_FUNC_CALL(
2648                         sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
2649         }
2650
2651 FINISH_OFF2:
2652         if (create_table_query) {
2653                 int i = 0;
2654                 for (i = 0; i < query_len; i++) {
2655                         if (create_table_query[i])
2656                                 EM_SAFE_FREE(create_table_query[i]);
2657                 }
2658                 EM_SAFE_FREE(create_table_query);
2659         }
2660
2661         if (err_code != NULL)
2662                 *err_code = error;
2663
2664         EM_DEBUG_FUNC_END("ret [%d]", ret);
2665         return ret;
2666 }
2667
2668 /* Query series --------------------------------------------------------------*/
2669 INTERNAL_FUNC int emstorage_query_mail_count(char *multi_user_name, const char *input_conditional_clause, int input_transaction, int *output_total_mail_count, int *output_unseen_mail_count)
2670 {
2671         EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_transaction[%d], output_total_mail_count[%p], output_unseen_mail_count[%p]", input_conditional_clause, input_transaction, output_total_mail_count, output_unseen_mail_count);
2672         int rc = -1;
2673         int query_size = 0;
2674         int error = EMAIL_ERROR_NONE;
2675         DB_STMT hStmt = NULL;
2676         char *sql_query_string = NULL;
2677         char **result;
2678         sqlite3 *local_db_handle = NULL;
2679
2680         if (!input_conditional_clause || (!output_total_mail_count && !output_unseen_mail_count)) {
2681                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2682                 return EMAIL_ERROR_INVALID_PARAM;
2683         }
2684
2685         query_size = EM_SAFE_STRLEN(input_conditional_clause) + QUERY_SIZE;
2686         sql_query_string = em_malloc(query_size);
2687         if (sql_query_string == NULL) {
2688                 EM_DEBUG_EXCEPTION("em_mallocfailed");
2689                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2690                 goto FINISH_OFF;
2691         }
2692
2693         local_db_handle = emstorage_get_db_connection(multi_user_name);
2694
2695         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2696
2697         SNPRINTF(sql_query_string, query_size, "SELECT COUNT(*) FROM mail_tbl");
2698         EM_SAFE_STRNCAT(sql_query_string, (char*)input_conditional_clause, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
2699
2700         if (output_total_mail_count) {
2701                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
2702                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2703                                 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
2704
2705                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2706                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2707                                 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
2708                 _get_stmt_field_data_int(hStmt, output_total_mail_count, 0);
2709         }
2710
2711         if (output_unseen_mail_count) {
2712                 EM_SAFE_STRNCAT(sql_query_string, " AND flags_seen_field = 0 ", query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
2713
2714                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
2715                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2716                                 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2717
2718                 *output_unseen_mail_count = atoi(result[1]);
2719                 sqlite3_free_table(result);
2720         }
2721
2722 FINISH_OFF:
2723
2724         if (hStmt != NULL) {
2725                 rc = sqlite3_finalize(hStmt);
2726                 if (rc != SQLITE_OK) {
2727                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
2728                         error = EMAIL_ERROR_DB_FAILURE;
2729                 }
2730         }
2731
2732         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2733
2734         EM_SAFE_FREE(sql_query_string);
2735
2736         EM_DEBUG_FUNC_END("error [%d]", error);
2737         return error;
2738 }
2739
2740 INTERNAL_FUNC int emstorage_query_mail_id_list(char *multi_user_name, const char *input_conditional_clause, int input_transaction, int **output_mail_id_list, int *output_mail_id_count)
2741 {
2742         EM_DEBUG_FUNC_BEGIN("input_conditional_clause [%p], input_transaction [%d], output_mail_id_list [%p], output_mail_id_count [%p]", input_conditional_clause, input_transaction, output_mail_id_list, output_mail_id_count);
2743
2744         int      i = 0;
2745         int      count = 0;
2746         int      rc = -1;
2747         int      cur_query = 0;
2748         int      col_index;
2749         int      query_size = 0;
2750         int      error = EMAIL_ERROR_NONE;
2751         int     *result_mail_id_list = NULL;
2752         char   **result = NULL;
2753         char     *sql_query_string = NULL;
2754         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2755
2756         EM_IF_NULL_RETURN_VALUE(input_conditional_clause, EMAIL_ERROR_INVALID_PARAM);
2757         EM_IF_NULL_RETURN_VALUE(output_mail_id_list, EMAIL_ERROR_INVALID_PARAM);
2758         EM_IF_NULL_RETURN_VALUE(output_mail_id_count, EMAIL_ERROR_INVALID_PARAM);
2759
2760         query_size = strlen(input_conditional_clause) + strlen("SELECT mail_id FROM mail_tbl ") + 10;  // 10 is extra space
2761         sql_query_string = em_malloc(query_size);
2762         if (sql_query_string == NULL) {
2763                 EM_DEBUG_EXCEPTION("em_mallocfailed");
2764                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2765                 goto FINISH_OFF;
2766         }
2767
2768         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2769
2770         /* Composing query */
2771         SNPRINTF_OFFSET(sql_query_string, cur_query, query_size, "SELECT mail_id FROM mail_tbl ");
2772         EM_SAFE_STRNCAT(sql_query_string, (char*)input_conditional_clause, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
2773
2774         EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
2775
2776         /* Performing query */
2777         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2778         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2779                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2780
2781         col_index = 1;
2782
2783         /*  to get mail list */
2784         if (count == 0) {
2785                 EM_DEBUG_LOG("No mail found...");
2786                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2787                 goto FINISH_OFF;
2788         }
2789
2790         EM_DEBUG_LOG("There are [%d] mails.", count);
2791
2792         if (!(result_mail_id_list = (int*)em_malloc(sizeof(int) * count))) {
2793                 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
2794                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2795                 goto FINISH_OFF;
2796         }
2797
2798         EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2799
2800         for (i = 0; i < count; i++)
2801                 _get_table_field_data_int(result, result_mail_id_list + i, col_index++);
2802
2803         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >> ", count);
2804
2805         *output_mail_id_list  = result_mail_id_list;
2806         *output_mail_id_count = count;
2807
2808 FINISH_OFF:
2809
2810         if (result)
2811                 sqlite3_free_table(result);
2812
2813         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2814
2815         EM_SAFE_FREE(sql_query_string);
2816
2817         if (error != EMAIL_ERROR_NONE)
2818                 EM_SAFE_FREE(result_mail_id_list);
2819
2820         EM_DEBUG_FUNC_END("error [%d]", error);
2821         return error;
2822 }
2823
2824 INTERNAL_FUNC int emstorage_query_mail_list(char *multi_user_name, const char *conditional_clause, int transaction, email_mail_list_item_t** result_mail_list,  int *result_count,  int *err_code)
2825 {
2826         EM_DEBUG_FUNC_BEGIN();
2827         EM_PROFILE_BEGIN(emstorage_query_mail_list_func);
2828
2829         int i = 0, count = 0, rc = -1, to_get_count = (result_mail_list) ? 0 : 1;
2830         int sql_query_string_length = 0;
2831         int local_inline_content_count = 0, local_attachment_count = 0;
2832         int cur_query = 0, base_count = 0, col_index;
2833         int ret = false, error = EMAIL_ERROR_NONE;
2834         char *date_time_string = NULL;
2835         char **result = NULL;
2836         char *field_mail_id = "mail_id";
2837         char *field_all     = "mail_id, account_id, mailbox_id, mailbox_type, full_address_from, email_address_sender, full_address_to, subject, body_download_status, mail_size, flags_seen_field, flags_deleted_field, flags_flagged_field, flags_answered_field, flags_recent_field, flags_draft_field, flags_forwarded_field, DRM_status, priority, save_status, lock_status, attachment_count, inline_content_count, date_time, preview_text, thread_id, thread_item_count, meeting_request_status, message_class, smime_type, scheduled_sending_time, remaining_resend_times, tag_id, eas_data_length, eas_data ";
2838         char *select_query_form = "SELECT %s FROM mail_tbl ";
2839         char *target_field = NULL;
2840         char *sql_query_string = NULL;
2841         email_mail_list_item_t *mail_list_item_from_tbl = NULL;
2842         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2843
2844         EM_IF_NULL_RETURN_VALUE(conditional_clause, false);
2845         EM_IF_NULL_RETURN_VALUE(result_count, false);
2846
2847         EMSTORAGE_START_READ_TRANSACTION(transaction);
2848
2849         /*  select clause */
2850         if (to_get_count) /*  count only */
2851                 target_field = field_mail_id;
2852         else /* mail list in plain form */
2853                 target_field = field_all;
2854
2855         sql_query_string_length = EM_SAFE_STRLEN(select_query_form) + EM_SAFE_STRLEN(target_field) + EM_SAFE_STRLEN(conditional_clause);
2856
2857         if (sql_query_string_length)
2858                 sql_query_string = em_malloc(sql_query_string_length);
2859
2860         if (sql_query_string == NULL) {
2861                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
2862                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2863                 goto FINISH_OFF;
2864         }
2865
2866         cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, sql_query_string_length, select_query_form, target_field);
2867
2868         strncat(sql_query_string, conditional_clause, sql_query_string_length - cur_query);
2869
2870         EM_DEBUG_LOG_DEV("query[%s]", sql_query_string);
2871
2872         /*  performing query */
2873         EM_PROFILE_BEGIN(emstorage_query_mail_list_performing_query);
2874         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2875         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2876                         ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2877         EM_PROFILE_END(emstorage_query_mail_list_performing_query);
2878
2879         if (!base_count)
2880                 base_count = ({
2881                                 int i = 0;
2882                                 char *tmp = NULL;
2883                                 for (tmp = field_all; tmp && *(tmp + 1); tmp = index(tmp + 1, ',')) i++ ;
2884                                 i;
2885                                 });
2886
2887         col_index = base_count;
2888
2889         EM_DEBUG_LOG_DEV("base_count [%d]", base_count);
2890
2891         if (to_get_count) {
2892                 /*  to get count */
2893                 if (!count) {
2894                         EM_DEBUG_LOG_DEV("No mail found...");
2895                         ret = false;
2896                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
2897                         goto FINISH_OFF;
2898                 }
2899                 EM_DEBUG_LOG_DEV("There are [%d] mails.", count);
2900         } else {
2901                 /*  to get mail list */
2902                 if (!count) {
2903                         EM_DEBUG_LOG_DEV("No mail found...");
2904                         ret = false;
2905                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
2906                         goto FINISH_OFF;
2907                 }
2908
2909                 EM_DEBUG_LOG_DEV("There are [%d] mails.", count);
2910                 if (!(mail_list_item_from_tbl = (email_mail_list_item_t*)em_malloc(sizeof(email_mail_list_item_t) * count))) {
2911                         EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
2912                         error = EMAIL_ERROR_OUT_OF_MEMORY;
2913                         goto FINISH_OFF;
2914                 }
2915
2916                 EM_PROFILE_BEGIN(emstorage_query_mail_list_loop);
2917                 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >> ");
2918                 for (i = 0; i < count; i++) {
2919                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_id), col_index++);
2920                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].account_id), col_index++);
2921                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mailbox_id), col_index++);
2922                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].mailbox_type), col_index++);
2923                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].full_address_from, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2924                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_sender, MAX_EMAIL_ADDRESS_LENGTH, 1, col_index++);
2925                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_recipient, STRING_LENGTH_FOR_DISPLAY,  1, col_index++);
2926                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].subject, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2927                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].body_download_status), col_index++);
2928                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_size), col_index++);
2929                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_seen_field), col_index++);
2930                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_deleted_field), col_index++);
2931                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_flagged_field), col_index++);
2932                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_answered_field), col_index++);
2933                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_recent_field), col_index++);
2934                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_draft_field), col_index++);
2935                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_forwarded_field), col_index++);
2936                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].DRM_status), col_index++);
2937                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].priority), col_index++);
2938                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].save_status), col_index++);
2939                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].lock_status), col_index++);
2940                         _get_table_field_data_int(result, &local_attachment_count, col_index++);
2941                         _get_table_field_data_int(result, &local_inline_content_count, col_index++);
2942                         _get_table_field_data_time_t(result, &(mail_list_item_from_tbl[i].date_time), col_index++);
2943                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].preview_text, MAX_PREVIEW_TEXT_LENGTH, 1, col_index++);
2944                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_id), col_index++);
2945                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_item_count), col_index++);
2946                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].meeting_request_status), col_index++);
2947                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].message_class), col_index++);
2948                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].smime_type), col_index++);
2949                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].scheduled_sending_time), col_index++);
2950                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].remaining_resend_times), col_index++);
2951                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].tag_id), col_index++);
2952                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].eas_data_length), col_index++);
2953                         _get_table_field_data_blob(result, (void**)&(mail_list_item_from_tbl[i].eas_data), mail_list_item_from_tbl[i].eas_data_length, col_index++);
2954
2955                         mail_list_item_from_tbl[i].attachment_count = (local_attachment_count > 0) ? 1 : 0;
2956                 }
2957                 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >> ", count);
2958                 EM_PROFILE_END(emstorage_query_mail_list_loop);
2959         }
2960
2961         ret = true;
2962
2963 FINISH_OFF:
2964         EM_DEBUG_LOG("MAIL_COUNT [%d]", count);
2965
2966         if (result)
2967                 sqlite3_free_table(result);
2968
2969         if (to_get_count)
2970                 *result_count = count;
2971         else {
2972                 if (ret == true) {
2973                         if (result_mail_list)
2974                                 *result_mail_list = mail_list_item_from_tbl;
2975                         *result_count = count;
2976                 } else
2977                         EM_SAFE_FREE(mail_list_item_from_tbl);
2978         }
2979
2980         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2981
2982         //      sqlite3_db_release_memory(local_db_handle);
2983
2984         EM_SAFE_FREE(sql_query_string);
2985         EM_SAFE_FREE(date_time_string);
2986
2987         if (err_code != NULL)
2988                 *err_code = error;
2989
2990         EM_PROFILE_END(emstorage_query_mail_list_func);
2991         EM_DEBUG_FUNC_END("ret [%d]", ret);
2992         return ret;
2993 }
2994
2995
2996 INTERNAL_FUNC int emstorage_query_mail_tbl(char *multi_user_name, const char *conditional_clause, int transaction, emstorage_mail_tbl_t** result_mail_tbl, int *result_count, int *err_code)
2997 {
2998         EM_DEBUG_FUNC_BEGIN("conditional_clause[%s], result_mail_tbl[%p], result_count [%p], transaction[%d], err_code[%p]", conditional_clause, result_mail_tbl, result_count, transaction, err_code);
2999
3000         if (!conditional_clause || !result_mail_tbl || !result_count) {
3001                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: [%p] [%p] [%p]", conditional_clause, result_mail_tbl, result_mail_tbl);
3002                 if (err_code != NULL)
3003                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3004                 return false;
3005         }
3006
3007         int i, col_index = FIELD_COUNT_OF_MAIL_TBL, rc, ret = false, count;
3008         int error = EMAIL_ERROR_NONE;
3009         char **result = NULL, sql_query_string[QUERY_SIZE] = {0, };
3010         emstorage_mail_tbl_t* p_data_tbl = NULL;
3011         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3012
3013         EMSTORAGE_START_READ_TRANSACTION(transaction);
3014
3015         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl %s", conditional_clause);
3016
3017         EM_DEBUG_LOG_DEV("Query[%s]", sql_query_string);
3018
3019         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3020         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3021                         ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
3022
3023         if (!count) {
3024                 EM_DEBUG_LOG("No mail found...");
3025                 ret = false;
3026                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
3027                 goto FINISH_OFF;
3028         }
3029
3030         EM_DEBUG_LOG("There are [%d] mails.", count);
3031         if (!(p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * count))) {
3032                 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
3033                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3034                 goto FINISH_OFF;
3035         }
3036
3037         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >> ");
3038         for (i = 0; i < count; i++) {
3039                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
3040                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3041                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3042                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_type), col_index++);
3043                 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 1, col_index++);
3044                 _get_table_field_data_time_t (result, &(p_data_tbl[i].date_time), col_index++);
3045                 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
3046                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
3047                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
3048                 _get_table_field_data_string(result, &(p_data_tbl[i].message_id), 0, col_index++);
3049                 _get_table_field_data_int(result, &(p_data_tbl[i].reference_mail_id), col_index++);
3050                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_from), 1, col_index++);
3051                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_reply), 1, col_index++);
3052                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_to), 1, col_index++);
3053                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_cc), 1, col_index++);
3054                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_bcc), 1, col_index++);
3055                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_return), 1, col_index++);
3056                 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_sender), 1, col_index++);
3057                 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_recipient), 1, col_index++);
3058                 _get_table_field_data_string(result, &(p_data_tbl[i].alias_sender), 1, col_index++);
3059                 _get_table_field_data_string(result, &(p_data_tbl[i].alias_recipient), 1, col_index++);
3060                 _get_table_field_data_int(result, &(p_data_tbl[i].body_download_status), col_index++);
3061                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
3062                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
3063                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
3064                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_size), col_index++);
3065                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_seen_field), col_index++);
3066                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_deleted_field), col_index++);
3067                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_flagged_field), col_index++);
3068                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_answered_field), col_index++);
3069                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_recent_field), col_index++);
3070                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_draft_field), col_index++);
3071                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_forwarded_field), col_index++);
3072                 _get_table_field_data_int(result, &(p_data_tbl[i].DRM_status), col_index++);
3073                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].priority), col_index++);
3074                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
3075                 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
3076                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].report_status), col_index++);
3077                 _get_table_field_data_int(result, &(p_data_tbl[i].attachment_count), col_index++);
3078                 _get_table_field_data_int(result, &(p_data_tbl[i].inline_content_count), col_index++);
3079                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
3080                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
3081                 _get_table_field_data_string(result, &(p_data_tbl[i].preview_text), 1, col_index++);
3082                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].meeting_request_status), col_index++);
3083                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].message_class), col_index++);
3084                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].digest_type), col_index++);
3085                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].smime_type), col_index++);
3086                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].scheduled_sending_time), col_index++);
3087                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].remaining_resend_times), col_index++);
3088                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].tag_id), col_index++);
3089                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].replied_time), col_index++);
3090                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].forwarded_time), col_index++);
3091                 _get_table_field_data_string(result, &(p_data_tbl[i].default_charset), 0, col_index++);
3092                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3093                 _get_table_field_data_blob(result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3094                 _get_table_field_data_string(result, &(p_data_tbl[i].user_name), 0, col_index++);
3095         }
3096
3097         ret = true;
3098
3099 FINISH_OFF:
3100         if (result)
3101                 sqlite3_free_table(result);
3102
3103         if (ret) {
3104                 *result_mail_tbl = p_data_tbl;
3105                 *result_count = count;
3106         } else {
3107                 *result_mail_tbl = NULL;
3108                 *result_count = 0;
3109         }
3110
3111         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3112
3113         //      sqlite3_db_release_memory(local_db_handle);
3114
3115         if (err_code != NULL)
3116                 *err_code = error;
3117
3118         EM_DEBUG_FUNC_END("ret [%d]", ret);
3119         return ret;
3120 }
3121
3122 #ifdef __FEATURE_BODY_SEARCH__
3123 INTERNAL_FUNC int emstorage_query_mail_text_tbl(char *multi_user_name, const char *conditional_clause, int transaction, emstorage_mail_text_tbl_t** result_mail_text_tbl, int *result_count, int *err_code)
3124 {
3125         EM_DEBUG_FUNC_BEGIN("conditional_clause[%s], result_mail_text_tbl[%p], result_count [%p], transaction[%d], err_code[%p]", conditional_clause, result_mail_text_tbl, result_count, transaction, err_code);
3126
3127         if (!conditional_clause || !result_mail_text_tbl || !result_count) { /*prevent 50930*/
3128                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3129                 if (err_code != NULL)
3130                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3131                 return false;
3132         }
3133
3134         int i = 0;
3135         int rc = 0;
3136         int ret = false;
3137         int count = 0;
3138         int col_index = FIELD_COUNT_OF_MAIL_TEXT_TBL;
3139         int error = EMAIL_ERROR_NONE;
3140         char **result = NULL;
3141         char sql_query_string[QUERY_SIZE] = {0, };
3142         emstorage_mail_text_tbl_t* p_data_tbl = NULL;
3143         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3144
3145         EMSTORAGE_START_READ_TRANSACTION(transaction);
3146
3147         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_text_tbl %s", conditional_clause);
3148
3149         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
3150
3151         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3152         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3153                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3154
3155         if (!count) {
3156                 EM_DEBUG_LOG("No mail found...");
3157                 ret = false;
3158                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
3159                 goto FINISH_OFF;
3160         }
3161
3162         EM_DEBUG_LOG("There are [%d] mails.", count);
3163         if (!(p_data_tbl = (emstorage_mail_text_tbl_t *)em_malloc(sizeof(emstorage_mail_text_tbl_t) * count))) {
3164                 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_text_tbl_t failed...");
3165                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3166                 goto FINISH_OFF;
3167         }
3168
3169         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
3170         for (i = 0; i < count; i++) {
3171                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
3172                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3173                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3174                 _get_table_field_data_string(result, &(p_data_tbl[i].body_text), 0, col_index++);
3175         }
3176
3177         ret = true;
3178
3179 FINISH_OFF:
3180         if (result)
3181                 sqlite3_free_table(result);
3182
3183         if (ret == true) {
3184                 *result_mail_text_tbl = p_data_tbl;
3185                 *result_count = count;
3186         } else
3187                 EM_SAFE_FREE(p_data_tbl);
3188
3189         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3190
3191         //      sqlite3_db_release_memory(local_db_handle);
3192
3193         if (err_code != NULL)
3194                 *err_code = error;
3195
3196         EM_DEBUG_FUNC_END("ret [%d]", ret);
3197         return ret;
3198 }
3199 #endif
3200
3201 INTERNAL_FUNC int emstorage_query_mailbox_tbl(char *multi_user_name, const char *input_conditional_clause, const char *input_ordering_clause, int input_get_mail_count,  int input_transaction, emstorage_mailbox_tbl_t **output_mailbox_list, int *output_mailbox_count)
3202 {
3203         EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_ordering_clause [%p], input_get_mail_count[%d], input_transaction[%d], output_mailbox_list[%p], output_mailbox_count[%d]", input_conditional_clause, input_ordering_clause, input_get_mail_count, input_transaction, output_mailbox_list, output_mailbox_count);
3204
3205         int i = 0;
3206         int rc;
3207         int count = 0;
3208         int col_index = 0;
3209         int error = EMAIL_ERROR_NONE;
3210         int read_count = 0;
3211         int total_count = 0;
3212         char **result;
3213         char sql_query_string[QUERY_SIZE] = {0, };
3214         char *fields = "MBT.mailbox_id, MBT.account_id, local_yn, MBT.mailbox_name, MBT.mailbox_type, alias, deleted_flag, modifiable_yn, total_mail_count_on_server, has_archived_mails, mail_slot_size, no_select, last_sync_time, MBT.eas_data_length, MBT.eas_data ";
3215         emstorage_mailbox_tbl_t* p_data_tbl = NULL;
3216         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3217
3218         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
3219
3220         if (input_get_mail_count == 0) {        /* without mail count */
3221                 col_index = 15;
3222                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT %s FROM mail_box_tbl AS MBT %s %s", fields, input_conditional_clause, input_ordering_clause);
3223         } else {        /* with read count and total count */
3224                 col_index = 17;
3225                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT %s, total, read  FROM mail_box_tbl AS MBT LEFT OUTER JOIN (SELECT mailbox_id, count(mail_id) AS total, SUM(flags_seen_field) AS read FROM mail_tbl WHERE flags_deleted_field = 0 GROUP BY mailbox_id) AS MT ON MBT.mailbox_id = MT.mailbox_id %s %s", fields, input_conditional_clause, input_ordering_clause);
3226         }
3227
3228         EM_DEBUG_LOG_DEV("query[%s]", sql_query_string);
3229
3230         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
3231         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3232                         ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string))
3233
3234                 EM_DEBUG_LOG_DEV("result count [%d]", count);
3235
3236         if (count == 0) {
3237                 EM_DEBUG_LOG_SEC("Can't find mailbox query[%s]", sql_query_string);
3238                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
3239                 goto FINISH_OFF;
3240         }
3241
3242         if ((p_data_tbl = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t) * count)) == NULL) {
3243                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
3244                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3245                 goto FINISH_OFF;
3246         }
3247
3248         for (i = 0; i < count; i++) {
3249                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3250                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3251                 _get_table_field_data_int(result, &(p_data_tbl[i].local_yn), col_index++);
3252                 _get_table_field_data_string(result, &(p_data_tbl[i].mailbox_name), 0, col_index++);
3253                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].mailbox_type), col_index++);
3254                 _get_table_field_data_string(result, &(p_data_tbl[i].alias), 0, col_index++);
3255                 _get_table_field_data_int(result, &(p_data_tbl[i].deleted_flag), col_index++);
3256                 _get_table_field_data_int(result, &(p_data_tbl[i].modifiable_yn), col_index++);
3257                 _get_table_field_data_int(result, &(p_data_tbl[i].total_mail_count_on_server), col_index++);
3258                 _get_table_field_data_int(result, &(p_data_tbl[i].has_archived_mails), col_index++);
3259                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_slot_size), col_index++);
3260                 _get_table_field_data_int(result, &(p_data_tbl[i].no_select), col_index++);
3261                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].last_sync_time), col_index++);
3262                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3263                 _get_table_field_data_blob(result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3264
3265                 if (input_get_mail_count == 1) {
3266                         _get_table_field_data_int(result, &(total_count), col_index++);
3267                         p_data_tbl[i].total_mail_count_on_local = total_count;
3268                         _get_table_field_data_int(result, &(read_count), col_index++);
3269                         p_data_tbl[i].unread_count = total_count - read_count;                  /*  return unread count, NOT  */
3270                 }
3271         }
3272
3273
3274 FINISH_OFF:
3275         if (result)
3276                 sqlite3_free_table(result);
3277
3278         if (error == EMAIL_ERROR_NONE) {
3279                 *output_mailbox_list = p_data_tbl;
3280                 *output_mailbox_count = count;
3281                 EM_DEBUG_LOG("Mailbox Count [%d]", count);
3282         }
3283
3284         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
3285
3286         //      sqlite3_db_release_memory(local_db_handle);
3287
3288         EM_DEBUG_FUNC_END("error [%d]", error);
3289         return error;
3290 }
3291
3292 /* Query series --------------------------------------------------------------*/
3293 INTERNAL_FUNC int emstorage_check_duplicated_account(char *multi_user_name, email_account_t* account, int transaction, int *err_code)
3294 {
3295         EM_DEBUG_FUNC_BEGIN();
3296         int rc = -1, ret = false;
3297         int error = EMAIL_ERROR_NONE;
3298         char **result;
3299         int count;
3300         char sql_query_string[QUERY_SIZE] = {0, };
3301         sqlite3 *local_db_handle = NULL;
3302
3303         if (account == NULL || account->user_email_address == NULL || account->incoming_server_user_name == NULL || account->incoming_server_address == NULL ||
3304                         account->outgoing_server_user_name == NULL || account->outgoing_server_address == NULL) {
3305                 if (err_code != NULL)
3306                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3307                 return false;
3308         }
3309
3310         local_db_handle = emstorage_get_db_connection(multi_user_name);
3311
3312         EMSTORAGE_START_READ_TRANSACTION(transaction);
3313
3314         SNPRINTF(sql_query_string, sizeof(sql_query_string),
3315                         "SELECT COUNT(*) FROM mail_account_tbl "
3316                         " WHERE "
3317                         " user_email_address = '%s' AND "
3318                         " incoming_server_user_name = '%s' AND "
3319                         " incoming_server_type = %d AND "
3320                         " incoming_server_address = '%s' AND "
3321                         " outgoing_server_user_name = '%s' AND "
3322                         " outgoing_server_type = %d AND "
3323                         " outgoing_server_address = '%s'; ",
3324                         account->user_email_address,
3325                         account->incoming_server_user_name, account->incoming_server_type, account->incoming_server_address,
3326                         account->outgoing_server_user_name, account->outgoing_server_type, account->outgoing_server_address
3327                         );
3328         EM_DEBUG_LOG_SEC("Query[%s]", sql_query_string);
3329         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
3330         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3331                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3332
3333         count = atoi(result[1]);
3334         sqlite3_free_table(result);
3335
3336         EM_DEBUG_LOG("Count of Duplicated Account Information: [%d]", count);
3337
3338         if (count == 0) {       /*  not duplicated account */
3339                 ret = true;
3340                 EM_DEBUG_LOG_SEC("NOT duplicated account: user_email_address[%s]", account->user_email_address);
3341         } else {        /*  duplicated account */
3342                 ret = false;
3343                 EM_DEBUG_LOG_SEC("The same account already exists. Duplicated account: user_email_address[%s]", account->user_email_address);
3344                 error = EMAIL_ERROR_ALREADY_EXISTS;
3345         }
3346
3347 FINISH_OFF:
3348
3349         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3350
3351         if (err_code != NULL)
3352                 *err_code = error;
3353
3354         EM_DEBUG_FUNC_END("ret [%d]", ret);
3355         return ret;
3356
3357 }
3358
3359 INTERNAL_FUNC int emstorage_get_account_count(char *multi_user_name, int *count, int transaction, int *err_code)
3360 {
3361         EM_DEBUG_FUNC_BEGIN();
3362
3363         if (!count) {
3364                 if (err_code != NULL)
3365                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3366                 return false;
3367         }
3368
3369         int rc = -1, ret = false;
3370         int error = EMAIL_ERROR_NONE;
3371
3372         DB_STMT hStmt = NULL;
3373         char sql_query_string[QUERY_SIZE] = {0, };
3374
3375
3376         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3377         EMSTORAGE_START_READ_TRANSACTION(transaction);
3378         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_account_tbl");
3379         EM_DEBUG_LOG_SEC("SQL STMT [%s]", sql_query_string);
3380         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string,
3381                                 EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3382         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
3383         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3384                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3385
3386         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3387         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3388                         ("sqlite3_step fail:%d", rc));
3389
3390         *count = sqlite3_column_int(hStmt, 0);
3391
3392         ret = true;
3393
3394 FINISH_OFF:
3395
3396         if (hStmt != NULL) {
3397                 rc = sqlite3_finalize(hStmt);
3398                 hStmt = NULL;
3399                 if (rc != SQLITE_OK) {
3400                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
3401                         error = EMAIL_ERROR_DB_FAILURE;
3402                 }
3403         }
3404
3405         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3406
3407         if (err_code != NULL)
3408                 *err_code = error;
3409
3410         EM_DEBUG_FUNC_END("ret [%d]", ret);
3411         return ret;
3412 }
3413
3414 INTERNAL_FUNC int emstorage_get_account_list(char *multi_user_name, int *select_num, emstorage_account_tbl_t **account_list, int transaction, int with_password, int *err_code)
3415 {
3416         EM_DEBUG_FUNC_BEGIN();
3417
3418         int i = 0, count = 0, rc = -1, ret = false;
3419         int field_index = 0;
3420         int sql_len = 0;
3421         int error = EMAIL_ERROR_NONE;
3422         emstorage_account_tbl_t *p_data_tbl = NULL;
3423         char sql_query_string[QUERY_SIZE] = {0, };
3424         char *sql = "SELECT count(*) FROM mail_account_tbl;";
3425         char **result;
3426         sqlite3 *local_db_handle = NULL;
3427         DB_STMT hStmt = NULL;
3428
3429         if (!select_num || !account_list) {
3430                 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, account_list);
3431                 if (err_code != NULL)
3432                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3433                 return false;
3434         }
3435
3436         local_db_handle = emstorage_get_db_connection(multi_user_name);
3437
3438         EMSTORAGE_START_READ_TRANSACTION(transaction);
3439
3440         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
3441         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3442                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
3443
3444         count = atoi(result[1]);
3445         sqlite3_free_table(result);
3446
3447         EM_DEBUG_LOG_DEV("count = %d", rc);
3448
3449         if (count <= 0) {
3450                 EM_DEBUG_LOG("no account found...");
3451                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3452                 ret = true;
3453                 goto FINISH_OFF;
3454         }
3455         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
3456         sql_len = EM_SAFE_STRLEN(sql_query_string);
3457
3458         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
3459                         "account_id, "
3460                         "account_name, "
3461                         "logo_icon_path, "
3462                         "user_data, "
3463                         "user_data_length, "
3464                         "account_svc_id, "
3465                         "sync_status, "
3466                         "sync_disabled, "
3467                         "default_mail_slot_size, "
3468                         "roaming_option, "
3469                         "color_label, "
3470                         "user_display_name, "
3471                         "user_email_address, "
3472                         "reply_to_address, "
3473                         "return_address, "
3474                         "incoming_server_type, "
3475                         "incoming_server_address, "
3476                         "incoming_server_port_number, "
3477                         "incoming_server_user_name, "
3478                         "incoming_server_password, "
3479                         "incoming_server_secure_connection, "
3480                         "incoming_server_authentication_method, "
3481                         "retrieval_mode, "
3482                         "keep_mails_on_pop_server_after_download, "
3483                         "check_interval, "
3484                         "auto_download_size, "
3485                         "peak_interval, "
3486                         "peak_days, "
3487                         "peak_start_time, "
3488                         "peak_end_time, "
3489                         "outgoing_server_type, "
3490                         "outgoing_server_address, "
3491                         "outgoing_server_port_number, "
3492                         "outgoing_server_user_name, "
3493                         "outgoing_server_password, "
3494                         "outgoing_server_secure_connection, "
3495                         "outgoing_server_need_authentication, "
3496                         "outgoing_server_use_same_authenticator, "
3497                         "priority, "
3498                         "keep_local_copy, "
3499                         "req_delivery_receipt, "
3500                         "req_read_receipt, "
3501                         "download_limit, "
3502                         "block_address, "
3503                         "block_subject, "
3504                         "display_name_from, "
3505                         "reply_with_body, "
3506                         "forward_with_files, "
3507                         "add_myname_card, "
3508                         "add_signature, "
3509                         "signature, "
3510                         "add_my_address_to_bcc, "
3511                         "notification_status, "
3512                         "vibrate_status, "
3513                         "display_content_status, "
3514                         "default_ringtone_status, "
3515                         "alert_ringtone_path, "
3516                         "auto_resend_times, "
3517                         "outgoing_server_size_limit, "
3518                         "wifi_auto_download, "
3519                         "pop_before_smtp, "
3520                         "incoming_server_requires_apop, "
3521                         "smime_type, "
3522                         "certificate_path, "
3523                         "cipher_type, "
3524                         "digest_type"
3525                         );
3526
3527         sql_len = EM_SAFE_STRLEN(sql_query_string);
3528
3529         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " FROM mail_account_tbl ORDER BY account_id");
3530
3531         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3532
3533         EM_DEBUG_LOG_DEV("After sqlite3_prepare_v2 hStmt = %p", hStmt);
3534         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3535                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3536
3537         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3538         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3539                         ("sqlite3_step fail:%d", rc));
3540         if (rc == SQLITE_DONE) {
3541                 EM_DEBUG_EXCEPTION("no account found...");
3542
3543                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3544                 count = 0;
3545                 ret = true;
3546                 goto FINISH_OFF;
3547         }
3548
3549         if (!(p_data_tbl = (emstorage_account_tbl_t*)malloc(sizeof(emstorage_account_tbl_t) * count))) {
3550                 EM_DEBUG_EXCEPTION("malloc failed...");
3551                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3552                 goto FINISH_OFF;
3553         }
3554         memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * count);
3555         for (i = 0; i < count; i++) {
3556                 /*  get recordset */
3557                 field_index = 0;
3558                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_id), field_index++);
3559                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].account_name), 0, field_index++);
3560                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].logo_icon_path), 0, field_index++);
3561                 _get_stmt_field_data_blob(hStmt, &(p_data_tbl[i].user_data), field_index++);
3562                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].user_data_length), field_index++);
3563                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_svc_id), field_index++);
3564                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_status), field_index++);
3565                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_disabled), field_index++);
3566                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].default_mail_slot_size), field_index++);
3567                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].roaming_option), field_index++);
3568                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].color_label), field_index++);
3569                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_display_name), 0, field_index++);
3570                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_email_address), 0, field_index++);
3571                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].reply_to_address), 0, field_index++);
3572                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].return_address), 0, field_index++);
3573                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].incoming_server_type), field_index++);
3574                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_address), 0, field_index++);
3575                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_port_number), field_index++);
3576                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_user_name), 0, field_index++);
3577                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_password), 0, field_index++);
3578                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_secure_connection), field_index++);
3579                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].incoming_server_authentication_method), field_index++);
3580                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].retrieval_mode), field_index++);
3581                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].keep_mails_on_pop_server_after_download), field_index++);
3582                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].check_interval), field_index++);
3583                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_download_size), field_index++);
3584                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_interval), field_index++);
3585                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_days), field_index++);
3586                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_start_time), field_index++);
3587                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_end_time), field_index++);
3588                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].outgoing_server_type), field_index++);
3589                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_address), 0, field_index++);
3590                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_port_number), field_index++);
3591                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_user_name), 0, field_index++);
3592                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_password), 0, field_index++);
3593                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_secure_connection), field_index++);
3594                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_need_authentication), field_index++);
3595                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_use_same_authenticator), field_index++);
3596                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.priority), field_index++);
3597                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.keep_local_copy), field_index++);
3598                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_delivery_receipt), field_index++);
3599                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_read_receipt), field_index++);
3600                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.download_limit), field_index++);
3601                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_address), field_index++);
3602                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_subject), field_index++);
3603                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.display_name_from), 0, field_index++);
3604                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.reply_with_body), field_index++);
3605                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.forward_with_files), field_index++);
3606                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_myname_card), field_index++);
3607                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_signature), field_index++);
3608                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.signature), 0, field_index++);
3609                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.add_my_address_to_bcc), field_index++);
3610                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.notification_status), field_index++);
3611                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.vibrate_status), field_index++);
3612                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.display_content_status), field_index++);
3613                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.default_ringtone_status), field_index++);
3614                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.alert_ringtone_path), 0, field_index++);
3615                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_resend_times), field_index++);
3616                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_size_limit), field_index++);
3617                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].wifi_auto_download), field_index++);
3618                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].pop_before_smtp), field_index++);
3619                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_requires_apop), field_index++);
3620                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].smime_type), field_index++);
3621                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].certificate_path), 0, field_index++);
3622                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].cipher_type), field_index++);
3623                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].digest_type), field_index++);
3624
3625                 /* EAS passwd is not accessible */
3626                 if (with_password == true && p_data_tbl[i].incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
3627                         /*  get password from the secure storage */
3628                         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3629                         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3630
3631                         EM_SAFE_FREE(p_data_tbl[i].incoming_server_password);
3632                         EM_SAFE_FREE(p_data_tbl[i].outgoing_server_password);
3633
3634                         /*  get password file name */
3635                         error = _get_password_file_name(multi_user_name, p_data_tbl[i].account_id,
3636                                         recv_password_file_name,
3637                                         send_password_file_name);
3638                         if (error != EMAIL_ERROR_NONE) {
3639                                 EM_DEBUG_EXCEPTION("_get_password_file_name error [%d]", error);
3640                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3641                                 goto FINISH_OFF;
3642                         }
3643
3644                         /*  read password from secure storage */
3645                         error = _read_password_from_secure_storage(recv_password_file_name,
3646                                         &(p_data_tbl[i].incoming_server_password));
3647                         if (error < 0) {
3648                                 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s] error [%d]",
3649                                                 recv_password_file_name, error);
3650                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3651                                 goto FINISH_OFF;
3652                         }
3653                         error = _read_password_from_secure_storage(send_password_file_name,
3654                                         &(p_data_tbl[i].outgoing_server_password));
3655                         if (error < 0) {
3656                                 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s]  error [%d]",
3657                                                 send_password_file_name, error);
3658                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3659                                 goto FINISH_OFF;
3660                         }
3661                 }
3662
3663                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3664                 EM_DEBUG_LOG_DEV("after sqlite3_step(), i = %d, rc = %d.", i,  rc);
3665                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3666                                 ("sqlite3_step fail:%d", rc));
3667         }
3668
3669         ret = true;
3670
3671 FINISH_OFF:
3672         if (ret == true) {
3673                 *account_list = p_data_tbl;
3674                 *select_num = count;
3675                 EM_DEBUG_LOG_DEV("COUNT : %d", count);
3676         } else if (p_data_tbl != NULL)
3677                 emstorage_free_account(&p_data_tbl, count, NULL);
3678
3679         if (hStmt != NULL) {
3680                 rc = sqlite3_finalize(hStmt);
3681                 hStmt = NULL;
3682                 if (rc != SQLITE_OK) {
3683                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
3684                         error = EMAIL_ERROR_DB_FAILURE;
3685                 }
3686         }
3687
3688         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3689
3690         if (err_code != NULL)
3691                 *err_code = error;
3692
3693         EM_DEBUG_FUNC_END("ret [%d]", ret);
3694         return ret;
3695 }
3696
3697 INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(char *multi_user_name,
3698                 char *server_mail_id,
3699                 int mailbox_id,
3700                 emstorage_mail_tbl_t **mail,
3701                 int transaction,
3702                 int *err_code)
3703 {
3704         EM_DEBUG_FUNC_BEGIN_SEC("mailbox_id [%d], server_mail_id[%s], mail[%p], transaction[%d], err_code[%p]",
3705                         mailbox_id, server_mail_id, mail, transaction, err_code);
3706
3707         int ret = false, error = EMAIL_ERROR_NONE, result_count;
3708         char conditional_clause[QUERY_SIZE] = {0, };
3709         emstorage_mail_tbl_t* p_data_tbl = NULL;
3710
3711         if (!server_mail_id || !mail) {
3712                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3713                 error = EMAIL_ERROR_INVALID_PARAM;
3714                 goto FINISH_OFF;
3715         }
3716
3717         sqlite3_snprintf(sizeof(conditional_clause), conditional_clause, "WHERE server_mail_id = '%q'", server_mail_id);
3718
3719         if (mailbox_id > 0) {
3720                 SNPRINTF(conditional_clause + strlen(conditional_clause),
3721                                 QUERY_SIZE - strlen(conditional_clause),
3722                                 " AND mailbox_id = %d",
3723                                 mailbox_id);
3724         }
3725
3726         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
3727         if (!emstorage_query_mail_tbl(multi_user_name,
3728                                 conditional_clause,
3729                                 transaction,
3730                                 &p_data_tbl,
3731                                 &result_count,
3732                                 &error)) {
3733                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3734                 goto FINISH_OFF;
3735         }
3736
3737         ret = true;
3738
3739 FINISH_OFF:
3740
3741         if (ret == true)
3742                 *mail = p_data_tbl;
3743
3744         if (err_code != NULL)
3745                 *err_code = error;
3746
3747         EM_DEBUG_FUNC_END("ret [%d]", ret);
3748         return ret;
3749 }
3750
3751
3752 static int _write_conditional_clause_for_getting_mail_list(int account_id, int mailbox_id, email_email_address_list_t* addr_list, int thread_id, int start_index, int limit_count, int search_type, const char *search_value, email_sort_type_t sorting, bool input_except_delete_flagged_mails, char *conditional_clause_string, int buffer_size, int *err_code)
3753 {
3754         int cur_clause = 0, conditional_clause_count = 0, i = 0;
3755
3756         if (account_id < ALL_ACCOUNT) {
3757                 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3758                 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3759         }
3760
3761         /*  where clause */
3762         if (account_id != ALL_ACCOUNT) {
3763                 cur_clause += (conditional_clause_count++ == 0) ?
3764                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE account_id = %d", account_id) :
3765                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND account_id = %d", account_id);
3766         }
3767
3768         if (mailbox_id > 0) {
3769                 cur_clause += (conditional_clause_count++ == 0) ?
3770                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_id = %d", mailbox_id) :
3771                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_id = %d", mailbox_id);
3772         }
3773
3774         if (thread_id > 0) {
3775                 cur_clause += (conditional_clause_count++ == 0) ?
3776                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_id = %d ", thread_id) :
3777                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_id = %d ", thread_id);
3778         } else if (thread_id == EMAIL_LIST_TYPE_THREAD) {
3779                 cur_clause += (conditional_clause_count++ == 0) ?
3780                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_item_count > 0") :
3781                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_item_count > 0");
3782         } else if (thread_id == EMAIL_LIST_TYPE_LOCAL) {
3783                 cur_clause += (conditional_clause_count++ == 0) ?
3784                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE server_mail_status = 0") :
3785                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND server_mail_status = 0");
3786         } else if (thread_id == EMAIL_LIST_TYPE_UNREAD) {
3787                 cur_clause += (conditional_clause_count++ == 0) ?
3788                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_seen_field == 0") :
3789                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_seen_field == 0");
3790         }
3791
3792         /*  EM_DEBUG_LOG("where clause added [%s]", conditional_clause_string); */
3793         if (addr_list && addr_list->address_count > 0) {
3794                 if (!addr_list->address_type) {
3795                         cur_clause += (conditional_clause_count++ == 0) ?
3796                                 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE email_address_sender IN(\"%s\"", addr_list->address_list[0]) :
3797                                 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND email_address_sender IN(\"%s\"", addr_list->address_list[0]);
3798
3799                         for (i = 1; i < addr_list->address_count; i++)
3800                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3801
3802                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3803                 } else {
3804                         cur_clause += (conditional_clause_count++ == 0) ?
3805                                 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE full_address_to IN(\"%s\") or full_address_cc IN(\"%s\"", addr_list->address_list[0], addr_list->address_list[0]) :
3806                                 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND full_address_to IN(\"%s\") or full_address_cc IN(\"%s\"", addr_list->address_list[0], addr_list->address_list[0]);
3807
3808                         for (i = 1; i < addr_list->address_count; i++)
3809                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3810
3811                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3812                 }
3813         }
3814
3815         if (input_except_delete_flagged_mails) {
3816                 cur_clause += (conditional_clause_count++ == 0) ?
3817                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_deleted_field = 0") :
3818                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_deleted_field = 0");
3819         }
3820
3821         if (search_value) {
3822                 switch (search_type) {
3823                 case EMAIL_SEARCH_FILTER_SUBJECT:
3824                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3825                                         " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3826                         break;
3827                 case EMAIL_SEARCH_FILTER_SENDER:
3828                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3829                                         " %s  ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3830                                         ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3831                         break;
3832                 case EMAIL_SEARCH_FILTER_RECIPIENT:
3833                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3834                                         " %s ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3835                                         "       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3836                                         "       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3837                                         ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value);
3838                         break;
3839                 case EMAIL_SEARCH_FILTER_ALL:
3840                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3841                                         " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\' "
3842                                         "       OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3843                                         "                       OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3844                                         "                       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3845                                         "                       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3846                                         "               ) "
3847                                         "       )"
3848                                         ")", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value, search_value, search_value);
3849                         break;
3850                 }
3851         }
3852
3853         /*  EM_DEBUG_LOG("where clause [%s]", conditional_clause_string); */
3854         static char sorting_str[][50] = {
3855                 " ORDER BY date_time DESC",                                              /* case EMAIL_SORT_DATETIME_HIGH: */
3856                 " ORDER BY date_time ASC",                                                /* case EMAIL_SORT_DATETIME_LOW: */
3857                 " ORDER BY full_address_from DESC, date_time DESC", /* case EMAIL_SORT_SENDER_HIGH: */
3858                 " ORDER BY full_address_from ASC, date_time DESC",  /* case EMAIL_SORT_SENDER_LOW: */
3859                 " ORDER BY full_address_to DESC, date_time DESC",   /* case EMAIL_SORT_RCPT_HIGH: */
3860                 " ORDER BY full_address_to ASC, date_time DESC",        /* case EMAIL_SORT_RCPT_LOW: */
3861                 " ORDER BY subject DESC, date_time DESC",                  /* case EMAIL_SORT_SUBJECT_HIGH: */
3862                 " ORDER BY subject ASC, date_time DESC",                        /* case EMAIL_SORT_SUBJECT_LOW: */
3863                 " ORDER BY priority DESC, date_time DESC",                /* case EMAIL_SORT_PRIORITY_HIGH: */
3864                 " ORDER BY priority ASC, date_time DESC",                  /* case EMAIL_SORT_PRIORITY_LOW: */
3865                 " ORDER BY attachment_count DESC, date_time DESC",  /* case EMAIL_SORT_ATTACHMENT_HIGH: */
3866                 " ORDER BY attachment_count ASC, date_time DESC",   /* case EMAIL_SORT_ATTACHMENT_LOW: */
3867                 " ORDER BY lock_status DESC, date_time DESC",      /* case EMAIL_SORT_FAVORITE_HIGH: */
3868                 " ORDER BY lock_status ASC, date_time DESC",            /* case EMAIL_SORT_FAVORITE_LOW: */
3869                 " ORDER BY mailbox_id DESC, date_time DESC",      /* case EMAIL_SORT_MAILBOX_ID_HIGH: */
3870                 " ORDER BY mailbox_id ASC, date_time DESC",             /* case EMAIL_SORT_MAILBOX_ID_LOW: */
3871                 " ORDER BY flags_flagged_field DESC, date_time DESC",  /* case EMAIL_SORT_FLAGGED_FLAG_HIGH: */
3872                 " ORDER BY flags_flagged_field ASC, date_time DESC",   /* case EMAIL_SORT_FLAGGED_FLAG_LOW: */
3873                 " ORDER BY flags_seen_field DESC, date_time DESC",     /* case EMAIL_SORT_SEEN_FLAG_HIGH: */
3874                 " ORDER BY flags_seen_field ASC, date_time DESC"       /* case EMAIL_SORT_SEEN_FLAG_LOW: */
3875         };
3876
3877         if (sorting < EMAIL_SORT_END)
3878                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " %s", sorting_str[sorting]);
3879         else
3880                 EM_DEBUG_LOG(" Invalid Sorting order ");
3881
3882         /*  limit clause */
3883         if (start_index != -1 && limit_count != -1)
3884                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " LIMIT %d, %d", start_index, limit_count);
3885
3886         return true;
3887 }
3888
3889
3890 /**
3891  *      emstorage_get_mail_list - Get the mail list information.
3892  *
3893  *
3894  */
3895 INTERNAL_FUNC int emstorage_get_mail_list(char *multi_user_name, int account_id, int mailbox_id, email_email_address_list_t* addr_list, int thread_id, int start_index, int limit_count, int search_type, const char *search_value, email_sort_type_t sorting, int transaction, email_mail_list_item_t** mail_list,  int *result_count,  int *err_code)
3896 {
3897         EM_DEBUG_FUNC_BEGIN();
3898         EM_PROFILE_BEGIN(emstorage_get_mail_list_func);
3899
3900         int ret = false, error = EMAIL_ERROR_NONE;
3901         char conditional_clause_string[QUERY_SIZE] = { 0, };
3902
3903         if (account_id < ALL_ACCOUNT) {
3904                 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3905                 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3906         }
3907         EM_IF_NULL_RETURN_VALUE(result_count, false);
3908
3909         _write_conditional_clause_for_getting_mail_list(account_id, mailbox_id, addr_list, thread_id, start_index, limit_count, search_type, search_value, sorting, true, conditional_clause_string, QUERY_SIZE, &error);
3910
3911         EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
3912
3913         if (!emstorage_query_mail_list(multi_user_name, conditional_clause_string, transaction, mail_list, result_count, &error)) {
3914                 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
3915                 goto FINISH_OFF;
3916         }
3917
3918         ret = true;
3919
3920 FINISH_OFF:
3921         if (err_code != NULL)
3922                 *err_code = error;
3923
3924         EM_PROFILE_END(emstorage_get_mail_list_func);
3925         EM_DEBUG_FUNC_END("ret [%d]", ret);
3926         return ret;
3927 }
3928
3929
3930 /**
3931  *      emstorage_get_mails - Get the Mail list information based on mailbox_name name
3932  *
3933  *
3934  */
3935 INTERNAL_FUNC int emstorage_get_mails(char *multi_user_name, int account_id, int mailbox_id, email_email_address_list_t* addr_list, int thread_id, int start_index, int limit_count, email_sort_type_t sorting,  int transaction, emstorage_mail_tbl_t** mail_list, int *result_count, int *err_code)
3936 {
3937         EM_DEBUG_FUNC_BEGIN();
3938         EM_PROFILE_BEGIN(emStorageGetMails);
3939
3940         int count = 0, ret = false, error = EMAIL_ERROR_NONE;
3941         emstorage_mail_tbl_t *p_data_tbl = NULL;
3942         char conditional_clause_string[QUERY_SIZE] = {0, };
3943
3944         EM_IF_NULL_RETURN_VALUE(mail_list, false);
3945         EM_IF_NULL_RETURN_VALUE(result_count, false);
3946
3947         if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3948                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3949                 if (err_code != NULL)
3950                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3951                 return false;
3952         }
3953
3954         _write_conditional_clause_for_getting_mail_list(account_id, mailbox_id, addr_list, thread_id, start_index, limit_count, 0, NULL, sorting, true, conditional_clause_string, QUERY_SIZE, &error);
3955
3956         EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
3957
3958         if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause_string, transaction, &p_data_tbl, &count,  &error)) {
3959                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3960                 goto FINISH_OFF;
3961         }
3962
3963         ret = true;
3964
3965 FINISH_OFF:
3966
3967         if (ret == true) {
3968                 *mail_list = p_data_tbl;
3969                 *result_count = count;
3970                 EM_DEBUG_LOG("COUNT : %d", count);
3971         } else if (p_data_tbl != NULL)
3972                 emstorage_free_mail(&p_data_tbl, count, NULL);
3973
3974         if (err_code != NULL)
3975                 *err_code = error;
3976
3977         EM_PROFILE_END(emStorageGetMails);
3978         EM_DEBUG_FUNC_END("ret [%d]", ret);
3979         return ret;
3980 }
3981
3982
3983
3984 /**
3985  *      emstorage_get_searched_mail_list - Get the mail list information after filtering
3986  *
3987  *
3988  */
3989 INTERNAL_FUNC int emstorage_get_searched_mail_list(char *multi_user_name, int account_id, int mailbox_id, int thread_id, int search_type, const char *search_value, int start_index, int limit_count, email_sort_type_t sorting, int transaction, email_mail_list_item_t **mail_list,  int *result_count,  int *err_code)
3990 {
3991         EM_DEBUG_FUNC_BEGIN();
3992
3993         int ret = false, error = EMAIL_ERROR_NONE;
3994         char conditional_clause[QUERY_SIZE] = {0, };
3995         char *temp_search_value = NULL;
3996         char *temp_search_value2 = NULL;
3997
3998         EM_IF_NULL_RETURN_VALUE(mail_list, false);
3999         EM_IF_NULL_RETURN_VALUE(result_count, false);
4000
4001         if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
4002                 EM_DEBUG_EXCEPTION("select_num[%p], Mail_list[%p]", result_count, mail_list);
4003                 error = EMAIL_ERROR_INVALID_PARAM;
4004                 goto FINISH_OFF;
4005         }
4006
4007         temp_search_value = em_replace_all_string((char*)search_value, "_", "\\_");
4008         temp_search_value2 = em_replace_all_string(temp_search_value, "%", "\\%");
4009
4010         _write_conditional_clause_for_getting_mail_list(account_id, mailbox_id, NULL, thread_id, start_index, limit_count, search_type, temp_search_value2, sorting, true, conditional_clause, QUERY_SIZE, &error);
4011
4012         EM_DEBUG_LOG("conditional_clause[%s]", conditional_clause);
4013
4014         if (!emstorage_query_mail_list(multi_user_name, conditional_clause, transaction, mail_list, result_count, &error)) {
4015                 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
4016                 goto FINISH_OFF;
4017         }
4018
4019         ret = true;
4020
4021 FINISH_OFF:
4022         EM_DEBUG_LOG("emstorage_get_searched_mail_list finish off");
4023
4024         if (err_code != NULL)
4025                 *err_code = error;
4026
4027         EM_SAFE_FREE(temp_search_value);
4028         EM_SAFE_FREE(temp_search_value2);
4029
4030         EM_DEBUG_FUNC_END("ret [%d]", ret);
4031         return ret;
4032 }
4033
4034
4035 static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name)
4036 {
4037         EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
4038
4039         if (account_id <= 0 || !recv_password_file_name || !send_password_file_name) {
4040                 EM_DEBUG_EXCEPTION("Invalid parameter");
4041                 return EMAIL_ERROR_INVALID_PARAM;
4042         }
4043
4044         EM_DEBUG_LOG("MULTI_USER_NAME : [%s]", multi_user_name);
4045
4046         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
4047                 snprintf(recv_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_recv_%s", account_id, multi_user_name);
4048                 snprintf(send_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_send_%s", account_id, multi_user_name);
4049         } else {
4050                 snprintf(recv_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_recv", account_id);
4051                 snprintf(send_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_send", account_id);
4052         }
4053
4054         EM_DEBUG_FUNC_END();
4055         return EMAIL_ERROR_NONE;
4056 }
4057
4058 static int _read_password_from_secure_storage(char *file_name, char **password)
4059 {
4060         EM_DEBUG_FUNC_BEGIN_SEC("file_name[%s]", file_name);
4061
4062         if (!file_name || !password) {
4063                 EM_DEBUG_EXCEPTION("Invalid Parameter");
4064                 return EMAIL_ERROR_INVALID_PARAM;
4065         }
4066
4067         int ret = EMAIL_ERROR_NONE;
4068
4069         ret = emcore_get_password_in_key_manager(file_name, password);
4070         if (ret != EMAIL_ERROR_NONE) {
4071                 EM_DEBUG_EXCEPTION("emcore_get_password_in_key_manager failed : [%d]", ret);
4072                 goto FINISH_OFF;
4073         }
4074
4075 FINISH_OFF:
4076
4077         EM_DEBUG_FUNC_END("ret [%d]", ret);
4078         return ret;
4079 }
4080
4081 INTERNAL_FUNC int emstorage_get_account_by_id(char *multi_user_name, int account_id, int pulloption, emstorage_account_tbl_t **account, int transaction, int *err_code)
4082 {
4083         EM_DEBUG_FUNC_BEGIN("account_id[%d], pulloption[%d], account[%p], transaction[%d], err_code[%p]", account_id, pulloption, account, transaction, err_code);
4084
4085         if (!account) {
4086                 EM_DEBUG_EXCEPTION("account_id[%d], account[%p]", account_id, account);
4087                 if (err_code != NULL)
4088                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4089                 return false;
4090         }
4091
4092         int ret = false;
4093         int error = EMAIL_ERROR_NONE;
4094         emstorage_account_tbl_t* p_data_tbl = NULL;
4095         DB_STMT hStmt = NULL;
4096         char sql_query_string[QUERY_SIZE] = {0, };
4097         int rc = -1;
4098         int sql_len = 0;
4099         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4100         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4101
4102         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4103
4104         EMSTORAGE_START_READ_TRANSACTION(transaction);
4105
4106         /*  Make query string */
4107         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
4108         sql_len = EM_SAFE_STRLEN(sql_query_string);
4109
4110         if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4111                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4112                                 "incoming_server_type,"
4113                                 "incoming_server_address,"
4114                                 "user_email_address,"
4115                                 "incoming_server_user_name,"
4116                                 "retrieval_mode,"
4117                                 "incoming_server_port_number,"
4118                                 "incoming_server_secure_connection,"
4119                                 "incoming_server_authentication_method,"
4120                                 "outgoing_server_type,"
4121                                 "outgoing_server_address,"
4122                                 "outgoing_server_port_number,"
4123                                 "outgoing_server_need_authentication,"
4124                                 "outgoing_server_secure_connection,"
4125                                 "outgoing_server_user_name,"
4126                                 "user_display_name,"
4127                                 "reply_to_address,"
4128                                 "return_address,"
4129                                 "account_id,"
4130                                 "keep_mails_on_pop_server_after_download,"
4131                                 "auto_download_size,"
4132                                 "peak_interval,"
4133                                 "peak_days,"
4134                                 "peak_start_time,"
4135                                 "peak_end_time,"
4136                                 "outgoing_server_use_same_authenticator,"
4137                                 "auto_resend_times,"
4138                                 "outgoing_server_size_limit,"
4139                                 "wifi_auto_download,"
4140                                 "pop_before_smtp,"
4141                                 "incoming_server_requires_apop,"
4142                                 "logo_icon_path,"
4143                                 "user_data,"
4144                                 "user_data_length,"
4145                                 "color_label,"
4146                                 "check_interval,"
4147                                 "sync_status,");
4148                 sql_len = EM_SAFE_STRLEN(sql_query_string);
4149         }
4150
4151         if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME) {
4152                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " account_name, ");
4153                 sql_len = EM_SAFE_STRLEN(sql_query_string);
4154         }
4155
4156         /*  get from secure storage, not from db */
4157         if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4158                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4159                                 "priority,"
4160                                 "keep_local_copy,"
4161                                 "req_delivery_receipt,"
4162                                 "req_read_receipt,"
4163                                 "download_limit,"
4164                                 "block_address,"
4165                                 "block_subject,"
4166                                 "display_name_from,"
4167                                 "reply_with_body,"
4168                                 "forward_with_files,"
4169                                 "add_myname_card,"
4170                                 "add_signature,"
4171                                 "signature,"
4172                                 "add_my_address_to_bcc,"
4173                                 "notification_status,"
4174                                 "vibrate_status,"
4175                                 "display_content_status,"
4176                                 "default_ringtone_status,"
4177                                 "alert_ringtone_path,"
4178                                 "account_svc_id,"
4179                                 "sync_disabled,"
4180                                 "default_mail_slot_size,"
4181                                 "roaming_option,"
4182                                 "smime_type,"
4183                                 "certificate_path,"
4184                                 "cipher_type,"
4185                                 "digest_type,");
4186
4187                 sql_len = EM_SAFE_STRLEN(sql_query_string);
4188         }
4189
4190         /*  dummy value, FROM WHERE clause */
4191         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "0 FROM mail_account_tbl WHERE account_id = %d", account_id);
4192
4193         /*  FROM clause */
4194         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
4195
4196         /*  execute a sql and count rows */
4197         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4198         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4199                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4200
4201         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4202         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4203                         ("sqlite3_step fail:%d", rc));
4204
4205         if (rc == SQLITE_DONE) {
4206                 EM_DEBUG_EXCEPTION("no matched account found...");
4207                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4208                 goto FINISH_OFF;
4209         }
4210
4211         /*  Assign query result to structure */
4212         if (!(p_data_tbl = (emstorage_account_tbl_t *)malloc(sizeof(emstorage_account_tbl_t) * 1))) {
4213                 EM_DEBUG_EXCEPTION("malloc failed...");
4214                 error = EMAIL_ERROR_OUT_OF_MEMORY;
4215                 goto FINISH_OFF;
4216         }
4217
4218         memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * 1);
4219         int col_index = 0;
4220
4221         if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4222                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_type), col_index++);
4223                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_address),  0, col_index++);
4224                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_email_address), 0, col_index++);
4225                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_user_name), 0, col_index++);
4226                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->retrieval_mode), col_index++);
4227                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_port_number), col_index++);
4228                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_secure_connection), col_index++);
4229                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_authentication_method), col_index++);
4230                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->outgoing_server_type), col_index++);
4231                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_address), 0, col_index++);
4232                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_port_number), col_index++);
4233                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_need_authentication), col_index++);
4234                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_secure_connection), col_index++);
4235                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_user_name), 0, col_index++);
4236                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_display_name), 0, col_index++);
4237                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->reply_to_address), 0, col_index++);
4238                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->return_address), 0, col_index++);
4239                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
4240                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->keep_mails_on_pop_server_after_download), col_index++);
4241                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_download_size), col_index++);
4242                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_interval), col_index++);
4243                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_days), col_index++);
4244                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_start_time), col_index++);
4245                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_end_time), col_index++);
4246                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_use_same_authenticator), col_index++);
4247                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_resend_times), col_index++);
4248                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_size_limit), col_index++);
4249                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->wifi_auto_download), col_index++);
4250                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->pop_before_smtp), col_index++);
4251                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_requires_apop), col_index++);
4252                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->logo_icon_path), 0, col_index++);
4253                 _get_stmt_field_data_blob(hStmt, &p_data_tbl->user_data, col_index++);
4254                 _get_stmt_field_data_int(hStmt, &p_data_tbl->user_data_length, col_index++);
4255                 _get_stmt_field_data_int(hStmt, &p_data_tbl->color_label, col_index++);
4256                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->check_interval), col_index++);
4257                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->sync_status), col_index++);
4258         }
4259
4260         if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME)
4261                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->account_name), 0, col_index++);
4262
4263         if (pulloption & EMAIL_ACC_GET_OPT_PASSWORD) {
4264                 /*  get password file name */
4265                 if ((error = _get_password_file_name(multi_user_name, p_data_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4266                         EM_DEBUG_EXCEPTION("_get_password_file_name failed [%d]", error);
4267                         goto FINISH_OFF;
4268                 }
4269
4270                 /*  read password from secure storage */
4271                 if ((error = _read_password_from_secure_storage(recv_password_file_name, &(p_data_tbl->incoming_server_password))) < 0) {
4272                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4273                         goto FINISH_OFF;
4274                 }
4275
4276                 EM_DEBUG_LOG_DEV("recv_password_file_name[%s], password[%s]", recv_password_file_name,  p_data_tbl->incoming_server_password);
4277
4278                 if (p_data_tbl->outgoing_server_use_same_authenticator == 0) {
4279                         if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl->outgoing_server_password))) < 0) {
4280                                 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4281                                 goto FINISH_OFF;
4282                         }
4283                         EM_DEBUG_LOG_DEV("send_password_file_name[%s], password[%s]", send_password_file_name,  p_data_tbl->outgoing_server_password);
4284                 }
4285         }
4286
4287         if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4288                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.priority), col_index++);
4289                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.keep_local_copy), col_index++);
4290                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_delivery_receipt), col_index++);
4291                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_read_receipt), col_index++);
4292                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.download_limit), col_index++);
4293                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_address), col_index++);
4294                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_subject), col_index++);
4295                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.display_name_from), 0, col_index++);
4296                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.reply_with_body), col_index++);
4297                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.forward_with_files), col_index++);
4298                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_myname_card), col_index++);
4299                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_signature), col_index++);
4300                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.signature), 0, col_index++);
4301                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.add_my_address_to_bcc), col_index++);
4302                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.notification_status), col_index++);
4303                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.vibrate_status), col_index++);
4304                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.display_content_status), col_index++);
4305                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.default_ringtone_status), col_index++);
4306                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.alert_ringtone_path), 0, col_index++);
4307                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_svc_id), col_index++);
4308                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->sync_disabled), col_index++);
4309                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->default_mail_slot_size), col_index++);
4310                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->roaming_option), col_index++);
4311                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->smime_type), col_index++);
4312                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->certificate_path), 0, col_index++);
4313                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->cipher_type), col_index++);
4314                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->digest_type), col_index++);
4315         }
4316
4317         ret = true;
4318
4319 FINISH_OFF:
4320         if (ret == true)
4321                 *account = p_data_tbl;
4322         else {
4323                 if (p_data_tbl)
4324                         emstorage_free_account((emstorage_account_tbl_t **)&p_data_tbl, 1, NULL);
4325         }
4326         if (hStmt != NULL) {
4327                 rc = sqlite3_finalize(hStmt);
4328                 if (rc != SQLITE_OK) {
4329                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
4330                         error = EMAIL_ERROR_DB_FAILURE;
4331                 }
4332         }
4333
4334         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4335
4336         if (err_code != NULL)
4337                 *err_code = error;
4338
4339         EM_DEBUG_FUNC_END("ret [%d]", ret);
4340         return ret;
4341 }
4342
4343 INTERNAL_FUNC int emstorage_get_password_length_of_account(char *multi_user_name, int account_id, int password_type, int *password_length, int *err_code)
4344 {
4345         EM_DEBUG_FUNC_BEGIN("account_id[%d], password_length[%p], err_code[%p]", account_id, password_length, err_code);
4346
4347         if (account_id <= 0 || password_length == NULL) {
4348                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4349                 if (err_code != NULL)
4350                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4351                 return false;
4352         }
4353
4354         int ret = false;
4355         int error = EMAIL_ERROR_NONE;
4356         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4357         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4358         char *temp_password = NULL;
4359
4360
4361         /*  get password file name */
4362         if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4363                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4364                 goto FINISH_OFF;
4365         }
4366
4367         /*  read password from secure storage */
4368         if (password_type == EMAIL_GET_INCOMING_PASSWORD_LENGTH) {
4369                 if ((error = _read_password_from_secure_storage(recv_password_file_name, &temp_password)) < 0 || !temp_password) {
4370                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed...");
4371                         goto FINISH_OFF;
4372                 }
4373         } else if (password_type == EMAIL_GET_OUTGOING_PASSWORD_LENGTH) {
4374                 if ((error = _read_password_from_secure_storage(send_password_file_name, &temp_password)) < 0 || !temp_password) {
4375                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed...");
4376                         goto FINISH_OFF;
4377                 }
4378         } else {
4379                 EM_DEBUG_LOG("Invalid password type");
4380                 if (err_code != NULL)
4381                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4382                 goto FINISH_OFF;
4383         }
4384
4385         *password_length = EM_SAFE_STRLEN(temp_password);
4386
4387         EM_DEBUG_LOG_SEC("recv_password_file_name[%s], *password_length[%d]", recv_password_file_name,  *password_length);
4388
4389         ret = true;
4390
4391 FINISH_OFF:
4392         EM_SAFE_FREE(temp_password);
4393
4394         if (err_code != NULL)
4395                 *err_code = error;
4396
4397         EM_DEBUG_FUNC_END("ret [%d]", ret);
4398         return ret;
4399 }
4400
4401 INTERNAL_FUNC int emstorage_update_account_password(char *multi_user_name, int input_account_id, char *input_incoming_server_password, char *input_outgoing_server_password)
4402 {
4403         EM_DEBUG_FUNC_BEGIN("input_account_id[%d], input_incoming_server_password[%p], input_outgoing_server_password[%p]", input_account_id, input_incoming_server_password, input_outgoing_server_password);
4404
4405         int err = EMAIL_ERROR_NONE;
4406         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4407         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4408
4409         if (input_incoming_server_password == NULL && input_outgoing_server_password == NULL) {
4410                 EM_DEBUG_EXCEPTION_SEC("Invalid param");
4411                 err = EMAIL_ERROR_INVALID_PARAM;
4412                 goto FINISH_OFF;
4413         }
4414
4415         /*  get password file name */
4416         if ((err = _get_password_file_name(multi_user_name,
4417                                         input_account_id,
4418                                         recv_password_file_name,
4419                                         send_password_file_name)) != EMAIL_ERROR_NONE) {
4420                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4421                 goto FINISH_OFF;
4422         }
4423
4424         EM_DEBUG_LOG_SEC("recv_password_file_name [%s] input_incoming_server_password [%s]",
4425                         recv_password_file_name, input_incoming_server_password);
4426         EM_DEBUG_LOG_SEC("send_password_file_name [%s] input_outgoing_server_password [%s]",
4427                         send_password_file_name, input_outgoing_server_password);
4428
4429         if (input_incoming_server_password) {
4430                 err = emcore_remove_password_in_key_manager(recv_password_file_name);
4431                 if (err != EMAIL_ERROR_NONE) {
4432                         EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", recv_password_file_name);
4433                         goto FINISH_OFF;
4434                 }
4435
4436                 /*  save recv passwords to the secure storage */
4437                 err = emcore_add_password_in_key_manager(recv_password_file_name, input_incoming_server_password);
4438                 if (err != EMAIL_ERROR_NONE) {
4439                         EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4440                         goto FINISH_OFF;
4441                 }
4442         }
4443
4444         if (input_outgoing_server_password) {
4445                 err = emcore_remove_password_in_key_manager(send_password_file_name);
4446                 if (err != EMAIL_ERROR_NONE) {
4447                         EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", send_password_file_name);
4448                         goto FINISH_OFF;
4449                 }
4450
4451                 /*  save send passwords to the secure storage */
4452                 err = emcore_add_password_in_key_manager(send_password_file_name, input_outgoing_server_password);
4453                 if (err != EMAIL_ERROR_NONE) {
4454                         EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4455                         goto FINISH_OFF;
4456                 }
4457         }
4458 FINISH_OFF:
4459
4460         EM_DEBUG_FUNC_END("err [%d]", err);
4461         return err;
4462 }
4463
4464 INTERNAL_FUNC int emstorage_update_account(char *multi_user_name, int account_id, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
4465 {
4466         EM_DEBUG_FUNC_BEGIN("account_id[%d], account[%p], transaction[%d], err_code[%p]", account_id, account_tbl, transaction, err_code);
4467
4468         if (account_id < FIRST_ACCOUNT_ID || !account_tbl) {
4469                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4470                 if (err_code != NULL)
4471                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4472                 return false;
4473         }
4474
4475         int error = EMAIL_ERROR_NONE;
4476         int rc, ret = false;
4477
4478         DB_STMT hStmt = NULL;
4479         char sql_query_string[QUERY_SIZE] = {0, };
4480
4481         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4482
4483         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4484
4485         SNPRINTF(sql_query_string, sizeof(sql_query_string),
4486                         "UPDATE mail_account_tbl SET"
4487                         "  account_name = ?"
4488                         ", logo_icon_path = ?"
4489                         ", user_data = ?"
4490                         ", user_data_length = ?"
4491                         ", account_svc_id = ?"
4492                         ", sync_status = ?"
4493                         ", sync_disabled = ?"
4494                         ", default_mail_slot_size = ?"
4495                         ", roaming_option = ?"
4496                         ", color_label = ?"
4497                         ", user_display_name = ?"
4498                         ", user_email_address = ?"
4499                         ", reply_to_address = ?"
4500                         ", return_address = ?"
4501                         ", incoming_server_type = ?"
4502                         ", incoming_server_address = ?"
4503                         ", incoming_server_port_number = ?"
4504                         ", incoming_server_user_name = ?"
4505                         ", incoming_server_secure_connection = ?"
4506                         ", incoming_server_authentication_method = ?"
4507                         ", retrieval_mode = ?"
4508                         ", keep_mails_on_pop_server_after_download = ?"
4509                         ", check_interval = ?"
4510                         ", auto_download_size = ?"
4511                         ", peak_interval = ?"
4512                         ", peak_days = ?"
4513                         ", peak_start_time = ?"
4514                         ", peak_end_time = ?"
4515                         ", outgoing_server_type = ?"
4516                         ", outgoing_server_address = ?"
4517                         ", outgoing_server_port_number = ?"
4518                         ", outgoing_server_user_name = ?"
4519                         ", outgoing_server_secure_connection = ?"
4520                         ", outgoing_server_need_authentication = ?"
4521                         ", outgoing_server_use_same_authenticator = ?"
4522                         ", priority = ?"
4523                         ", keep_local_copy = ?"
4524                         ", req_delivery_receipt = ?"
4525                         ", req_read_receipt = ?"
4526                         ", download_limit = ?"
4527                         ", block_address = ?"
4528                         ", block_subject = ?"
4529                         ", display_name_from = ?"
4530                         ", reply_with_body = ?"
4531                         ", forward_with_files = ?"
4532                         ", add_myname_card = ?"
4533                         ", add_signature = ?"
4534                         ", signature = ?"
4535                         ", add_my_address_to_bcc = ?"
4536                         ", notification_status = ?"
4537                         ", vibrate_status = ?"
4538                         ", display_content_status = ?"
4539                         ", default_ringtone_status = ?"
4540                         ", alert_ringtone_path = ?"
4541                         ", auto_resend_times = ?"
4542                         ", outgoing_server_size_limit = ?"
4543                         ", wifi_auto_download = ?"
4544                         ", pop_before_smtp = ?"
4545                         ", incoming_server_requires_apop = ?"
4546                         ", smime_type = ?"
4547                         ", certificate_path = ?"
4548                         ", cipher_type = ?"
4549                         ", digest_type = ?"
4550                         ", user_name = ?"
4551                         " WHERE account_id = ?");
4552
4553         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4554         EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
4555         EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
4556         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4557                         ("sqlite3_prepare fail:(%d) %s", rc, sqlite3_errmsg(local_db_handle)));
4558
4559         int i = 0;
4560
4561         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4562         _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4563         _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4564         _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4565         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4566         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4567         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4568         _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4569         _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
4570         _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
4571         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4572         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4573         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4574         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4575         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4576         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4577         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
4578         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4579         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
4580         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
4581         _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
4582         _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
4583         _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
4584         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
4585         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
4586         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
4587         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
4588         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
4589         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
4590         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4591         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
4592         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
4593         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
4594         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
4595         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
4596         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
4597         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
4598         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
4599         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
4600         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
4601         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
4602         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
4603         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4604         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
4605         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
4606         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
4607         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
4608         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
4609         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
4610         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
4611         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
4612         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
4613         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
4614         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4615         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
4616         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
4617         _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
4618         _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
4619         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
4620         _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
4621         _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4622         _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
4623         _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
4624         _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4625         _bind_stmt_field_data_int(hStmt, i++, account_id);
4626
4627         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4628         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
4629                         ("sqlite3_step fail:%d", rc));
4630         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4631                         ("sqlite3_step fail:%d", rc));
4632
4633         /*  validate account existence */
4634         rc = sqlite3_changes(local_db_handle);
4635         if (rc == 0) {
4636                 EM_DEBUG_EXCEPTION("no matched account found...");
4637                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4638                 goto FINISH_OFF;
4639         }
4640
4641         if (account_tbl->incoming_server_password || account_tbl->outgoing_server_password) {
4642                 if ((error = emstorage_update_account_password(multi_user_name, account_id, account_tbl->incoming_server_password, account_tbl->outgoing_server_password)) != EMAIL_ERROR_NONE) {
4643                         EM_DEBUG_EXCEPTION("emstorage_update_account_password failed [%d]", error);
4644                         goto FINISH_OFF;
4645                 }
4646         }
4647
4648         ret = true;
4649
4650 FINISH_OFF:
4651         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4652
4653         if (hStmt != NULL) {
4654                 rc = sqlite3_finalize(hStmt);
4655                 if (rc != SQLITE_OK) {
4656                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
4657                         error = EMAIL_ERROR_DB_FAILURE;
4658                 }
4659         }
4660
4661         if (error == EMAIL_ERROR_NONE) {
4662                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_tbl->account_id, 0, NULL, 0))
4663                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_ACCOUNT_UPDATE] : Notification Failed >>> ");
4664         }
4665
4666         if (err_code != NULL)
4667                 *err_code = error;
4668
4669         EM_DEBUG_FUNC_END("ret [%d]", ret);
4670         return ret;
4671 }
4672
4673 INTERNAL_FUNC int emstorage_set_field_of_accounts_with_integer_value(char *multi_user_name, int account_id, char *field_name, int value, int transaction)
4674 {
4675         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], field_name[%s], value[%d], transaction[%d]", account_id, field_name, value, transaction);
4676         int error = EMAIL_ERROR_NONE;
4677         int result = 0;
4678         char sql_query_string[QUERY_SIZE] = {0, };
4679         sqlite3 *local_db_handle = NULL;
4680
4681         if (!account_id  || !field_name) {
4682                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4683                 return EMAIL_ERROR_INVALID_PARAM;
4684         }
4685
4686         local_db_handle = emstorage_get_db_connection(multi_user_name);
4687
4688         /* Write query string */
4689         SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_account_tbl SET %s = %d WHERE account_id = %d", field_name, value, account_id);
4690
4691         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4692
4693         /* Execute query */
4694         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4695
4696         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4697         if (error != EMAIL_ERROR_NONE) {
4698                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4699                 goto FINISH_OFF;
4700         }
4701
4702         if (sqlite3_changes(local_db_handle) == 0)
4703                 EM_DEBUG_LOG("no mail matched...");
4704
4705
4706 FINISH_OFF:
4707         result = (error == EMAIL_ERROR_NONE) ? true : false;
4708         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, error);
4709
4710         if (error == EMAIL_ERROR_NONE) {
4711                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_id, 0, field_name, value))
4712                         EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_ACCOUNT_UPDATE [%s,%d]", field_name, value);
4713         }
4714         EM_DEBUG_FUNC_END("error [%d]", error);
4715         return error;
4716 }
4717
4718 INTERNAL_FUNC int emstorage_get_sync_status_of_account(char *multi_user_name, int account_id, int *result_sync_status, int *err_code)
4719 {
4720         EM_DEBUG_FUNC_BEGIN("account_id[%d], result_sync_status [%p], err_code[%p]", account_id, result_sync_status, err_code);
4721
4722         if (!result_sync_status) {
4723                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4724                 if (err_code != NULL)
4725                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4726                 return false;
4727         }
4728
4729         int error = EMAIL_ERROR_NONE, rc, ret = false, sync_status = 0, count, i, col_index;
4730         char sql_query_string[QUERY_SIZE] = {0, };
4731         char **result = NULL;
4732         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4733
4734         if (account_id)
4735                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl WHERE account_id = %d", account_id);
4736         else
4737                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl");
4738
4739         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
4740         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
4741                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4742
4743         if (!count) {
4744                 EM_DEBUG_EXCEPTION("no matched account found...");
4745                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4746                 goto FINISH_OFF;
4747         }
4748
4749         col_index = 1;
4750         *result_sync_status = 0;
4751
4752         for (i = 0; i < count; i++) {
4753                 _get_table_field_data_int(result, &sync_status, col_index++);
4754                 *result_sync_status |= sync_status;
4755         }
4756
4757         EM_DEBUG_LOG("sync_status [%d]", sync_status);
4758
4759         sqlite3_free_table(result);
4760
4761         ret = true;
4762
4763 FINISH_OFF:
4764
4765
4766         if (err_code != NULL)
4767                 *err_code = error;
4768
4769         EM_DEBUG_FUNC_END("ret [%d]", ret);
4770         return ret;
4771 }
4772
4773 INTERNAL_FUNC int emstorage_update_sync_status_of_account(char *multi_user_name, int account_id, email_set_type_t set_operator, int sync_status, int transaction, int *err_code)
4774 {
4775         EM_DEBUG_FUNC_BEGIN("account_id[%d], set_operator[%d], sync_status [%d], transaction[%d], err_code[%p]", account_id, set_operator, sync_status, transaction, err_code);
4776
4777         int error = EMAIL_ERROR_NONE, rc, ret = false, set_value = sync_status, result_sync_status;
4778         char sql_query_string[QUERY_SIZE] = {0, };
4779         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4780
4781         if (set_operator != SET_TYPE_SET && account_id) {
4782                 if (!emstorage_get_sync_status_of_account(multi_user_name, account_id, &result_sync_status, &error)) {
4783                         EM_DEBUG_EXCEPTION("emstorage_get_sync_status_of_account failed [%d]", error);
4784                         if (err_code != NULL)
4785                                 *err_code = error;
4786                         return false;
4787                 }
4788                 switch (set_operator) {
4789                 case SET_TYPE_UNION:
4790                         set_value = result_sync_status | set_value;
4791                         break;
4792                 case SET_TYPE_MINUS:
4793                         set_value = result_sync_status & (~set_value);
4794                         break;
4795                 default:
4796                         EM_DEBUG_EXCEPTION("EMAIL_ERROR_NOT_SUPPORTED [%d]", set_operator);
4797                         error = EMAIL_ERROR_NOT_SUPPORTED;
4798                         break;
4799                 }
4800                 EM_DEBUG_LOG("set_value [%d]", set_value);
4801         }
4802
4803         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4804
4805         if (account_id)
4806                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE account_id = %d", set_value, account_id);
4807         else
4808                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE incoming_server_type <> 5", set_value);
4809
4810         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4811
4812         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4813         if (error != EMAIL_ERROR_NONE) {
4814                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4815                 goto FINISH_OFF;
4816         }
4817
4818         rc = sqlite3_changes(local_db_handle);
4819
4820         if (rc == 0) {
4821                 EM_DEBUG_EXCEPTION("no matched account found...");
4822                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4823                 goto FINISH_OFF;
4824         }
4825
4826         ret = true;
4827
4828 FINISH_OFF:
4829         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4830
4831         if (error == EMAIL_ERROR_NONE) {
4832                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE_SYNC_STATUS, account_id, set_value, NULL, 0))
4833                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_UPDATE_SYNC_STATUS] : Notification failed");
4834         }
4835
4836         if (err_code != NULL)
4837                 *err_code = error;
4838
4839         EM_DEBUG_FUNC_END("ret [%d]", ret);
4840         return ret;
4841 }
4842
4843 INTERNAL_FUNC int emstorage_add_account(char *multi_user_name, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
4844 {
4845         EM_DEBUG_FUNC_BEGIN("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4846
4847         if (!account_tbl) {
4848                 EM_DEBUG_EXCEPTION("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4849                 if (err_code != NULL)
4850                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4851                 return false;
4852         }
4853
4854         int row_count = 0;
4855         int i = 0;
4856         int rc = -1, ret = false;
4857         int error = EMAIL_ERROR_NONE;
4858         DB_STMT hStmt = NULL;
4859         char sql_query_string[QUERY_SIZE] = {0, };
4860         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4861         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4862
4863         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4864
4865         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4866
4867         char *sql = "SELECT rowid FROM mail_account_tbl;";
4868         char **result = NULL;
4869
4870         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, &row_count, NULL, NULL), rc);
4871         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
4872                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
4873
4874         if (NULL == result[1]) rc = 1;
4875         else {
4876                 for (i = 1; i <= row_count; i++) {
4877                         if (i != atoi(result[i]))
4878                                 break;
4879                 }
4880
4881                 rc = i;
4882         }
4883         sqlite3_free_table(result);
4884         result = NULL;
4885
4886         if (rc < 0 || rc > EMAIL_ACCOUNT_MAX) {
4887                 EM_DEBUG_EXCEPTION("OVERFLOWED THE MAX ACCOUNT");
4888                 error = EMAIL_ERROR_ACCOUNT_MAX_COUNT;
4889                 goto FINISH_OFF;
4890         }
4891
4892         account_tbl->account_id = rc;
4893
4894         if ((error = _get_password_file_name(multi_user_name, account_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4895                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4896                 goto FINISH_OFF;
4897         }
4898
4899         EM_DEBUG_LOG(" >>>> ACCOUNT_ID [ %d ] ", account_tbl->account_id);
4900         SNPRINTF(sql_query_string, sizeof(sql_query_string),
4901                         "INSERT INTO mail_account_tbl VALUES "
4902                         "(        "
4903                         "    ? "  /*   account_id */
4904                         "  , ? "  /*   account_name */
4905                         "  , ? "  /*   logo_icon_path */
4906                         "  , ? "  /*   user_data */
4907                         "  , ? "  /*   user_data_length */
4908                         "  , ? "  /*   account_svc_id */
4909                         "  , ? "  /*   sync_status */
4910                         "  , ? "  /*   sync_disabled */
4911                         "  , ? "  /*   default_mail_slot_size */
4912                         "  , ? "  /*   roaming_option */
4913                         "  , ? "  /*   color_label */
4914                         "  , ? "  /*   user_display_name */
4915                         "  , ? "  /*   user_email_address */
4916                         "  , ? "  /*   reply_to_address */
4917                         "  , ? "  /*   return_address */
4918                         "  , ? "  /*   incoming_server_type */
4919                         "  , ? "  /*   incoming_server_address */
4920                         "  , ? "  /*   incoming_server_port_number */
4921                         "  , ? "  /*   incoming_server_user_name */
4922                         "  , ? "  /*   incoming_server_password */
4923                         "  , ? "  /*   incoming_server_secure_connection */
4924                         "  , ? "  /*   incoming_server_authentication_method */
4925                         "  , ? "  /*   retrieval_mode */
4926                         "  , ? "  /*   keep_mails_on_pop_server_after_download */
4927                         "  , ? "  /*   check_interval */
4928                         "  , ? "  /*   auto_download_size */
4929                         "  , ? "  /*   peak_interval */
4930                         "  , ? "  /*   peak_days */
4931                         "  , ? "  /*   peak_start_time */
4932                         "  , ? "  /*   peak_end_time */
4933                         "  , ? "  /*   outgoing_server_type */
4934                         "  , ? "  /*   outgoing_server_address */
4935                         "  , ? "  /*   outgoing_server_port_number */
4936                         "  , ? "  /*   outgoing_server_user_name */
4937                         "  , ? "  /*   outgoing_server_password */
4938                         "  , ? "  /*   outgoing_server_secure_connection */
4939                         "  , ? "  /*   outgoing_server_need_authentication */
4940                         "  , ? "  /*   outgoing_server_use_same_authenticator */
4941                         "  , ? "  /*   priority */
4942                         "  , ? "  /*   keep_local_copy */
4943                         "  , ? "  /*   req_delivery_receipt */
4944                         "  , ? "  /*   req_read_receipt */
4945                         "  , ? "  /*   download_limit */
4946                         "  , ? "  /*   block_address */
4947                         "  , ? "  /*   block_subject */
4948                         "  , ? "  /*   display_name_from */
4949                         "  , ? "  /*   reply_with_body */
4950                         "  , ? "  /*   forward_with_files */
4951                         "  , ? "  /*   add_myname_card */
4952                         "  , ? "  /*   add_signature */
4953                         "  , ? "  /*   signature */
4954                         "  , ? "  /*   add_my_address_to_bcc */
4955                         "  , ? "  /*   auto_resend_times */
4956                         "  , ? "  /*   outgoing_server_size_limit */
4957                         "  , ? "  /*   wifi_auto_download */
4958                         "  , ? "  /*   pop_before_smtp */
4959                         "  , ? "  /*   incoming_server_requires_apop */
4960                         "  , ? "  /*   smime_type */
4961                         "  , ? "  /*   certificate_path */
4962                         "  , ? "  /*   cipher_type */
4963                         "  , ? "  /*   digest_type */
4964                         "  , ? "  /*   notification_status */
4965                         "  , ? "  /*   vibrate_status */
4966                         "  , ? "  /*   display_content_status */
4967                         "  , ? "  /*   default_ringtone_status */
4968                         "  , ? "  /*   alert_ringtone_path */
4969                         "  , ? "  /*   user_name */
4970                         ") ");
4971
4972         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4973         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4974                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4975
4976         i = 0;
4977
4978         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_id);
4979         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4980         _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4981         _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4982         _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4983         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4984         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4985         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4986         _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4987         _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
4988         _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
4989         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4990         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4991         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4992         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4993         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4994         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4995         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
4996         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4997         _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
4998         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
4999         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
5000         _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
5001         _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
5002         _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
5003         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
5004         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
5005         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
5006         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
5007         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
5008         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
5009         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5010         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
5011         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
5012         _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
5013         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
5014         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
5015         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
5016         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
5017         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
5018         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
5019         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
5020         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
5021         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
5022         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
5023         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
5024         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
5025         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
5026         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
5027         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
5028         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
5029         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
5030         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
5031         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
5032         _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
5033         _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
5034         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
5035         _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
5036         _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
5037         _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
5038         _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
5039         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
5040         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
5041         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
5042         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
5043         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5044         _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5045
5046         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5047
5048         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
5049                         ("sqlite3_step fail:%d", rc));
5050         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5051                         ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
5052
5053
5054         /*  save passwords to the secure storage */
5055         EM_DEBUG_LOG_SEC("save to the secure storage : recv_file[%s], send_file[%s]", recv_password_file_name, send_password_file_name);
5056         error = emcore_add_password_in_key_manager(recv_password_file_name, account_tbl->incoming_server_password);
5057         if (error != EMAIL_ERROR_NONE) {
5058                 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
5059                 goto FINISH_OFF;
5060         }
5061
5062         error = emcore_add_password_in_key_manager(send_password_file_name, account_tbl->outgoing_server_password);
5063         if (error != EMAIL_ERROR_NONE) {
5064                 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
5065                 goto FINISH_OFF;
5066         }
5067
5068         ret = true;
5069
5070 FINISH_OFF:
5071         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5072
5073         if (hStmt != NULL) {
5074                 rc = sqlite3_finalize(hStmt);
5075                 if (rc != SQLITE_OK) {
5076                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5077                         error = EMAIL_ERROR_DB_FAILURE;
5078                 }
5079         }
5080
5081         if (error == EMAIL_ERROR_NONE) {
5082                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_ADD, account_tbl->account_id, 0, NULL, 0))
5083                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_ADD] : Notification failed");
5084         }
5085
5086         if (err_code != NULL)
5087                 *err_code = error;
5088
5089         EM_DEBUG_FUNC_END("ret [%d]", ret);
5090         return ret;
5091 }
5092
5093 INTERNAL_FUNC int emstorage_delete_account(char *multi_user_name, int account_id, int transaction, int *err_code)
5094 {
5095         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
5096
5097         if (account_id < FIRST_ACCOUNT_ID) {
5098                 EM_DEBUG_EXCEPTION(" account_id[%d]", account_id);
5099
5100                 if (err_code != NULL)
5101                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5102                 return false;
5103         }
5104
5105         int rc = -1, ret = false;
5106         int error = EMAIL_ERROR_NONE;
5107
5108         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5109
5110         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5111
5112         /*  TODO : delete password files - file names can be obtained from db or a rule that makes a name */
5113         char sql_query_string[QUERY_SIZE] = {0, };
5114         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5115         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5116
5117         /*  get password file name */
5118         if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
5119                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
5120                 goto FINISH_OFF;
5121         }
5122
5123         /*  delete from db */
5124         memset(sql_query_string, 0x00, sizeof(sql_query_string));
5125         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_account_tbl WHERE account_id = %d", account_id);
5126         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5127         if (error != EMAIL_ERROR_NONE) {
5128                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5129                 goto FINISH_OFF;
5130         }
5131
5132         /*  validate account existence */
5133         rc = sqlite3_changes(local_db_handle);
5134         if (rc == 0) {
5135                 EM_DEBUG_EXCEPTION(" no matched account found...");
5136                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
5137                 goto FINISH_OFF;
5138         }
5139
5140         /*  delete from secure storage */
5141         error = emcore_remove_password_in_key_manager(recv_password_file_name);
5142         if (error != EMAIL_ERROR_NONE) {
5143                 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5144                 goto FINISH_OFF;
5145         }
5146
5147         error = emcore_remove_password_in_key_manager(send_password_file_name);
5148         if (error != EMAIL_ERROR_NONE) {
5149                 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5150                 goto FINISH_OFF;
5151         }
5152
5153         ret = true;
5154
5155 FINISH_OFF:
5156         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5157
5158         if (err_code != NULL)
5159                 *err_code = error;
5160
5161         EM_DEBUG_FUNC_END("ret [%d]", ret);
5162         return ret;
5163 }
5164
5165
5166 INTERNAL_FUNC int emstorage_free_account(emstorage_account_tbl_t** account_list, int count, int *err_code)
5167 {
5168         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%d], err_code[%p]", account_list, count, err_code);
5169
5170         int ret = false;
5171         int error = EMAIL_ERROR_NONE;
5172
5173         if (count > 0) {
5174                 if (!account_list || !*account_list) {
5175                         EM_DEBUG_EXCEPTION("account_list[%p], count[%d]", account_list, count);
5176                         error = EMAIL_ERROR_INVALID_PARAM;
5177                         goto FINISH_OFF;
5178                 }
5179
5180                 emstorage_account_tbl_t* p = *account_list;
5181                 int i = 0;
5182
5183                 for (; i < count; i++) {
5184                         EM_SAFE_FREE(p[i].account_name);
5185                         EM_SAFE_FREE(p[i].incoming_server_address);
5186                         EM_SAFE_FREE(p[i].user_email_address);
5187                         EM_SAFE_FREE(p[i].user_data);
5188                         EM_SAFE_FREE(p[i].incoming_server_user_name);
5189                         EM_SAFE_FREE(p[i].incoming_server_password);
5190                         EM_SAFE_FREE(p[i].outgoing_server_address);
5191                         EM_SAFE_FREE(p[i].outgoing_server_user_name);
5192                         EM_SAFE_FREE(p[i].outgoing_server_password);
5193                         EM_SAFE_FREE(p[i].user_display_name);
5194                         EM_SAFE_FREE(p[i].reply_to_address);
5195                         EM_SAFE_FREE(p[i].return_address);
5196                         EM_SAFE_FREE(p[i].logo_icon_path);
5197                         EM_SAFE_FREE(p[i].options.display_name_from);
5198                         EM_SAFE_FREE(p[i].options.signature);
5199                         EM_SAFE_FREE(p[i].options.alert_ringtone_path);
5200                         EM_SAFE_FREE(p[i].certificate_path);
5201                         EM_SAFE_FREE(p[i].user_name);
5202                 }
5203
5204                 EM_SAFE_FREE(p);
5205                 *account_list = NULL;
5206         }
5207
5208         ret = true;
5209
5210 FINISH_OFF:
5211         if (err_code != NULL)
5212                 *err_code = error;
5213
5214         EM_DEBUG_FUNC_END("ret [%d]", ret);
5215         return ret;
5216 }
5217
5218 INTERNAL_FUNC int emstorage_get_mailbox_count(char *multi_user_name, int account_id, int local_yn, int *count, int transaction, int *err_code)
5219 {
5220         EM_DEBUG_FUNC_BEGIN("account_id[%d], local_yn[%d], count[%p], transaction[%d], err_code[%p]", account_id, local_yn, count, transaction, err_code);
5221
5222         if ((account_id < FIRST_ACCOUNT_ID) || (count == NULL)) {
5223                 EM_DEBUG_EXCEPTION(" account_list[%d], local_yn[%d], count[%p]", account_id, local_yn, count);
5224
5225                 if (err_code != NULL)
5226                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5227                 return false;
5228         }
5229
5230         int rc = -1, ret = false;
5231         int error = EMAIL_ERROR_NONE;
5232         char sql_query_string[QUERY_SIZE] = {0, };
5233
5234         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5235         EMSTORAGE_START_READ_TRANSACTION(transaction);
5236
5237         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_box_tbl WHERE account_id = %d AND local_yn = %d", account_id, local_yn);
5238
5239         char **result;
5240
5241         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5242         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
5243                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5244
5245         *count = atoi(result[1]);
5246         sqlite3_free_table(result);
5247
5248
5249         ret = true;
5250
5251 FINISH_OFF:
5252
5253         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5254
5255         if (err_code != NULL)
5256                 *err_code = error;
5257
5258         EM_DEBUG_FUNC_END("ret [%d]", ret);
5259         return ret;
5260 }
5261
5262 INTERNAL_FUNC int emstorage_get_mailbox_list(char *multi_user_name, int account_id, int local_yn, email_mailbox_sort_type_t sort_type, int *select_num, emstorage_mailbox_tbl_t** mailbox_list, int transaction, int *err_code)
5263 {
5264         EM_DEBUG_FUNC_BEGIN("account_id[%d], local_yn[%d], select_num[%p], mailbox_list[%p], transaction[%d], err_code[%p]", account_id, local_yn, select_num, mailbox_list, transaction, err_code);
5265
5266         if (!select_num || !mailbox_list) {
5267                 EM_DEBUG_EXCEPTION("Invalid parameters");
5268
5269                 if (err_code != NULL)
5270                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5271
5272                 return false;
5273         }
5274
5275         int ret = false;
5276         int error = EMAIL_ERROR_NONE;
5277         char conditional_clause_string[QUERY_SIZE] = {0, };
5278         char ordering_clause_string[QUERY_SIZE] = {0, };
5279
5280         if (account_id == ALL_ACCOUNT) {
5281                 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5282                         SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " WHERE local_yn = %d ", local_yn);
5283         } else {
5284                 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d  ", account_id);
5285                 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5286                         SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " AND local_yn = %d ", local_yn);
5287         }
5288
5289         EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5290
5291         switch (sort_type) {
5292         case EMAIL_MAILBOX_SORT_BY_NAME_ASC:
5293                 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name ASC");
5294                 break;
5295
5296         case EMAIL_MAILBOX_SORT_BY_NAME_DSC:
5297                 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name DESC");
5298                 break;
5299
5300         case EMAIL_MAILBOX_SORT_BY_TYPE_ASC:
5301                 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type ASC");
5302                 break;
5303
5304         case EMAIL_MAILBOX_SORT_BY_TYPE_DSC:
5305                 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type DEC");
5306                 break;
5307         }
5308
5309         EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5310
5311         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5312                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5313                 goto FINISH_OFF;
5314         }
5315
5316         ret = true;
5317
5318 FINISH_OFF:
5319
5320         if (err_code != NULL)
5321                 *err_code = error;
5322
5323         EM_DEBUG_FUNC_END("ret [%d]", ret);
5324         return ret;
5325 }
5326
5327 INTERNAL_FUNC int emstorage_get_mailbox_list_ex(char *multi_user_name, int account_id, int local_yn, int with_count, int *select_num, emstorage_mailbox_tbl_t **mailbox_list, int transaction, int *err_code)
5328 {
5329         EM_DEBUG_FUNC_BEGIN("account_id[%d], local_yn[%d], select_num[%p], mailbox_list[%p], transaction[%d], err_code[%p]", account_id, local_yn, select_num, mailbox_list, transaction, err_code);
5330
5331         int ret = false;
5332         int error = EMAIL_ERROR_NONE;
5333         int where_clause_count = 0;
5334         char conditional_clause_string[QUERY_SIZE] = {0, };
5335         char ordering_clause_string[QUERY_SIZE] = {0, };
5336
5337         if (!select_num || !mailbox_list) {
5338                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5339                 error = EMAIL_ERROR_INVALID_PARAM;
5340                 goto FINISH_OFF;
5341         }
5342
5343         if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL) {
5344                 SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), "WHERE local_yn = %d ",  local_yn);
5345                 where_clause_count++;
5346         }
5347
5348         if (account_id > 0) {
5349                 if (where_clause_count == 0) {
5350                         SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " WHERE ");
5351                         SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " account_id = %d ", account_id);
5352                 } else
5353                         SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " AND account_id = %d ", account_id);
5354         }
5355
5356         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY CASE WHEN MBT.mailbox_name"
5357                         " GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\'"
5358                         " THEN 2 ELSE 1 END ASC,"
5359                         " MBT.mailbox_name COLLATE CONVERTUTF8 ASC ");
5360         EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5361         EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5362
5363         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 1, 1, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5364                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
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_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)
5380 {
5381         EM_DEBUG_FUNC_BEGIN("account_id[%d], parent_mailbox_name[%p], select_num[%p], mailbox_list[%p], transaction[%d], err_code[%p]", account_id, parent_mailbox_name, select_num, mailbox_list, transaction, err_code);
5382
5383         int ret = false;
5384         int error = EMAIL_ERROR_NONE;
5385         char conditional_clause_string[QUERY_SIZE] = {0, };
5386
5387         if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list || !parent_mailbox_name) {
5388                 EM_DEBUG_EXCEPTION("account_id[%d], parent_mailbox_name[%p], select_num[%p], mailbox_list[%p]", account_id, parent_mailbox_name, select_num, mailbox_list);
5389                 if (err_code != NULL)
5390                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5391                 return false;
5392         }
5393
5394         sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string, "WHERE account_id = %d  AND (UPPER(mailbox_name) = UPPER('%q') OR UPPER(mailbox_name) LIKE UPPER('%q/%%'))", account_id, parent_mailbox_name, parent_mailbox_name);
5395         EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5396
5397         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, " ORDER BY mailbox_name DESC ", 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5398                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5399                 goto FINISH_OFF;
5400         }
5401
5402         ret = true;
5403
5404 FINISH_OFF:
5405
5406
5407         if (err_code != NULL)
5408                 *err_code = error;
5409
5410         EM_DEBUG_FUNC_END("ret [%d]", ret);
5411         return ret;
5412 }
5413
5414 INTERNAL_FUNC int emstorage_get_mailbox_by_modifiable_yn(char *multi_user_name, int account_id, int local_yn, int *select_num, emstorage_mailbox_tbl_t** mailbox_list, int transaction, int *err_code)
5415 {
5416         EM_DEBUG_FUNC_BEGIN("account_id[%d], local_yn[%d], select_num[%p], mailbox_list[%p], transaction[%d], err_code[%p]", account_id, local_yn, select_num, mailbox_list, transaction, err_code);
5417         if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list) {
5418                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5419
5420                 if (err_code != NULL)
5421                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5422
5423                 return false;
5424         }
5425
5426         int ret = false;
5427         int error = EMAIL_ERROR_NONE;
5428         char conditional_clause_string[QUERY_SIZE] = {0, };
5429
5430         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND modifiable_yn = 0", account_id);
5431         EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
5432
5433         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, " ORDER BY mailbox_name", 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5434                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5435                 goto FINISH_OFF;
5436         }
5437
5438         ret = true;
5439
5440 FINISH_OFF:
5441
5442         if (err_code != NULL)
5443                 *err_code = error;
5444
5445         EM_DEBUG_FUNC_END("ret [%d]", ret);
5446         return ret;
5447 }
5448
5449 INTERNAL_FUNC int emstorage_stamp_last_sync_time_of_mailbox(char *multi_user_name, int input_mailbox_id, int input_transaction)
5450 {
5451         EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_transaction [%d]", input_mailbox_id, input_transaction);
5452
5453         int      result_code = false;
5454         int      error = EMAIL_ERROR_NONE;
5455         time_t   current_time = 0;
5456         char     sql_query_string[QUERY_SIZE] = {0, };
5457         sqlite3 *local_db_handle = NULL;
5458
5459         if (!input_mailbox_id) {
5460                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5461                 return EMAIL_ERROR_INVALID_PARAM;
5462         }
5463
5464         time(&current_time);
5465
5466         local_db_handle = emstorage_get_db_connection(multi_user_name);
5467         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
5468
5469         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5470                         "UPDATE mail_box_tbl SET"
5471                         " last_sync_time = %d"
5472                         " WHERE mailbox_id = %d"
5473                         , (int)current_time
5474                         , input_mailbox_id);
5475
5476         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
5477         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5478         if (error != EMAIL_ERROR_NONE) {
5479                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5480                 goto FINISH_OFF;
5481         }
5482
5483 FINISH_OFF:
5484
5485         if (error == EMAIL_ERROR_NONE)
5486                 result_code = true;
5487
5488         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, result_code, error);
5489
5490         EM_DEBUG_FUNC_END("error [%d]", error);
5491         return error;
5492 }
5493
5494 INTERNAL_FUNC int emstorage_get_mailbox_by_name(char *multi_user_name, int account_id, int local_yn, char *mailbox_name, emstorage_mailbox_tbl_t **result_mailbox, int transaction, int *err_code)
5495 {
5496         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], local_yn[%d], mailbox_name[%s], result_mailbox[%p], transaction[%d], err_code[%p]", account_id, local_yn, mailbox_name, result_mailbox, transaction, err_code);
5497         EM_PROFILE_BEGIN(profile_emstorage_get_mailbox_by_name);
5498
5499         if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !result_mailbox) {
5500                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], mailbox_name[%s], result_mailbox[%p]", account_id, local_yn, mailbox_name, result_mailbox);
5501
5502                 if (err_code != NULL)
5503                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5504                 return false;
5505         }
5506
5507         int ret = false;
5508         int error = EMAIL_ERROR_NONE;
5509         int result_count = 0;
5510         char conditional_clause_string[QUERY_SIZE] = {0, };
5511
5512         if (strcmp(mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
5513                 if (!(*result_mailbox = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t)))) {
5514                         EM_DEBUG_EXCEPTION("malloc failed...");
5515                         error = EMAIL_ERROR_OUT_OF_MEMORY;
5516                         goto FINISH_OFF;
5517                 }
5518
5519                 (*result_mailbox)->mailbox_id                 = 0;
5520                 (*result_mailbox)->account_id                 = account_id;
5521                 (*result_mailbox)->local_yn                   = 1;
5522                 (*result_mailbox)->mailbox_name               = EM_SAFE_STRDUP(mailbox_name);
5523                 (*result_mailbox)->mailbox_type               = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;
5524                 (*result_mailbox)->alias                      = EM_SAFE_STRDUP(mailbox_name);
5525                 (*result_mailbox)->deleted_flag               = 0;
5526                 (*result_mailbox)->modifiable_yn              = 1;
5527                 (*result_mailbox)->total_mail_count_on_server = 1;
5528                 (*result_mailbox)->has_archived_mails         = 0;
5529                 (*result_mailbox)->mail_slot_size             = 0x0FFFFFFF;
5530                 (*result_mailbox)->no_select                  = 0;
5531         } else {
5532                 if (local_yn == -1)
5533                         sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string, "WHERE account_id = %d AND mailbox_name = '%q'", account_id, mailbox_name);
5534                 else
5535                         sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string, "WHERE account_id = %d AND local_yn = %d AND mailbox_name = '%q'", account_id, local_yn, mailbox_name);
5536
5537                 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5538
5539                 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5540                         EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5541                         goto FINISH_OFF;
5542                 }
5543         }
5544
5545         ret = true;
5546
5547 FINISH_OFF:
5548
5549         if (err_code != NULL)
5550                 *err_code = error;
5551
5552         EM_PROFILE_END(profile_emstorage_get_mailbox_by_name);
5553         EM_DEBUG_FUNC_END("ret [%d]", ret);
5554         return ret;
5555 }
5556
5557 INTERNAL_FUNC int emstorage_get_mailbox_by_mailbox_type(char *multi_user_name, int account_id, email_mailbox_type_e mailbox_type, emstorage_mailbox_tbl_t **output_mailbox, int transaction, int *err_code)
5558 {
5559         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_type[%d], output_mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox_type, output_mailbox, transaction, err_code);
5560
5561         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_USER_DEFINED) || !output_mailbox) {
5562                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_type[%d], output_mailbox[%p]", account_id, mailbox_type, output_mailbox);
5563                 if (err_code != NULL)
5564                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5565                 return false;
5566         }
5567
5568         int ret = false;
5569         int error = EMAIL_ERROR_NONE;
5570         int result_count = 0;
5571         emstorage_mailbox_tbl_t *result_mailbox = NULL;
5572         char conditional_clause_string[QUERY_SIZE] = {0,};
5573
5574
5575         SNPRINTF(conditional_clause_string, QUERY_SIZE, "WHERE account_id = %d AND mailbox_type = %d ", account_id, mailbox_type);
5576
5577         EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5578
5579         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", true, false, &result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5580                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl error [%d]", error);
5581                 goto FINISH_OFF;
5582         }
5583
5584         ret = true;
5585
5586 FINISH_OFF:
5587         if (ret == true)
5588                 *output_mailbox = result_mailbox;
5589
5590         if (err_code != NULL)
5591                 *err_code = error;
5592
5593         EM_DEBUG_FUNC_END("ret [%d]", ret);
5594         return ret;
5595 }
5596
5597 INTERNAL_FUNC int emstorage_get_mailbox_by_id(char *multi_user_name, int input_mailbox_id, emstorage_mailbox_tbl_t** output_mailbox)
5598 {
5599         EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], output_mailbox[%p]", input_mailbox_id, output_mailbox);
5600
5601         if (input_mailbox_id <= 0 || !output_mailbox) {
5602                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: input_mailbox_id[%d] output_mailbox[%p]", input_mailbox_id, output_mailbox);
5603                 return  EMAIL_ERROR_INVALID_PARAM;
5604         }
5605
5606         int  ret = EMAIL_ERROR_NONE;
5607         int  result_count = 0;
5608         char conditional_clause_string[QUERY_SIZE] = {0, };
5609
5610         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE MBT.mailbox_id = %d", input_mailbox_id);
5611
5612         EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5613
5614         if ((ret = emstorage_query_mailbox_tbl(multi_user_name,
5615                                         conditional_clause_string,
5616                                         "", true, false, output_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5617                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", ret);
5618                 goto FINISH_OFF;
5619         }
5620
5621 FINISH_OFF:
5622
5623         EM_DEBUG_FUNC_END("ret [%d]", ret);
5624         return ret;
5625 }
5626
5627 INTERNAL_FUNC int emstorage_get_mailbox_by_keyword(char *multi_user_name, int account_id, char *keyword, emstorage_mailbox_tbl_t** result_mailbox, int * result_count, int transaction, int *err_code)
5628 {
5629         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], keyword[%s], result_mailbox[%p], transaction[%d], err_code[%p]", account_id, keyword, result_mailbox, transaction, err_code);
5630
5631         if (account_id < 0 || !keyword || !result_mailbox) {
5632                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], keyword[%s], result_mailbox[%p]", account_id, keyword, result_mailbox);
5633
5634                 if (err_code != NULL)
5635                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5636                 return false;
5637         }
5638
5639         int ret = false;
5640         int error = EMAIL_ERROR_NONE;
5641         char conditional_clause_string[QUERY_SIZE] = {0, };
5642
5643         if (account_id == 0)
5644                 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5645                                 "WHERE alias LIKE \'%%%q%%\'", keyword);
5646         else if (account_id > 0)
5647                 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5648                                 "WHERE account_id = %d AND alias LIKE \'%%%q%%\'", account_id, keyword);
5649
5650         EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5651
5652         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, result_count)) != EMAIL_ERROR_NONE) {
5653                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5654                 goto FINISH_OFF;
5655         }
5656
5657         ret = true;
5658
5659 FINISH_OFF:
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_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)
5669 {
5670         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_type[%d], mailbox_id[%p], transaction[%d], err_code[%p]", account_id, mailbox_type, mailbox_id, transaction, err_code);
5671         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_id) {
5672                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_id[%p]", account_id, mailbox_type, mailbox_id);
5673                 if (err_code != NULL)
5674                         *err_code = EMAIL_ERROR_INVALID_PARAM;
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_id  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_int(hStmt, mailbox_id, 0);
5718
5719         ret = true;
5720
5721 FINISH_OFF:
5722         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5723         if (hStmt != NULL) {
5724                 rc = sqlite3_finalize(hStmt);
5725                 if (rc != SQLITE_OK) {
5726                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5727                         error = EMAIL_ERROR_DB_FAILURE;
5728                 }
5729         }
5730
5731         if (err_code != NULL)
5732                 *err_code = error;
5733
5734         EM_DEBUG_FUNC_END("ret [%d]", ret);
5735         return ret;
5736 }
5737
5738 INTERNAL_FUNC int emstorage_get_mailbox_name_by_mailbox_type(char *multi_user_name, int account_id, email_mailbox_type_e mailbox_type, char **mailbox_name, int transaction, int *err_code)
5739 {
5740         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_type[%d], mailbox_name[%p], transaction[%d], err_code[%p]", account_id, mailbox_type, mailbox_name, transaction, err_code);
5741         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_name) {
5742                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
5743                 if (err_code != NULL)
5744                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5745                 return false;
5746         }
5747
5748         int rc, ret = false;
5749         int error = EMAIL_ERROR_NONE;
5750         emstorage_account_tbl_t* account = NULL;
5751         DB_STMT hStmt = NULL;
5752         char sql_query_string[QUERY_SIZE] = {0, };
5753
5754         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5755         EMSTORAGE_START_READ_TRANSACTION(transaction);
5756
5757         /*  Check whether the account exists. */
5758         if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
5759                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5760                 goto FINISH_OFF;
5761         }
5762
5763         if (account)
5764                 emstorage_free_account(&account, 1, NULL);
5765
5766         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mailbox_name  FROM mail_box_tbl WHERE account_id = %d AND mailbox_type = %d ", account_id, mailbox_type);
5767
5768         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5769
5770
5771         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5772
5773         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5774                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5775
5776
5777         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5778         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5779                         ("sqlite3_step fail:%d", rc));
5780
5781         if (rc == SQLITE_DONE) {
5782                 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5783                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5784                 goto FINISH_OFF;
5785         }
5786
5787         _get_stmt_field_data_string(hStmt, mailbox_name, 0, 0);
5788
5789         ret = true;
5790
5791 FINISH_OFF:
5792         if (hStmt != NULL) {
5793                 rc = sqlite3_finalize(hStmt);
5794                 if (rc != SQLITE_OK) {
5795                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5796                         error = EMAIL_ERROR_DB_FAILURE;
5797                 }
5798         }
5799
5800         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5801
5802         if (err_code != NULL)
5803                 *err_code = error;
5804
5805         EM_DEBUG_FUNC_END("ret [%d]", ret);
5806         return ret;
5807 }
5808
5809 INTERNAL_FUNC int emstorage_update_mailbox_modifiable_yn(char *multi_user_name, int account_id, int local_yn, char *mailbox_name, int modifiable_yn, int transaction, int *err_code)
5810 {
5811         EM_DEBUG_FUNC_BEGIN("account_id [%d], local_yn [%d], mailbox_name [%p], modifiable_yn [%d], transaction [%d], err_code [%p]", account_id, local_yn, mailbox_name, modifiable_yn, transaction, err_code);
5812         int ret = false;
5813         int error = EMAIL_ERROR_NONE;
5814         char sql_query_string[QUERY_SIZE] = {0, };
5815         char *replaced_mailbox_name = NULL;
5816
5817         if (mailbox_name) {
5818                 if (strstr(mailbox_name, "'"))
5819                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5820                 else
5821                         replaced_mailbox_name = strdup(mailbox_name);
5822         }
5823
5824         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5825
5826         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5827
5828         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5829                         "UPDATE mail_box_tbl SET"
5830                         " modifiable_yn = %d"
5831                         " WHERE account_id = %d"
5832                         " AND local_yn = %d"
5833                         " AND mailbox_name = '%s'"
5834                         , modifiable_yn
5835                         , account_id
5836                         , local_yn
5837                         , replaced_mailbox_name);
5838
5839         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5840         if (error != EMAIL_ERROR_NONE) {
5841                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5842                 goto FINISH_OFF;
5843         }
5844
5845         ret = true;
5846
5847 FINISH_OFF:
5848         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5849
5850         EM_SAFE_FREE(replaced_mailbox_name);
5851
5852         if (err_code != NULL)
5853                 *err_code = error;
5854
5855         EM_DEBUG_FUNC_END("ret [%d]", ret);
5856         return ret;
5857
5858 }
5859
5860 INTERNAL_FUNC int emstorage_update_mailbox_total_count(char *multi_user_name,
5861                 int account_id,
5862                 int input_mailbox_id,
5863                 int total_count_on_server,
5864                 int transaction,
5865                 int *err_code)
5866 {
5867         EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], total_count_on_server[%d], "
5868                         "transaction[%d], err_code[%p]",
5869                         account_id, input_mailbox_id, total_count_on_server,  transaction, err_code);
5870
5871         int ret = false;
5872         int error = EMAIL_ERROR_NONE;
5873         char sql_query_string[QUERY_SIZE] = {0, };
5874
5875         if (account_id <= 0 || input_mailbox_id <= 0) {
5876                 EM_DEBUG_EXCEPTION("account_id[%d], input_mailbox_id[%d]", account_id, input_mailbox_id);
5877                 if (err_code != NULL)
5878                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5879                 EM_DEBUG_FUNC_END("ret [%d]", ret);
5880                 return false;
5881         }
5882
5883         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5884
5885         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5886
5887         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5888                         "UPDATE mail_box_tbl SET"
5889                         " total_mail_count_on_server = %d"
5890                         " WHERE account_id = %d"
5891                         " AND mailbox_id = %d"
5892                         , total_count_on_server
5893                         , account_id
5894                         , input_mailbox_id);
5895
5896         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
5897         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5898         if (error != EMAIL_ERROR_NONE) {
5899                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5900                 goto FINISH_OFF;
5901         }
5902
5903         ret = true;
5904
5905 FINISH_OFF:
5906
5907         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5908
5909         if (err_code != NULL)
5910                 *err_code = error;
5911
5912         EM_DEBUG_FUNC_END("ret [%d]", ret);
5913         return ret;
5914
5915 }
5916
5917 INTERNAL_FUNC int emstorage_update_mailbox(char *multi_user_name, int account_id, int local_yn, int input_mailbox_id, emstorage_mailbox_tbl_t *result_mailbox, int transaction, int *err_code)
5918 {
5919         EM_DEBUG_FUNC_BEGIN("account_id[%d], local_yn[%d], input_mailbox_id[%d], result_mailbox[%p], transaction[%d], err_code[%p]", account_id, local_yn, input_mailbox_id, result_mailbox, transaction, err_code);
5920
5921         if (account_id < FIRST_ACCOUNT_ID || input_mailbox_id <= 0 || !result_mailbox) {
5922                 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], input_mailbox_id[%d], result_mailbox[%p]", account_id, local_yn, input_mailbox_id, result_mailbox);
5923
5924                 if (err_code != NULL)
5925                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5926                 EM_DEBUG_FUNC_END("ret [%d]", EMAIL_ERROR_INVALID_PARAM);
5927                 return false;
5928         }
5929
5930         int rc, ret = false;
5931         int error = EMAIL_ERROR_NONE;
5932         char sql_query_string[QUERY_SIZE] = {0, };
5933         DB_STMT hStmt = NULL;
5934         int i = 0;
5935
5936         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5937         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5938
5939         if (local_yn != -1) {
5940                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5941                                 "UPDATE mail_box_tbl SET"
5942                                 "  mailbox_id = ?"
5943                                 ", mailbox_name = ?"
5944                                 ", mailbox_type = ?"
5945                                 ", alias = ?"
5946                                 ", deleted_flag = ?"
5947                                 ", modifiable_yn= ?"
5948                                 ", mail_slot_size= ?"
5949                                 ", total_mail_count_on_server = ?"
5950                                 " WHERE account_id = %d"
5951                                 " AND local_yn = %d"
5952                                 " AND mailbox_id = '%d'"
5953                                 , account_id
5954                                 , local_yn
5955                                 , input_mailbox_id);
5956         } else {
5957                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5958                                 "UPDATE mail_box_tbl SET"
5959                                 "  mailbox_id = ?"
5960                                 ", mailbox_name = ?"
5961                                 ", mailbox_type = ?"
5962                                 ", alias = ?"
5963                                 ", deleted_flag = ?"
5964                                 ", modifiable_yn= ?"
5965                                 ", mail_slot_size= ?"
5966                                 ", total_mail_count_on_server = ?"
5967                                 " WHERE account_id = %d"
5968                                 " AND mailbox_id = '%d'"
5969                                 , account_id
5970                                 , input_mailbox_id);
5971         }
5972
5973
5974
5975         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5976         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5977                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5978
5979         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_id);
5980         _bind_stmt_field_data_string(hStmt, i++, (char *)result_mailbox->mailbox_name ? result_mailbox->mailbox_name : "", 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
5981         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_type);
5982         _bind_stmt_field_data_string(hStmt, i++, (char *)result_mailbox->alias ? result_mailbox->alias : "", 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
5983         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->deleted_flag);
5984         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->modifiable_yn);
5985         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mail_slot_size);
5986         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->total_mail_count_on_server);
5987
5988
5989         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5990         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
5991                         ("sqlite3_step fail:%d", rc));
5992         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5993                         ("sqlite3_step fail:%d", rc));
5994
5995         ret = true;
5996
5997 FINISH_OFF:
5998         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5999
6000         if (hStmt != NULL) {
6001                 rc = sqlite3_finalize(hStmt);
6002                 if (rc != SQLITE_OK) {
6003                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6004                         error = EMAIL_ERROR_DB_FAILURE;
6005                 }
6006         }
6007
6008         if (err_code != NULL)
6009                 *err_code = error;
6010
6011         EM_DEBUG_FUNC_END("ret [%d]", ret);
6012         return ret;
6013 }
6014
6015 INTERNAL_FUNC int emstorage_update_mailbox_type(char *multi_user_name, int account_id, int local_yn, int input_mailbox_id, email_mailbox_type_e new_mailbox_type, int transaction, int *err_code)
6016 {
6017         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], local_yn[%d], input_mailbox_id[%d], new_mailbox_type[%d], transaction[%d], err_code[%p]", account_id, local_yn, input_mailbox_id, new_mailbox_type, transaction, err_code);
6018
6019         if (account_id < FIRST_ACCOUNT_ID) {
6020                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
6021
6022                 if (err_code != NULL)
6023                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6024                 return false;
6025         }
6026
6027         int rc, ret = false;
6028         int error = EMAIL_ERROR_NONE;
6029         char sql_query_string[QUERY_SIZE] = {0, };
6030
6031         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6032         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6033
6034         EM_DEBUG_LOG("emstorage_update_mailbox_type");
6035
6036         DB_STMT hStmt_box_tbl = NULL;
6037         DB_STMT hStmt_mail_tbl = NULL;
6038         int i = 0;
6039
6040         /*  Update mail_box_tbl */
6041         if (local_yn != -1) {
6042                 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
6043                                 "UPDATE mail_box_tbl SET"
6044                                 " mailbox_type = ?"
6045                                 " WHERE account_id = %d"
6046                                 " AND local_yn = %d"
6047                                 " AND mailbox_id = '%d'"
6048                                 , account_id
6049                                 , local_yn
6050                                 , input_mailbox_id);
6051         } else {
6052                 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
6053                                 "UPDATE mail_box_tbl SET"
6054                                 " mailbox_type = ?"
6055                                 " WHERE account_id = %d"
6056                                 " AND mailbox_id = '%d'"
6057                                 , account_id
6058                                 , input_mailbox_id);
6059         }
6060
6061         EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
6062
6063         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_box_tbl, NULL), rc);
6064
6065         if (SQLITE_OK != rc) {
6066                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6067                 error = EMAIL_ERROR_DB_FAILURE;
6068                 goto FINISH_OFF;
6069         }
6070
6071         _bind_stmt_field_data_int(hStmt_box_tbl, i++, new_mailbox_type);
6072
6073
6074         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_box_tbl), rc);
6075
6076         if (rc == SQLITE_FULL) {
6077                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6078                 error   = EMAIL_ERROR_MAIL_MEMORY_FULL;
6079                 goto FINISH_OFF;
6080         }
6081
6082         if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
6083                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6084                 error = EMAIL_ERROR_DB_FAILURE;
6085                 goto FINISH_OFF;
6086         }
6087
6088
6089         /*  Update mail_tbl */
6090         i = 0;
6091         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6092                         "UPDATE mail_tbl SET"
6093                         " mailbox_type = ?"
6094                         " WHERE account_id = %d"
6095                         " AND mailbox_id = '%d'"
6096                         , account_id
6097                         , input_mailbox_id);
6098
6099         EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
6100
6101         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_mail_tbl, NULL), rc);
6102         if (SQLITE_OK != rc) {
6103                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6104                 error = EMAIL_ERROR_DB_FAILURE;
6105                 goto FINISH_OFF;
6106         }
6107
6108         _bind_stmt_field_data_int(hStmt_mail_tbl, i++, new_mailbox_type);
6109
6110         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_mail_tbl), rc);
6111         if (rc == SQLITE_FULL) {
6112                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6113                 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
6114                 goto FINISH_OFF;
6115         }
6116
6117         if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
6118                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6119                 error = EMAIL_ERROR_DB_FAILURE;
6120                 goto FINISH_OFF;
6121         }
6122
6123         ret = true;
6124
6125 FINISH_OFF:
6126         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6127
6128         if (hStmt_box_tbl != NULL) {
6129                 rc = sqlite3_finalize(hStmt_box_tbl);
6130                 if (rc != SQLITE_OK) {
6131                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6132                         error = EMAIL_ERROR_DB_FAILURE;
6133                 }
6134         }
6135
6136         if (hStmt_mail_tbl != NULL) {
6137                 rc = sqlite3_finalize(hStmt_mail_tbl);
6138                 if (rc != SQLITE_OK) {
6139                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6140                         error = EMAIL_ERROR_DB_FAILURE;
6141                 }
6142         }
6143
6144         if (err_code != NULL)
6145                 *err_code = error;
6146
6147         EM_DEBUG_FUNC_END("ret [%d]", ret);
6148         return ret;
6149 }
6150
6151 INTERNAL_FUNC int emstorage_set_local_mailbox(char *multi_user_name, int input_mailbox_id, int input_is_local_mailbox, int transaction)
6152 {
6153         EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], new_mailbox_type[%d], transaction[%d], err_code[%p]", input_mailbox_id, input_is_local_mailbox, transaction);
6154
6155         int rc, ret = false;
6156         int error = EMAIL_ERROR_NONE;
6157         char sql_query_string[QUERY_SIZE] = {0, };
6158
6159         if (input_mailbox_id < 0) {
6160                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6161                 return EMAIL_ERROR_INVALID_PARAM;
6162         }
6163
6164         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6165
6166         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6167
6168         EM_DEBUG_LOG("emstorage_update_mailbox_type");
6169
6170         DB_STMT hStmt = NULL;
6171         int i = 0;
6172
6173         /*  Update mail_box_tbl */
6174         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6175                         "UPDATE mail_box_tbl SET"
6176                         " local_yn = ?"
6177                         " WHERE mailbox_id = %d"
6178                         , input_mailbox_id);
6179
6180         EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
6181
6182         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6183         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6184                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6185
6186         _bind_stmt_field_data_int(hStmt, i++, input_is_local_mailbox);
6187
6188         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6189         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
6190                         ("sqlite3_step fail:%d", rc));
6191         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6192                         ("sqlite3_step fail:%d", rc));
6193         /*
6194            if (hStmt != NULL) {
6195            rc = sqlite3_finalize(hStmt);
6196            if (rc != SQLITE_OK) {
6197            EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6198            error = EMAIL_ERROR_DB_FAILURE;
6199            }
6200            hStmt = NULL;
6201            }
6202            */
6203         ret = true;
6204
6205 FINISH_OFF:
6206         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6207
6208         if (hStmt != NULL) {
6209                 rc = sqlite3_finalize(hStmt);
6210                 if (rc != SQLITE_OK) {
6211                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6212                         error = EMAIL_ERROR_DB_FAILURE;
6213                 }
6214         }
6215
6216         EM_DEBUG_FUNC_END("error [%d]", error);
6217         return error;
6218 }
6219
6220 INTERNAL_FUNC int emstorage_set_field_of_mailbox_with_integer_value(char *multi_user_name, int input_account_id, int *input_mailbox_id_array, int input_mailbox_id_count, char *input_field_name, int input_value, int transaction)
6221 {
6222         EM_DEBUG_FUNC_BEGIN("input_account_id [%d] input_mailbox_id_array[%p] input_mailbox_id_count[%d] input_field_name[%p] input_value[%d] err_code[%p]", input_account_id, input_mailbox_id_array, input_mailbox_id_count, input_field_name, input_value, transaction);
6223         int i = 0;
6224         int err = EMAIL_ERROR_NONE;
6225         int result = false;
6226         int cur_mailbox_id_string = 0;
6227         int mailbox_id_string_buffer_length = 0;
6228         char  sql_query_string[QUERY_SIZE] = {0, };
6229         char *mailbox_id_string_buffer = NULL;
6230         char *parameter_string = NULL;
6231         sqlite3 *local_db_handle = NULL;
6232
6233         if (input_mailbox_id_array == NULL || input_mailbox_id_count == 0 || input_field_name == NULL) {
6234                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6235                 return EMAIL_ERROR_INVALID_PARAM;
6236         }
6237
6238         local_db_handle = emstorage_get_db_connection(multi_user_name);
6239
6240         /* Generating mail id list string */
6241         mailbox_id_string_buffer_length = MAILBOX_ID_STRING_LENGTH * input_mailbox_id_count;
6242
6243         mailbox_id_string_buffer = em_malloc(mailbox_id_string_buffer_length);
6244
6245         if (!mailbox_id_string_buffer) {
6246                 EM_DEBUG_EXCEPTION("em_mallocfailed");
6247                 err = EMAIL_ERROR_OUT_OF_MEMORY;
6248                 goto FINISH_OFF;
6249         }
6250
6251         for (i = 0; i < input_mailbox_id_count; i++)
6252                 cur_mailbox_id_string += SNPRINTF_OFFSET(mailbox_id_string_buffer, cur_mailbox_id_string, mailbox_id_string_buffer_length, "%d,", input_mailbox_id_array[i]);
6253
6254         if (EM_SAFE_STRLEN(mailbox_id_string_buffer) > 1)
6255                 mailbox_id_string_buffer[EM_SAFE_STRLEN(mailbox_id_string_buffer) - 1] = NULL_CHAR;
6256
6257         /* Generating notification parameter string */
6258         parameter_string = em_malloc(mailbox_id_string_buffer_length + EM_SAFE_STRLEN(input_field_name) + 2);
6259
6260         if (!parameter_string) {
6261                 EM_DEBUG_EXCEPTION("em_mallocfailed");
6262                 err = EMAIL_ERROR_OUT_OF_MEMORY;
6263                 goto FINISH_OFF;
6264         }
6265
6266         SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", input_field_name, 0x01, mailbox_id_string_buffer);
6267
6268         /* Write query string */
6269         SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_box_tbl SET %s = %d WHERE mailbox_id in (%s) ", input_field_name, input_value, mailbox_id_string_buffer);
6270
6271         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
6272
6273         /* Execute query */
6274         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
6275         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6276         if (err != EMAIL_ERROR_NONE) {
6277                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
6278                 goto FINISH_OFF;
6279         }
6280
6281         if (sqlite3_changes(local_db_handle) == 0)
6282                 EM_DEBUG_LOG("no mail matched...");
6283
6284         result = true;
6285
6286 FINISH_OFF:
6287         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, err);
6288
6289         if (err == EMAIL_ERROR_NONE && parameter_string) {
6290                 if (!emcore_notify_storage_event(NOTI_MAILBOX_FIELD_UPDATE, input_account_id, 0, parameter_string, input_value))
6291                         EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_MAILBOX_FIELD_UPDATE [%s,%d]",
6292                                         input_field_name, input_value);
6293         }
6294
6295         EM_SAFE_FREE(mailbox_id_string_buffer);
6296         EM_SAFE_FREE(parameter_string);
6297
6298         EM_DEBUG_FUNC_END("err [%d]", err);
6299         return err;
6300 }
6301
6302 INTERNAL_FUNC int emstorage_add_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox_tbl, int transaction, int *err_code)
6303 {
6304         EM_DEBUG_FUNC_BEGIN("mailbox_tbl[%p], transaction[%d], err_code[%p]", mailbox_tbl, transaction, err_code);
6305
6306         if (!mailbox_tbl) {
6307                 if (err_code != NULL)
6308                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6309                 return false;
6310         }
6311
6312         int rc, ret = false;
6313         int error = EMAIL_ERROR_NONE;
6314         DB_STMT hStmt = NULL;
6315         char sql_query_string[QUERY_SIZE] = {0,};
6316         char **result = NULL;
6317         time_t current_time;
6318         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6319         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6320
6321         EM_SAFE_STRNCPY(sql_query_string, "SELECT max(rowid) FROM mail_box_tbl;", sizeof(sql_query_string));
6322
6323         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6324         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6325                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6326
6327         time(&current_time);
6328
6329         if (NULL == result[1])
6330                 rc = 1;
6331         else
6332                 rc = atoi(result[1]) + 1;
6333         sqlite3_free_table(result);
6334
6335         memset(sql_query_string, 0, sizeof(char) * QUERY_SIZE);
6336
6337         mailbox_tbl->mailbox_id = rc;
6338
6339         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6340                         "INSERT INTO mail_box_tbl VALUES "
6341                         "(?"    /* mailbox_id */
6342                         ", ?"    /* account_id */
6343                         ", ?"    /* local_yn */
6344                         ", ?"    /* mailbox_name */
6345                         ", ?"    /* mailbox_type */
6346                         ", ?"    /* alias */
6347                         ", ?"    /* deleted_flag */
6348                         ", ?"    /* modifiable_yn */
6349                         ", ?"    /* total_mail_count_on_server */
6350                         ", ?"    /* has_archived_mails */
6351                         ", ?"    /* mail_slot_size */
6352                         ", ?"    /* no_select */
6353                         ", ?"    /* last_sync_time */
6354                         ", ?"    /* eas_data_length */
6355                         ", ?"    /* eas_data */
6356                         ")");
6357
6358
6359         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6360         EM_DEBUG_LOG_DEV("After sqlite3_prepare hStmt = %p", hStmt);
6361         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6362                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6363
6364         int col_index = 0;
6365
6366         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_id);
6367         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->account_id);
6368         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->local_yn);
6369         _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
6370         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_type);
6371         _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->alias, 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
6372         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->deleted_flag);
6373         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->modifiable_yn);
6374         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->total_mail_count_on_server);
6375         _bind_stmt_field_data_int(hStmt, col_index++, 0);
6376         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mail_slot_size);
6377         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->no_select);
6378         _bind_stmt_field_data_int(hStmt, col_index++, current_time);
6379         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->eas_data_length);
6380         _bind_stmt_field_data_blob(hStmt, col_index++, (void*)mailbox_tbl->eas_data, mailbox_tbl->eas_data_length);
6381
6382
6383         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6384         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6385                         ("sqlite3_step fail:%dn", rc));
6386
6387         ret = true;
6388
6389 FINISH_OFF:
6390         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6391         if (hStmt != NULL) {
6392                 rc = sqlite3_finalize(hStmt);
6393                 if (rc != SQLITE_OK) {
6394                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6395                         error = EMAIL_ERROR_DB_FAILURE;
6396                 }
6397         }
6398
6399         if (error == EMAIL_ERROR_NONE) {
6400                 if (!emcore_notify_storage_event(NOTI_MAILBOX_ADD, mailbox_tbl->account_id, mailbox_tbl->mailbox_id,
6401                                         mailbox_tbl->mailbox_name, mailbox_tbl->mailbox_type))
6402                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6403         }
6404
6405         if (err_code != NULL)
6406                 *err_code = error;
6407
6408         EM_DEBUG_FUNC_END("ret [%d]", ret);
6409         return ret;
6410 }
6411
6412 INTERNAL_FUNC int emstorage_set_all_mailbox_modifiable_yn(char *multi_user_name, int account_id, int modifiable_yn, int transaction, int *err_code)
6413 {
6414         EM_DEBUG_FUNC_BEGIN("account_id[%d], modifiable_yn[%d], err_code[%p]", account_id, modifiable_yn, err_code);
6415
6416         if (account_id < FIRST_ACCOUNT_ID) {
6417
6418                 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
6419
6420                 if (err_code != NULL)
6421                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6422                 return false;
6423         }
6424         int rc, ret = false;
6425         int error = EMAIL_ERROR_NONE;
6426         char sql_query_string[QUERY_SIZE] = {0,};
6427         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6428         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6429
6430
6431         SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET modifiable_yn = %d WHERE account_id = %d", modifiable_yn, account_id);
6432
6433         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6434         if (error != EMAIL_ERROR_NONE) {
6435                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6436                 goto FINISH_OFF;
6437         }
6438
6439         rc = sqlite3_changes(local_db_handle);
6440         if (rc == 0)
6441                 EM_DEBUG_EXCEPTION("All mailbox_name modifiable_yn set to 0 already");
6442
6443
6444         ret = true;
6445
6446 FINISH_OFF:
6447         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6448
6449         if (err_code != NULL)
6450                 *err_code = error;
6451         EM_DEBUG_FUNC_END("ret [%d]", ret);
6452         return ret;
6453
6454
6455 }
6456
6457 INTERNAL_FUNC int emstorage_delete_mailbox(char *multi_user_name, int account_id, int local_yn, int input_mailbox_id, int transaction, int *err_code)
6458 {
6459         EM_DEBUG_FUNC_BEGIN("account_id[%d], local_yn[%d], input_mailbox_id[%d], transaction[%d], err_code[%p]", account_id, local_yn, input_mailbox_id, transaction, err_code);
6460
6461         if (account_id < FIRST_ACCOUNT_ID) {
6462                 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
6463
6464                 if (err_code != NULL)
6465                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6466                 return false;
6467         }
6468
6469         int rc, ret = false;
6470         int error = EMAIL_ERROR_NONE;
6471         char sql_query_string[QUERY_SIZE] = {0, };
6472         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6473         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6474
6475         if (local_yn == -1)
6476                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_box_tbl WHERE account_id = %d ", account_id);
6477         else
6478                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_box_tbl WHERE account_id = %d AND local_yn = %d ", account_id, local_yn);
6479
6480         if (input_mailbox_id > 0) {             /* 0 means all mailbox */
6481                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(1 + EM_SAFE_STRLEN(sql_query_string)), "AND mailbox_id = %d", input_mailbox_id);
6482         }
6483
6484         EM_DEBUG_LOG_SEC("mailbox sql_query_string [%s]", sql_query_string);
6485         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6486         if (error != EMAIL_ERROR_NONE) {
6487                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6488                 goto FINISH_OFF;
6489         }
6490
6491         rc = sqlite3_changes(local_db_handle);
6492         if (rc == 0) {
6493                 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
6494                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
6495                 ret = true;
6496         }
6497         ret = true;
6498
6499 FINISH_OFF:
6500
6501         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6502
6503         if (error == EMAIL_ERROR_NONE) {
6504                 if (!emcore_notify_storage_event(NOTI_MAILBOX_DELETE, account_id, input_mailbox_id, NULL, 0))
6505                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6506         }
6507
6508         if (err_code != NULL)
6509                 *err_code = error;
6510
6511         EM_DEBUG_FUNC_END("ret [%d]", ret);
6512         return ret;
6513 }
6514
6515 INTERNAL_FUNC int emstorage_free_mailbox(emstorage_mailbox_tbl_t** mailbox_list, int count, int *err_code)
6516 {
6517         EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d], err_code[%p]", mailbox_list, count, err_code);
6518
6519         int ret = false;
6520         int error = EMAIL_ERROR_NONE;
6521
6522         if (count > 0) {
6523                 if (!mailbox_list || !*mailbox_list) {
6524                         EM_DEBUG_EXCEPTION(" mailbox_list[%p], count[%d]", mailbox_list, count);
6525
6526                         error = EMAIL_ERROR_INVALID_PARAM;
6527                         goto FINISH_OFF;
6528                 }
6529
6530                 emstorage_mailbox_tbl_t* p = *mailbox_list;
6531                 int i = 0;
6532
6533                 for (; i < count; i++) {
6534                         EM_SAFE_FREE(p[i].mailbox_name);
6535                         EM_SAFE_FREE(p[i].alias);
6536                         EM_SAFE_FREE(p[i].eas_data); /*valgrind*/
6537                 }
6538
6539                 EM_SAFE_FREE(p); *mailbox_list = NULL;
6540         }
6541
6542         ret = true;
6543
6544 FINISH_OFF:
6545         if (err_code != NULL)
6546                 *err_code = error;
6547
6548         EM_DEBUG_FUNC_END("ret [%d]", ret);
6549         return ret;
6550 }
6551
6552 INTERNAL_FUNC int emstorage_get_count_read_mail_uid(char *multi_user_name, int account_id, char *mailbox_name, int *count, int transaction, int *err_code)
6553 {
6554         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_name[%p], count[%p], transaction[%d], err_code[%p]", account_id, mailbox_name , count,  transaction, err_code);
6555
6556         if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !count) {
6557                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], count[%p], exist[%p]", account_id, mailbox_name, count);
6558
6559                 if (err_code != NULL)
6560                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6561                 return false;
6562         }
6563
6564         int rc = -1, ret = false;
6565         int error = EMAIL_ERROR_NONE;
6566         char sql_query_string[QUERY_SIZE] = {0, };
6567         char *replaced_mailbox_name = NULL;
6568
6569         if (strstr(mailbox_name, "'"))
6570                 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6571         else
6572                 replaced_mailbox_name = EM_SAFE_STRDUP(mailbox_name);
6573
6574         EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6575
6576
6577         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6578         EMSTORAGE_START_READ_TRANSACTION(transaction);
6579         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_read_mail_uid_tbl WHERE account_id = %d AND mailbox_name = '%s'  ", account_id, replaced_mailbox_name);
6580         EM_DEBUG_LOG_SEC(">>> SQL [ %s ] ", sql_query_string);
6581
6582         char **result;
6583
6584         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6585         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6586                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6587
6588         *count = atoi(result[1]);
6589         sqlite3_free_table(result);
6590
6591         ret = true;
6592
6593 FINISH_OFF:
6594         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6595
6596         EM_SAFE_FREE(replaced_mailbox_name);
6597
6598         if (err_code != NULL)
6599                 *err_code = error;
6600
6601         EM_DEBUG_FUNC_END("ret [%d]", ret);
6602         return ret;
6603 }
6604
6605 INTERNAL_FUNC int emstorage_check_read_mail_uid(char *multi_user_name, int account_id, char *mailbox_name, char *uid, int *exist, int transaction, int *err_code)
6606 {
6607         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_name[%p], uid[%p], exist[%p], transaction[%d], err_code[%p]", account_id, mailbox_name , uid, exist, transaction, err_code);
6608
6609         if (account_id < FIRST_ACCOUNT_ID || !uid || !exist) {
6610                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], uid[%p], exist[%p]", account_id, mailbox_name , uid, exist);
6611
6612                 if (err_code != NULL)
6613                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6614                 return false;
6615         }
6616
6617         int rc = -1, ret = false;
6618         int error = EMAIL_ERROR_NONE;
6619         char sql_query_string[QUERY_SIZE] = {0, };
6620         char *replaced_mailbox_name = NULL;
6621
6622         EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6623
6624         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6625         EMSTORAGE_START_READ_TRANSACTION(transaction);
6626
6627         if (mailbox_name) {
6628                 if (strstr(mailbox_name, "'"))
6629                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6630                 else
6631                         replaced_mailbox_name = strdup(mailbox_name);
6632
6633                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_read_mail_uid_tbl WHERE account_id = %d AND mailbox_name = '%s' AND server_uid = '%s' ", account_id, replaced_mailbox_name, uid);
6634         } else {
6635                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_read_mail_uid_tbl WHERE account_id = %d AND server_uid = '%s' ", account_id, uid);
6636         }
6637
6638         char **result = NULL;
6639
6640         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6641         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6642                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6643
6644         *exist = atoi(result[1]);
6645         sqlite3_free_table(result);
6646
6647         if (*exist > 0)
6648                 *exist = 1;
6649         else
6650                 *exist = 0;
6651
6652         ret = true;
6653
6654 FINISH_OFF:
6655         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6656
6657         EM_SAFE_FREE(replaced_mailbox_name);
6658
6659         if (err_code != NULL)
6660                 *err_code = error;
6661
6662         EM_DEBUG_FUNC_END("ret [%d]", ret);
6663         return ret;
6664 }
6665
6666 INTERNAL_FUNC int emstorage_get_downloaded_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code)
6667 {
6668         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], err_code[%p]", mail_id, mail, err_code);
6669
6670         if (!mail || mail_id <= 0) {
6671                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
6672                 if (err_code != NULL)
6673                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6674                 return false;
6675         }
6676
6677         int rc, ret = false;
6678         int error = EMAIL_ERROR_NONE;
6679         DB_STMT hStmt = NULL;
6680         char sql_query_string[QUERY_SIZE] = {0, };
6681
6682         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6683         EMSTORAGE_START_READ_TRANSACTION(transaction);
6684
6685         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE local_uid = %d", mail_id);
6686
6687         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6688         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6689
6690         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6691                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6692
6693
6694         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6695         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6696                         ("sqlite3_step fail:%d", rc));
6697
6698         *mail = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
6699         if (*mail == NULL) {
6700                 error = EMAIL_ERROR_OUT_OF_MEMORY;
6701                 EM_DEBUG_EXCEPTION("Memory allocation for mail failed.");
6702                 goto FINISH_OFF;
6703
6704         }
6705         memset(*mail, 0x00, sizeof(emstorage_mail_tbl_t));
6706
6707         _get_stmt_field_data_int(hStmt, &((*mail)->account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6708         _get_stmt_field_data_int(hStmt, &((*mail)->mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6709         _get_stmt_field_data_string(hStmt, &((*mail)->server_mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6710         _get_stmt_field_data_int(hStmt, &((*mail)->mail_id), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6711         _get_stmt_field_data_string(hStmt, &((*mail)->server_mail_id), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6712         _get_stmt_field_data_int(hStmt, &((*mail)->mail_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6713         _get_stmt_field_data_char(hStmt, &((*mail)->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6714
6715         (*mail)->server_mail_status = 1;
6716
6717         ret = true;
6718
6719 FINISH_OFF:
6720
6721         if (hStmt != NULL) {
6722                 rc = sqlite3_finalize(hStmt);
6723                 if (rc != SQLITE_OK) {
6724                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6725                         error = EMAIL_ERROR_DB_FAILURE;
6726                 }
6727         }
6728
6729         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6730
6731         if (err_code != NULL)
6732                 *err_code = error;
6733
6734         EM_DEBUG_FUNC_END("ret [%d]", ret);
6735         return ret;
6736 }
6737
6738 INTERNAL_FUNC int emstorage_get_downloaded_list(char *multi_user_name, int account_id, int mailbox_id, emstorage_read_mail_uid_tbl_t **read_mail_uid, int *count, int transaction, int *err_code)
6739 {
6740         EM_PROFILE_BEGIN(emStorageGetDownloadList);
6741         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], read_mail_uid[%p], count[%p], transaction[%d], err_code[%p]", account_id, mailbox_id, read_mail_uid, count, transaction, err_code);
6742         if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid || !count) {
6743                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_id[%s], read_mail_uid[%p], count[%p]", account_id, mailbox_id, read_mail_uid, count);
6744
6745                 if (err_code != NULL)
6746                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6747                 return false;
6748         }
6749
6750         int rc, ret = false;
6751         int error = EMAIL_ERROR_NONE;
6752
6753         DB_STMT hStmt = NULL;
6754         char sql_query_string[QUERY_SIZE] = {0, };
6755
6756         emstorage_read_mail_uid_tbl_t* p_data_tbl = NULL;
6757         int i = 0;
6758
6759         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6760         EMSTORAGE_START_READ_TRANSACTION(transaction);
6761
6762         if (mailbox_id)
6763                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE account_id = %d AND mailbox_id = %d", account_id, mailbox_id);
6764         else
6765                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
6766
6767         EM_DEBUG_LOG_SEC(" sql_query_string : %s", sql_query_string);
6768
6769
6770
6771         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6772         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6773         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6774                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6775
6776
6777         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6778         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6779                         ("sqlite3_step fail:%d", rc));
6780
6781         char **result;
6782         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL); */
6783         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL), rc);
6784         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6785                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6786
6787         sqlite3_free_table(result);
6788         if (*count == 0) {
6789                 EM_DEBUG_LOG("No mail found in mail_read_mail_uid_tbl");
6790                 ret = true;
6791                 goto FINISH_OFF;
6792         }
6793
6794
6795         if (!(p_data_tbl = (emstorage_read_mail_uid_tbl_t*)malloc(sizeof(emstorage_read_mail_uid_tbl_t) * *count))) {
6796                 EM_DEBUG_EXCEPTION(" malloc failed...");
6797                 error = EMAIL_ERROR_OUT_OF_MEMORY;
6798                 goto FINISH_OFF;
6799         }
6800
6801         memset(p_data_tbl, 0x00, sizeof(emstorage_read_mail_uid_tbl_t)*(*count));
6802
6803         for (i = 0; i < *count; ++i) {
6804                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6805                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6806                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6807                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].local_uid), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6808                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].server_uid), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6809                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rfc822_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6810                 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6811                 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_flagged_field), FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6812
6813                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6814                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6815                                 ("sqlite3_step fail:%d", rc));
6816         }
6817
6818         ret = true;
6819
6820 FINISH_OFF:
6821         if (ret == true)
6822                 *read_mail_uid = p_data_tbl;
6823         else if (p_data_tbl)
6824                 emstorage_free_read_mail_uid(&p_data_tbl, *count, NULL);
6825
6826         if (hStmt != NULL) {
6827                 rc = sqlite3_finalize(hStmt);
6828                 if (rc != SQLITE_OK) {
6829                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6830                         error = EMAIL_ERROR_DB_FAILURE;
6831                 }
6832         }
6833
6834         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6835
6836         if (err_code != NULL)
6837                 *err_code = error;
6838
6839         EM_PROFILE_END(emStorageGetDownloadList);
6840         EM_DEBUG_FUNC_END("ret [%d]", ret);
6841         return ret;
6842 }
6843
6844 INTERNAL_FUNC int emstorage_get_downloaded_mail_size(char *multi_user_name, int account_id, char *mailbox_id, int local_uid, char *mailbox_name, char *uid, int *mail_size, int transaction, int *err_code)
6845 {
6846         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%p], locacal_uid[%d], mailbox_name[%p], uid[%p], mail_size[%p], transaction[%d], err_code[%p]", account_id, mailbox_id, local_uid, mailbox_name, uid, mail_size, transaction, err_code);
6847
6848         if (account_id < FIRST_ACCOUNT_ID || !mail_size) {
6849                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_id[%p], locacal_uid[%d], mailbox_name[%p], uid[%p], mail_size[%p]", account_id, mailbox_id, local_uid, mailbox_name, uid, mail_size);
6850
6851                 if (err_code != NULL)
6852                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6853                 return false;
6854         }
6855
6856         int rc, ret = false;
6857         int error = EMAIL_ERROR_NONE;
6858         DB_STMT hStmt = NULL;
6859         char sql_query_string[QUERY_SIZE] = {0, };
6860         char *replaced_mailbox_name = NULL;
6861
6862         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6863         EMSTORAGE_START_READ_TRANSACTION(transaction);
6864
6865         if (mailbox_name) {
6866                 if (strstr(mailbox_name, "'"))
6867                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6868                 else
6869                         replaced_mailbox_name = strdup(mailbox_name);
6870
6871                 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6872
6873                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6874                                 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6875                                 "WHERE account_id = %d "
6876                                 "AND mailbox_id = '%s' "
6877                                 "AND local_uid = %d "
6878                                 "AND mailbox_name = '%s' "
6879                                 "AND server_uid = '%s'",
6880                                 account_id, mailbox_id, local_uid, replaced_mailbox_name, uid);
6881         } else {
6882                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6883                                 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6884                                 "WHERE account_id = %d "
6885                                 "AND mailbox_id = '%s' "
6886                                 "AND local_uid = %d "
6887                                 "AND server_uid = '%s'",
6888                                 account_id, mailbox_id, local_uid, uid);
6889         }
6890
6891
6892         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6893         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6894                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6895
6896
6897         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6898         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6899                         ("sqlite3_step fail:%d", rc));
6900
6901         if (rc == SQLITE_DONE) {
6902                 EM_DEBUG_LOG("no matched mail found....");
6903                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
6904                 goto FINISH_OFF;
6905         }
6906
6907         _get_stmt_field_data_int(hStmt, mail_size, 0);
6908
6909         ret = true;
6910
6911 FINISH_OFF:
6912         EM_SAFE_FREE(replaced_mailbox_name);
6913
6914         if (hStmt != NULL) {
6915                 rc = sqlite3_finalize(hStmt);
6916                 if (rc != SQLITE_OK) {
6917                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6918                         error = EMAIL_ERROR_DB_FAILURE;
6919                 }
6920         }
6921
6922         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6923
6924         if (err_code != NULL)
6925                 *err_code = error;
6926
6927         EM_DEBUG_FUNC_END("ret [%d]", ret);
6928         return ret;
6929 }
6930
6931 INTERNAL_FUNC int emstorage_add_downloaded_mail(char *multi_user_name, emstorage_read_mail_uid_tbl_t *read_mail_uid, int transaction, int *err_code)
6932 {
6933         EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], transaction[%d], err_code[%p]", read_mail_uid, transaction, err_code);
6934
6935         if (!read_mail_uid) {
6936                 EM_DEBUG_EXCEPTION("read_mail_uid[%p]", read_mail_uid);
6937                 if (err_code != NULL)
6938                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6939                 return false;
6940         }
6941
6942         int rc, rc2,  ret = false;
6943         int error = EMAIL_ERROR_NONE;
6944         DB_STMT hStmt = NULL;
6945         char sql_query_string[QUERY_SIZE] = {0, };
6946
6947         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6948         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6949
6950         char *sql = "SELECT max(rowid) FROM mail_read_mail_uid_tbl;";
6951         char **result = NULL;
6952
6953
6954         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6955         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6956                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6957
6958         if (NULL == result[1]) rc = 1;
6959         else rc = atoi(result[1])+1;
6960         sqlite3_free_table(result);
6961
6962         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6963                         "INSERT INTO mail_read_mail_uid_tbl VALUES "
6964                         "(?"  /* account_id */
6965                         ", ?"  /* mailbox_id */
6966                         ", ?"  /* mailbox_name */
6967                         ", ?"  /* local_uid */
6968                         ", ?"  /* server_uid */
6969                         ", ?"  /* rfc822_size */
6970                         ", ?"  /* sync_status */
6971                         ", ?"  /* flags_seen_field */
6972                         ", ?"  /* flags_flagged_field */
6973                         ", ?)");
6974
6975
6976         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
6977         if (rc2 != SQLITE_OK) {
6978                 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6979                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6980
6981                 error = EMAIL_ERROR_DB_FAILURE;
6982                 goto FINISH_OFF;
6983         }
6984
6985         EM_DEBUG_LOG("account_id[%d] mailbox_id[%d] local_uid [%d]"
6986                         "server_uid[%s] rfc822_size[%d] rc[%d]",
6987                         read_mail_uid->account_id, read_mail_uid->mailbox_id, read_mail_uid->local_uid,
6988                         read_mail_uid->server_uid, read_mail_uid->rfc822_size, rc);
6989
6990         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->account_id);
6991         _bind_stmt_field_data_int(hStmt, LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->mailbox_id);
6992         _bind_stmt_field_data_int(hStmt, LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->local_uid);
6993         _bind_stmt_field_data_string(hStmt, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL, (char *)read_mail_uid->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6994         _bind_stmt_field_data_string(hStmt, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, (char *)read_mail_uid->server_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6995         _bind_stmt_field_data_int(hStmt, RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->rfc822_size);
6996         _bind_stmt_field_data_int(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_seen_field);
6997         _bind_stmt_field_data_int(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_flagged_field);
6998         _bind_stmt_field_data_int(hStmt, IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, rc);
6999
7000
7001         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7002         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
7003                         ("sqlite3_step fail:%d", rc));
7004         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7005                         ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
7006
7007
7008         ret = true;
7009
7010 FINISH_OFF:
7011         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7012         if (hStmt != NULL) {
7013                 rc = sqlite3_finalize(hStmt);
7014                 if (rc != SQLITE_OK) {
7015                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7016                         error = EMAIL_ERROR_DB_FAILURE;
7017                 }
7018         }
7019
7020         if (err_code != NULL)
7021                 *err_code = error;
7022
7023         EM_DEBUG_FUNC_END("ret [%d]", ret);
7024         return ret;
7025 }
7026
7027 #ifdef __FEATURE_BODY_SEARCH__
7028 INTERNAL_FUNC int emstorage_add_mail_text(char *multi_user_name, emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
7029 {
7030         EM_DEBUG_FUNC_BEGIN("mail_text[%p], transaction[%d], err_code[%p]", mail_text, transaction, err_code);
7031
7032         if (!mail_text) {
7033                 EM_DEBUG_EXCEPTION("mail_text[%p]", mail_text);
7034                 if (err_code != NULL)
7035                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7036                 return false;
7037         }
7038
7039         int rc, rc2,  ret = false;
7040         int error = EMAIL_ERROR_NONE;
7041         DB_STMT hStmt = NULL;
7042         char sql_query_string[QUERY_SIZE] = {0, };
7043
7044         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7045         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7046
7047         char *sql = "SELECT max(rowid) FROM mail_text_tbl;";
7048         char **result = NULL;
7049
7050         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7051         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7052                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7053         sqlite3_free_table(result);
7054
7055         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7056                         "INSERT INTO mail_text_tbl VALUES "
7057                         "(?"
7058                         ", ?"
7059                         ", ?"
7060                         ", ?)");
7061
7062         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
7063         if (rc2 != SQLITE_OK) {
7064                 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7065                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc2, sqlite3_errmsg(local_db_handle));
7066
7067                 error = EMAIL_ERROR_DB_FAILURE;
7068                 goto FINISH_OFF;
7069         }
7070
7071         EM_DEBUG_LOG("mail_id[%d] account_id[%d] mailbox_id[%d]", mail_text->mail_id,
7072                         mail_text->account_id, mail_text->mailbox_id);
7073         EM_DEBUG_LOG_DEV("body_text VALUE [%s] ", mail_text->body_text);
7074
7075         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mail_id);
7076         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->account_id);
7077         _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mailbox_id);
7078         _bind_stmt_field_data_string(hStmt, BODY_TEXT_IDX_IN_MAIL_TEXT_TBL, (char *)mail_text->body_text, 0, -1);
7079
7080         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7081         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
7082                         ("sqlite3_step fail:%d", rc));
7083         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7084                         ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
7085
7086         ret = true;
7087
7088 FINISH_OFF:
7089         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7090         if (hStmt != NULL) {
7091                 rc = sqlite3_finalize(hStmt);
7092                 if (rc != SQLITE_OK) {
7093                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7094                         error = EMAIL_ERROR_DB_FAILURE;
7095                 }
7096         }
7097
7098         if (err_code != NULL)
7099                 *err_code = error;
7100
7101         EM_DEBUG_FUNC_END("ret [%d]", ret);
7102         return ret;
7103 }
7104 #endif
7105
7106 INTERNAL_FUNC int emstorage_change_read_mail_uid(char *multi_user_name, int account_id, int mailbox_id, int local_uid, char *mailbox_name, char *uid, emstorage_read_mail_uid_tbl_t* read_mail_uid, int transaction, int *err_code)
7107 {
7108         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], local_uid[%d], mailbox_name[%p], uid[%p], read_mail_uid[%p], transaction[%d], err_code[%p]", account_id, mailbox_id, local_uid, mailbox_name, uid, read_mail_uid, transaction, err_code);
7109
7110         if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid) {
7111                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_id[%d], local_uid[%d], mailbox_name[%p], uid[%p], read_mail_uid[%p]", account_id, mailbox_id, local_uid, mailbox_name, uid, read_mail_uid);
7112
7113                 if (err_code != NULL)
7114                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7115                 return false;
7116         }
7117
7118         int rc, ret = false;
7119         int error = EMAIL_ERROR_NONE;
7120         DB_STMT hStmt = NULL;
7121         char sql_query_string[QUERY_SIZE] = {0, };
7122
7123         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7124
7125         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7126
7127         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7128                         "UPDATE mail_read_mail_uid_tbl SET"
7129                         "  account_id = ?"
7130                         ", mailbox_id = ?"
7131                         ", mailbox_name = ?"
7132                         ", local_uid  = ?"
7133                         ", server_uid = ?"
7134                         ", rfc822_size = ?"
7135                         ", flags_seen_field  = ?"
7136                         ", flags_flagged_field  = ?"
7137                         " WHERE account_id = ?"
7138                         " AND mailbox_id  = ?"
7139                         " AND local_uid   = ?"
7140                         " AND mailbox_name= ?"
7141                         " AND server_uid = ?");
7142
7143
7144         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7145         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7146         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7147                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7148
7149
7150         int i = 0;
7151
7152         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->account_id);
7153         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->mailbox_id);
7154         _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7155         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->local_uid);
7156         _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->server_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7157         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->rfc822_size);
7158         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_seen_field);
7159         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_flagged_field);
7160         _bind_stmt_field_data_int(hStmt, i++, account_id);
7161         _bind_stmt_field_data_int(hStmt, i++, mailbox_id);
7162         _bind_stmt_field_data_int(hStmt, i++, local_uid);
7163         _bind_stmt_field_data_string(hStmt, i++, (char*)mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7164         _bind_stmt_field_data_string(hStmt, i++, (char*)uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7165
7166
7167         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7168         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7169                         ("sqlite3_step fail:%d", rc));
7170
7171         ret = true;
7172
7173 FINISH_OFF:
7174         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7175         if (hStmt != NULL) {
7176                 rc = sqlite3_finalize(hStmt);
7177                 if (rc != SQLITE_OK) {
7178                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7179                         error = EMAIL_ERROR_DB_FAILURE;
7180                 }
7181         }
7182
7183         if (err_code != NULL)
7184                 *err_code = error;
7185
7186         EM_DEBUG_FUNC_END("ret [%d]", ret);
7187         return ret;
7188 }
7189
7190 INTERNAL_FUNC int emstorage_remove_downloaded_mail(char *multi_user_name,
7191                 int account_id,
7192                 int mailbox_id,
7193                 char *mailbox_name,
7194                 char *uid,
7195                 int transaction,
7196                 int *err_code)
7197 {
7198         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], mailbox_id[%d], mailbox_name[%s], "
7199                         "uid[%s], transaction[%d], err_code[%p]",
7200                         account_id, mailbox_id, mailbox_name, uid, transaction, err_code);
7201
7202         if (account_id < FIRST_ACCOUNT_ID) {
7203                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_name[%s], uid[%s]", account_id, mailbox_name, uid);
7204
7205                 if (err_code != NULL)
7206                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7207                 return false;
7208         }
7209
7210         int ret = false;
7211         int error = EMAIL_ERROR_NONE;
7212         char sql_query_string[QUERY_SIZE] = {0, };
7213         char *replaced_mailbox_name = NULL;
7214         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7215
7216         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7217
7218         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7219                         "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d ", account_id);
7220
7221         if (mailbox_id > 0) {
7222                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7223                                 sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7224                                 "AND mailbox_id = %d ", mailbox_id);
7225         }
7226
7227         if (mailbox_name) {             /*  NULL means all mailbox_name */
7228                 if (strstr(mailbox_name, "'"))
7229                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
7230                 else
7231                         replaced_mailbox_name = strdup(mailbox_name);
7232
7233                 SNPRINTF(sql_query_string+EM_SAFE_STRLEN(sql_query_string),
7234                                 sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7235                                 "AND mailbox_name = '%s' ", replaced_mailbox_name);
7236         }
7237
7238         if (uid) {              /*  NULL means all mail */
7239                 sqlite3_snprintf(sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7240                                 sql_query_string + EM_SAFE_STRLEN(sql_query_string), "AND server_uid = '%q' ",
7241                                 uid);
7242         }
7243
7244         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7245         if (error != EMAIL_ERROR_NONE) {
7246                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7247                 goto FINISH_OFF;
7248         }
7249
7250         ret = true;
7251
7252 FINISH_OFF:
7253         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7254
7255         EM_SAFE_FREE(replaced_mailbox_name);
7256
7257         if (err_code != NULL)
7258                 *err_code = error;
7259
7260         EM_DEBUG_FUNC_END("ret [%d]", ret);
7261         return ret;
7262 }
7263
7264 INTERNAL_FUNC int emstorage_free_read_mail_uid(emstorage_read_mail_uid_tbl_t** read_mail_uid, int count, int *err_code)
7265 {
7266         EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], count[%d], err_code[%p]", read_mail_uid, count, err_code);
7267
7268         int ret = false;
7269         int error = EMAIL_ERROR_NONE;
7270
7271         if (count > 0) {
7272                 if (!read_mail_uid || !*read_mail_uid) {
7273                         EM_DEBUG_EXCEPTION(" read_mail_uid[%p], count[%d]", read_mail_uid, count);
7274
7275                         error = EMAIL_ERROR_INVALID_PARAM;
7276                         goto FINISH_OFF;
7277                 }
7278
7279                 emstorage_read_mail_uid_tbl_t* p = *read_mail_uid;
7280                 int i;
7281
7282                 for (i = 0; i < count; i++) {
7283                         EM_SAFE_FREE(p[i].mailbox_name);
7284                         EM_SAFE_FREE(p[i].server_uid);
7285                 }
7286
7287                 EM_SAFE_FREE(p); *read_mail_uid = NULL;
7288         }
7289
7290         ret = true;
7291
7292 FINISH_OFF:
7293         if (err_code != NULL)
7294                 *err_code = error;
7295
7296         EM_DEBUG_FUNC_END("ret [%d]", ret);
7297         return ret;
7298 }
7299
7300 INTERNAL_FUNC int emstorage_get_rule_count_by_account_id(char *multi_user_name, int account_id, int *count, int transaction, int *err_code)
7301 {
7302         EM_DEBUG_FUNC_BEGIN("account_id [%d], count[%p], transaction[%d], err_code[%p]", count, transaction, err_code);
7303
7304         if (!count) {
7305                 EM_DEBUG_EXCEPTION("count[%p]", count);
7306
7307                 if (err_code != NULL)
7308                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7309                 return false;
7310         }
7311
7312         int rc = -1, ret = false;
7313         int error =  EMAIL_ERROR_NONE;
7314         char sql_query_string[QUERY_SIZE] = {0, };
7315
7316         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7317         EMSTORAGE_START_READ_TRANSACTION(transaction);
7318
7319         if (account_id != ALL_ACCOUNT)
7320                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl where account_id = %d", account_id);
7321         else
7322                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl");
7323
7324         char **result;
7325
7326         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7327         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7328                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7329
7330         *count = atoi(result[1]);
7331         sqlite3_free_table(result);
7332
7333         ret = true;
7334
7335 FINISH_OFF:
7336         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7337
7338         if (err_code != NULL)
7339                 *err_code = error;
7340
7341         EM_DEBUG_FUNC_END("ret [%d]", ret);
7342         return ret;
7343 }
7344
7345 INTERNAL_FUNC int emstorage_get_rule(char *multi_user_name, int account_id, int type, int start_idx, int *select_num, int *is_completed, emstorage_rule_tbl_t** rule_list, int transaction, int *err_code)
7346 {
7347         EM_DEBUG_FUNC_BEGIN("account_id[%d], type[%d], start_idx[%d], select_num[%p], is_completed[%p], rule_list[%p], transaction[%d], err_code[%p]", account_id, type, start_idx, select_num, is_completed, rule_list, transaction, err_code);
7348
7349         if (!select_num || !is_completed || !rule_list) {               /*  only global rule supported. */
7350                 EM_DEBUG_EXCEPTION(" account_id[%d], type[%d], start_idx[%d], select_num[%p], is_completed[%p], rule_list[%p]", account_id, type, start_idx, select_num, is_completed, rule_list);
7351
7352                 if (err_code != NULL)
7353                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7354                 return false;
7355         }
7356
7357         int ret = false;
7358         int error = EMAIL_ERROR_NONE;
7359
7360         emstorage_rule_tbl_t* p_data_tbl = NULL;
7361         int i = 0, count = 0;
7362         DB_STMT hStmt = NULL;
7363         char sql_query_string[QUERY_SIZE] = {0, };
7364
7365         int rc;
7366
7367         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7368         EMSTORAGE_START_READ_TRANSACTION(transaction);
7369
7370         if (account_id != ALL_ACCOUNT) {
7371                 if (type)
7372                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d AND type = %d", account_id, type);
7373                 else
7374                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d ORDER BY type", account_id);
7375         } else {
7376                 if (type)
7377                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE type = %d", type);
7378                 else
7379                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl ORDER BY type");
7380         }
7381
7382         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7383         /*      EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt); */
7384         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7385                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7386
7387
7388         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7389         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7390                         ("sqlite3_step fail:%d", rc));
7391
7392         char **result;
7393         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL); */
7394         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
7395         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7396                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7397
7398         sqlite3_free_table(result);
7399
7400         if (count == 0) {
7401                 EM_DEBUG_LOG_DEV("No matching rule found...");
7402                 ret = true;
7403                 error = EMAIL_ERROR_FILTER_NOT_FOUND; /*there is no matched rule*/
7404                 goto FINISH_OFF;
7405         }
7406
7407
7408         if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t) * count))) {
7409                 EM_DEBUG_EXCEPTION(" malloc failed...");
7410
7411                 error = EMAIL_ERROR_OUT_OF_MEMORY;
7412                 goto FINISH_OFF;
7413         }
7414
7415         memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t) * count);
7416
7417         for (i = 0; i < count; i++) {
7418                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7419                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7420                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7421                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].type), TYPE_IDX_IN_MAIL_RULE_TBL);
7422                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7423                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7424                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7425                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7426                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7427                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7428
7429                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7430                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7431                                 ("sqlite3_step fail:%d", rc));
7432         }
7433
7434         ret = true;
7435
7436 FINISH_OFF:
7437
7438         EM_DEBUG_LOG("[%d] rules found.", count);
7439
7440         if (ret == true) {
7441                 *rule_list = p_data_tbl;
7442                 *select_num = count;
7443         } else if (p_data_tbl != NULL)
7444                 emstorage_free_rule(&p_data_tbl, count, NULL); /* CID FIX */
7445
7446         if (hStmt != NULL) {
7447                 rc = sqlite3_finalize(hStmt);
7448                 if (rc != SQLITE_OK) {
7449                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7450                         error = EMAIL_ERROR_DB_FAILURE;
7451                 }
7452         }
7453
7454         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7455
7456         if (err_code != NULL)
7457                 *err_code = error;
7458
7459         EM_DEBUG_FUNC_END("ret [%d]", ret);
7460         return ret;
7461 }
7462
7463 INTERNAL_FUNC int emstorage_get_rule_by_id(char *multi_user_name, int rule_id, emstorage_rule_tbl_t** rule, int transaction, int *err_code)
7464 {
7465         EM_DEBUG_FUNC_BEGIN("rule_id[%d], rule[%p], transaction[%d], err_code[%p]", rule_id, rule, transaction, err_code);
7466         int error = EMAIL_ERROR_NONE;
7467         int ret = false;
7468         DB_STMT hStmt = NULL;
7469
7470         if (rule_id <= 0) {
7471                 EM_DEBUG_EXCEPTION("Invalid parameter");
7472                 error = EMAIL_ERROR_INVALID_PARAM;
7473                 goto FINISH_OFF;
7474         }
7475
7476         if (!rule) {
7477                 EM_DEBUG_EXCEPTION("rule_id[%d], rule[%p]", rule_id, rule);
7478                 error = EMAIL_ERROR_INVALID_PARAM;
7479                 goto FINISH_OFF;
7480         }
7481
7482         emstorage_rule_tbl_t* p_data_tbl = NULL;
7483         int rc;
7484
7485         char sql_query_string[QUERY_SIZE] = {0, };
7486         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7487         EMSTORAGE_START_READ_TRANSACTION(transaction);
7488
7489         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7490
7491         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7492         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7493                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7494
7495
7496         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7497         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7498                         ("sqlite3_step fail:%d", rc));
7499
7500         if (rc == SQLITE_DONE) {
7501                 EM_DEBUG_EXCEPTION(" no matched rule found...");
7502                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7503                 goto FINISH_OFF;
7504         }
7505
7506         if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t)))) {
7507                 EM_DEBUG_EXCEPTION(" malloc failed...");
7508                 error = EMAIL_ERROR_OUT_OF_MEMORY;
7509                 goto FINISH_OFF;
7510         }
7511
7512         memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t));
7513         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7514         _get_stmt_field_data_int(hStmt, &(p_data_tbl->rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7515         _get_stmt_field_data_string(hStmt, &(p_data_tbl->filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7516         _get_stmt_field_data_int(hStmt, &(p_data_tbl->type), TYPE_IDX_IN_MAIL_RULE_TBL);
7517         _get_stmt_field_data_string(hStmt, &(p_data_tbl->value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7518         _get_stmt_field_data_string(hStmt, &(p_data_tbl->value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7519         _get_stmt_field_data_int(hStmt, &(p_data_tbl->action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7520         _get_stmt_field_data_int(hStmt, &(p_data_tbl->target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7521         _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7522         _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7523
7524         ret = true;
7525
7526 FINISH_OFF:
7527
7528         if (ret == true)
7529                 *rule = p_data_tbl;
7530
7531         if (hStmt != NULL) {
7532                 rc = sqlite3_finalize(hStmt);
7533                 if (rc != SQLITE_OK) {
7534                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7535                         error = EMAIL_ERROR_DB_FAILURE;
7536                 }
7537         }
7538
7539         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7540
7541         if (err_code != NULL)
7542                 *err_code = error;
7543
7544         EM_DEBUG_FUNC_END("ret [%d]", ret);
7545         return ret;
7546 }
7547
7548 INTERNAL_FUNC int emstorage_change_rule(char *multi_user_name, int rule_id, emstorage_rule_tbl_t* new_rule, int transaction, int *err_code)
7549 {
7550         EM_DEBUG_FUNC_BEGIN("rule_id[%d], new_rule[%p], transaction[%d], err_code[%p]", rule_id, new_rule, transaction, err_code);
7551
7552         if (!new_rule) {                /*  only global rule supported. */
7553                 EM_DEBUG_EXCEPTION("rule_id[%d], new_rule[%p]", rule_id, new_rule);
7554
7555                 if (err_code != NULL)
7556                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7557                 return false;
7558         }
7559
7560         int rc, ret = false;
7561         int error = EMAIL_ERROR_NONE;
7562
7563         DB_STMT hStmt = NULL;
7564         char sql_query_string[QUERY_SIZE] = {0, };
7565         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7566         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7567
7568         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7569                         "UPDATE mail_rule_tbl SET"
7570                         "  filter_name = ?"
7571                         ", type = ?"
7572                         ", value = ?"
7573                         ", value2 = ?"
7574                         ", action_type = ?"
7575                         ", target_mailbox_id = ?"
7576                         ", flag1 = ?"
7577                         ", flag2 = ?"
7578                         ", account_id = ?"
7579                         ", rule_id = ?"
7580                         " WHERE rule_id = %d"
7581                         , rule_id);
7582
7583
7584         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7585         EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
7586         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7587                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7588
7589         int i = 0;
7590
7591         _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->filter_name, 0, FILTER_NAME_LEN_IN_MAIL_RULE_TBL);
7592         _bind_stmt_field_data_int(hStmt, i++, new_rule->type);
7593         _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7594         _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7595         _bind_stmt_field_data_int(hStmt, i++, new_rule->action_type);
7596         _bind_stmt_field_data_int(hStmt, i++, new_rule->target_mailbox_id);
7597         _bind_stmt_field_data_int(hStmt, i++, new_rule->flag1);
7598         _bind_stmt_field_data_int(hStmt, i++, new_rule->flag2);
7599         _bind_stmt_field_data_int(hStmt, i++, new_rule->account_id);
7600         _bind_stmt_field_data_int(hStmt, i++, rule_id);
7601
7602
7603         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7604         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7605                         ("sqlite3_step fail:%d", rc));
7606
7607         ret = true;
7608
7609 FINISH_OFF:
7610         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7611         if (hStmt != NULL) {
7612                 rc = sqlite3_finalize(hStmt);
7613                 if (rc != SQLITE_OK) {
7614                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7615                         error = EMAIL_ERROR_DB_FAILURE;
7616                 }
7617         }
7618
7619         if (err_code != NULL)
7620                 *err_code = error;
7621
7622         EM_DEBUG_FUNC_END("ret [%d]", ret);
7623         return ret;
7624 }
7625
7626 INTERNAL_FUNC int emstorage_find_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7627 {
7628         EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7629
7630         if (!rule) {
7631                 EM_DEBUG_LOG("rule is NULL");
7632                 if (err_code != NULL)
7633                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7634                 return false;
7635         }
7636
7637         DB_STMT hStmt = NULL;
7638         char sql_query_string[QUERY_SIZE] = {0,};
7639         int error = EMAIL_ERROR_NONE;
7640         int rc = 0;
7641         int ret = false;
7642
7643         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7644         EMSTORAGE_START_READ_TRANSACTION(transaction);
7645
7646         switch (rule->action_type) {
7647         case EMAIL_FILTER_MOVE:
7648                 if (rule->type == EMAIL_PRIORITY_SENDER) {
7649                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7650                                         "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7651                                         rule->action_type, rule->type, rule->value2);
7652                 } else {
7653                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7654                                         "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(filter_name) = UPPER(\'%q\')", rule->action_type, rule->type, rule->filter_name);
7655                 }
7656                 break;
7657         case EMAIL_FILTER_BLOCK:
7658                 if (rule->type == EMAIL_FILTER_FROM)
7659                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7660                                         "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7661                                         rule->action_type, rule->type, rule->value2);
7662                 else if (rule->type == EMAIL_FILTER_SUBJECT)
7663                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7664                                         "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value) = UPPER(\'%q\')",
7665                                         rule->action_type, rule->type, rule->value);
7666                 else if (rule->type == (EMAIL_FILTER_SUBJECT | EMAIL_FILTER_FROM))
7667                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7668                                         "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND (type = %d AND UPPER(value) = UPPER(\'%q\')) OR (type = %d AND UPPER(value2) = UPPER(\'%q\'))",
7669                                         rule->action_type, EMAIL_FILTER_SUBJECT, rule->value, EMAIL_FILTER_FROM, rule->value2);
7670                 break;
7671
7672         default:
7673                 EM_DEBUG_EXCEPTION("Invalid parameter : rule->action_type[%d]", rule->action_type);
7674                 error = EMAIL_ERROR_INVALID_PARAM;
7675                 goto FINISH_OFF;
7676                 break;
7677         }
7678
7679         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7680         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7681                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7682
7683         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7684         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7685                         ("sqlite3_step fail:%d", rc));
7686
7687         if (rc == SQLITE_DONE) {
7688                 EM_DEBUG_EXCEPTION(" no matched rule found...");
7689                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7690         }
7691
7692         ret = true;
7693
7694 FINISH_OFF:
7695
7696         if (hStmt != NULL) {
7697                 rc = sqlite3_finalize(hStmt);
7698                 if (rc != SQLITE_OK) {
7699                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7700                         error = EMAIL_ERROR_DB_FAILURE;
7701                 }
7702         }
7703
7704         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7705
7706         if (err_code)
7707                 *err_code = error;
7708
7709         EM_DEBUG_FUNC_END("ret [%d]", ret);
7710         return ret;
7711 }
7712
7713 INTERNAL_FUNC int emstorage_add_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7714 {
7715         EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7716
7717         if (!rule) {    /*  only global rule supported. */
7718                 EM_DEBUG_LOG("rule is NULL");
7719                 if (err_code != NULL)
7720                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7721                 return false;
7722         }
7723
7724         int rc, rc_2, ret = false;
7725         int error = EMAIL_ERROR_NONE;
7726         DB_STMT hStmt = NULL;
7727         char sql_query_string[QUERY_SIZE] = {0, };
7728
7729         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7730
7731         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7732
7733         char *sql;
7734         char **result;
7735         sql = "SELECT max(rowid) FROM mail_rule_tbl;";
7736
7737         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7738         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7739                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7740
7741         if (NULL == result[1])
7742                 rc = 1;
7743         else
7744                 rc = atoi(result[1])+1;
7745
7746         sqlite3_free_table(result);
7747
7748         rule->rule_id = rc;
7749
7750         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7751                         "INSERT INTO mail_rule_tbl VALUES "
7752                         "(?"            /*  account id */
7753                         ", ?"           /*  rule_id */
7754                         ", ?"           /*  filter_name */
7755                         ", ?"           /*  type */
7756                         ", ?"           /*  value */
7757                         ", ?"           /*  value2 */
7758                         ", ?"           /*  action_type */
7759                         ", ?"           /*  target_mailbox_id */
7760                         ", ?"           /*  flag1 */
7761                         ", ?)");        /*  flag2 */
7762
7763         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc_2);
7764         if (rc_2 != SQLITE_OK) {
7765                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc_2, sqlite3_errmsg(local_db_handle));
7766                 error = EMAIL_ERROR_DB_FAILURE;
7767                 goto FINISH_OFF;
7768         }
7769
7770         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL, rule->account_id);
7771         _bind_stmt_field_data_int(hStmt, RULE_ID_IDX_IN_MAIL_RULE_TBL, rule->rule_id);
7772         _bind_stmt_field_data_string(hStmt, FILTER_NAME_IDX_IN_MAIL_RULE_TBL, (char*)rule->filter_name, 0, FILTER_NAME_LEN_IN_MAIL_RULE_TBL);
7773         _bind_stmt_field_data_int(hStmt, TYPE_IDX_IN_MAIL_RULE_TBL, rule->type);
7774         _bind_stmt_field_data_string(hStmt, VALUE_IDX_IN_MAIL_RULE_TBL, (char*)rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7775         _bind_stmt_field_data_string(hStmt, VALUE2_IDX_IN_MAIL_RULE_TBL, (char*)rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7776         _bind_stmt_field_data_int(hStmt, ACTION_TYPE_IDX_IN_MAIL_RULE_TBL, rule->action_type);
7777         _bind_stmt_field_data_int(hStmt, TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL, rule->target_mailbox_id);
7778         _bind_stmt_field_data_int(hStmt, FLAG1_IDX_IN_MAIL_RULE_TBL, rule->flag1);
7779         _bind_stmt_field_data_int(hStmt, FLAG2_IDX_IN_MAIL_RULE_TBL, rule->flag2);
7780
7781         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7782         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7783                         ("sqlite3_step fail:%d", rc));
7784
7785         ret = true;
7786
7787 FINISH_OFF:
7788         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7789         if (hStmt != NULL) {
7790                 rc = sqlite3_finalize(hStmt);
7791                 if (rc != SQLITE_OK) {
7792                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7793                         error = EMAIL_ERROR_DB_FAILURE;
7794                 }
7795         }
7796
7797         if (err_code != NULL)
7798                 *err_code = error;
7799
7800         EM_DEBUG_FUNC_END("ret [%d]", ret);
7801         return ret;
7802 }
7803
7804 INTERNAL_FUNC int emstorage_delete_rule(char *multi_user_name, int rule_id, int transaction, int *err_code)
7805 {
7806         EM_DEBUG_FUNC_BEGIN("rule_id[%d], transaction[%d], err_code[%p]", rule_id, transaction, err_code);
7807
7808         if (rule_id <= 0) {             /*  only global rule supported. */
7809                 EM_DEBUG_EXCEPTION("rule_id[%d]", rule_id);
7810
7811                 if (err_code != NULL)
7812                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7813                 return false;
7814         }
7815
7816         int rc, ret = false;
7817         int error = EMAIL_ERROR_NONE;
7818         char sql_query_string[QUERY_SIZE] = {0, };
7819         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7820         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7821
7822         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7823         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7824         if (error != EMAIL_ERROR_NONE) {
7825                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7826                 goto FINISH_OFF;
7827         }
7828
7829         rc = sqlite3_changes(local_db_handle);
7830         if (rc == 0) {
7831                 EM_DEBUG_EXCEPTION(" no matched rule found...");
7832
7833                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7834                 goto FINISH_OFF;
7835         }
7836
7837         ret = true;
7838
7839 FINISH_OFF:
7840         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7841
7842         if (err_code != NULL)
7843                 *err_code = error;
7844
7845         EM_DEBUG_FUNC_END("ret [%d]", ret);
7846         return ret;
7847 }
7848
7849 INTERNAL_FUNC int emstorage_free_rule(emstorage_rule_tbl_t** rule_list, int count, int *err_code)
7850 {
7851         EM_DEBUG_FUNC_BEGIN("rule_list[%p], conut[%d], err_code[%p]", rule_list, count, err_code);
7852
7853         int ret = false;
7854         int error = EMAIL_ERROR_NONE;
7855
7856         if (count > 0) {
7857                 if (!rule_list || !*rule_list) {
7858                         EM_DEBUG_EXCEPTION(" rule_list[%p], conut[%d]", rule_list, count);
7859
7860                         error = EMAIL_ERROR_INVALID_PARAM;
7861                         goto FINISH_OFF;
7862                 }
7863
7864                 emstorage_rule_tbl_t* p = *rule_list;
7865                 int i = 0;
7866
7867                 for (; i < count; i++)
7868                         EM_SAFE_FREE(p[i].value);
7869
7870                 EM_SAFE_FREE(p); *rule_list = NULL;
7871         }
7872
7873 FINISH_OFF:
7874         if (err_code != NULL)
7875                 *err_code = error;
7876
7877         EM_DEBUG_FUNC_END("ret [%d]", ret);
7878         return ret;
7879 }
7880
7881 INTERNAL_FUNC int emstorage_get_mail_count(char *multi_user_name, int account_id, int mailbox_id, int *total, int *unseen, int transaction, int *err_code)
7882 {
7883         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], total[%p], unseen[%p], transaction[%d], err_code[%p]", account_id, mailbox_id, total, unseen, transaction, err_code);
7884
7885         if (!total && !unseen) {
7886                 EM_DEBUG_EXCEPTION(" accoun_id[%d], mailbox_id[%d], total[%p], unseen[%p]", account_id, mailbox_id, total, unseen);
7887                 if (err_code != NULL)
7888                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7889                 return false;
7890         }
7891
7892         int rc = -1, ret = false;
7893         int error = EMAIL_ERROR_NONE;
7894         DB_STMT hStmt = NULL;
7895         char sql_query_string[QUERY_SIZE] = {0, };
7896         char *replaced_mailbox_name = NULL;
7897
7898         memset(&sql_query_string, 0x00, sizeof(sql_query_string));
7899         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7900         EMSTORAGE_START_READ_TRANSACTION(transaction);
7901
7902         if (total) {
7903                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl");
7904
7905                 if (account_id != ALL_ACCOUNT) {
7906                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE account_id = %d", account_id);
7907                         if (mailbox_id)
7908                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND mailbox_id = %d", mailbox_id);
7909                 } else if (mailbox_id)
7910                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE mailbox_id = %d", mailbox_id);
7911
7912 #ifdef USE_GET_RECORD_COUNT_API
7913                 char **result;
7914
7915                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7916                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF2; },
7917                                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7918
7919                 *total = atoi(result[1]);
7920                 sqlite3_free_table(result);
7921 #else
7922
7923                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7924                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF2; },
7925                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7926
7927                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7928                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF2; },
7929                                 ("sqlite3_step fail:%d", rc));
7930                 _get_stmt_field_data_int(hStmt, total, 0);
7931 #endif          /*  USE_GET_RECORD_COUNT_API */
7932         }
7933
7934         if (unseen) {
7935                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl WHERE flags_seen_field = 0");               /*  fSEEN = 0x01 */
7936
7937                 if (account_id != ALL_ACCOUNT) {
7938                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7939                                 sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND account_id = %d", account_id);
7940                 }
7941
7942                 if (mailbox_id) {
7943                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7944                                 sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND mailbox_id = %d", mailbox_id);
7945                 } else
7946                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7947                                 sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND mailbox_type NOT IN (3, 5)");
7948
7949                 char **result;
7950                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7951                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc,
7952                                 { error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7953                                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7954
7955                 *unseen = atoi(result[1]);
7956                 sqlite3_free_table(result);
7957
7958         }
7959 FINISH_OFF:
7960         ret = true;
7961
7962 FINISH_OFF2:
7963
7964 #ifndef USE_PREPARED_QUERY_
7965         if (hStmt != NULL) {
7966                 rc = sqlite3_finalize(hStmt);
7967                 if (rc != SQLITE_OK) {
7968                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7969                         error = EMAIL_ERROR_DB_FAILURE;
7970                 }
7971         }
7972 #endif
7973
7974         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7975
7976         EM_SAFE_FREE(replaced_mailbox_name);
7977
7978         if (err_code != NULL)
7979                 *err_code = error;
7980
7981         EM_DEBUG_FUNC_END("ret [%d]", ret);
7982         return ret;
7983 }
7984
7985 INTERNAL_FUNC int emstorage_get_mail_field_by_id(char *multi_user_name, int mail_id, int type, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
7986 {
7987         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
7988
7989         if (mail_id <= 0 || !mail) {
7990                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
7991                 if (err_code != NULL)
7992                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7993                 return false;
7994         }
7995
7996         int col_index = 0;
7997         emstorage_mail_tbl_t* p_data_tbl = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
7998
7999         if (p_data_tbl == NULL) {
8000                 EM_DEBUG_EXCEPTION("malloc failed...");
8001                 if (err_code != NULL)
8002                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8003                 return false;
8004         }
8005
8006         memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8007         DB_STMT hStmt = NULL;
8008         char sql_query_string[QUERY_SIZE] = {0, };
8009
8010         int rc, ret = false;
8011         int error = EMAIL_ERROR_NONE;
8012
8013         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8014         EMSTORAGE_START_READ_TRANSACTION(transaction);
8015
8016         switch (type) {
8017         case RETRIEVE_SUMMARY:
8018                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8019                                 "SELECT account_id, "
8020                                 "mail_id, "
8021                                 "mailbox_id, "
8022                                 "server_mail_status, "
8023                                 "server_mailbox_name, "
8024                                 "server_mail_id, "
8025                                 "file_path_plain, "
8026                                 "file_path_html,"
8027                                 "file_path_mime_entity, "
8028                                 "flags_seen_field, "
8029                                 "save_status, "
8030                                 "lock_status, "
8031                                 "thread_id, "
8032                                 "thread_item_count "
8033                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8034                 break;
8035
8036         case RETRIEVE_FIELDS_FOR_DELETE:
8037                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8038                                 "SELECT account_id, "
8039                                 "mail_id, "
8040                                 "server_mail_status, "
8041                                 "server_mailbox_name, "
8042                                 "server_mail_id "
8043                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8044                 break;
8045
8046         case RETRIEVE_ACCOUNT:
8047                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8048                                 "SELECT account_id "
8049                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8050                 break;
8051
8052         case RETRIEVE_FLAG:
8053                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8054                                 "SELECT account_id, "
8055                                 "flags_seen_field, "
8056                                 "thread_id, "
8057                                 "mailbox_id "
8058                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8059                 break;
8060
8061         default:
8062                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
8063                 error = EMAIL_ERROR_INVALID_PARAM;
8064                 goto FINISH_OFF;
8065         }
8066
8067         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
8068
8069         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8070         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8071                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8072
8073
8074         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8075         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8076                         ("sqlite3_step fail:%d", rc));
8077
8078         if (rc == SQLITE_DONE) {
8079                 EM_DEBUG_LOG("no matched mail found...");
8080                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8081                 goto FINISH_OFF;
8082         }
8083         switch (type) {
8084         case RETRIEVE_SUMMARY:
8085                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8086                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
8087                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
8088                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
8089                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
8090                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8091                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, col_index++);
8092                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, col_index++);
8093                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, col_index++);
8094                 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8095                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), col_index++);
8096                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), col_index++);
8097                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8098                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_item_count), col_index++);
8099                 break;
8100
8101         case RETRIEVE_FIELDS_FOR_DELETE:
8102                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8103                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
8104                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
8105                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
8106                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8107                 break;
8108
8109         case RETRIEVE_ACCOUNT:
8110                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8111                 break;
8112
8113         case RETRIEVE_FLAG:
8114                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8115                 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8116                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8117                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
8118                 break;
8119         }
8120
8121         ret = true;
8122
8123 FINISH_OFF:
8124         if (ret == true)
8125                 *mail = p_data_tbl;
8126         else if (p_data_tbl != NULL)
8127                 emstorage_free_mail(&p_data_tbl,  1, NULL);
8128
8129         if (hStmt != NULL) {
8130                 rc = sqlite3_finalize(hStmt);
8131                 if (rc != SQLITE_OK) {
8132                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8133                         error = EMAIL_ERROR_DB_FAILURE;
8134                 }
8135         }
8136
8137
8138         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8139
8140         if (err_code != NULL)
8141                 *err_code = error;
8142
8143         EM_DEBUG_FUNC_END("ret [%d]", ret);
8144         return ret;
8145 }
8146
8147 INTERNAL_FUNC int emstorage_get_mail_field_by_multiple_mail_id(char *multi_user_name, int mail_ids[], int number_of_mails, int type, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
8148 {
8149         EM_DEBUG_FUNC_BEGIN("mail_ids[%p], number_of_mails [%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_ids, number_of_mails, type, mail, transaction, err_code);
8150
8151         int ret = false;
8152         int error = EMAIL_ERROR_NONE;
8153         int query_string_length = 0;
8154         int i = 0, item_count = 0, rc = -1, field_count, col_index, cur_sql_query_string = 0;
8155         char **result = NULL;
8156         char *sql_query_string = NULL;
8157         emstorage_mail_tbl_t* p_data_tbl = NULL;
8158         sqlite3 *local_db_handle = NULL;
8159
8160         if (number_of_mails <= 0 || !mail_ids) {
8161                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8162                 if (err_code != NULL)
8163                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8164                 return false;
8165         }
8166
8167         p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * number_of_mails);
8168
8169         query_string_length = (sizeof(char) * 8 * number_of_mails) + 512;
8170         sql_query_string = (char*)em_malloc(query_string_length);
8171
8172         if (p_data_tbl == NULL || sql_query_string == NULL) {
8173                 EM_DEBUG_EXCEPTION("malloc failed...");
8174
8175                 EM_SAFE_FREE(p_data_tbl);
8176                 EM_SAFE_FREE(sql_query_string);
8177
8178                 if (err_code != NULL)
8179                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8180                 return false;
8181         }
8182
8183         local_db_handle = emstorage_get_db_connection(multi_user_name);
8184
8185         EMSTORAGE_START_READ_TRANSACTION(transaction);
8186
8187         switch (type) {
8188         case RETRIEVE_SUMMARY:
8189                 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8190                                 "SELECT account_id, "
8191                                 "mail_id, "
8192                                 "mailbox_id, "
8193                                 "server_mail_status, "
8194                                 "server_mailbox_name, "
8195                                 "server_mail_id, "
8196                                 "file_path_plain, "
8197                                 "file_path_html, "
8198                                 "file_path_mime_entity, "
8199                                 "subject, "
8200                                 "flags_seen_field, "
8201                                 "save_status, "
8202                                 "lock_status, "
8203                                 "thread_id, "
8204                                 "thread_item_count "
8205                                 "FROM mail_tbl WHERE mail_id in (");
8206                 field_count = 15;
8207                 break;
8208
8209         case RETRIEVE_FIELDS_FOR_DELETE:
8210                 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8211                                 "SELECT account_id, "
8212                                 "mail_id, "
8213                                 "server_mail_status, "
8214                                 "server_mailbox_name, "
8215                                 "server_mail_id "
8216                                 "FROM mail_tbl WHERE mail_id in (");
8217                 field_count = 5;
8218                 break;
8219
8220         case RETRIEVE_ACCOUNT:
8221                 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8222                                 "SELECT account_id FROM mail_tbl WHERE mail_id in (");
8223                 field_count = 1;
8224                 break;
8225
8226         case RETRIEVE_FLAG:
8227                 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8228                                 "SELECT account_id, "
8229                                 "mail_id, "
8230                                 "mailbox_id, "
8231                                 "flags_seen_field, "
8232                                 "thread_id "
8233                                 "FROM mail_tbl WHERE mail_id in (");
8234                 field_count = 5;
8235                 break;
8236
8237         default:
8238                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
8239                 error = EMAIL_ERROR_INVALID_PARAM;
8240                 goto FINISH_OFF;
8241         }
8242
8243         for (i = 0; i < number_of_mails; i++)
8244                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_string_length, "%d,", mail_ids[i]);
8245         sql_query_string[EM_SAFE_STRLEN(sql_query_string) - 1] = ')';
8246
8247         EM_DEBUG_LOG_SEC("Query [%s], Length [%d]", sql_query_string, EM_SAFE_STRLEN(sql_query_string));
8248
8249         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &item_count, 0, NULL), rc);
8250         if (SQLITE_OK != rc && -1 != rc) {
8251                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
8252                 error = EMAIL_ERROR_DB_FAILURE;
8253                 goto FINISH_OFF;
8254         }
8255         EM_DEBUG_LOG("item_count [%d]", item_count);
8256
8257         if (number_of_mails != item_count) {
8258                 EM_DEBUG_EXCEPTION("Can't find all emails");
8259                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8260                 goto FINISH_OFF;
8261         }
8262
8263         col_index = field_count;
8264
8265         for (i = 0; i < item_count; i++)        {
8266                 switch (type) {
8267                 case RETRIEVE_SUMMARY:
8268                         _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8269                         _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8270                         _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8271                         _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8272                         _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8273                         _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8274                         _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
8275                         _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
8276                         _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
8277                         _get_table_field_data_string(result, &(p_data_tbl[i].subject), 0, col_index++);
8278                         _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8279                         _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
8280                         _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
8281                         _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8282                         _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
8283                         break;
8284
8285                 case RETRIEVE_FIELDS_FOR_DELETE:
8286                         _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8287                         _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8288                         _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8289                         _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8290                         _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8291                         break;
8292
8293                 case RETRIEVE_ACCOUNT:
8294                         _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8295                         break;
8296
8297                 case RETRIEVE_FLAG:
8298                         _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8299                         _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8300                         _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8301                         _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8302                         _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8303                         break;
8304                 }
8305         }
8306
8307         ret = true;
8308
8309 FINISH_OFF:
8310         if (ret == true)
8311                 *mail = p_data_tbl;
8312         else
8313                 emstorage_free_mail(&p_data_tbl, number_of_mails, NULL);
8314
8315         if (result)
8316                 sqlite3_free_table(result);
8317
8318         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8319
8320         EM_SAFE_FREE(sql_query_string);
8321
8322         if (err_code != NULL)
8323                 *err_code = error;
8324
8325         EM_DEBUG_FUNC_END("ret [%d]", ret);
8326         return ret;
8327 }
8328
8329 INTERNAL_FUNC int emstorage_get_mail_by_id(char *multi_user_name, int mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code)
8330 {
8331         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8332
8333         if (mail_id <= 0 || !mail) {
8334                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
8335                 if (err_code != NULL)
8336                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8337                 return false;
8338         }
8339
8340         int ret = false, error = EMAIL_ERROR_NONE, count;
8341         char conditional_clause[QUERY_SIZE] = {0, };
8342         emstorage_mail_tbl_t* p_data_tbl = NULL;
8343
8344         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8345         EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8346
8347         if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8348                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8349                 goto FINISH_OFF;
8350         }
8351
8352         ret = true;
8353
8354 FINISH_OFF:
8355         if (ret == true)
8356                 *mail = p_data_tbl;
8357         else if (p_data_tbl != NULL)
8358                 emstorage_free_mail(&p_data_tbl, 1, &error);
8359
8360         if (err_code != NULL)
8361                 *err_code = error;
8362
8363         EM_DEBUG_FUNC_END("ret [%d]", ret);
8364         return ret;
8365 }
8366
8367 #ifdef __FEATURE_BODY_SEARCH__
8368 INTERNAL_FUNC int emstorage_get_mail_text_by_id(char *multi_user_name, int mail_id, emstorage_mail_text_tbl_t **mail_text, int transaction, int *err_code)
8369 {
8370         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
8371
8372         if (mail_id <= 0 || !mail_text) {
8373                 EM_DEBUG_EXCEPTION("mail_id[%d], mail_text[%p]", mail_id, mail_text);
8374                 if (err_code != NULL)
8375                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8376                 return false;
8377         }
8378
8379         int ret = false;
8380         int error = EMAIL_ERROR_NONE;
8381         int count = 0;
8382         char conditional_clause[QUERY_SIZE] = {0, };
8383         emstorage_mail_text_tbl_t *p_data_tbl = NULL;
8384
8385         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8386         EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8387
8388         if (!emstorage_query_mail_text_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8389                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8390                 goto FINISH_OFF;
8391         }
8392
8393         ret = true;
8394
8395 FINISH_OFF:
8396         if (ret == true)
8397                 *mail_text = p_data_tbl;
8398         else if (p_data_tbl != NULL)
8399                 emstorage_free_mail_text(&p_data_tbl, 1, &error);
8400
8401         if (err_code != NULL)
8402                 *err_code = error;
8403
8404         EM_DEBUG_FUNC_END("ret [%d]", ret);
8405         return ret;
8406 }
8407 #endif
8408
8409 INTERNAL_FUNC int emstorage_mail_search_start(char *multi_user_name,
8410                 emstorage_search_filter_t *search,
8411                 int account_id,
8412                 int mailbox_id,
8413                 int sorting,
8414                 DB_STMT *search_handle,
8415                 int *searched,
8416                 int transaction,
8417                 int *err_code)
8418 {
8419         EM_DEBUG_FUNC_BEGIN("search[%p], account_id[%d], mailbox_id[%d], sorting[%d], "
8420                         "search_handle[%p], searched[%p], transaction[%d], err_code[%p]",
8421                         search, account_id, mailbox_id, sorting, search_handle,
8422                         searched, transaction, err_code);
8423
8424         if (!search_handle || !searched) {
8425                 if (err_code != NULL)
8426                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8427                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8428                 EM_DEBUG_FUNC_END("false");
8429                 return false;
8430         }
8431
8432         emstorage_search_filter_t* p = search;
8433         int error = EMAIL_ERROR_NONE;
8434         DB_STMT hStmt = NULL;
8435         char sql_query_string[QUERY_SIZE] = {0, };
8436         int rc, ret = false;
8437         int and = false, mail_count = 0;
8438
8439         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8440         EMSTORAGE_START_READ_TRANSACTION(transaction);
8441
8442         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl");
8443
8444         if (account_id != ALL_ACCOUNT) {
8445                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE account_id = %d", account_id);
8446                 and = true;
8447         }
8448
8449         if (mailbox_id) {
8450                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s mailbox_id = %d", and ? "AND" : "WHERE", mailbox_id);
8451                 and = true;
8452         }
8453
8454         while (p) {
8455                 if (p->key_type) {
8456                         if (!strncmp(p->key_type, "subject", strlen("subject"))) {
8457                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s subject LIKE '%%%%%s%%%%'", and ? "AND" : "WHERE", p->key_value);
8458                                 and = true;
8459                         } else if (!strncmp(p->key_type, "full_address_from", strlen("full_address_from"))) {
8460                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s full_address_from LIKE '%%%%%s%%%%'", and ? "AND" : "WHERE", p->key_value);
8461                                 and = true;
8462                         } else if (!strncmp(p->key_type, "full_address_to", strlen("full_address_to"))) {
8463                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s full_address_to LIKE '%%%%%s%%%%'", and ? "AND" : "WHERE", p->key_value);
8464                                 and = true;
8465                         } else if (!strncmp(p->key_type, "email_address", strlen("email_address"))) {
8466                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s email_address_sender = '%s' OR email_address_recipient = '%s'", and ? "AND" : "WHERE", p->key_value, p->key_value);
8467                                 and = true;
8468                         }
8469                         p = p->next;
8470                 }
8471         }
8472
8473         if (sorting)
8474                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " ORDER BY date_time");
8475
8476         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
8477
8478
8479         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8480         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8481                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8482
8483
8484         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8485         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8486                         ("sqlite3_step fail:%d", rc));
8487
8488         char **result;
8489
8490         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &mail_count, NULL, NULL), rc);
8491         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
8492                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8493
8494         sqlite3_free_table(result);
8495
8496         ret = true;
8497
8498 FINISH_OFF:
8499         if (ret == true) {
8500                 *search_handle = hStmt;
8501                 *searched = mail_count;
8502                 EM_DEBUG_LOG("mail_count [%d]", mail_count);
8503         } else {
8504                 if (hStmt != NULL) {
8505                         rc = sqlite3_finalize(hStmt);
8506                         if (rc != SQLITE_OK) {
8507                                 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8508                                 error = EMAIL_ERROR_DB_FAILURE;
8509                         }
8510                 }
8511
8512                 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8513         }
8514
8515         if (err_code != NULL)
8516                 *err_code = error;
8517
8518         EM_DEBUG_FUNC_END("ret [%d]", ret);
8519         return ret;
8520 }
8521
8522 INTERNAL_FUNC int emstorage_mail_search_result(DB_STMT search_handle, emstorage_mail_field_type_t type, void** data, int transaction, int *err_code)
8523 {
8524         EM_DEBUG_FUNC_BEGIN("search_handle[%d], type[%d], data[%p], transaction[%d], err_code[%p]", search_handle, type, data, transaction, err_code);
8525
8526         if (search_handle == 0 || !data) {
8527                 EM_DEBUG_EXCEPTION(" search_handle[%d], type[%d], data[%p]", search_handle, type, data);
8528
8529                 if (err_code != NULL)
8530                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8531                 return false;
8532         }
8533
8534         emstorage_mail_tbl_t* p_data_tbl = NULL;
8535         DB_STMT hStmt = search_handle;
8536         int rc, ret = false;
8537         int error = EMAIL_ERROR_NONE;
8538
8539         switch (type) {
8540         case RETRIEVE_ID:
8541                 _get_stmt_field_data_int(hStmt, (int *)data, MAIL_ID_IDX_IN_MAIL_TBL);
8542                 break;
8543
8544         case RETRIEVE_ENVELOPE:
8545         case RETRIEVE_ALL:
8546                 if (!(p_data_tbl = em_malloc(sizeof(emstorage_mail_tbl_t)))) {
8547                         EM_DEBUG_EXCEPTION(" em_mallocfailed...");
8548                         error = EMAIL_ERROR_OUT_OF_MEMORY;
8549                         goto FINISH_OFF;
8550                 }
8551
8552                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8553                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8554                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_size), MAIL_SIZE_IDX_IN_MAIL_TBL);
8555                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8556                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_from), 1, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL);
8557                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_to), 1, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL);
8558                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject), 1, SUBJECT_IDX_IN_MAIL_TBL);
8559                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->body_download_status), BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL);
8560                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL);
8561                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8562                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8563                 _get_stmt_field_data_time_t(hStmt, &(p_data_tbl->date_time), DATETIME_IDX_IN_MAIL_TBL);
8564                 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL);
8565                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->DRM_status), DRM_STATUS_IDX_IN_MAIL_TBL);
8566                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->priority), PRIORITY_IDX_IN_MAIL_TBL);
8567                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), SAVE_STATUS_IDX_IN_MAIL_TBL);
8568                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), LOCK_STATUS_IDX_IN_MAIL_TBL);
8569                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->report_status), REPORT_STATUS_IDX_IN_MAIL_TBL);
8570                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8571                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->meeting_request_status), MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL);
8572                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->message_class), MESSAGE_CLASS_IDX_IN_MAIL_TBL);
8573                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->digest_type), DIGEST_TYPE_IDX_IN_MAIL_TBL);
8574                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->smime_type), SMIME_TYPE_IDX_IN_MAIL_TBL);
8575                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->scheduled_sending_time), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8576                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->remaining_resend_times), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8577                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->tag_id), TAG_ID_IDX_IN_MAIL_TBL);
8578                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->eas_data_length), EAS_DATA_LENGTH_IDX_IN_MAIL_TBL);
8579                 _get_stmt_field_data_blob(hStmt, (void**)&(p_data_tbl->eas_data), EAS_DATA_IDX_IN_MAIL_TBL);
8580
8581                 if (type == RETRIEVE_ALL) {
8582                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8583                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8584                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_reply), 1, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL);
8585                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_cc), 1, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL);
8586                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_bcc), 1, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL);
8587                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_return), 1, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL);
8588                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->message_id), 0, MESSAGE_ID_IDX_IN_MAIL_TBL);
8589                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8590                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8591                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_count), ATTACHMENT_COUNT_IDX_IN_MAIL_TBL);
8592                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8593                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->replied_time), REPLIED_TIME_IDX_IN_MAIL_TBL);
8594                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->forwarded_time), FORWARDED_TIME_IDX_IN_MAIL_TBL);
8595                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->default_charset), 0, DEFAULT_CHARSET_IDX_IN_MAIL_TBL);
8596                 }
8597
8598                 if (p_data_tbl->body_download_status) {
8599                         struct stat buf;
8600
8601                         if (p_data_tbl->file_path_html) {
8602                                 if (stat(p_data_tbl->file_path_html, &buf) == -1)
8603                                         p_data_tbl->body_download_status = 0;
8604                         } else if (p_data_tbl->file_path_plain) {
8605                                 if (stat(p_data_tbl->file_path_plain, &buf) == -1)
8606                                         p_data_tbl->body_download_status = 0;
8607                         } else
8608                                 p_data_tbl->body_download_status = 0;
8609                 }
8610
8611                 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8612                 break;
8613
8614         case RETRIEVE_SUMMARY:
8615                 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
8616                         EM_DEBUG_EXCEPTION(" malloc failed...");
8617
8618                         error = EMAIL_ERROR_OUT_OF_MEMORY;
8619                         goto FINISH_OFF;
8620                 }
8621
8622                 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8623
8624                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8625                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8626                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8627                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8628                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8629
8630                 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8631                 break;
8632
8633         case RETRIEVE_ADDRESS:
8634                 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
8635                         EM_DEBUG_EXCEPTION(" malloc failed...");
8636                         error = EMAIL_ERROR_OUT_OF_MEMORY;
8637                         goto FINISH_OFF;
8638                 }
8639
8640                 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8641                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8642                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8643                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8644                 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8645                 break;
8646
8647         default:
8648                 break;
8649         }
8650
8651         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8652         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8653                         ("sqlite3_step fail:%d", rc));
8654
8655         ret = true;
8656
8657 FINISH_OFF:
8658
8659         if (err_code != NULL)
8660                 *err_code = error;
8661
8662         if (ret == false && p_data_tbl)
8663                 emstorage_free_mail(&p_data_tbl, 1, NULL);
8664
8665         EM_DEBUG_FUNC_END("ret [%d]", ret);
8666         return ret;
8667 }
8668
8669 INTERNAL_FUNC int emstorage_mail_search_end(DB_STMT search_handle, int transaction, int *err_code)
8670 {
8671         EM_DEBUG_FUNC_BEGIN("search_handle[%d], transaction[%d], err_code[%p]", search_handle, transaction, err_code);
8672
8673         int error = EMAIL_ERROR_NONE;
8674         int rc, ret = false;
8675
8676         if (search_handle == 0) {
8677                 EM_DEBUG_EXCEPTION(" search_handle[%d]", search_handle);
8678                 error = EMAIL_ERROR_INVALID_PARAM;
8679                 goto FINISH_OFF;
8680         }
8681
8682         DB_STMT hStmt = search_handle;
8683
8684         rc = sqlite3_finalize(hStmt);
8685         if (rc != SQLITE_OK) {
8686                 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8687                 error = EMAIL_ERROR_DB_FAILURE;
8688         }
8689
8690         ret = true;
8691
8692 FINISH_OFF:
8693         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8694
8695         if (err_code != NULL)
8696                 *err_code = error;
8697
8698         EM_DEBUG_FUNC_END("ret [%d]", ret);
8699         return ret;
8700 }
8701
8702 INTERNAL_FUNC int emstorage_change_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t *mail, int transaction, int *err_code)
8703 {
8704         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8705
8706         if (mail_id <= 0 || !mail) {
8707                 EM_DEBUG_EXCEPTION(" mail_id[%d], mail[%p]", mail_id, mail);
8708
8709                 if (err_code != NULL)
8710                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8711                 return false;
8712         }
8713
8714         DB_STMT hStmt = NULL;
8715         char sql_query_string[QUERY_SIZE] = {0, };
8716         int rc = -1;
8717         int ret = false;
8718         int error = EMAIL_ERROR_NONE;
8719         int i = 0;
8720         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8721         char mailbox_id_param_string[10] = {0,};
8722
8723         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
8724
8725         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8726                         "UPDATE mail_tbl SET"
8727                         "  mail_id = ?"
8728                         ", account_id = ?"
8729                         ", mailbox_id = ?"
8730                         ", mail_size = ?"
8731                         ", server_mail_status = ?"
8732                         ", server_mailbox_name = ?"
8733                         ", server_mail_id = ?"
8734                         ", reference_mail_id = ?"
8735                         ", full_address_from = ?"
8736                         ", full_address_reply = ?"  /* 10 */
8737                         ", full_address_to = ?"
8738                         ", full_address_cc = ?"
8739                         ", full_address_bcc = ?"
8740                         ", full_address_return = ?"
8741                         ", subject = ?"
8742                         ", body_download_status = ?"
8743                         ", file_path_plain = ?"
8744                         ", file_path_html = ?"
8745                         ", file_path_mime_entity = ?"
8746                         ", date_time = ?"
8747                         ", flags_seen_field      = ?"
8748                         ", flags_deleted_field   = ?"
8749                         ", flags_flagged_field   = ?"
8750                         ", flags_answered_field  = ?"
8751                         ", flags_recent_field    = ?"
8752                         ", flags_draft_field     = ?"
8753                         ", flags_forwarded_field = ?"
8754                         ", DRM_status = ?"
8755                         ", priority = ?"
8756                         ", save_status = ?"
8757                         ", lock_status = ?"
8758                         ", message_id = ?"
8759                         ", report_status = ?"
8760                         ", preview_text = ?"
8761                         ", smime_type = ?"
8762                         ", scheduled_sending_time = ?"
8763                         ", remaining_resend_times = ?"
8764                         ", tag_id = ?"
8765                         ", replied_time = ?"
8766                         ", forwarded_time = ?"
8767                         ", default_charset = ?"
8768                         ", eas_data_length = ?"
8769                         ", eas_data = ?"
8770                         " WHERE mail_id = %d AND account_id != 0 "
8771                         , mail_id);
8772
8773
8774         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8775         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8776                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8777
8778         _bind_stmt_field_data_int(hStmt, i++, mail->mail_id);
8779         _bind_stmt_field_data_int(hStmt, i++, mail->account_id);
8780         _bind_stmt_field_data_int(hStmt, i++, mail->mailbox_id);
8781         _bind_stmt_field_data_int(hStmt, i++, mail->mail_size);
8782         _bind_stmt_field_data_int(hStmt, i++, mail->server_mail_status);
8783         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mailbox_name, 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
8784         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mail_id, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
8785         _bind_stmt_field_data_int(hStmt, i++, mail->reference_mail_id);
8786         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
8787         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
8788         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
8789         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
8790         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
8791         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
8792         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8793         _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
8794         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8795         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8796         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
8797         _bind_stmt_field_data_int(hStmt, i++, mail->date_time);
8798         _bind_stmt_field_data_char(hStmt, i++, mail->flags_seen_field);
8799         _bind_stmt_field_data_char(hStmt, i++, mail->flags_deleted_field);
8800         _bind_stmt_field_data_char(hStmt, i++, mail->flags_flagged_field);
8801         _bind_stmt_field_data_char(hStmt, i++, mail->flags_answered_field);
8802         _bind_stmt_field_data_char(hStmt, i++, mail->flags_recent_field);
8803         _bind_stmt_field_data_char(hStmt, i++, mail->flags_draft_field);
8804         _bind_stmt_field_data_char(hStmt, i++, mail->flags_forwarded_field);
8805         _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
8806         _bind_stmt_field_data_int(hStmt, i++, mail->priority);
8807         _bind_stmt_field_data_int(hStmt, i++, mail->save_status);
8808         _bind_stmt_field_data_int(hStmt, i++, mail->lock_status);
8809         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->message_id, 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
8810         _bind_stmt_field_data_int(hStmt, i++, mail->report_status);
8811         _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8812         _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
8813         _bind_stmt_field_data_int(hStmt, i++, mail->scheduled_sending_time);
8814         _bind_stmt_field_data_int(hStmt, i++, mail->remaining_resend_times);
8815         _bind_stmt_field_data_int(hStmt, i++, mail->tag_id);
8816         _bind_stmt_field_data_int(hStmt, i++, mail->replied_time);
8817         _bind_stmt_field_data_int(hStmt, i++, mail->forwarded_time);
8818         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
8819         _bind_stmt_field_data_int(hStmt, i++, mail->eas_data_length);
8820         _bind_stmt_field_data_blob(hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
8821
8822         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8823         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8824                         ("sqlite3_step fail:%d", rc));
8825
8826         rc = sqlite3_changes(local_db_handle);
8827         if (rc == 0) {
8828                 EM_DEBUG_LOG(" no matched mail found...");
8829                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8830                 goto FINISH_OFF;
8831         }
8832
8833         ret = true;
8834
8835 FINISH_OFF:
8836         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
8837         if (hStmt != NULL) {
8838                 rc = sqlite3_finalize(hStmt);
8839                 if (rc != SQLITE_OK) {
8840                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8841                         error = EMAIL_ERROR_DB_FAILURE;
8842                 }
8843         }
8844
8845
8846         if (error == EMAIL_ERROR_NONE && mail) {
8847                 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
8848                 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail->mail_id, mailbox_id_param_string, 0))
8849                         EM_DEBUG_EXCEPTION("emcore_notify_storage_eventfailed [NOTI_MAIL_UPDATE]");
8850         }
8851
8852         if (err_code != NULL)
8853                 *err_code = error;
8854
8855         EM_DEBUG_FUNC_END("ret [%d]", ret);
8856         return ret;
8857 }
8858
8859 /**
8860  *  emstorage_clean_save_status(int save_status, int  *err_code) - set the all mail status to the set value
8861  *
8862  *
8863  **/
8864 INTERNAL_FUNC int emstorage_clean_save_status(char *multi_user_name, int save_status, int  *err_code)
8865 {
8866         EM_DEBUG_FUNC_BEGIN("save_status[%d], err_code[%p]", save_status, err_code);
8867
8868         EM_IF_NULL_RETURN_VALUE(err_code, false);
8869
8870         int ret = false;
8871         int error = EMAIL_ERROR_NONE;
8872         int rc = 0;
8873         char sql_query_string[QUERY_SIZE] = {0, };
8874         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8875
8876         memset(sql_query_string, 0x00, sizeof(sql_query_string));
8877         SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET save_status = %d WHERE save_status = %d", save_status, EMAIL_MAIL_STATUS_SENDING);
8878
8879         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
8880         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
8881         if (error != EMAIL_ERROR_NONE) {
8882                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
8883                 goto FINISH_OFF;
8884         }
8885
8886         rc = sqlite3_changes(local_db_handle);
8887         if (rc == 0) {
8888                 EM_DEBUG_LOG(" No Matched Mail Exists ");
8889                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8890         }
8891
8892         ret = true;
8893
8894 FINISH_OFF:
8895
8896         if (err_code != NULL)
8897                 *err_code = error;
8898
8899         EM_DEBUG_FUNC_END("ret [%d]", ret);
8900         return ret;
8901 }
8902
8903 INTERNAL_FUNC int emstorage_set_field_of_mails_with_integer_value(char *multi_user_name, int account_id, int mail_ids[], int mail_ids_count, char *field_name, int value, int transaction, int *err_code)
8904 {
8905         EM_DEBUG_FUNC_BEGIN_SEC("account_id [%d], mail_ids[%p], mail_ids_count[%d], field_name[%s], value[%d], transaction[%d], err_code[%p]", account_id, mail_ids, mail_ids_count, field_name, value, transaction, err_code);
8906         int i = 0;
8907         int error = EMAIL_ERROR_NONE;
8908         int ret = false;
8909         int query_size = 0;
8910         int cur_mail_id_string = 0;
8911         int mail_id_string_buffer_length = 0;
8912         int parameter_string_length = 0;
8913         char  *sql_query_string = NULL;
8914         char *mail_id_string_buffer = NULL;
8915         char *parameter_string = NULL;
8916         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8917         email_mail_attribute_type target_mail_attribute_type = 0;
8918
8919         if (!mail_ids  || !field_name || account_id == 0) {
8920                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8921                 if (err_code != NULL)
8922                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8923                 return false;
8924         }
8925
8926         if ((error = emcore_get_attribute_type_by_mail_field_name(field_name, &target_mail_attribute_type)) != EMAIL_ERROR_NONE) {
8927                 EM_DEBUG_EXCEPTION("emstorageemcore_get_attribute_type_by_mail_field_name failed [%d]", error);
8928                 if (err_code != NULL)
8929                         *err_code = error;
8930                 return false;
8931         }
8932
8933         /* Generating mail id list string */
8934         mail_id_string_buffer_length = MAIL_ID_STRING_LENGTH * mail_ids_count;
8935
8936         mail_id_string_buffer = em_malloc(mail_id_string_buffer_length);
8937
8938         if (!mail_id_string_buffer) {
8939                 EM_DEBUG_EXCEPTION("em_mallocfailed");
8940                 if (err_code != NULL)
8941                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8942                 return false;
8943         }
8944
8945         for (i = 0; i < mail_ids_count; i++)
8946                 cur_mail_id_string += SNPRINTF_OFFSET(mail_id_string_buffer, cur_mail_id_string, mail_id_string_buffer_length, "%d,", mail_ids[i]);
8947
8948         if (EM_SAFE_STRLEN(mail_id_string_buffer) > 1)
8949                 mail_id_string_buffer[EM_SAFE_STRLEN(mail_id_string_buffer) - 1] = NULL_CHAR;
8950
8951         /* Generating notification parameter string */
8952         parameter_string_length = mail_id_string_buffer_length + EM_SAFE_STRLEN(field_name) + 2;
8953         parameter_string = em_malloc(parameter_string_length);
8954
8955         if (!parameter_string) {
8956                 EM_DEBUG_EXCEPTION("em_mallocfailed");
8957                 if (err_code != NULL)
8958                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8959                 EM_SAFE_FREE(mail_id_string_buffer);
8960                 return false;
8961         }
8962
8963         SNPRINTF(parameter_string, parameter_string_length, "%s%c%s", field_name, 0x01, mail_id_string_buffer);
8964         query_size = EM_SAFE_STRLEN(mail_id_string_buffer) + EM_SAFE_STRLEN(field_name) + 250;
8965
8966         sql_query_string = em_malloc(query_size);
8967         if (sql_query_string == NULL) {
8968                 EM_DEBUG_EXCEPTION("em_mallocfailed");
8969                 error = EMAIL_ERROR_OUT_OF_MEMORY;
8970                 goto FINISH_OFF;
8971         }
8972         /* Write query string */
8973         SNPRINTF(sql_query_string, query_size, "UPDATE mail_tbl SET %s = %d WHERE mail_id in (%s) AND account_id = %d", field_name, value, mail_id_string_buffer, account_id);
8974
8975         EM_DEBUG_LOG_DEV("sql_query_string [%s]", sql_query_string);
8976
8977         /* Execute query */
8978         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
8979         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
8980         if (error != EMAIL_ERROR_NONE) {
8981                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
8982                 goto FINISH_OFF;
8983         }
8984
8985         if (sqlite3_changes(local_db_handle) == 0)
8986                 EM_DEBUG_LOG("no mail matched...");
8987
8988         ret = true;
8989
8990 FINISH_OFF:
8991         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
8992
8993         if (error == EMAIL_ERROR_NONE && parameter_string) {
8994                 if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, target_mail_attribute_type, parameter_string, value))
8995                         EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_MAIL_FIELD_UPDATE [%s,%d]", field_name, value);
8996         }
8997
8998         EM_SAFE_FREE(mail_id_string_buffer);
8999         EM_SAFE_FREE(parameter_string);
9000         EM_SAFE_FREE(sql_query_string);
9001
9002
9003         if (err_code != NULL)
9004                 *err_code = error;
9005
9006         EM_DEBUG_FUNC_END("error [%d]", error);
9007         return ret;
9008 }
9009
9010 #ifdef __FEATURE_BODY_SEARCH__
9011 INTERNAL_FUNC int emstorage_change_mail_text_field(char *multi_user_name, int mail_id, emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
9012 {
9013         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
9014
9015         if (mail_id <= 0 || !mail_text) {
9016                 EM_DEBUG_EXCEPTION(" mail_id[%d], mail_text[%p]", mail_id, mail_text);
9017                 if (err_code != NULL)
9018                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9019                 return false;
9020         }
9021
9022         int ret = false;
9023         int error = EMAIL_ERROR_NONE;
9024         DB_STMT hStmt = NULL;
9025         char sql_query_string[QUERY_SIZE] = {0, };
9026
9027         int i = 0;
9028         int rc = 0;
9029
9030         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9031         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9032
9033         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9034                         "UPDATE mail_text_tbl SET"
9035                         " body_text = ?"
9036                         " WHERE mail_id = %d AND account_id != 0"
9037                         , mail_id);
9038         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9039
9040         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9041         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9042                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9043
9044         i = 0;
9045         _bind_stmt_field_data_string(hStmt, i++, (char *)mail_text->body_text, 0, -1);
9046
9047         if (hStmt != NULL) {
9048                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9049                 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9050                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9051
9052                 rc = sqlite3_changes(local_db_handle);
9053                 if (rc == 0) {
9054                         EM_DEBUG_LOG(" no matched mail found...");
9055                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
9056                         goto FINISH_OFF;
9057                 }
9058         }
9059
9060         ret = true;
9061
9062 FINISH_OFF:
9063         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9064
9065         if (hStmt != NULL) {
9066                 rc = sqlite3_finalize(hStmt);
9067                 if (rc != SQLITE_OK) {
9068                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9069                         error = EMAIL_ERROR_DB_FAILURE;
9070                 }
9071                 hStmt = NULL;
9072         }
9073
9074         if (err_code != NULL)
9075                 *err_code = error;
9076
9077         EM_DEBUG_FUNC_END("ret [%d]", ret);
9078         return ret;
9079 }
9080 #endif
9081
9082 INTERNAL_FUNC int emstorage_change_mail_field(char *multi_user_name, int mail_id, email_mail_change_type_t type, emstorage_mail_tbl_t *mail, int transaction, int *err_code)
9083 {
9084         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
9085
9086         int ret = false;
9087         int error = EMAIL_ERROR_NONE;
9088         int move_flag = 0;
9089         int rc = 0;
9090         int i = 0;
9091         int mailbox_id = 0;
9092         DB_STMT hStmt = NULL;
9093         char sql_query_string[QUERY_SIZE] = {0, };
9094         char mailbox_id_param_string[10] = {0,};
9095         sqlite3 *local_db_handle = NULL;
9096
9097         if (mail_id <= 0 || !mail) {
9098                 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], mail[%p]", mail_id, type, mail);
9099                 if (err_code != NULL)
9100                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9101                 return false;
9102         }
9103
9104         local_db_handle = emstorage_get_db_connection(multi_user_name);
9105
9106         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9107
9108         switch (type) {
9109         case APPEND_BODY:
9110                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9111                                 "UPDATE mail_tbl SET"
9112                                 "  body_download_status = ?"
9113                                 ", file_path_plain = ?"
9114                                 ", file_path_html = ?"
9115                                 ", file_path_mime_entity = ?"
9116                                 ", flags_seen_field      = ?"
9117                                 ", flags_deleted_field   = ?"
9118                                 ", flags_flagged_field   = ?"
9119                                 ", flags_answered_field  = ?"
9120                                 ", flags_recent_field    = ?"
9121                                 ", flags_draft_field     = ?"
9122                                 ", flags_forwarded_field = ?"
9123                                 ", DRM_status = ?"
9124                                 ", attachment_count = ?"
9125                                 ", preview_text= ?"
9126                                 ", meeting_request_status = ? "
9127                                 ", message_class = ? "
9128                                 ", digest_type = ? "
9129                                 ", smime_type = ? "
9130                                 " WHERE mail_id = %d AND account_id != 0"
9131                                 , mail_id);
9132
9133
9134                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9135                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9136                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9137                 i = 0;
9138
9139                 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9140                 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9141                 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9142                 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9143                 _bind_stmt_field_data_int(hStmt, i++, mail->flags_seen_field);
9144                 _bind_stmt_field_data_int(hStmt, i++, mail->flags_deleted_field);
9145                 _bind_stmt_field_data_int(hStmt, i++, mail->flags_flagged_field);
9146                 _bind_stmt_field_data_int(hStmt, i++, mail->flags_answered_field);
9147                 _bind_stmt_field_data_int(hStmt, i++, mail->flags_recent_field);
9148                 _bind_stmt_field_data_int(hStmt, i++, mail->flags_draft_field);
9149                 _bind_stmt_field_data_int(hStmt, i++, mail->flags_forwarded_field);
9150                 _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
9151                 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9152                 _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9153                 _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
9154                 _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
9155                 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9156                 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9157                 break;
9158
9159         case UPDATE_MAILBOX: {
9160                                                          int err;
9161                                                          emstorage_mailbox_tbl_t *mailbox_tbl;
9162
9163                                                          if ((err = emstorage_get_mailbox_by_id(multi_user_name, mail->mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
9164                                                                  EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_id failed [%d]", err);
9165                                                                  goto FINISH_OFF;
9166                                                          }
9167
9168                                                          SNPRINTF(sql_query_string, sizeof(sql_query_string),
9169                                                                          "UPDATE mail_tbl SET"
9170                                                                          " mailbox_id = '%d'"
9171                                                                          ",mailbox_type = '%d'"
9172                                                                          " WHERE mail_id = %d AND account_id != 0"
9173                                                                          , mailbox_tbl->mailbox_id
9174                                                                          , mailbox_tbl->mailbox_type
9175                                                                          , mail_id);
9176                                                          move_flag = 1;
9177
9178
9179                                                          EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9180                                                          EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9181                                                                          ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9182
9183                                                          i = 0;
9184                                                          _bind_stmt_field_data_string(hStmt, i++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
9185
9186                                                          emstorage_free_mailbox(&mailbox_tbl, 1, NULL); /*prevent 26251*/
9187
9188                                                  }
9189                                                  break;
9190
9191         case UPDATE_FLAG:
9192                                                  SNPRINTF(sql_query_string, sizeof(sql_query_string),
9193                                                                  "UPDATE mail_tbl SET"
9194                                                                  " flags_seen_field      = %d"
9195                                                                  ",flags_deleted_field   = %d"
9196                                                                  ",flags_flagged_field   = %d"
9197                                                                  ",flags_answered_field  = %d"
9198                                                                  ",flags_recent_field    = %d"
9199                                                                  ",flags_draft_field     = %d"
9200                                                                  ",flags_forwarded_field = %d"
9201                                                                  "  WHERE mail_id = %d AND account_id != 0"
9202                                                                  , mail->flags_seen_field
9203                                                                  , mail->flags_deleted_field
9204                                                                  , mail->flags_flagged_field
9205                                                                  , mail->flags_answered_field
9206                                                                  , mail->flags_recent_field
9207                                                                  , mail->flags_draft_field
9208                                                                  , mail->flags_forwarded_field
9209                                                                  , mail_id);
9210                                                  EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9211
9212
9213                                                  EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9214                                                  EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9215                                                                  ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9216
9217                                                  break;
9218
9219         case UPDATE_EXTRA_FLAG:
9220                                                  SNPRINTF(sql_query_string, sizeof(sql_query_string),
9221                                                                  "UPDATE mail_tbl SET"
9222                                                                  "  priority = %d"
9223                                                                  ", save_status = %d"
9224                                                                  ", lock_status = %d"
9225                                                                  ", report_status = %d"
9226                                                                  ", DRM_status = %d"
9227                                                                  " WHERE mail_id = %d AND account_id != 0"
9228                                                                  , mail->priority
9229                                                                  , mail->save_status
9230                                                                  , mail->lock_status
9231                                                                  , mail->report_status
9232                                                                  , mail->DRM_status
9233                                                                  , mail_id);
9234                                                  EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9235
9236
9237                                                  EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9238                                                  EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9239                                                                  ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9240                                                  break;
9241
9242         case UPDATE_STICKY_EXTRA_FLAG:
9243                                                  SNPRINTF(sql_query_string, sizeof(sql_query_string),
9244                                                                  "UPDATE mail_tbl SET"
9245                                                                  "  lock_status = %d"
9246                                                                  "  WHERE mail_id = %d AND account_id != 0"
9247                                                                  , mail->lock_status
9248                                                                  , mail_id);
9249                                                  EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9250
9251
9252                                                  EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9253                                                  EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9254                                                                  ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9255                                                  break;
9256
9257         case UPDATE_MAIL:
9258                                                  SNPRINTF(sql_query_string, sizeof(sql_query_string),
9259                                                                  "UPDATE mail_tbl SET"
9260                                                                  "  full_address_from = ?"
9261                                                                  ", full_address_reply = ?"
9262                                                                  ", full_address_to = ?"
9263                                                                  ", full_address_cc = ?"
9264                                                                  ", full_address_bcc = ?"
9265                                                                  ", full_address_return = ?"
9266                                                                  ", subject = ?"
9267                                                                  ", file_path_plain = ?"
9268                                                                  ", date_time = ?"
9269                                                                  ", flags_seen_field = ?"
9270                                                                  ", flags_deleted_field = ?"
9271                                                                  ", flags_flagged_field = ?"
9272                                                                  ", flags_answered_field = ?"
9273                                                                  ", flags_recent_field = ?"
9274                                                                  ", flags_draft_field = ?"
9275                                                                  ", flags_forwarded_field = ?"
9276                                                                  ", priority = ?"
9277                                                                  ", save_status = ?"
9278                                                                  ", lock_status = ?"
9279                                                                  ", report_status = ?"
9280                                                                  ", DRM_status = ?"
9281                                                                  ", file_path_html = ?"
9282                                                                  ", file_path_mime_entity = ?"
9283                                                                  ", mail_size = ?"
9284                                                                  ", preview_text = ?"
9285                                                                  ", body_download_status = ?"
9286                                                                  ", attachment_count = ?"
9287                                                                  ", inline_content_count = ?"
9288                                                                  ", meeting_request_status = ?"
9289                                                                  ", message_class = ?"
9290                                                                  ", digest_type = ?"
9291                                                                  ", smime_type = ?"
9292                                                                  ", scheduled_sending_time = ?"
9293                                                                  ", remaining_resend_times = ?"
9294                                                                  ", tag_id = ?"
9295                                                                  ", replied_time = ?"
9296                                                                  ", forwarded_time = ?"
9297                                                                  ", default_charset = ?"
9298                                                                  ", eas_data_length = ?"
9299                                                                  ", eas_data = ?"
9300                                                                  " WHERE mail_id = %d AND account_id != 0"
9301                                                                  , mail_id);
9302
9303
9304                                                  EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9305                                                  EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
9306                                                  EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9307                                                                  ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9308                                                  i = 0;
9309                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
9310                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
9311                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
9312                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
9313                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
9314                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
9315                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9316                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9317                                                  _bind_stmt_field_data_time_t(hStmt, i++, mail->date_time);
9318                                                  _bind_stmt_field_data_char(hStmt, i++, mail->flags_seen_field);
9319                                                  _bind_stmt_field_data_char(hStmt, i++, mail->flags_deleted_field);
9320                                                  _bind_stmt_field_data_char(hStmt, i++, mail->flags_flagged_field);
9321                                                  _bind_stmt_field_data_char(hStmt, i++, mail->flags_answered_field);
9322                                                  _bind_stmt_field_data_char(hStmt, i++, mail->flags_recent_field);
9323                                                  _bind_stmt_field_data_char(hStmt, i++, mail->flags_draft_field);
9324                                                  _bind_stmt_field_data_char(hStmt, i++, mail->flags_forwarded_field);
9325                                                  _bind_stmt_field_data_int(hStmt, i++, mail->priority);
9326                                                  _bind_stmt_field_data_int(hStmt, i++, mail->save_status);
9327                                                  _bind_stmt_field_data_int(hStmt, i++, mail->lock_status);
9328                                                  _bind_stmt_field_data_int(hStmt, i++, mail->report_status);
9329                                                  _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
9330                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9331                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9332                                                  _bind_stmt_field_data_int(hStmt, i++, mail->mail_size);
9333                                                  _bind_stmt_field_data_nstring(hStmt, i++, (char*)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9334                                                  _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9335                                                  _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9336                                                  _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
9337                                                  _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
9338                                                  _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
9339                                                  _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9340                                                  _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9341                                                  _bind_stmt_field_data_int(hStmt, i++, mail->scheduled_sending_time);
9342                                                  _bind_stmt_field_data_int(hStmt, i++, mail->remaining_resend_times);
9343                                                  _bind_stmt_field_data_int(hStmt, i++, mail->tag_id);
9344                                                  _bind_stmt_field_data_int(hStmt, i++, mail->replied_time);
9345                                                  _bind_stmt_field_data_int(hStmt, i++, mail->forwarded_time);
9346                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
9347                                                  _bind_stmt_field_data_int(hStmt, i++, mail->eas_data_length);
9348                                                  _bind_stmt_field_data_blob(hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
9349                                                  break;
9350
9351         case UPDATE_DATETIME: {
9352                                                           SNPRINTF(sql_query_string, sizeof(sql_query_string),
9353                                                                           "UPDATE mail_tbl SET"
9354                                                                           " date_time = '%ld'"
9355                                                                           " WHERE mail_id = %d AND account_id != 0"
9356                                                                           , mail->date_time
9357                                                                           , mail_id);
9358
9359                                                           EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9360                                                           EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9361                                                                           ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9362                                                           break;
9363                                                   }
9364
9365         case UPDATE_FROM_CONTACT_INFO:
9366                                                   EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_FROM_CONTACT_INFO");
9367                                                   SNPRINTF(sql_query_string, sizeof(sql_query_string),
9368                                                                   "UPDATE mail_tbl SET"
9369                                                                   " email_address_sender = ?,"
9370                                                                   " WHERE mail_id = %d",
9371                                                                   mail_id);
9372
9373                                                   hStmt = NULL;
9374
9375                                                   EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9376                                                   EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9377                                                                   ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9378                                                   i = 0;
9379                                                   _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9380                                                   break;
9381
9382         case UPDATE_TO_CONTACT_INFO:
9383                                                   EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_TO_CONTACT_INFO");
9384                                                   SNPRINTF(sql_query_string, sizeof(sql_query_string),
9385                                                                   "UPDATE mail_tbl SET"
9386                                                                   " email_address_recipient = ?,"
9387                                                                   " WHERE mail_id = %d",
9388                                                                   mail_id);
9389
9390                                                   hStmt = NULL;
9391
9392                                                   EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9393                                                   EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9394                                                                   ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9395                                                   i = 0;
9396                                                   _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9397                                                   break;
9398
9399         case UPDATE_ALL_CONTACT_INFO:
9400                                                   EM_DEBUG_LOG("emstorage_change_mail_field - mail change type is UPDATE_ALL_CONTACT_INFO");
9401                                                   SNPRINTF(sql_query_string, sizeof(sql_query_string),
9402                                                                   "UPDATE mail_tbl SET"
9403                                                                   " email_address_sender = ?,"
9404                                                                   " email_address_recipient = ?,"
9405                                                                   " WHERE mail_id = %d",
9406                                                                   mail_id);
9407
9408                                                   hStmt = NULL;
9409
9410                                                   EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9411                                                   EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9412                                                                   ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9413                                                   i = 0;
9414                                                   _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9415                                                   _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9416                                                   break;
9417
9418
9419 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
9420         case UPDATE_PARTIAL_BODY_DOWNLOAD:
9421
9422                                                   SNPRINTF(sql_query_string, sizeof(sql_query_string),
9423                                                                   "UPDATE mail_tbl SET"
9424                                                                   "  body_download_status = ?"
9425                                                                   ", file_path_plain = ?"
9426                                                                   ", file_path_html = ?"
9427                                                                   ", file_path_mime_entity = ?"
9428                                                                   ", attachment_count = ?"
9429                                                                   ", inline_content_count = ?"
9430                                                                   ", preview_text = ?"
9431                                                                   ", digest_type = ?"
9432                                                                   ", smime_type = ?"
9433                                                                   " WHERE mail_id = %d"
9434                                                                   , mail_id);
9435
9436
9437                                                   EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9438                                                   EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9439                                                                   ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9440                                                   i = 0;
9441
9442                                                   _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9443                                                   _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9444                                                   _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html,  0, TEXT_2_LEN_IN_MAIL_TBL);
9445                                                   _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9446                                                   _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9447                                                   _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
9448                                                   _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text,    0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9449                                                   _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9450                                                   _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9451
9452                                                   break;
9453
9454 #endif
9455         case UPDATE_FILE_PATH:
9456                                                   SNPRINTF(sql_query_string, sizeof(sql_query_string),
9457                                                                   "UPDATE mail_tbl SET"
9458                                                                   ", file_path_plain = ?"
9459                                                                   ", file_path_html = ?"
9460                                                                   ", file_path_mime_entity = ?"
9461                                                                   " WHERE mail_id = %d"
9462                                                                   , mail_id);
9463
9464
9465                                                   EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9466                                                   EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
9467                                                   EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9468                                                                   ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9469                                                   i = 0;
9470                                                   _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9471                                                   _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9472                                                   _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9473                                                   break;
9474
9475         default:
9476                                                   EM_DEBUG_LOG(" type[%d]", type);
9477
9478                                                   error = EMAIL_ERROR_INVALID_PARAM;
9479                                                   goto FINISH_OFF;
9480         }
9481
9482         if (hStmt != NULL) {
9483
9484                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9485                 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
9486                                 ("sqlite3_step fail:%d", rc));
9487                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9488                                 ("sqlite3_step fail:%d", rc));
9489                 rc = sqlite3_changes(local_db_handle);
9490                 if (rc == 0) {
9491                         EM_DEBUG_LOG(" no matched mail found...");
9492                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
9493                         goto FINISH_OFF;
9494                 }
9495         }
9496
9497         if (mail->account_id == 0) {
9498                 emstorage_mail_tbl_t* mail_for_account_tbl = NULL;
9499                 if (!emstorage_get_mail_field_by_id(multi_user_name, mail_id, RETRIEVE_ACCOUNT, &mail_for_account_tbl, true, &error) || !mail_for_account_tbl) {
9500                         EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id error [%d]", error);
9501                         goto FINISH_OFF;
9502                 }
9503                 mail->account_id = mail_for_account_tbl->account_id;
9504                 if (mail_for_account_tbl)
9505                         emstorage_free_mail(&mail_for_account_tbl, 1, NULL);
9506         }
9507
9508         ret = true;
9509
9510 FINISH_OFF:
9511         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9512
9513         if (hStmt != NULL) {
9514                 rc = sqlite3_finalize(hStmt);
9515                 if (rc != SQLITE_OK) {
9516                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9517                         error = EMAIL_ERROR_DB_FAILURE;
9518                 }
9519                 hStmt = NULL;
9520         }
9521
9522         if (error == EMAIL_ERROR_NONE &&  move_flag != 1 && transaction) {
9523                 if (!emstorage_get_mailbox_id_by_mailbox_type(multi_user_name, mail->account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &mailbox_id, false, &error))
9524                         EM_DEBUG_EXCEPTION("emstorage_get_mailbox_id_by_mailbox_type error [%d]", error);
9525
9526                 if (mail->mailbox_id == mailbox_id) {
9527                         SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
9528                         if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, mailbox_id_param_string, type))
9529                                 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventerror [ NOTI_MAIL_UPDATE ] >>>> ");
9530                 } else {
9531                         /* h.gahlaut@samsung.com: Jan 10, 2011 Publishing noti to refresh outbox when email sending status changes */
9532                         if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, NULL, type))
9533                                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventerror [ NOTI_MAIL_UPDATE ]");
9534                 }
9535         }
9536
9537         if (err_code != NULL)
9538                 *err_code = error;
9539
9540         EM_DEBUG_FUNC_END("ret [%d]", ret);
9541         return ret;
9542 }
9543
9544 INTERNAL_FUNC int emstorage_increase_mail_id(char *multi_user_name, int *mail_id, int transaction, int *err_code)
9545 {
9546         EM_DEBUG_FUNC_BEGIN("mail_id[%p], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9547
9548         int rc, ret = false;
9549         int error = EMAIL_ERROR_NONE;
9550         int latest_mail_id = 0;
9551         sqlite3 *local_db_handle = NULL;
9552         char *sql = "SELECT MAX(mail_id) FROM mail_tbl;";
9553         char **result = NULL;
9554
9555 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9556         _timedlock_shm_mutex(mapped_for_generating_mail_id, 2);
9557 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9558
9559         ret = vconf_get_int(VCONF_KEY_LATEST_MAIL_ID, &latest_mail_id);
9560         if (ret < 0 || latest_mail_id == 0) {
9561                 EM_DEBUG_LOG("vconf_get_int() failed [%d] or latest_mail_id is zero", ret);
9562
9563                 local_db_handle = emstorage_get_db_connection(multi_user_name);
9564
9565                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9566                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
9567                                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9568                 if (NULL == result[1])
9569                         rc = 1;
9570                 else
9571                         rc = atoi(result[1]) + 1;
9572
9573                 sqlite3_free_table(result);
9574                 latest_mail_id = rc;
9575         }
9576
9577         latest_mail_id++;
9578
9579         ret = vconf_set_int(VCONF_KEY_LATEST_MAIL_ID, latest_mail_id);
9580
9581         if (mail_id)
9582                 *mail_id = latest_mail_id;
9583
9584 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9585         _unlockshm_mutex(mapped_for_generating_mail_id);
9586 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9587
9588         ret = true;
9589
9590 FINISH_OFF:
9591
9592         if (err_code != NULL)
9593                 *err_code = error;
9594
9595         EM_DEBUG_FUNC_END("ret [%d]", ret);
9596         return ret;
9597 }
9598
9599 INTERNAL_FUNC int emstorage_add_mail(char *multi_user_name, emstorage_mail_tbl_t *mail_tbl_data, int get_id, int transaction, int *err_code)
9600 {
9601         EM_PROFILE_BEGIN(profile_emstorage_add_mail);
9602         EM_DEBUG_FUNC_BEGIN("mail_tbl_data[%p], get_id[%d], transaction[%d], err_code[%p]", mail_tbl_data, get_id, transaction, err_code);
9603
9604         if (!mail_tbl_data) {
9605                 EM_DEBUG_EXCEPTION("mail_tbl_data[%p], get_id[%d]", mail_tbl_data, get_id);
9606                 if (err_code != NULL)
9607                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9608                 return false;
9609         }
9610
9611         int rc, ret = false;
9612         int error = EMAIL_ERROR_NONE;
9613         char sql_query_string[QUERY_SIZE] = {0, };
9614         DB_STMT hStmt = NULL;
9615
9616         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9617
9618         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9619
9620         if (get_id) {
9621                 /*  increase unique id */
9622                 char *sql = "SELECT max(rowid) FROM mail_tbl;";
9623                 char **result;
9624
9625                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9626                 /*              EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
9627                                 ("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle))); */
9628                 if (rc != SQLITE_OK) {
9629                         EM_DEBUG_EXCEPTION("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle));
9630                         error = EMAIL_ERROR_DB_FAILURE;
9631                         sqlite3_free_table(result);
9632                         goto FINISH_OFF;
9633                 }
9634
9635                 if (NULL == result[1])
9636                         rc = 1;
9637                 else
9638                         rc = atoi(result[1])+1;
9639
9640                 sqlite3_free_table(result);
9641
9642                 mail_tbl_data->mail_id   = rc;
9643                 mail_tbl_data->thread_id = rc;
9644         }
9645
9646         if (mail_tbl_data->date_time == 0)
9647                 mail_tbl_data->date_time = time(NULL);
9648
9649         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9650                         "INSERT INTO mail_tbl VALUES "
9651                         "(?" /*  mail_id */
9652                         ", ?" /*  account_id */
9653                         ", ?" /*  mailbox_id */
9654                         ", ?" /*  mailbox_type */
9655                         ", ?" /*  subject */
9656
9657                         ", ?" /*  date_time */
9658                         ", ?" /*  server_mail_status */
9659                         ", ?" /*  server_mailbox_name */
9660                         ", ?" /*  server_mail_id */
9661                         ", ?" /*  message_id */
9662
9663                         ", ?" /*  reference_mail_id */
9664                         ", ?" /*  full_address_from */
9665                         ", ?" /*  full_address_reply */
9666                         ", ?" /*  full_address_to */
9667                         ", ?" /*  full_address_cc */
9668
9669                         ", ?" /*  full_address_bcc */
9670                         ", ?" /*  full_address_return */
9671                         ", ?" /*  email_address_sender */
9672                         ", ?" /*  email_address_recipient */
9673                         ", ?" /*  alias_sender */
9674
9675                         ", ?" /*  alias_recipient */
9676                         ", ?" /*  body_download_status */
9677                         ", ?" /*  file_path_plain */
9678                         ", ?" /*  file_path_html */
9679                         ", ?" /*  file_path_mime_entity */
9680
9681                         ", ?" /*  mail_size */
9682                         ", ?" /*  flags_seen_field */
9683                         ", ?" /*  flags_deleted_field */
9684                         ", ?" /*  flags_flagged_field */
9685                         ", ?" /*  flags_answered_field */
9686
9687                         ", ?" /*  flags_recent_field */
9688                         ", ?" /*  flags_draft_field */
9689                         ", ?" /*  flags_forwarded_field */
9690                         ", ?" /*  DRM_status */
9691                         ", ?" /*  priority */
9692
9693                         ", ?" /*  save_status */
9694                         ", ?" /*  lock_status */
9695                         ", ?" /*  report_status */
9696                         ", ?" /*  attachment_count */
9697                         ", ?" /*  inline_content_count */
9698
9699                         ", ?" /*  thread_id */
9700                         ", ?" /*  thread_item_count */
9701                         ", ?" /*  preview_text */
9702                         ", ?" /*  meeting_request_status */
9703                         ", ?" /*  message_class */
9704
9705                         ", ?" /*  digest_type */
9706                         ", ?" /*  smime_type */
9707                         ", ?" /*  scheduled_sending_time */
9708                         ", ?" /*  remaining_resend_times */
9709                         ", ?" /*  tag_id */
9710
9711                         ", ?" /*  replied_time */
9712                         ", ?" /*  forwarded_time */
9713                         ", ?" /*  default charset */
9714                         ", ?" /*  eas_data_length */
9715                         ", ?" /*  eas_data */
9716                         ", ?" /*  user_name */
9717                         ")");
9718
9719         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle,
9720                                 sql_query_string,
9721                                 EM_SAFE_STRLEN(sql_query_string),
9722                                 &hStmt,
9723                                 NULL),
9724                         rc);
9725         if (rc != SQLITE_OK) {
9726                 EM_DEBUG_EXCEPTION("sqlite3_prepare error [%d] [%s] SQL(%s) ",
9727                                 rc, sql_query_string, sqlite3_errmsg(local_db_handle));
9728                 error = EMAIL_ERROR_DB_FAILURE;
9729                 goto FINISH_OFF;
9730         }
9731
9732         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mail_id);
9733         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, mail_tbl_data->account_id);
9734         _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_id);
9735         _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_type);
9736         _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9737
9738         _bind_stmt_field_data_int(hStmt, DATETIME_IDX_IN_MAIL_TBL, mail_tbl_data->date_time);
9739         _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->server_mail_status);
9740         _bind_stmt_field_data_string(hStmt, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->server_mailbox_name, 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
9741         _bind_stmt_field_data_string(hStmt, SERVER_MAIL_ID_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->server_mail_id, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
9742         _bind_stmt_field_data_string(hStmt, MESSAGE_ID_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->message_id, 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
9743
9744         _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, mail_tbl_data->reference_mail_id);
9745         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
9746         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
9747         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
9748         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
9749
9750         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
9751         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
9752         _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9753         _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9754         _bind_stmt_field_data_string(hStmt, ALIAS_SENDER_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->alias_sender, 1, FROM_CONTACT_NAME_LEN_IN_MAIL_TBL);
9755
9756         _bind_stmt_field_data_string(hStmt, ALIAS_RECIPIENT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->alias_recipient, 1, FROM_CONTACT_NAME_LEN_IN_MAIL_TBL);
9757         _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->body_download_status);
9758         _bind_stmt_field_data_string(hStmt, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9759         _bind_stmt_field_data_string(hStmt, FILE_PATH_HTML_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9760         _bind_stmt_field_data_string(hStmt, FILE_PATH_MIME_ENTITY_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9761
9762         _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, mail_tbl_data->mail_size);
9763         _bind_stmt_field_data_int(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_seen_field);
9764         _bind_stmt_field_data_int(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_deleted_field);
9765         _bind_stmt_field_data_int(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_flagged_field);
9766         _bind_stmt_field_data_int(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_answered_field);
9767
9768         _bind_stmt_field_data_int(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_recent_field);
9769         _bind_stmt_field_data_int(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_draft_field);
9770         _bind_stmt_field_data_int(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_forwarded_field);
9771         _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->DRM_status);
9772         _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, mail_tbl_data->priority);
9773
9774         _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->save_status);
9775         _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->lock_status);
9776         _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->report_status);
9777         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->attachment_count);
9778         _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->inline_content_count);
9779
9780         _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, mail_tbl_data->thread_id);
9781         _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->thread_item_count);
9782         _bind_stmt_field_data_nstring(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9783         _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->meeting_request_status);
9784         _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, mail_tbl_data->message_class);
9785
9786         _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->digest_type);
9787         _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->smime_type);
9788         _bind_stmt_field_data_int(hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->scheduled_sending_time);
9789         _bind_stmt_field_data_int(hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, mail_tbl_data->remaining_resend_times);
9790         _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, mail_tbl_data->tag_id);
9791
9792         _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->replied_time);
9793         _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->forwarded_time);
9794         _bind_stmt_field_data_string(hStmt, DEFAULT_CHARSET_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
9795         _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, mail_tbl_data->eas_data_length);
9796         _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, (void*)mail_tbl_data->eas_data, mail_tbl_data->eas_data_length);
9797         _bind_stmt_field_data_string(hStmt, USER_NAME_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->user_name, 0, TEXT_2_LEN_IN_MAIL_TBL);
9798
9799         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9800         if (rc == SQLITE_FULL) {
9801                 EM_DEBUG_EXCEPTION("sqlite3_step error [%d]", rc);
9802                 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
9803                 goto FINISH_OFF;
9804         }
9805         if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
9806                 EM_DEBUG_EXCEPTION("sqlite3_step error [%d]", rc);
9807                 error = EMAIL_ERROR_DB_FAILURE;
9808                 goto FINISH_OFF;
9809         }
9810         ret = true;
9811
9812 FINISH_OFF:
9813         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9814
9815         if (hStmt != NULL) {
9816                 rc = sqlite3_finalize(hStmt);
9817                 if (rc != SQLITE_OK) {
9818                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9819                         error = EMAIL_ERROR_DB_FAILURE;
9820                 }
9821         }
9822
9823         if (err_code != NULL)
9824                 *err_code = error;
9825
9826         EM_PROFILE_END(profile_emstorage_add_mail);
9827         EM_DEBUG_FUNC_END("ret [%d]", ret);
9828         return ret;
9829 }
9830
9831 INTERNAL_FUNC int emstorage_move_multiple_mails_on_db(char *multi_user_name, int input_source_account_id, int input_mailbox_id, int mail_ids[], int number_of_mails, int transaction, int *err_code)
9832 {
9833         EM_DEBUG_FUNC_BEGIN("input_source_account_id [%d], input_mailbox_id [%d], mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", input_source_account_id, input_mailbox_id, mail_ids, number_of_mails, transaction, err_code);
9834
9835         int ret = false, i, cur_conditional_clause = 0;
9836         int error = EMAIL_ERROR_NONE;
9837         int target_account_id;
9838         int conditional_clause_len = 0;
9839         char *sql_query_string = NULL, *conditional_clause = NULL;
9840         emstorage_mailbox_tbl_t *result_mailbox = NULL;
9841         email_mailbox_type_e target_mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED;
9842         char* target_mailbox_name = NULL;
9843
9844         if (!mail_ids || input_mailbox_id <= 0) {
9845                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9846                 if (err_code != NULL)
9847                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9848                 return false;
9849         }
9850
9851         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9852
9853         if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &result_mailbox)) != EMAIL_ERROR_NONE || !result_mailbox) {
9854                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", error);
9855                 if (err_code != NULL)
9856                         *err_code = error;
9857                 return false;
9858         }
9859
9860         if (result_mailbox->mailbox_name) {
9861                 if (strstr(result_mailbox->mailbox_name, "'"))
9862                         target_mailbox_name = em_replace_all_string(result_mailbox->mailbox_name, "'", "''");
9863                 else
9864                         target_mailbox_name = strdup(result_mailbox->mailbox_name);
9865         }
9866
9867         target_mailbox_type = result_mailbox->mailbox_type;
9868         target_account_id   = result_mailbox->account_id;
9869         emstorage_free_mailbox(&result_mailbox, 1, NULL);
9870
9871         conditional_clause_len =  (sizeof(char) * 8 * number_of_mails) + 512;
9872         conditional_clause = em_malloc(conditional_clause_len);
9873         if (conditional_clause == NULL) {
9874                 EM_DEBUG_EXCEPTION("em_mallocfailed");
9875                 error = EMAIL_ERROR_OUT_OF_MEMORY;
9876                 goto FINISH_OFF;
9877         }
9878         cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE mail_id in (");
9879
9880         for (i = 0; i < number_of_mails; i++)
9881                 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
9882
9883         /* prevent 34415 */
9884         char *last_comma = rindex(conditional_clause, ',');
9885         if (last_comma) *last_comma = ')'; /* replace , with) */
9886
9887         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9888
9889         /* Updating a mail_tbl */
9890
9891         sql_query_string = em_malloc(conditional_clause_len);
9892         if (sql_query_string == NULL) {
9893                 EM_DEBUG_EXCEPTION("em_mallocfailed");
9894                 error = EMAIL_ERROR_OUT_OF_MEMORY;
9895                 goto FINISH_OFF;
9896         }
9897         SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_tbl SET mailbox_type = %d, mailbox_id = %d, account_id = %d %s", target_mailbox_type, input_mailbox_id, target_account_id, conditional_clause);
9898         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9899
9900         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9901         if (error != EMAIL_ERROR_NONE) {
9902                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9903                 goto FINISH_OFF;
9904         }
9905
9906         /* Updating a mail_attachment_tbl */
9907         memset(sql_query_string, 0x00, conditional_clause_len);
9908         SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_attachment_tbl SET mailbox_id = '%d', account_id = %d %s", input_mailbox_id, target_account_id, conditional_clause);
9909         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9910         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9911         if (error != EMAIL_ERROR_NONE) {
9912                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9913                 goto FINISH_OFF;
9914         }
9915
9916         /* Updating a mail_meeting_tbl */
9917         memset(sql_query_string, 0x00, conditional_clause_len);
9918         SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_meeting_tbl SET mailbox_id = %d, account_id = %d %s", input_mailbox_id, target_account_id, conditional_clause);
9919         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9920         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9921         if (error != EMAIL_ERROR_NONE) {
9922                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9923                 goto FINISH_OFF;
9924         }
9925
9926 #ifdef __FEATURE_BODY_SEARCH__
9927         /* Updating mail_text_tbl */
9928         memset(sql_query_string, 0x00, conditional_clause_len);
9929         SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_text_tbl SET mailbox_id = %d, account_id = %d %s", input_mailbox_id, target_account_id, conditional_clause);
9930         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9931         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9932         if (error != EMAIL_ERROR_NONE) {
9933                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9934                 goto FINISH_OFF;
9935         }
9936 #endif
9937
9938         /* Updating a mail_read_mail_uid_tbl */
9939         memset(conditional_clause, 0x00, conditional_clause_len);
9940         cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE local_uid in (");
9941
9942         for (i = 0; i < number_of_mails; i++)
9943                 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
9944
9945         /* prevent 34415 */
9946         last_comma = rindex(conditional_clause, ',');
9947         if (last_comma) *last_comma = ')'; /* replace , with) */
9948
9949         memset(sql_query_string, 0x00, conditional_clause_len);
9950         SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_read_mail_uid_tbl SET mailbox_name = '%s', mailbox_id = %d, account_id = %d %s", target_mailbox_name, input_mailbox_id, target_account_id, conditional_clause);
9951         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9952         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9953         if (error != EMAIL_ERROR_NONE) {
9954                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9955                 goto FINISH_OFF;
9956         }
9957
9958         ret = true;
9959
9960 FINISH_OFF:
9961         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9962
9963         EM_SAFE_FREE(target_mailbox_name);
9964         EM_SAFE_FREE(conditional_clause);
9965         EM_SAFE_FREE(sql_query_string);
9966
9967         if (err_code != NULL)
9968                 *err_code = error;
9969
9970         EM_DEBUG_FUNC_END("ret [%d]", ret);
9971         return ret;
9972 }
9973
9974 INTERNAL_FUNC int emstorage_delete_mail(char *multi_user_name, int mail_id, int from_server, int transaction, int *err_code)
9975 {
9976         EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9977
9978         if (!mail_id) {
9979                 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
9980                 if (err_code != NULL)
9981                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9982                 return false;
9983         }
9984
9985         int ret = false;
9986         int error = EMAIL_ERROR_NONE;
9987         char sql_query_string[QUERY_SIZE] = {0, };
9988         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9989         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9990
9991         memset(sql_query_string, 0x00, sizeof(sql_query_string));
9992
9993         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id = %d ", mail_id);
9994         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9995         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9996         if (error != EMAIL_ERROR_NONE) {
9997                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9998                 goto FINISH_OFF;
9999         }
10000
10001         ret = true;
10002
10003 FINISH_OFF:
10004         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10005
10006         if (err_code != NULL)
10007                 *err_code = error;
10008
10009         EM_DEBUG_FUNC_END("ret [%d]", ret);
10010         return ret;
10011 }
10012
10013 INTERNAL_FUNC int emstorage_delete_multiple_mails(char *multi_user_name, int mail_ids[], int number_of_mails, int transaction, int *err_code)
10014 {
10015         EM_DEBUG_FUNC_BEGIN("mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", mail_ids, number_of_mails, transaction, err_code);
10016
10017         int ret = false, i, cur_sql_query_string = 0;
10018         int error = EMAIL_ERROR_NONE;
10019         int query_size = 0;
10020         char *sql_query_string = NULL;
10021
10022         if (!mail_ids) {
10023                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10024                 if (err_code != NULL)
10025                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10026                 return false;
10027         }
10028
10029         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10030         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10031
10032         query_size = (sizeof(char) * 8 * number_of_mails) + 512;
10033         sql_query_string =  em_malloc(query_size);
10034         if (sql_query_string == NULL) {
10035                 EM_DEBUG_EXCEPTION("em_mallocfailed");
10036                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10037                 goto FINISH_OFF;
10038         }
10039
10040         cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_tbl WHERE mail_id in (");
10041
10042         for (i = 0; i < number_of_mails; i++)
10043                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
10044
10045         /* prevent 34414 */
10046         char *last_comma = rindex(sql_query_string, ',');
10047         if (last_comma != NULL) *last_comma = ')'; /* replace , with) */
10048
10049         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10050         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10051         if (error != EMAIL_ERROR_NONE) {
10052                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10053                 goto FINISH_OFF;
10054         }
10055
10056 #ifdef __FEATURE_BODY_SEARCH__
10057         /* delete mail_text from mail_text_tbl */
10058         cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_text_tbl WHERE mail_id in (");
10059
10060         for (i = 0; i < number_of_mails; i++)
10061                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
10062
10063         last_comma = rindex(sql_query_string, ',');
10064         *last_comma = ')'; /* replace , with) */
10065
10066         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10067         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10068         if (error != EMAIL_ERROR_NONE) {
10069                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10070                 goto FINISH_OFF;
10071         }
10072 #endif
10073
10074         ret = true;
10075
10076 FINISH_OFF:
10077         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10078
10079         EM_SAFE_FREE(sql_query_string);
10080         if (err_code != NULL)
10081                 *err_code = error;
10082
10083         EM_DEBUG_FUNC_END("ret [%d]", ret);
10084         return ret;
10085 }
10086
10087 INTERNAL_FUNC int emstorage_delete_mail_by_account(char *multi_user_name, int account_id, int transaction, int *err_code)
10088 {
10089         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
10090
10091         if (account_id < FIRST_ACCOUNT_ID) {
10092                 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
10093
10094                 if (err_code != NULL)
10095                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10096                 return false;
10097         }
10098
10099         int rc, ret = false;
10100         int error = EMAIL_ERROR_NONE;
10101         char sql_query_string[QUERY_SIZE] = {0, };
10102
10103         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10104         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10105
10106         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d", account_id);
10107         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10108         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10109         if (error != EMAIL_ERROR_NONE) {
10110                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10111                 goto FINISH_OFF;
10112         }
10113
10114         rc = sqlite3_changes(local_db_handle);
10115         if (rc == 0) {
10116                 EM_DEBUG_LOG(" no mail found...");
10117                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10118         }
10119
10120         /* Delete all mails  mail_read_mail_uid_tbl table based on account id */
10121         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
10122         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10123         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10124         if (error != EMAIL_ERROR_NONE) {
10125                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10126                 goto FINISH_OFF;
10127         }
10128
10129         rc = sqlite3_changes(local_db_handle);
10130         if (rc == 0) {
10131                 EM_DEBUG_LOG("No mail found...");
10132                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10133         }
10134
10135 #ifdef __FEATURE_BODY_SEARCH__
10136         /* Delete all mail_text in mail_text_tbl table based on account id */
10137         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE account_id = %d", account_id);
10138         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10139         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10140         if (error != EMAIL_ERROR_NONE) {
10141                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10142                 goto FINISH_OFF;
10143         }
10144
10145         rc = sqlite3_changes(local_db_handle);
10146         if (rc == 0) {
10147                 EM_DEBUG_LOG("No mail found...");
10148                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10149         }
10150 #endif
10151
10152         ret = true;
10153
10154 FINISH_OFF:
10155         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10156
10157         if (error == EMAIL_ERROR_NONE) {
10158                 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_WITH_ACCOUNT, account_id, 0 , NULL, 0))
10159                         EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAIL_DELETE_ALL ]");
10160         }
10161
10162         if (err_code != NULL)
10163                 *err_code = error;
10164
10165         EM_DEBUG_FUNC_END("ret [%d]", ret);
10166         return ret;
10167 }
10168
10169 INTERNAL_FUNC int emstorage_delete_mail_by_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox, int transaction, int *err_code)
10170 {
10171         EM_DEBUG_FUNC_BEGIN("mailbox[%p], transaction[%d], err_code[%p]", mailbox, transaction, err_code);
10172
10173         if (mailbox == NULL) {
10174                 EM_DEBUG_EXCEPTION("mailbox [%p]", mailbox);
10175                 if (err_code != NULL)
10176                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10177                 return false;
10178         }
10179
10180         int ret = false;
10181         int error = EMAIL_ERROR_NONE;
10182         char sql_query_string[QUERY_SIZE] = {0, };
10183         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10184         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10185
10186         if (strcmp(mailbox->mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
10187                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d AND mailbox_type = %d", mailbox->account_id, mailbox->mailbox_type);
10188                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10189                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10190                 if (error != EMAIL_ERROR_NONE) {
10191                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10192                         goto FINISH_OFF;
10193                 }
10194
10195                 /* Delete Mails from mail_read_mail_uid_tbl */
10196                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d AND mailbox_name = '%s'", mailbox->account_id, mailbox->mailbox_name);
10197                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10198                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10199                 if (error != EMAIL_ERROR_NONE) {
10200                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10201                         goto FINISH_OFF;
10202                 }
10203         } else {
10204                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d AND mailbox_id = %d", mailbox->account_id, mailbox->mailbox_id);
10205                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10206                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10207                 if (error != EMAIL_ERROR_NONE) {
10208                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10209                         goto FINISH_OFF;
10210                 }
10211
10212                 /* Delete Mails from mail_read_mail_uid_tbl */
10213                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d AND mailbox_id = %d", mailbox->account_id, mailbox->mailbox_id);
10214                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10215                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10216                 if (error != EMAIL_ERROR_NONE) {
10217                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10218                         goto FINISH_OFF;
10219                 }
10220
10221 #ifdef __FEATURE_BODY_SEARCH__
10222                 /* Delete Mails from mail_text_tbl */
10223                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE account_id = %d AND mailbox_id = %d", mailbox->account_id, mailbox->mailbox_id);
10224                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10225                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10226                 if (error != EMAIL_ERROR_NONE) {
10227                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10228                         goto FINISH_OFF;
10229                 }
10230 #endif
10231         }
10232         ret = true;
10233
10234 FINISH_OFF:
10235         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10236
10237         if (error == EMAIL_ERROR_NONE) {
10238                 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_ALL, mailbox->account_id, mailbox->mailbox_id , mailbox->mailbox_name, 0))
10239                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventFailed [ NOTI_MAIL_DELETE_ALL ] >>>> ");
10240         }
10241
10242         if (err_code != NULL)
10243                 *err_code = error;
10244
10245         EM_DEBUG_FUNC_END("ret [%d]", ret);
10246         return ret;
10247 }
10248
10249 INTERNAL_FUNC int emstorage_free_mail(emstorage_mail_tbl_t** mail_list, int count, int *err_code)
10250 {
10251         EM_DEBUG_FUNC_BEGIN("mail_list[%p], count[%d], err_code[%p]", mail_list, count, err_code);
10252
10253         if (count > 0) {
10254                 if ((mail_list == NULL) || (*mail_list == NULL)) {
10255                         EM_DEBUG_EXCEPTION("mail_ilst[%p], count[%d]", mail_list, count);
10256
10257                         if (err_code)
10258                                 *err_code = EMAIL_ERROR_INVALID_PARAM;
10259                         return false;
10260                 }
10261
10262                 emstorage_mail_tbl_t* p = *mail_list;
10263                 int i = 0;
10264
10265                 for (; i < count; i++, p++) {
10266                         EM_SAFE_FREE(p->server_mailbox_name);
10267                         EM_SAFE_FREE(p->server_mail_id);
10268                         EM_SAFE_FREE(p->full_address_from);
10269                         EM_SAFE_FREE(p->full_address_reply);
10270                         EM_SAFE_FREE(p->full_address_to);
10271                         EM_SAFE_FREE(p->full_address_cc);
10272                         EM_SAFE_FREE(p->full_address_bcc);
10273                         EM_SAFE_FREE(p->full_address_return);
10274                         EM_SAFE_FREE(p->subject);
10275                         EM_SAFE_FREE(p->file_path_plain);
10276                         EM_SAFE_FREE(p->file_path_html);
10277                         EM_SAFE_FREE(p->file_path_mime_entity);
10278                         EM_SAFE_FREE(p->message_id);
10279                         EM_SAFE_FREE(p->email_address_sender);
10280                         EM_SAFE_FREE(p->email_address_recipient);
10281                         EM_SAFE_FREE(p->preview_text);
10282                         EM_SAFE_FREE(p->alias_sender);
10283                         EM_SAFE_FREE(p->alias_recipient);
10284                         EM_SAFE_FREE(p->default_charset);
10285                         EM_SAFE_FREE(p->pgp_password);
10286                         EM_SAFE_FREE(p->eas_data);
10287                         EM_SAFE_FREE(p->user_name);
10288                 }
10289                 EM_SAFE_FREE(*mail_list);
10290         }
10291
10292         if (err_code != NULL)
10293                 *err_code = EMAIL_ERROR_NONE;
10294
10295         EM_DEBUG_FUNC_END();
10296         return true;
10297 }
10298
10299 #ifdef __FEATURE_BODY_SEARCH__
10300 INTERNAL_FUNC void emstorage_free_mail_text(emstorage_mail_text_tbl_t** mail_text_list, int count, int *err_code)
10301 {
10302         EM_DEBUG_FUNC_BEGIN("mail_text_list[%p], count[%d], err_code[%p]", mail_text_list, count, err_code);
10303
10304         if (count > 0) {
10305                 if ((mail_text_list == NULL) || (*mail_text_list == NULL)) {
10306                         EM_DEBUG_LOG("Nothing to free: mail_text_list[%p]", mail_text_list);
10307                         return;
10308                 }
10309
10310                 emstorage_mail_text_tbl_t *p = *mail_text_list;
10311                 int i = 0;
10312
10313                 for (; i < count; i++, p++)
10314                         EM_SAFE_FREE(p->body_text);
10315
10316                 EM_SAFE_FREE(*mail_text_list);
10317         }
10318
10319         EM_DEBUG_FUNC_END();
10320 }
10321 #endif
10322
10323 INTERNAL_FUNC int emstorage_get_attachment_count(char *multi_user_name, int mail_id, int *count, int transaction, int *err_code)
10324 {
10325         EM_DEBUG_FUNC_BEGIN("mail_id[%d], count[%p], transaction[%d], err_code[%p]", mail_id, count, transaction, err_code);
10326
10327         if (mail_id <= 0 || !count) {
10328                 EM_DEBUG_EXCEPTION("mail_id[%d], count[%p]", mail_id, count);
10329                 if (err_code != NULL)
10330                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10331                 return false;
10332         }
10333
10334         int rc = -1, ret = false;
10335         int error = EMAIL_ERROR_NONE;
10336         char sql_query_string[QUERY_SIZE] = {0, };
10337
10338         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10339         EMSTORAGE_START_READ_TRANSACTION(transaction);
10340
10341         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
10342
10343         char **result;
10344
10345         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10346         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10347                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10348
10349         *count = atoi(result[1]);
10350         sqlite3_free_table(result);
10351
10352         ret = true;
10353
10354 FINISH_OFF:
10355         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10356
10357         if (err_code != NULL)
10358                 *err_code = error;
10359
10360         EM_DEBUG_FUNC_END("ret [%d]", ret);
10361         return ret;
10362 }
10363
10364 INTERNAL_FUNC int emstorage_get_attachment_list(char *multi_user_name, int input_mail_id, int input_transaction, emstorage_attachment_tbl_t** output_attachment_list, int *output_attachment_count)
10365 {
10366         EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], input_transaction[%d], output_attachment_list[%p], output_attachment_count[%p]", input_mail_id, input_transaction, output_attachment_list, output_attachment_count);
10367
10368         if (input_mail_id <= 0 || !output_attachment_list || !output_attachment_count) {
10369                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10370                 return EMAIL_ERROR_INVALID_PARAM;
10371         }
10372
10373         int                         error = EMAIL_ERROR_NONE;
10374         int                         i = 0;
10375         int                         rc = -1;
10376         char                      **result = NULL;
10377         char                        sql_query_string[QUERY_SIZE] = {0, };
10378         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10379         DB_STMT hStmt = NULL;
10380         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10381
10382         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
10383         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", input_mail_id);
10384         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10385         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10386                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10387
10388         *output_attachment_count = atoi(result[1]);
10389         sqlite3_free_table(result);
10390
10391         if (*output_attachment_count == 0) {
10392                 error = EMAIL_ERROR_NONE;
10393                 goto FINISH_OFF;
10394         }
10395
10396         p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * (*output_attachment_count));
10397
10398         if (!p_data_tbl) {
10399                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
10400                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10401                 goto FINISH_OFF;
10402         }
10403
10404         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE mail_id = %d ORDER BY attachment_id", input_mail_id);
10405         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
10406
10407         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10408         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10409
10410         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10411         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },  ("sqlite3_step fail:%d", rc));
10412
10413         if (rc == SQLITE_DONE) {
10414                 EM_DEBUG_EXCEPTION("no matched attachment found...");
10415                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10416                 goto FINISH_OFF;
10417         }
10418         for (i = 0; i < *output_attachment_count; i++) {
10419                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10420                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10421                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10422                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10423                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10424                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10425                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10426                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10427                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10428                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10429                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10430                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10431                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10432                 EM_DEBUG_LOG("attachment[%d].attachment_id : %d", i, p_data_tbl[i].attachment_id);
10433                 EM_DEBUG_LOG("attachment_mime_type : %s", p_data_tbl[i].attachment_mime_type);
10434                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10435                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
10436         }
10437
10438 FINISH_OFF:
10439
10440         if (error == EMAIL_ERROR_NONE)
10441                 *output_attachment_list = p_data_tbl;
10442         else if (p_data_tbl != NULL)
10443                 emstorage_free_attachment(&p_data_tbl, *output_attachment_count, NULL);
10444
10445         if (hStmt) {
10446                 rc = sqlite3_finalize(hStmt);
10447                 if (rc != SQLITE_OK) {
10448                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10449                         error = EMAIL_ERROR_DB_FAILURE;
10450                         if (*output_attachment_list)
10451                                 emstorage_free_attachment(output_attachment_list, *output_attachment_count, NULL); /* prevent */
10452                 }
10453         }
10454
10455         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
10456
10457         EM_DEBUG_FUNC_END("error [%d]", error);
10458         return error;
10459 }
10460
10461 INTERNAL_FUNC int emstorage_get_attachment(char *multi_user_name, int attachment_id, emstorage_attachment_tbl_t **attachment, int transaction, int *err_code)
10462 {
10463         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], transaction[%d], err_code[%p]", attachment_id, attachment, transaction, err_code);
10464
10465         if (attachment_id <= 0 || !attachment) {
10466                 EM_DEBUG_EXCEPTION("attachment_id[%d], attachment[%p]", attachment_id, attachment);
10467                 if (err_code != NULL)
10468                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10469                 return false;
10470         }
10471
10472         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10473         int rc, ret = false;
10474         int error = EMAIL_ERROR_NONE;
10475         char sql_query_string[QUERY_SIZE] = {0, };
10476
10477         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10478         EMSTORAGE_START_READ_TRANSACTION(transaction);
10479
10480         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_id = %d",  attachment_id);
10481
10482         sqlite3_stmt* hStmt = NULL;
10483
10484         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10485         EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
10486
10487         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10488                         ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10489
10490
10491         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10492         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10493                         ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10494
10495         if (rc == SQLITE_DONE) {
10496                 EM_DEBUG_LOG("no matched attachment found...");
10497                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10498                 goto FINISH_OFF;
10499         }
10500
10501         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10502                 EM_DEBUG_EXCEPTION("malloc failed...");
10503                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10504                 goto FINISH_OFF;
10505         }
10506
10507         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10508         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10509         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10510         _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10511         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10512         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10513         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10514         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10515         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10516         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10517         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10518         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10519         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10520
10521 #ifdef __ATTACHMENT_OPTI__
10522         _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10523         _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10524 #endif
10525
10526         ret = true;
10527
10528 FINISH_OFF:
10529         if (ret == true)
10530                 *attachment = p_data_tbl;
10531
10532         if (hStmt != NULL) {
10533                 rc = sqlite3_finalize(hStmt);
10534                 if (rc != SQLITE_OK) {
10535                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10536                         error = EMAIL_ERROR_DB_FAILURE;
10537                 }
10538         }
10539
10540         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10541
10542         if (err_code != NULL)
10543                 *err_code = error;
10544
10545         EM_DEBUG_FUNC_END("ret [%d]", ret);
10546         return ret;
10547 }
10548
10549 INTERNAL_FUNC int emstorage_get_attachment_nth(char *multi_user_name, int mail_id, int nth, emstorage_attachment_tbl_t **attachment_tbl, int transaction, int *err_code)
10550 {
10551         EM_DEBUG_FUNC_BEGIN("mail_id[%d], nth[%d], attachment_tbl[%p], transaction[%d], err_code[%p]", mail_id, nth, attachment_tbl, transaction, err_code);
10552
10553         if (mail_id <= 0 || nth <= 0 || !attachment_tbl) {
10554                 EM_DEBUG_EXCEPTION(" mail_id[%d], nth[%d], attachment[%p]", mail_id, nth, attachment_tbl);
10555
10556                 if (err_code != NULL)
10557                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10558                 return false;
10559         }
10560
10561         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10562         char *p = NULL;
10563         int rc, ret = false;
10564         int error = EMAIL_ERROR_NONE;
10565         char sql_query_string[QUERY_SIZE] = {0, };
10566
10567         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10568         EMSTORAGE_START_READ_TRANSACTION(transaction);
10569
10570         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE mail_id = %d ORDER BY attachment_id LIMIT %d, 1", mail_id, (nth - 1));
10571         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10572
10573         DB_STMT hStmt = NULL;
10574
10575         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10576         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10577                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10578
10579
10580         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10581         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10582                         ("sqlite3_step fail:%d", rc));
10583
10584         if (rc == SQLITE_DONE) {
10585                 EM_DEBUG_EXCEPTION("no matched attachment found: mail_id[%d] nth[%d]", mail_id, nth);
10586                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10587                 goto FINISH_OFF;
10588         }
10589
10590         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10591                 EM_DEBUG_EXCEPTION(" malloc failed...");
10592                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10593                 goto FINISH_OFF;
10594         }
10595
10596         p_data_tbl->attachment_id = sqlite3_column_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10597         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10598                 p_data_tbl->attachment_name = cpy_str(p);
10599         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10600                 p_data_tbl->attachment_path = cpy_str(p);
10601         if ((p = (char *)sqlite3_column_text(hStmt, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10602                 p_data_tbl->content_id = cpy_str(p);
10603         p_data_tbl->attachment_size = sqlite3_column_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10604         p_data_tbl->mail_id = sqlite3_column_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10605         p_data_tbl->account_id = sqlite3_column_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10606         p_data_tbl->mailbox_id = sqlite3_column_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10607         p_data_tbl->attachment_save_status = sqlite3_column_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10608         p_data_tbl->attachment_drm_type = sqlite3_column_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10609         p_data_tbl->attachment_drm_method = sqlite3_column_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10610         p_data_tbl->attachment_inline_content_status = sqlite3_column_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10611         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10612                 p_data_tbl->attachment_mime_type = cpy_str(p);
10613 #ifdef __ATTACHMENT_OPTI__
10614         p_data_tbl->encoding = sqlite3_column_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10615         if ((p = (char *)sqlite3_column_text(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10616                 p_data_tbl->section = cpy_str(p);
10617 #endif
10618         ret = true;
10619
10620 FINISH_OFF:
10621         if (ret == true)
10622                 *attachment_tbl = p_data_tbl;
10623
10624         if (hStmt != NULL) {
10625                 rc = sqlite3_finalize(hStmt);
10626                 if (rc != SQLITE_OK) {
10627                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10628                         error = EMAIL_ERROR_DB_FAILURE;
10629                 }
10630         }
10631
10632         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10633
10634         if (err_code != NULL)
10635                 *err_code = error;
10636
10637         EM_DEBUG_FUNC_END("ret [%d]", ret);
10638         return ret;
10639 }
10640
10641 INTERNAL_FUNC int emstorage_get_attachment_by_attachment_path(char *multi_user_name, char *attachment_path, emstorage_attachment_tbl_t **attachment, int transaction, int *err_code)
10642 {
10643         EM_DEBUG_FUNC_BEGIN("attachment_path[%p], attachment[%p], transaction[%d], err_code[%p]", attachment_path, attachment, transaction, err_code);
10644
10645         if (attachment_path == NULL || !attachment) {
10646                 EM_DEBUG_EXCEPTION("attachment_path[%p], attachment[%p]", attachment_path, attachment);
10647                 if (err_code != NULL)
10648                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10649                 return false;
10650         }
10651
10652         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10653         int rc, ret = false;
10654         int error = EMAIL_ERROR_NONE;
10655         char sql_query_string[QUERY_SIZE] = {0, };
10656
10657         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10658         EMSTORAGE_START_READ_TRANSACTION(transaction);
10659
10660         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_path = '%s'", attachment_path);
10661
10662         sqlite3_stmt* hStmt = NULL;
10663
10664         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10665         EM_DEBUG_LOG_DEV("before sqlite3_prepare hStmt = %p", hStmt);
10666
10667         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10668                         ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10669
10670
10671         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10672         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10673                         ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10674
10675         if (rc == SQLITE_DONE) {
10676                 EM_DEBUG_LOG("no matched attachment found...");
10677                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10678                 goto FINISH_OFF;
10679         }
10680
10681         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10682                 EM_DEBUG_EXCEPTION("malloc failed...");
10683                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10684                 goto FINISH_OFF;
10685         }
10686
10687         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10688         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10689         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10690         _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10691         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10692         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10693         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10694         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10695         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10696         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10697         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10698         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10699         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10700
10701 #ifdef __ATTACHMENT_OPTI__
10702         _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10703         _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10704 #endif
10705
10706         ret = true;
10707
10708 FINISH_OFF:
10709         if (ret == true)
10710                 *attachment = p_data_tbl;
10711
10712         if (hStmt != NULL) {
10713                 rc = sqlite3_finalize(hStmt);
10714                 if (rc != SQLITE_OK) {
10715                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10716                         error = EMAIL_ERROR_DB_FAILURE;
10717                 }
10718         }
10719
10720         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10721
10722         if (err_code != NULL)
10723                 *err_code = error;
10724
10725         EM_DEBUG_FUNC_END("ret [%d]", ret);
10726         return ret;
10727 }
10728
10729 INTERNAL_FUNC int emstorage_change_attachment_field(char *multi_user_name, int mail_id, email_mail_change_type_t type, emstorage_attachment_tbl_t *attachment, int transaction, int *err_code)
10730 {
10731         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], attachment[%p], transaction[%d], err_code[%p]", mail_id, type, attachment, transaction, err_code);
10732
10733         if (mail_id <= 0 || !attachment) {
10734                 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], attachment[%p]", mail_id, type, attachment);
10735                 if (err_code != NULL)
10736                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10737                 return false;;
10738         }
10739
10740         int rc, ret = false;
10741         int error = EMAIL_ERROR_NONE;
10742         DB_STMT hStmt = NULL;
10743         char sql_query_string[QUERY_SIZE] = {0, };
10744
10745         int i = 0;
10746
10747         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10748
10749         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10750
10751         switch (type) {
10752         case UPDATE_MAILBOX:
10753                 EM_DEBUG_LOG("UPDATE_MAILBOX");
10754                 if (!attachment->mailbox_id) {
10755                         EM_DEBUG_EXCEPTION(" attachment->mailbox_id[%d]", attachment->mailbox_id);
10756                         error = EMAIL_ERROR_INVALID_PARAM;
10757                         goto FINISH_OFF;
10758                 }
10759                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10760                                 "UPDATE mail_attachment_tbl SET mailbox_id = ? WHERE mail_id = %d", mail_id);
10761
10762                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10763                 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10764                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10765                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10766
10767                 _bind_stmt_field_data_int(hStmt, i++, attachment->mailbox_id);
10768                 break;
10769
10770         case UPDATE_SAVENAME:
10771                 EM_DEBUG_LOG("UPDATE_SAVENAME");
10772                 if (!attachment->attachment_path) {
10773                         EM_DEBUG_EXCEPTION(" attachment->attachment_path[%p]", attachment->attachment_path);
10774                         error = EMAIL_ERROR_INVALID_PARAM;
10775                         goto FINISH_OFF;
10776                 }
10777
10778                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10779                                 "UPDATE mail_attachment_tbl SET"
10780                                 "  attachment_size = ?"
10781                                 ", attachment_save_status = ?"
10782                                 ", attachment_path = ?"
10783                                 " WHERE mail_id = %d"
10784                                 " AND attachment_id = %d"
10785                                 , attachment->mail_id
10786                                 , attachment->attachment_id);
10787
10788
10789                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10790                 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10791                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10792                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10793
10794                 _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_size);
10795                 _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_save_status);
10796                 _bind_stmt_field_data_string(hStmt, i++, (char *)attachment->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10797                 break;
10798
10799         default:
10800                 EM_DEBUG_LOG("type[%d]", type);
10801                 error = EMAIL_ERROR_INVALID_PARAM;
10802                 goto FINISH_OFF;
10803         }
10804         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10805
10806         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10807         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10808                         ("sqlite3_step fail:%d", rc));
10809         ret = true;
10810
10811 FINISH_OFF:
10812
10813         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10814
10815         if (hStmt != NULL) {
10816                 rc = sqlite3_finalize(hStmt);
10817                 if (rc != SQLITE_OK) {
10818                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10819                         error = EMAIL_ERROR_DB_FAILURE;
10820                 }
10821         }
10822
10823         if (err_code != NULL)
10824                 *err_code = error;
10825         EM_DEBUG_FUNC_END("ret [%d]", ret);
10826         return ret;
10827 }
10828
10829 INTERNAL_FUNC int emstorage_rename_mailbox(char *multi_user_name, int input_mailbox_id, char *input_new_mailbox_name, char *input_new_mailbox_alias, void *input_eas_data, int input_eas_data_length, int input_transaction)
10830 {
10831         EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d] input_new_mailbox_name[%p] input_new_mailbox_alias[%p] input_eas_data[%p] input_eas_data_length[%d] input_transaction[%d]", input_mailbox_id, input_new_mailbox_name, input_new_mailbox_alias, input_eas_data, input_eas_data_length, input_transaction);
10832
10833         int rc = 0;
10834         int ret = false;
10835         int error = EMAIL_ERROR_NONE;
10836         int field_idx = 0;
10837         int account_id = 0;
10838         char sql_query_string[QUERY_SIZE] = {0, };
10839         char *replaced_mailbox_name = NULL;
10840         char *replaced_alias = NULL;
10841         sqlite3 *local_db_handle = NULL;
10842         DB_STMT hStmt = NULL;
10843         emstorage_mailbox_tbl_t *old_mailbox_data = NULL;
10844
10845         if (input_mailbox_id <= 0 || !input_new_mailbox_name || !input_new_mailbox_alias) {
10846                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10847                 return EMAIL_ERROR_INVALID_PARAM;
10848         }
10849
10850         if (strstr(input_new_mailbox_name, "'"))
10851                 replaced_mailbox_name = em_replace_all_string(input_new_mailbox_name, "'", "''");
10852         else
10853                 replaced_mailbox_name = strdup(input_new_mailbox_name);
10854
10855         if (strstr(input_new_mailbox_alias, "'"))
10856                 replaced_alias = em_replace_all_string(input_new_mailbox_alias, "'", "''");
10857         else
10858                 replaced_alias = strdup(input_new_mailbox_alias);
10859
10860         local_db_handle = emstorage_get_db_connection(multi_user_name);
10861
10862         if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &old_mailbox_data)) != EMAIL_ERROR_NONE) {
10863                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", error);
10864                 EM_SAFE_FREE(replaced_mailbox_name);
10865                 EM_SAFE_FREE(replaced_alias);
10866                 return error;
10867         }
10868
10869         if (old_mailbox_data == NULL) {
10870                 EM_DEBUG_LOG("old_mailbox_data is NULL");
10871                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
10872                 goto FINISH_OFF;
10873         }
10874
10875         account_id = old_mailbox_data->account_id;
10876
10877         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
10878
10879         if (input_eas_data && input_eas_data_length > 0) {
10880                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10881                                 "UPDATE mail_box_tbl SET"
10882                                 " mailbox_name = ?"
10883                                 ",alias = ?"
10884                                 ",eas_data = ?"
10885                                 ",eas_data_length = ?"
10886                                 " WHERE mailbox_id = ?");
10887
10888                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10889
10890                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10891                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10892
10893                 _bind_stmt_field_data_string(hStmt, field_idx++, input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10894                 _bind_stmt_field_data_string(hStmt, field_idx++, input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10895                 _bind_stmt_field_data_blob(hStmt, field_idx++, input_eas_data, input_eas_data_length);
10896                 _bind_stmt_field_data_int(hStmt, field_idx++, input_eas_data_length);
10897                 _bind_stmt_field_data_int(hStmt, field_idx++, input_mailbox_id);
10898         } else {
10899                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10900                                 "UPDATE mail_box_tbl SET"
10901                                 " mailbox_name = ?"
10902                                 ",alias = ?"
10903                                 " WHERE mailbox_id = ?");
10904
10905                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10906
10907                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10908                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10909
10910                 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10911                 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10912                 _bind_stmt_field_data_int(hStmt, field_idx++ , input_mailbox_id);
10913         }
10914
10915
10916         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10917         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
10918                         ("sqlite3_step fail:%d", rc));
10919         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10920                         ("sqlite3_step fail:%d", rc));
10921
10922         if (sqlite3_changes(local_db_handle) == 0)
10923                 EM_DEBUG_LOG("no mail_meeting_tbl matched...");
10924
10925         ret = true;
10926
10927 FINISH_OFF:
10928         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, error);
10929         if (error == EMAIL_ERROR_NONE) {
10930                 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME, account_id, input_mailbox_id, input_new_mailbox_name, 0))
10931                         EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAILBOX_RENAME ] >>>> ");
10932         } else {
10933                 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME_FAIL, account_id, input_mailbox_id, input_new_mailbox_name, error))
10934                         EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAILBOX_RENAME_FAIL ] >>>> ");
10935         }
10936
10937         EM_SAFE_FREE(replaced_mailbox_name);
10938         EM_SAFE_FREE(replaced_alias);
10939
10940         if (old_mailbox_data)
10941                 emstorage_free_mailbox(&old_mailbox_data, 1, NULL);
10942
10943         if (hStmt != NULL) {
10944                 rc = sqlite3_finalize(hStmt);
10945                 if (rc != SQLITE_OK) {
10946                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10947                         error = EMAIL_ERROR_DB_FAILURE;
10948                 }
10949         }
10950
10951         EM_DEBUG_FUNC_END("error [%d]", error);
10952         return error;
10953 }
10954
10955 INTERNAL_FUNC int emstorage_get_new_attachment_no(char *multi_user_name, int *attachment_no, int *err_code)
10956 {
10957         EM_DEBUG_FUNC_BEGIN("attachment_no [%p], err_code[%p]", attachment_no, err_code);
10958         int rc, ret = false;
10959         int error = EMAIL_ERROR_NONE;
10960         char *sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
10961         char **result;
10962
10963         if (!attachment_no) {
10964                 EM_DEBUG_EXCEPTION("Invalid attachment");
10965                 if (err_code != NULL)
10966                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10967                 return false;
10968         }
10969
10970         *attachment_no = -1;
10971
10972         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10973
10974
10975         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
10976         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10977                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
10978
10979         if (NULL == result[1])
10980                 rc = 1;
10981         else
10982                 rc = atoi(result[1])+1;
10983
10984         sqlite3_free_table(result);
10985
10986         *attachment_no = rc;
10987         EM_DEBUG_LOG("attachment_no [%d]", *attachment_no);
10988         ret = true;
10989
10990 FINISH_OFF:
10991
10992         if (err_code != NULL)
10993                 *err_code = error;
10994
10995         EM_DEBUG_FUNC_END("ret [%d]", ret);
10996         return ret;
10997 }
10998
10999 INTERNAL_FUNC int emstorage_add_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int iscopy, int transaction, int *err_code)
11000 {
11001         EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], iscopy[%d], transaction[%d], err_code[%p]", attachment_tbl, iscopy, transaction, err_code);
11002
11003         char *sql = NULL;
11004         char **result;
11005         int rc, ret = false;
11006         int error = EMAIL_ERROR_NONE;
11007         char sql_query_string[QUERY_SIZE] = {0, };
11008
11009         DB_STMT hStmt = NULL;
11010         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11011
11012         if (!attachment_tbl) {
11013                 EM_DEBUG_EXCEPTION("attachment_tbl[%p], iscopy[%d]", attachment_tbl, iscopy);
11014                 if (err_code != NULL)
11015                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11016                 return false;
11017         }
11018
11019         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11020
11021         sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
11022
11023         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
11024         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
11025                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
11026
11027         if (NULL == result[1]) rc = 1;
11028         else rc = atoi(result[1]) + 1;
11029         sqlite3_free_table(result);
11030
11031         attachment_tbl->attachment_id = rc;
11032
11033         SNPRINTF(sql_query_string, sizeof(sql_query_string),
11034                         "INSERT INTO mail_attachment_tbl VALUES "
11035                         "(?"    /* attachment_id */
11036                         ", ?"   /* attachment_name */
11037                         ", ?"   /* attachment_path */
11038                         ", ?"   /* content_id */
11039                         ", ?"   /* attachment_size */
11040                         ", ?"   /* mail_id */
11041                         ", ?"   /* account_id */
11042                         ", ?"   /* mailbox_id */
11043                         ", ?"   /* attachment_save_status */
11044                         ", ?"   /* attachment_drm_type */
11045                         ", ?"   /* attachment_drm_method */
11046                         ", ?"   /* attachment_inline_content_status */
11047                         ", ?"   /* attachment_mime_type */
11048 #ifdef __ATTACHMENT_OPTI__
11049                         ", ?"
11050                         ", ?"
11051 #endif
11052                         ")");
11053
11054
11055         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11056         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11057                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11058
11059         _bind_stmt_field_data_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_id);
11060         _bind_stmt_field_data_string(hStmt, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->attachment_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
11061         _bind_stmt_field_data_string(hStmt, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
11062         _bind_stmt_field_data_string(hStmt, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->content_id, 0, CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL);
11063         _bind_stmt_field_data_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_size);
11064         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mail_id);
11065         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->account_id);
11066         _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mailbox_id);
11067         _bind_stmt_field_data_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_save_status);
11068         _bind_stmt_field_data_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_type);
11069         _bind_stmt_field_data_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_method);
11070         _bind_stmt_field_data_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_inline_content_status);
11071         _bind_stmt_field_data_string(hStmt, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->attachment_mime_type, 0, ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL);
11072 #ifdef __ATTACHMENT_OPTI__
11073         _bind_stmt_field_data_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->encoding);
11074         _bind_stmt_field_data_string(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->section, 0, ATTACHMENT_LEN_IN_MAIL_ATTACHMENT_TBL);
11075 #endif
11076
11077
11078         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11079         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
11080                         ("sqlite3_step fail:%d", rc));
11081         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11082                         ("sqlite3_step fail:%d", rc));
11083
11084         rc = sqlite3_changes(local_db_handle);
11085         if (rc == 0) {
11086                 EM_DEBUG_LOG(" no matched mail found...");
11087                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11088                 goto FINISH_OFF;
11089         }
11090
11091         ret = true;
11092
11093 FINISH_OFF:
11094         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11095
11096         if (hStmt != NULL) {
11097                 rc = sqlite3_finalize(hStmt);
11098                 if (rc != SQLITE_OK) {
11099                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11100                         error = EMAIL_ERROR_DB_FAILURE;
11101                 }
11102         }
11103
11104         if (err_code != NULL)
11105                 *err_code = error;
11106
11107         EM_DEBUG_FUNC_END("ret [%d]", ret);
11108         return ret;
11109 }
11110
11111 INTERNAL_FUNC int emstorage_update_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int transaction, int *err_code)
11112 {
11113         EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], transaction[%d], err_code[%p]", attachment_tbl, transaction, err_code);
11114
11115         int rc, ret = false, field_idx = 0;
11116         int error = EMAIL_ERROR_NONE;
11117         DB_STMT hStmt = NULL;
11118         char sql_query_string[QUERY_SIZE] = {0, };
11119
11120         if (!attachment_tbl) {
11121                 EM_DEBUG_EXCEPTION(" attachment_tbl[%p] ", attachment_tbl);
11122                 if (err_code)
11123                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11124                 return false;
11125         }
11126
11127         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11128
11129         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11130
11131         SNPRINTF(sql_query_string, sizeof(sql_query_string),
11132                         "UPDATE mail_attachment_tbl SET  "
11133                         "  attachment_name = ?"
11134                         ", attachment_path =  ?"
11135                         ", content_id = ?"
11136                         ", attachment_size = ?"
11137                         ", mail_id = ?"
11138                         ", account_id = ?"
11139                         ", mailbox_id = ?"
11140                         ", attachment_save_status = ?"
11141                         ", attachment_drm_type = ?"
11142                         ", attachment_drm_method = ?"
11143                         ", attachment_inline_content_status = ? "
11144                         ", attachment_mime_type = ? "
11145                         " WHERE attachment_id = ?;");
11146
11147
11148         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11149
11150         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11151                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11152
11153         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
11154         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
11155         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->content_id, 0, CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL);
11156         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_size);
11157         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->mail_id);
11158         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->account_id);
11159         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->mailbox_id);
11160         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_save_status);
11161         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_drm_type);
11162         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_drm_method);
11163         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_inline_content_status);
11164         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_mime_type, 0, ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL);
11165         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_id);
11166
11167
11168         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11169         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
11170                         ("sqlite3_step fail:%d", rc));
11171         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11172                         ("sqlite3_step fail:%d", rc));
11173
11174         rc = sqlite3_changes(local_db_handle);
11175         if (rc == 0) {
11176                 EM_DEBUG_LOG(" no matched mail found...");
11177                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11178                 goto FINISH_OFF;
11179         }
11180
11181         ret = true;
11182
11183 FINISH_OFF:
11184         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11185         if (hStmt != NULL) {
11186                 rc = sqlite3_finalize(hStmt);
11187                 if (rc != SQLITE_OK) {
11188                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11189                         error = EMAIL_ERROR_DB_FAILURE;
11190                 }
11191         }
11192
11193         if (err_code != NULL)
11194                 *err_code = error;
11195
11196         EM_DEBUG_FUNC_END("ret [%d]", ret);
11197         return ret;
11198 }
11199
11200 INTERNAL_FUNC int emstorage_delete_attachment_on_db(char *multi_user_name, int attachment_id, int transaction, int *err_code)
11201 {
11202         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], transaction[%d], err_code[%p]", attachment_id, transaction, err_code);
11203
11204         if (attachment_id < 0) {
11205                 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
11206                 if (err_code != NULL)
11207                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11208                 return false;
11209         }
11210
11211         int ret = false;
11212         int error = EMAIL_ERROR_NONE;
11213         char sql_query_string[QUERY_SIZE] = {0, };
11214         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11215
11216         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11217
11218         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
11219
11220         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11221         if (error != EMAIL_ERROR_NONE) {
11222                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11223                 goto FINISH_OFF;
11224         }
11225
11226         ret = true;
11227
11228 FINISH_OFF:
11229         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11230
11231         if (err_code)
11232                 *err_code = error;
11233
11234         EM_DEBUG_FUNC_END("ret [%d]", ret);
11235         return ret;
11236 }
11237
11238 INTERNAL_FUNC int emstorage_delete_all_attachments_of_mail(char *multi_user_name, int mail_id, int transaction, int *err_code)
11239 {
11240         EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
11241         int ret = false;
11242         int error = EMAIL_ERROR_NONE;
11243         char sql_query_string[QUERY_SIZE] = {0, };
11244         sqlite3 *local_db_handle = NULL;
11245
11246         if (mail_id <= 0) {
11247                 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
11248                 if (err_code != NULL)
11249                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11250                 return false;
11251         }
11252
11253         local_db_handle = emstorage_get_db_connection(multi_user_name);
11254
11255         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11256
11257         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
11258         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11259         if (error != EMAIL_ERROR_NONE) {
11260                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11261                 goto FINISH_OFF;
11262         }
11263
11264         ret = true;
11265
11266 FINISH_OFF:
11267         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11268
11269         if (err_code)
11270                 *err_code = error;
11271
11272         EM_DEBUG_FUNC_END("ret [%d]", ret);
11273         return ret;
11274 }
11275
11276 INTERNAL_FUNC int emstorage_delete_attachment_all_on_db(char *multi_user_name, int account_id, char *mailbox, int transaction, int *err_code)
11277 {
11278         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox, transaction, err_code);
11279
11280         int error = EMAIL_ERROR_NONE;
11281         int ret = false;
11282         char sql_query_string[QUERY_SIZE] = {0, };
11283         char *replaced_mailbox = NULL;
11284         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11285
11286         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11287
11288         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl");
11289
11290         if (account_id != ALL_ACCOUNT) /*  '0' means all account */
11291                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE account_id = %d", account_id);
11292
11293         if (mailbox) /*  NULL means all mailbox_name */ {
11294                 if (strstr(mailbox, "'"))
11295                         replaced_mailbox = em_replace_all_string(mailbox, "'", "''");
11296                 else
11297                         replaced_mailbox = strdup(mailbox);
11298
11299                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s mailbox_name = '%s'", account_id != ALL_ACCOUNT ? "AND" : "WHERE", replaced_mailbox);
11300                 EM_SAFE_FREE(replaced_mailbox); /*prevent 49434*/
11301         }
11302
11303         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11304         if (error != EMAIL_ERROR_NONE) {
11305                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11306                 goto FINISH_OFF;
11307         }
11308
11309         ret = true;
11310
11311 FINISH_OFF:
11312         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11313
11314         if (err_code != NULL)
11315                 *err_code = error;
11316
11317         EM_DEBUG_FUNC_END("ret [%d]", ret);
11318         return ret;
11319 }
11320
11321 INTERNAL_FUNC int emstorage_free_attachment(emstorage_attachment_tbl_t** attachment_tbl_list, int count, int *err_code)
11322 {
11323         EM_DEBUG_FUNC_BEGIN("attachment_tbl_list[%p], count[%d], err_code[%p]", attachment_tbl_list, count, err_code);
11324
11325         if (count > 0) {
11326                 if ((attachment_tbl_list == NULL) || (*attachment_tbl_list == NULL)) {
11327                         EM_DEBUG_LOG("Nothing to free: attachment_tbl_list[%p], count[%d]", attachment_tbl_list, count);
11328                         if (err_code != NULL)
11329                                 *err_code = EMAIL_ERROR_INVALID_PARAM;
11330                         return false;
11331                 }
11332
11333                 emstorage_attachment_tbl_t* p = *attachment_tbl_list;
11334                 int i;
11335
11336                 for (i = 0; i < count; i++) {
11337                         EM_SAFE_FREE(p[i].attachment_name);
11338                         EM_SAFE_FREE(p[i].attachment_path);
11339                         EM_SAFE_FREE(p[i].content_id);
11340                         EM_SAFE_FREE(p[i].attachment_mime_type);
11341 #ifdef __ATTACHMENT_OPTI__
11342                         EM_SAFE_FREE(p[i].section);
11343 #endif
11344                 }
11345
11346                 EM_SAFE_FREE(p);
11347                 *attachment_tbl_list = NULL;
11348         }
11349
11350         if (err_code != NULL)
11351                 *err_code = EMAIL_ERROR_NONE;
11352         EM_DEBUG_FUNC_END();
11353         return true;
11354 }
11355
11356 INTERNAL_FUNC int emstorage_begin_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11357 {
11358         EM_DEBUG_FUNC_BEGIN();
11359         EM_PROFILE_BEGIN(emStorageBeginTransaction);
11360         int ret = true;
11361
11362         _timedlock_shm_mutex(mapped_for_db_lock, 2);
11363
11364         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11365
11366         int rc;
11367         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN immediate;", NULL, NULL, NULL), rc);
11368         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11369                         ("SQL(BEGIN) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11370
11371         if (ret == false) {
11372                 if (err_code != NULL) *err_code = EMAIL_ERROR_DB_FAILURE;
11373         }
11374
11375         EM_PROFILE_END(emStorageBeginTransaction);
11376         EM_DEBUG_FUNC_END("ret [%d]", ret);
11377         return ret;
11378 }
11379
11380 INTERNAL_FUNC int emstorage_commit_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11381 {
11382         EM_DEBUG_FUNC_BEGIN();
11383         int ret = true;
11384         int rc;
11385
11386         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11387
11388         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
11389         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; }, ("SQL(END) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11390
11391         if (ret == false && err_code != NULL)
11392                 *err_code = EMAIL_ERROR_DB_FAILURE;
11393
11394         _unlockshm_mutex(mapped_for_db_lock);
11395
11396         EM_DEBUG_FUNC_END("ret [%d]", ret);
11397         return ret;
11398 }
11399
11400 INTERNAL_FUNC int emstorage_rollback_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11401 {
11402         EM_DEBUG_FUNC_BEGIN();
11403         int ret = true;
11404         int rc;
11405         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11406
11407         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "ROLLBACK;", NULL, NULL, NULL), rc);
11408         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11409                         ("SQL(ROLLBACK) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11410
11411         if (ret == false && err_code != NULL)
11412                 *err_code = EMAIL_ERROR_DB_FAILURE;
11413
11414         _unlockshm_mutex(mapped_for_db_lock);
11415
11416         EM_DEBUG_FUNC_END("ret [%d]", ret);
11417         return ret;
11418 }
11419
11420 INTERNAL_FUNC int emstorage_is_mailbox_full(char *multi_user_name, int account_id, email_mailbox_t *mailbox, int *result, int *err_code)
11421 {
11422         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], result[%p], err_code[%p]", account_id, mailbox, result, err_code);
11423
11424         if (account_id < FIRST_ACCOUNT_ID || !mailbox || !result) {
11425                 if (mailbox)
11426                         EM_DEBUG_EXCEPTION("Invalid Parameter. accoun_id[%d], mailbox[%p]", account_id, mailbox);
11427
11428                 if (err_code != NULL)
11429                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11430
11431                 return false;
11432         }
11433
11434         int ret = false;
11435         int error = EMAIL_ERROR_NONE;
11436         int mail_count = 0;
11437
11438         if (!emstorage_get_mail_count(multi_user_name, account_id, mailbox->mailbox_id, &mail_count, NULL, true, &error)) {
11439                 EM_DEBUG_EXCEPTION("emstorage_get_mail_count failed [%d]", error);
11440                 goto FINISH_OFF;
11441         }
11442
11443         if (mailbox) {
11444                 EM_DEBUG_LOG("mail_count[%d] mail_slot_size[%d]", mail_count, mailbox->mail_slot_size);
11445                 if (mail_count >= mailbox->mail_slot_size)
11446                         *result = true;
11447                 else
11448                         *result = false;
11449
11450                 ret = true;
11451         }
11452
11453         ret = true;
11454 FINISH_OFF:
11455
11456         if (err_code != NULL)
11457                 *err_code = error;
11458
11459         EM_DEBUG_FUNC_END("ret [%d]", ret);
11460         return ret;
11461 }
11462
11463 INTERNAL_FUNC int emstorage_clear_mail_data(char *multi_user_name, int transaction, int *err_code)
11464 {
11465         EM_DEBUG_FUNC_BEGIN("transaction[%d], err_code[%p]", transaction, err_code);
11466
11467         int ret = false;
11468         int error = EMAIL_ERROR_NONE;
11469         char sql_query_string[QUERY_SIZE] = {0, };
11470         const email_db_object_t* tables = _g_db_tables;
11471         const email_db_object_t* indexes = _g_db_indexes;
11472
11473         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11474         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11475
11476         if (!emstorage_delete_dir((char *)MAIL_HOME, &error)) {
11477                 EM_DEBUG_EXCEPTION(" emstorage_delete_dir failed - %d", error);
11478
11479                 goto FINISH_OFF;
11480         }
11481
11482         mkdir(MAIL_HOME, DIRECTORY_PERMISSION);
11483         mkdir(MAIL_TEMP, DIRECTORY_PERMISSION);
11484         chmod(MAIL_TEMP, 0777);
11485
11486         /*  first clear index. */
11487         while (indexes->object_name) {
11488                 if (indexes->data_flag) {
11489                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP index %s", indexes->object_name);
11490                         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11491                         if (error != EMAIL_ERROR_NONE) {
11492                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11493                                 goto FINISH_OFF;
11494                         }
11495                 }
11496                 indexes++;
11497         }
11498
11499         while (tables->object_name) {
11500                 if (tables->data_flag) {
11501                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP table %s", tables->object_name);
11502                         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11503                         if (error != EMAIL_ERROR_NONE) {
11504                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11505                                 goto FINISH_OFF;
11506                         }
11507                 }
11508
11509                 tables++;
11510         }
11511         ret = true;
11512
11513 FINISH_OFF:
11514         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11515
11516         if (err_code != NULL)
11517                 *err_code = error;
11518
11519         EM_DEBUG_FUNC_END("ret [%d]", ret);
11520         return ret;
11521 }
11522 /*======================= DB File Utils =============================================*/
11523 #include <dirent.h>
11524 #include <sys/types.h>
11525 #define  DIR_SEPERATOR "/"
11526
11527 INTERNAL_FUNC char *emstorage_make_directory_path_from_file_path(char *file_name)
11528 {
11529         EM_DEBUG_FUNC_BEGIN("Filename [ %p ]", file_name);
11530         char delims[] = "/";
11531         char *result = NULL;
11532         gchar **token = NULL;
11533
11534         token = g_strsplit_set(file_name, delims, 1);
11535
11536         if (token && token[0]) {
11537                 EM_DEBUG_LOG_SEC(">>>> Directory_name [ %s ]", token[0]);
11538                 result = EM_SAFE_STRDUP(token[0]);
11539         } else
11540                 EM_DEBUG_LOG(">>>> No Need to create Directory");
11541
11542         g_strfreev(token);
11543
11544         return result;
11545 }
11546
11547 INTERNAL_FUNC int emstorage_get_save_name(char *multi_user_name, int account_id, int mail_id, int atch_id,
11548                 char *fname, char *move_buf, char *path_buf, int maxlen, int *err_code)
11549 {
11550         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], mail_id[%d], atch_id[%d], fname[%s], move_buf[%p], path_buf[%p], err_code[%p]", account_id, mail_id, atch_id, fname, move_buf, path_buf, err_code);
11551         EM_PROFILE_BEGIN(profile_emstorage_get_save_name);
11552
11553         int ret = false;
11554         int error = EMAIL_ERROR_NONE;
11555         char *dir_name = NULL;
11556         char create_dir[1024] = {0};
11557         char *temp_file = NULL;
11558         char *prefix_path = NULL;
11559         char *modified_fname = NULL;
11560
11561         if (!move_buf || !path_buf || account_id < FIRST_ACCOUNT_ID || mail_id < 0 || atch_id < 0) {
11562                 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d], fname[%p], move_buf[%p], path_buf[%p]", account_id, mail_id, atch_id, fname, move_buf, path_buf);
11563                 error = EMAIL_ERROR_INVALID_PARAM;
11564                 goto FINISH_OFF;
11565         }
11566
11567         snprintf(path_buf, 512, "%s", MAIL_HOME);
11568         snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, account_id);
11569
11570         if (mail_id > 0)
11571                 snprintf(path_buf+EM_SAFE_STRLEN(path_buf),   512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, mail_id);
11572
11573         if (atch_id > 0)
11574                 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, atch_id);
11575
11576         if (fname) {
11577                 temp_file = EM_SAFE_STRDUP(fname);
11578                 if (temp_file && strstr(temp_file, "/"))
11579                         dir_name = emstorage_make_directory_path_from_file_path(temp_file);
11580         }
11581
11582         if (dir_name) {
11583                 snprintf(create_dir, sizeof(create_dir), "%s%s%s", path_buf, DIR_SEPERATOR, dir_name);
11584                 EM_DEBUG_LOG(">>>>> DIR PATH [%s]", create_dir);
11585                 ret = mkdir(create_dir, DIRECTORY_PERMISSION);
11586                 EM_DEBUG_LOG("mkdir result = [%d]", ret);
11587                 EM_SAFE_FREE(dir_name);
11588         }
11589
11590         if (fname) {
11591                 EM_DEBUG_LOG_DEV(">>>>> fname [%s]", fname);
11592
11593                 /* Did not allow the slash */
11594                 modified_fname = reg_replace_new(fname, "/", "_");
11595                 EM_DEBUG_LOG_SEC("modified_fname : [%s]", modified_fname);
11596
11597                 if (modified_fname == NULL) modified_fname = g_strdup(fname);
11598
11599                 if (EM_SAFE_STRLEN(modified_fname) + EM_SAFE_STRLEN(path_buf) + strlen(DIR_SEPERATOR) > maxlen - 1) {
11600                         char *modified_name = NULL;
11601                         int remain_len  = (maxlen - 1) - EM_SAFE_STRLEN(path_buf) - strlen(DIR_SEPERATOR);
11602
11603                         if (remain_len <= 0) {
11604                                 error = EMAIL_ERROR_MAX_EXCEEDED;
11605                                 goto FINISH_OFF;
11606                         }
11607
11608                         if (remain_len > MAX_FILENAME)
11609                                 remain_len = MAX_FILENAME;
11610
11611                         modified_name = em_shrink_filename(modified_fname, remain_len);
11612
11613                         if (!modified_name) {
11614                                 error = EMAIL_ERROR_MAX_EXCEEDED;
11615                                 goto FINISH_OFF;
11616                         }
11617
11618                         snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),
11619                                 "%s%s", DIR_SEPERATOR, modified_name);
11620                         EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11621                         EM_SAFE_FREE(modified_name);
11622                 } else {
11623                         if (EM_SAFE_STRLEN(modified_fname) > MAX_FILENAME - 1) {
11624                                 char *modified_name = NULL;
11625
11626                                 modified_name = em_shrink_filename(modified_fname, MAX_FILENAME);
11627                                 if (!modified_name) {
11628                                         error = EMAIL_ERROR_MAX_EXCEEDED;
11629                                         goto FINISH_OFF;
11630                                 }
11631
11632                                 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),
11633                                         "%s%s", DIR_SEPERATOR, modified_name);
11634                                 EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11635                                 EM_SAFE_FREE(modified_name);
11636                         } else {
11637                                 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),
11638                                         "%s%s", DIR_SEPERATOR, modified_fname);
11639                         }
11640                 }
11641         }
11642
11643         EM_DEBUG_LOG_SEC(">>>>> path_buf [%s]", path_buf);
11644
11645         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11646                 error = emcore_get_container_path(multi_user_name, &prefix_path);
11647                 if (error != EMAIL_ERROR_NONE) {
11648                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11649                         goto FINISH_OFF;
11650                 }
11651                 snprintf(move_buf, 512, "%s/%s", prefix_path, path_buf);
11652                 EM_DEBUG_LOG_SEC("move_buf : [%s]", move_buf);
11653         } else {
11654                 snprintf(move_buf, 512, "%s", path_buf);
11655                 EM_DEBUG_LOG_SEC("move_buf : [%s]", move_buf);
11656         }
11657
11658         ret = true;
11659
11660 FINISH_OFF:
11661
11662         EM_SAFE_FREE(temp_file);
11663         EM_SAFE_FREE(prefix_path);
11664         EM_SAFE_FREE(modified_fname);
11665
11666         if (err_code != NULL)
11667                 *err_code = error;
11668
11669         EM_PROFILE_END(profile_emstorage_get_save_name);
11670         EM_DEBUG_FUNC_END("ret [%d]", ret);
11671         return ret;
11672 }
11673
11674 /*
11675    INTERNAL_FUNC int emstorage_get_dele_name(char *multi_user_name, int account_id, int mail_id, int atch_id, char *fname, char *name_buf, int *err_code)
11676    {
11677    EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], fname[%p], name_buf[%p], err_code[%p]", account_id, mail_id, atch_id, fname, name_buf, err_code);
11678
11679    if (!name_buf || account_id < FIRST_ACCOUNT_ID) {
11680    EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d], fname[%p], name_buf[%p]", account_id, mail_id, atch_id, fname, name_buf);
11681    if (err_code != NULL)
11682  *err_code = EMAIL_ERROR_INVALID_PARAM;
11683  return false;
11684  }
11685
11686  sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
11687
11688  if (mail_id > 0)
11689  sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, mail_id);
11690  else
11691  goto FINISH_OFF;
11692
11693  if (atch_id > 0)
11694  sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, atch_id);
11695  else
11696  goto FINISH_OFF;
11697
11698 FINISH_OFF:
11699 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), ".DELE");
11700
11701 EM_DEBUG_FUNC_END();
11702 return true;
11703 }
11704 */
11705
11706 INTERNAL_FUNC int emstorage_create_dir(char *multi_user_name, int account_id, int mail_id, int atch_id, int *err_code)
11707 {
11708         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], err_code[%p]", account_id, mail_id, atch_id, err_code);
11709         EM_PROFILE_BEGIN(profile_emcore_save_create_dir);
11710         int ret = false;
11711         int error = EMAIL_ERROR_NONE;
11712         char buf[512];
11713         struct stat sbuf;
11714         char *prefix_path = NULL;
11715         char errno_buf[ERRNO_BUF_SIZE] = {0};
11716
11717         memset(buf, 0x00, sizeof(buf));
11718
11719         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11720                 error = emcore_get_container_path(multi_user_name, &prefix_path);
11721                 if (error != EMAIL_ERROR_NONE) {
11722                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11723                         goto FINISH_OFF;
11724                 }
11725         } else {
11726                 prefix_path = strdup("");
11727         }
11728
11729         if (account_id >= FIRST_ACCOUNT_ID) {
11730                 SNPRINTF(buf, sizeof(buf), "%s%s%s%s%d", prefix_path,
11731                                 DIR_SEPERATOR,
11732                                 MAIL_HOME,
11733                                 DIR_SEPERATOR,
11734                                 account_id);
11735
11736                 if (stat(buf, &sbuf) == 0) {
11737                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11738                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11739                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11740                                 goto FINISH_OFF;
11741                         }
11742                 } else {
11743                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11744                                 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11745                                 EM_DEBUG_EXCEPTION("mkdir failed: %s", EM_STRERROR(errno_buf));
11746                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11747                                 if (errno == 28)
11748                                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11749                                 goto FINISH_OFF;
11750                         }
11751
11752                         if (account_id == EML_FOLDER)
11753                                 chmod(buf, 0777);
11754                 }
11755         }
11756
11757         if (mail_id > 0) {
11758                 int space_left_in_buffer = sizeof(buf) - EM_SAFE_STRLEN(buf);
11759
11760                 if (account_id < FIRST_ACCOUNT_ID) {
11761                         EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11762                         error = EMAIL_ERROR_INVALID_PARAM;
11763                         goto FINISH_OFF;
11764                 }
11765
11766                 if (space_left_in_buffer + 10 > sizeof(buf)) {
11767                         EM_DEBUG_EXCEPTION("Buffer overflowed");
11768                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11769                         goto FINISH_OFF;
11770                 }
11771
11772                 SNPRINTF(buf+EM_SAFE_STRLEN(buf), space_left_in_buffer, "%s%d", DIR_SEPERATOR, mail_id);
11773
11774                 if (stat(buf, &sbuf) == 0) {
11775                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11776                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11777                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11778                                 goto FINISH_OFF;
11779                         }
11780                 } else {
11781                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11782                                 EM_DEBUG_EXCEPTION("mkdir failed [%s]", buf);
11783                                 EM_DEBUG_EXCEPTION("mkdir failed [%d][%s]", errno, EM_STRERROR(errno_buf));
11784                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11785                                 if (errno == 28)
11786                                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11787                                 goto FINISH_OFF;
11788                         }
11789
11790                         if (account_id == EML_FOLDER)
11791                                 chmod(buf, 0777);
11792                 }
11793         }
11794
11795         if (atch_id > 0) {
11796                 if (account_id < FIRST_ACCOUNT_ID || mail_id <= 0) {
11797                         EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11798                         error = EMAIL_ERROR_INVALID_PARAM;
11799                         goto FINISH_OFF;
11800                 }
11801
11802                 SNPRINTF(buf+EM_SAFE_STRLEN(buf), sizeof(buf)-(EM_SAFE_STRLEN(buf)+1), "%s%d", DIR_SEPERATOR, atch_id);
11803
11804                 if (stat(buf, &sbuf) == 0) {
11805                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11806                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11807                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11808                                 goto FINISH_OFF;
11809                         }
11810                 } else {
11811                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11812                                 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11813                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11814                                 if (errno == 28)
11815                                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11816                                 goto FINISH_OFF;
11817                         }
11818
11819                         if (account_id == EML_FOLDER)
11820                                 chmod(buf, 0777);
11821                 }
11822         }
11823
11824         ret = true;
11825
11826 FINISH_OFF:
11827
11828         EM_SAFE_FREE(prefix_path);
11829
11830         if (err_code != NULL)
11831                 *err_code = error;
11832
11833         EM_PROFILE_END(profile_emcore_save_create_dir);
11834         EM_DEBUG_FUNC_END("ret [%d]", ret);
11835         return ret;
11836 }
11837
11838 INTERNAL_FUNC int emstorage_copy_file(char *src_file, char *dst_file, int sync_status, int *err_code)
11839 {
11840         EM_DEBUG_FUNC_BEGIN_SEC("src_file[%s], dst_file[%s], err_code[%p]", src_file, dst_file, err_code);
11841         EM_DEBUG_LOG("Using the fsync function");
11842         int ret = false;
11843         int error = EMAIL_ERROR_NONE;
11844         struct stat st_buf;
11845
11846         int fp_src = 0;
11847         int fp_dst = 0;
11848         int nread = 0;
11849         char buf[FILE_MAX_BUFFER_SIZE] = {0};
11850         char errno_buf[ERRNO_BUF_SIZE] = {0};
11851
11852         if (!src_file || !dst_file) {
11853                 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
11854
11855                 error = EMAIL_ERROR_INVALID_PARAM;
11856                 goto FINISH_OFF;
11857         }
11858
11859         if (stat(src_file, &st_buf) < 0) {
11860                 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", src_file);
11861
11862                 error = EMAIL_ERROR_SYSTEM_FAILURE;             /* EMAIL_ERROR_INVALID_PATH; */
11863                 goto FINISH_OFF;
11864         }
11865
11866         error = em_open(src_file, O_RDONLY, 0, &fp_src);
11867         if (error != EMAIL_ERROR_NONE) {
11868                 EM_DEBUG_EXCEPTION(">>>> Source Fail em_open %s Failed: %d", src_file, error);
11869                 goto FINISH_OFF;
11870         }
11871
11872         error = em_open(dst_file, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, &fp_dst); /*prevent 24474*/
11873         if (error != EMAIL_ERROR_NONE) {
11874                 EM_DEBUG_EXCEPTION(">>>> Destination Fail em_open %s:  %d", dst_file, error);
11875                 goto FINISH_OFF;
11876         }
11877
11878         memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
11879
11880         while ((nread = read(fp_src, buf, FILE_MAX_BUFFER_SIZE)) > 0) {
11881                 if (nread > 0 && nread <= FILE_MAX_BUFFER_SIZE) {
11882                         EM_DEBUG_LOG("Nread Value [%d]", nread);
11883                         char *buf_ptr;
11884                         ssize_t byte_written = 0;
11885                         size_t remain_byte = nread;
11886                         buf_ptr = buf;
11887                         errno = 0;
11888
11889                         while (remain_byte > 0 && buf_ptr && errno == 0) {
11890                                 byte_written = write(fp_dst, buf_ptr, remain_byte);
11891
11892                                 if (byte_written < 0) {
11893                                         /* interrupted by a signal */
11894                                         if (errno == EINTR) {
11895                                                 errno = 0;
11896                                                 continue;
11897                                         }
11898
11899                                         EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
11900                                         error = EMAIL_ERROR_UNKNOWN;
11901                                         goto FINISH_OFF;
11902                                 }
11903                                 EM_DEBUG_LOG("NWRITTEN [%d]", byte_written);
11904                                 remain_byte -= byte_written;
11905                                 buf_ptr += byte_written;
11906                         }
11907                 }
11908
11909                 memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
11910         }
11911
11912         ret = true;
11913
11914 FINISH_OFF:
11915         EM_SAFE_CLOSE(fp_src);
11916
11917         if (fp_dst >= 0) { /*prevent 24474*/
11918                 if (sync_status) {
11919                         EM_DEBUG_LOG("Before fsync");
11920                         fsync(fp_dst);
11921                 }
11922                 close(fp_dst);
11923         }
11924
11925         if (nread < 0 || error == EMAIL_ERROR_UNKNOWN)
11926                 remove(dst_file);
11927
11928         if (err_code != NULL)
11929                 *err_code = error;
11930         EM_DEBUG_FUNC_END("ret [%d]", ret);
11931         return ret;
11932 }
11933 /* create Directory if user has deleted [deepam.p@samsung.com] */
11934 INTERNAL_FUNC void emstorage_create_dir_if_delete()
11935 {
11936         EM_DEBUG_FUNC_BEGIN();
11937
11938         mkdir(DATA_PATH, DIRECTORY_PERMISSION);
11939         mkdir(EMAIL_PATH, DIRECTORY_PERMISSION);
11940         mkdir(MAIL_HOME, DIRECTORY_PERMISSION);
11941         mkdir(MAIL_TEMP, DIRECTORY_PERMISSION);
11942         chmod(MAIL_TEMP, 0777);
11943
11944         EM_DEBUG_FUNC_END();
11945 }
11946
11947 static int _get_temp_file_name(char **filename, int *err_code)
11948 {
11949         EM_DEBUG_FUNC_BEGIN("filename[%p], err_code[%p]", filename, err_code);
11950
11951         int ret = false;
11952         int error = EMAIL_ERROR_NONE;
11953
11954         if (filename == NULL) {
11955                 EM_DEBUG_EXCEPTION(" filename[%p]", filename);
11956                 error = EMAIL_ERROR_INVALID_PARAM;
11957                 goto FINISH_OFF;
11958         }
11959
11960         char tempname[512] = {0x00, };
11961         struct timeval tv;
11962
11963         gettimeofday(&tv, NULL);
11964         srand(tv.tv_usec);
11965         unsigned int seed = time(NULL);
11966         SNPRINTF(tempname, sizeof(tempname), "%s%c%d", MAIL_TEMP, '/', rand_r(&seed));
11967
11968         char *p = EM_SAFE_STRDUP(tempname);
11969         if (p == NULL) {
11970                 EM_DEBUG_EXCEPTION(" strdup failed...");
11971                 error = EMAIL_ERROR_OUT_OF_MEMORY;
11972                 goto FINISH_OFF;
11973         }
11974
11975         *filename = p;
11976
11977         ret = true;
11978
11979 FINISH_OFF:
11980         if (err_code != NULL)
11981                 *err_code = error;
11982
11983         EM_DEBUG_FUNC_END("ret [%d]", ret);
11984         return ret;
11985 }
11986
11987 INTERNAL_FUNC int emstorage_add_content_type(char *file_path, char *char_set, int *err_code)
11988 {
11989         EM_DEBUG_FUNC_BEGIN("File path [ %p ]  Character Set [ %p ] err_code [ %p]", file_path, char_set, err_code);
11990
11991         EM_IF_NULL_RETURN_VALUE(file_path, false);
11992         EM_IF_NULL_RETURN_VALUE(char_set, false);
11993         EM_IF_NULL_RETURN_VALUE(err_code, false);
11994
11995         char *buf =  NULL;
11996         char *buf1 = NULL;
11997         struct stat st_buf;
11998         int buf_size = 0;
11999         char *low_char_set = NULL;
12000         char *match_str = NULL;
12001         int nwritten = 0;
12002         int ret = false;
12003         int error = EMAIL_ERROR_NONE;
12004         int data_count_to_written = 0;
12005         char *temp_file_name = NULL;
12006         int err = 0;
12007         FILE* fp_src = NULL;
12008         FILE* fp_dest = NULL;
12009         int nread = 0;
12010
12011
12012         if (stat(file_path, &st_buf) < 0) {
12013                 EM_DEBUG_EXCEPTION_SEC(" stat(\"%s\") failed...", file_path);
12014                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12015                 goto FINISH_OFF;
12016         }
12017
12018         buf_size =  st_buf.st_size;
12019         EM_DEBUG_LOG(">>>> File Size [ %d ] ", buf_size);
12020         buf = (char *)calloc(1, buf_size+1);
12021
12022         if (!buf) {
12023                 EM_DEBUG_LOG(">>> Memory cannot be allocated ");
12024                 goto FINISH_OFF;
12025         }
12026
12027         error = em_fopen(file_path, "rb", &fp_src);
12028         if (error != EMAIL_ERROR_NONE || fp_src == NULL) {
12029                 EM_DEBUG_EXCEPTION_SEC(" file_path fopen failed - %s [%d]", file_path, error);
12030                 goto FINISH_OFF;
12031         }
12032
12033         if ((nread = fread(buf, 1, buf_size, fp_src)) > 0) {
12034                 if (nread > 0 && nread <= buf_size) {
12035                         EM_DEBUG_LOG(">>>> Nread Value [ %d ] ", nread);
12036
12037                         /**
12038                          *   1.Add check for whether content type is there.
12039                          *   2. If not based on the character set, Append it in File
12040                          **/
12041
12042                         low_char_set = calloc(1, EM_SAFE_STRLEN(char_set) + strlen(" \" /></head>") +1); /*prevent 34359*/
12043                         if (low_char_set == NULL) {
12044                                 EM_DEBUG_EXCEPTION("calloc failed");
12045                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12046                                 goto FINISH_OFF;
12047                         }
12048
12049                         strncat(low_char_set, char_set, sizeof(low_char_set)-EM_SAFE_STRLEN(low_char_set)-1);
12050                         EM_DEBUG_LOG(">>>> CHAR SET [ %s ] ", low_char_set);
12051                         strncat(low_char_set, " \" /></head>", sizeof(low_char_set)-EM_SAFE_STRLEN(low_char_set)-1); /*prevent 34359*/
12052                         EM_DEBUG_LOG(">>> CHARSET [ %s ] ", low_char_set);
12053                         buf[nread] = '\0';
12054
12055                         match_str = strstr(buf, CONTENT_TYPE_DATA);
12056
12057                         if (match_str == NULL) {
12058                                 EM_DEBUG_LOG(">>>>emstorage_add_content_type 3 ");
12059                                 if (fp_src != NULL) {
12060                                         fclose(fp_src);
12061                                         fp_src = NULL;
12062                                 }
12063                                 data_count_to_written = EM_SAFE_STRLEN(low_char_set)+strlen(CONTENT_DATA)+1; /*prevent 34359*/
12064                                 buf1 = (char *)calloc(1, data_count_to_written);
12065
12066                                 if (buf1) {
12067                                         strncat(buf1, CONTENT_DATA, sizeof(buf1)-EM_SAFE_STRLEN(buf1)-1); /*prevent 34359*/
12068                                         EM_DEBUG_LOG(">>>>> BUF 1 [ %s ] ", buf1);
12069                                         strncat(buf1, low_char_set, sizeof(buf1)-EM_SAFE_STRLEN(buf1)-1);
12070                                         EM_DEBUG_LOG(">>>> HTML TAG DATA  [ %s ] ", buf1);
12071
12072                                         /* 1. Create a temporary file name */
12073                                         if (!_get_temp_file_name(&temp_file_name, &err)) {
12074                                                 EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed - %d", err);
12075                                                 if (err_code != NULL) *err_code = err;
12076                                                 goto FINISH_OFF;
12077                                         }
12078                                         EM_DEBUG_LOG_SEC(">>>>>>> TEMP APPEND FILE PATH [ %s ] ", temp_file_name);
12079
12080                                         /* Open the Temp file in Append mode */
12081                                         error = em_fopen(temp_file_name, "ab", &fp_dest);
12082                                         if (error != EMAIL_ERROR_NONE) {
12083                                                 EM_DEBUG_EXCEPTION_SEC(" fopen failed - %s [%d]", temp_file_name, error);
12084                                                 goto FINISH_OFF;
12085                                         }
12086
12087                                         /* 2. write the Latest data */
12088                                         nwritten = fwrite(buf1, data_count_to_written-1, 1, fp_dest);
12089
12090                                         if (nwritten > 0) {
12091                                                 EM_DEBUG_LOG(" Latest Data  : [%d ] bytes written ", nwritten);
12092                                                 nwritten = 0;
12093                                                 /* 3. Append old data */
12094                                                 nwritten = fwrite(buf, nread-1, 1, fp_dest);
12095
12096                                                 if (nwritten <= 0) {
12097                                                         EM_DEBUG_EXCEPTION(" Error Occured while writing Old data : [%d ] bytes written ", nwritten);
12098                                                         error = EMAIL_ERROR_SYSTEM_FAILURE;
12099                                                         goto FINISH_OFF;
12100                                                 } else {
12101                                                         EM_DEBUG_LOG(">>>> OLD data appended [ %d ] ", nwritten);
12102
12103                                                         if (!emstorage_move_file(temp_file_name, file_path, false, &err)) {
12104                                                                 EM_DEBUG_EXCEPTION(" emstorage_move_file failed - %d", err);
12105                                                                 goto FINISH_OFF;
12106                                                         }
12107                                                 }
12108
12109                                         } else {
12110                                                 EM_DEBUG_EXCEPTION(" Error Occured while writing New data : [%d ] bytes written ", nwritten);
12111                                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12112                                                 goto FINISH_OFF;
12113                                         }
12114                                 }
12115                         }
12116                 }
12117         }
12118
12119         ret = true;
12120 FINISH_OFF:
12121
12122         EM_SAFE_FREE(buf);
12123         EM_SAFE_FREE(buf1);
12124         EM_SAFE_FREE(low_char_set);
12125         EM_SAFE_FREE(temp_file_name);
12126
12127         if (fp_src != NULL) {
12128                 fclose(fp_src);
12129                 fp_src = NULL;
12130         }
12131
12132         if (fp_dest != NULL) {
12133                 fclose(fp_dest);
12134                 fp_dest = NULL;
12135         }
12136
12137         if (err_code)
12138                 *err_code = error;
12139
12140         EM_DEBUG_FUNC_END("ret [%d]", ret);
12141         return ret;
12142
12143 }
12144
12145 INTERNAL_FUNC int emstorage_move_file(char *src_file, char *dst_file, int sync_status, int *err_code)
12146 {
12147         EM_DEBUG_FUNC_BEGIN("src_file[%p], dst_file[%p], err_code[%p]", src_file, dst_file, err_code);
12148
12149         int ret = false;
12150         int error = EMAIL_ERROR_NONE;
12151         char errno_buf[ERRNO_BUF_SIZE] = {0};
12152
12153         if (src_file == NULL || dst_file == NULL) {
12154                 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
12155                 error = EMAIL_ERROR_INVALID_PARAM;
12156                 goto FINISH_OFF;
12157         }
12158
12159         EM_DEBUG_LOG_SEC("src_file[%s], dst_file[%s]", src_file, dst_file);
12160
12161         if (strcmp(src_file, dst_file) != 0) {
12162                 EM_DEBUG_LOG("oldpath and newpath are not on the same mounted file system.");
12163                 if (!emstorage_copy_file(src_file, dst_file, sync_status, &error)) {
12164                         EM_DEBUG_EXCEPTION("emstorage_copy_file failed - %d", error);
12165
12166                         struct stat temp_file_stat;
12167                         if (stat(src_file, &temp_file_stat) < 0)
12168                                 EM_DEBUG_EXCEPTION("no src file found [%s] : %s", src_file, EM_STRERROR(errno_buf));
12169
12170                         if (stat(dst_file, &temp_file_stat) < 0)
12171                                 EM_DEBUG_EXCEPTION("no dst file found [%s] : %s", dst_file, EM_STRERROR(errno_buf));
12172
12173                         error = EMAIL_ERROR_FILE_NOT_FOUND;
12174                         goto FINISH_OFF;
12175
12176
12177                 }
12178                 remove(src_file);
12179                 EM_DEBUG_LOG("src[%s] removed", src_file);
12180         } else {
12181                 EM_DEBUG_LOG("src[%s] = dst[%s]", src_file, dst_file);
12182         }
12183
12184         ret = true;
12185
12186 FINISH_OFF:
12187         if (err_code != NULL)
12188                 *err_code = error;
12189
12190         EM_DEBUG_FUNC_END("ret [%d]", ret);
12191         return ret;
12192 }
12193
12194 INTERNAL_FUNC int emstorage_delete_file(char *src_file, int *err_code)
12195 {
12196         EM_DEBUG_FUNC_BEGIN("src_file[%p], err_code[%p]", src_file, err_code);
12197
12198         int ret = false;
12199         int error = EMAIL_ERROR_NONE;
12200
12201         if (src_file == NULL) {
12202                 EM_DEBUG_EXCEPTION(" src_file[%p]", src_file);
12203
12204                 error = EMAIL_ERROR_INVALID_PARAM;
12205                 goto FINISH_OFF;
12206         }
12207
12208         if (remove(src_file) != 0) {
12209                 if (errno != ENOENT) {
12210                         EM_DEBUG_EXCEPTION(" remove failed - %d", errno);
12211
12212                         error = EMAIL_ERROR_SYSTEM_FAILURE;
12213                         goto FINISH_OFF;
12214                 } else {
12215                         EM_DEBUG_EXCEPTION(" no file found...");
12216
12217                         error = EMAIL_ERROR_FILE_NOT_FOUND;
12218                 }
12219         }
12220
12221         ret = true;
12222
12223 FINISH_OFF:
12224         if (err_code != NULL)
12225                 *err_code = error;
12226
12227         EM_DEBUG_FUNC_END("ret [%d]", ret);
12228         return ret;
12229 }
12230
12231
12232 INTERNAL_FUNC int emstorage_delete_dir(char *src_dir, int *err_code)
12233 {
12234         EM_DEBUG_FUNC_BEGIN("src_dir[%p], err_code[%p]", src_dir, err_code);
12235
12236         if (src_dir == NULL) {
12237                 EM_DEBUG_EXCEPTION("src_dir[%p]", src_dir);
12238
12239                 if (err_code != NULL)
12240                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12241                 return false;
12242         }
12243
12244         int error = EMAIL_ERROR_NONE;
12245
12246         DIR *dirp;
12247         struct dirent *dp;
12248         struct stat sbuf;
12249         char buf[512];
12250
12251         dirp = opendir(src_dir);
12252
12253         if (dirp == NULL) {
12254                 if (errno == ENOENT) {
12255                         EM_DEBUG_EXCEPTION("directory[%s] does not exist...", src_dir);
12256                         if (err_code != NULL)
12257                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12258                         return true;
12259                 } else {
12260                         EM_DEBUG_EXCEPTION("opendir failed [%s] [%d]", src_dir, errno);
12261                         if (err_code != NULL)
12262                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12263                         return false;
12264                 }
12265         }
12266
12267         while ((dp = readdir(dirp))) {
12268                 if (strncmp(dp->d_name, ".", 1) == 0 || strncmp(dp->d_name, "..", 2) == 0) /* prevent 34360 */
12269                         continue;
12270
12271                 SNPRINTF(buf, sizeof(buf), "%s/%s", src_dir, dp->d_name);
12272
12273                 if (lstat(buf, &sbuf) == 0 || stat(buf, &sbuf) == 0) {
12274                         /*  check directory */
12275                         if ((sbuf.st_mode & S_IFMT) == S_IFDIR) {       /*  directory */
12276                                 /*  recursive call */
12277                                 if (!emstorage_delete_dir(buf, &error)) {
12278                                         closedir(dirp);
12279                                         if (err_code != NULL)
12280                                                 *err_code = error;
12281                                         return false;
12282                                 }
12283                         } else {        /*  file */
12284                                 if (remove(buf) < 0) {
12285                                         EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", buf, errno);
12286                                         closedir(dirp);
12287                                         if (err_code != NULL)
12288                                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12289                                         return false;
12290                                 }
12291                         }
12292                 } else
12293                         EM_DEBUG_EXCEPTION("content does not exist...");
12294         }
12295
12296         closedir(dirp);
12297
12298         EM_DEBUG_LOG_DEV("remove direcotory [%s]", src_dir);
12299
12300         /* EM_DEBUG_FUNC_BEGIN(); */
12301
12302         if (remove(src_dir) < 0) {
12303                 EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", src_dir, errno);
12304                 if (err_code != NULL)
12305                         *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12306                 return false;
12307         }
12308
12309         if (err_code != NULL)
12310                 *err_code = error;
12311
12312         return true;
12313 }
12314
12315 /* faizan.h@samsung.com */
12316 INTERNAL_FUNC int emstorage_update_server_uid(char *multi_user_name,
12317                 int mail_id,
12318                 char *old_server_uid,
12319                 char *new_server_uid,
12320                 int *err_code)
12321 {
12322         EM_DEBUG_FUNC_BEGIN("new_server_uid[%s], old_server_uid[%s]", new_server_uid, old_server_uid);
12323         int ret = false;
12324         int error = EMAIL_ERROR_NONE;
12325         int transaction = true;
12326         int temp_strlen = 0;
12327         int and_operation = 0;
12328         char sql_query_string[QUERY_SIZE] = {0, };
12329         char conditional_clause_string[QUERY_SIZE] = {0};
12330
12331         if ((mail_id <= 0 || !old_server_uid) && !new_server_uid) {
12332                 EM_DEBUG_EXCEPTION("Invalid parameters");
12333                 if (err_code)
12334                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12335                 return false;
12336         }
12337
12338         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12339         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12340
12341         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE ");
12342
12343         if (mail_id > 0) {
12344                 temp_strlen = strlen(conditional_clause_string);
12345                 SNPRINTF(conditional_clause_string + temp_strlen, sizeof(conditional_clause_string) - temp_strlen,
12346                                 "mail_id = %d ", mail_id);
12347                 and_operation = 1;
12348         }
12349
12350         if (old_server_uid) {
12351                 temp_strlen = strlen(conditional_clause_string);
12352                 if (!and_operation) {
12353                         sqlite3_snprintf(sizeof(conditional_clause_string) - temp_strlen, conditional_clause_string + temp_strlen,
12354                                         "server_mail_id = '%q'", old_server_uid);
12355                 } else {
12356                         sqlite3_snprintf(sizeof(conditional_clause_string) - temp_strlen, conditional_clause_string + temp_strlen,
12357                                         "and server_mail_id = '%q'", old_server_uid);
12358                 }
12359         }
12360
12361         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
12362                         "UPDATE mail_tbl SET server_mail_id = '%q' %s", new_server_uid, conditional_clause_string);
12363
12364         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12365         if (error != EMAIL_ERROR_NONE) {
12366                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12367                 goto FINISH_OFF;
12368         }
12369
12370         ret = true;
12371
12372 FINISH_OFF:
12373
12374         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12375
12376         if (err_code != NULL)
12377                 *err_code = error;
12378
12379         EM_DEBUG_FUNC_END("ret [%d]", ret);
12380         return ret;
12381
12382 }
12383
12384 INTERNAL_FUNC int emstorage_update_read_mail_uid(char *multi_user_name, int mail_id, char *new_server_uid, char *mbox_name, int *err_code)
12385 {
12386         EM_DEBUG_FUNC_BEGIN_SEC("mail_id[%d], new_server_uid[%s], mbox_name[%s]", mail_id, new_server_uid, mbox_name);
12387
12388         int ret = false;
12389         int error = EMAIL_ERROR_NONE;
12390         char sql_query_string[QUERY_SIZE] = {0, };
12391         int transaction = true;
12392
12393         if (!mail_id || !new_server_uid || !mbox_name) {
12394                 EM_DEBUG_EXCEPTION("Invalid parameters");
12395                 if (err_code != NULL)
12396                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12397                 return false;
12398         }
12399
12400         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12401         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12402
12403
12404         SNPRINTF(sql_query_string, sizeof(sql_query_string),
12405                         "UPDATE mail_read_mail_uid_tbl SET server_uid=\'%s\', mailbox_id=\'%s\', mailbox_name=\'%s\' WHERE local_uid=%d ", new_server_uid, mbox_name, mbox_name, mail_id);
12406
12407         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12408         if (error != EMAIL_ERROR_NONE) {
12409                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12410                 goto FINISH_OFF;
12411         }
12412
12413         ret     = true;
12414
12415 FINISH_OFF:
12416
12417         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12418
12419         if (err_code != NULL)
12420                 *err_code = error;
12421
12422         EM_DEBUG_FUNC_END("ret [%d]", ret);
12423         return ret;
12424
12425 }
12426
12427 INTERNAL_FUNC int emstorage_update_save_status(char *multi_user_name, int account_id, int *err_code)
12428 {
12429         EM_DEBUG_FUNC_BEGIN();
12430
12431         int ret = false;
12432         int transaction = true;
12433         int error = EMAIL_ERROR_NONE;
12434         char sql_query_string[QUERY_SIZE] = {0,};
12435
12436         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12437
12438         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12439
12440         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12441
12442         if (account_id <= ALL_ACCOUNT)
12443                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET save_status = %d WHERE (save_status = %d or save_status = %d)", EMAIL_MAIL_STATUS_NONE, EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12444         else
12445                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET save_status = %d WHERE (save_status = %d or save_status = %d) and account_id = %d ", EMAIL_MAIL_STATUS_NONE, EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED, account_id);
12446
12447         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12448         if (error != EMAIL_ERROR_NONE) {
12449                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12450                 goto FINISH_OFF;
12451         }
12452
12453         ret = true;
12454
12455 FINISH_OFF:
12456         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12457
12458         if (err_code != NULL)
12459                 *err_code = error;
12460
12461         EM_DEBUG_FUNC_END("ret [%d]", ret);
12462         return ret;
12463
12464 }
12465
12466 int emstorage_get_unread_mailid(char *multi_user_name, int account_id, int vip_mode, int **mail_ids, int *mail_number, int *err_code)
12467 {
12468         EM_DEBUG_FUNC_BEGIN();
12469
12470         if ((!mail_ids) || (account_id <= 0 &&  account_id != -1)) {
12471                 EM_DEBUG_EXCEPTION(" mail_id[%p], account_id[%d] ", mail_ids, account_id);
12472                 if (err_code != NULL)
12473                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12474                 return false;
12475         }
12476
12477         int ret = false;
12478         int rc = -1;
12479         int error = EMAIL_ERROR_NONE;
12480         int count = 0;
12481         int i = 0;
12482         int col_index = 0;
12483         int *p_mail_ids = NULL;
12484         int transaction = false;
12485         char **result = NULL;
12486         char sql_query_string[QUERY_SIZE] = {0, };
12487         char temp_query_string[QUERY_SIZE] = {0,};
12488         char sql_select_query_string[QUERY_SIZE] = {0, };
12489
12490         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12491         EMSTORAGE_START_READ_TRANSACTION(transaction);
12492
12493         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12494
12495         if (account_id == -1) {
12496                 SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string),
12497                         "SELECT mail_id FROM mail_tbl WHERE flags_seen_field = 0 AND (save_status = %d or save_status = %d)",
12498                         EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12499         } else {
12500                 SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string),
12501                         "SELECT mail_id FROM mail_tbl WHERE account_id = %d AND flags_seen_field = 0 AND (save_status = %d or save_status = %d)",
12502                         account_id, EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12503         }
12504
12505         if (vip_mode) {
12506                 SNPRINTF(temp_query_string, sizeof(temp_query_string),
12507                         "%s AND tag_id < 0", sql_select_query_string);
12508         } else {
12509                 SNPRINTF(temp_query_string, sizeof(temp_query_string), "%s", sql_select_query_string);
12510         }
12511
12512         SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s ORDER BY date_time ASC", temp_query_string);
12513
12514         EM_DEBUG_LOG_SEC("query: [%s]", sql_query_string);
12515
12516         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
12517         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
12518                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12519
12520         EM_DEBUG_LOG("Count : %d", count);
12521
12522         if (count == 0) {
12523                 EM_DEBUG_EXCEPTION("no Mails found...");
12524                 ret = false;
12525                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
12526                 goto FINISH_OFF;
12527         }
12528
12529         p_mail_ids = em_malloc(count * sizeof(int));
12530         if (p_mail_ids == NULL) {
12531                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
12532                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12533                 goto FINISH_OFF;
12534         }
12535
12536         col_index = 1;
12537
12538         for (i = 0; i < count; i++)
12539                 _get_table_field_data_int(result, &(p_mail_ids[i]), col_index++);
12540
12541         ret = true;
12542
12543 FINISH_OFF:
12544
12545         if (result)
12546                 sqlite3_free_table(result);
12547
12548         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12549
12550         //      sqlite3_db_release_memory(local_db_handle);
12551
12552         if (ret == true) {
12553                 if (mail_ids != NULL)
12554                         *mail_ids = p_mail_ids;
12555
12556                 if (mail_number != NULL)
12557                         *mail_number = count;
12558         } else {
12559                 EM_SAFE_FREE(p_mail_ids);
12560         }
12561
12562         if (err_code != NULL)
12563                 *err_code = error;
12564
12565         EM_DEBUG_FUNC_END("ret [%d]", ret);
12566         return ret;
12567 }
12568
12569 int setting_system_command(const char *command)
12570
12571 {
12572         int pid = 0, status = 0;
12573         char *const environ[] = { NULL };
12574
12575         if (command == 0)
12576                 return 1;
12577
12578         pid = fork();
12579
12580         if (pid == -1)
12581                 return -1;
12582
12583         if (pid == 0) {
12584                 char *argv[4];
12585
12586                 argv[0] = "sh";
12587                 argv[1] = "-c";
12588                 argv[2] = (char *)command;
12589                 argv[3] = 0;
12590
12591                 execve("/bin/sh", argv, environ);
12592                 abort();
12593         }
12594
12595         do {
12596                 if (waitpid(pid, &status, 0) == -1) {
12597                         if (errno != EINTR)
12598                                 return -1;
12599                 } else {
12600                         return status;
12601                 }
12602         } while (1);
12603
12604         return 0;
12605 }
12606
12607
12608 INTERNAL_FUNC int emstorage_mail_get_total_diskspace_usage(unsigned long *total_usage,  int transaction, int *err_code)
12609 {
12610         EM_DEBUG_FUNC_BEGIN("total_usage[%p],  transaction[%d], err_code[%p]", total_usage, transaction, err_code);
12611
12612         if (!total_usage) {
12613                 EM_DEBUG_EXCEPTION("total_usage[%p]", total_usage);
12614
12615                 if (err_code != NULL)
12616                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12617                 return false;
12618         }
12619
12620         int   ret = false;
12621         int   error = EMAIL_ERROR_NONE;
12622         char  syscmd[256] = {0, };
12623         char  line[256] = {0, };
12624         char *line_from_file = NULL;
12625         FILE *fp = NULL;
12626         unsigned long total_diskusage = 0;
12627
12628         SNPRINTF(syscmd, sizeof(syscmd), "touch %s", SETTING_MEMORY_TEMP_FILE_PATH);
12629         if (setting_system_command(syscmd) == -1) {
12630                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
12631
12632                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12633                 goto FINISH_OFF;
12634         }
12635
12636         SNPRINTF(syscmd, sizeof(syscmd), "du -hsk %s > %s", EMAIL_PATH, SETTING_MEMORY_TEMP_FILE_PATH);
12637         EM_DEBUG_LOG(" cmd : %s", syscmd);
12638         if (setting_system_command(syscmd) == -1) {
12639                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : Setting > Memory] System Command [%s] is failed", syscmd);
12640
12641                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12642                 goto FINISH_OFF;
12643         }
12644
12645         error = em_fopen(SETTING_MEMORY_TEMP_FILE_PATH, "r", &fp);
12646         if (error != EMAIL_ERROR_NONE) {
12647                 perror(SETTING_MEMORY_TEMP_FILE_PATH);
12648                 goto FINISH_OFF;
12649         }
12650
12651         line_from_file = fgets(line, sizeof(line), fp);
12652
12653         if (line_from_file == NULL) {
12654                 EM_DEBUG_EXCEPTION("fgets failed");
12655                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12656                 goto FINISH_OFF;
12657         }
12658         total_diskusage = strtoul(line, NULL, 10);
12659
12660         memset(syscmd, 0, sizeof(syscmd));
12661         SNPRINTF(syscmd, sizeof(syscmd), "rm -f %s", SETTING_MEMORY_TEMP_FILE_PATH);
12662         if (setting_system_command(syscmd) == -1) {
12663                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage :  [Setting > Memory] System Command [%s] is failed", syscmd);
12664                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12665                 goto FINISH_OFF;
12666         }
12667
12668         EM_DEBUG_LOG("[Setting > Memory] @@@@@ Size of Directory [%s] is %ld KB", EMAIL_PATH, total_diskusage);
12669
12670         ret = true;
12671
12672 FINISH_OFF:
12673         if (err_code != NULL)
12674                 *err_code = error;
12675
12676         if (ret)
12677                 *total_usage = total_diskusage;
12678         else
12679                 *total_usage = 0;
12680
12681         if (fp) fclose(fp); /* prevent 32730 */
12682
12683         EM_DEBUG_FUNC_END("ret [%d]", ret);
12684         return ret;
12685 }
12686 #define MAILHOME_UTF8    tzplatform_mkpath(TZ_USER_DATA, "email/.email_data/7/348/UTF-8")
12687
12688 INTERNAL_FUNC int emstorage_test(char *multi_user_name, int mail_id, int account_id, char *full_address_to, char *full_address_cc, char *full_address_bcc, int *err_code)
12689 {
12690         DB_STMT hStmt = NULL;
12691         int ret = false;
12692         int error = EMAIL_ERROR_NONE;
12693         int rc = 0;
12694         char sql_query_string[QUERY_SIZE] = {0, };
12695
12696         SNPRINTF(sql_query_string, sizeof(sql_query_string),
12697                         "INSERT INTO mail_tbl VALUES "
12698                         "(?" /*  mail_id */
12699                         ", ?" /*  account_id */
12700                         ", ?" /*  mail_size */
12701                         ", ?" /*  server_mail_status */
12702                         ", ?" /*  server_mailbox_name */
12703                         ", ?" /*  server_mail_id */
12704                         ", ?" /*  reference_mail_id */
12705                         ", ?" /*  full_address_from */
12706                         ", ?" /*  full_address_reply */
12707                         ", ?" /*  full_address_to */
12708                         ", ?" /*  full_address_cc */
12709                         ", ?" /*  full_address_bcc */
12710                         ", ?" /*  full_address_return */
12711                         ", ?" /*  subject */
12712                         ", ?" /*  body_download_status */
12713                         ", ?" /*  file_path_plain */
12714                         ", ?" /*  file_path_html */
12715                         ", ?" /*  date_time */
12716                         ", ?" /*  flags_seen_field */
12717                         ", ?" /*  flags_deleted_field */
12718                         ", ?" /*  flags_flagged_field */
12719                         ", ?" /*  flags_answered_field */
12720                         ", ?" /*  flags_recent_field */
12721                         ", ?" /*  flags_draft_field */
12722                         ", ?" /*  flags_forwarded_field */
12723                         ", ?" /*  DRM_status */
12724                         ", ?" /*  priority */
12725                         ", ?" /*  save_status */
12726                         ", ?" /*  lock_status */
12727                         ", ?" /*  message_id */
12728                         ", ?" /*  report_status */
12729                         ", ?" /*  email_address_sender */
12730                         ", ?" /*  email_address_recipient */
12731                         ", ?" /*  attachment_count */
12732                         ", ?" /*  inline_content_count */
12733                         ", ?" /*  preview_text */
12734                         ", ?" /*  thread_id */
12735                         ", ?" /*  mailbox_type */
12736                         ", ?" /*  alias_sender */
12737                         ", ?" /*  alias_recipient */
12738                         ", ?" /*  thread_item_count */
12739                         ", ?" /*  meeting_request_status */
12740                         ", ?" /*  message_class */
12741                         ", ?" /*  digest_type */
12742                         ", ?" /*  smime_type */
12743                         ", ?" /*  scheduled_sending_time */
12744                         ", ?" /*  remaining_resend_times */
12745                         ", ?" /*  tag_id   */
12746                         ", ?" /*  replied_time */
12747                         ", ?" /*  forwarded_time */
12748                         ", ?" /*  default_charset */
12749                         ", ?" /*  eas_data_length */
12750                         ", ?" /*  eas_data */
12751                         ")");
12752
12753         int transaction = true;
12754         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12755
12756         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12757
12758
12759         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12760         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
12761                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12762
12763         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_id);
12764         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, account_id);
12765         _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, EMAIL_MAILBOX_TYPE_OUTBOX);
12766         _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, "save test - long", 1, SUBJECT_LEN_IN_MAIL_TBL);
12767         _bind_stmt_field_data_string(hStmt, DATETIME_IDX_IN_MAIL_TBL, "20100316052908", 0, DATETIME_LEN_IN_MAIL_TBL);
12768         _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, 0);
12769         _bind_stmt_field_data_string(hStmt, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL, "", 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
12770         _bind_stmt_field_data_string(hStmt, SERVER_MAIL_ID_IDX_IN_MAIL_TBL, "", 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
12771         _bind_stmt_field_data_string(hStmt, MESSAGE_ID_IDX_IN_MAIL_TBL, "", 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
12772         _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, 0);
12773         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL, "<test08@streaming.s3glab.net>", 1, FROM_LEN_IN_MAIL_TBL);
12774         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL, "", 1, REPLY_TO_LEN_IN_MAIL_TBL);
12775         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL, full_address_to, 1, TO_LEN_IN_MAIL_TBL);
12776         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL, full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
12777         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL, full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
12778         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL, "", 1, RETURN_PATH_LEN_IN_MAIL_TBL);
12779         _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL, "<sender_name@sender_host.com>", 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12780         _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL, "<recipient_name@recipient_host.com>", 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12781         _bind_stmt_field_data_string(hStmt, ALIAS_SENDER_IDX_IN_MAIL_TBL, "send_alias", 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12782         _bind_stmt_field_data_string(hStmt, ALIAS_RECIPIENT_IDX_IN_MAIL_TBL, "recipient_alias", 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12783         _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, 1);
12784         _bind_stmt_field_data_string(hStmt, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL, (char *)MAILHOME_UTF8, 0, TEXT_1_LEN_IN_MAIL_TBL);
12785         _bind_stmt_field_data_string(hStmt, FILE_PATH_HTML_IDX_IN_MAIL_TBL, "", 0, TEXT_2_LEN_IN_MAIL_TBL);
12786         _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, 4);
12787         _bind_stmt_field_data_char(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, 0);
12788         _bind_stmt_field_data_char(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, 0);
12789         _bind_stmt_field_data_char(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, 0);
12790         _bind_stmt_field_data_char(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, 0);
12791         _bind_stmt_field_data_char(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, 0);
12792         _bind_stmt_field_data_char(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, 0);
12793         _bind_stmt_field_data_char(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, 0);
12794         _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, 0);
12795         _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, 0);
12796         _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, 0);
12797         _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, 0);
12798         _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, 0);
12799         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12800         _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, 0);
12801         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12802         _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, 0);
12803         _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, 0);
12804         _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, "preview body", 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
12805         _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, 0);
12806         _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, 0);
12807         _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, 0);
12808         _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, 0);
12809         _bind_stmt_field_data_int(hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, 0);
12810         _bind_stmt_field_data_int(hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, 0);
12811         _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, 0);
12812         _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, 0);
12813         _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, 0);
12814         _bind_stmt_field_data_string(hStmt, DEFAULT_CHARSET_IDX_IN_MAIL_TBL, "UTF-8", 0, TEXT_2_LEN_IN_MAIL_TBL);
12815         _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, 0);
12816         _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, NULL, 0);
12817
12818         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12819         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
12820                         ("sqlite3_step fail:%d", rc));
12821         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
12822                         ("sqlite3_step fail:%d", rc));
12823         ret = true;
12824
12825 FINISH_OFF:
12826         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12827         if (hStmt != NULL) {
12828                 rc = sqlite3_finalize(hStmt);
12829                 if (rc != SQLITE_OK) {
12830                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
12831                         error = EMAIL_ERROR_DB_FAILURE;
12832                 }
12833         }
12834
12835         if (err_code != NULL)
12836                 *err_code = error;
12837
12838         EM_DEBUG_FUNC_END("ret [%d]", ret);
12839         return ret;
12840 }
12841
12842
12843 INTERNAL_FUNC int emstorage_get_max_mail_count()
12844 {
12845         return EMAIL_MAIL_MAX_COUNT;
12846 }
12847
12848 #define STRIPPED_SUBJECT_BUFFER_SIZE 4086
12849
12850 INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(char *multi_user_name,
12851                 emstorage_mail_tbl_t *mail_tbl,
12852                 int *thread_id,
12853                 int *result_latest_mail_id_in_thread,
12854                 int *thread_item_count)
12855 {
12856         EM_DEBUG_FUNC_BEGIN("mail_tbl [%p], thread_id [%p], "
12857                         "result_latest_mail_id_in_thread [%p], thread_item_count [%p]",
12858                         mail_tbl, thread_id, result_latest_mail_id_in_thread, thread_item_count);
12859         EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
12860
12861         int rc = 0, query_size = 0, query_size_account = 0;
12862         int i = 0;
12863         int search_thread = false;
12864         int account_id = 0;
12865         int err_code = EMAIL_ERROR_NONE;
12866         int count = 0, result_thread_id = -1, latest_mail_id_in_thread = -1;
12867         time_t latest_date_time = 0;
12868         char *subject = NULL;
12869         char *p_subject = NULL;
12870         char *sql_query_string = NULL, *sql_account = NULL;
12871         int col_index = 4;
12872         int temp_thread_id = -1;
12873         char *sql_format = "SELECT mail_id, thread_id, date_time, subject "
12874                 "FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
12875         char *sql_format_account = " AND account_id = %d ";
12876         char *sql_format_order_by = " ORDER BY thread_id, date_time DESC ";
12877         char **result = NULL;
12878         char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
12879         char stripped_subject2[STRIPPED_SUBJECT_BUFFER_SIZE];
12880
12881         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12882
12883         EM_DEBUG_LOG("subject: [%p], mail_id: [%d]", subject, mail_tbl->mail_id);
12884
12885         EM_IF_NULL_RETURN_VALUE(mail_tbl, EMAIL_ERROR_INVALID_PARAM);
12886         EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
12887         EM_IF_NULL_RETURN_VALUE(result_latest_mail_id_in_thread, EMAIL_ERROR_INVALID_PARAM);
12888         EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
12889
12890         account_id   = mail_tbl->account_id;
12891         subject      = mail_tbl->subject;
12892
12893         EM_DEBUG_LOG_SEC("subject: [%s]", subject);
12894
12895         if (EM_SAFE_STRLEN(subject) == 0 && mail_tbl->mail_id != 0) {
12896                 result_thread_id = mail_tbl->mail_id;
12897                 count = 1;
12898                 goto FINISH_OFF;
12899         }
12900
12901         if (em_find_pos_stripped_subject_for_thread_view(subject,
12902                                 stripped_subject,
12903                                 STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE)      {
12904                 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
12905                 err_code =  EMAIL_ERROR_UNKNOWN;
12906                 result_thread_id = -1;
12907                 goto FINISH_OFF;
12908         }
12909
12910         EM_DEBUG_LOG_SEC("stripped_subject: [%s]", stripped_subject);
12911
12912         if (EM_SAFE_STRLEN(stripped_subject) == 0) {
12913                 result_thread_id = -1;
12914                 goto FINISH_OFF;
12915         }
12916
12917         EM_DEBUG_LOG_SEC("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s",
12918                         EM_SAFE_STRLEN(stripped_subject), stripped_subject);
12919
12920         if (account_id > 0) {
12921                 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
12922                 sql_account = malloc(query_size_account);
12923                 if (sql_account == NULL) {
12924                         EM_DEBUG_EXCEPTION("malloc for sql_account  is failed %d", query_size_account);
12925                         err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
12926                         goto FINISH_OFF;
12927                 }
12928                 snprintf(sql_account, query_size_account, sql_format_account, account_id);
12929         }
12930
12931         /* prevent 34362 */
12932         query_size = strlen(sql_format) + strlen(stripped_subject)*2 + 50 + query_size_account + strlen(sql_format_order_by); /*  + query_size_mailbox; */
12933         sql_query_string = malloc(query_size);
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, mail_tbl->mailbox_id);
12942
12943         if (account_id > 0)
12944                 EM_SAFE_STRNCAT(sql_query_string, sql_account, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
12945
12946         EM_SAFE_STRNCAT(sql_query_string, sql_format_order_by, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
12947         EM_SAFE_STRNCAT(sql_query_string, ";", query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
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),
12952                         rc);
12953
12954         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
12955                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12956
12957         EM_DEBUG_LOG("Result rows count : %d", count);
12958
12959         if (count == 0)
12960                 result_thread_id = -1;
12961         else {
12962                 for (i = 0; i < count; i++) {
12963                         EM_SAFE_FREE(p_subject);
12964
12965                         _get_table_field_data_int(result, &latest_mail_id_in_thread, col_index++);
12966                         _get_table_field_data_int(result, &result_thread_id, col_index++);
12967                         _get_table_field_data_time_t(result, &latest_date_time, col_index++);
12968                         _get_table_field_data_string(result, &p_subject, 0, col_index++);
12969
12970                         if (temp_thread_id == result_thread_id)
12971                                 continue;
12972
12973                         temp_thread_id = result_thread_id;
12974
12975                         if (em_find_pos_stripped_subject_for_thread_view(p_subject,
12976                                                 stripped_subject2,
12977                                                 STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE)      {
12978                                 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
12979                                 err_code = EMAIL_ERROR_UNKNOWN;
12980                                 result_thread_id = -1;
12981                                 goto FINISH_OFF;
12982                         }
12983
12984                         if (g_strcmp0(stripped_subject2, stripped_subject) == 0) {
12985                                 if (latest_date_time < mail_tbl->date_time)
12986                                         *result_latest_mail_id_in_thread = latest_mail_id_in_thread;
12987                                 else
12988                                         *result_latest_mail_id_in_thread = mail_tbl->mail_id;
12989
12990                                 search_thread = true;
12991                         }
12992
12993                         if (search_thread) {
12994                                 EM_DEBUG_LOG("latest_mail_id_in_thread [%d], mail_id [%d]",
12995                                                 latest_mail_id_in_thread, mail_tbl->mail_id);
12996                                 break;
12997                         } else {
12998                                 result_thread_id = -1;
12999                                 count = 0;
13000                         }
13001                 }
13002
13003         }
13004
13005 FINISH_OFF:
13006
13007         *thread_id = result_thread_id;
13008         *thread_item_count = count;
13009
13010         EM_DEBUG_LOG("Result thread id : %d", *thread_id);
13011         EM_DEBUG_LOG("Result count : %d", *thread_item_count);
13012         EM_DEBUG_LOG("err_code : %d", err_code);
13013
13014         EM_SAFE_FREE(sql_account);
13015         EM_SAFE_FREE(sql_query_string);
13016         EM_SAFE_FREE(p_subject);
13017
13018         sqlite3_free_table(result);
13019
13020         EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
13021
13022         return err_code;
13023 }
13024
13025 INTERNAL_FUNC int emstorage_get_thread_id_from_mailbox(char *multi_user_name, int account_id, int mailbox_id, char *mail_subject, int *thread_id, int *thread_item_count)
13026 {
13027         EM_DEBUG_FUNC_BEGIN("mailbox_id [%d], subject [%p], thread_id [%p], thread_item_count [%p]", mailbox_id, mail_subject, thread_id, thread_item_count);
13028         EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
13029
13030         int rc = 0;
13031         int query_size = 0;
13032         int query_size_account = 0;
13033         int err_code = EMAIL_ERROR_NONE;
13034         int count = 0;
13035         int result_thread_id = -1;
13036         char *sql_query_string = NULL;
13037         char *sql_account = NULL;
13038         char *sql_format = "SELECT thread_id FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
13039         char *sql_format_account = " AND account_id = %d ";
13040         char *sql_format_order_by = " ORDER BY date_time DESC ";
13041         char **result = NULL;
13042         char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
13043         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13044
13045         EM_IF_NULL_RETURN_VALUE(mail_subject, EMAIL_ERROR_INVALID_PARAM);
13046         EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
13047         EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
13048
13049         EM_DEBUG_LOG_SEC("subject: [%s]", mail_subject);
13050
13051         if (em_find_pos_stripped_subject_for_thread_view(mail_subject, stripped_subject, STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE)     {
13052                 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view  is failed");
13053                 err_code =  EMAIL_ERROR_UNKNOWN;
13054                 result_thread_id = -1;
13055                 goto FINISH_OFF;
13056         }
13057
13058         if (EM_SAFE_STRLEN(stripped_subject) == 0) {
13059                 result_thread_id = -1;
13060                 goto FINISH_OFF;
13061         }
13062
13063         EM_DEBUG_LOG("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s", EM_SAFE_STRLEN(stripped_subject), stripped_subject);
13064
13065         if (account_id > 0) {
13066                 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
13067                 sql_account = malloc(query_size_account);
13068                 if (sql_account == NULL) {
13069                         EM_DEBUG_EXCEPTION("malloc for sql_account  is failed %d", query_size_account);
13070                         err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
13071                         goto FINISH_OFF;
13072                 }
13073                 snprintf(sql_account, query_size_account, sql_format_account, account_id);
13074         }
13075
13076         query_size = EM_SAFE_STRLEN(sql_format) + EM_SAFE_STRLEN(stripped_subject)*2 + 50 + query_size_account + EM_SAFE_STRLEN(sql_format_order_by); /*  + query_size_mailbox; */
13077         sql_query_string = malloc(query_size);
13078
13079         if (sql_query_string == NULL) {
13080                 EM_DEBUG_EXCEPTION("malloc for sql  is failed %d", query_size);
13081                 err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
13082                 goto FINISH_OFF;
13083         }
13084
13085         sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mailbox_id);
13086
13087         if (account_id > 0)
13088                 EM_SAFE_STRNCAT(sql_query_string, sql_account, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13089         EM_SAFE_STRNCAT(sql_query_string, sql_format_order_by, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13090         EM_SAFE_STRNCAT(sql_query_string, ";", query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13091
13092         EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
13093
13094         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
13095
13096         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13097                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13098
13099         EM_DEBUG_LOG("Result rows count : %d", count);
13100
13101         if (count == 0)
13102                 result_thread_id = -1;
13103         else
13104                 _get_table_field_data_int(result, &result_thread_id, 1);
13105
13106 FINISH_OFF:
13107         *thread_id = result_thread_id;
13108         *thread_item_count = count;
13109
13110         EM_DEBUG_LOG("Result thread id : %d", *thread_id);
13111         EM_DEBUG_LOG("Result count : %d", *thread_item_count);
13112         EM_DEBUG_LOG("err_code : %d", err_code);
13113
13114         EM_SAFE_FREE(sql_account);
13115         EM_SAFE_FREE(sql_query_string);
13116
13117         sqlite3_free_table(result);
13118
13119         EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
13120
13121         return err_code;
13122 }
13123
13124 INTERNAL_FUNC int emstorage_get_thread_information(char *multi_user_name, int thread_id, emstorage_mail_tbl_t** mail_tbl, int transaction, int *err_code)
13125 {
13126         EM_DEBUG_FUNC_BEGIN();
13127
13128         int count = 0, ret = false;
13129         int error = EMAIL_ERROR_NONE;
13130         emstorage_mail_tbl_t *p_data_tbl = NULL;
13131         char conditional_clause[QUERY_SIZE] = {0, };
13132
13133         EM_IF_NULL_RETURN_VALUE(mail_tbl, false);
13134
13135         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE thread_id = %d AND thread_item_count > 0", thread_id);
13136         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
13137
13138         if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
13139                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
13140                 goto FINISH_OFF;
13141         }
13142
13143         if (p_data_tbl)
13144                 EM_DEBUG_LOG("thread_id : %d, thread_item_count : %d", p_data_tbl[0].thread_id, p_data_tbl[0].thread_item_count);
13145
13146         ret = true;
13147
13148 FINISH_OFF:
13149         if (ret == true)
13150                 *mail_tbl = p_data_tbl;
13151         else if (p_data_tbl != NULL)
13152                 emstorage_free_mail(&p_data_tbl, 1, NULL);
13153
13154         if (err_code != NULL)
13155                 *err_code = error;
13156
13157         EM_DEBUG_FUNC_END("ret [%d]", ret);
13158         return ret;
13159 }
13160
13161 INTERNAL_FUNC int emstorage_get_sender_list(char *multi_user_name, int account_id, int mailbox_id, int search_type, const char *search_value, email_sort_type_t sorting, email_sender_list_t** sender_list, int *sender_count,  int *err_code)
13162 {
13163         EM_DEBUG_FUNC_BEGIN_SEC("account_id [%d], mailbox_id [%d], search_type [%d], search_value [%p], sorting [%d], sender_list[%p], sender_count[%p] err_code[%p]"
13164                         , account_id , mailbox_id , search_type , search_value , sorting , sender_list, sender_count, err_code);
13165
13166         if ((!sender_list) || (!sender_count)) {
13167                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
13168                 if (err_code != NULL)
13169                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13170                 return false;
13171         }
13172
13173         int rc = -1, ret = false;
13174         int error = EMAIL_ERROR_NONE;
13175         int count = 0;
13176         int i, col_index = 0;
13177         int read_count = 0;
13178         email_sender_list_t *p_sender_list = NULL;
13179         char sql_query_string[QUERY_SIZE] = {0, };
13180         char **result = NULL;
13181         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13182
13183         SNPRINTF(sql_query_string, sizeof(sql_query_string),
13184                         "SELECT email_address_sender, alias_sender, COUNT(email_address_sender), SUM(flags_seen_field = 1) "
13185                         "FROM mail_tbl ");
13186
13187         /*  mailbox_id */
13188         if (mailbox_id)
13189                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE mailbox_id = %d ", mailbox_id);
13190         else    /*  NULL  means all mailbox_name. but except for trash(3), spambox(5), all emails(for GMail, 7) */
13191                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE mailbox_type not in (3, 5, 7, 8) ");
13192
13193         /*  account id */
13194         /*  '0' (ALL_ACCOUNT) means all account */
13195         if (account_id > ALL_ACCOUNT)
13196                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1), " AND account_id = %d ", account_id);
13197
13198         if (search_value) {
13199                 switch (search_type) {
13200                 case EMAIL_SEARCH_FILTER_SUBJECT:
13201                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13202                                         " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\')) ", search_value);
13203                         break;
13204                 case EMAIL_SEARCH_FILTER_SENDER:
13205                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13206                                         " AND  ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13207                                         ") ", search_value);
13208                         break;
13209                 case EMAIL_SEARCH_FILTER_RECIPIENT:
13210                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13211                                         " AND ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13212                                         "       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13213                                         "       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13214                                         ") ", search_value, search_value, search_value);
13215                         break;
13216                 case EMAIL_SEARCH_FILTER_ALL:
13217                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13218                                         " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') "
13219                                         "       OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13220                                         "                       OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13221                                         "                       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13222                                         "                       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13223                                         "               ) "
13224                                         "       )"
13225                                         ")", search_value, search_value, search_value, search_value, search_value);
13226                         break;
13227                 }
13228         }
13229
13230
13231         /*  sorting option is not available now. The order of sender list is ascending order by display name */
13232         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
13233                         "GROUP BY email_address_sender "
13234                         "ORDER BY UPPER(alias_sender) ");
13235
13236         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
13237
13238         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
13239         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13240                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13241
13242         EM_DEBUG_LOG("Count of Sender [%d]", count);
13243
13244         if (!(p_sender_list = (email_sender_list_t*)em_malloc(sizeof(email_sender_list_t) * count))) {
13245                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
13246                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13247                 goto FINISH_OFF;
13248         }
13249
13250         col_index = 4;
13251
13252         EM_DEBUG_LOG(">>>> DATA ASSIGN START >>");
13253         for (i = 0; i < count; i++) {
13254                 _get_table_field_data_string(result, &(p_sender_list[i].address), 1, col_index++);
13255                 _get_table_field_data_string(result, &(p_sender_list[i].display_name), 1, col_index++);
13256                 _get_table_field_data_int(result, &(p_sender_list[i].total_count), col_index++);
13257                 _get_table_field_data_int(result, &(read_count), col_index++);
13258                 p_sender_list[i].unread_count = p_sender_list[i].total_count - read_count;              /*  unread count = total - read          */
13259         }
13260         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >>", count);
13261
13262         sqlite3_free_table(result);
13263         result = NULL;
13264
13265         ret = true;
13266
13267 FINISH_OFF:
13268         if (ret == true) {
13269                 *sender_list = p_sender_list;
13270                 *sender_count = count;
13271                 EM_DEBUG_LOG(">>>> COUNT : %d >>", count);
13272         }
13273
13274
13275         if (err_code != NULL)
13276                 *err_code = error;
13277
13278         EM_DEBUG_FUNC_END("ret [%d]", ret);
13279         return ret;
13280 }
13281
13282 INTERNAL_FUNC int emstorage_free_sender_list(email_sender_list_t **sender_list, int count)
13283 {
13284         EM_DEBUG_FUNC_BEGIN("sender_list[%p], count[%d]", sender_list, count);
13285
13286         int err = EMAIL_ERROR_NONE;
13287
13288         if (count > 0) {
13289                 if (!sender_list || !*sender_list) {
13290                         EM_DEBUG_EXCEPTION("sender_list[%p], count[%d]", sender_list, count);
13291                         err = EMAIL_ERROR_INVALID_PARAM;
13292                         return err;
13293                 }
13294
13295                 email_sender_list_t* p = *sender_list;
13296                 int i = 0;
13297
13298                 for (; i < count; i++) {
13299                         EM_SAFE_FREE(p[i].address);
13300                         EM_SAFE_FREE(p[i].display_name);
13301                 }
13302
13303                 EM_SAFE_FREE(p);
13304                 *sender_list = NULL;
13305         }
13306
13307         return err;
13308 }
13309
13310
13311 INTERNAL_FUNC int emstorage_free_address_info_list(email_address_info_list_t **address_info_list)
13312 {
13313         EM_DEBUG_FUNC_BEGIN("address_info_list[%p]", address_info_list);
13314
13315         int err = EMAIL_ERROR_NONE;
13316         email_address_info_t *p_address_info = NULL;
13317         GList *list = NULL;
13318         GList *node = NULL;
13319         int i = 0;
13320
13321         if (!address_info_list || !*address_info_list) {
13322                 EM_DEBUG_EXCEPTION("address_info_list[%p]", address_info_list);
13323                 err = EMAIL_ERROR_INVALID_PARAM;
13324                 return err;
13325         }
13326
13327         /*  delete GLists */
13328         for (i = EMAIL_ADDRESS_TYPE_FROM; i <= EMAIL_ADDRESS_TYPE_BCC; i++) {
13329                 switch (i) {
13330                 case EMAIL_ADDRESS_TYPE_FROM:
13331                         list = (*address_info_list)->from;
13332                         break;
13333                 case EMAIL_ADDRESS_TYPE_TO:
13334                         list = (*address_info_list)->to;
13335                         break;
13336                 case EMAIL_ADDRESS_TYPE_CC:
13337                         list = (*address_info_list)->cc;
13338                         break;
13339                 case EMAIL_ADDRESS_TYPE_BCC:
13340                         list = (*address_info_list)->bcc;
13341                         break;
13342                 }
13343
13344                 /*  delete dynamic-allocated memory for each item */
13345                 node = g_list_first(list);
13346                 while (node != NULL) {
13347                         p_address_info = (email_address_info_t*)node->data;
13348                         EM_SAFE_FREE(p_address_info->address);
13349                         EM_SAFE_FREE(p_address_info->display_name);
13350                         EM_SAFE_FREE(node->data);
13351
13352                         node = g_list_next(node);
13353                 }
13354                 g_list_free(list);
13355         }
13356
13357         EM_SAFE_FREE(*address_info_list);
13358         *address_info_list = NULL;
13359
13360         EM_DEBUG_FUNC_END("err [%d]", err);
13361         return err;
13362 }
13363 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
13364
13365 INTERNAL_FUNC int emstorage_add_pbd_activity(char *multi_user_name, email_event_partial_body_thd* local_activity, int *activity_id, int transaction, int *err_code)
13366 {
13367         EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
13368
13369         if (!local_activity || !activity_id) {
13370                 EM_DEBUG_EXCEPTION("local_activity[%p], transaction[%d], activity_id[%p], err_code[%p]", local_activity, activity_id, transaction, err_code);
13371                 if (err_code != NULL)
13372                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13373                 return false;
13374         }
13375
13376         int rc = -1;
13377         int ret = false;
13378         int error = EMAIL_ERROR_NONE;
13379         int i = 0;
13380
13381         char sql_query_string[QUERY_SIZE] = {0, };
13382         DB_STMT hStmt = NULL;
13383         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13384
13385         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13386         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13387         SNPRINTF(sql_query_string, sizeof(sql_query_string),
13388                         "INSERT INTO mail_partial_body_activity_tbl VALUES "
13389                         "("
13390                         "? "  /* Account ID */
13391                         ",?"  /* Local Mail ID */
13392                         ",?"  /* Server mail ID */
13393                         ",?"  /* Activity ID */
13394                         ",?"  /* Activity type*/
13395                         ",?"  /* Mailbox ID*/
13396                         ",?"  /* Mailbox name*/
13397                         ",?"  /* Multi User Name */
13398                         ") ");
13399
13400         char *sql = "SELECT max(rowid) FROM mail_partial_body_activity_tbl;";
13401         char **result = NULL;
13402
13403
13404         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13405
13406         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13407                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13408
13409         if (NULL == result[1]) rc = 1;
13410         else rc = atoi(result[1])+1;
13411         sqlite3_free_table(result);
13412         result = NULL;
13413
13414         *activity_id = local_activity->activity_id = rc;
13415
13416         EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [ %d ], MAIL ID [ %d ], ACTIVITY TYPE [ %d ], SERVER MAIL ID [ %lu ]", \
13417                         local_activity->activity_id, local_activity->mail_id, local_activity->activity_type, local_activity->server_mail_id);
13418
13419         if (local_activity->mailbox_id)
13420                 EM_DEBUG_LOG(" MAILBOX ID [ %d ]", local_activity->mailbox_id);
13421
13422
13423         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13424         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13425                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13426
13427
13428         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
13429         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
13430         _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
13431         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
13432         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
13433         _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
13434         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->mailbox_name, 0, 3999);
13435         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
13436
13437
13438         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13439
13440         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
13441                         ("sqlite3_step fail:%d", rc));
13442         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13443                         ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
13444
13445         ret = true;
13446
13447 FINISH_OFF:
13448         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13449         if (hStmt != NULL) {
13450                 rc = sqlite3_finalize(hStmt);
13451                 hStmt = NULL;
13452                 if (rc != SQLITE_OK) {
13453                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13454                         error = EMAIL_ERROR_DB_FAILURE;
13455                 }
13456         }
13457
13458         if (err_code != NULL)
13459                 *err_code = error;
13460
13461         EM_DEBUG_FUNC_END("ret [%d]", ret);
13462         return ret;
13463 }
13464
13465 INTERNAL_FUNC int emstorage_get_pbd_mailbox_list(char *multi_user_name, int account_id, int **mailbox_list, int *count, int transaction, int *err_code)
13466 {
13467         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13468
13469         if (account_id < FIRST_ACCOUNT_ID || NULL == mailbox_list || *mailbox_list == NULL || NULL == count) {
13470                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13471                 if (err_code != NULL)
13472                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13473                 return false;
13474         }
13475
13476         int ret = false;
13477         int error = EMAIL_ERROR_NONE;
13478         char **result;
13479         int i = 0, rc = -1;
13480         int *mbox_list = NULL;
13481         DB_STMT hStmt = NULL;
13482         char sql_query_string[QUERY_SIZE] = {0, };
13483
13484         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13485         EMSTORAGE_START_READ_TRANSACTION(transaction);
13486
13487         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13488         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(distinct mailbox_id) FROM mail_partial_body_activity_tbl WHERE account_id = %d order by mailbox_id", account_id);
13489
13490
13491         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13492         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13493                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13494
13495         *count = atoi(result[1]);
13496         sqlite3_free_table(result);
13497
13498         if (*count <= 0) {
13499                 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
13500                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13501                 ret = true;
13502                 goto FINISH_OFF;
13503         }
13504         EM_DEBUG_LOG("Mailbox count = %d", *count);
13505
13506         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13507
13508         /* SNPRINTF(g_sql_query, sizeof(g_sql_query), "SELECT distinct mailbox_name FROM mail_partial_body_activity_tbl WHERE account_id = %d order by activity_id", account_id); */
13509         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct mailbox_id FROM mail_partial_body_activity_tbl WHERE account_id = %d order by mailbox_id", account_id);
13510
13511         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13512
13513
13514         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13515
13516
13517         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13518                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13519
13520
13521         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13522         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13523                         ("sqlite3_step fail:%d", rc));
13524
13525         mbox_list = (int *)em_malloc(sizeof(int) * (*count));
13526         if (NULL == mbox_list) {
13527                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
13528                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13529                 goto FINISH_OFF;
13530         }
13531
13532         memset(mbox_list, 0x00, sizeof(int) * (*count));
13533
13534         for (i = 0; i < (*count); i++) {
13535                 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
13536                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13537                 /* EM_DEBUG_LOG("In emstorage_get_pdb_mailbox_list() loop, After sqlite3_step(), , i = %d, rc = %d.", i,  rc); */
13538                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13539                                 ("sqlite3_step fail:%d", rc));
13540                 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
13541         }
13542
13543         ret = true;
13544
13545 FINISH_OFF:
13546         if (ret == true)
13547                 *mailbox_list = mbox_list;
13548         else
13549                 EM_SAFE_FREE(mbox_list);
13550
13551         if (hStmt != NULL) {
13552                 rc = sqlite3_finalize(hStmt);
13553                 hStmt = NULL;
13554                 if (rc != SQLITE_OK) {
13555                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13556                         error = EMAIL_ERROR_DB_FAILURE;
13557                 }
13558         }
13559
13560         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13561         if (err_code != NULL)
13562                 *err_code = error;
13563         EM_DEBUG_FUNC_END("ret [%d]", ret);
13564         return ret;
13565 }
13566
13567 INTERNAL_FUNC int emstorage_get_pbd_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
13568 {
13569         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13570
13571         if (NULL == account_list || NULL == count) {
13572                 EM_DEBUG_EXCEPTION("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13573                 if (err_code != NULL)
13574                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13575                 return false;
13576         }
13577
13578         int ret = false;
13579         int error = EMAIL_ERROR_NONE;
13580         char *sql = "SELECT count(distinct account_id) FROM mail_partial_body_activity_tbl";
13581         char **result;
13582         int i = 0, rc = -1;
13583         int *result_account_list = NULL;
13584         DB_STMT hStmt = NULL;
13585         char sql_query_string[QUERY_SIZE] = {0, };
13586
13587         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13588
13589         EMSTORAGE_START_READ_TRANSACTION(transaction);
13590
13591         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13592         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13593                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13594
13595         *count = atoi(result[1]);
13596         sqlite3_free_table(result);
13597
13598         if (*count <= 0) {
13599                 EM_DEBUG_EXCEPTION("no account found...");
13600                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13601                 ret = true;
13602                 goto FINISH_OFF;
13603         }
13604
13605         EM_DEBUG_LOG("Account count [%d]", *count);
13606
13607         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13608
13609         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_partial_body_activity_tbl");
13610
13611         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13612
13613
13614         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13615
13616         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
13617         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13618                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13619
13620
13621         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13622         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13623                         ("sqlite3_step fail:%d", rc));
13624
13625         if (NULL == (result_account_list = (int *)em_malloc(sizeof(int) * (*count)))) {
13626                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
13627                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13628                 goto FINISH_OFF;
13629         }
13630
13631         memset(result_account_list, 0x00, sizeof(int) * (*count));
13632
13633         for (i = 0; i < (*count); i++) {
13634                 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
13635
13636                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13637                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13638                                 ("sqlite3_step fail:%d", rc));
13639                 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
13640         }
13641
13642         ret = true;
13643
13644 FINISH_OFF:
13645         if (ret == true)
13646                 *account_list = result_account_list;
13647         else
13648                 EM_SAFE_FREE(result_account_list);
13649
13650         if (hStmt != NULL) {
13651                 rc = sqlite3_finalize(hStmt);
13652                 hStmt = NULL;
13653                 if (rc != SQLITE_OK) {
13654                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13655                         error = EMAIL_ERROR_DB_FAILURE;
13656                 }
13657         }
13658
13659         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13660         if (err_code != NULL)
13661                 *err_code = error;
13662         EM_DEBUG_FUNC_END("ret [%d]", ret);
13663         return ret;
13664 }
13665
13666 INTERNAL_FUNC int emstorage_get_pbd_activity_data(char *multi_user_name, int account_id, int input_mailbox_id, email_event_partial_body_thd** event_start, int *count, int transaction, int *err_code)
13667 {
13668         EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
13669
13670         if (account_id < FIRST_ACCOUNT_ID || NULL == event_start || 0 == input_mailbox_id || NULL == count) {
13671                 EM_DEBUG_EXCEPTION("account_id[%d], email_event_partial_body_thd[%p], input_mailbox_id[%d], count[%p], err_code[%p]", account_id, event_start, input_mailbox_id, count, err_code);
13672
13673                 if (err_code != NULL)
13674                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13675                 return false;
13676         }
13677
13678         int rc = -1;
13679         int ret = false;
13680         char **result;
13681         int error = EMAIL_ERROR_NONE;
13682         int i = 0;
13683         DB_STMT hStmt = NULL;
13684         email_event_partial_body_thd* event_list = NULL;
13685         char sql_query_string[QUERY_SIZE] = {0, };
13686
13687         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13688
13689         EMSTORAGE_START_READ_TRANSACTION(transaction);
13690
13691         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13692         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl WHERE account_id = %d AND mailbox_id = '%d' order by activity_id", account_id, input_mailbox_id);
13693
13694
13695         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13696         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13697                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13698
13699         *count = atoi(result[1]);
13700         sqlite3_free_table(result);
13701
13702         EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
13703
13704         if (*count <= 0) {
13705                 EM_DEBUG_LOG("No matched activity found in mail_partial_body_activity_tbl");
13706                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
13707                 ret = true;
13708                 goto FINISH_OFF;
13709         }
13710         EM_DEBUG_LOG("Activity Count = %d", *count);
13711
13712         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13713         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_partial_body_activity_tbl WHERE account_id = %d AND mailbox_id = '%d' order by activity_id", account_id, input_mailbox_id);
13714
13715         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13716
13717
13718         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13719
13720         EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
13721         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13722                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13723
13724
13725         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13726         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13727                         ("sqlite3_step fail:%d", rc));
13728
13729         if (!(event_list = (email_event_partial_body_thd*)em_malloc(sizeof(email_event_partial_body_thd) * (*count)))) {
13730                 EM_DEBUG_EXCEPTION("Malloc failed");
13731
13732                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13733                 goto FINISH_OFF;
13734         }
13735         memset(event_list, 0x00, sizeof(email_event_partial_body_thd) * (*count));
13736
13737         for (i = 0; i < (*count); i++) {
13738                 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_IDX_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13739                 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13740                 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13741                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13742                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_type), ACTIVITY_TYPE_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13743                 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13744                 _get_stmt_field_data_string(hStmt, &(event_list[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13745                 _get_stmt_field_data_string(hStmt, &(event_list[i].multi_user_name), 0, MULTI_USER_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13746
13747                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13748                 /* EM_DEBUG_LOG("In emstorage_get_pbd_activity_data() loop, After sqlite3_step(), , i = %d, rc = %d.", i,  rc); */
13749                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13750                                 ("sqlite3_step fail:%d", rc));
13751
13752                 event_list[i].event_type = 0;
13753         }
13754
13755         ret = true;
13756
13757 FINISH_OFF:
13758         if (true == ret)
13759                 *event_start = event_list;
13760         else {
13761                 for (i = 0; i < (*count); i++)
13762                         emcore_free_partial_body_thd_event(event_list, NULL);
13763                 EM_SAFE_FREE(event_list); /*prevent 54559*/
13764                 *event_start = NULL;
13765                 *count = 0;
13766         }
13767
13768         if (hStmt != NULL) {
13769                 rc = sqlite3_finalize(hStmt);
13770                 hStmt = NULL;
13771                 if (rc != SQLITE_OK) {
13772                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13773                         error = EMAIL_ERROR_DB_FAILURE;
13774                 }
13775         }
13776
13777         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13778         if (err_code != NULL)
13779                 *err_code = error;
13780
13781         EM_DEBUG_FUNC_END("ret [%d]", ret);
13782         return ret;
13783 }
13784
13785 INTERNAL_FUNC int emstorage_delete_pbd_activity(char *multi_user_name, int account_id, int mail_id, int activity_id, int transaction, int *err_code)
13786 {
13787         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d] , activity_id[%d], transaction[%d], err_code[%p]", account_id, mail_id, activity_id, transaction, err_code);
13788
13789
13790         if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
13791                 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], activity_id[%d], transaction[%d], err_code[%p]", account_id, mail_id, activity_id, transaction, err_code);
13792
13793                 if (err_code != NULL)
13794                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13795                 return false;
13796         }
13797
13798         int rc = -1;
13799         int ret = false;
13800         int error = EMAIL_ERROR_NONE;
13801         char sql_query_string[QUERY_SIZE] = {0, };
13802         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13803
13804         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13805         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13806
13807         if (activity_id == 0)
13808                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d AND mail_id = %d", account_id, mail_id);
13809         else
13810                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d AND activity_id = %d", account_id, activity_id);
13811
13812         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13813         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
13814         if (error != EMAIL_ERROR_NONE) {
13815                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
13816                 goto FINISH_OFF;
13817         }
13818
13819         /*  validate activity existence */
13820         rc = sqlite3_changes(local_db_handle);
13821         if (rc == 0) {
13822                 EM_DEBUG_EXCEPTION("No matching activity found");
13823                 error = EMAIL_ERROR_DATA_NOT_FOUND;
13824                 ret = true;
13825                 goto FINISH_OFF;
13826         }
13827
13828         ret = true;
13829
13830 FINISH_OFF:
13831         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13832
13833         if (err_code != NULL)
13834                 *err_code = error;
13835
13836         EM_DEBUG_FUNC_END("ret [%d]", ret);
13837         return ret;
13838 }
13839
13840 INTERNAL_FUNC int emstorage_get_mailbox_pbd_activity_count(char *multi_user_name, int account_id, int input_mailbox_id, int *activity_count, int transaction, int *err_code)
13841 {
13842         EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
13843
13844         if (account_id < FIRST_ACCOUNT_ID || NULL == activity_count || NULL == err_code) {
13845                 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
13846                 if (err_code != NULL)
13847                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13848                 return false;
13849         }
13850         int rc = -1;
13851         int ret = false;
13852         int error = EMAIL_ERROR_NONE;
13853         char sql_query_string[QUERY_SIZE] = {0, };
13854
13855         DB_STMT hStmt = NULL;
13856
13857         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13858
13859         EMSTORAGE_START_READ_TRANSACTION(transaction);
13860         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13861
13862         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl WHERE account_id = %d and mailbox_id = '%d'", account_id, input_mailbox_id);
13863
13864         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13865
13866
13867         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13868         EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
13869         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13870                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13871
13872
13873         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13874         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13875                         ("sqlite3_step fail:%d", rc));
13876
13877         _get_stmt_field_data_int(hStmt, activity_count, 0);
13878
13879         EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
13880
13881         ret = true;
13882
13883 FINISH_OFF:
13884
13885         if (hStmt != NULL) {
13886                 rc = sqlite3_finalize(hStmt);
13887                 hStmt = NULL;
13888                 if (rc != SQLITE_OK) {
13889                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13890                         error = EMAIL_ERROR_DB_FAILURE;
13891                 }
13892         }
13893
13894         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13895
13896         if (err_code != NULL)
13897                 *err_code = error;
13898
13899         EM_DEBUG_FUNC_END("ret [%d]", ret);
13900         return ret;
13901 }
13902
13903 INTERNAL_FUNC int emstorage_get_pbd_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
13904 {
13905         EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
13906
13907         if (NULL == activity_count || NULL == err_code) {
13908                 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
13909                 if (err_code != NULL)
13910                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13911                 return false;
13912         }
13913         int rc = -1;
13914         int ret = false;
13915         int error = EMAIL_ERROR_NONE;
13916         DB_STMT hStmt = NULL;
13917         char sql_query_string[QUERY_SIZE] = {0, };
13918
13919         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13920
13921         EMSTORAGE_START_READ_TRANSACTION(transaction);
13922         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13923
13924         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl;");
13925
13926         EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
13927
13928         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13929         EM_DEBUG_LOG_DEV("  before sqlite3_prepare hStmt = %p", hStmt);
13930         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13931                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13932
13933         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13934         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13935                         ("sqlite3_step fail:%d", rc));
13936
13937         _get_stmt_field_data_int(hStmt, activity_count, 0);
13938
13939         EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
13940
13941         ret = true;
13942
13943 FINISH_OFF:
13944
13945
13946         if (hStmt != NULL) {
13947                 rc = sqlite3_finalize(hStmt);
13948                 hStmt = NULL;
13949                 if (rc != SQLITE_OK) {
13950                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13951                         error = EMAIL_ERROR_DB_FAILURE;
13952                 }
13953         }
13954
13955         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13956         if (err_code != NULL)
13957                 *err_code = error;
13958
13959         EM_DEBUG_FUNC_END("ret [%d]", ret);
13960         return ret;
13961 }
13962
13963 INTERNAL_FUNC int emstorage_delete_full_pbd_activity_data(char *multi_user_name, int account_id, int transaction, int *err_code)
13964 {
13965         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
13966         if (account_id < FIRST_ACCOUNT_ID) {
13967                 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
13968                 if (err_code != NULL)
13969                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13970                 return false;
13971         }
13972
13973         int rc = -1;
13974         int ret = false;
13975         int error = EMAIL_ERROR_NONE;
13976         char sql_query_string[QUERY_SIZE] = {0, };
13977
13978         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13979
13980         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13981         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13982         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d", account_id);
13983
13984         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13985         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
13986         if (error != EMAIL_ERROR_NONE) {
13987                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
13988                 goto FINISH_OFF;
13989         }
13990
13991         rc = sqlite3_changes(local_db_handle);
13992         if (rc == 0) {
13993                 EM_DEBUG_EXCEPTION("No matching activities found in mail_partial_body_activity_tbl");
13994                 error = EMAIL_ERROR_DATA_NOT_FOUND;
13995                 ret = true;
13996                 goto FINISH_OFF;
13997         }
13998
13999         ret = true;
14000
14001 FINISH_OFF:
14002
14003         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14004
14005         if (err_code != NULL)
14006                 *err_code = error;
14007
14008         EM_DEBUG_FUNC_END("ret [%d]", ret);
14009         return ret;
14010 }
14011
14012 /*Himanshu[h.gahlaut]-> Added below API to update mail_partial_body_activity_tbl
14013   if a mail is moved before its partial body is downloaded.Currently not used but should be used if mail move from server is enabled*/
14014
14015 INTERNAL_FUNC int emstorage_update_pbd_activity(char *multi_user_name, char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code)
14016 {
14017         EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
14018
14019         int rc = -1, ret = false;
14020         int error = EMAIL_ERROR_NONE;
14021         char sql_query_string[QUERY_SIZE] = {0, };
14022
14023         int transaction = true;
14024
14025         if (!old_server_uid || !new_server_uid || !mbox_name) {
14026                 EM_DEBUG_EXCEPTION("Invalid parameters");
14027                 error = EMAIL_ERROR_INVALID_PARAM;
14028                 return false;
14029         }
14030
14031         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14032
14033         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14034         memset(sql_query_string, 0x00, sizeof(sql_query_string));
14035         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14036                         "UPDATE mail_partial_body_activity_tbl SET server_mail_id = %s , mailbox_name=\'%s\' WHERE server_mail_id = %s ", new_server_uid, mbox_name, old_server_uid);
14037
14038         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14039         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14040         if (error != EMAIL_ERROR_NONE) {
14041                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14042                 goto FINISH_OFF;
14043         }
14044
14045         rc = sqlite3_changes(local_db_handle);
14046         if (rc == 0)
14047                 EM_DEBUG_LOG("No matching found in mail_partial_body_activity_tbl");
14048
14049         ret = true;
14050
14051 FINISH_OFF:
14052         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14053
14054         if (err_code != NULL)
14055                 *err_code = error;
14056
14057         EM_DEBUG_FUNC_END("ret [%d]", ret);
14058         return ret;
14059 }
14060
14061
14062 INTERNAL_FUNC int emstorage_create_file(char *data_string, size_t file_size, char *dst_file_name, int *err_code)
14063 {
14064         EM_DEBUG_FUNC_BEGIN_SEC("file_size[%d] , dst_file_name[%s], err_code[%p]", file_size, dst_file_name, err_code);
14065
14066         int ret = false;
14067         int error = EMAIL_ERROR_NONE;
14068         FILE* fp_dst = NULL;
14069         char errno_buf[ERRNO_BUF_SIZE] = {0};
14070
14071         if (!data_string || !dst_file_name) {
14072                 EM_DEBUG_LOG("data_string[%p], dst_file_name[%p]", data_string, dst_file_name);
14073                 error = EMAIL_ERROR_INVALID_PARAM;
14074                 goto FINISH_OFF;
14075         }
14076
14077         error = em_fopen(dst_file_name, "w", &fp_dst);
14078         if (error != EMAIL_ERROR_NONE) {
14079                 EM_DEBUG_EXCEPTION_SEC("em_fopen failed - %s: %d", dst_file_name, error);
14080                 goto FINISH_OFF;
14081         }
14082
14083         if (fwrite(data_string, 1, file_size, fp_dst) == 0) {
14084                 EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
14085                 error = EMAIL_ERROR_UNKNOWN;
14086                 goto FINISH_OFF;
14087         }
14088
14089         ret = true;
14090
14091 FINISH_OFF:
14092
14093         if (fp_dst != NULL)
14094                 fclose(fp_dst);
14095
14096         if (err_code != NULL)
14097                 *err_code = error;
14098
14099         EM_DEBUG_FUNC_END("ret [%d]", ret);
14100         return ret;
14101 }
14102
14103 #endif /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
14104
14105
14106
14107 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
14108 INTERNAL_FUNC int emstorage_update_read_mail_uid_by_server_uid(char *multi_user_name, char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code)
14109 {
14110         EM_DEBUG_FUNC_BEGIN();
14111         int rc = -1;
14112         int ret = false;
14113         int error = EMAIL_ERROR_NONE;
14114         char sql_query_string[QUERY_SIZE] = {0, };
14115
14116         int transaction = true;
14117
14118         if (!old_server_uid || !new_server_uid || !mbox_name) {
14119                 EM_DEBUG_EXCEPTION("Invalid parameters");
14120                 if (err_code != NULL)
14121                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14122                 return false;
14123         }
14124
14125         EM_DEBUG_LOG_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
14126
14127         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14128         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14129
14130
14131         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14132                         "UPDATE mail_read_mail_uid_tbl SET server_uid=\'%s\' , mailbox_name=\'%s\' WHERE server_uid=%s ", new_server_uid, mbox_name, old_server_uid);
14133
14134         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
14135         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14136         if (error != EMAIL_ERROR_NONE) {
14137                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14138                 goto FINISH_OFF;
14139         }
14140
14141         rc = sqlite3_changes(local_db_handle);
14142         if (rc == 0) {
14143                 EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
14144                 error = EMAIL_ERROR_DATA_NOT_FOUND;
14145                 goto FINISH_OFF;
14146         }
14147
14148         ret = true;
14149
14150 FINISH_OFF:
14151         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14152
14153         if (err_code != NULL)
14154                 *err_code = error;
14155
14156         EM_DEBUG_FUNC_END("ret [%d]", ret);
14157         return ret;
14158
14159 }
14160
14161
14162 /**
14163  * @fn emstorage_get_id_set_from_mail_ids(int mail_ids[], int mail_id_count, email_id_set_t** server_uids, int *id_set_count, int *err_code);
14164  * Prepare an array of mail_id and corresponding server mail id.
14165  *
14166  * @author                                      h.gahlaut@samsung.com
14167  * @param[in] mail_ids                  Specifies the comma separated string of mail_ids. Maximaum size of string should be less than or equal to (QUERY_SIZE - 88)
14168  *                                                      where 88 is the length of fixed keywords including ending null character in the QUERY to be formed
14169  * @param[out] idset                    Returns the array of mail_id and corresponding server_mail_id sorted by server_mail_ids
14170  * @param[out] id_set_count             Returns the no. of cells in idset array i.e. no. of sets of mail_ids and server_mail_ids
14171  * @param[out] err_code         Returns the error code.
14172  * @remarks                                     An Example of Query to be exexuted in this API:
14173  *                                                      SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (12, 13, 56, 78);
14174  * @return This function returns true on success or false on failure.
14175  */
14176 INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *multi_user_name,
14177                 char *mail_ids,
14178                 email_id_set_t** idset,
14179                 int *id_set_count,
14180                 int *err_code)
14181 {
14182         EM_DEBUG_FUNC_BEGIN();
14183         EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds);
14184
14185         int error = EMAIL_ERROR_NONE;
14186         int ret = false;
14187         email_id_set_t* p_id_set = NULL;
14188         int count = 0;
14189         const int buf_size = QUERY_SIZE;
14190         char sql_query_string[QUERY_SIZE] = {0, };
14191         int space_left_in_query_buffer = buf_size;
14192         int i = 0;
14193         int rc = -1;
14194         char *server_mail_id = NULL;
14195         char **result = NULL;
14196         int col_index = 0;
14197
14198
14199         if (NULL == mail_ids || NULL == idset || NULL == id_set_count) {
14200                 EM_DEBUG_EXCEPTION("Invalid Parameters mail_ids[%p] idset[%p] id_set_count [%p]",
14201                                 mail_ids, idset, id_set_count);
14202                 if (err_code != NULL)
14203                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14204                 return false;
14205         }
14206
14207         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14208
14209         SNPRINTF(sql_query_string, space_left_in_query_buffer,
14210                         "SELECT local_uid, server_uid FROM mail_read_mail_uid_tbl WHERE local_uid in (%s) ORDER BY server_uid",
14211                         mail_ids);
14212
14213         EM_DEBUG_LOG_SEC("SQL Query formed [%s] ", sql_query_string);
14214
14215         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14216         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14217         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result);
14218                         goto FINISH_OFF; },     ("SQL(%s) sqlite3_get_table fail:%d -%s",
14219                                 sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14220
14221         EM_DEBUG_LOG(" Count of mails [%d ]", count);
14222
14223         if (count <= 0) {
14224                 EM_DEBUG_EXCEPTION("Can't find proper mail");
14225                 error = EMAIL_ERROR_DATA_NOT_FOUND;
14226                 goto FINISH_OFF;
14227         }
14228
14229         if (NULL == (p_id_set = (email_id_set_t*)em_malloc(sizeof(email_id_set_t) * count))) {
14230                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
14231                 error = EMAIL_ERROR_OUT_OF_MEMORY;
14232                 goto FINISH_OFF;
14233         }
14234
14235         col_index = 2;
14236
14237         EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds_Loop);
14238         EM_DEBUG_LOG(">>>> DATA ASSIGN START");
14239         for (i = 0; i < count; i++) {
14240                 _get_table_field_data_int(result, &(p_id_set[i].mail_id), col_index++);
14241                 _get_table_field_data_string(result, &server_mail_id, 1, col_index++);
14242                 if (server_mail_id) {
14243                         p_id_set[i].server_mail_id = strtoul(server_mail_id, NULL, 10);
14244                         EM_SAFE_FREE(server_mail_id);
14245                 }
14246         }
14247         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d]", count);
14248         EM_PROFILE_END(EmStorageGetIdSetFromMailIds_Loop);
14249
14250         sqlite3_free_table(result);
14251         result = NULL;
14252
14253         ret = true;
14254
14255 FINISH_OFF:
14256
14257         if (ret == true) {
14258                 *idset = p_id_set;
14259                 *id_set_count = count;
14260                 EM_DEBUG_LOG(" idset[%p] id_set_count [%d]", *idset, *id_set_count);
14261         } else
14262                 EM_SAFE_FREE(p_id_set);
14263
14264
14265         if (err_code != NULL)
14266                 *err_code = error;
14267
14268         EM_PROFILE_END(EmStorageGetIdSetFromMailIds);
14269
14270         EM_DEBUG_FUNC_END("ret [%d]", ret);
14271         return ret;
14272 }
14273
14274
14275
14276 #endif
14277
14278 INTERNAL_FUNC int emstorage_delete_triggers_from_lucene(char *multi_user_name)
14279 {
14280         EM_DEBUG_FUNC_BEGIN();
14281         int ret = true, transaction = true;
14282         int error = EMAIL_ERROR_NONE;
14283         char sql_query_string[QUERY_SIZE] = {0, };
14284         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14285         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14286
14287         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerDelete;");
14288         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14289         if (error != EMAIL_ERROR_NONE) {
14290                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14291                 goto FINISH_OFF;
14292         }
14293
14294         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerInsert;");
14295         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14296         if (error != EMAIL_ERROR_NONE) {
14297                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14298                 goto FINISH_OFF;
14299         }
14300
14301         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerUpdate;");
14302         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14303         if (error != EMAIL_ERROR_NONE) {
14304                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14305                 goto FINISH_OFF;
14306         }
14307
14308         ret = true;
14309
14310 FINISH_OFF:
14311         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14312
14313         EM_DEBUG_FUNC_END("ret [%d]", ret);
14314         return ret;
14315 }
14316
14317 INTERNAL_FUNC int emstorage_update_tag_id(char *multi_user_name, int old_filter_id, int new_filter_id, int *err_code)
14318 {
14319         EM_DEBUG_FUNC_BEGIN("new_filter_id[%d], old_filter_id[%d]", new_filter_id, old_filter_id);
14320         int ret = false;
14321         int error = EMAIL_ERROR_NONE;
14322         char sql_query_string[QUERY_SIZE] = {0, };
14323         int transaction = true;
14324
14325         if (old_filter_id < 0 || new_filter_id < 0) {
14326                 EM_DEBUG_EXCEPTION("Invalid parameters");
14327                 if (err_code != NULL)
14328                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14329                 return false;
14330         }
14331
14332         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14333
14334         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14335
14336         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14337                         "UPDATE mail_tbl SET tag_id=%d WHERE tag_id=%d ", new_filter_id, old_filter_id);
14338
14339         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14340         if (error != EMAIL_ERROR_NONE) {
14341                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14342                 goto FINISH_OFF;
14343         }
14344
14345         ret = true;
14346
14347 FINISH_OFF:
14348         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14349
14350         if (err_code != NULL)
14351                 *err_code = error;
14352
14353         EM_DEBUG_FUNC_END("ret [%d]", ret);
14354         return ret;
14355 }
14356
14357 INTERNAL_FUNC int emstorage_filter_mails_by_rule(char *multi_user_name, int account_id, int dest_mailbox_id, int dest_mailbox_type, int reset, emstorage_rule_tbl_t *rule, int ** filtered_mail_id_list, int *count_of_mails, int *err_code)
14358 {
14359         EM_DEBUG_FUNC_BEGIN("account_id[%d], dest_mailbox_id[%d] rule[%p], filtered_mail_id_list[%p], count_of_mails[%p], err_code[%p]", account_id, dest_mailbox_id, rule, filtered_mail_id_list, count_of_mails, err_code);
14360
14361         if ((account_id < 0) || (dest_mailbox_id < 0) || (!rule)) {
14362                 EM_DEBUG_EXCEPTION("Invalid Parameter");
14363
14364                 if (err_code != NULL)
14365                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14366                 return false;
14367         }
14368
14369         int rc = -1, ret = false, error = EMAIL_ERROR_NONE;
14370         int count = 0, col_index = 0, i = 0, where_pararaph_length = 0, *mail_list = NULL;
14371         int tag_id = rule->rule_id;
14372         char **result = NULL, *where_pararaph = NULL;
14373         char sql_query_string[QUERY_SIZE] = {0, };
14374         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14375
14376         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl ");
14377
14378         where_pararaph_length = EM_SAFE_STRLEN(rule->value) + 2 * (EM_SAFE_STRLEN(rule->value2)) + 100;
14379         where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
14380         if (where_pararaph == NULL) {
14381                 EM_DEBUG_EXCEPTION("malloc failed for where_pararaph.");
14382                 error = EMAIL_ERROR_OUT_OF_MEMORY;
14383                 goto FINISH_OFF;
14384         }
14385
14386         if (account_id != ALL_ACCOUNT)
14387                 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (0)", account_id);
14388         else
14389                 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_type NOT in (0)");
14390
14391         if (rule->type & EMAIL_FILTER_SUBJECT) {
14392                 if (rule->flag2 == RULE_TYPE_INCLUDES)
14393                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject like \'%%%q%%\'", rule->value);
14394                 else /*  RULE_TYPE_EXACTLY */
14395                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject = \'%q\'", rule->value);
14396         }
14397
14398         if (rule->type & EMAIL_FILTER_FROM) {
14399                 if (rule->flag2 == RULE_TYPE_INCLUDES)
14400                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND full_address_from like \'%%%q%%\'", rule->value2);
14401 #ifdef __FEATURE_COMPARE_DOMAIN__
14402                 else if (rule->flag2 == RULE_TYPE_COMPARE_DOMAIN)
14403                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND (full_address_from like \'@%%%q\' OR full_address_from like \'@%%%q>%%\')", rule->value2, rule->value2);
14404 #endif /*__FEATURE_COMPARE_DOMAIN__ */
14405                 else /*  RULE_TYPE_EXACTLY */
14406                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND full_address_from = \'%q\'", rule->value2);
14407         }
14408
14409         if (rule->type == EMAIL_PRIORITY_SENDER) {
14410                 if (rule->flag2 == RULE_TYPE_INCLUDES)
14411                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND full_address_from like \'%%%q%%\'", rule->value2);
14412                 else /*  RULE_TYPE_EXACTLY */
14413                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND full_address_from = \'%q\' OR email_address_sender = \'%q\'", rule->value2, rule->value2);
14414
14415                 tag_id = PRIORITY_SENDER_TAG_ID;
14416         }
14417
14418         /* prevent 34361 */
14419         if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14420                 EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14421         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14422
14423         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14424         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14425         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
14426                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14427
14428         EM_DEBUG_LOG("Count of mails [%d]", count);
14429
14430         if (count) {
14431                 mail_list = malloc(sizeof(int) * count);
14432                 if (mail_list == NULL) {
14433                         EM_DEBUG_EXCEPTION("malloc failed for mail_list.");
14434                         error = EMAIL_ERROR_OUT_OF_MEMORY;
14435                         goto FINISH_OFF;
14436                 }
14437
14438                 col_index = 1;
14439
14440                 for (i = 0; i < count; i++)
14441                         _get_table_field_data_int(result, &(mail_list[i]), col_index++);
14442
14443                 memset(sql_query_string, 0x00, QUERY_SIZE);
14444                 if (reset) {
14445                         switch (rule->action_type) {
14446                         case EMAIL_FILTER_MOVE:
14447                                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = 0 ");
14448                                 break;
14449                         case EMAIL_FILTER_BLOCK:
14450                         default:
14451                                 EM_DEBUG_LOG("Not support : action_type[%d]", rule->action_type);
14452                                 ret = true;
14453                                 goto FINISH_OFF;
14454                         }
14455                 } else {
14456                         switch (rule->action_type) {
14457                         case EMAIL_FILTER_MOVE:
14458                                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = %d ", tag_id);
14459                                 break;
14460                         case EMAIL_FILTER_BLOCK:
14461                                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET mailbox_id = %d, mailbox_type = %d ", dest_mailbox_id, dest_mailbox_type);
14462                                 break;
14463                         default:
14464                                 EM_DEBUG_LOG("Not support");
14465                                 ret = true;
14466                                 goto FINISH_OFF;
14467                         }
14468                 }
14469                 /* prevent 34361 */
14470                 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14471                         EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14472
14473                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14474                 if (error != EMAIL_ERROR_NONE) {
14475                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14476                         goto FINISH_OFF;
14477                 }
14478
14479 #ifdef __FEATURE_BODY_SEARCH__
14480                 /* Updating mail_text_tbl */
14481                 if (rule->action_type == EMAIL_FILTER_BLOCK) {
14482                         memset(sql_query_string, 0x00, QUERY_SIZE);
14483                         SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_text_tbl SET mailbox_id = %d ", dest_mailbox_id);
14484                         if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14485                                 EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14486
14487                         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14488                         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14489                         if (error != EMAIL_ERROR_NONE) {
14490                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14491                                 goto FINISH_OFF;
14492                         }
14493                 }
14494 #endif
14495         }
14496
14497         ret = true;
14498
14499 FINISH_OFF:
14500
14501         if (ret) {
14502                 if (filtered_mail_id_list)
14503                         *filtered_mail_id_list = mail_list;
14504
14505                 if (count_of_mails)
14506                         *count_of_mails = count;
14507         } else
14508                 EM_SAFE_FREE(mail_list);
14509
14510         sqlite3_free_table(result);
14511         result = NULL;
14512
14513
14514         EM_SAFE_FREE(where_pararaph);
14515
14516         if (err_code != NULL)
14517                 *err_code = error;
14518
14519         EM_DEBUG_FUNC_END("ret [%d]", ret);
14520         return ret;
14521 }
14522
14523 #define EMAIL_SLOT_UNIT 25
14524 INTERNAL_FUNC int emstorage_set_mail_slot_size(char *multi_user_name, int account_id, int mailbox_id, int new_slot_size, int transaction, int *err_code)
14525 {
14526         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%p] new_slot_size[%d], err_code[%p]", account_id, mailbox_id, new_slot_size, err_code);
14527         int ret = false, err = EMAIL_ERROR_NONE;
14528         int where_pararaph_length = 0;
14529         char *where_pararaph = NULL;
14530         char sql_query_string[QUERY_SIZE] = {0, };
14531         int and = 0;
14532         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14533
14534         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
14535
14536         if (new_slot_size > 0)
14537                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = %d ", new_slot_size);
14538         else if (new_slot_size == 0)
14539                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", EMAIL_SLOT_UNIT);
14540         else
14541                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", new_slot_size * -1);
14542
14543
14544         if (mailbox_id)
14545                 where_pararaph_length = 80;
14546         else
14547                 where_pararaph_length = 50;
14548
14549         if (new_slot_size == 0)
14550                 where_pararaph_length += 70;
14551
14552         where_pararaph = malloc(sizeof(char) * where_pararaph_length);
14553         if (where_pararaph == NULL) {
14554                 EM_DEBUG_EXCEPTION("Memory allocation failed for where_pararaph");
14555                 err = EMAIL_ERROR_OUT_OF_MEMORY;
14556                 goto FINISH_OFF;
14557         }
14558         memset(where_pararaph, 0x00, where_pararaph_length);
14559
14560         if (account_id > ALL_ACCOUNT) {
14561                 and = 1;
14562                 if (mailbox_id)
14563                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_id = %d ", mailbox_id);
14564                 else
14565                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d ", account_id);
14566         }
14567
14568         if (new_slot_size == 0)
14569                 SNPRINTF(where_pararaph + EM_SAFE_STRLEN(where_pararaph), where_pararaph_length - EM_SAFE_STRLEN(where_pararaph), " %s total_mail_count_on_server > mail_slot_size ", (and ? "AND" : "WHERE"));
14570
14571         if (strlen(sql_query_string) + EM_SAFE_STRLEN(where_pararaph) < QUERY_SIZE) /* prevent 34363 */
14572                 EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14573         else {
14574                 EM_DEBUG_EXCEPTION("Query buffer overflowed !!!");
14575                 err = EMAIL_ERROR_OUT_OF_MEMORY;
14576                 goto FINISH_OFF;
14577         }
14578
14579         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14580         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14581         if (err != EMAIL_ERROR_NONE) {
14582                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
14583                 goto FINISH_OFF;
14584         }
14585
14586         ret = true;
14587
14588 FINISH_OFF:
14589         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
14590
14591         EM_SAFE_FREE(where_pararaph);
14592
14593         if (err_code != NULL)
14594                 *err_code = err;
14595
14596         EM_DEBUG_FUNC_END("ret [%d]", ret);
14597         return ret;
14598 }
14599
14600 INTERNAL_FUNC int emstorage_add_meeting_request(char *multi_user_name, int account_id, int input_mailbox_id, email_meeting_request_t* meeting_req, int transaction, int *err_code)
14601 {
14602         EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], meeting_req[%p], transaction[%d], err_code[%p]", account_id, input_mailbox_id, meeting_req, transaction, err_code);
14603
14604         if (!meeting_req || meeting_req->mail_id <= 0) {
14605                 if (meeting_req)
14606                         EM_DEBUG_EXCEPTION("mail_id[%]d", meeting_req->mail_id);
14607
14608                 if (err_code != NULL)
14609                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14610
14611                 return false;
14612         }
14613
14614         int rc = -1;
14615         int ret = false;
14616         int error = EMAIL_ERROR_NONE;
14617         DB_STMT hStmt = NULL;
14618         char sql_query_string[QUERY_SIZE] = {0, };
14619         int col_index = 0;
14620         time_t temp_unix_time = 0;
14621
14622         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14623         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14624
14625         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14626                         "INSERT INTO mail_meeting_tbl VALUES "
14627                         "(?"            /*  mail_id */
14628                         ", ?"           /*  account_id */
14629                         ", ?"           /*  mailbox_id */
14630                         ", ?"           /*  meeting_response */
14631                         ", ?"           /*  start_time */
14632                         ", ?"           /*  end_time */
14633                         ", ?"           /*  location */
14634                         ", ?"           /*  global_object_id */
14635                         ", ?"           /*  offset */
14636                         ", ?"           /*  standard_name */
14637                         ", ?"           /*  standard_time_start_date */
14638                         ", ?"           /*  standard_biad */
14639                         ", ?"           /*  daylight_name */
14640                         ", ?"           /*  daylight_time_start_date */
14641                         ", ?"           /*  daylight_bias */
14642                         ")");
14643
14644         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14645         if (rc != SQLITE_OK) {
14646                 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
14647                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
14648
14649                 error = EMAIL_ERROR_DB_FAILURE;
14650                 goto FINISH_OFF;
14651         }
14652
14653         col_index = 0;
14654         /*
14655            EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
14656            EM_DEBUG_LOG_SEC(">>>>> account_id[%d]", account_id);
14657            EM_DEBUG_LOG_SEC(">>>>> mailbox_name[%s]", mailbox_name);
14658            EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
14659            EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
14660            EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
14661            EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
14662            EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
14663            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
14664            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
14665            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
14666            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
14667            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
14668            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
14669            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
14670            */
14671         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->mail_id);
14672         _bind_stmt_field_data_int(hStmt, col_index++, account_id);
14673         _bind_stmt_field_data_int(hStmt, col_index++, input_mailbox_id);
14674         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
14675
14676         temp_unix_time = timegm(&(meeting_req->start_time));
14677         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14678         temp_unix_time = timegm(&(meeting_req->end_time));
14679         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14680
14681         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->location, 0, LOCATION_LEN_IN_MAIL_MEETING_TBL);
14682         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->global_object_id, 0, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
14683
14684         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
14685         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.standard_name, 0, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
14686         temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
14687         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14688         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
14689
14690         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.daylight_name, 0, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
14691         temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
14692         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14693         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
14694
14695
14696         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14697         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
14698                         ("sqlite3_step fail:%d", rc));
14699         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14700                         ("sqlite3_step fail:%d", rc));
14701
14702         ret = true;
14703
14704 FINISH_OFF:
14705         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14706         if (hStmt != NULL) {
14707                 rc = sqlite3_finalize(hStmt);
14708                 if (rc != SQLITE_OK) {
14709                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14710                         error = EMAIL_ERROR_DB_FAILURE;
14711                 }
14712         }
14713
14714         if (err_code != NULL)
14715                 *err_code = error;
14716
14717         EM_DEBUG_FUNC_END("ret [%d]", ret);
14718         return ret;
14719 }
14720
14721 INTERNAL_FUNC int emstorage_query_meeting_request(char *multi_user_name, const char *conditional_clause, email_meeting_request_t **output_meeting_req, int *output_result_count, int transaction)
14722 {
14723         EM_DEBUG_FUNC_BEGIN("conditional_clause[%s] output_meeting_req[%p] output_result_count[%p] transaction[%d]", conditional_clause, output_meeting_req, output_result_count, transaction);
14724
14725         int i = 0;
14726         int col_index = 0;
14727         int rc;
14728         int count = 0;
14729         int dummy = 0;
14730         int err = EMAIL_ERROR_NONE;
14731         char **result = NULL;
14732         char sql_query_string[QUERY_SIZE] = {0, };
14733         email_meeting_request_t* p_temp_meeting_req = NULL;
14734         sqlite3 *local_db_handle = NULL;
14735         time_t temp_unix_time;
14736
14737         if (conditional_clause == NULL || output_meeting_req == NULL || output_result_count == NULL) {
14738                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14739                 err = EMAIL_ERROR_INVALID_PARAM;
14740                 goto FINISH_OFF;
14741         }
14742
14743         col_index = _field_count_of_table[CREATE_TABLE_MAIL_MEETING_TBL];
14744         EM_DEBUG_LOG("col_index [%d]", col_index);
14745
14746         local_db_handle = emstorage_get_db_connection(multi_user_name);
14747
14748         EMSTORAGE_START_READ_TRANSACTION(transaction);
14749
14750         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_meeting_tbl %s", conditional_clause);
14751
14752         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14753
14754         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14755         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
14756                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14757
14758         if (!count) {
14759                 EM_DEBUG_EXCEPTION("No meeting_request found...");
14760                 err = EMAIL_ERROR_DATA_NOT_FOUND;
14761                 goto FINISH_OFF;
14762         }
14763
14764         EM_DEBUG_LOG("There are [%d] meeting requests.", count);
14765         if (!(p_temp_meeting_req = (email_meeting_request_t*)em_malloc(sizeof(email_meeting_request_t) * count))) {
14766                 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
14767                 err = EMAIL_ERROR_OUT_OF_MEMORY;
14768                 goto FINISH_OFF;
14769         }
14770
14771         EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
14772
14773         for (i = 0; i < count; i++) {
14774                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].mail_id), col_index++);
14775                 _get_table_field_data_int(result, &dummy, col_index++); /* account_id. but why should this field exist in DB table? */
14776                 _get_table_field_data_int(result, &dummy, col_index++); /* mailbox_id */
14777                 _get_table_field_data_int(result, (int*)&(p_temp_meeting_req[i].meeting_response), col_index++);
14778                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* start time */
14779                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].start_time));
14780                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* end time */
14781                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].end_time));
14782                 _get_table_field_data_string(result, &p_temp_meeting_req[i].location, 1, col_index++);
14783                 _get_table_field_data_string(result, &p_temp_meeting_req[i].global_object_id, 1, col_index++);
14784                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.offset_from_GMT), col_index++);
14785                 _get_table_field_data_string_without_allocation(result, p_temp_meeting_req[i].time_zone.standard_name, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL, 1, col_index++);
14786                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14787                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.standard_time_start_date));
14788                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.standard_bias), col_index++);
14789                 _get_table_field_data_string_without_allocation(result, p_temp_meeting_req[i].time_zone.daylight_name, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL, 1, col_index++);
14790                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14791                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.daylight_time_start_date));
14792                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.daylight_bias), col_index++);
14793         }
14794
14795
14796 FINISH_OFF:
14797         if (result)
14798                 sqlite3_free_table(result);
14799
14800         if (err == EMAIL_ERROR_NONE) {
14801                 if (p_temp_meeting_req)
14802                         *output_meeting_req = p_temp_meeting_req;
14803                 *output_result_count = count;
14804         } else
14805                 EM_SAFE_FREE(p_temp_meeting_req);
14806
14807         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14808
14809         //      sqlite3_db_release_memory(local_db_handle);
14810
14811         EM_DEBUG_FUNC_END("err [%d]", err);
14812         return err;
14813 }
14814
14815 INTERNAL_FUNC int emstorage_get_meeting_request(char *multi_user_name, int mail_id, email_meeting_request_t ** meeting_req, int transaction, int *err_code)
14816 {
14817         EM_DEBUG_FUNC_BEGIN();
14818
14819         int count = 0;
14820         int ret = false;
14821         int error = EMAIL_ERROR_NONE;
14822         char conditional_clause[QUERY_SIZE] = {0, };
14823
14824         EM_IF_NULL_RETURN_VALUE(meeting_req, false);
14825
14826
14827         SNPRINTF(conditional_clause, QUERY_SIZE, " WHERE mail_id = %d", mail_id);
14828         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
14829
14830         if ((error = emstorage_query_meeting_request(multi_user_name, conditional_clause, meeting_req, &count, transaction)) != EMAIL_ERROR_NONE) {
14831                 EM_DEBUG_EXCEPTION("emstorage_query_meeting_request failed. [%d]", error);
14832                 goto FINISH_OFF;
14833         }
14834
14835         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->mail_id[%d]", (*meeting_req)->mail_id);
14836         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->meeting_response[%d]", (*meeting_req)->meeting_response);
14837         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->start_time[%s]", asctime(&((*meeting_req)->start_time)));
14838         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->end_time[%s]", asctime(&((*meeting_req)->end_time)));
14839         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->location[%s]", (*meeting_req)->location);
14840         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->global_object_id[%s]", (*meeting_req)->global_object_id);
14841         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.offset_from_GMT[%d]", (*meeting_req)->time_zone.offset_from_GMT);
14842         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_name[%s]", (*meeting_req)->time_zone.standard_name);
14843         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.standard_time_start_date)));
14844         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_bias[%d]", (*meeting_req)->time_zone.standard_bias);
14845         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_name[%s]", (*meeting_req)->time_zone.daylight_name);
14846         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.daylight_time_start_date)));
14847         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_bias[%d]", (*meeting_req)->time_zone.daylight_bias);
14848         ret = true;
14849
14850 FINISH_OFF:
14851
14852         if (err_code != NULL)
14853                 *err_code = error;
14854
14855         EM_DEBUG_FUNC_END("ret [%d]", ret);
14856         return ret;
14857 }
14858
14859 INTERNAL_FUNC int emstorage_update_meeting_request(char *multi_user_name, email_meeting_request_t* meeting_req, int transaction, int *err_code)
14860 {
14861         EM_DEBUG_FUNC_BEGIN("meeting_req[%p], transaction[%d], err_code[%p]", meeting_req, transaction, err_code);
14862
14863         int ret = false;
14864         int error = EMAIL_ERROR_NONE;
14865         int rc;
14866         DB_STMT hStmt = NULL;
14867         char sql_query_string[QUERY_SIZE] = {0, };
14868         time_t temp_unix_time = 0;
14869
14870         if (!meeting_req) {
14871                 EM_DEBUG_EXCEPTION("Invalid Parameter!");
14872                 if (err_code != NULL)
14873                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14874                 return false;
14875         }
14876
14877         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14878         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14879
14880         memset(sql_query_string, 0x00, sizeof(sql_query_string));
14881         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14882                         "UPDATE mail_meeting_tbl "
14883                         "SET "
14884                         /* "  account_id = ?, "         //  not update here, this can be changed when move or copy */
14885                         /* "  mailbox_name = ?, "               //  not update here, this can be changed when move or copy */
14886                         "  meeting_response = ?, "
14887                         "  start_time = ?, "
14888                         "  end_time = ?, "
14889                         "  location = ?, "
14890                         "  global_object_id = ?, "
14891                         "  offset = ?, "
14892                         "  standard_name = ?, "
14893                         "  standard_time_start_date = ?, "
14894                         "  standard_bias = ?, "
14895                         "  daylight_name = ?, "
14896                         "  daylight_time_start_date = ?, "
14897                         "  daylight_bias = ? "
14898                         "WHERE mail_id = %d",
14899                         meeting_req->mail_id);
14900
14901         EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
14902
14903         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14904         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14905                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14906         /*
14907            EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
14908            EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
14909            EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
14910            EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
14911            EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
14912            EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
14913            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
14914            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
14915            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
14916            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
14917            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
14918            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
14919            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
14920            */
14921         int col_index = 0;
14922
14923         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
14924         temp_unix_time = timegm(&(meeting_req->start_time));
14925         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14926         temp_unix_time = timegm(&(meeting_req->end_time));
14927         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14928         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->location, 1, LOCATION_LEN_IN_MAIL_MEETING_TBL);
14929         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->global_object_id, 1, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
14930         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
14931         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.standard_name, 1, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
14932         temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
14933         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14934         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
14935         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.daylight_name, 1, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
14936         temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
14937         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14938         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
14939
14940
14941         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14942         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14943                         ("sqlite3_step fail:%d", rc));
14944         ret = true;
14945
14946
14947 FINISH_OFF:
14948         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14949
14950         if (hStmt != NULL) {
14951                 rc = sqlite3_finalize(hStmt);
14952                 if (rc != SQLITE_OK) {
14953                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14954                         error = EMAIL_ERROR_DB_FAILURE;
14955                 }
14956         }
14957
14958         if (err_code != NULL)
14959                 *err_code = error;
14960
14961         EM_DEBUG_FUNC_END("ret [%d]", ret);
14962         return ret;
14963 }
14964
14965 INTERNAL_FUNC int emstorage_delete_meeting_request(char *multi_user_name, int account_id, int mail_id, int input_mailbox_id, int transaction, int *err_code)
14966 {
14967         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], input_mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mail_id, input_mailbox_id, transaction, err_code);
14968
14969         if (account_id < ALL_ACCOUNT || mail_id < 0) {
14970                 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d]", account_id, mail_id);
14971
14972                 if (err_code != NULL)
14973                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14974                 return false;
14975         }
14976
14977         int ret = false;
14978         int error = EMAIL_ERROR_NONE;
14979         int and = false;
14980         char sql_query_string[QUERY_SIZE] = {0, };
14981
14982         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14983         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14984
14985         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_meeting_tbl ");
14986
14987         if (account_id != ALL_ACCOUNT) {                /*  NOT '0' means a specific account. '0' means all account */
14988                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE account_id = %d", account_id);
14989                 and = true;
14990         }
14991         if (mail_id > 0) {
14992                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s mail_id = %d", (and ? "AND" : "WHERE"), mail_id);
14993                 and = true;
14994         }
14995         if (input_mailbox_id > 0) {             /*  0 means all mailbox_id */
14996                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s mailbox_id = '%d'",  (and ? "AND" : "WHERE"), input_mailbox_id);
14997         }
14998
14999         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15000         if (error != EMAIL_ERROR_NONE) {
15001                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
15002                 goto FINISH_OFF;
15003         }
15004
15005         ret = true;
15006
15007 FINISH_OFF:
15008         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15009
15010         if (err_code)
15011                 *err_code = error;
15012
15013         EM_DEBUG_FUNC_END("ret [%d]", ret);
15014         return ret;
15015 }
15016
15017
15018 INTERNAL_FUNC void emstorage_free_meeting_request(email_meeting_request_t *meeting_req)
15019 {
15020         EM_DEBUG_FUNC_BEGIN();
15021
15022         if (!meeting_req) return;
15023
15024         EM_SAFE_FREE(meeting_req->location);
15025         EM_SAFE_FREE(meeting_req->global_object_id);
15026
15027         EM_DEBUG_FUNC_END();
15028 }
15029
15030 INTERNAL_FUNC int emstorage_get_overflowed_mail_id_list(char *multi_user_name, int account_id, int input_mailbox_id, int mail_slot_size, int **mail_id_list, int *mail_id_count, int transaction, int *err_code)
15031 {
15032         EM_DEBUG_FUNC_BEGIN("account_id [%d], input_mailbox_id [%d], mail_slot_size [%d], mail_id_list [%p], mail_id_count [%p], transaction [%d], err_code [%p]", account_id, input_mailbox_id, mail_slot_size, mail_id_list, mail_id_count, transaction, err_code);
15033         EM_PROFILE_BEGIN(profile_emstorage_get_overflowed_mail_id_list);
15034         char sql_query_string[QUERY_SIZE] = {0, };
15035         char **result = NULL;
15036         int rc = -1, ret = false;
15037         int error = EMAIL_ERROR_NONE;
15038         int counter = 0, col_index = 0;
15039         int result_mail_id_count = 0;
15040         int *result_mail_id_list = NULL;
15041
15042         if (input_mailbox_id <= 0 || !mail_id_list || !mail_id_count || account_id < 1) {
15043                 EM_DEBUG_EXCEPTION("Invalid Parameter");
15044                 if (err_code)
15045                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15046                 return false;
15047         }
15048
15049         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl WHERE account_id = %d AND mailbox_id = %d ORDER BY date_time DESC LIMIT %d, 10000", account_id, input_mailbox_id, mail_slot_size);
15050
15051         EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
15052
15053         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15054         EMSTORAGE_START_READ_TRANSACTION(transaction);
15055
15056         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_mail_id_count, 0, NULL), rc);
15057         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15058                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15059
15060         if (!result_mail_id_count) {
15061                 EM_DEBUG_LOG("No mail found...");
15062                 ret = false;
15063                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
15064                 goto FINISH_OFF;
15065         }
15066
15067         EM_DEBUG_LOG("There are [%d] overflowed mails in mailbox_id [%d]", result_mail_id_count, input_mailbox_id);
15068
15069         if (!(result_mail_id_list = (int *)malloc(sizeof(int) * result_mail_id_count))) {
15070                 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
15071                 error = EMAIL_ERROR_OUT_OF_MEMORY;
15072                 sqlite3_free_table(result);
15073                 goto FINISH_OFF;
15074         }
15075
15076         memset(result_mail_id_list, 0x00, sizeof(int) * result_mail_id_count);
15077
15078         col_index = 1;
15079
15080         for (counter = 0; counter < result_mail_id_count; counter++)
15081                 _get_table_field_data_int(result, result_mail_id_list + counter, col_index++);
15082
15083         ret = true;
15084
15085 FINISH_OFF:
15086         EM_DEBUG_LOG("finish off [%d]", ret);
15087
15088         if (result)
15089                 sqlite3_free_table(result);
15090
15091         if (ret == true) {
15092                 *mail_id_list = result_mail_id_list;
15093                 *mail_id_count = result_mail_id_count;
15094         } else
15095                 EM_SAFE_FREE(result_mail_id_list);
15096
15097         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15098
15099         if (err_code != NULL)
15100                 *err_code = error;
15101
15102         EM_PROFILE_END(profile_emstorage_get_overflowed_mail_id_list);
15103         EM_DEBUG_FUNC_END("ret [%d]", ret);
15104         return ret;
15105 }
15106
15107 INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(char *multi_user_name, int mail_id, int *thread_id, int *err_code)
15108 {
15109         EM_DEBUG_FUNC_BEGIN("mail_id[%d], thread_id[%p], err_code[%p]", mail_id, thread_id, err_code);
15110
15111         int rc = -1, ret = false;
15112         int err = EMAIL_ERROR_NONE;
15113         char sql_query_string[QUERY_SIZE] = {0, };
15114         char **result;
15115         int result_count = 0;
15116
15117         if (mail_id == 0 || thread_id == NULL) {
15118                 EM_DEBUG_EXCEPTION("Invalid Parameter");
15119                 if (err_code)
15120                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15121                 return false;
15122         }
15123
15124         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15125
15126         memset(sql_query_string, 0, QUERY_SIZE);
15127         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT thread_id FROM mail_tbl WHERE mail_id = %d", mail_id);
15128
15129         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
15130         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
15131         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15132                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15133
15134         if (!result_count) {
15135                 EM_DEBUG_LOG("No mail found...");
15136                 ret = false;
15137                 err = EMAIL_ERROR_MAIL_NOT_FOUND;
15138                 /* sqlite3_free_table(result); */
15139                 goto FINISH_OFF;
15140         }
15141
15142         _get_table_field_data_int(result, thread_id, 1);
15143
15144         sqlite3_free_table(result);
15145
15146         ret = true;
15147
15148 FINISH_OFF:
15149
15150         if (err_code != NULL)
15151                 *err_code = err;
15152
15153         EM_DEBUG_FUNC_END("ret [%d]", ret);
15154         return ret;
15155 }
15156
15157 INTERNAL_FUNC int emstorage_update_latest_thread_mail(char *multi_user_name,
15158                 int account_id,
15159                 int mailbox_id,
15160                 int mailbox_type,
15161                 int thread_id,
15162                 int *updated_thread_id,
15163                 int latest_mail_id,
15164                 int thread_item_count,
15165                 int noti_type,
15166                 int transaction,
15167                 int *err_code)
15168 {
15169         EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id [%d], thread_id[%d], updated_thread_id[%p], "
15170                         "latest_mail_id [%d], thread_item_count[%d], err_code[%p]",
15171                         account_id, mailbox_id, thread_id, updated_thread_id,
15172                         latest_mail_id, thread_item_count, err_code);
15173
15174         int rc = -1, ret = false;
15175         int err = EMAIL_ERROR_NONE;
15176         char sql_query_string[QUERY_SIZE] = {0, };
15177         char **result = NULL;
15178         int result_count = 0;
15179
15180         if (thread_id == 0) {
15181                 EM_DEBUG_EXCEPTION("Invalid Parameter");
15182                 if (err_code)
15183                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15184                 return false;
15185         }
15186
15187         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15188
15189         if (thread_item_count == 0 && latest_mail_id == 0) {
15190                 memset(sql_query_string, 0, QUERY_SIZE);
15191                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id, count(*) FROM (SELECT account_id, mail_id, thread_id, mailbox_id FROM mail_tbl ORDER BY date_time) WHERE account_id = %d AND thread_id = %d AND mailbox_id = %d", account_id, thread_id, mailbox_id);
15192
15193                 /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
15194                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
15195                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15196                                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15197                 EM_DEBUG_LOG("result_count[%d]", result_count);
15198                 if (result_count == 0) {
15199                         EM_DEBUG_LOG("No mail found...");
15200                         ret = false;
15201                         if (err_code)
15202                                 *err_code =  EMAIL_ERROR_MAIL_NOT_FOUND;
15203                         sqlite3_free_table(result);
15204                         return false;
15205                 }
15206
15207                 _get_table_field_data_int(result, &latest_mail_id, 2);
15208                 _get_table_field_data_int(result, &thread_item_count, 3);
15209
15210                 EM_DEBUG_LOG("latest_mail_id[%d]", latest_mail_id);
15211                 EM_DEBUG_LOG("thread_item_count[%d]", thread_item_count);
15212
15213                 sqlite3_free_table(result);
15214         }
15215
15216         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15217
15218         if (thread_item_count < 0) {
15219                 memset(sql_query_string, 0, QUERY_SIZE);
15220                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = 0 WHERE account_id = %d AND thread_id = %d", account_id, thread_id);
15221                 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15222                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15223                 if (err != EMAIL_ERROR_NONE) {
15224                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15225                         goto FINISH_OFF;
15226                 }
15227         } else if (thread_id != latest_mail_id) {
15228                 /* Initialize the thread id */
15229                 memset(sql_query_string, 0, QUERY_SIZE);
15230                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = 0, thread_id = %d WHERE account_id = %d AND mailbox_id = %d AND thread_id = %d", latest_mail_id, account_id, mailbox_id, thread_id);
15231                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15232                 if (err != EMAIL_ERROR_NONE) {
15233                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15234                         goto FINISH_OFF;
15235                 }
15236
15237                 /* update the thread item count */
15238                 memset(sql_query_string, 0, QUERY_SIZE);
15239                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = %d WHERE account_id = %d AND mail_id = %d ", thread_item_count, account_id, latest_mail_id);
15240                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15241                 if (err != EMAIL_ERROR_NONE) {
15242                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15243                         goto FINISH_OFF;
15244                 }
15245         } else {
15246                 memset(sql_query_string, 0, QUERY_SIZE);
15247                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = %d WHERE account_id = %d AND mail_id = %d ", thread_item_count, account_id, latest_mail_id);
15248                 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15249                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15250                 if (err != EMAIL_ERROR_NONE) {
15251                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15252                         goto FINISH_OFF;
15253                 }
15254         }
15255         ret = true;
15256
15257 FINISH_OFF:
15258         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15259
15260         if (thread_id != latest_mail_id) {
15261                 if (err == EMAIL_ERROR_NONE) {
15262                         EM_DEBUG_LOG("noti_type[%d]", noti_type);
15263
15264                         if (latest_mail_id > 0 && thread_id > 0 && noti_type > 0) {
15265                                 char mailbox_id_str[25] = {0,};
15266                                 snprintf(mailbox_id_str, sizeof(mailbox_id_str), "%d", mailbox_id);
15267                                 if (!emcore_notify_storage_event(noti_type, thread_id, latest_mail_id, mailbox_id_str, account_id))
15268                                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventfailed [NOTI_THREAD_ID_CHANGED] >>>> ");
15269
15270                                 if (updated_thread_id) *updated_thread_id = latest_mail_id;
15271                         }
15272                 }
15273         } else if (thread_item_count >= 0) {
15274                 if (err == EMAIL_ERROR_NONE) {
15275                         char parameter_string[500] = {0,};
15276                         SNPRINTF(parameter_string, sizeof(parameter_string), "%s%c%d", "thread_item_count", 0x01, latest_mail_id);
15277                         if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT, parameter_string, thread_item_count))
15278                                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventfailed [NOTI_MAIL_FIELD_UPDATE] >>>> ");
15279                 }
15280         }
15281
15282         if (err_code != NULL)
15283                 *err_code = err;
15284
15285         EM_DEBUG_FUNC_END("ret [%d]", ret);
15286         return ret;
15287 }
15288
15289 INTERNAL_FUNC int emstorage_update_thread_id_of_mail(char *multi_user_name, int account_id, int mailbox_id, int mail_id, int thread_id, int thread_item_count, int transaction, int *err_code)
15290 {
15291         EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id [%d], mail_id[%d], thread_id[%d], thread_item_count[%d], err_code[%p]", account_id, mailbox_id, mail_id, thread_id, thread_item_count, err_code);
15292
15293         int ret = false;
15294         int err = EMAIL_ERROR_NONE;
15295         char sql_query_string[QUERY_SIZE] = {0, };
15296
15297         if (thread_id == 0) {
15298                 EM_DEBUG_EXCEPTION("Invalid Parameter");
15299                 if (err_code)
15300                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15301                 return false;
15302         }
15303
15304         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15305
15306         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15307
15308         memset(sql_query_string, 0, QUERY_SIZE);
15309         SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = %d, thread_id = %d WHERE account_id = %d AND mail_id = %d", thread_item_count, thread_id, account_id, mail_id);
15310         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15311         if (err != EMAIL_ERROR_NONE) {
15312                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15313                 goto FINISH_OFF;
15314         }
15315
15316         ret = true;
15317
15318 FINISH_OFF:
15319         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15320
15321         if (err_code != NULL)
15322                 *err_code = err;
15323
15324         EM_DEBUG_FUNC_END("ret [%d]", ret);
15325         return ret;
15326 }
15327
15328 #ifdef __FEATURE_LOCAL_ACTIVITY__
15329 /**
15330  * emstorage_add_activity - Add Email Local activity during OFFLINE mode
15331  *
15332  */
15333 INTERNAL_FUNC int emstorage_add_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15334 {
15335         EM_DEBUG_FUNC_BEGIN();
15336
15337         EM_DEBUG_LOG(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15338
15339         int rc = -1, ret = false;
15340         int error = EMAIL_ERROR_NONE;
15341         DB_STMT hStmt = NULL;
15342         char sql_query_string[8192] = { 0x00, };
15343         int i = 0;
15344
15345         if (!local_activity) {
15346                 EM_DEBUG_EXCEPTION(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15347                 if (err_code != NULL)
15348                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15349                 return false;
15350         }
15351
15352         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15353
15354         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15355
15356         memset(sql_query_string, 0x00 , sizeof(sql_query_string));
15357         SNPRINTF(sql_query_string, sizeof(sql_query_string), "INSERT INTO mail_local_activity_tbl VALUES (?, ?, ?, ?, ?, ?, ?)");
15358
15359         EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ] ", local_activity->activity_id);
15360         EM_DEBUG_LOG(">>>>> MAIL ID [ %d ] ", local_activity->mail_id);
15361         EM_DEBUG_LOG(">>>>> ACCOUNT ID [ %d ] ", local_activity->account_id);
15362         EM_DEBUG_LOG(">>>>> ACTIVITY TYPE [ %d ] ", local_activity->activity_type);
15363         EM_DEBUG_LOG_SEC(">>>>> SERVER MAIL ID [ %s ] ", local_activity->server_mailid);
15364         EM_DEBUG_LOG(">>>>> SOURCE MAILBOX [ %s ] ", local_activity->src_mbox);
15365         EM_DEBUG_LOG(">>>>> DEST MAILBOX   [ %s ] ", local_activity->dest_mbox);
15366
15367         EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15368
15369
15370         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15371         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15372                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15373
15374         EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15375
15376         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
15377         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
15378         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
15379         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
15380         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->server_mailid, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
15381         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->src_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15382         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->dest_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15383
15384
15385         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15386
15387         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
15388                         ("sqlite3_step fail:%d", rc));
15389         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15390                         ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
15391
15392         ret = true;
15393
15394 FINISH_OFF:
15395         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15396
15397         if (hStmt != NULL) {
15398                 rc = sqlite3_finalize(hStmt);
15399                 if (rc != SQLITE_OK) {
15400                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15401                         error = EMAIL_ERROR_DB_FAILURE;
15402                 }
15403         }
15404
15405         if (err_code != NULL)
15406                 *err_code = error;
15407
15408         EM_DEBUG_FUNC_END("ret [%d]", ret);
15409         return ret;
15410 }
15411
15412 /**
15413  *      emstorage_get_activity - Get the Local activity Information
15414  *
15415  *
15416  */
15417 INTERNAL_FUNC int emstorage_get_activity(int account_id, int activityid, emstorage_activity_tbl_t** activity_list, int *select_num, int transaction, int *err_code)
15418 {
15419         EM_DEBUG_FUNC_BEGIN();
15420
15421         int i = 0, count = 0, rc = -1, ret = false;
15422         int error = EMAIL_ERROR_NONE;
15423         emstorage_activity_tbl_t *p_activity_tbl = NULL;
15424         char sql_query_string[1024] = {0x00, };
15425         char **result = NULL;
15426         int col_index ;
15427
15428         EM_IF_NULL_RETURN_VALUE(activity_list, false);
15429         EM_IF_NULL_RETURN_VALUE(select_num, false);
15430
15431
15432         if (!select_num || !activity_list || account_id <= 0 || activityid < 0) {
15433                 EM_DEBUG_LOG(" select_num[%p], activity_list[%p] account_id [%d] activityid [%d] ", select_num, activity_list, account_id, activityid);
15434                 if (err_code)
15435                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15436                 return false;
15437         }
15438
15439         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15440
15441         EMSTORAGE_START_READ_TRANSACTION(transaction);
15442
15443         memset(sql_query_string, 0x00, sizeof(sql_query_string));
15444
15445         if (activityid == ALL_ACTIVITIES) {
15446                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
15447                         "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d order by activity_id", account_id);
15448         } else {
15449                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
15450                         "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d AND activity_id = %d ", account_id, activityid);
15451         }
15452
15453         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
15454
15455
15456
15457         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15458         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15459                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15460
15461         col_index = 7;
15462
15463         if (!(p_activity_tbl = (emstorage_activity_tbl_t*)em_malloc(sizeof(emstorage_activity_tbl_t) * count))) {
15464                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
15465                 error = EMAIL_ERROR_OUT_OF_MEMORY;
15466                 goto FINISH_OFF;
15467         }
15468
15469
15470         for (i = 0; i < count; i++) {
15471                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15472                 if (result[col_index])
15473                         p_activity_tbl[i].activity_id = atoi(result[col_index++]);
15474
15475                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15476                 if (result[col_index])
15477                         p_activity_tbl[i].account_id = atoi(result[col_index++]);
15478
15479                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15480                 if (result[col_index])
15481                         p_activity_tbl[i].mail_id = atoi(result[col_index++]);
15482
15483                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15484                 if (result[col_index])
15485                         p_activity_tbl[i].activity_type = atoi(result[col_index++]);
15486
15487
15488                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15489                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15490                         p_activity_tbl[i].server_mailid = EM_SAFE_STRDUP(result[col_index++]);
15491                 else
15492                         col_index++;
15493
15494                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15495                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15496                         p_activity_tbl[i].src_mbox = EM_SAFE_STRDUP(result[col_index++]);
15497                 else
15498                         col_index++;
15499
15500                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15501                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15502                         p_activity_tbl[i].dest_mbox = EM_SAFE_STRDUP(result[col_index++]);
15503                 else
15504                         col_index++;
15505
15506         }
15507
15508         if (result)
15509                 sqlite3_free_table(result);
15510
15511         ret = true;
15512
15513 FINISH_OFF:
15514
15515         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15516
15517         if (ret == true) {
15518                 *activity_list = p_activity_tbl;
15519                 *select_num = count;
15520                 EM_DEBUG_LOG(">>>> COUNT : %d >> ", count);
15521         } else if (p_activity_tbl != NULL) {
15522                 emstorage_free_local_activity(&p_activity_tbl, count, NULL);
15523         }
15524
15525
15526         if (err_code != NULL)
15527                 *err_code = error;
15528
15529         EM_DEBUG_FUNC_END("ret [%d]", ret);
15530         return ret;
15531 }
15532
15533
15534 INTERNAL_FUNC int emstorage_get_next_activity_id(int *activity_id, int *err_code)
15535 {
15536
15537         EM_DEBUG_FUNC_BEGIN();
15538
15539         int ret = false;
15540         int err = EMAIL_ERROR_NONE;
15541         int rc = -1;
15542         char *sql = NULL;
15543         char **result = NULL;
15544
15545         if (NULL == activity_id) {
15546                 EM_DEBUG_EXCEPTION(" activity_id[%p]", activity_id);
15547                 if (err_code)
15548                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15549                 return false;
15550         }
15551
15552         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15553
15554         /*  increase unique id */
15555
15556         sql = "SELECT max(rowid) FROM mail_local_activity_tbl;";
15557
15558         /*  rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); n EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc); */
15559         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15560                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
15561
15562         if (NULL == result[1])
15563                 rc = 1;
15564         else
15565                 rc = atoi(result[1])+1;
15566
15567         *activity_id = rc;
15568
15569         if (result)
15570                 sqlite3_free_table(result);
15571
15572         ret = true;
15573
15574 FINISH_OFF:
15575
15576         if (err_code)
15577                 *err_code = err;
15578
15579         EM_DEBUG_FUNC_END("ret [%d]", ret);
15580         return ret;
15581
15582 }
15583
15584 INTERNAL_FUNC int emstorage_get_activity_id_list(char *multi_user_name, int account_id, int ** activity_id_list, int *activity_id_count, int lowest_activity_type, int highest_activity_type, int transaction, int *err_code)
15585 {
15586
15587         EM_DEBUG_FUNC_BEGIN();
15588
15589         EM_DEBUG_LOG(" account_id[%d], activity_id_list[%p], activity_id_count[%p] err_code[%p]", account_id,  activity_id_list, activity_id_count, err_code);
15590
15591         if (account_id <= 0 || NULL == activity_id_list || NULL == activity_id_count || lowest_activity_type <= 0 || highest_activity_type <= 0) {
15592                 if (err_code != NULL)
15593                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15594                 return false;
15595         }
15596
15597         int ret = false;
15598         int error = EMAIL_ERROR_NONE;
15599         int i = 0, rc = -1, count = 0;
15600         char sql_query_string[1024] = {0x00, };
15601         int *activity_ids = NULL;
15602         int col_index = 0;
15603         char **result = NULL;
15604         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15605         EMSTORAGE_START_READ_TRANSACTION(transaction);
15606
15607         memset(sql_query_string, 0x00, sizeof(sql_query_string));
15608
15609         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct activity_id FROM mail_local_activity_tbl WHERE account_id = %d AND activity_type >= %d AND activity_type <= %d order by activity_id", account_id, lowest_activity_type, highest_activity_type);
15610
15611         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
15612
15613
15614         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15615         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15616                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15617
15618         col_index = 1;
15619
15620         EM_DEBUG_LOG(" Activity COUNT : %d ... ", count);
15621
15622         if (NULL == (activity_ids = (int *)em_malloc(sizeof(int) * count))) {
15623                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
15624                 error = EMAIL_ERROR_OUT_OF_MEMORY;
15625                 goto FINISH_OFF;
15626         }
15627
15628         for (i = 0; i < count; i++) {
15629                 activity_ids[i] = atoi(result[col_index]);
15630                 col_index++;
15631                 EM_DEBUG_LOG("activity_id %d", activity_ids[i]);
15632         }
15633
15634         ret = true;
15635
15636 FINISH_OFF:
15637
15638
15639         if (ret == true) {
15640                 *activity_id_count = count;
15641                 *activity_id_list = activity_ids;
15642         } else if (activity_ids != NULL) /* Prevent defect - 216566 */
15643                 EM_SAFE_FREE(activity_ids);
15644
15645
15646         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15647         if (err_code != NULL)
15648                 *err_code = error;
15649
15650         EM_DEBUG_FUNC_END("ret [%d]", ret);
15651         return ret;
15652 }
15653
15654 INTERNAL_FUNC int emstorage_free_activity_id_list(int *activity_id_list, int *error_code)
15655 {
15656         EM_DEBUG_FUNC_BEGIN();
15657
15658         int error = EMAIL_ERROR_NONE;
15659         int ret = false;
15660
15661         EM_DEBUG_LOG(" activity_id_list [%p]", activity_id_list);
15662
15663         if (NULL == activity_id_list) {
15664                 error = EMAIL_ERROR_INVALID_PARAM;
15665                 goto FINISH_OFF;
15666         } else {
15667                 EM_SAFE_FREE(activity_id_list);
15668         }
15669
15670
15671         ret = true;
15672
15673 FINISH_OFF:
15674
15675         if (NULL != error_code)
15676                 *error_code = error;
15677
15678         EM_DEBUG_FUNC_END("ret [%d]", ret);
15679         return ret;
15680 }
15681
15682 /**
15683  * emstorage_delete_local_activity - Deletes the Local acitivity Generated based on activity_type
15684  * or based on server mail id
15685  *
15686  */
15687 INTERNAL_FUNC int emstorage_delete_local_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15688 {
15689         EM_DEBUG_FUNC_BEGIN();
15690
15691
15692         EM_DEBUG_LOG(" local_activity[%p] ", local_activity);
15693
15694         if (!local_activity) {
15695                 EM_DEBUG_EXCEPTION(" local_activity[%p] ", local_activity);
15696                 if (err_code != NULL)
15697                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15698                 return false;
15699         }
15700
15701         int rc = -1, ret = false;                       /* Prevent_FIX  */
15702         int err = EMAIL_ERROR_NONE;
15703         int query_and = 0;
15704         int query_where = 0;
15705         char sql_query_string[8192] = { 0x00, };
15706         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15707         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15708
15709         memset(sql_query_string, 0x00, sizeof(sql_query_string));
15710
15711         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_local_activity_tbl ");
15712
15713         EM_DEBUG_LOG_SEC(">>> Query [ %s ] ", sql_query_string);
15714
15715         if (local_activity->account_id) {
15716                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15717                                 " WHERE account_id = %d ", local_activity->account_id);
15718                 query_and = 1;
15719                 query_where = 1;
15720         }
15721
15722         if (local_activity->server_mailid) {
15723                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15724                                 " %s %s server_mailid = '%s' ", query_where ? "" : "WHERE", query_and ? "AND" : "", local_activity->server_mailid);
15725                 query_and = 1;
15726                 query_where = 1;
15727         }
15728
15729
15730         if (local_activity->mail_id) {
15731                 EM_DEBUG_LOG(">>>> MAIL ID [ %d ] , ACTIVITY TYPE [%d ]", local_activity->mail_id, local_activity->activity_type);
15732
15733                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15734                                 " %s %s mail_id = %d  ", query_where ? "" : "WHERE", query_and ? "AND" : "", local_activity->mail_id);
15735
15736                 query_and = 1;
15737                 query_where = 1;
15738
15739         }
15740
15741         if (local_activity->activity_type > 0) {
15742                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15743                                 " %s %s activity_type = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "" , local_activity->activity_type);
15744         }
15745
15746         if (local_activity->activity_id > 0) {
15747                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15748                                 " %s %s activity_id = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "" , local_activity->activity_id);
15749
15750         }
15751
15752         EM_DEBUG_LOG_SEC(">>>>> Query [ %s ] ", sql_query_string);
15753         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15754         if (error != EMAIL_ERROR_NONE) {
15755                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
15756                 goto FINISH_OFF;
15757         }
15758
15759         rc = sqlite3_changes(local_db_handle);
15760         if (rc == 0) {
15761                 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
15762                 err = EMAIL_ERROR_MAILBOX_NOT_FOUND;
15763         }
15764
15765         ret = true;
15766
15767 FINISH_OFF:
15768         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15769
15770         if (hStmt != NULL) {
15771                 rc = sqlite3_finalize(hStmt);
15772                 if (rc != SQLITE_OK) {
15773                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15774                         error = EMAIL_ERROR_DB_FAILURE;
15775                 }
15776         }
15777
15778         if (err_code != NULL)
15779                 *err_code = err;
15780         EM_DEBUG_FUNC_END("ret [%d]", ret);
15781         return ret;
15782 }
15783
15784 /**
15785  *      emstorage_free_local_activity - Free the Local Activity data
15786  */
15787 INTERNAL_FUNC int emstorage_free_local_activity(emstorage_activity_tbl_t **local_activity_list, int count, int *err_code)
15788 {
15789         EM_DEBUG_FUNC_BEGIN();
15790
15791         EM_DEBUG_LOG(" local_activity_list[%p], count[%d], err_code[%p]", local_activity_list, count, err_code);
15792
15793         int ret = false;
15794         int error = EMAIL_ERROR_INVALID_PARAM;
15795
15796         if (count > 0) {
15797                 if (!local_activity_list || !*local_activity_list) {
15798                         EM_DEBUG_EXCEPTION(" local_activity_list[%p], count[%d]", local_activity_list, count);
15799
15800                         error = EMAIL_ERROR_INVALID_PARAM;
15801                         goto FINISH_OFF;
15802                 }
15803
15804                 emstorage_activity_tbl_t* p = *local_activity_list;
15805                 int i = 0;
15806                 if (p) {
15807                         for (; i < count; i++) {
15808                                 EM_SAFE_FREE(p[i].dest_mbox);
15809                                 EM_SAFE_FREE(p[i].src_mbox);
15810                                 EM_SAFE_FREE(p[i].server_mailid);
15811                         }
15812
15813                         EM_SAFE_FREE(p);
15814                         *local_activity_list = NULL;
15815                 }
15816         }
15817
15818         ret = true;
15819
15820 FINISH_OFF:
15821
15822         if (err_code != NULL)
15823                 *err_code = error;
15824
15825         EM_DEBUG_FUNC_END("ret [%d]", ret);
15826         return ret;
15827
15828 }
15829 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
15830
15831
15832 static int _get_key_value_string_for_list_filter_rule(email_list_filter_rule_t *input_list_filter_rule, char **output_key_value_string)
15833 {
15834         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_key_value_string [%p]", input_list_filter_rule, output_key_value_string);
15835
15836         int  ret = EMAIL_ERROR_NONE;
15837         char key_value_string[QUERY_SIZE] = { 0, };
15838         char *temp_key_value_1 = NULL;
15839         char *temp_key_value_2 = NULL;
15840
15841         if (input_list_filter_rule == NULL || output_key_value_string == NULL) {
15842                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15843                 return EMAIL_ERROR_INVALID_PARAM;
15844         }
15845
15846         switch (input_list_filter_rule->target_attribute) {
15847         case EMAIL_MAIL_ATTRIBUTE_MAIL_ID:
15848         case EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID:
15849         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID:
15850         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE:
15851         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_STATUS:
15852         case EMAIL_MAIL_ATTRIBUTE_REFERENCE_MAIL_ID:
15853         case EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS:
15854         case EMAIL_MAIL_ATTRIBUTE_MAIL_SIZE:
15855         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN:
15856         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML:
15857         case EMAIL_MAIL_ATTRIBUTE_FILE_SIZE:
15858         case EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD:
15859         case EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD:
15860         case EMAIL_MAIL_ATTRIBUTE_FLAGS_FLAGGED_FIELD:
15861         case EMAIL_MAIL_ATTRIBUTE_FLAGS_ANSWERED_FIELD:
15862         case EMAIL_MAIL_ATTRIBUTE_FLAGS_RECENT_FIELD:
15863         case EMAIL_MAIL_ATTRIBUTE_FLAGS_DRAFT_FIELD:
15864         case EMAIL_MAIL_ATTRIBUTE_FLAGS_FORWARDED_FIELD:
15865         case EMAIL_MAIL_ATTRIBUTE_DRM_STATUS:
15866         case EMAIL_MAIL_ATTRIBUTE_PRIORITY:
15867         case EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS:
15868         case EMAIL_MAIL_ATTRIBUTE_LOCK_STATUS:
15869         case EMAIL_MAIL_ATTRIBUTE_REPORT_STATUS:
15870         case EMAIL_MAIL_ATTRIBUTE_ATTACHMENT_COUNT:
15871         case EMAIL_MAIL_ATTRIBUTE_INLINE_CONTENT_COUNT:
15872         case EMAIL_MAIL_ATTRIBUTE_THREAD_ID:
15873         case EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT:
15874         case EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS:
15875         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_CLASS:
15876         case EMAIL_MAIL_ATTRIBUTE_DIGEST_TYPE:
15877         case EMAIL_MAIL_ATTRIBUTE_SMIME_TYPE:
15878         case EMAIL_MAIL_ATTRIBUTE_REMAINING_RESEND_TIMES:
15879         case EMAIL_MAIL_ATTRIBUTE_TAG_ID:
15880         case EMAIL_MAIL_ATTRIBUTE_EAS_DATA_LENGTH_TYPE:
15881                 SNPRINTF(key_value_string, QUERY_SIZE, "%d", input_list_filter_rule->key_value.integer_type_value);
15882                 break;
15883
15884         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
15885         case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
15886         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
15887         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
15888         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
15889         case EMAIL_MAIL_ATTRIBUTE_FROM:
15890         case EMAIL_MAIL_ATTRIBUTE_TO:
15891         case EMAIL_MAIL_ATTRIBUTE_CC:
15892         case EMAIL_MAIL_ATTRIBUTE_BCC:
15893         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
15894                 if (input_list_filter_rule->key_value.string_type_value == NULL) {
15895                         EM_DEBUG_EXCEPTION("Invalid string_type_value [%p]", input_list_filter_rule->key_value.string_type_value);
15896                         ret = EMAIL_ERROR_INVALID_PARAM;
15897                         goto FINISH_OFF;
15898                 }
15899
15900                 temp_key_value_1 = input_list_filter_rule->key_value.string_type_value;
15901
15902                 temp_key_value_2 = em_replace_all_string(temp_key_value_1, "_", "\\_");
15903                 temp_key_value_1 = em_replace_all_string(temp_key_value_2, "%", "\\%");
15904
15905                 if (input_list_filter_rule->rule_type == EMAIL_LIST_FILTER_RULE_INCLUDE)
15906                         SNPRINTF(key_value_string, QUERY_SIZE, "\'%%%s%%\'", temp_key_value_1);
15907                 else
15908                         SNPRINTF(key_value_string, QUERY_SIZE, "\'%s\'", temp_key_value_1);
15909                 break;
15910
15911         case EMAIL_MAIL_ATTRIBUTE_DATE_TIME:
15912         case EMAIL_MAIL_ATTRIBUTE_SCHEDULED_SENDING_TIME:
15913         case EMAIL_MAIL_ATTRIBUTE_REPLIED_TIME:
15914         case EMAIL_MAIL_ATTRIBUTE_FORWARDED_TIME:
15915                 SNPRINTF(key_value_string, QUERY_SIZE, "%d", (int)input_list_filter_rule->key_value.datetime_type_value);
15916                 break;
15917
15918         default:
15919                 ret = EMAIL_ERROR_INVALID_PARAM;
15920                 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
15921                 break;
15922         }
15923
15924         if (ret == EMAIL_ERROR_NONE && EM_SAFE_STRLEN(key_value_string) > 0)
15925                 *output_key_value_string = strdup(key_value_string);
15926
15927 FINISH_OFF:
15928
15929         EM_SAFE_FREE(temp_key_value_1);
15930         EM_SAFE_FREE(temp_key_value_2);
15931
15932         EM_DEBUG_FUNC_END("ret [%d]", ret);
15933         return ret;
15934 }
15935
15936 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15937 static int _get_cert_password_file_name(int index, char *cert_password_file_name)
15938 {
15939         EM_DEBUG_FUNC_BEGIN("index : [%d]", index);
15940
15941         if (index <= 0 || !cert_password_file_name) {
15942                 EM_DEBUG_EXCEPTION("Invalid parameter");
15943                 return EMAIL_ERROR_INVALID_PARAM;
15944         }
15945
15946         sprintf(cert_password_file_name, ".email_cert_%d", index);
15947
15948         EM_DEBUG_FUNC_END();
15949         return EMAIL_ERROR_NONE;
15950 }
15951 #endif
15952
15953 static int _make_filter_rule_string(email_list_filter_rule_t *input_list_filter_rule, char **output_string)
15954 {
15955         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
15956
15957         int   ret = EMAIL_ERROR_NONE;
15958         int   is_alpha = 0;
15959         int   length_field_name = 0;
15960         int   length_value = 0;
15961         char  result_rule_string[QUERY_SIZE] = { 0 , };
15962         char *mod_field_name_string = NULL;
15963         char *mod_value_string = NULL;
15964         char *temp_field_name_string = NULL;
15965         char *temp_key_value_string = NULL;
15966
15967         if (input_list_filter_rule == NULL || output_string == NULL) {
15968                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15969                 return  EMAIL_ERROR_INVALID_PARAM;
15970         }
15971
15972         temp_field_name_string = emcore_get_mail_field_name_by_attribute_type(input_list_filter_rule->target_attribute);
15973
15974         if (temp_field_name_string == NULL) {
15975                 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
15976                 return EMAIL_ERROR_INVALID_PARAM;
15977         }
15978
15979         if (_get_key_value_string_for_list_filter_rule(input_list_filter_rule, &temp_key_value_string) != EMAIL_ERROR_NONE || temp_key_value_string == NULL) {
15980                 EM_DEBUG_EXCEPTION("_get_key_value_string_for_list_filter_rule failed");
15981                 return EMAIL_ERROR_INVALID_PARAM;
15982         }
15983
15984         length_field_name = EM_SAFE_STRLEN(temp_field_name_string);
15985         length_value      = EM_SAFE_STRLEN(temp_key_value_string);
15986
15987         switch (input_list_filter_rule->target_attribute) {
15988         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
15989         case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
15990         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
15991         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
15992         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
15993         case EMAIL_MAIL_ATTRIBUTE_FROM:
15994         case EMAIL_MAIL_ATTRIBUTE_TO:
15995         case EMAIL_MAIL_ATTRIBUTE_CC:
15996         case EMAIL_MAIL_ATTRIBUTE_BCC:
15997         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
15998                 is_alpha = 1;
15999                 break;
16000         default:
16001                 is_alpha = 0;
16002                 break;
16003         }
16004
16005         if (is_alpha == 1 && input_list_filter_rule->case_sensitivity == false) {
16006                 length_field_name += strlen("UPPER() ");
16007                 length_value      += strlen("UPPER() ");
16008                 mod_field_name_string = em_malloc(sizeof(char) * length_field_name);
16009                 if (mod_field_name_string == NULL) {
16010                         EM_DEBUG_EXCEPTION("em_mallocfailed");
16011                         EM_SAFE_FREE(temp_field_name_string);
16012                         ret = EMAIL_ERROR_OUT_OF_MEMORY;
16013                         goto FINISH_OFF;
16014                 }
16015
16016                 mod_value_string = em_malloc(sizeof(char) * length_value);
16017                 if (mod_value_string == NULL) {
16018                         EM_DEBUG_EXCEPTION("em_mallocfailed");
16019                         EM_SAFE_FREE(temp_field_name_string);
16020                         ret = EMAIL_ERROR_OUT_OF_MEMORY;
16021                         goto FINISH_OFF;
16022                 }
16023
16024                 SNPRINTF(mod_field_name_string, length_field_name, "UPPER(%s)", temp_field_name_string);
16025                 SNPRINTF(mod_value_string,      length_value, "UPPER(%s)", temp_key_value_string);
16026                 EM_SAFE_FREE(temp_key_value_string);
16027         } else {
16028                 mod_field_name_string = strdup(temp_field_name_string);
16029                 mod_value_string      = temp_key_value_string;
16030         }
16031
16032         switch (input_list_filter_rule->rule_type) {
16033         case EMAIL_LIST_FILTER_RULE_EQUAL:
16034                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = %s ", mod_field_name_string, mod_value_string);
16035                 break;
16036         case EMAIL_LIST_FILTER_RULE_NOT_EQUAL:
16037                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s != %s ", mod_field_name_string, mod_value_string);
16038                 break;
16039         case EMAIL_LIST_FILTER_RULE_LESS_THAN:
16040                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s < %s ", mod_field_name_string, mod_value_string);
16041                 break;
16042         case EMAIL_LIST_FILTER_RULE_GREATER_THAN:
16043                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s > %s ", mod_field_name_string, mod_value_string);
16044                 break;
16045         case EMAIL_LIST_FILTER_RULE_LESS_THAN_OR_EQUAL:
16046                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s <= %s ", mod_field_name_string, mod_value_string);
16047                 break;
16048         case EMAIL_LIST_FILTER_RULE_GREATER_THAN_OR_EQUAL:
16049                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s >= %s ", mod_field_name_string, mod_value_string);
16050                 break;
16051         case EMAIL_LIST_FILTER_RULE_INCLUDE:
16052                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s LIKE %s ", mod_field_name_string, mod_value_string);
16053                 break;
16054         case EMAIL_LIST_FILTER_RULE_IN:
16055                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s IN (%s) ", mod_field_name_string, mod_value_string);
16056                 break;
16057         case EMAIL_LIST_FILTER_RULE_NOT_IN:
16058                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s NOT IN (%s) ", mod_field_name_string, mod_value_string);
16059                 break;
16060         default:
16061                 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16062                 ret = EMAIL_ERROR_INVALID_PARAM;
16063                 goto FINISH_OFF;
16064         }
16065
16066         *output_string = strdup(result_rule_string);
16067
16068 FINISH_OFF:
16069         EM_SAFE_FREE(mod_field_name_string);
16070         EM_SAFE_FREE(mod_value_string);
16071
16072         EM_DEBUG_FUNC_END("ret [%d]", ret);
16073         return ret;
16074 }
16075
16076 static int _make_filter_attach_rule_string(char *multi_user_name, email_list_filter_rule_attach_t *input_list_filter_rule, char **output_string)
16077 {
16078         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16079
16080         char *field_name_string = NULL;
16081         char  key_value_string[QUERY_SIZE] = {0,};
16082         char  result_rule_string[QUERY_SIZE] = {0,};
16083         int rc = -1;
16084         int count = 0;
16085         int query_size = 0;
16086         int cur_query = 0;
16087         int col_index = 0;
16088         int error = EMAIL_ERROR_NONE;
16089         char **result = NULL;
16090         char sql_query_string[QUERY_SIZE] = {0,};
16091         char *sql_query_string2 = NULL;
16092         sqlite3 *local_db_handle = NULL;
16093         int *mail_ids = NULL;
16094
16095         if (input_list_filter_rule == NULL || output_string == NULL) {
16096                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16097                 return  EMAIL_ERROR_INVALID_PARAM;
16098         }
16099
16100         field_name_string = EM_SAFE_STRDUP("attachment_name");
16101         SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
16102
16103         switch (input_list_filter_rule->rule_type) {
16104
16105         case EMAIL_LIST_FILTER_RULE_INCLUDE:
16106                 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
16107                 break;
16108
16109         case EMAIL_LIST_FILTER_RULE_MATCH:
16110                 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
16111                 break;
16112
16113         default:
16114                 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16115                 error = EMAIL_ERROR_INVALID_PARAM;
16116                 goto FINISH_OFF;
16117         }
16118
16119         local_db_handle = emstorage_get_db_connection(multi_user_name);
16120
16121         EMSTORAGE_START_READ_TRANSACTION(true);
16122         SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_attachment_tbl %s", result_rule_string);
16123
16124         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16125         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16126                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16127
16128         col_index = 1;
16129
16130         if (!count) {
16131                 EM_DEBUG_LOG("No mail found...");
16132                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
16133                 *output_string = strdup("mail_id IN () ");
16134                 goto FINISH_OFF;
16135         }
16136
16137         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
16138         int i = 0;
16139         if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
16140                 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
16141                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16142                 goto FINISH_OFF;
16143         }
16144
16145         for (i = 0; i < count; i++) {
16146                 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16147                 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16148         }
16149
16150         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >>", count);
16151         sqlite3_free_table(result);
16152         EMSTORAGE_FINISH_READ_TRANSACTION(true);
16153
16154         //      sqlite3_db_release_memory(local_db_handle);
16155
16156
16157         query_size = (10 * count) + strlen("mail_id IN ()  ");
16158
16159         sql_query_string2 = em_malloc(query_size);
16160         if (sql_query_string2 == NULL) {
16161                 EM_DEBUG_EXCEPTION("em_mallocfailed");
16162                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16163                 goto FINISH_OFF;
16164         }
16165
16166         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN (");
16167         for (i = 0; i < count-1; i++)
16168                 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16169
16170         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d) ", mail_ids[count-1]);
16171
16172         *output_string = strdup(sql_query_string2);
16173 FINISH_OFF:
16174
16175         EM_SAFE_FREE(mail_ids); /* prevent */
16176         EM_SAFE_FREE(sql_query_string2);
16177         EM_SAFE_FREE(field_name_string);
16178         EM_DEBUG_FUNC_END("error [%d]", error);
16179         return error;
16180 }
16181
16182 static int _make_filter_fts_rule_string(char *multi_user_name, email_list_filter_rule_fts_t *input_list_filter_rule, char **output_string)
16183 {
16184         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16185         char *field_name_string = NULL;
16186         char key_value_string[QUERY_SIZE] = {0,};
16187         char  result_rule_string[QUERY_SIZE] = {0,};
16188         int rc = -1;
16189         int count = 0;
16190         int col_index = 0;
16191         int query_size = 0;
16192         int error = EMAIL_ERROR_NONE;
16193         char **result = NULL;
16194         char sql_query_string[QUERY_SIZE] = {0,};
16195         char *sql_query_string2 = NULL;
16196         sqlite3 *local_db_handle = NULL;
16197         int *mail_ids = NULL;
16198
16199         if (input_list_filter_rule == NULL || output_string == NULL) {
16200                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16201                 return  EMAIL_ERROR_INVALID_PARAM;
16202         }
16203
16204         field_name_string = EM_SAFE_STRDUP("body_text");
16205         SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
16206
16207         switch (input_list_filter_rule->rule_type) {
16208
16209         case EMAIL_LIST_FILTER_RULE_INCLUDE:
16210                 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
16211                 break;
16212
16213         case EMAIL_LIST_FILTER_RULE_MATCH:
16214                 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
16215                 break;
16216
16217         default:
16218                 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16219                 error = EMAIL_ERROR_INVALID_PARAM;
16220                 goto FINISH_OFF;
16221         }
16222
16223         local_db_handle = emstorage_get_db_connection(multi_user_name);
16224
16225         EMSTORAGE_START_READ_TRANSACTION(true);
16226         SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_text_tbl %s", result_rule_string);
16227
16228         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16229         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16230                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16231
16232         col_index = 1;
16233
16234         if (!count) {
16235                 EM_DEBUG_LOG("No mail found...");
16236                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
16237                 *output_string = strdup("mail_id IN () ");
16238                 goto FINISH_OFF;
16239         }
16240
16241         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
16242         int i = 0;
16243
16244         if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
16245                 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
16246                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16247                 goto FINISH_OFF;
16248         }
16249
16250         for (i = 0; i < count; i++) {
16251                 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16252                 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16253         }
16254
16255         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >>", count);
16256         sqlite3_free_table(result);
16257         EMSTORAGE_FINISH_READ_TRANSACTION(true);
16258
16259         //      sqlite3_db_release_memory(local_db_handle);
16260
16261
16262         query_size = (10 * count) + strlen("mail_id IN ()  ");
16263         sql_query_string2 = em_malloc(query_size);
16264         if (sql_query_string2 == NULL) {
16265                 EM_DEBUG_EXCEPTION("em_mallocfailed");
16266                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16267                 goto FINISH_OFF;
16268         }
16269         int cur_query = 0;
16270         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN (");
16271         for (i = 0; i < count-1; i++)
16272                 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16273
16274         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d) ", mail_ids[count-1]);
16275
16276         *output_string = strdup(sql_query_string2);
16277
16278 FINISH_OFF:
16279         EM_SAFE_FREE(mail_ids); /* prevent */
16280         EM_SAFE_FREE(sql_query_string2);
16281         EM_SAFE_FREE(field_name_string);
16282         EM_DEBUG_FUNC_END("error [%d]", error);
16283         return error;
16284 }
16285
16286 static int _make_order_rule_string(char *multi_user_name, email_list_sorting_rule_t *input_sorting_rule, char **output_string)
16287 {
16288         EM_DEBUG_FUNC_BEGIN("input_sorting_rule [%p], output_string [%p]", input_sorting_rule, output_string);
16289
16290         char  result_rule_string[QUERY_SIZE] = { 0 , };
16291         int   ret = EMAIL_ERROR_NONE;
16292
16293         emstorage_account_tbl_t *account_tbl_array = NULL;
16294         int count = 0;
16295         int i = 0;
16296         char *result_str = NULL;
16297         char *tmp_str1 = NULL;
16298         char *tmp_str2 = NULL;
16299         char query_per_account[QUERY_SIZE] = { 0 , };
16300
16301         if (input_sorting_rule->force_boolean_check) {
16302                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = 0 ",
16303                         emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16304         } else {
16305                 EM_SAFE_STRNCPY(result_rule_string,
16306                         emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16307                         QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16308         }
16309
16310         switch (input_sorting_rule->sort_order) {
16311         case EMAIL_SORT_ORDER_ASCEND:
16312                 EM_SAFE_STRNCAT(result_rule_string, " ASC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16313                 break;
16314
16315         case EMAIL_SORT_ORDER_DESCEND:
16316                 EM_SAFE_STRNCAT(result_rule_string, " DESC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16317                 break;
16318
16319         case EMAIL_SORT_ORDER_NOCASE_ASCEND:
16320                 EM_SAFE_STRNCAT(result_rule_string, " COLLATE NOCASE ASC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16321                 break;
16322
16323         case EMAIL_SORT_ORDER_NOCASE_DESCEND:
16324                 EM_SAFE_STRNCAT(result_rule_string, " COLLATE NOCASE DESC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16325                 break;
16326
16327         case EMAIL_SORT_ORDER_TO_CCBCC:
16328                 memset(result_rule_string, 0, QUERY_SIZE);
16329                 if (input_sorting_rule->key_value.string_type_value)
16330                         sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16331                                         " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 ELSE 2 END ",
16332                                         input_sorting_rule->key_value.string_type_value);
16333                 break;
16334
16335         case EMAIL_SORT_ORDER_TO_CC_BCC:
16336                 memset(result_rule_string, 0, QUERY_SIZE);
16337                 if (input_sorting_rule->key_value.string_type_value)
16338                         sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16339                                         " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 WHEN full_address_cc LIKE \'%%%q%%\' THEN 2 ELSE 3 END ",
16340                                         input_sorting_rule->key_value.string_type_value, input_sorting_rule->key_value.string_type_value);
16341                 break;
16342
16343         case EMAIL_SORT_ORDER_TO_CCBCC_ALL:
16344                 if (!emstorage_get_account_list(multi_user_name, &count, &account_tbl_array, true, false, NULL)) {
16345                         EM_DEBUG_EXCEPTION("emstorage_get_account_list failed");
16346                         goto FINISH_OFF;
16347                 }
16348
16349                 if (!count) {
16350                         EM_DEBUG_LOG("No account exist");
16351                         ret = EMAIL_ERROR_INVALID_PARAM;
16352                         goto FINISH_OFF;
16353                 }
16354
16355                 for (i = 0; i < count; i++) {
16356                         if (i > 0 && result_str) {
16357                                 tmp_str2 = result_str;
16358                                 result_str = g_strconcat(tmp_str2, " OR ", NULL);
16359                                 EM_SAFE_FREE(tmp_str2);
16360                         }
16361
16362                         memset(query_per_account, 0, QUERY_SIZE);
16363                         snprintf(query_per_account, QUERY_SIZE,
16364                                         "(account_id = %d AND full_address_to LIKE \'%%%s%%\')",
16365                                         account_tbl_array[i].account_id, account_tbl_array[i].user_email_address);
16366
16367                         tmp_str1 = result_str;
16368                         if (tmp_str1)
16369                                 result_str = g_strconcat(tmp_str1, query_per_account, NULL);
16370                         else
16371                                 result_str = g_strdup(query_per_account);
16372                         EM_SAFE_FREE(tmp_str1);
16373                 }
16374
16375                 snprintf(result_rule_string, QUERY_SIZE,
16376                                 " CASE WHEN %s THEN 1 ELSE 2 END ", result_str);
16377
16378                 EM_SAFE_FREE(result_str);
16379                 if (account_tbl_array)
16380                         emstorage_free_account(&account_tbl_array, count, NULL);
16381                 break;
16382
16383         case EMAIL_SORT_ORDER_LOCALIZE_ASCEND:
16384                 memset(result_rule_string, 0, QUERY_SIZE);
16385                 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16386                                 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END ASC, %s COLLATE NOCASE ASC ",
16387                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16388                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16389                 break;
16390
16391         case EMAIL_SORT_ORDER_LOCALIZE_DESCEND:
16392                 memset(result_rule_string, 0, QUERY_SIZE);
16393                 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16394                                 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END DESC, %s COLLATE NOCASE DESC ",
16395                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16396                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16397                 break;
16398
16399         default:
16400                 EM_DEBUG_EXCEPTION("Invalid sort_order [%d]", input_sorting_rule->sort_order);
16401                 ret = EMAIL_ERROR_INVALID_PARAM;
16402                 goto FINISH_OFF;
16403         }
16404
16405         *output_string = strdup(result_rule_string);
16406
16407 FINISH_OFF:
16408         EM_DEBUG_FUNC_END("ret [%d]", ret);
16409         return ret;
16410 }
16411
16412 INTERNAL_FUNC int emstorage_write_conditional_clause_for_getting_mail_list(char *multi_user_name, email_list_filter_t *input_filter_list, int input_filter_count, email_list_sorting_rule_t *input_sorting_rule_list, int input_sorting_rule_count, int input_start_index, int input_limit_count, char **output_conditional_clause)
16413 {
16414         EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d], input_sorting_rule_list[%p], input_sorting_rule_count [%d], input_start_index [%d], input_limit_count [%d], output_conditional_clause [%p]", input_filter_list, input_filter_count, input_sorting_rule_list, input_sorting_rule_count, input_start_index, input_limit_count, output_conditional_clause);
16415         int ret = EMAIL_ERROR_NONE;
16416         int i = 0;
16417         int string_offset = 0;
16418         int query_size = 0;
16419         int new_query_size = 0;
16420         char *conditional_clause_string = NULL;
16421         char *result_string_for_a_item = NULL;
16422
16423         if ((input_filter_count > 0 && !input_filter_list) || (input_sorting_rule_count > 0 && !input_sorting_rule_list) || output_conditional_clause == NULL) {
16424                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16425                 return EMAIL_ERROR_INVALID_PARAM;
16426         }
16427
16428         conditional_clause_string = em_malloc(QUERY_SIZE);
16429         if (conditional_clause_string == NULL) {
16430                 EM_DEBUG_EXCEPTION("Memory is full");
16431                 return EMAIL_ERROR_OUT_OF_MEMORY;
16432         }
16433
16434         if (input_filter_count > 0) {
16435                 query_size = QUERY_SIZE;
16436                 g_strlcpy(conditional_clause_string, " WHERE ", QUERY_SIZE);
16437
16438                 for (i = 0; i < input_filter_count; i++) {
16439                         switch (input_filter_list[i].list_filter_item_type) {
16440                         case EMAIL_LIST_FILTER_ITEM_RULE:
16441                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE", i);
16442                                 _make_filter_rule_string(&(input_filter_list[i].list_filter_item.rule), &result_string_for_a_item);
16443                                 break;
16444
16445                         case EMAIL_LIST_FILTER_ITEM_RULE_FTS:
16446                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_FTS", i);
16447                                 _make_filter_fts_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_fts), &result_string_for_a_item);
16448                                 break;
16449
16450                         case EMAIL_LIST_FILTER_ITEM_RULE_ATTACH:
16451                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_ATTACH", i);
16452                                 _make_filter_attach_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_attach), &result_string_for_a_item);
16453                                 break;
16454
16455                         case EMAIL_LIST_FILTER_ITEM_OPERATOR:
16456                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_OPERATOR", i);
16457                                 switch (input_filter_list[i].list_filter_item.operator_type) {
16458                                 case EMAIL_LIST_FILTER_OPERATOR_AND:
16459                                         result_string_for_a_item = strdup("AND ");
16460                                         break;
16461                                 case EMAIL_LIST_FILTER_OPERATOR_OR:
16462                                         result_string_for_a_item = strdup("OR ");
16463                                         break;
16464                                 case EMAIL_LIST_FILTER_OPERATOR_LEFT_PARENTHESIS:
16465                                         result_string_for_a_item = strdup(" (");
16466                                         break;
16467                                 case EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS:
16468                                         result_string_for_a_item = strdup(") ");
16469                                         break;
16470                                 }
16471                                 break;
16472
16473                         default:
16474                                 EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", input_filter_list[i].list_filter_item_type);
16475                                 ret = EMAIL_ERROR_INVALID_PARAM;
16476                                 goto FINISH_OFF;
16477                         }
16478
16479                         if (result_string_for_a_item == NULL) {
16480                                 EM_DEBUG_EXCEPTION("result_string_for_a_item is null");
16481                                 ret = EMAIL_ERROR_INVALID_PARAM;
16482                                 goto FINISH_OFF;
16483                         }
16484
16485                         if (strlen(conditional_clause_string) + EM_SAFE_STRLEN(result_string_for_a_item) >= query_size) { /* prevent 34364 */
16486                                 EM_DEBUG_LOG("QUERY is too long");
16487                                 new_query_size = EM_SAFE_STRLEN(result_string_for_a_item) + EM_SAFE_STRLEN(conditional_clause_string) + QUERY_SIZE;
16488                                 conditional_clause_string = realloc(conditional_clause_string, new_query_size);
16489                                 if (conditional_clause_string == NULL) {
16490                                         EM_DEBUG_EXCEPTION("realloc failed");
16491                                         ret = EMAIL_ERROR_OUT_OF_MEMORY;
16492                                         goto FINISH_OFF;
16493                                 }
16494
16495                                 query_size = new_query_size;
16496                         }
16497
16498                         EM_SAFE_STRNCAT(conditional_clause_string, result_string_for_a_item , QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16499                         EM_SAFE_FREE(result_string_for_a_item);
16500                 }
16501         }
16502
16503         if (input_sorting_rule_count > 0) {
16504                 EM_SAFE_STRNCAT(conditional_clause_string, "ORDER BY ", QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16505
16506                 for (i = 0; i < input_sorting_rule_count; i++) {
16507                         if ((ret = _make_order_rule_string(multi_user_name, &input_sorting_rule_list[i], &result_string_for_a_item)) != EMAIL_ERROR_NONE) {
16508                                 EM_DEBUG_EXCEPTION("_make_order_rule_string failed. [%d]", ret);
16509                                 goto FINISH_OFF;
16510                         }
16511                         if (i > 0)
16512                                 EM_SAFE_STRNCAT(conditional_clause_string, ", " , QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16513                         EM_SAFE_STRNCAT(conditional_clause_string, result_string_for_a_item , QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16514                         EM_SAFE_FREE(result_string_for_a_item);
16515                 }
16516         }
16517
16518         if (input_start_index != -1 && input_limit_count != -1) {
16519                 string_offset = strlen(conditional_clause_string);
16520                 SNPRINTF_OFFSET(conditional_clause_string, string_offset, query_size, " LIMIT %d, %d", input_start_index, input_limit_count);
16521         }
16522
16523         *output_conditional_clause = strdup(conditional_clause_string);
16524
16525 FINISH_OFF:
16526         EM_SAFE_FREE(result_string_for_a_item);
16527         EM_SAFE_FREE(conditional_clause_string);
16528
16529         EM_DEBUG_FUNC_END("ret [%d]", ret);
16530         return ret;
16531 }
16532
16533 INTERNAL_FUNC int emstorage_free_list_filter(email_list_filter_t **input_filter_list, int input_filter_count)
16534 {
16535         EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d]", input_filter_list, input_filter_count);
16536         int err = EMAIL_ERROR_NONE;
16537         int i = 0;
16538         email_list_filter_t *temp_filter_list = NULL;
16539
16540         EM_IF_NULL_RETURN_VALUE(input_filter_list, EMAIL_ERROR_INVALID_PARAM);
16541
16542         for (i = 0; i < input_filter_count; i++) {
16543                 temp_filter_list = (*input_filter_list) + i;
16544                 if (!temp_filter_list)
16545                         continue;
16546
16547                 if (temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE) {
16548                         switch (temp_filter_list->list_filter_item.rule.target_attribute) {
16549                         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
16550                         case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
16551                         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
16552                         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
16553                         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
16554                         case EMAIL_MAIL_ATTRIBUTE_FROM:
16555                         case EMAIL_MAIL_ATTRIBUTE_TO:
16556                         case EMAIL_MAIL_ATTRIBUTE_CC:
16557                         case EMAIL_MAIL_ATTRIBUTE_BCC:
16558                         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN:
16559                         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML:
16560                         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
16561                                 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule.key_value.string_type_value);
16562                                 break;
16563                         default:
16564                                 break;
16565                         }
16566                 } else if (temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE_FTS && temp_filter_list->list_filter_item.rule_fts.target_attribute == EMAIL_MAIL_TEXT_ATTRIBUTE_FULL_TEXT) {
16567                         EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_fts.key_value.string_type_value);
16568                 } else if (temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE_ATTACH && temp_filter_list->list_filter_item.rule_attach.target_attribute == EMAIL_MAIL_ATTACH_ATTRIBUTE_ATTACHMENT_NAME) {
16569                         EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_attach.key_value.string_type_value);
16570                 }
16571         }
16572
16573         EM_SAFE_FREE(*input_filter_list);
16574
16575         EM_DEBUG_FUNC_END("err [%d]", err);
16576         return err;
16577 }
16578
16579 /* Tasks --------------------------------------------------------------------------*/
16580 INTERNAL_FUNC int emstorage_add_task(char *multi_user_name, email_task_type_t input_task_type, email_task_priority_t input_task_priority, char *input_task_parameter, int input_task_parameter_length, int input_transaction, int *output_task_id)
16581 {
16582         EM_DEBUG_FUNC_BEGIN("input_task_type [%d] input_task_priority[%p], input_task_parameter[%p] input_task_parameter_length[%d] input_transaction[%d] output_task_id[%p]", input_task_type, input_task_priority, input_task_parameter, input_task_parameter_length, input_transaction, output_task_id);
16583         int ret = 0;
16584         int i = 0;
16585         int task_id = 0;
16586         int err = EMAIL_ERROR_NONE;
16587         int rc = -1;
16588         DB_STMT hStmt = NULL;
16589         char sql_query_string[QUERY_SIZE] = {0, };
16590         sqlite3 *local_db_handle = NULL;
16591         char *sql = "SELECT max(rowid) FROM mail_task_tbl;";
16592         char **result = NULL;
16593
16594         if (input_task_parameter == NULL || output_task_id == NULL) {
16595                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16596                 return EMAIL_ERROR_INVALID_PARAM;
16597         }
16598
16599         local_db_handle = emstorage_get_db_connection(multi_user_name);
16600         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, err);
16601
16602         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
16603         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16604                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
16605
16606         if (NULL == result[1])
16607                 task_id = 1;
16608         else
16609                 task_id = atoi(result[1])+1;
16610
16611         *output_task_id = task_id;
16612
16613         sqlite3_free_table(result);
16614         result = NULL;
16615
16616         SNPRINTF(sql_query_string, sizeof(sql_query_string),
16617                         "INSERT INTO mail_task_tbl VALUES "
16618                         "(        "
16619                         "    ? "  /*   task_id */
16620                         "  , ? "  /*   task_type */
16621                         "  , ? "  /*   task_status */
16622                         "  , ? "  /*   task_priority */
16623                         "  , ? "  /*   task_parameter_length */
16624                         "  , ? "  /*   task_parameter */
16625                         "  , ? "  /*   date_time */
16626                         ") ");
16627
16628         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16629         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16630                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16631
16632         EM_DEBUG_LOG_SEC(">>>> SQL STMT [%s] ", sql_query_string);
16633
16634
16635         _bind_stmt_field_data_int(hStmt, i++, task_id);
16636         _bind_stmt_field_data_int(hStmt, i++, input_task_type);
16637         _bind_stmt_field_data_int(hStmt, i++, EMAIL_TASK_STATUS_WAIT);
16638         _bind_stmt_field_data_int(hStmt, i++, input_task_priority);
16639         _bind_stmt_field_data_int(hStmt, i++, input_task_parameter_length);
16640         _bind_stmt_field_data_blob(hStmt, i++, input_task_parameter, input_task_parameter_length);
16641         _bind_stmt_field_data_int(hStmt, i++, time(NULL));
16642
16643         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16644
16645         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {err = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
16646                         ("sqlite3_step fail:%d", rc));
16647         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16648                         ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
16649
16650         ret = (err == EMAIL_ERROR_NONE);
16651
16652 FINISH_OFF:
16653         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, err);
16654
16655         if (hStmt != NULL) {
16656                 rc = sqlite3_finalize(hStmt);
16657                 if (rc != SQLITE_OK) {
16658                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16659                         err = EMAIL_ERROR_DB_FAILURE;
16660                 }
16661         }
16662
16663         EM_DEBUG_FUNC_END("err [%d]", err);
16664         return err;
16665 }
16666
16667 INTERNAL_FUNC int emstorage_delete_task(char *multi_user_name, int task_id, int transaction)
16668 {
16669         EM_DEBUG_FUNC_BEGIN("task_id[%d], transaction[%d]", task_id, transaction);
16670         int ret = false;
16671         int err = EMAIL_ERROR_NONE;
16672         char sql_query_string[QUERY_SIZE] = {0, };
16673
16674         sqlite3 *local_db_handle = NULL;
16675
16676         if (task_id < 0) {
16677                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16678                 return EMAIL_ERROR_INVALID_PARAM;
16679         }
16680
16681         local_db_handle = emstorage_get_db_connection(multi_user_name);
16682
16683         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
16684
16685         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_task_tbl WHERE task_id = %d", task_id);
16686         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16687         if (err != EMAIL_ERROR_NONE) {
16688                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16689                 goto FINISH_OFF;
16690         }
16691
16692         ret = true;
16693
16694 FINISH_OFF:
16695         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
16696
16697         EM_DEBUG_FUNC_END("err [%d]", err);
16698         return err;
16699 }
16700
16701 INTERNAL_FUNC int emstorage_update_task_status(char *multi_user_name, int task_id, email_task_status_type_t task_status, int transaction)
16702 {
16703         EM_DEBUG_FUNC_BEGIN("task_id[%d] task_status[%d] transaction[%d]", task_id, task_status, transaction);
16704         int ret = false;
16705         int err = EMAIL_ERROR_NONE;
16706         char sql_query_string[QUERY_SIZE] = {0, };
16707
16708         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16709         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
16710
16711         SNPRINTF(sql_query_string, sizeof(sql_query_string),
16712                         "UPDATE mail_task_tbl SET"
16713                         " task_status = %d"
16714                         " WHERE task_id = %d"
16715                         , task_status
16716                         , task_id);
16717         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16718         if (err != EMAIL_ERROR_NONE) {
16719                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16720                 goto FINISH_OFF;
16721         }
16722
16723         ret = true;
16724
16725 FINISH_OFF:
16726         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
16727
16728         EM_DEBUG_FUNC_END("err [%d]", err);
16729         return err;
16730 }
16731
16732 INTERNAL_FUNC int emstorage_query_task(char *multi_user_name,
16733                 const char *input_conditional_clause,
16734                 const char *input_ordering_clause,
16735                 email_task_t **output_task_list,
16736                 int *output_task_count)
16737 {
16738         EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_ordering_clause [%p], "
16739                         "output_task_list[%p], output_task_count[%d]",
16740                         input_conditional_clause, input_ordering_clause, output_task_list, output_task_count);
16741         int i = 0, count = 0, rc = -1;
16742         int cur_query = 0;
16743         int field_index = 0;
16744         int err = EMAIL_ERROR_NONE;
16745         email_task_t *task_item_from_tbl = NULL;
16746         char sql_query_string[QUERY_SIZE] = {0, };
16747         char *field_list = "task_id, task_type, task_status, task_priority, task_parameter_length, task_parameter ";
16748         char **result;
16749         sqlite3 *local_db_handle = NULL;
16750         DB_STMT hStmt = NULL;
16751
16752         EM_IF_NULL_RETURN_VALUE(input_conditional_clause, false);
16753         EM_IF_NULL_RETURN_VALUE(output_task_count, false);
16754
16755         local_db_handle = emstorage_get_db_connection(multi_user_name);
16756
16757         SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
16758                         "SELECT COUNT(*) FROM mail_task_tbl %s", input_conditional_clause);
16759         EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
16760
16761         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
16762         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16763                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16764
16765         count = atoi(result[1]);
16766         sqlite3_free_table(result);
16767
16768         EM_DEBUG_LOG("count = %d", rc);
16769
16770         if (count == 0) {
16771                 EM_DEBUG_EXCEPTION("no task found...");
16772                 err = EMAIL_ERROR_TASK_NOT_FOUND;
16773                 goto FINISH_OFF;
16774         }
16775
16776         SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
16777                         "SELECT %s FROM mail_task_tbl %s %s", field_list, input_conditional_clause, input_ordering_clause);
16778         EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
16779
16780         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16781
16782         EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
16783         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16784                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16785
16786         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16787         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16788                         ("sqlite3_step fail:%d", rc));
16789
16790         if (rc == SQLITE_DONE) {
16791                 EM_DEBUG_EXCEPTION("no task found...");
16792                 err = EMAIL_ERROR_TASK_NOT_FOUND;
16793                 count = 0;
16794                 goto FINISH_OFF;
16795         }
16796
16797         if (!(task_item_from_tbl = (email_task_t*)em_malloc(sizeof(email_task_t) * count))) {
16798                 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
16799                 err = EMAIL_ERROR_OUT_OF_MEMORY;
16800                 goto FINISH_OFF;
16801         }
16802
16803         for (i = 0; i < count; i++) {
16804                 /*  get recordset */
16805                 field_index = 0;
16806
16807                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_id), field_index++);
16808                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_type), field_index++);
16809                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_status), field_index++);
16810                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_priority), field_index++);
16811                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_parameter_length), field_index++);
16812                 _get_stmt_field_data_blob(hStmt, (void**)&(task_item_from_tbl[i].task_parameter), field_index++);
16813
16814                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16815                 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i,  rc);
16816                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16817                                 ("sqlite3_step fail:%d", rc));
16818         }
16819
16820 FINISH_OFF:
16821
16822         if (err == EMAIL_ERROR_NONE) {
16823                 if (output_task_list)
16824                         *output_task_list = task_item_from_tbl;
16825                 *output_task_count = count;
16826         } else {
16827                 if (task_item_from_tbl) {
16828                         for (i = 0; i < count; i++)
16829                                 EM_SAFE_FREE(task_item_from_tbl[i].task_parameter);
16830
16831                         free(task_item_from_tbl);
16832                 }
16833         }
16834
16835         if (hStmt != NULL) {
16836                 rc = sqlite3_finalize(hStmt);
16837                 hStmt = NULL;
16838                 if (rc != SQLITE_OK) {
16839                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16840                         err = EMAIL_ERROR_DB_FAILURE;
16841                 }
16842         }
16843
16844         EM_DEBUG_FUNC_END("err [%d]", err);
16845         return err;
16846 }
16847
16848 INTERNAL_FUNC int emstorage_check_and_update_server_uid_by_message_id(char *multi_user_name, int account_id, email_mailbox_type_e input_mailbox_type, char *message_id, char *server_uid, int *mail_id)
16849 {
16850         EM_DEBUG_FUNC_BEGIN("account_id:[%d], mailbox_type:[%d], message_id:[%s], server_uid:[%s]", account_id, input_mailbox_type, message_id, server_uid);
16851         int err = EMAIL_ERROR_NONE;
16852
16853         if (message_id == NULL) {
16854                 EM_DEBUG_EXCEPTION("Invalid parameter");
16855                 err = EMAIL_ERROR_INVALID_PARAM;
16856                 return err;
16857         }
16858
16859         int rc = -1;
16860         int count = 0;
16861         int temp_mail_id = 0;
16862         int where_pararaph_length = 0;
16863         char *where_pararaph = NULL;
16864         char sql_query_string[QUERY_SIZE] = {0, };
16865         char **result = NULL;
16866         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16867
16868         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id from mail_tbl ");
16869
16870         where_pararaph_length = EM_SAFE_STRLEN(message_id) + 100;
16871         where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
16872         if (where_pararaph == NULL) {
16873                 EM_DEBUG_EXCEPTION("em_mallocfailed");
16874                 err = EMAIL_ERROR_OUT_OF_MEMORY;
16875                 goto FINISH_OFF;
16876         }
16877
16878         if (account_id != ALL_ACCOUNT)
16879                 sqlite3_snprintf(where_pararaph_length, where_pararaph, "WHERE account_id = %d AND mailbox_type = %d AND message_id like '%q'", account_id, input_mailbox_type, message_id);
16880         else
16881                 sqlite3_snprintf(where_pararaph_length, where_pararaph, "WHERE mailbox_type = %d AND message_id like '%q'", input_mailbox_type, message_id);
16882
16883         if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
16884                 EM_SAFE_STRNCAT(sql_query_string, where_pararaph , QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
16885
16886         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
16887
16888         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16889         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16890                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16891
16892
16893         EM_DEBUG_LOG("Count of mails [%d]", count);
16894
16895         if (count) {
16896                 _get_table_field_data_int(result, &temp_mail_id, 1);
16897                 EM_DEBUG_LOG("Searched mail_id [%d]", temp_mail_id);
16898
16899                 memset(sql_query_string, 0x00, QUERY_SIZE);
16900                 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string, "UPDATE mail_tbl set server_mail_id = '%q'", server_uid);
16901
16902                 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE) {
16903                         EM_SAFE_STRNCAT(sql_query_string, where_pararaph,
16904                                         QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
16905                 }
16906
16907                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16908                 if (err != EMAIL_ERROR_NONE) {
16909                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16910                         goto FINISH_OFF;
16911                 }
16912
16913         } else {
16914                 err = EMAIL_ERROR_MAIL_NOT_FOUND;
16915         }
16916
16917 FINISH_OFF:
16918
16919         sqlite3_free_table(result);
16920         result = NULL;
16921
16922
16923         EM_SAFE_FREE(where_pararaph);
16924
16925         if (mail_id != NULL)
16926                 *mail_id = temp_mail_id;
16927
16928         EM_DEBUG_FUNC_END("err : [%d]", err);
16929         return err;
16930 }
16931 /* Tasks --------------------------------------------------------------------------*/
16932
16933 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
16934 INTERNAL_FUNC int emstorage_add_auto_download_activity(char *multi_user_name, email_event_auto_download *local_activity, int *activity_id, int transaction, int *err_code)
16935 {
16936         EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
16937
16938         if (!local_activity || !activity_id) {
16939                 EM_DEBUG_EXCEPTION("local_activity[%p], activity_id[%p]", local_activity, activity_id);
16940                 if (err_code != NULL)
16941                         *err_code = EMAIL_ERROR_INVALID_PARAM;
16942                 return false;
16943         }
16944
16945         int rc = -1;
16946         int ret = false;
16947         int error = EMAIL_ERROR_NONE;
16948         int i = 0;
16949
16950         char sql_query_string[QUERY_SIZE] = {0, };
16951         DB_STMT hStmt = NULL;
16952
16953         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16954         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
16955
16956         memset(sql_query_string, 0x00, sizeof(sql_query_string));
16957         SNPRINTF(sql_query_string, sizeof(sql_query_string),
16958                         "INSERT INTO mail_auto_download_activity_tbl VALUES "
16959                         "("
16960                         "? "  /* Activity ID */
16961                         ",?"  /* Status */
16962                         ",?"  /* Account ID */
16963                         ",?"  /* Local Mail ID */
16964                         ",?"  /* Server mail ID */
16965                         ",?"  /* Mailbox ID*/
16966                         ",?"  /* Multi USER NAME */
16967                         ") ");
16968
16969         char *sql = "SELECT max(rowid) FROM mail_auto_download_activity_tbl;";
16970         char **result = NULL;
16971
16972
16973         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
16974
16975         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16976                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
16977
16978         if (NULL == result[1]) rc = 1;
16979         else rc = atoi(result[1])+1;
16980         sqlite3_free_table(result);
16981         result = NULL;
16982
16983         *activity_id = local_activity->activity_id = rc;
16984
16985         EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [%d], MAIL ID [%d], SERVER MAIL ID [%lu]",
16986                         local_activity->activity_id, local_activity->mail_id, local_activity->server_mail_id);
16987
16988         if (local_activity->mailbox_id)
16989                 EM_DEBUG_LOG(" MAILBOX ID [%d]", local_activity->mailbox_id);
16990
16991
16992         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16993         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16994                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16995
16996
16997         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
16998         _bind_stmt_field_data_int(hStmt, i++, local_activity->status);
16999         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
17000         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
17001         _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
17002         _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
17003         _bind_stmt_field_data_string(hStmt, i++, (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
17004
17005
17006         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17007
17008         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
17009                         ("sqlite3_step fail:%d", rc));
17010         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17011                         ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
17012
17013         ret = true;
17014
17015 FINISH_OFF:
17016         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17017         if (hStmt != NULL) {
17018                 rc = sqlite3_finalize(hStmt);
17019                 hStmt = NULL;
17020                 if (rc != SQLITE_OK) {
17021                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17022                         error = EMAIL_ERROR_DB_FAILURE;
17023                 }
17024         }
17025
17026         if (err_code != NULL)
17027                 *err_code = error;
17028
17029         EM_DEBUG_FUNC_END("ret [%d]", ret);
17030         return ret;
17031 }
17032
17033
17034 INTERNAL_FUNC int emstorage_delete_auto_download_activity(char *multi_user_name, int account_id, int mail_id, int activity_id, int transaction, int *err_code)
17035 {
17036         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d] , activity_id[%d], transaction[%d], err_code[%p]", account_id, mail_id, activity_id, transaction, err_code);
17037
17038         if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
17039                 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], activity_id[%d], transaction[%d], err_code[%p]", account_id, mail_id, activity_id, transaction, err_code);
17040
17041                 if (err_code != NULL)
17042                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17043                 return false;
17044         }
17045
17046         int rc = -1;
17047         int ret = false;
17048         int error = EMAIL_ERROR_NONE;
17049         char sql_query_string[QUERY_SIZE] = {0, };
17050         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17051
17052         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17053         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17054
17055         if (activity_id == 0)
17056                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d AND mail_id = %d", account_id, mail_id);
17057         else
17058                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d AND activity_id = %d", account_id, activity_id);
17059
17060         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17061         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17062         if (error != EMAIL_ERROR_NONE) {
17063                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17064                 goto FINISH_OFF;
17065         }
17066
17067         /*  validate activity existence */
17068         rc = sqlite3_changes(local_db_handle);
17069         if (rc == 0) {
17070                 EM_DEBUG_EXCEPTION("No matching activity found");
17071                 error = EMAIL_ERROR_DATA_NOT_FOUND;
17072                 ret = true;
17073                 goto FINISH_OFF;
17074         }
17075
17076         ret = true;
17077
17078 FINISH_OFF:
17079         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
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 INTERNAL_FUNC int emstorage_delete_all_auto_download_activity(char *multi_user_name, int account_id, int transaction, int *err_code)
17089 {
17090         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17091
17092         if (account_id < FIRST_ACCOUNT_ID) {
17093                 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17094                 if (err_code != NULL)
17095                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17096                 return false;
17097         }
17098
17099         int rc = -1;
17100         int ret = false;
17101         int error = EMAIL_ERROR_NONE;
17102         char sql_query_string[QUERY_SIZE] = {0, };
17103
17104         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17105
17106         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17107         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17108         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d", account_id);
17109
17110         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17111         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17112         if (error != EMAIL_ERROR_NONE) {
17113                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17114                 goto FINISH_OFF;
17115         }
17116
17117         rc = sqlite3_changes(local_db_handle);
17118         if (rc == 0) {
17119                 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17120                 error = EMAIL_ERROR_DATA_NOT_FOUND;
17121                 ret = true;
17122                 goto FINISH_OFF;
17123         }
17124
17125         ret = true;
17126
17127 FINISH_OFF:
17128         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17129
17130         if (err_code != NULL)
17131                 *err_code = error;
17132
17133         EM_DEBUG_FUNC_END("ret [%d]", ret);
17134         return ret;
17135 }
17136
17137
17138 INTERNAL_FUNC int emstorage_delete_auto_download_activity_by_mailbox(char *multi_user_name, int account_id, int mailbox_id, int transaction, int *err_code)
17139 {
17140         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17141
17142         if (account_id < FIRST_ACCOUNT_ID || mailbox_id < 0) {
17143                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17144                 if (err_code != NULL)
17145                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17146                 return false;
17147         }
17148
17149         int rc = -1;
17150         int ret = false;
17151         int error = EMAIL_ERROR_NONE;
17152         char sql_query_string[QUERY_SIZE] = {0, };
17153
17154         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17155
17156         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17157         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17158         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d AND mailbox_id = %d", account_id, mailbox_id);
17159
17160         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17161         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17162         if (error != EMAIL_ERROR_NONE) {
17163                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17164                 goto FINISH_OFF;
17165         }
17166
17167         rc = sqlite3_changes(local_db_handle);
17168         if (rc == 0) {
17169                 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17170                 error = EMAIL_ERROR_DATA_NOT_FOUND;
17171                 ret = true;
17172                 goto FINISH_OFF;
17173         }
17174
17175         ret = true;
17176
17177 FINISH_OFF:
17178         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17179
17180         if (err_code != NULL)
17181                 *err_code = error;
17182
17183         EM_DEBUG_FUNC_END("ret [%d]", ret);
17184         return ret;
17185 }
17186
17187
17188 INTERNAL_FUNC int emstorage_get_auto_download_activity(char *multi_user_name, int account_id, int input_mailbox_id, email_event_auto_download **event_start, int *count, int transaction, int *err_code)
17189 {
17190         EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
17191
17192         if (account_id < FIRST_ACCOUNT_ID || !event_start || input_mailbox_id <= 0 || !count) {
17193                 EM_DEBUG_EXCEPTION("account_id[%d], event_start[%p], input_mailbox_id[%d], count[%p], err_code[%p]", account_id, event_start, input_mailbox_id, count, err_code);
17194
17195                 if (err_code != NULL)
17196                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17197                 return false;
17198         }
17199
17200         int rc = -1;
17201         int ret = false;
17202         char **result;
17203         int error = EMAIL_ERROR_NONE;
17204         int i = 0;
17205         DB_STMT hStmt = NULL;
17206         email_event_auto_download *event_list = NULL;
17207         char sql_query_string[QUERY_SIZE] = {0, };
17208
17209         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17210
17211         EMSTORAGE_START_READ_TRANSACTION(transaction);
17212
17213         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17214         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl WHERE account_id = %d AND mailbox_id = '%d' order by activity_id", account_id, input_mailbox_id);
17215
17216
17217         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17218         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17219                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17220
17221         *count = atoi(result[1]);
17222         sqlite3_free_table(result);
17223
17224         EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
17225
17226         if (!*count) {
17227                 EM_DEBUG_LOG("No matched activity found in mail_auto_download_activity_tbl");
17228                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
17229                 ret = true;
17230                 goto FINISH_OFF;
17231         }
17232         EM_DEBUG_LOG("Activity Count = %d", *count);
17233
17234         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17235         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_auto_download_activity_tbl WHERE account_id = %d AND mailbox_id = '%d' order by activity_id", account_id, input_mailbox_id);
17236
17237         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17238
17239
17240         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17241
17242         EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
17243         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17244                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17245
17246
17247         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17248         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17249                         ("sqlite3_step fail:%d", rc));
17250
17251         if (!(event_list = (email_event_auto_download *)em_malloc(sizeof(email_event_auto_download)*(*count)))) {
17252                 EM_DEBUG_EXCEPTION("Malloc failed");
17253
17254                 error = EMAIL_ERROR_OUT_OF_MEMORY;
17255                 goto FINISH_OFF;
17256         }
17257
17258         for (i = 0; i < (*count); i++) {
17259                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17260                 _get_stmt_field_data_int(hStmt, &(event_list[i].status), STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17261                 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17262                 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17263                 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17264                 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17265
17266                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17267                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17268                                 ("sqlite3_step fail:%d", rc));
17269         }
17270
17271         ret = true;
17272
17273 FINISH_OFF:
17274
17275         if (true == ret)
17276                 *event_start = event_list;
17277         else {
17278                 EM_SAFE_FREE(event_list);
17279                 *event_start = NULL;
17280                 *count = 0;
17281         }
17282
17283         if (hStmt != NULL) {
17284                 rc = sqlite3_finalize(hStmt);
17285                 hStmt = NULL;
17286                 if (rc != SQLITE_OK) {
17287                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17288                         error = EMAIL_ERROR_DB_FAILURE;
17289                 }
17290         }
17291
17292         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17293         if (err_code != NULL)
17294                 *err_code = error;
17295
17296         EM_DEBUG_FUNC_END("ret [%d]", ret);
17297         return ret;
17298 }
17299
17300
17301 INTERNAL_FUNC int emstorage_get_auto_download_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
17302 {
17303         EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
17304
17305         if (!activity_count || !err_code) {
17306                 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
17307                 if (err_code != NULL)
17308                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17309                 return false;
17310         }
17311
17312         int rc = -1;
17313         int ret = false;
17314         int error = EMAIL_ERROR_NONE;
17315         DB_STMT hStmt = NULL;
17316         char sql_query_string[QUERY_SIZE] = {0, };
17317
17318         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17319
17320         EMSTORAGE_START_READ_TRANSACTION(transaction);
17321         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17322
17323         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl;");
17324
17325         EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
17326
17327
17328         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17329         EM_DEBUG_LOG_DEV("before sqlite3_prepare hStmt = %p", hStmt);
17330         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17331                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17332
17333
17334         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17335         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17336                         ("sqlite3_step fail:%d", rc));
17337
17338         _get_stmt_field_data_int(hStmt, activity_count, 0);
17339
17340         EM_DEBUG_LOG("counts of activities in activity table [%d]", *activity_count);
17341
17342         ret = true;
17343
17344 FINISH_OFF:
17345
17346         if (hStmt != NULL) {
17347                 rc = sqlite3_finalize(hStmt);
17348                 hStmt = NULL;
17349                 if (rc != SQLITE_OK) {
17350                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17351                         error = EMAIL_ERROR_DB_FAILURE;
17352                 }
17353         }
17354
17355         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17356         if (err_code != NULL)
17357                 *err_code = error;
17358
17359         EM_DEBUG_FUNC_END("ret [%d]", ret);
17360         return ret;
17361 }
17362
17363
17364 INTERNAL_FUNC int emstorage_get_auto_download_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
17365 {
17366         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
17367
17368         if (!account_list || !count) {
17369                 EM_DEBUG_EXCEPTION("account_list[%p], count[%p]", account_list, count);
17370                 if (err_code != NULL)
17371                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17372                 return false;
17373         }
17374
17375         int ret = false;
17376         int error = EMAIL_ERROR_NONE;
17377         char *sql = "SELECT count(distinct account_id) FROM mail_auto_download_activity_tbl";
17378         char **result;
17379         int i = 0, rc = -1;
17380         int *result_account_list = NULL;
17381         DB_STMT hStmt = NULL;
17382         char sql_query_string[QUERY_SIZE] = {0, };
17383         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17384
17385         EMSTORAGE_START_READ_TRANSACTION(transaction);
17386
17387         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
17388         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17389                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
17390
17391         *count = atoi(result[1]);
17392         sqlite3_free_table(result);
17393
17394         if (!*count) {
17395                 EM_DEBUG_EXCEPTION("no account found...");
17396                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
17397                 ret = true;
17398                 goto FINISH_OFF;
17399         }
17400
17401         EM_DEBUG_LOG("Account count [%d]", *count);
17402
17403         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17404
17405         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_auto_download_activity_tbl");
17406
17407         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17408
17409
17410         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17411
17412         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
17413         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17414                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17415
17416
17417         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17418         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17419                         ("sqlite3_step fail:%d", rc));
17420
17421         if (NULL == (result_account_list = (int *)em_malloc(sizeof(int)*(*count)))) {
17422                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
17423                 error = EMAIL_ERROR_OUT_OF_MEMORY;
17424                 goto FINISH_OFF;
17425         }
17426
17427         for (i = 0; i < (*count); i++) {
17428                 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
17429
17430                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17431                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17432                                 ("sqlite3_step fail:%d", rc));
17433                 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
17434         }
17435
17436         ret = true;
17437
17438 FINISH_OFF:
17439
17440         if (ret == true)
17441                 *account_list = result_account_list;
17442         else
17443                 EM_SAFE_FREE(result_account_list);
17444
17445         if (hStmt != NULL) {
17446                 rc = sqlite3_finalize(hStmt);
17447                 hStmt = NULL;
17448                 if (rc != SQLITE_OK) {
17449                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17450                         error = EMAIL_ERROR_DB_FAILURE;
17451                 }
17452         }
17453
17454         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17455         if (err_code != NULL)
17456                 *err_code = error;
17457         EM_DEBUG_FUNC_END("ret [%d]", ret);
17458         return ret;
17459 }
17460
17461
17462 INTERNAL_FUNC int emstorage_get_auto_download_mailbox_list(char *multi_user_name, int account_id, int **mailbox_list, int *count, int transaction, int *err_code)
17463 {
17464         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
17465
17466         if (account_id < FIRST_ACCOUNT_ID || !mailbox_list || !count) {
17467                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p]", account_id, mailbox_list, count);
17468                 if (err_code != NULL)
17469                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17470                 return false;
17471         }
17472
17473         int ret = false;
17474         int error = EMAIL_ERROR_NONE;
17475         char **result;
17476         int i = 0, rc = -1;
17477         int *mbox_list = NULL;
17478         DB_STMT hStmt = NULL;
17479         char sql_query_string[QUERY_SIZE] = {0, };
17480
17481         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17482
17483         EMSTORAGE_START_READ_TRANSACTION(transaction);
17484
17485         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17486         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(distinct mailbox_id) FROM mail_auto_download_activity_tbl WHERE account_id = %d order by mailbox_id", account_id);
17487
17488
17489         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17490         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17491                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17492
17493         *count = atoi(result[1]);
17494         sqlite3_free_table(result);
17495
17496         if (!*count) {
17497                 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
17498                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
17499                 ret = true;
17500                 goto FINISH_OFF;
17501         }
17502         EM_DEBUG_LOG("Mailbox count = %d", *count);
17503
17504         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17505
17506         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct mailbox_id FROM mail_auto_download_activity_tbl WHERE account_id = %d order by mailbox_id", account_id);
17507
17508         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
17509
17510
17511         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17512
17513
17514         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17515                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17516
17517
17518         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17519         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17520                         ("sqlite3_step fail:%d", rc));
17521
17522         mbox_list = (int *)em_malloc(sizeof(int)*(*count)); /* prevent */
17523         if (mbox_list == NULL) {
17524                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
17525                 error = EMAIL_ERROR_OUT_OF_MEMORY;
17526                 goto FINISH_OFF;
17527         }
17528
17529         for (i = 0; i < (*count); i++) {
17530                 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
17531                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17532
17533                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17534                                 ("sqlite3_step fail:%d", rc));
17535                 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
17536         }
17537
17538         ret = true;
17539
17540 FINISH_OFF:
17541
17542         if (ret == true)
17543                 *mailbox_list = mbox_list;
17544         else
17545                 EM_SAFE_FREE(mbox_list);
17546
17547         if (hStmt != NULL) {
17548                 rc = sqlite3_finalize(hStmt);
17549                 hStmt = NULL;
17550                 if (rc != SQLITE_OK) {
17551                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17552                         error = EMAIL_ERROR_DB_FAILURE;
17553                 }
17554         }
17555
17556         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17557         if (err_code != NULL)
17558                 *err_code = error;
17559         EM_DEBUG_FUNC_END("ret [%d]", ret);
17560         return ret;
17561 }
17562
17563
17564 INTERNAL_FUNC int emstorage_get_auto_download_activity_count_by_mailbox(char *multi_user_name, int account_id, int input_mailbox_id, int *activity_count, int transaction, int *err_code)
17565 {
17566         EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
17567
17568         if (account_id < FIRST_ACCOUNT_ID || !activity_count || !err_code) {
17569                 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
17570                 if (err_code != NULL)
17571                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17572                 return false;
17573         }
17574
17575         int rc = -1;
17576         int ret = false;
17577         int error = EMAIL_ERROR_NONE;
17578         char sql_query_string[QUERY_SIZE] = {0, };
17579         DB_STMT hStmt = NULL;
17580
17581         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17582
17583         EMSTORAGE_START_READ_TRANSACTION(transaction);
17584         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17585         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl WHERE account_id = %d and mailbox_id = '%d'", account_id, input_mailbox_id);
17586
17587         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
17588
17589         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17590         EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
17591         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17592                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17593
17594         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17595         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17596                         ("sqlite3_step fail:%d", rc));
17597
17598         _get_stmt_field_data_int(hStmt, activity_count, 0);
17599
17600         EM_DEBUG_LOG("count of activities in activity table [%d]", *activity_count);
17601
17602         ret = true;
17603
17604 FINISH_OFF:
17605
17606         if (hStmt != NULL) {
17607                 rc = sqlite3_finalize(hStmt);
17608                 hStmt = NULL;
17609                 if (rc != SQLITE_OK) {
17610                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17611                         error = EMAIL_ERROR_DB_FAILURE;
17612                 }
17613         }
17614
17615         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17616
17617         if (err_code != NULL)
17618                 *err_code = error;
17619
17620         EM_DEBUG_FUNC_END("ret [%d]", ret);
17621         return ret;
17622 }
17623
17624
17625 INTERNAL_FUNC int emstorage_update_auto_download_activity(char *multi_user_name, char *old_server_uid, char *new_server_uid, char *mailbox_name, int mailbox_id, int *err_code)
17626 {
17627         EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mailbox_id[%d]", old_server_uid, new_server_uid, mailbox_id);
17628
17629         int rc = -1, ret = false;
17630         int error = EMAIL_ERROR_NONE;
17631         char sql_query_string[QUERY_SIZE] = {0, };
17632         int transaction = true;
17633
17634         if (!old_server_uid || !new_server_uid || (!mailbox_name && mailbox_id < 0)) {
17635                 EM_DEBUG_EXCEPTION("Invalid parameters");
17636                 error = EMAIL_ERROR_INVALID_PARAM;
17637                 return false;
17638         }
17639
17640         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17641
17642         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17643         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17644
17645         if (mailbox_id > 0)
17646                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17647                                 "UPDATE mail_auto_download_activity_tbl SET server_mail_id = %s , mailbox_id ='%d' WHERE server_mail_id = %s ", new_server_uid, mailbox_id, old_server_uid);
17648         else if (mailbox_name)
17649                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17650                                 "UPDATE mail_auto_download_activity_tbl SET server_mail_id = %s WHERE server_mail_id = %s ", new_server_uid, old_server_uid);
17651
17652         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17653         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17654         if (error != EMAIL_ERROR_NONE) {
17655                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17656                 goto FINISH_OFF;
17657         }
17658
17659         rc = sqlite3_changes(local_db_handle);
17660         if (rc == 0)
17661                 EM_DEBUG_LOG("No matching found in mail_auto_download_activity_tbl");
17662
17663         ret = true;
17664
17665 FINISH_OFF:
17666
17667         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17668
17669         if (err_code != NULL)
17670                 *err_code = error;
17671
17672         EM_DEBUG_FUNC_END("ret [%d]", ret);
17673         return ret;
17674 }
17675
17676 #endif
17677
17678 #ifdef __FEATURE_UPDATE_DB_TABLE_SCHEMA__
17679
17680 typedef struct {
17681         char *column_name;
17682         char *column_type;
17683 } email_column_info_t;
17684
17685 static int get_column_information_from_table_callback(void *arg1, int argc, char **argv, char **input_column_name)
17686 {
17687         EM_DEBUG_FUNC_BEGIN("arg1[%p] argc[%d] argv[%p] column_name[%p]", arg1, argc, argv, input_column_name);
17688
17689         int i = 0;
17690         int validated = 0;
17691         char *column_name = NULL;
17692         char *column_type = NULL;
17693         GList *new_list = *((GList**)arg1);
17694         email_column_info_t *column_info_item = NULL;
17695
17696         for (i = 0; i < argc; ++i) {
17697                 /* EM_DEBUG_LOG("%s = %s", input_column_name[i], argv[i]); */
17698                 if (EM_SAFE_STRCMP(input_column_name[i], "name") == 0) {
17699                         if (column_name)
17700                                 EM_SAFE_FREE(column_name);
17701
17702                         column_name = EM_SAFE_STRDUP(argv[i]);
17703                         validated = 1;
17704                 } else if (EM_SAFE_STRCMP(input_column_name[i], "type") == 0) {
17705                         if (column_type)
17706                                 EM_SAFE_FREE(column_type);
17707
17708                         column_type = EM_SAFE_STRDUP(argv[i]);
17709                 }
17710         }
17711
17712         if (validated) {
17713                 EM_DEBUG_LOG("column_name[%s] column_type[%s]", column_name, column_type);
17714                 column_info_item = em_malloc(sizeof(email_column_info_t));
17715                 if (column_info_item == NULL) {
17716                         EM_DEBUG_EXCEPTION("em_mallocfailed");
17717                         goto FINISH_OFF;
17718                 }
17719
17720                 column_info_item->column_name = EM_SAFE_STRDUP(column_name);
17721                 column_info_item->column_type = EM_SAFE_STRDUP(column_type);
17722                 new_list = g_list_append(new_list, (gpointer)column_info_item);
17723                 *((GList**)arg1) = new_list;
17724         }
17725
17726 FINISH_OFF:
17727
17728         EM_SAFE_FREE(column_name);
17729         EM_SAFE_FREE(column_type);
17730
17731         EM_DEBUG_FUNC_END();
17732         return 0;
17733 }
17734
17735 static int emstorage_get_column_information_from_table(char *multi_user_name, const char *input_table_name, GList **output_column_info)
17736 {
17737         EM_DEBUG_FUNC_BEGIN("input_table_name[%p] output_column_info[%p]", input_table_name, output_column_info);
17738         int err = EMAIL_ERROR_NONE;
17739         int result_from_sqlite = 0;
17740         char *error_message_from_sqlite = NULL;
17741         char sql_query_string[QUERY_SIZE] = {0, };
17742         GList *new_list = NULL;
17743         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17744
17745         SNPRINTF(sql_query_string, QUERY_SIZE, "pragma table_info(%s);", input_table_name);
17746
17747         result_from_sqlite = sqlite3_exec(local_db_handle, sql_query_string, get_column_information_from_table_callback, &new_list, &error_message_from_sqlite);
17748
17749         if (result_from_sqlite != SQLITE_OK)
17750                 EM_DEBUG_EXCEPTION("sqlite3_exec returns [%d]", result_from_sqlite);
17751
17752         EM_DEBUG_LOG("new_list[%p] output_column_info[%p]", new_list, output_column_info);
17753
17754         if (new_list && output_column_info) {
17755                 EM_DEBUG_LOG("g_list_length[%d]", g_list_length(new_list));
17756                 *output_column_info = new_list;
17757         }
17758
17759         EM_DEBUG_FUNC_END("err [%d]", err);
17760         return err;
17761 }
17762
17763 static  int emstorage_create_renamed_table(char *multi_user_name, char **input_full_query, int input_query_index, char *input_source_table_name, char *input_new_table_name)
17764 {
17765         EM_DEBUG_FUNC_BEGIN("input_full_query [%p] input_query_index[%d] input_source_table_name[%p] input_new_table_name[%p]", input_full_query, input_query_index, input_source_table_name, input_new_table_name);
17766         int error = EMAIL_ERROR_NONE;
17767         int rc = -1;
17768         sqlite3 *local_db_handle = NULL;
17769         char sql_query_string[QUERY_SIZE] = {0, };
17770
17771         if (input_full_query == NULL || input_source_table_name == NULL || input_new_table_name == NULL) {
17772                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17773                 error = EMAIL_ERROR_INVALID_PARAM;
17774                 goto FINISH_OFF;
17775         }
17776
17777         local_db_handle = emstorage_get_db_connection(multi_user_name);
17778
17779         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17780         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17781
17782         EM_DEBUG_LOG("[%s] will be replaced by [%s]", input_source_table_name, input_new_table_name);
17783
17784         EM_SAFE_STRNCPY(sql_query_string, input_full_query[input_query_index], sizeof(sql_query_string)-1); /*prevent 21984*/
17785         reg_replace(sql_query_string, input_source_table_name, input_new_table_name);
17786
17787         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17788 FINISH_OFF:
17789
17790         if (error == EMAIL_ERROR_NONE) {
17791                 EMSTORAGE_PROTECTED_FUNC_CALL(
17792                         sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17793         } else {
17794                 EMSTORAGE_PROTECTED_FUNC_CALL(
17795                         sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17796         }
17797
17798         EM_DEBUG_FUNC_END("error [%d]", error);
17799         return error;
17800 }
17801
17802 static int emstorage_add_column(char *multi_user_name, char *input_table_name, email_column_info_t *input_new_column)
17803 {
17804         EM_DEBUG_FUNC_BEGIN("input_table_name[%p] input_new_column[%p]", input_table_name, input_new_column);
17805         int error = EMAIL_ERROR_NONE;
17806         int rc = -1;
17807         sqlite3 *local_db_handle = NULL;
17808         char sql_query_string[QUERY_SIZE] = {0, };
17809
17810         if (input_table_name == NULL || input_new_column == NULL) {
17811                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17812                 error = EMAIL_ERROR_INVALID_PARAM;
17813                 goto FINISH_OFF;
17814         }
17815
17816         local_db_handle = emstorage_get_db_connection(multi_user_name);
17817
17818         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17819         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17820         SNPRINTF(sql_query_string, QUERY_SIZE, "ALTER TABLE %s ADD COLUMN %s %s;", input_table_name, input_new_column->column_name, input_new_column->column_type);
17821         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17822 FINISH_OFF:
17823
17824         if (error == EMAIL_ERROR_NONE) {
17825                 EMSTORAGE_PROTECTED_FUNC_CALL(
17826                         sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17827         } else {
17828                 EMSTORAGE_PROTECTED_FUNC_CALL(
17829                         sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17830         }
17831
17832         EM_DEBUG_FUNC_END("error [%d]", error);
17833         return error;
17834 }
17835
17836 static int emstorage_drop_table(char *multi_user_name, char *input_table_name)
17837 {
17838         EM_DEBUG_FUNC_BEGIN("input_table_name[%p]", input_table_name);
17839         int error = EMAIL_ERROR_NONE;
17840         int rc = -1;
17841         sqlite3 *local_db_handle = NULL;
17842         char sql_query_string[QUERY_SIZE] = {0, };
17843
17844         if (input_table_name == NULL) {
17845                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17846                 error = EMAIL_ERROR_INVALID_PARAM;
17847                 goto FINISH_OFF;
17848         }
17849
17850         local_db_handle = emstorage_get_db_connection(multi_user_name);
17851
17852         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17853         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17854         SNPRINTF(sql_query_string, QUERY_SIZE, "DROP TABLE %s;", input_table_name);
17855         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17856 FINISH_OFF:
17857
17858         if (error == EMAIL_ERROR_NONE) {
17859                 EMSTORAGE_PROTECTED_FUNC_CALL(
17860                         sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17861         } else {
17862                 EMSTORAGE_PROTECTED_FUNC_CALL(
17863                         sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17864         }
17865
17866         EM_DEBUG_FUNC_END("error [%d]", error);
17867         return error;
17868 }
17869
17870 gint glist_compare_column_name(gconstpointer old_column_info, gconstpointer new_column_info)
17871 {
17872         EM_DEBUG_FUNC_BEGIN("old_column_info[%p] new_column_info[%p]", old_column_info, new_column_info);
17873         email_column_info_t *left_one  = (email_column_info_t*)old_column_info;
17874         email_column_info_t *right_one = (email_column_info_t*)new_column_info;
17875
17876         if (old_column_info == NULL || new_column_info == NULL)
17877                 return -1;
17878
17879         return EM_SAFE_STRCMP((char*)left_one->column_name, (char*)right_one->column_name);
17880 }
17881
17882 INTERNAL_FUNC int emstorage_update_db_table_schema(char *multi_user_name)
17883 {
17884         EM_DEBUG_FUNC_BEGIN();
17885         int i = 0;
17886         int j = 0;
17887         int error = EMAIL_ERROR_NONE;
17888         int query_len = 0;
17889         email_column_info_t *new_column_info = NULL;
17890         email_column_info_t *p_column_info = NULL;
17891         char **create_table_query = NULL;
17892         GList *found_data = NULL;
17893         GList *column_list_of_old_table = NULL;
17894         GList *column_list_of_new_table = NULL;
17895         char table_names[CREATE_TABLE_MAX][2][50] = { { "mail_account_tbl", "mail_account_tbl_new" },
17896                 { "mail_box_tbl", "mail_box_tbl_new" },
17897                 { "mail_read_mail_uid_tbl", "mail_read_mail_uid_tbl_new" },
17898                 { "mail_rule_tbl", "mail_rule_tbl_new" },
17899                 { "mail_tbl", "mail_tbl_new" },
17900                 { "mail_attachment_tbl", "mail_attachment_tbl_new" },
17901 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
17902                 { "mail_partial_body_activity_tbl", "mail_partial_body_activity_tbl_new" },
17903 #else
17904                 { "", "" },
17905 #endif
17906                 { "mail_meeting_tbl", "mail_meeting_tbl_new" },
17907 #ifdef __FEATURE_LOCAL_ACTIVITY__
17908                 { "mail_local_activity_tbl", "mail_local_activity_tbl_new" },
17909 #else
17910                 { "", "" },
17911 #endif
17912                 { "mail_certificate_tbl", "mail_certificate_tbl_new" },
17913                 { "mail_task_tbl", "mail_task_tbl_new" },
17914 #ifdef __FEATURE_BODY_SEARCH__
17915                 { "mail_text_tbl", "mail_text_tbl_new" },
17916 #else
17917                 { "", "" },
17918 #endif
17919
17920 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
17921                 { "mail_auto_download_activity_tbl", "mail_auto_download_activity_tbl_new" }
17922 #else
17923                 { "", "" }
17924 #endif
17925
17926         };
17927
17928         error = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
17929
17930         if (error != EMAIL_ERROR_NONE) {
17931                 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
17932                 goto FINISH_OFF;
17933         }
17934
17935         if (query_len < CREATE_TABLE_MAX) {
17936                 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
17937                 error = EMAIL_ERROR_SYSTEM_FAILURE;
17938                 goto FINISH_OFF;
17939         }
17940
17941         for (i = CREATE_TABLE_MAIL_ACCOUNT_TBL; i < CREATE_TABLE_MAX; i++) {
17942                 EM_DEBUG_LOG("table [%s] new_table [%s]", table_names[i][0], table_names[i][1]);
17943                 if (EM_SAFE_STRLEN(table_names[i][0]) && EM_SAFE_STRLEN(table_names[i][1])) {
17944                         /* Check existing of _new table */
17945                         emstorage_drop_table(multi_user_name, table_names[i][1]);
17946                         error = emstorage_create_renamed_table(multi_user_name, create_table_query, i, table_names[i][0], table_names[i][1]);
17947                         if (error != EMAIL_ERROR_NONE) {
17948                                 EM_DEBUG_EXCEPTION("emstorage_create_renamed_table failed [%d]", error);
17949                                 goto FINISH_OFF;
17950                         }
17951
17952                         emstorage_get_column_information_from_table(multi_user_name, table_names[i][0], &column_list_of_old_table);
17953                         emstorage_get_column_information_from_table(multi_user_name, table_names[i][1], &column_list_of_new_table);
17954
17955                         /* Compare fields and add new field */
17956                         for (j = 0; j < g_list_length(column_list_of_new_table); j++) {
17957                                 new_column_info = (email_column_info_t*)g_list_nth_data(column_list_of_new_table, j);
17958                                 found_data = g_list_find_custom(column_list_of_old_table, (gconstpointer)new_column_info, glist_compare_column_name);
17959                                 if (found_data == NULL) {
17960                                         /* add new field*/
17961                                         emstorage_add_column(multi_user_name, table_names[i][0], new_column_info);
17962                                 }
17963                         }
17964
17965                         emstorage_drop_table(multi_user_name, table_names[i][1]);
17966                 } else {
17967                         EM_DEBUG_LOG("Skipped");
17968                 }
17969         }
17970
17971 FINISH_OFF:
17972         if (create_table_query) {
17973                 int i = 0;
17974                 for (i = 0; i < query_len; i++) {
17975                         if (create_table_query[i])
17976                                 EM_SAFE_FREE(create_table_query[i]);
17977                 }
17978                 EM_SAFE_FREE(create_table_query);
17979         }
17980
17981         found_data = g_list_first(column_list_of_old_table);
17982         while (found_data != NULL) {
17983                 p_column_info = (email_column_info_t *)found_data->data;
17984                 EM_SAFE_FREE(p_column_info->column_name);
17985                 EM_SAFE_FREE(p_column_info->column_type);
17986                 EM_SAFE_FREE(p_column_info);
17987
17988                 found_data = g_list_next(found_data);
17989         }
17990         g_list_free(column_list_of_old_table);
17991
17992         found_data = g_list_first(column_list_of_new_table);
17993         while (found_data != NULL) {
17994                 p_column_info = (email_column_info_t *)found_data->data;
17995                 EM_SAFE_FREE(p_column_info->column_name);
17996                 EM_SAFE_FREE(p_column_info->column_type);
17997                 EM_SAFE_FREE(p_column_info);
17998
17999                 found_data = g_list_next(found_data);
18000         }
18001         g_list_free(column_list_of_new_table);
18002
18003         EM_DEBUG_FUNC_END("error [%d]", error);
18004         return error;
18005 }
18006 #endif /* __FEATURE_UPDATE_DB_TABLE_SCHEMA__ */
18007
18008 /*EOF*/