fix svace and License issue
[platform/core/messaging/email-service.git] / email-core / email-storage / email-storage.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21
22
23 /******************************************************************************
24  * File: email-storage.c
25  * Desc: email-service Storage Library on Sqlite3
26  *
27  * Auth:
28  *
29  * History:
30  *      2007.03.02 : created
31  *****************************************************************************/
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <stdbool.h>
37 #include <time.h>
38 #include <sys/types.h>
39 #include <sys/time.h>
40 #include <sys/wait.h>
41 #include <glib.h>
42 #include <sys/stat.h>
43 #include <unistd.h>
44 #include <errno.h>
45 #include <dirent.h>
46 #include <vconf.h>
47 #include <sys/mman.h>
48 #include <fcntl.h>
49 #include <tzplatform_config.h>
50
51 #define __USE_UNIX98
52 #define __USE_GNU
53 #include <pthread.h>
54
55 #include "email-internal-types.h"
56 #include "email-convert.h"
57 #include "email-core-utils.h"
58 #include "email-utilities.h"
59 #include "email-storage.h"
60 #include "email-debug-log.h"
61 #include "email-types.h"
62 #include "email-convert.h"
63 #include "email-core-signal.h"
64 #include "email-core-event.h"
65 #include "email-core-container.h"
66 #include "email-core-gmime.h"
67 #include "email-core-key-manager.h"
68
69 #define SETTING_MEMORY_TEMP_FILE_PATH "/tmp/email_tmp_file"
70 #define CONTENT_DATA                  "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset="
71 #define CONTENT_TYPE_DATA             "<meta http-equiv=\"Content-Type\" content=\"text/html; charset="
72
73 #define FLAG_SEEN       0x01
74 #define FLAG_DELETED    0x02
75 #define FLAG_FLAGGED    0x04
76 #define FLAG_ANSWERED   0x08
77 #define FLAG_OLD        0x10
78 #define FLAG_DRAFT      0x20
79
80 #undef close
81
82 #define ISSUE_ORGANIZATION_LEN_IN_MAIL_CERTIFICATE_TBL  256
83 #define EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL       128
84 #define SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL      1027
85 #define FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL           256
86
87 #define ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL            50
88 #define RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL   50
89 #define EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL              128
90 #define USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL               50
91 #define PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL                50
92 #define SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL     50
93 #define SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL            50
94 #define SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL        50
95 #define DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL            30
96 #define REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL           128
97 #define RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL             128
98 #define LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL          256
99 #define DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL       256
100 #define SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL               256
101 #define MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL                128
102 #define ALIAS_LEN_IN_MAIL_BOX_TBL                       128
103 #define LOCAL_MBOX_LEN_IN_MAIL_READ_MAIL_UID_TBL        128
104 #define MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL      128
105 #define S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL             128
106 #define DATA2_LEN_IN_MAIL_READ_MAIL_UID_TBL             256
107 #define FILTER_NAME_LEN_IN_MAIL_RULE_TBL                256
108 #define VALUE_LEN_IN_MAIL_RULE_TBL                      256
109 #define VALUE2_LEN_IN_MAIL_RULE_TBL                     256
110 #define DEST_MAILBOX_LEN_IN_MAIL_RULE_TBL               128
111 #define MAILBOX_NAME_LEN_IN_MAIL_ATTACHMENT_TBL         128
112 #define ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL      256
113 #define ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL      256
114 #define CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL           256
115 #define ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL 128
116 #define MAILBOX_LEN_IN_MAIL_TBL                         128
117 #define SERVER_MAILBOX_LEN_IN_MAIL_TBL                  128
118 #define SERVER_MAIL_ID_LEN_IN_MAIL_TBL                  128
119 #define FROM_LEN_IN_MAIL_TBL                            256
120 #define SENDER_LEN_IN_MAIL_TBL                          256
121 #define REPLY_TO_LEN_IN_MAIL_TBL                        256
122 #define TO_LEN_IN_MAIL_TBL                              3999
123 #define CC_LEN_IN_MAIL_TBL                              3999
124 #define BCC_LEN_IN_MAIL_TBL                             3999
125 #define RETURN_PATH_LEN_IN_MAIL_TBL                     3999
126 #define SUBJECT_LEN_IN_MAIL_TBL                         1027
127 #define THREAD_TOPIC_LEN_IN_MAIL_TBL                    256
128 #define TEXT_1_LEN_IN_MAIL_TBL                          256
129 #define TEXT_2_LEN_IN_MAIL_TBL                          256
130 #define MIME_ENTITY_LEN_IN_MAIL_TBL                     256
131 #define DATETIME_LEN_IN_MAIL_TBL                        128
132 #define MESSAGE_ID_LEN_IN_MAIL_TBL                      256
133 #define FROM_CONTACT_NAME_LEN_IN_MAIL_TBL               3999
134 #define FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL              3999
135 #define TO_CONTACT_NAME_LEN_IN_MAIL_TBL                 3999
136 #define TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL                3999
137 #define MAILBOX_LEN_IN_MAIL_MEETING_TBL                 128
138 #define LOCATION_LEN_IN_MAIL_MEETING_TBL                1024
139 #define GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL        512
140 #define STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL           32
141 #define DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL           32
142 #define PREVIEWBODY_LEN_IN_MAIL_TBL                     512
143 #define CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL        256
144
145 /*  this define is used for query to change data (delete, insert, update) */
146 #define EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction_flag, error_code) \
147         do { \
148                 if (transaction_flag) { \
149                         if (emstorage_begin_transaction(multi_user_name, NULL, NULL, &error_code) == false) { \
150                                 EM_DEBUG_EXCEPTION("emstorage_begin_transaction error [%d]", error_code); \
151                                 goto FINISH_OFF; \
152                         } \
153                 } \
154         } while (0)
155
156 /*  this define is used for query to change data (delete, insert, update) */
157 #define EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction_flag, result_code, error_code) \
158         do { \
159                 if (transaction_flag) { \
160                         if (result_code == true) { \
161                                 if (emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL) == false) { \
162                                         EM_DEBUG_EXCEPTION("emstorage_commit_transaction error"); \
163                                         error_code = EMAIL_ERROR_DB_FAILURE; \
164                                         result_code = false; \
165                                 } \
166                         } \
167                         else { \
168                                 if (emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL) == false) { \
169                                         EM_DEBUG_EXCEPTION("emstorage_rollback_transaction error"); \
170                                         error_code = EMAIL_ERROR_DB_FAILURE; \
171                                 } \
172                         } \
173                 } \
174         } while (0)
175
176 /*  this define is used for query to read (select) */
177 #define EMSTORAGE_START_READ_TRANSACTION(transaction_flag) \
178         if (transaction_flag) { \
179                 /*_timedlock_shm_mutex(mapped_for_db_lock, 2);*/\
180         }
181
182 /*  this define is used for query to read (select) */
183 #define EMSTORAGE_FINISH_READ_TRANSACTION(transaction_flag) \
184         if (transaction_flag) { \
185                 /*_unlockshm_mutex(mapped_for_db_lock);*/\
186         }
187
188 /*  for safety DB operation */
189 static pthread_mutex_t _db_handle_lock = PTHREAD_MUTEX_INITIALIZER;
190
191 /* for safety secure-storage operation */
192 static pthread_mutex_t _ss_handle_lock = PTHREAD_MUTEX_INITIALIZER;
193
194 #define _MULTIPLE_DB_HANDLE
195
196 #ifdef _MULTIPLE_DB_HANDLE
197
198 typedef struct {
199     char *user_name;
200         pthread_t       thread_id;
201         sqlite3 *db_handle;
202 } db_handle_t;
203
204 #define MAX_DB_CLIENT 100
205
206 /* static int _db_handle_count = 0; */
207 db_handle_t _db_handle_list[MAX_DB_CLIENT] = {{NULL, 0, 0}, };
208
209
210 sqlite3 *emstorage_get_db_handle(char *multi_user_name)
211 {
212         EM_DEBUG_FUNC_BEGIN();
213         int i;
214         pthread_t current_thread_id = THREAD_SELF();
215         sqlite3 *result_db_handle = NULL;
216
217         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
218                 ENTER_CRITICAL_SECTION(_db_handle_lock);
219                 for (i = 0; i < MAX_DB_CLIENT; i++) {
220                         if (EM_SAFE_STRCASECMP(_db_handle_list[i].user_name, multi_user_name) != 0)
221                                 continue;
222
223                         if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id)) {
224                                 EM_DEBUG_LOG_DEV("found db handle at [%d]", i);
225                                 result_db_handle = _db_handle_list[i].db_handle;
226                                 break;
227                         }
228                 }
229                 LEAVE_CRITICAL_SECTION(_db_handle_lock);
230         } else {
231                 ENTER_CRITICAL_SECTION(_db_handle_lock);
232                 for (i = 0; i < MAX_DB_CLIENT; i++) {
233                         if (EM_SAFE_STRLEN(_db_handle_list[i].user_name) > 0)
234                                 continue;
235
236                         if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id)) {
237                                 EM_DEBUG_LOG_DEV("found db handle at [%d]", i);
238                                 result_db_handle = _db_handle_list[i].db_handle;
239                                 break;
240                         }
241                 }
242                 LEAVE_CRITICAL_SECTION(_db_handle_lock);
243         }
244
245         if (!result_db_handle)
246                 EM_DEBUG_LOG("no db_handle for [%d] found", current_thread_id);
247
248         EM_DEBUG_FUNC_END();
249         return result_db_handle;
250 }
251
252 int emstorage_set_db_handle(char *multi_user_name, sqlite3 *db_handle)
253 {
254         EM_DEBUG_FUNC_BEGIN();
255         int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
256         pthread_t current_thread_id = THREAD_SELF();
257
258         ENTER_CRITICAL_SECTION(_db_handle_lock);
259         for (i = 0; i < MAX_DB_CLIENT; i++)     {
260                 if (_db_handle_list[i].thread_id == 0) {
261                         _db_handle_list[i].thread_id = current_thread_id;
262                         _db_handle_list[i].db_handle = db_handle;
263             /* Only distinguished container and host  */
264             _db_handle_list[i].user_name = EM_SAFE_STRDUP(multi_user_name);
265                         EM_DEBUG_LOG("current_thread_id [%d], index [%d]", current_thread_id, i);
266                         error_code =  EMAIL_ERROR_NONE;
267                         break;
268                 }
269         }
270         LEAVE_CRITICAL_SECTION(_db_handle_lock);
271
272         if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
273                 EM_DEBUG_EXCEPTION("Exceeded the limitation of db client. Can't find empty slot in _db_handle_list.");
274
275         EM_DEBUG_FUNC_END("error_code [%d]", error_code);
276         return error_code;
277 }
278
279 int emstorage_remove_db_handle()
280 {
281         EM_DEBUG_FUNC_BEGIN();
282         int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
283         ENTER_CRITICAL_SECTION(_db_handle_lock);
284         for (i = 0; i < MAX_DB_CLIENT; i++) {
285                 if (_db_handle_list[i].thread_id == THREAD_SELF()) {
286                         _db_handle_list[i].thread_id = 0;
287                         _db_handle_list[i].db_handle = NULL;
288             EM_SAFE_FREE(_db_handle_list[i].user_name);
289
290                         EM_DEBUG_LOG("index [%d]", i);
291                         error_code = EMAIL_ERROR_NONE;
292                         break;
293                 }
294         }
295         LEAVE_CRITICAL_SECTION(_db_handle_lock);
296
297         if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
298                 EM_DEBUG_EXCEPTION("Can't find proper thread_id");
299
300         EM_DEBUG_FUNC_END("error_code [%d]", error_code);
301         return error_code;
302 }
303
304 int emstorage_reset_db_handle_list()
305 {
306         EM_DEBUG_FUNC_BEGIN();
307         int i;
308
309         ENTER_CRITICAL_SECTION(_db_handle_lock);
310         for (i = 0; i < MAX_DB_CLIENT; i++) {
311                 _db_handle_list[i].thread_id = 0;
312                 _db_handle_list[i].db_handle = NULL;
313         EM_SAFE_FREE(_db_handle_list[i].user_name);
314         }
315         LEAVE_CRITICAL_SECTION(_db_handle_lock)
316
317         EM_DEBUG_FUNC_END();
318         return EMAIL_ERROR_NONE;
319 }
320
321
322 sqlite3 *emstorage_get_db_connection(char *multi_user_name)
323 {
324         return emstorage_db_open(multi_user_name, NULL);
325 }
326
327 #else   /*  _MULTIPLE_DB_HANDLE */
328
329 sqlite3 *_db_handle = NULL;
330
331 sqlite3 *emstorage_get_db_connection()
332 {
333         if (NULL == _db_handle)
334                 emstorage_db_open(NULL);
335         return _db_handle;
336 }
337 #endif  /*  _MULTIPLE_DB_HANDLE */
338
339 /* ------------------------------------------------------------------------------ */
340 /*  Mutex using shared memory */
341 typedef struct {
342         pthread_mutex_t mutex;
343         int data;
344 } mmapped_t;
345
346 mmapped_t       *mapped_for_db_lock = NULL;
347 int              shm_fd_for_db_lock = 0;
348
349 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
350 mmapped_t       *mapped_for_generating_mail_id = NULL;
351 int              shm_fd_for_generating_mail_id = 0;
352 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
353
354 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__
355 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
356         do {\
357                 _timedlock_shm_mutex(mapped_for_db_lock, 2);\
358                 return_value = function_call;\
359                 _unlockshm_mutex(mapped_for_db_lock);\
360         } while (0)
361
362 #else /*  __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
363 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
364         {  return_value = function_call; }
365 #endif /*  __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
366
367 static int emstorage_exec_query_by_prepare_v2(sqlite3 *local_db_handle, char *query_string)
368 {
369         EM_DEBUG_FUNC_BEGIN("local_db_handle[%p] query_string[%p]", local_db_handle, query_string);
370         int error = EMAIL_ERROR_NONE;
371         int rc = 0;
372         DB_STMT db_statement = NULL;
373
374         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, query_string, EM_SAFE_STRLEN(query_string), &db_statement, NULL), rc);
375         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
376                 ("sqlite3_prepare failed [%d] [%s]", rc, query_string));
377
378         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(db_statement), rc);
379         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
380         ("sqlite3_step failed [%d] [%s]", rc, query_string));
381
382 FINISH_OFF:
383
384         if (db_statement != NULL) {
385                 rc = sqlite3_finalize(db_statement);
386                 if (rc != SQLITE_OK) {
387                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
388                         error = EMAIL_ERROR_DB_FAILURE;
389                 }
390         }
391
392         return error;
393 }
394
395 INTERNAL_FUNC int emstorage_shm_file_init(const char *shm_file_name)
396 {
397         EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
398         char errno_buf[ERRNO_BUF_SIZE] = {0};
399
400         if (!shm_file_name) {
401                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
402                 return EMAIL_ERROR_INVALID_PARAM;
403         }
404
405         int fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); /*  note: permission is not working */
406         if (fd < 0) {
407                 EM_DEBUG_EXCEPTION("shm_open errno [%d] [%s]", errno, EM_STRERROR(errno_buf));
408                 return EMAIL_ERROR_SYSTEM_FAILURE;
409         }
410
411         fchmod(fd, 0666);
412         EM_DEBUG_LOG("** Create SHM FILE **");
413         if (ftruncate(fd, sizeof(mmapped_t)) != 0) {
414                 EM_DEBUG_EXCEPTION("ftruncate errno [%d]", errno);
415                 return EMAIL_ERROR_SYSTEM_FAILURE;
416         }
417
418         mmapped_t *m = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
419         if (m == MAP_FAILED) {
420                 EM_DEBUG_EXCEPTION("mmap errno [%d]", errno);
421                 EM_SAFE_CLOSE(fd);
422                 return EMAIL_ERROR_SYSTEM_FAILURE;
423         }
424
425         m->data = 0;
426
427         pthread_mutexattr_t mattr;
428         pthread_mutexattr_init(&mattr);
429         pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
430         pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
431         pthread_mutex_init(&(m->mutex), &mattr);
432         pthread_mutexattr_destroy(&mattr);
433
434     pthread_mutex_destroy(&(m->mutex));
435     munmap(m, sizeof(mmapped_t));
436
437         EM_SAFE_CLOSE(fd);
438         EM_DEBUG_FUNC_END();
439         return EMAIL_ERROR_NONE;
440 }
441
442 int emstorage_shm_file_destroy(const char *shm_file_name)
443 {
444         EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
445         char errno_buf[ERRNO_BUF_SIZE] = {0};
446
447         if (!shm_file_name) {
448                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
449                 return EMAIL_ERROR_INVALID_PARAM;
450         }
451
452         if (shm_unlink(shm_file_name) != 0)
453                 EM_DEBUG_EXCEPTION("shm_unlink failed: %s", EM_STRERROR(errno_buf));
454         EM_DEBUG_FUNC_END();
455         return EMAIL_ERROR_NONE;
456 }
457
458 int _initialize_shm_mutex(const char *shm_file_name, int *param_shm_fd, mmapped_t **param_mapped)
459 {
460         EM_DEBUG_FUNC_BEGIN("shm_file_name [%p] param_shm_fd [%p], param_mapped [%p]", shm_file_name, param_shm_fd, param_mapped);
461         char errno_buf[ERRNO_BUF_SIZE] = {0};
462
463         if (!shm_file_name || !param_shm_fd || !param_mapped) {
464                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
465                 return EMAIL_ERROR_INVALID_PARAM;
466         }
467
468         if (!(*param_mapped)) {
469                 EM_DEBUG_LOG("** mapping begin **");
470                 if (!(*param_shm_fd)) { /*  open shm_file_name at first. Otherwise, the num of files in /proc/pid/fd will be increasing  */
471                         *param_shm_fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
472                         if ((*param_shm_fd) == -1) {
473                                 EM_DEBUG_EXCEPTION("FAIL: shm_open(): %s", EM_STRERROR(errno_buf));
474                                 return EMAIL_ERROR_SYSTEM_FAILURE;
475                         }
476                 }
477
478         fchmod((*param_shm_fd), 0666);
479         EM_DEBUG_LOG("** Create SHM FILE **");
480         if (ftruncate((*param_shm_fd), sizeof(mmapped_t)) != 0) {
481             EM_DEBUG_EXCEPTION("ftruncate errno [%d]", errno);
482             return EMAIL_ERROR_SYSTEM_FAILURE;
483         }
484
485                 mmapped_t *tmp = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ|PROT_WRITE, MAP_SHARED, (*param_shm_fd), 0);
486                 if (tmp == MAP_FAILED) {
487                         EM_DEBUG_EXCEPTION("mmap failed: %s", EM_STRERROR(errno_buf));
488                         return EMAIL_ERROR_SYSTEM_FAILURE;
489                 }
490
491         tmp->data = 0;
492
493         pthread_mutexattr_t mattr;
494         pthread_mutexattr_init(&mattr);
495         pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
496         pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
497         pthread_mutex_init(&(tmp->mutex), &mattr);
498         pthread_mutexattr_destroy(&mattr);
499
500                 *param_mapped = tmp;
501         }
502
503         EM_DEBUG_FUNC_END();
504         return EMAIL_ERROR_NONE;
505 }
506
507 int _timedlock_shm_mutex(mmapped_t *param_mapped, int sec)
508 {
509         EM_DEBUG_FUNC_BEGIN("param_mapped [%p], sec [%d]", param_mapped, sec);
510
511         if (!param_mapped) {
512                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
513                 return EMAIL_ERROR_INVALID_PARAM;
514         }
515
516         struct timespec abs_time;
517         clock_gettime(CLOCK_REALTIME, &abs_time);
518         abs_time.tv_sec += sec;
519         char errno_buf[ERRNO_BUF_SIZE] = {0};
520
521         int err = pthread_mutex_timedlock(&(param_mapped->mutex), &abs_time);
522
523         if (err == EOWNERDEAD) {
524                 err = pthread_mutex_consistent(&(param_mapped->mutex));
525                 EM_DEBUG_EXCEPTION("Previous owner is dead with lock. Fix mutex : %s", EM_STRERROR(errno_buf));
526         } else if (err != 0) {
527                 EM_DEBUG_EXCEPTION("ERROR : %s", EM_STRERROR(errno_buf));
528                 return err;
529         }
530
531         EM_DEBUG_FUNC_END();
532         return EMAIL_ERROR_NONE;
533 }
534
535 void _unlockshm_mutex(mmapped_t *param_mapped)
536 {
537         EM_DEBUG_FUNC_BEGIN();
538         pthread_mutex_unlock(&(param_mapped->mutex));
539         EM_DEBUG_FUNC_END();
540 }
541 /* ------------------------------------------------------------------------------ */
542
543
544 static int _open_counter = 0;
545
546 static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name);
547 static int _read_password_from_secure_storage(char *file_name, char **password);
548
549 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
550 static int _get_cert_password_file_name(int index, char *cert_password_file_name);
551 #endif
552
553 typedef struct {
554         const char *object_name;
555         unsigned int data_flag;
556 } email_db_object_t;
557
558 static const email_db_object_t _g_db_tables[] = {
559         { "mail_read_mail_uid_tbl", 1},
560         { "mail_tbl", 1},
561         { "mail_attachment_tbl", 1},
562         { NULL,  0},
563 };
564
565 static const email_db_object_t _g_db_indexes[] = {
566         { "mail_read_mail_uid_idx1", 1},
567         { "mail_idx1", 1},
568         { "mail_attachment_idx1", 1},
569         { NULL,  1},
570 };
571
572 enum {
573         CREATE_TABLE_MAIL_ACCOUNT_TBL,
574         CREATE_TABLE_MAIL_BOX_TBL,
575         CREATE_TABLE_MAIL_READ_MAIL_UID_TBL,
576         CREATE_TABLE_MAIL_RULE_TBL,
577         CREATE_TABLE_MAIL_TBL,
578         CREATE_TABLE_MAIL_ATTACHMENT_TBL,
579 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
580         CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL,
581 #else
582         CREATE_TABLE_DUMMY_INDEX1,
583 #endif
584         CREATE_TABLE_MAIL_MEETING_TBL,
585 #ifdef __FEATURE_LOCAL_ACTIVITY__
586         CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL,
587 #else
588         CREATE_TABLE_DUMMY_INDEX2,
589 #endif
590         CREATE_TABLE_MAIL_CERTIFICATE_TBL,
591         CREATE_TABLE_MAIL_TASK_TBL,
592 #ifdef __FEATURE_BODY_SEARCH__
593         CREATE_TABLE_MAIL_TEXT_TBL,
594 #else
595         CREATE_TABLE_DUMMY_INDEX3,
596 #endif
597
598 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
599         CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
600 #else
601         CREATE_TABLE_DUMMY_INDEX4,
602 #endif
603
604         /*CREATE INDEX*/
605         CREATE_TABLE_MAIL_ACCOUNT_IDX,
606         CREATE_TABLE_MAIL_BOX_IDX,
607         CREATE_TABLE_MAIL_READ_MAIL_UID_IDX,
608         CREATE_TABLE_MAIL_IDX,
609         CREATE_TABLE_MAIL_ATTACHMENT_IDX,
610         CREATE_TABLE_MAIL_MEETING_IDX,
611         CREATE_TABLE_MAIL_TASK_IDX,
612         CREATE_TABLE_MAIL_DATETIME_IDX,
613         CREATE_TABLE_MAIL_THREAD_IDX,
614         CREATE_TABLE_MAX,
615 };
616
617 enum {
618         DATA1_IDX_IN_MAIL_ACTIVITY_TBL = 0,
619         TRANSTYPE_IDX_IN_MAIL_ACTIVITY_TBL,
620         FLAG_IDX_IN_MAIL_ACTIVITY_TBL,
621 };
622
623 enum {
624         CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL = 0,
625         ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
626         ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
627         ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
628         EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
629         EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
630         EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
631         ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL,
632         EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL,
633         SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL,
634         FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL,
635 };
636
637 enum {
638         TO_RECIPIENT = 0,
639         CC_RECIPIENT,
640         BCC_RECIPIENT,
641 };
642 enum {
643         ACCOUNT_ID_IDX_IN_MAIL_BOX_TBL = 0,
644         LOCAL_YN_IDX_IN_MAIL_BOX_TBL,
645         MAILBOX_NAME_IDX_IN_MAIL_BOX_TBL,
646         MAILBOX_TYPE_IDX_IN_MAIL_BOX_TBL,
647         ALIAS_IDX_IN_MAIL_BOX_TBL,
648         SYNC_WITH_SERVER_YN_IDX_IN_MAIL_BOX_TBL,
649         MODIFIABLE_YN_IDX_IN_MAIL_BOX_TBL,
650         TOTAL_MAIL_COUNT_ON_SERVER_IDX_IN_MAIL_BOX_TBL,
651         ARCHIVE_IDX_IN_MAIL_BOX_TBL,
652         MAIL_SLOT_SIZE_IDX_IN_MAIL_BOX_TBL,
653 };
654
655 enum {
656         ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL = 0,
657         LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
658         MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL,
659         LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
660         SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
661         RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL,
662         SYNC_STATUS_IDX_IN_MAIL_READ_MAIL_UID_TBL,
663         FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL,
664         FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL,
665         IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL,          /* unused */
666 };
667
668 #ifdef __FEATURE_BODY_SEARCH__
669 enum {
670         MAIL_ID_IDX_IN_MAIL_TEXT_TBL = 0,
671         ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL,
672         MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL,
673         BODY_TEXT_IDX_IN_MAIL_TEXT_TBL,
674         FIELD_COUNT_OF_MAIL_TEXT_TBL,
675 };
676 #endif
677
678 enum {
679         ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL = 0,
680         RULE_ID_IDX_IN_MAIL_RULE_TBL,
681         FILTER_NAME_IDX_IN_MAIL_RULE_TBL,
682         TYPE_IDX_IN_MAIL_RULE_TBL,
683         VALUE_IDX_IN_MAIL_RULE_TBL,
684         VALUE2_IDX_IN_MAIL_RULE_TBL,
685         ACTION_TYPE_IDX_IN_MAIL_RULE_TBL,
686         TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL,
687         FLAG1_IDX_IN_MAIL_RULE_TBL,
688         FLAG2_IDX_IN_MAIL_RULE_TBL,
689 };
690
691 enum {
692         MAIL_ID_IDX_IN_MAIL_TBL = 0,
693         ACCOUNT_ID_IDX_IN_MAIL_TBL,
694         MAILBOX_ID_IDX_IN_MAIL_TBL,
695         MAILBOX_TYPE_IDX_IN_MAIL_TBL,
696         SUBJECT_IDX_IN_MAIL_TBL,
697         DATETIME_IDX_IN_MAIL_TBL,
698         SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL,
699         SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL,
700         SERVER_MAIL_ID_IDX_IN_MAIL_TBL,
701         MESSAGE_ID_IDX_IN_MAIL_TBL,
702         REFERENCE_ID_IDX_IN_MAIL_TBL,
703         FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL,
704         FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL,
705         FULL_ADDRESS_TO_IDX_IN_MAIL_TBL,
706         FULL_ADDRESS_CC_IDX_IN_MAIL_TBL,
707         FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL,
708         FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL,
709         EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL,
710         EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL,
711         ALIAS_SENDER_IDX_IN_MAIL_TBL,
712         ALIAS_RECIPIENT_IDX_IN_MAIL_TBL,
713         BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL,
714         FILE_PATH_PLAIN_IDX_IN_MAIL_TBL,
715         FILE_PATH_HTML_IDX_IN_MAIL_TBL,
716         FILE_PATH_MIME_ENTITY_IDX_IN_MAIL_TBL,
717         MAIL_SIZE_IDX_IN_MAIL_TBL,
718         FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL,
719         FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL,
720         FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL,
721         FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL,
722         FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL,
723         FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL,
724         FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL,
725         DRM_STATUS_IDX_IN_MAIL_TBL,
726         PRIORITY_IDX_IN_MAIL_TBL,
727         SAVE_STATUS_IDX_IN_MAIL_TBL,
728         LOCK_STATUS_IDX_IN_MAIL_TBL,
729         REPORT_STATUS_IDX_IN_MAIL_TBL,
730         ATTACHMENT_COUNT_IDX_IN_MAIL_TBL,
731         INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL,
732         THREAD_ID_IDX_IN_MAIL_TBL,
733         THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL,
734         PREVIEW_TEXT_IDX_IN_MAIL_TBL,
735         MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL,
736         MESSAGE_CLASS_IDX_IN_MAIL_TBL,
737         DIGEST_TYPE_IDX_IN_MAIL_TBL,
738         SMIME_TYPE_IDX_IN_MAIL_TBL,
739         SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL,
740         REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL,
741         TAG_ID_IDX_IN_MAIL_TBL,
742         REPLIED_TIME_IDX_IN_MAIL_TBL,
743         FORWARDED_TIME_IDX_IN_MAIL_TBL,
744         DEFAULT_CHARSET_IDX_IN_MAIL_TBL,
745         EAS_DATA_LENGTH_IDX_IN_MAIL_TBL,
746         EAS_DATA_IDX_IN_MAIL_TBL,
747         USER_NAME_IDX_IN_MAIL_TBL,
748         FIELD_COUNT_OF_MAIL_TBL,  /* End of mail_tbl */
749 };
750
751 enum {
752         ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL = 0,
753         ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL,
754         ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL,
755         CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
756         ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL,
757         MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
758         ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
759         MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
760         ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
761         ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
762         ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL,
763         ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
764         ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
765 #ifdef __ATTACHMENT_OPTI__
766         ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL,
767         SECTION_IDX_IN_MAIL_ATTACHMENT_TBL,
768 #endif
769 };
770
771 enum {
772         IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL = 0,
773 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
774         MAIL_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
775         ACCOUNT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
776         ADDRESS_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
777         ADDRESS_IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL,
778 #endif
779         ADDRESS_IDX_IN_MAIL_CONTACT_SYNC_TBL,
780         CONTACT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
781         STORAGE_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
782         CONTACT_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
783 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
784         DISPLAY_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
785         FLAG1_IDX_IN_MAIL_CONTACT_SYNC_TBL,
786 #endif
787 };
788
789 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
790 enum {
791         ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL = 0,
792         STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
793         ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
794         MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
795         SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
796         MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
797     MULTI_USER_NAME_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
798 };
799 #endif
800
801 /* sowmya.kr 03032010, changes for get list of mails for given addr list */
802 typedef struct _em_mail_id_list {
803         int mail_id;
804         struct _em_mail_id_list *next;
805 } em_mail_id_list;
806
807 static char *g_test_query[] = {
808                 /*  1. select mail_account_tbl */
809                 "SELECT"
810                 " account_name, "
811                 " incoming_server_type, "
812                 " incoming_server_address, "
813                 " user_email_address, "
814                 " incoming_server_user_name, "
815                 " incoming_server_password, "
816                 " retrieval_mode, "
817                 " incoming_server_port_number, "
818                 " incoming_server_secure_connection, "
819                 " incoming_server_authentication_method,"
820                 " outgoing_server_type, "
821                 " outgoing_server_address, "
822                 " outgoing_server_port_number, "
823                 " outgoing_server_need_authentication, "
824                 " outgoing_server_secure_connection, "
825                 " outgoing_server_user_name, "
826                 " outgoing_server_password, "
827                 " display_name, "
828                 " reply_to_addr, "
829                 " return_addr, "
830                 " account_id, "
831                 " keep_mails_on_pop_server_after_download, "
832                 " auto_resend_times, "
833                 " outgoing_server_size_limit, "
834                 " wifi_auto_download, "
835                 " pop_before_smtp, "
836                 " incoming_server_requires_apop,"
837                 " logo_icon_path, "
838                 " is_preset_account, "
839                 " check_interval, "
840                 " priority, "
841                 " keep_local_copy, "
842                 " req_delivery_receipt, "
843                 " req_read_receipt, "
844                 " download_limit, "
845                 " block_address, "
846                 " block_subject, "
847                 " display_name_from, "
848                 " reply_with_body, "
849                 " forward_with_files, "
850                 " add_myname_card, "
851                 " add_signature, "
852                 " signature"
853                 ", add_my_address_to_bcc"
854                 ", notification_status "
855                 ", vibrate_status "
856                 ", display_content_status "
857                 ", default_ringtone_status "
858                 ", alert_ringtone_path "
859                 ", account_svc_id "
860                 ", index_color "
861                 ", sync_status "
862                 ", sync_disabled "
863                 ", smime_type"
864                 ", certificate_path"
865                 ", cipher_type"
866                 ", digest_type"
867                 " FROM mail_account_tbl",
868                 /*  2. select mail_box_tbl */
869                 "SELECT "
870                 "   mailbox_id, "
871                 "   account_id, "
872                 "   local_yn,  "
873                 "   mailbox_name,  "
874                 "   mailbox_type,   "
875                 "   alias,  "
876                 "   deleted_flag,  "
877                 "   modifiable_yn,  "
878                 "   total_mail_count_on_server,  "
879                 "   has_archived_mails, "
880                 "   mail_slot_size, "
881                 "   no_select, "
882                 "   last_sync_time "
883                 " FROM mail_box_tbl ",
884                 /*  3. select mail_read_mail_uid_tbl */
885                 "SELECT  "
886                 "   account_id,  "
887                 "   mailbox_id,  "
888                 "   mailbox_name,  "
889                 "   local_uid,  "
890                 "   server_uid,  "
891                 "   rfc822_size ,  "
892                 "   sync_status,  "
893                 "   flags_seen_field,  "
894                 "   idx_num "
895                 " FROM mail_read_mail_uid_tbl ",
896                 /*  4. select mail_rule_tbl */
897                 "SELECT "
898                 "   account_id, "
899                 "   rule_id, "
900                 "   filter_name, "
901                 "   type, "
902                 "   value, "
903                 "   value2, "
904                 "   action_type, "
905                 "   target_mailbox_id,  "
906                 "   flag1, "
907                 "   flag2 "
908                 " FROM mail_rule_tbl    ",
909                 /*  5. select mail_tbl */
910                 "SELECT"
911                 "       mail_id, "
912                 "       account_id, "
913                 "       mailbox_id, "
914                 "       mailbox_name, "
915                 "   mailbox_type, "
916                 "   subject, "
917                 "       date_time, "
918                 "       server_mail_status, "
919                 "       server_mailbox_name, "
920                 "       server_mail_id, "
921                 "   message_id, "
922                 "       reference_mail_id, "
923                 "   full_address_from, "
924                 "   full_address_reply, "
925                 "   full_address_to, "
926                 "   full_address_cc, "
927                 "   full_address_bcc, "
928                 "   full_address_return, "
929                 "   email_address_sender, "
930                 "   email_address_recipient, "
931                 "   alias_sender, "
932                 "   alias_recipient, "
933                 "       body_download_status, "
934                 "       file_path_plain, "
935                 "       file_path_html, "
936                 "   file_path_mime_entity, "
937                 "       mail_size, "
938                 "   flags_seen_field     ,"
939                 "   flags_deleted_field  ,"
940                 "   flags_flagged_field  ,"
941                 "   flags_answered_field ,"
942                 "   flags_recent_field   ,"
943                 "   flags_draft_field    ,"
944                 "   flags_forwarded_field,"
945                 "       DRM_status, "
946                 "       priority, "
947                 "       save_status, "
948                 "       lock_status, "
949                 "       report_status, "
950                 "   attachment_count, "
951                 "       inline_content_count, "
952                 "       thread_id, "
953                 "       thread_item_count, "
954                 "   preview_text, "
955                 "       meeting_request_status, "
956                 "   message_class, "
957                 "   digest_type, "
958                 "   smime_type "
959                 " FROM mail_tbl",
960                 /*  6. select mail_attachment_tbl */
961                 "SELECT "
962                 "       attachment_id, "
963                 "       attachment_name, "
964                 "       attachment_path, "
965                 "       attachment_size, "
966                 "       mail_id,  "
967                 "       account_id, "
968                 "       mailbox_id, "
969                 "       attachment_save_status,  "
970                 "       attachment_drm_type,  "
971                 "       attachment_drm_method,  "
972                 "       attachment_inline_content_status,  "
973                 "       attachment_mime_type  "
974                 " FROM mail_attachment_tbl ",
975
976 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
977                 "SELECT  "
978                 "   account_id, "
979                 "   mail_id, "
980                 "   server_mail_id, "
981                 "   activity_id, "
982                 "   activity_type, "
983                 "   mailbox_id, "
984                 "   mailbox_name "
985                 "   multi_user_name "
986                 " FROM mail_partial_body_activity_tbl ",
987 #endif
988
989                 "SELECT  "
990                 "   mail_id, "
991                 "   account_id, "
992                 "   mailbox_id, "
993                 "   meeting_response, "
994                 "   start_time, "
995                 "   end_time, "
996                 "   location, "
997                 "   global_object_id, "
998                 "   offset, "
999                 "   standard_name, "
1000                 "   standard_time_start_date, "
1001                 "   standard_bias, "
1002                 "   daylight_name, "
1003                 "   daylight_time_start_date, "
1004                 "   daylight_bias "
1005                 " FROM mail_meeting_tbl ",
1006
1007 #ifdef __FEATURE_LOCAL_ACTIVITY__
1008                 "SELECT "
1009                 "       activity_id, "
1010                 "   account_id, "
1011                 "   mail_id, "
1012                 "   activity_type, "
1013                 "   server_mailid, "
1014                 "   src_mbox , "
1015                 "       dest_mbox "
1016                 " FROM mail_local_activity_tbl  ",
1017 #endif
1018                 "SELECT "
1019                 "       certificate_id, "
1020                 "   issue_year, "
1021                 "   issue_month, "
1022                 "   issue_day, "
1023                 "   expiration_year, "
1024                 "   expiration_month, "
1025                 "   expiration_day, "
1026                 "   issue_organization_name, "
1027                 "   email_address, "
1028                 "   subject_str, "
1029                 "   filepath, "
1030                 "   password "
1031                 " FROM mail_certificate_tbl     ",
1032                 "SELECT "
1033                 "       task_id, "
1034                 "   task_type, "
1035                 "   task_status, "
1036                 "   task_priority, "
1037                 "   task_parameter_length, "
1038                 "   task_parameter , "
1039                 "       date_time "
1040                 " FROM mail_task_tbl    ",
1041 #ifdef __FEATURE_BODY_SEARCH__
1042                 "SELECT "
1043                 "       mail_id, "
1044                 "   account_id, "
1045                 "   mailbox_id, "
1046                 "   body_text "
1047                 " FROM mail_text_tbl    ",
1048 #endif
1049 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
1050                 "SELECT  "
1051                 "   activity_id, "
1052                 "   status, "
1053                 "   account_id, "
1054                 "   mail_id, "
1055                 "   server_mail_id, "
1056                 "   mailbox_id, "
1057                 "   multi_user_name, "
1058                 " FROM mail_auto_download_activity_tbl ",
1059 #endif
1060                 NULL,
1061 };
1062
1063 int _field_count_of_table[CREATE_TABLE_MAX] = { 0, };
1064
1065 static int _get_table_field_data_char(char  **table, char *buf, int index)
1066 {
1067         if ((table == NULL) || (buf == NULL) || (index < 0)) {
1068                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1069                 return false;
1070         }
1071
1072         if (table[index] != NULL) {
1073                 *buf = (char)atoi(table[index]);
1074                 return true;
1075         }
1076
1077         /*  EM_DEBUG_LOG("Empty field. Set as zero"); */
1078
1079         *buf = 0;
1080         return false;
1081 }
1082
1083 static int _get_table_field_data_int(char  **table, int *buf, int index)
1084 {
1085         if ((table == NULL) || (buf == NULL) || (index < 0)) {
1086                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1087                 return false;
1088         }
1089
1090         if (table[index] != NULL) {
1091                 *buf = atoi(table[index]);
1092                 return true;
1093         }
1094
1095         /*  EM_DEBUG_LOG("Empty field. Set as zero"); */
1096
1097         *buf = 0;
1098         return false;
1099 }
1100
1101 static int _get_table_field_data_time_t(char  **table, time_t *buf, int index)
1102 {
1103         if ((table == NULL) || (buf == NULL) || (index < 0)) {
1104                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1105                 return false;
1106         }
1107
1108         if (table[index] != NULL) {
1109                 *buf = (time_t)atol(table[index]);
1110                 return true;
1111         }
1112
1113         /*  EM_DEBUG_LOG("Empty field. Set as zero"); */
1114
1115         *buf = 0;
1116         return false;
1117 }
1118
1119 static int _get_table_field_data_string(char **table, char **buf, int ucs2, int index)
1120 {
1121         int ret = false;
1122
1123         if ((table == NULL) || (buf == NULL) || (index < 0)) {
1124                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1125                 return false;
1126         }
1127
1128         char *pTemp = table[index];
1129         int sLen = 0;
1130         if (pTemp == NULL)
1131                 *buf = NULL;
1132         else {
1133                 sLen = EM_SAFE_STRLEN(pTemp);
1134                 if (sLen) {
1135                         *buf = (char *) em_malloc(sLen + 1);
1136                         if (*buf == NULL) {
1137                                 EM_DEBUG_EXCEPTION("malloc is failed");
1138                                 goto FINISH_OFF;
1139                         }
1140                         strncpy(*buf, pTemp, sLen);
1141                 } else
1142                         *buf = NULL;
1143         }
1144 #ifdef _PRINT_STORAGE_LOG_
1145         if (*buf)
1146                 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", *buf, index);
1147         else
1148                 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1149 #endif
1150         ret = true;
1151 FINISH_OFF:
1152
1153         return ret;
1154 }
1155
1156 static int _get_table_field_data_string_without_allocation(char **table, char *buf, int buffer_size, int ucs2, int index)
1157 {
1158         if ((table == NULL) || (buf == NULL) || (index < 0)) {
1159                 EM_DEBUG_EXCEPTION(" table[%p], buf[%p], index[%d]", table, buf, index);
1160                         return false;
1161         }
1162
1163         char *pTemp = table[index];
1164
1165         if (pTemp == NULL)
1166                 buf = NULL;
1167         else {
1168                 memset(buf, 0, buffer_size);
1169                 strncpy(buf, pTemp, buffer_size - 1);
1170         }
1171 #ifdef _PRINT_STORAGE_LOG_
1172         if (buf)
1173                 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", buf, index);
1174         else
1175                 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1176 #endif
1177
1178         return true;
1179 }
1180
1181 static int _get_table_field_data_blob(char **table, void **buffer, int buffer_size, int index)
1182 {
1183         if ((table == NULL) || (buffer == NULL) || (index < 0)) {
1184                 EM_DEBUG_EXCEPTION(" table[%p], buffer[%p], buffer_size [%d], index[%d]", table, buffer, buffer_size, index);
1185                 return false;
1186         }
1187
1188         char *temp_buffer = table[index];
1189
1190         if (temp_buffer == NULL)
1191                 buffer = NULL;
1192         else {
1193                 *buffer = malloc(buffer_size);
1194                 if (*buffer == NULL) {
1195                         EM_DEBUG_EXCEPTION("allocation failed.");
1196                         return false;
1197                 }
1198                 memset(*buffer, 0, buffer_size);
1199                 memcpy(*buffer, temp_buffer, buffer_size);
1200         }
1201 #ifdef _PRINT_STORAGE_LOG_
1202         if (buf)
1203                 EM_DEBUG_LOG("_get_table_field_data_string - buffer[%s], index[%d]", buffer, index);
1204         else
1205                 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1206 #endif
1207
1208         return true;
1209 }
1210
1211 static int _get_stmt_field_data_char(DB_STMT hStmt, char *buf, int index)
1212 {
1213         if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1214                 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1215                 return false;
1216         }
1217
1218         if (sqlite3_column_text(hStmt, index) != NULL) {
1219                 *buf = (char)sqlite3_column_int(hStmt, index);
1220 #ifdef _PRINT_STORAGE_LOG_
1221                         EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1222 #endif
1223                 return true;
1224         }
1225
1226         EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1227
1228         return false;
1229 }
1230
1231 static int _get_stmt_field_data_int(DB_STMT hStmt, int *buf, int index)
1232 {
1233         if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1234                 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1235                 return false;
1236         }
1237
1238         if (sqlite3_column_text(hStmt, index) != NULL) {
1239                 *buf = sqlite3_column_int(hStmt, index);
1240 #ifdef _PRINT_STORAGE_LOG_
1241                         EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1242 #endif
1243                 return true;
1244         }
1245
1246         EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1247
1248         return false;
1249 }
1250
1251 static int _get_stmt_field_data_time_t(DB_STMT hStmt, time_t *buf, int index)
1252 {
1253         if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1254                 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1255                 return false;
1256         }
1257
1258         if (sqlite3_column_text(hStmt, index) != NULL) {
1259                 *buf = (time_t)sqlite3_column_int(hStmt, index);
1260 #ifdef _PRINT_STORAGE_LOG_
1261                 EM_DEBUG_LOG("_get_stmt_field_data_time_t [%d]", *buf);
1262 #endif
1263                 return true;
1264         }
1265
1266         EM_DEBUG_LOG("_get_stmt_field_data_time_t fail. index [%d]", index);
1267         return false;
1268 }
1269
1270 static int _get_stmt_field_data_string(DB_STMT hStmt, char **buf, int ucs2, int index)
1271 {
1272         if (!hStmt || !buf || (index < 0)) { /*prevent 39619*/
1273                 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1274                 return false;
1275         }
1276
1277         int sLen = 0;
1278         sLen = sqlite3_column_bytes(hStmt, index);
1279
1280 #ifdef _PRINT_STORAGE_LOG_
1281                 EM_DEBUG_LOG("_get_stmt_field_data_string sqlite3_column_bytes sLen[%d]", sLen);
1282 #endif
1283
1284         if (sLen > 0) {
1285                 *buf = (char *) em_malloc(sLen + 1);
1286                 if (*buf == NULL) {
1287                         EM_DEBUG_EXCEPTION("em_mallocfailed");
1288                         return false;
1289                 }
1290
1291                 strncpy(*buf, (char *)sqlite3_column_text(hStmt, index), sLen);
1292         } else
1293                 *buf = NULL;
1294
1295 #ifdef _PRINT_STORAGE_LOG_
1296         if (*buf)
1297                 EM_DEBUG_LOG("buf[%s], index[%d]", *buf, index);
1298         else
1299                 EM_DEBUG_LOG("_get_stmt_field_data_string - No string got");
1300 #endif
1301
1302         return false;
1303 }
1304
1305 static void _get_stmt_field_data_blob(DB_STMT hStmt, void **buf, int index)
1306 {
1307         if (!hStmt || !buf || (index < 0)) { /*prevent 39618*/
1308                 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], index[%d]", hStmt, buf, index);
1309                 return;
1310         }
1311
1312         int sLen = 0;
1313         sLen = sqlite3_column_bytes(hStmt, index);
1314
1315 #ifdef _PRINT_STORAGE_LOG_
1316         EM_DEBUG_LOG("_get_stmt_field_data_blob sqlite3_column_bytes sLen[%d]", sLen);
1317 #endif
1318
1319         if (sLen > 0) {
1320                 *buf = (char *) em_malloc(sLen);
1321                 if (*buf == NULL) {
1322                         EM_DEBUG_EXCEPTION("em_mallocfailed");
1323                         return;
1324                 }
1325
1326                 memcpy(*buf, (void *)sqlite3_column_blob(hStmt, index), sLen);
1327         } else
1328                 *buf = NULL;
1329
1330 }
1331
1332 static int _get_stmt_field_data_string_without_allocation(DB_STMT hStmt, char *buf, int buffer_size, int ucs2, int index)
1333 {
1334         if (!hStmt || !buf || (index < 0)) { /*prevent 39620*/
1335                 EM_DEBUG_EXCEPTION("hStmt[%d], buf[%p], buffer_size[%d], index[%d]", hStmt, buf, buffer_size, index);
1336                 return false;
1337         }
1338
1339         int sLen = 0;
1340         sLen = sqlite3_column_bytes(hStmt, index);
1341
1342 #ifdef _PRINT_STORAGE_LOG_
1343                 EM_DEBUG_LOG("_get_stmt_field_data_string_without_allocation sqlite3_column_bytes sLen[%d]", sLen);
1344 #endif
1345
1346         if (sLen > 0) {
1347                 memset(buf, 0, buffer_size);
1348                 strncpy(buf, (char *)sqlite3_column_text(hStmt, index), buffer_size - 1);
1349         } else
1350                 strcpy(buf, "");
1351
1352 #ifdef _PRINT_STORAGE_LOG_
1353         EM_DEBUG_LOG("buf[%s], index[%d]", buf, index);
1354 #endif
1355
1356         return false;
1357 }
1358
1359 static int _bind_stmt_field_data_char(DB_STMT hStmt, int index, char value)
1360 {
1361         if ((hStmt == NULL) || (index < 0)) {
1362                 EM_DEBUG_EXCEPTION("index[%d]", index);
1363                 return false;
1364         }
1365
1366         int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1367
1368         if (ret != SQLITE_OK) {
1369                 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1370                 return false;
1371         }
1372
1373         return true;
1374 }
1375
1376 static int _bind_stmt_field_data_int(DB_STMT hStmt, int index, int value)
1377 {
1378         if ((hStmt == NULL) || (index < 0)) {
1379                 EM_DEBUG_EXCEPTION("index[%d]", index);
1380                 return false;
1381         }
1382
1383         int ret = sqlite3_bind_int(hStmt, index+1, value);
1384
1385         if (ret != SQLITE_OK) {
1386                 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1387                 return false;
1388         }
1389
1390         return true;
1391 }
1392
1393 static int _bind_stmt_field_data_time_t(DB_STMT hStmt, int index, time_t value)
1394 {
1395         if ((hStmt == NULL) || (index < 0)) {
1396                 EM_DEBUG_EXCEPTION("index[%d]", index);
1397                 return false;
1398         }
1399
1400         int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1401
1402         if (ret != SQLITE_OK) {
1403                 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1404                 return false;
1405         }
1406
1407         return true;
1408 }
1409
1410 static int _bind_stmt_field_data_string(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1411 {
1412         if ((hStmt == NULL) || (index < 0)) {
1413                 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1414                 return false;
1415         }
1416
1417 #ifdef _PRINT_STORAGE_LOG_
1418         EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1419 #endif
1420
1421         int ret = 0;
1422         if (value != NULL)
1423                 ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1424         else
1425                 ret = sqlite3_bind_text(hStmt, index+1, "", -1, NULL);
1426
1427         if (ret != SQLITE_OK) {
1428                 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1429                 return false;
1430         }
1431         return true;
1432 }
1433
1434 /* destroy function for sqlite3_bind_text */
1435 void _bind_stmt_free_string(void* arg)
1436 {
1437         EM_DEBUG_FUNC_BEGIN();
1438         char* p = (char*) arg;
1439         EM_SAFE_FREE(p);
1440         EM_DEBUG_FUNC_END();
1441 }
1442
1443 static int _bind_stmt_field_data_nstring(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1444 {
1445         if ((hStmt == NULL) || (index < 0)) {
1446                 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1447                 return false;
1448         }
1449
1450 #ifdef _PRINT_STORAGE_LOG_
1451         EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1452 #endif
1453
1454         int ret = 0;
1455         if (value != NULL) {
1456                 if (strlen(value) <= max_len)
1457                         ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1458                 else {
1459                         char *buf = (char*)em_malloc(sizeof(char) * (max_len));
1460                         if (buf == NULL) {
1461                                 EM_DEBUG_EXCEPTION("em_mallocfailed");
1462                                 return false;
1463                         }
1464                         snprintf(buf, max_len-1, "%s", value);
1465                         ret = sqlite3_bind_text(hStmt, index+1, buf, -1, _bind_stmt_free_string);
1466                 }
1467         } else
1468                 ret = sqlite3_bind_text(hStmt, index+1, "", -1, NULL);
1469
1470         if (ret != SQLITE_OK) {
1471                 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1472                 return false;
1473         }
1474         return true;
1475 }
1476
1477
1478 static int _bind_stmt_field_data_blob(DB_STMT hStmt, int index, void *blob, int blob_size)
1479 {
1480         if ((hStmt == NULL) || (index < 0)) {
1481                 EM_DEBUG_EXCEPTION("index[%d], blob_size[%d]", index, blob_size);
1482                 return false;
1483         }
1484
1485 #ifdef _PRINT_STORAGE_LOG_
1486         EM_DEBUG_LOG("hStmt = %p, index = %d, blob_size = %d, blob = [%p]", hStmt, index, blob_size, blob);
1487 #endif
1488
1489         int ret = 0;
1490         if (blob_size > 0)
1491                 ret = sqlite3_bind_blob(hStmt, index+1, blob, blob_size, SQLITE_STATIC);
1492         else
1493                 ret = sqlite3_bind_null(hStmt, index+1);
1494
1495         if (ret != SQLITE_OK) {
1496                 EM_DEBUG_EXCEPTION("sqlite3_bind_blob fail [%d]", ret);
1497                 return false;
1498         }
1499         return true;
1500 }
1501
1502
1503 static int _delete_temp_file(const char *path)
1504 {
1505         EM_DEBUG_FUNC_BEGIN("path[%p]", path);
1506
1507         DIR *dp = NULL;
1508         struct dirent *entry = NULL;
1509
1510         char buf[1024] = {0x00, };
1511
1512         if ((dp = opendir(path)) == NULL) {
1513                 EM_DEBUG_EXCEPTION("opendir(\"%s\") failed...", path);
1514                 return false;
1515         }
1516
1517         while ((entry = readdir(dp)) != NULL) {
1518                 SNPRINTF(buf, sizeof(buf), "%s/%s", path, entry->d_name);
1519                 remove(buf);
1520         }
1521
1522         closedir(dp);
1523         EM_DEBUG_FUNC_END();
1524         return true;
1525 }
1526
1527 char *cpy_str(char *src)
1528 {
1529         char *p = NULL;
1530
1531         if (src) {
1532                 if (!(p = em_malloc((int)EM_SAFE_STRLEN(src) + 1))) {
1533                         EM_DEBUG_EXCEPTION("mailoc failed...");
1534                         return NULL;
1535                 }
1536                 strncpy(p, src, EM_SAFE_STRLEN(src));
1537         }
1538
1539         return p;
1540 }
1541
1542 static void _emstorage_close_once(void)
1543 {
1544         EM_DEBUG_FUNC_BEGIN();
1545
1546         EM_DEBUG_FUNC_END();
1547 }
1548
1549 INTERNAL_FUNC int emstorage_close(int *err_code)
1550 {
1551         EM_DEBUG_FUNC_BEGIN();
1552
1553         int ret = false;
1554         int error = EMAIL_ERROR_NONE;
1555
1556         if (!emstorage_db_close(NULL, &error))
1557
1558         if (--_open_counter == 0)
1559                 _emstorage_close_once();
1560
1561         ret = true;
1562
1563         if (err_code != NULL)
1564                 *err_code = error;
1565
1566         EM_DEBUG_FUNC_END("ret [%d]", ret);
1567         return ret;
1568 }
1569
1570 static void *_emstorage_open_once(char *multi_user_name, int *err_code)
1571 {
1572         EM_DEBUG_FUNC_BEGIN();
1573
1574         int error = EMAIL_ERROR_NONE;
1575
1576     if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1577         char buf[MAX_PATH] = {0};
1578                 char *prefix_path = NULL;
1579
1580                 error = emcore_get_container_path(multi_user_name, &prefix_path);
1581                 if (error != EMAIL_ERROR_NONE) {
1582                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
1583                         goto FINISH_OFF;
1584                 }
1585
1586         memset(buf, 0x00, sizeof(buf));
1587         SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, EMAILPATH);
1588         mkdir(buf, DIRECTORY_PERMISSION);
1589
1590         memset(buf, 0x00, sizeof(buf));
1591         SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAILHOME);
1592         mkdir(buf, DIRECTORY_PERMISSION);
1593
1594         memset(buf, 0x00, sizeof(buf));
1595         SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAILTEMP);
1596         mkdir(buf, DIRECTORY_PERMISSION);
1597
1598         _delete_temp_file(buf);
1599                 EM_SAFE_FREE(prefix_path);
1600     } else {
1601         mkdir(DATA_PATH, DIRECTORY_PERMISSION);
1602         mkdir(EMAILPATH, DIRECTORY_PERMISSION);
1603         mkdir(MAILHOME, DIRECTORY_PERMISSION);
1604         mkdir(MAILTEMP, DIRECTORY_PERMISSION);
1605
1606         _delete_temp_file(MAILTEMP);
1607     }
1608
1609         if (!emstorage_create_table(multi_user_name, EMAIL_CREATE_DB_NORMAL, &error)) {
1610                 EM_DEBUG_EXCEPTION(" emstorage_create_table failed - %d", error);
1611                 goto FINISH_OFF;
1612         }
1613
1614 FINISH_OFF:
1615
1616
1617         if (err_code != NULL)
1618                 *err_code = error;
1619
1620         return NULL;
1621 }
1622
1623  /*  pData : a parameter which is registered when busy handler is registerd */
1624  /*  count : retry count */
1625 static int _callback_sqlite_busy_handler(void *pData, int count)
1626 {
1627         if (10 - count > 0) {
1628                 struct timespec time = {
1629                         .tv_sec = 0,
1630                         .tv_nsec = (count + 1) * 100 * 1000 * 1000
1631                 };
1632                 EM_DEBUG_LOG("Busy handler called!!: PID[%d] / CNT [%d]", getpid(), count);
1633                 nanosleep(&time, NULL);
1634                 return 1;
1635         } else {
1636                 EM_DEBUG_EXCEPTION("Busy handler will be returned SQLITE_BUSY error PID[%d] / CNT[%d]", getpid(), count);
1637                 return 0;
1638         }
1639 }
1640
1641 static int _callback_collation_utf7_sort(void *data, int length_text_a, const void *text_a,
1642                                                                                                          int length_text_b, const void *text_b)
1643 {
1644     EM_DEBUG_FUNC_BEGIN();
1645     int result = 0;
1646     char *converted_string_a = NULL;
1647     char *converted_string_b = NULL;
1648
1649         EM_DEBUG_LOG_DEV("text_a : [%s]", text_a);
1650         converted_string_a = emcore_convert_mutf7_to_utf8((char *)text_a);
1651         EM_DEBUG_LOG_DEV("Converted text_a : [%s]", converted_string_a);
1652
1653         EM_DEBUG_LOG_DEV("text_b : [%s]", text_b);
1654         converted_string_b = emcore_convert_mutf7_to_utf8((char *)text_b);
1655         EM_DEBUG_LOG_DEV("Converted text_b : [%s]", converted_string_b);
1656
1657         if (converted_string_a && converted_string_b)
1658                 result = strcmp(converted_string_a, converted_string_b);
1659
1660         EM_SAFE_FREE(converted_string_a);
1661         EM_SAFE_FREE(converted_string_b);
1662
1663         EM_DEBUG_FUNC_END();
1664         return result;
1665 }
1666
1667 static int _delete_all_files_and_directories(char *db_file_path, int *err_code)
1668 {
1669         EM_DEBUG_FUNC_BEGIN();
1670
1671         int error = EMAIL_ERROR_NONE;
1672         int ret = false;
1673
1674         if (!emstorage_delete_file(db_file_path, &error)) {
1675                 if (error != EMAIL_ERROR_FILE_NOT_FOUND) {
1676                         EM_DEBUG_EXCEPTION_SEC("remove failed - %s", EMAIL_SERVICE_DB_FILE_PATH);
1677                         goto FINISH_OFF;
1678                 }
1679         }
1680
1681         if (!emstorage_delete_dir(MAILHOME, &error)) {
1682                 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed");
1683                 goto FINISH_OFF;
1684         }
1685
1686         ret = true;
1687
1688 FINISH_OFF:
1689         if (err_code)
1690                 *err_code = error;
1691         EM_DEBUG_FUNC_END();
1692         return ret;
1693 }
1694
1695 static int _recovery_from_malformed_db_file(char *db_file_path, int *err_code)
1696 {
1697         EM_DEBUG_FUNC_BEGIN();
1698
1699         int error = EMAIL_ERROR_NONE;
1700         int ret = false;
1701
1702         /* Delete all files and directories */
1703         if (!_delete_all_files_and_directories(db_file_path, &error)) {
1704                 EM_DEBUG_EXCEPTION("_delete_all_files_and_directories failed [%d]", error);
1705                 goto FINISH_OFF;
1706         }
1707
1708         /* Delete all accounts on MyAccount */
1709
1710         /* Delete all managed connection to DB */
1711         emstorage_reset_db_handle_list();
1712
1713         ret = true;
1714
1715 FINISH_OFF:
1716         if (err_code)
1717                 *err_code = error;
1718         EM_DEBUG_FUNC_END();
1719         return ret;
1720 }
1721
1722 int _xsystem(const char *argv[])
1723 {
1724         int status = 0;
1725         pid_t pid;
1726         pid = fork();
1727
1728         switch (pid) {
1729                 case -1:
1730                         perror("fork failed");
1731                         return -1;
1732                 case 0:
1733                         if (execvp(argv[0], (char *const *)argv) == -1) {
1734                                 perror("execute init db script");
1735                                 _exit(-1);
1736                         }
1737
1738                         _exit(2);
1739                 default:
1740                         /* parent */
1741                         break;
1742         }
1743
1744         if (waitpid(pid, &status, 0) == -1) {
1745                 perror("waitpid failed");
1746                 return -1;
1747         }
1748
1749         if (WIFSIGNALED(status)) {
1750                 perror("signal");
1751                 return -1;
1752         }
1753
1754         if (!WIFEXITED(status)) {
1755                 perror("should not happen");
1756                 return -1;
1757         }
1758
1759         return WEXITSTATUS(status);
1760 }
1761
1762 #define SCRIPT_INIT_DB "/usr/bin/email-service_init_db.sh"
1763
1764 INTERNAL_FUNC int emstorage_init_db(char *multi_user_name)
1765 {
1766         EM_DEBUG_FUNC_BEGIN();
1767         int err = EMAIL_ERROR_NONE;
1768         char *prefix_path = NULL;
1769         char *output_file_path = NULL;
1770         char temp_file_path[MAX_PATH] = {0};
1771
1772         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1773                 err = emcore_get_container_path(multi_user_name, &prefix_path);
1774                 if (err != EMAIL_ERROR_NONE) {
1775                         if (err != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1776                                 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", err);
1777                                 goto FINISH_OFF;
1778                         }
1779                 }
1780         } else {
1781                 prefix_path = strdup("");
1782         }
1783
1784         if (err == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1785                 err = emcore_get_canonicalize_path(EMAIL_SERVICE_DB_FILE_PATH, &output_file_path);
1786                 if (err != EMAIL_ERROR_NONE) {
1787                         EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", err);
1788                         goto FINISH_OFF;
1789                 }
1790
1791                 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
1792         } else {
1793                 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
1794         }
1795
1796         EM_DEBUG_LOG("db file path : [%s]", temp_file_path);
1797
1798         if (!g_file_test(temp_file_path, G_FILE_TEST_EXISTS)) {
1799                 int ret = true;
1800                 const char *argv_script[] = {"/bin/sh", SCRIPT_INIT_DB, NULL};
1801                 ret = _xsystem(argv_script);
1802
1803                 if (ret == -1) {
1804                         EM_DEBUG_EXCEPTION("_xsystem failed");
1805                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1806                 }
1807         }
1808
1809 FINISH_OFF:
1810
1811         EM_SAFE_FREE(prefix_path);
1812         EM_SAFE_FREE(output_file_path);
1813
1814         EM_DEBUG_FUNC_END();
1815         return err;
1816 }
1817
1818 INTERNAL_FUNC int em_db_open(char *db_file_path, sqlite3 **sqlite_handle, int *err_code)
1819 {
1820         EM_DEBUG_FUNC_BEGIN();
1821         int rc = 0;
1822         int error = EMAIL_ERROR_NONE;
1823         int ret = false;
1824
1825         EM_DEBUG_LOG_DEV("*sqlite_handle[%p]", *sqlite_handle);
1826
1827         if (*sqlite_handle) { /*prevent 33351*/
1828                 EM_DEBUG_LOG_DEV(">>>>> DB Already Opened......");
1829                 if (err_code != NULL)
1830                         *err_code = error;
1831                 return true;
1832         }
1833
1834     EM_DEBUG_LOG("DB file path : [%s]", db_file_path);
1835
1836         /*  db open */
1837         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1838         if (SQLITE_OK != rc) {
1839                 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1840                 if (SQLITE_PERM == rc || SQLITE_CANTOPEN == rc) {
1841                         error = EMAIL_ERROR_PERMISSION_DENIED;
1842                 } else {
1843                         error = EMAIL_ERROR_DB_FAILURE;
1844                 }
1845                 sqlite3_close(*sqlite_handle);
1846                 *sqlite_handle = NULL;
1847
1848                 if (SQLITE_CORRUPT == rc) /* SQLITE_CORRUPT : The database disk image is malformed */ {/* Recovery DB file */
1849                         EM_DEBUG_LOG("The database disk image is malformed. Trying to remove and create database disk image and directories");
1850                         if (!_recovery_from_malformed_db_file(db_file_path, &error)) {
1851                                 EM_DEBUG_EXCEPTION("_recovery_from_malformed_db_file failed [%d]", error);
1852                                 goto FINISH_OFF;
1853                         }
1854
1855                         EM_DEBUG_LOG("Open DB again");
1856                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1857                         if (SQLITE_OK != rc) {
1858                                 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1859                                 if (SQLITE_PERM == rc) {
1860                                         error = EMAIL_ERROR_PERMISSION_DENIED;
1861                                 } else {
1862                                         error = EMAIL_ERROR_DB_FAILURE;
1863                                 }
1864                                 sqlite3_close(*sqlite_handle);
1865                                 *sqlite_handle = NULL;
1866                                 goto FINISH_OFF; /*prevent 33351*/
1867                         }
1868                 } else
1869                         goto FINISH_OFF;
1870         }
1871
1872         /* register busy handler */
1873         EM_DEBUG_LOG_DEV(">>>>> Register DB Handle to busy handler: *sqlite_handle[%p]", *sqlite_handle);
1874         rc = sqlite3_busy_handler(*sqlite_handle, _callback_sqlite_busy_handler, NULL);  /*  Busy Handler registration, NULL is a parameter which will be passed to handler */
1875         if (SQLITE_OK != rc) {
1876                 EM_DEBUG_EXCEPTION("sqlite3_busy_handler fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1877                 error = EMAIL_ERROR_DB_FAILURE;
1878                 sqlite3_close(*sqlite_handle);
1879                 *sqlite_handle = NULL;
1880                 goto FINISH_OFF;
1881         }
1882
1883         /* Register collation callback function */
1884         rc = sqlite3_create_collation(*sqlite_handle, "CONVERTUTF8", SQLITE_UTF8, NULL, _callback_collation_utf7_sort);
1885         if (SQLITE_OK != rc) {
1886                 EM_DEBUG_EXCEPTION("sqlite3_create_collation failed : [%d][%s]", rc, sqlite3_errmsg(*sqlite_handle));
1887                 error = EMAIL_ERROR_DB_FAILURE;
1888                 sqlite3_close(*sqlite_handle);
1889                 *sqlite_handle = NULL;
1890                 goto FINISH_OFF;
1891         }
1892
1893         ret = true;
1894
1895 FINISH_OFF:
1896         if (err_code != NULL)
1897                 *err_code = error;
1898
1899         EM_DEBUG_FUNC_END("ret [%d]", ret);
1900         return ret;
1901 }
1902
1903 INTERNAL_FUNC sqlite3* emstorage_db_open(char *multi_user_name, int *err_code)
1904 {
1905         EM_DEBUG_FUNC_BEGIN();
1906
1907         sqlite3 *_db_handle = NULL;
1908
1909         int error = EMAIL_ERROR_NONE;
1910         char *prefix_path = NULL;
1911
1912         _db_handle = emstorage_get_db_handle(multi_user_name);
1913
1914         if (_db_handle == NULL) {
1915                 char *output_file_path = NULL;
1916         char temp_file_path[MAX_PATH] = {0};
1917
1918         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1919                         error = emcore_get_container_path(multi_user_name, &prefix_path);
1920                         if (error != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION && error != EMAIL_ERROR_NONE) {
1921                                 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", error);
1922                                 goto FINISH_OFF;
1923                         }
1924         } else {
1925             prefix_path = strdup("");
1926         }
1927
1928                 if (error == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1929                         if ((error = emcore_get_canonicalize_path(EMAIL_SERVICE_DB_FILE_PATH, &output_file_path)) != EMAIL_ERROR_NONE) {
1930                                 EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", error);
1931                                 goto FINISH_OFF;
1932                         }
1933
1934                         SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
1935                         EM_SAFE_FREE(output_file_path);
1936                 } else {
1937                         SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
1938                 }
1939
1940                 if (!em_db_open(temp_file_path, &_db_handle, &error)) {
1941                         EM_DEBUG_EXCEPTION("em_db_open failed[%d]", error);
1942                         goto FINISH_OFF;
1943                 }
1944
1945                 _initialize_shm_mutex(SHM_FILE_FOR_DB_LOCK, &shm_fd_for_db_lock, &mapped_for_db_lock);
1946
1947 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
1948                 _initialize_shm_mutex(SHM_FILE_FOR_MAIL_ID_LOCK, &shm_fd_for_generating_mail_id, &mapped_for_generating_mail_id);
1949 #endif /*__FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
1950
1951                 emstorage_set_db_handle(multi_user_name, _db_handle);
1952
1953                 emstorage_initialize_field_count();
1954         }
1955
1956 FINISH_OFF:
1957
1958         EM_SAFE_FREE(prefix_path);
1959
1960         if (err_code != NULL)
1961                 *err_code = error;
1962
1963         EM_DEBUG_FUNC_END("ret [%p]", _db_handle);
1964         return _db_handle;
1965 }
1966
1967 INTERNAL_FUNC int emstorage_db_close(char *multi_user_name, int *err_code)
1968 {
1969         EM_DEBUG_FUNC_BEGIN();
1970 #ifdef _MULTIPLE_DB_HANDLE
1971         sqlite3 *_db_handle = emstorage_get_db_handle(multi_user_name);
1972 #endif
1973
1974         int error = EMAIL_ERROR_NONE;
1975         int ret = false;
1976
1977         if (_db_handle) {
1978                 ret = sqlite3_close(_db_handle);
1979                 if (ret != SQLITE_OK) {
1980                         EM_DEBUG_EXCEPTION(" sqlite3_close fail - %d", ret);
1981                         error = EMAIL_ERROR_DB_FAILURE;
1982                         ret = false;
1983                         goto FINISH_OFF;
1984                 }
1985 #ifdef _MULTIPLE_DB_HANDLE
1986                 emstorage_remove_db_handle();
1987 #endif
1988                 _db_handle = NULL;
1989         }
1990
1991         ret = true;
1992
1993 FINISH_OFF:
1994         if (err_code != NULL)
1995                 *err_code = error;
1996
1997         EM_DEBUG_FUNC_END("ret [%d]", ret);
1998         return ret;
1999 }
2000
2001 INTERNAL_FUNC int emstorage_open(char *multi_user_name, int *err_code)
2002 {
2003         EM_DEBUG_FUNC_BEGIN();
2004
2005     int ret = false;
2006     int error = EMAIL_ERROR_NONE;
2007     int retValue;
2008         char *prefix_path = NULL;
2009     char buf[MAX_PATH] = {0};
2010
2011     if (EM_SAFE_STRLEN(multi_user_name) <= 0) {
2012         SNPRINTF(buf, sizeof(buf), "%s", DB_PATH);
2013     } else {
2014                 error = emcore_get_container_path(multi_user_name, &prefix_path);
2015                 if (error != EMAIL_ERROR_NONE) {
2016                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
2017                         goto FINISH_OFF;
2018                 }
2019         SNPRINTF(buf, sizeof(buf), "%s/%s", prefix_path, DB_PATH);
2020     }
2021
2022         if (!g_file_test(buf, G_FILE_TEST_EXISTS)) {
2023             retValue = mkdir(buf, DIRECTORY_PERMISSION);
2024
2025                 EM_DEBUG_LOG("mkdir return- %d", retValue);
2026             EM_DEBUG_LOG("emstorage_open - before sqlite3_open - pid = %d", getpid());
2027         }
2028
2029     if (emstorage_db_open(multi_user_name, &error) == NULL) {
2030         EM_DEBUG_EXCEPTION("emstorage_db_open failed[%d]", error);
2031         goto FINISH_OFF;
2032     }
2033
2034     if (_open_counter++ == 0) {
2035         _emstorage_open_once(multi_user_name, &error);
2036     }
2037
2038     ret = true;
2039
2040 FINISH_OFF:
2041
2042         EM_SAFE_FREE(prefix_path);
2043
2044     if (err_code != NULL)
2045         *err_code = error;
2046
2047     EM_DEBUG_FUNC_END("ret [%d]", ret);
2048     return ret;
2049 }
2050
2051 static int emstorage_get_field_count_from_create_table_query(char *input_create_table_query, int *output_field_count)
2052 {
2053         EM_DEBUG_FUNC_BEGIN("input_create_table_query[%d], output_field_count[%p]", input_create_table_query, output_field_count);
2054         int err = EMAIL_ERROR_NONE;
2055         int field_count = 0;
2056         char *pos = NULL;
2057
2058         if (input_create_table_query == NULL || output_field_count == NULL) {
2059                 err = EMAIL_ERROR_INVALID_PARAM;
2060                 goto FINISH_OFF;
2061         }
2062
2063         pos = input_create_table_query;
2064
2065         do {
2066                 field_count++;
2067                 if (pos == NULL || *pos == (char)0)
2068                         break;
2069                 pos += 1;
2070                 pos = strchr(pos, ',');
2071         } while (pos);
2072
2073         *output_field_count = field_count;
2074
2075         EM_DEBUG_LOG_DEV("field_count [%d]", field_count);
2076
2077 FINISH_OFF:
2078
2079         EM_DEBUG_FUNC_END("err [%d]", err);
2080         return err;
2081 }
2082
2083 INTERNAL_FUNC int emstorage_initialize_field_count()
2084 {
2085         EM_DEBUG_FUNC_BEGIN();
2086         int err = EMAIL_ERROR_NONE;
2087         int query_len = 0;
2088         char **create_table_query = NULL;
2089
2090         if (_field_count_of_table[CREATE_TABLE_MAIL_ACCOUNT_TBL] != 0) {
2091                 err = EMAIL_ERROR_ALREADY_INITIALIZED;
2092                 goto FINISH_OFF;
2093         }
2094
2095         err = emcore_load_query_from_file(EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2096         if (err != EMAIL_ERROR_NONE) {
2097                 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", err);
2098                 goto FINISH_OFF;
2099         }
2100
2101         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_ACCOUNT_TBL]));
2102         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_BOX_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_BOX_TBL]));
2103         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL]));
2104         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_RULE_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_RULE_TBL]));
2105         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_TBL]));
2106         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_ATTACHMENT_TBL]));
2107         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL]));
2108         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_MEETING_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_MEETING_TBL]));
2109 #ifdef __FEATURE_LOCAL_ACTIVITY__
2110         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL]));
2111 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2112         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_CERTIFICATE_TBL]));
2113         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_TASK_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_TASK_TBL]));
2114         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_TEXT_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_TEXT_TBL]));
2115 FINISH_OFF:
2116
2117         if (create_table_query) {
2118                 int i = 0;
2119                 for (i = 0; i < query_len; i++) {
2120                         if (create_table_query[i]) {
2121                                 EM_SAFE_FREE(create_table_query[i]);
2122                         }
2123                 }
2124                 EM_SAFE_FREE(create_table_query);
2125         }
2126
2127         EM_DEBUG_FUNC_END("err [%d]", err);
2128         return err;
2129 }
2130
2131 INTERNAL_FUNC int emstorage_create_table(char *multi_user_name, emstorage_create_db_type_t type, int *err_code)
2132 {
2133         EM_DEBUG_FUNC_BEGIN();
2134
2135         int error = EMAIL_ERROR_NONE;
2136         int rc = -1, ret = false;
2137         int query_len = 0;
2138         char sql_query_string[QUERY_SIZE] = {0, };
2139         char **create_table_query = NULL;
2140
2141         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2142
2143         error = emcore_load_query_from_file(EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2144         if (error != EMAIL_ERROR_NONE) {
2145                 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
2146                 goto FINISH_OFF2;
2147         }
2148
2149         if (query_len < CREATE_TABLE_MAX) {
2150                 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
2151                 error = EMAIL_ERROR_SYSTEM_FAILURE;
2152                 goto FINISH_OFF2;
2153         }
2154
2155         EM_DEBUG_LOG("local_db_handle = %p.", local_db_handle);
2156
2157         char *sql;
2158         char **result = NULL;
2159
2160         /*  1. create mail_account_tbl */
2161         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_account_tbl';";
2162         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2163         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2164                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2165
2166         EM_DEBUG_LOG("emstorage_create_table - result[1] = %s %c", result[1], result[1]);
2167
2168         if (atoi(result[1]) < 1) {
2169                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2170                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2171                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2172
2173                 EM_DEBUG_LOG("CREATE TABLE mail_account_tbl");
2174                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2175                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2176                 if (error != EMAIL_ERROR_NONE) {
2177                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2178                         goto FINISH_OFF;
2179                 }
2180
2181                 /*  create mail_account_tbl unique index */
2182                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ACCOUNT_IDX]);
2183                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2184                 if (error != EMAIL_ERROR_NONE) {
2185                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2186                         goto FINISH_OFF;
2187                 }
2188
2189                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2190         } /*  mail_account_tbl */
2191         else if (type == EMAIL_CREATE_DB_CHECK) {
2192                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], NULL, NULL, NULL), rc);
2193                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], rc, sqlite3_errmsg(local_db_handle)));
2194         }
2195
2196         sqlite3_free_table(result);
2197         result = NULL;
2198
2199         /*  2. create mail_box_tbl */
2200         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_box_tbl';";
2201
2202         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2203         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2204                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2205
2206         if (atoi(result[1]) < 1) {
2207                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2208                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2209                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2210
2211                 EM_DEBUG_LOG("CREATE TABLE mail_box_tbl");
2212
2213                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_BOX_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2214                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2215                 if (error != EMAIL_ERROR_NONE) {
2216                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2217                         goto FINISH_OFF;
2218                 }
2219
2220                 /*  create mail_local_mailbox_tbl unique index */
2221                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_BOX_IDX]);
2222                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2223                 if (error != EMAIL_ERROR_NONE) {
2224                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2225                         goto FINISH_OFF;
2226                 }
2227
2228                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2229         } /*  mail_box_tbl */
2230         else if (type == EMAIL_CREATE_DB_CHECK) {
2231                 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_BOX_TBL], NULL, NULL, NULL);
2232                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2233                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_BOX_TBL], rc, sqlite3_errmsg(local_db_handle)));
2234         }
2235         sqlite3_free_table(result);
2236         result = NULL;
2237
2238         /*  3. create mail_read_mail_uid_tbl */
2239         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_read_mail_uid_tbl';";
2240         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2241         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2242                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2243
2244
2245         if (atoi(result[1]) < 1) {
2246                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2247                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2248                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2249
2250                 EM_DEBUG_LOG("CREATE TABLE mail_read_mail_uid_tbl");
2251
2252                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2253                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2254                 if (error != EMAIL_ERROR_NONE) {
2255                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2256                         goto FINISH_OFF;
2257                 }
2258
2259                 /*  create mail_read_mail_uid_tbl unique index */
2260                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_IDX]);
2261                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2262                 if (error != EMAIL_ERROR_NONE) {
2263                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2264                         goto FINISH_OFF;
2265                 }
2266
2267                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2268         } /*  mail_read_mail_uid_tbl */
2269         else if (type == EMAIL_CREATE_DB_CHECK) {
2270                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], NULL, NULL, NULL), rc);
2271                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2272                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], rc, sqlite3_errmsg(local_db_handle)));
2273         }
2274         sqlite3_free_table(result);
2275         result = NULL;
2276
2277         /*  4. create mail_rule_tbl */
2278         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_rule_tbl';";
2279
2280         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2281         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2282                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2283
2284         if (atoi(result[1]) < 1) {
2285                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2286                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2287                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2288
2289                 EM_DEBUG_LOG("CREATE TABLE mail_rule_tbl");
2290
2291                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_RULE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2292                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2293                 if (error != EMAIL_ERROR_NONE) {
2294                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2295                         goto FINISH_OFF;
2296                 }
2297
2298                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2299         } /*  mail_rule_tbl */
2300         else if (type == EMAIL_CREATE_DB_CHECK) {
2301                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_RULE_TBL], NULL, NULL, NULL), rc);
2302                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2303                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_RULE_TBL], rc, sqlite3_errmsg(local_db_handle)));
2304         }
2305         sqlite3_free_table(result);
2306         result = NULL;
2307
2308         /*  5. create mail_tbl */
2309         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_tbl';";
2310         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2311         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2312                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2313
2314         if (atoi(result[1]) < 1) {
2315                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2316                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2317                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2318                 EM_DEBUG_LOG("CREATE TABLE mail_tbl");
2319
2320                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2321                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2322                 if (error != EMAIL_ERROR_NONE) {
2323                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2324                         goto FINISH_OFF;
2325                 }
2326
2327                 /*  create mail_tbl unique index */
2328                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_IDX]);
2329                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2330                 if (error != EMAIL_ERROR_NONE) {
2331                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2332                         goto FINISH_OFF;
2333                 }
2334
2335                 /*  create mail_tbl index for date_time */
2336                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_DATETIME_IDX]);
2337                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2338                 if (error != EMAIL_ERROR_NONE) {
2339                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2340                         goto FINISH_OFF;
2341                 }
2342
2343                 /*  create mail_tbl index for thread_item_count */
2344                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_THREAD_IDX]);
2345                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2346                 if (error != EMAIL_ERROR_NONE) {
2347                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2348                         goto FINISH_OFF;
2349                 }
2350
2351                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2352                 /*  just one time call */
2353 /*              EFTSInitFTSIndex(FTS_EMAIL_IDX); */
2354         } /*  mail_tbl */
2355         else if (type == EMAIL_CREATE_DB_CHECK) {
2356                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TBL], NULL, NULL, NULL), rc);
2357                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2358                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TBL], rc, sqlite3_errmsg(local_db_handle)));
2359         }
2360         sqlite3_free_table(result);
2361         result = NULL;
2362
2363         /*  6. create mail_attachment_tbl */
2364         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_attachment_tbl';";
2365         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2366         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2367                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2368
2369         if (atoi(result[1]) < 1) {
2370                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2371                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2372                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2373
2374                 EM_DEBUG_LOG("CREATE TABLE mail_attachment_tbl");
2375
2376                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2377                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2378                 if (error != EMAIL_ERROR_NONE) {
2379                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2380                         goto FINISH_OFF;
2381                 }
2382
2383                 /*  create mail_attachment_tbl unique index */
2384                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_IDX]);
2385                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2386                 if (error != EMAIL_ERROR_NONE) {
2387                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2388                         goto FINISH_OFF;
2389                 }
2390
2391                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2392         } /*  mail_attachment_tbl */
2393         else if (type == EMAIL_CREATE_DB_CHECK) {
2394                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], NULL, NULL, NULL), rc);
2395                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2396                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], rc, sqlite3_errmsg(local_db_handle)));
2397         }
2398         sqlite3_free_table(result);
2399         result = NULL;
2400
2401 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
2402
2403         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_partial_body_activity_tbl';";
2404         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2405         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2406                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2407
2408         if (atoi(result[1]) < 1) {
2409
2410                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2411                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2412                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2413
2414                 EM_DEBUG_LOG("CREATE TABLE mail_partial_body_activity_tbl");
2415
2416                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2417                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2418                 if (error != EMAIL_ERROR_NONE) {
2419                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2420                         goto FINISH_OFF;
2421                 }
2422
2423                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2424         } /*  mail_rule_tbl */
2425         else if (type == EMAIL_CREATE_DB_CHECK) {
2426                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2427                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2428                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2429         }
2430         sqlite3_free_table(result);
2431         result = NULL;
2432
2433 #endif /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
2434
2435         /*  create mail_meeting_tbl */
2436         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_meeting_tbl';";
2437         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2438         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2439                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2440
2441         if (atoi(result[1]) < 1) {
2442                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2443                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2444                         ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2445
2446                 EM_DEBUG_LOG("CREATE TABLE mail_meeting_tbl");
2447
2448                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_MEETING_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2449                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2450                 if (error != EMAIL_ERROR_NONE) {
2451                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2452                         goto FINISH_OFF;
2453                 }
2454
2455                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_MEETING_IDX]);
2456                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2457                 if (error != EMAIL_ERROR_NONE) {
2458                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2459                         goto FINISH_OFF;
2460                 }
2461
2462                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2463         } /*  mail_contact_sync_tbl */
2464         else if (type == EMAIL_CREATE_DB_CHECK) {
2465                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], NULL, NULL, NULL), rc);
2466                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2467                         ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], rc, sqlite3_errmsg(local_db_handle)));
2468         }
2469         sqlite3_free_table(result);
2470         result = NULL;
2471
2472 #ifdef __FEATURE_LOCAL_ACTIVITY__
2473
2474                 sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_local_activity_tbl';";
2475                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2476                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2477                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2478
2479                 if (atoi(result[1]) < 1) {
2480
2481                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2482                         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2483                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2484
2485                         EM_DEBUG_LOG(" CREATE TABLE mail_local_activity_tbl");
2486
2487                         SNPRINTF(sql_query_string, sizeof(sql_query_string), create_table_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL]);
2488                         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2489                         if (error != EMAIL_ERROR_NONE) {
2490                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2491                                 goto FINISH_OFF;
2492                         }
2493
2494                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2495                 } /*  mail_rule_tbl */
2496                 else if (type == EMAIL_CREATE_DB_CHECK) {
2497                         rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL);
2498                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2499                         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2500                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2501                 }
2502                 sqlite3_free_table(result);
2503                 result = NULL;
2504 #endif /*  __FEATURE_LOCAL_ACTIVITY__ */
2505         /*  create mail_certificate_tbl */
2506         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_certificate_tbl';";
2507         /*  rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL);   */
2508         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2509         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2510
2511         if (atoi(result[1]) < 1) {
2512                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2513                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2514
2515                 EM_DEBUG_LOG("CREATE TABLE mail_certificate_tbl");
2516
2517                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2518                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2519                 if (error != EMAIL_ERROR_NONE) {
2520                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2521                         goto FINISH_OFF;
2522                 }
2523
2524                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2525         } /*  mail_contact_sync_tbl */
2526         else if (type == EMAIL_CREATE_DB_CHECK) {
2527                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], NULL, NULL, NULL), rc);
2528                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], rc, sqlite3_errmsg(local_db_handle)));
2529         }
2530
2531         sqlite3_free_table(result);
2532         result = NULL;
2533
2534         /*  create mail_task_tbl */
2535         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_task_tbl';";
2536         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2537         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2538
2539         if (atoi(result[1]) < 1) {
2540                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2541                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2542
2543                 EM_DEBUG_LOG("CREATE TABLE mail_task_tbl");
2544
2545                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TASK_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2546                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2547                 if (error != EMAIL_ERROR_NONE) {
2548                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2549                         goto FINISH_OFF;
2550                 }
2551
2552                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_TASK_IDX]);
2553                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2554                 if (error != EMAIL_ERROR_NONE) {
2555                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2556                         goto FINISH_OFF;
2557                 }
2558
2559                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2560         } /*  mail_task_tbl */
2561         else if (type == EMAIL_CREATE_DB_CHECK) {
2562                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TASK_TBL], NULL, NULL, NULL), rc);
2563                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TASK_TBL], rc, sqlite3_errmsg(local_db_handle)));
2564         }
2565
2566         sqlite3_free_table(result);
2567         result = NULL;
2568
2569 #ifdef __FEATURE_BODY_SEARCH__
2570         /*  create mail_text_tbl */
2571         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_text_tbl';";
2572         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2573         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2574
2575         if (atoi(result[1]) < 1) {
2576                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2577                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2578
2579                 EM_DEBUG_LOG("CREATE TABLE mail_text_tbl");
2580
2581                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TEXT_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2582                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2583                 if (error != EMAIL_ERROR_NONE) {
2584                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2585                         goto FINISH_OFF;
2586                 }
2587
2588                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2589         } /*  mail_text_tbl */
2590         else if (type == EMAIL_CREATE_DB_CHECK) {
2591                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TEXT_TBL], NULL, NULL, NULL), rc);
2592                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TEXT_TBL], rc, sqlite3_errmsg(local_db_handle)));
2593         }
2594
2595         sqlite3_free_table(result);
2596         result = NULL;
2597
2598 #endif
2599
2600 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
2601         /*  create mail_auto_download_activity_tbl */
2602         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_auto_download_activity_tbl';";
2603         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2604         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2605
2606         if (atoi(result[1]) < 1) {
2607                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2608                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2609
2610                 EM_DEBUG_LOG("CREATE TABLE mail_auto_download_activity_tbl");
2611
2612                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], sizeof(sql_query_string)-1);
2613                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2614                 if (error != EMAIL_ERROR_NONE) {
2615                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2616                         goto FINISH_OFF;
2617                 }
2618
2619                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2620         } /*  mail_auto_download_activity_tbl */
2621         else if (type == EMAIL_CREATE_DB_CHECK) {
2622                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2623                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2624         }
2625
2626         sqlite3_free_table(result);
2627         result = NULL;
2628 #endif
2629
2630
2631         ret = true;
2632
2633 FINISH_OFF:
2634
2635         if (result) sqlite3_free_table(result);
2636
2637         if (ret == true) {
2638                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2639         } else {
2640                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
2641         }
2642
2643 FINISH_OFF2:
2644         if (create_table_query) {
2645                 int i = 0;
2646                 for (i = 0; i < query_len; i++) {
2647                         if (create_table_query[i]) {
2648                                 EM_SAFE_FREE(create_table_query[i]);
2649                         }
2650                 }
2651                 EM_SAFE_FREE(create_table_query);
2652         }
2653
2654         if (err_code != NULL)
2655                 *err_code = error;
2656
2657         EM_DEBUG_FUNC_END("ret [%d]", ret);
2658         return ret;
2659 }
2660
2661 /* Query series --------------------------------------------------------------*/
2662 INTERNAL_FUNC int emstorage_query_mail_count(char *multi_user_name, const char *input_conditional_clause, int input_transaction, int *output_total_mail_count, int *output_unseen_mail_count)
2663 {
2664         EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_transaction[%d], output_total_mail_count[%p], output_unseen_mail_count[%p]", input_conditional_clause, input_transaction, output_total_mail_count, output_unseen_mail_count);
2665         int rc = -1;
2666         int query_size = 0;
2667         int error = EMAIL_ERROR_NONE;
2668         DB_STMT hStmt = NULL;
2669         char *sql_query_string = NULL;
2670         char **result;
2671         sqlite3 *local_db_handle = NULL;
2672
2673         if (!input_conditional_clause || (!output_total_mail_count && !output_unseen_mail_count)) {
2674                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2675                 return EMAIL_ERROR_INVALID_PARAM;
2676         }
2677
2678         query_size = EM_SAFE_STRLEN(input_conditional_clause) + QUERY_SIZE;
2679         sql_query_string = em_malloc(query_size);
2680         if (sql_query_string == NULL) {
2681                 EM_DEBUG_EXCEPTION("em_mallocfailed");
2682                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2683                 goto FINISH_OFF;
2684         }
2685
2686         local_db_handle = emstorage_get_db_connection(multi_user_name);
2687
2688         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2689
2690         SNPRINTF(sql_query_string, query_size, "SELECT COUNT(*) FROM mail_tbl");
2691         EM_SAFE_STRCAT(sql_query_string, (char*)input_conditional_clause);
2692
2693         if (output_total_mail_count) {
2694                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
2695                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2696                         ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
2697
2698                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2699                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2700                         ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
2701                 _get_stmt_field_data_int(hStmt, output_total_mail_count, 0);
2702         }
2703
2704         if (output_unseen_mail_count) {
2705                 EM_SAFE_STRCAT(sql_query_string, " AND flags_seen_field = 0 ");
2706
2707                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
2708                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2709                         ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2710
2711                 *output_unseen_mail_count = atoi(result[1]);
2712                 sqlite3_free_table(result);
2713         }
2714
2715 FINISH_OFF:
2716
2717         if (hStmt != NULL) {
2718                 rc = sqlite3_finalize(hStmt);
2719                 if (rc != SQLITE_OK) {
2720                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
2721                         error = EMAIL_ERROR_DB_FAILURE;
2722                 }
2723         }
2724
2725         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2726
2727         EM_SAFE_FREE(sql_query_string);
2728
2729         EM_DEBUG_FUNC_END("error [%d]", error);
2730         return error;
2731 }
2732
2733 INTERNAL_FUNC int emstorage_query_mail_id_list(char *multi_user_name, const char *input_conditional_clause, int input_transaction, int **output_mail_id_list, int *output_mail_id_count)
2734 {
2735         EM_DEBUG_FUNC_BEGIN("input_conditional_clause [%p], input_transaction [%d], output_mail_id_list [%p], output_mail_id_count [%p]", input_conditional_clause, input_transaction, output_mail_id_list, output_mail_id_count);
2736
2737         int      i = 0;
2738         int      count = 0;
2739         int      rc = -1;
2740         int      cur_query = 0;
2741         int      col_index;
2742         int      query_size = 0;
2743         int      error = EMAIL_ERROR_NONE;
2744         int     *result_mail_id_list = NULL;
2745         char   **result = NULL;
2746         char     *sql_query_string = NULL;
2747         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2748
2749         EM_IF_NULL_RETURN_VALUE(input_conditional_clause, EMAIL_ERROR_INVALID_PARAM);
2750         EM_IF_NULL_RETURN_VALUE(output_mail_id_list, EMAIL_ERROR_INVALID_PARAM);
2751         EM_IF_NULL_RETURN_VALUE(output_mail_id_count, EMAIL_ERROR_INVALID_PARAM);
2752
2753         query_size = strlen(input_conditional_clause) + strlen("SELECT mail_id FROM mail_tbl ") + 10;  // 10 is extra space
2754         sql_query_string = em_malloc(query_size);
2755         if (sql_query_string == NULL) {
2756                 EM_DEBUG_EXCEPTION("em_mallocfailed");
2757                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2758                 goto FINISH_OFF;
2759         }
2760
2761         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2762
2763         /* Composing query */
2764         SNPRINTF_OFFSET(sql_query_string, cur_query, query_size, "SELECT mail_id FROM mail_tbl ");
2765         EM_SAFE_STRCAT(sql_query_string, (char*)input_conditional_clause);
2766
2767         EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
2768
2769         /* Performing query */
2770         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2771         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2772                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2773
2774         col_index = 1;
2775
2776         /*  to get mail list */
2777         if (count == 0) {
2778                 EM_DEBUG_LOG("No mail found...");
2779                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2780                 goto FINISH_OFF;
2781         }
2782
2783         EM_DEBUG_LOG("There are [%d] mails.", count);
2784
2785         if (!(result_mail_id_list = (int*)em_malloc(sizeof(int) * count))) {
2786                 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
2787                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2788                 goto FINISH_OFF;
2789         }
2790
2791         EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2792
2793         for (i = 0; i < count; i++)
2794                 _get_table_field_data_int(result, result_mail_id_list + i, col_index++);
2795
2796         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >> ", count);
2797
2798         *output_mail_id_list  = result_mail_id_list;
2799         *output_mail_id_count = count;
2800
2801 FINISH_OFF:
2802
2803         if (result)
2804                 sqlite3_free_table(result);
2805
2806         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2807
2808         EM_SAFE_FREE(sql_query_string);
2809
2810         if (error != EMAIL_ERROR_NONE)
2811                 EM_SAFE_FREE(result_mail_id_list);
2812
2813         EM_DEBUG_FUNC_END("error [%d]", error);
2814         return error;
2815 }
2816
2817 INTERNAL_FUNC int emstorage_query_mail_list(char *multi_user_name, const char *conditional_clause, int transaction, email_mail_list_item_t** result_mail_list,  int *result_count,  int *err_code)
2818 {
2819         EM_DEBUG_FUNC_BEGIN();
2820         EM_PROFILE_BEGIN(emstorage_query_mail_list_func);
2821
2822         int i = 0, count = 0, rc = -1, to_get_count = (result_mail_list) ? 0 : 1;
2823         int sql_query_string_length = 0;
2824         int local_inline_content_count = 0, local_attachment_count = 0;
2825         int cur_query = 0, base_count = 0, col_index;
2826         int ret = false, error = EMAIL_ERROR_NONE;
2827         char *date_time_string = NULL;
2828         char **result = NULL;
2829         char *field_mail_id = "mail_id";
2830         char *field_all     = "mail_id, account_id, mailbox_id, mailbox_type, full_address_from, email_address_sender, full_address_to, subject, body_download_status, mail_size, flags_seen_field, flags_deleted_field, flags_flagged_field, flags_answered_field, flags_recent_field, flags_draft_field, flags_forwarded_field, DRM_status, priority, save_status, lock_status, attachment_count, inline_content_count, date_time, preview_text, thread_id, thread_item_count, meeting_request_status, message_class, smime_type, scheduled_sending_time, remaining_resend_times, tag_id, eas_data_length, eas_data ";
2831         char *select_query_form = "SELECT %s FROM mail_tbl ";
2832         char *target_field = NULL;
2833         char *sql_query_string = NULL;
2834         email_mail_list_item_t *mail_list_item_from_tbl = NULL;
2835         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2836
2837         EM_IF_NULL_RETURN_VALUE(conditional_clause, false);
2838         EM_IF_NULL_RETURN_VALUE(result_count, false);
2839
2840         EMSTORAGE_START_READ_TRANSACTION(transaction);
2841
2842         /*  select clause */
2843         if (to_get_count) /*  count only */
2844                 target_field = field_mail_id;
2845         else /* mail list in plain form */
2846                 target_field = field_all;
2847
2848         sql_query_string_length = EM_SAFE_STRLEN(select_query_form) + EM_SAFE_STRLEN(target_field) + EM_SAFE_STRLEN(conditional_clause);
2849
2850         if (sql_query_string_length)
2851                 sql_query_string = em_malloc(sql_query_string_length);
2852
2853         if (sql_query_string == NULL) {
2854                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
2855                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2856                 goto FINISH_OFF;
2857         }
2858
2859         cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, sql_query_string_length, select_query_form, target_field);
2860
2861         strncat(sql_query_string, conditional_clause, sql_query_string_length - cur_query);
2862
2863         EM_DEBUG_LOG_DEV("query[%s]", sql_query_string);
2864
2865         /*  performing query */
2866         EM_PROFILE_BEGIN(emstorage_query_mail_list_performing_query);
2867         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2868         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2869                 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2870         EM_PROFILE_END(emstorage_query_mail_list_performing_query);
2871
2872         if (!base_count)
2873                 base_count = ({
2874                                 int i = 0;
2875                                 char *tmp = NULL;
2876                                 for (tmp = field_all; tmp && *(tmp + 1); tmp = index(tmp + 1, ',')) i++ ;
2877                                 i;
2878                                 });
2879
2880         col_index = base_count;
2881
2882         EM_DEBUG_LOG_DEV("base_count [%d]", base_count);
2883
2884         if (to_get_count) {
2885                 /*  to get count */
2886                 if (!count) {
2887                         EM_DEBUG_LOG_DEV("No mail found...");
2888                         ret = false;
2889                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
2890                         goto FINISH_OFF;
2891                 }
2892                 EM_DEBUG_LOG_DEV("There are [%d] mails.", count);
2893         } else {
2894                 /*  to get mail list */
2895                 if (!count) {
2896                         EM_DEBUG_LOG_DEV("No mail found...");
2897                         ret = false;
2898                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
2899                         goto FINISH_OFF;
2900                 }
2901
2902                 EM_DEBUG_LOG_DEV("There are [%d] mails.", count);
2903                 if (!(mail_list_item_from_tbl = (email_mail_list_item_t*)em_malloc(sizeof(email_mail_list_item_t) * count))) {
2904                         EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
2905                         error = EMAIL_ERROR_OUT_OF_MEMORY;
2906                         goto FINISH_OFF;
2907                 }
2908
2909                 EM_PROFILE_BEGIN(emstorage_query_mail_list_loop);
2910                 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >> ");
2911                 for (i = 0; i < count; i++) {
2912                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_id), col_index++);
2913                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].account_id), col_index++);
2914                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mailbox_id), col_index++);
2915                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].mailbox_type), col_index++);
2916                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].full_address_from, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2917                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_sender, MAX_EMAIL_ADDRESS_LENGTH, 1, col_index++);
2918                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_recipient, STRING_LENGTH_FOR_DISPLAY,  1, col_index++);
2919                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].subject, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
2920                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].body_download_status), col_index++);
2921                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_size), col_index++);
2922                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_seen_field), col_index++);
2923                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_deleted_field), col_index++);
2924                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_flagged_field), col_index++);
2925                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_answered_field), col_index++);
2926                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_recent_field), col_index++);
2927                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_draft_field), col_index++);
2928                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_forwarded_field), col_index++);
2929                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].DRM_status), col_index++);
2930                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].priority), col_index++);
2931                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].save_status), col_index++);
2932                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].lock_status), col_index++);
2933                         _get_table_field_data_int(result, &local_attachment_count, col_index++);
2934                         _get_table_field_data_int(result, &local_inline_content_count, col_index++);
2935                         _get_table_field_data_time_t(result, &(mail_list_item_from_tbl[i].date_time), col_index++);
2936                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].preview_text, MAX_PREVIEW_TEXT_LENGTH, 1, col_index++);
2937                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_id), col_index++);
2938                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_item_count), col_index++);
2939                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].meeting_request_status), col_index++);
2940                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].message_class), col_index++);
2941                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].smime_type), col_index++);
2942                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].scheduled_sending_time), col_index++);
2943                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].remaining_resend_times), col_index++);
2944                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].tag_id), col_index++);
2945                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].eas_data_length), col_index++);
2946                         _get_table_field_data_blob(result, (void**)&(mail_list_item_from_tbl[i].eas_data), mail_list_item_from_tbl[i].eas_data_length, col_index++);
2947
2948                         mail_list_item_from_tbl[i].attachment_count = (local_attachment_count > 0) ? 1 : 0;
2949                 }
2950                 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >> ", count);
2951                 EM_PROFILE_END(emstorage_query_mail_list_loop);
2952         }
2953
2954         ret = true;
2955
2956 FINISH_OFF:
2957         EM_DEBUG_LOG("MAIL_COUNT [%d]", count);
2958
2959         if (result)
2960                 sqlite3_free_table(result);
2961
2962         if (to_get_count)
2963                 *result_count = count;
2964         else {
2965                 if (ret == true) {
2966                         if (result_mail_list)
2967                                 *result_mail_list = mail_list_item_from_tbl;
2968                         *result_count = count;
2969                 } else
2970                         EM_SAFE_FREE(mail_list_item_from_tbl);
2971         }
2972
2973         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
2974
2975 //      sqlite3_db_release_memory(local_db_handle);
2976
2977         EM_SAFE_FREE(sql_query_string);
2978         EM_SAFE_FREE(date_time_string);
2979
2980         if (err_code != NULL)
2981                 *err_code = error;
2982
2983         EM_PROFILE_END(emstorage_query_mail_list_func);
2984         EM_DEBUG_FUNC_END("ret [%d]", ret);
2985         return ret;
2986 }
2987
2988
2989 INTERNAL_FUNC int emstorage_query_mail_tbl(char *multi_user_name, const char *conditional_clause, int transaction, emstorage_mail_tbl_t** result_mail_tbl, int *result_count, int *err_code)
2990 {
2991         EM_DEBUG_FUNC_BEGIN("conditional_clause[%s], result_mail_tbl[%p], result_count [%p], transaction[%d], err_code[%p]", conditional_clause, result_mail_tbl, result_count, transaction, err_code);
2992
2993         if (!conditional_clause || !result_mail_tbl || !result_count) {
2994                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: [%p] [%p] [%p]", conditional_clause, result_mail_tbl, result_mail_tbl);
2995                 if (err_code != NULL)
2996                         *err_code = EMAIL_ERROR_INVALID_PARAM;
2997                 return false;
2998         }
2999
3000         int i, col_index = FIELD_COUNT_OF_MAIL_TBL, rc, ret = false, count;
3001         int error = EMAIL_ERROR_NONE;
3002         char **result = NULL, sql_query_string[QUERY_SIZE] = {0, };
3003         emstorage_mail_tbl_t* p_data_tbl = NULL;
3004         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3005
3006         EMSTORAGE_START_READ_TRANSACTION(transaction);
3007
3008         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl %s", conditional_clause);
3009
3010         EM_DEBUG_LOG_DEV("Query[%s]", sql_query_string);
3011
3012         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3013         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3014                 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
3015
3016         if (!count) {
3017                 EM_DEBUG_LOG("No mail found...");
3018                 ret = false;
3019                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
3020                 goto FINISH_OFF;
3021         }
3022
3023         EM_DEBUG_LOG("There are [%d] mails.", count);
3024         if (!(p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * count))) {
3025                 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
3026                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3027                 goto FINISH_OFF;
3028         }
3029
3030         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >> ");
3031         for (i = 0; i < count; i++) {
3032                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
3033                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3034                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3035                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_type), col_index++);
3036                 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 1, col_index++);
3037                 _get_table_field_data_time_t (result, &(p_data_tbl[i].date_time), col_index++);
3038                 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
3039                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
3040                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
3041                 _get_table_field_data_string(result, &(p_data_tbl[i].message_id), 0, col_index++);
3042                 _get_table_field_data_int(result, &(p_data_tbl[i].reference_mail_id), col_index++);
3043                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_from), 1, col_index++);
3044                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_reply), 1, col_index++);
3045                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_to), 1, col_index++);
3046                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_cc), 1, col_index++);
3047                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_bcc), 1, col_index++);
3048                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_return), 1, col_index++);
3049                 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_sender), 1, col_index++);
3050                 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_recipient), 1, col_index++);
3051                 _get_table_field_data_string(result, &(p_data_tbl[i].alias_sender), 1, col_index++);
3052                 _get_table_field_data_string(result, &(p_data_tbl[i].alias_recipient), 1, col_index++);
3053                 _get_table_field_data_int(result, &(p_data_tbl[i].body_download_status), col_index++);
3054                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
3055                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
3056                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
3057                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_size), col_index++);
3058                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_seen_field), col_index++);
3059                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_deleted_field), col_index++);
3060                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_flagged_field), col_index++);
3061                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_answered_field), col_index++);
3062                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_recent_field), col_index++);
3063                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_draft_field), col_index++);
3064                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_forwarded_field), col_index++);
3065                 _get_table_field_data_int(result, &(p_data_tbl[i].DRM_status), col_index++);
3066                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].priority), col_index++);
3067                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
3068                 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
3069                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].report_status), col_index++);
3070                 _get_table_field_data_int(result, &(p_data_tbl[i].attachment_count), col_index++);
3071                 _get_table_field_data_int(result, &(p_data_tbl[i].inline_content_count), col_index++);
3072                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
3073                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
3074                 _get_table_field_data_string(result, &(p_data_tbl[i].preview_text), 1, col_index++);
3075                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].meeting_request_status), col_index++);
3076                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].message_class), col_index++);
3077                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].digest_type), col_index++);
3078                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].smime_type), col_index++);
3079                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].scheduled_sending_time), col_index++);
3080                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].remaining_resend_times), col_index++);
3081                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].tag_id), col_index++);
3082                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].replied_time), col_index++);
3083                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].forwarded_time), col_index++);
3084                 _get_table_field_data_string(result, &(p_data_tbl[i].default_charset), 0, col_index++);
3085                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3086                 _get_table_field_data_blob(result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3087                 _get_table_field_data_string(result, &(p_data_tbl[i].user_name), 0, col_index++);
3088         }
3089
3090         ret = true;
3091
3092 FINISH_OFF:
3093         if (result)
3094                 sqlite3_free_table(result);
3095
3096         if (ret) {
3097                 *result_mail_tbl = p_data_tbl;
3098                 *result_count = count;
3099         } else {
3100                 *result_mail_tbl = NULL;
3101                 *result_count = 0;
3102         }
3103
3104         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3105
3106 //      sqlite3_db_release_memory(local_db_handle);
3107
3108         if (err_code != NULL)
3109                 *err_code = error;
3110
3111         EM_DEBUG_FUNC_END("ret [%d]", ret);
3112         return ret;
3113 }
3114
3115 #ifdef __FEATURE_BODY_SEARCH__
3116 INTERNAL_FUNC int emstorage_query_mail_text_tbl(char *multi_user_name, const char *conditional_clause, int transaction, emstorage_mail_text_tbl_t** result_mail_text_tbl, int *result_count, int *err_code)
3117 {
3118         EM_DEBUG_FUNC_BEGIN("conditional_clause[%s], result_mail_text_tbl[%p], result_count [%p], transaction[%d], err_code[%p]", conditional_clause, result_mail_text_tbl, result_count, transaction, err_code);
3119
3120         if (!conditional_clause || !result_mail_text_tbl || !result_count) { /*prevent 50930*/
3121                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3122                 if (err_code != NULL)
3123                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3124                 return false;
3125         }
3126
3127         int i = 0;
3128         int rc = 0;
3129         int ret = false;
3130         int count = 0;
3131         int col_index = FIELD_COUNT_OF_MAIL_TEXT_TBL;
3132         int error = EMAIL_ERROR_NONE;
3133         char **result = NULL;
3134         char sql_query_string[QUERY_SIZE] = {0, };
3135         emstorage_mail_text_tbl_t* p_data_tbl = NULL;
3136         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3137
3138         EMSTORAGE_START_READ_TRANSACTION(transaction);
3139
3140         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_text_tbl %s", conditional_clause);
3141
3142         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
3143
3144         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3145         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3146                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3147
3148         if (!count) {
3149                 EM_DEBUG_LOG("No mail found...");
3150                 ret = false;
3151                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
3152                 goto FINISH_OFF;
3153         }
3154
3155         EM_DEBUG_LOG("There are [%d] mails.", count);
3156         if (!(p_data_tbl = (emstorage_mail_text_tbl_t *)em_malloc(sizeof(emstorage_mail_text_tbl_t) * count))) {
3157                 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_text_tbl_t failed...");
3158                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3159                 goto FINISH_OFF;
3160         }
3161
3162         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
3163         for (i = 0; i < count; i++) {
3164                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
3165                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3166                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3167                 _get_table_field_data_string(result, &(p_data_tbl[i].body_text), 0, col_index++);
3168         }
3169
3170         ret = true;
3171
3172 FINISH_OFF:
3173         if (result)
3174                 sqlite3_free_table(result);
3175
3176         if (ret == true) {
3177                 *result_mail_text_tbl = p_data_tbl;
3178                 *result_count = count;
3179         } else
3180                 EM_SAFE_FREE(p_data_tbl);
3181
3182         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3183
3184 //      sqlite3_db_release_memory(local_db_handle);
3185
3186         if (err_code != NULL)
3187                 *err_code = error;
3188
3189         EM_DEBUG_FUNC_END("ret [%d]", ret);
3190         return ret;
3191 }
3192 #endif
3193
3194 INTERNAL_FUNC int emstorage_query_mailbox_tbl(char *multi_user_name, const char *input_conditional_clause, const char *input_ordering_clause, int input_get_mail_count,  int input_transaction, emstorage_mailbox_tbl_t **output_mailbox_list, int *output_mailbox_count)
3195 {
3196         EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_ordering_clause [%p], input_get_mail_count[%d], input_transaction[%d], output_mailbox_list[%p], output_mailbox_count[%d]", input_conditional_clause, input_ordering_clause, input_get_mail_count, input_transaction, output_mailbox_list, output_mailbox_count);
3197
3198         int i = 0;
3199         int rc;
3200         int count = 0;
3201         int col_index = 0;
3202         int error = EMAIL_ERROR_NONE;
3203         int read_count = 0;
3204         int total_count = 0;
3205         char **result;
3206         char sql_query_string[QUERY_SIZE] = {0, };
3207         char *fields = "MBT.mailbox_id, MBT.account_id, local_yn, MBT.mailbox_name, MBT.mailbox_type, alias, deleted_flag, modifiable_yn, total_mail_count_on_server, has_archived_mails, mail_slot_size, no_select, last_sync_time, MBT.eas_data_length, MBT.eas_data ";
3208         emstorage_mailbox_tbl_t* p_data_tbl = NULL;
3209         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3210
3211         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
3212
3213         if (input_get_mail_count == 0) {        /* without mail count */
3214                 col_index = 15;
3215                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT %s FROM mail_box_tbl AS MBT %s %s", fields, input_conditional_clause, input_ordering_clause);
3216         } else {        /* with read count and total count */
3217                 col_index = 17;
3218                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT %s, total, read  FROM mail_box_tbl AS MBT LEFT OUTER JOIN (SELECT mailbox_id, count(mail_id) AS total, SUM(flags_seen_field) AS read FROM mail_tbl WHERE flags_deleted_field = 0 GROUP BY mailbox_id) AS MT ON MBT.mailbox_id = MT.mailbox_id %s %s", fields, input_conditional_clause, input_ordering_clause);
3219         }
3220
3221         EM_DEBUG_LOG_DEV("query[%s]", sql_query_string);
3222
3223         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
3224         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3225                 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string))
3226
3227         EM_DEBUG_LOG_DEV("result count [%d]", count);
3228
3229         if (count == 0) {
3230                 EM_DEBUG_LOG_SEC("Can't find mailbox query[%s]", sql_query_string);
3231                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
3232                 goto FINISH_OFF;
3233         }
3234
3235         if ((p_data_tbl = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t) * count)) == NULL) {
3236                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
3237                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3238                 goto FINISH_OFF;
3239         }
3240
3241         for (i = 0; i < count; i++) {
3242                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3243                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3244                 _get_table_field_data_int(result, &(p_data_tbl[i].local_yn), col_index++);
3245                 _get_table_field_data_string(result, &(p_data_tbl[i].mailbox_name), 0, col_index++);
3246                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].mailbox_type), col_index++);
3247                 _get_table_field_data_string(result, &(p_data_tbl[i].alias), 0, col_index++);
3248                 _get_table_field_data_int(result, &(p_data_tbl[i].deleted_flag), col_index++);
3249                 _get_table_field_data_int(result, &(p_data_tbl[i].modifiable_yn), col_index++);
3250                 _get_table_field_data_int(result, &(p_data_tbl[i].total_mail_count_on_server), col_index++);
3251                 _get_table_field_data_int(result, &(p_data_tbl[i].has_archived_mails), col_index++);
3252                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_slot_size), col_index++);
3253                 _get_table_field_data_int(result, &(p_data_tbl[i].no_select), col_index++);
3254                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].last_sync_time), col_index++);
3255                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3256                 _get_table_field_data_blob(result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3257
3258                 if (input_get_mail_count == 1) {
3259                         _get_table_field_data_int(result, &(total_count), col_index++);
3260                         p_data_tbl[i].total_mail_count_on_local = total_count;
3261                         _get_table_field_data_int(result, &(read_count), col_index++);
3262                         p_data_tbl[i].unread_count = total_count - read_count;                  /*  return unread count, NOT  */
3263                 }
3264         }
3265
3266
3267 FINISH_OFF:
3268         if (result)
3269                 sqlite3_free_table(result);
3270
3271         if (error == EMAIL_ERROR_NONE) {
3272                 *output_mailbox_list = p_data_tbl;
3273                 *output_mailbox_count = count;
3274                 EM_DEBUG_LOG("Mailbox Count [%d]", count);
3275         }
3276
3277         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
3278
3279 //      sqlite3_db_release_memory(local_db_handle);
3280
3281         EM_DEBUG_FUNC_END("error [%d]", error);
3282         return error;
3283 }
3284
3285 /* Query series --------------------------------------------------------------*/
3286 INTERNAL_FUNC int emstorage_check_duplicated_account(char *multi_user_name, email_account_t* account, int transaction, int *err_code)
3287 {
3288         EM_DEBUG_FUNC_BEGIN();
3289         int rc = -1, ret = false;
3290         int error = EMAIL_ERROR_NONE;
3291         char **result;
3292         int count;
3293         char sql_query_string[QUERY_SIZE] = {0, };
3294         sqlite3 *local_db_handle = NULL;
3295
3296         if (account == NULL || account->user_email_address == NULL || account->incoming_server_user_name == NULL || account->incoming_server_address == NULL ||
3297                 account->outgoing_server_user_name == NULL || account->outgoing_server_address == NULL) {
3298                 if (err_code != NULL)
3299                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3300                 return false;
3301         }
3302
3303         local_db_handle = emstorage_get_db_connection(multi_user_name);
3304
3305         EMSTORAGE_START_READ_TRANSACTION(transaction);
3306
3307         SNPRINTF(sql_query_string, sizeof(sql_query_string),
3308                 "SELECT COUNT(*) FROM mail_account_tbl "
3309                 " WHERE "
3310                 " user_email_address = '%s' AND "
3311                 " incoming_server_user_name = '%s' AND "
3312                 " incoming_server_type = %d AND "
3313                 " incoming_server_address = '%s' AND "
3314                 " outgoing_server_user_name = '%s' AND "
3315                 " outgoing_server_type = %d AND "
3316                 " outgoing_server_address = '%s'; ",
3317                 account->user_email_address,
3318                 account->incoming_server_user_name, account->incoming_server_type, account->incoming_server_address,
3319                 account->outgoing_server_user_name, account->outgoing_server_type, account->outgoing_server_address
3320         );
3321         EM_DEBUG_LOG_SEC("Query[%s]", sql_query_string);
3322         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
3323         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3324                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3325
3326         count = atoi(result[1]);
3327         sqlite3_free_table(result);
3328
3329         EM_DEBUG_LOG("Count of Duplicated Account Information: [%d]", count);
3330
3331         if (count == 0) {       /*  not duplicated account */
3332                 ret = true;
3333                 EM_DEBUG_LOG_SEC("NOT duplicated account: user_email_address[%s]", account->user_email_address);
3334         } else {        /*  duplicated account */
3335                 ret = false;
3336                 EM_DEBUG_LOG_SEC("The same account already exists. Duplicated account: user_email_address[%s]", account->user_email_address);
3337                 error = EMAIL_ERROR_ALREADY_EXISTS;
3338         }
3339
3340 FINISH_OFF:
3341
3342         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3343
3344         if (err_code != NULL)
3345                 *err_code = error;
3346
3347         EM_DEBUG_FUNC_END("ret [%d]", ret);
3348         return ret;
3349
3350 }
3351
3352 INTERNAL_FUNC int emstorage_get_account_count(char *multi_user_name, int *count, int transaction, int *err_code)
3353 {
3354         EM_DEBUG_FUNC_BEGIN();
3355
3356         if (!count) {
3357                 if (err_code != NULL)
3358                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3359                 return false;
3360         }
3361
3362         int rc = -1, ret = false;
3363         int error = EMAIL_ERROR_NONE;
3364
3365         DB_STMT hStmt = NULL;
3366         char sql_query_string[QUERY_SIZE] = {0, };
3367
3368
3369         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3370         EMSTORAGE_START_READ_TRANSACTION(transaction);
3371         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_account_tbl");
3372         EM_DEBUG_LOG_SEC("SQL STMT [%s]", sql_query_string);
3373         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string,
3374                                                           EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3375         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
3376         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3377                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3378
3379         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3380         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3381                 ("sqlite3_step fail:%d", rc));
3382
3383         *count = sqlite3_column_int(hStmt, 0);
3384
3385         ret = true;
3386
3387 FINISH_OFF:
3388
3389         if (hStmt != NULL) {
3390                 rc = sqlite3_finalize(hStmt);
3391                 hStmt = NULL;
3392                 if (rc != SQLITE_OK) {
3393                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
3394                         error = EMAIL_ERROR_DB_FAILURE;
3395                 }
3396         }
3397
3398         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3399
3400         if (err_code != NULL)
3401                 *err_code = error;
3402
3403         EM_DEBUG_FUNC_END("ret [%d]", ret);
3404         return ret;
3405 }
3406
3407 INTERNAL_FUNC int emstorage_get_account_list(char *multi_user_name, int *select_num, emstorage_account_tbl_t **account_list, int transaction, int with_password, int *err_code)
3408 {
3409         EM_DEBUG_FUNC_BEGIN();
3410
3411         int i = 0, count = 0, rc = -1, ret = false;
3412         int field_index = 0;
3413         int sql_len = 0;
3414         int error = EMAIL_ERROR_NONE;
3415         emstorage_account_tbl_t *p_data_tbl = NULL;
3416         char sql_query_string[QUERY_SIZE] = {0, };
3417         char *sql = "SELECT count(*) FROM mail_account_tbl;";
3418         char **result;
3419         sqlite3 *local_db_handle = NULL;
3420         DB_STMT hStmt = NULL;
3421
3422         if (!select_num || !account_list) {
3423                 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, account_list);
3424                 if (err_code != NULL)
3425                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3426                 return false;
3427         }
3428
3429         local_db_handle = emstorage_get_db_connection(multi_user_name);
3430
3431         EMSTORAGE_START_READ_TRANSACTION(transaction);
3432
3433         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
3434         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3435                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
3436
3437         count = atoi(result[1]);
3438         sqlite3_free_table(result);
3439
3440         EM_DEBUG_LOG_DEV("count = %d", rc);
3441
3442         if (count == 0) {
3443                 EM_DEBUG_LOG("no account found...");
3444                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3445                 ret = true;
3446                 goto FINISH_OFF;
3447         }
3448         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
3449         sql_len = EM_SAFE_STRLEN(sql_query_string);
3450
3451         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
3452                 "account_id, "
3453                 "account_name, "
3454                 "logo_icon_path, "
3455                 "user_data, "
3456                 "user_data_length, "
3457                 "account_svc_id, "
3458                 "sync_status, "
3459                 "sync_disabled, "
3460                 "default_mail_slot_size, "
3461                 "roaming_option, "
3462                 "color_label, "
3463                 "user_display_name, "
3464                 "user_email_address, "
3465                 "reply_to_address, "
3466                 "return_address, "
3467                 "incoming_server_type, "
3468                 "incoming_server_address, "
3469                 "incoming_server_port_number, "
3470                 "incoming_server_user_name, "
3471                 "incoming_server_password, "
3472                 "incoming_server_secure_connection, "
3473                 "incoming_server_authentication_method, "
3474                 "retrieval_mode, "
3475                 "keep_mails_on_pop_server_after_download, "
3476                 "check_interval, "
3477                 "auto_download_size, "
3478                 "peak_interval, "
3479                 "peak_days, "
3480                 "peak_start_time, "
3481                 "peak_end_time, "
3482                 "outgoing_server_type, "
3483                 "outgoing_server_address, "
3484                 "outgoing_server_port_number, "
3485                 "outgoing_server_user_name, "
3486                 "outgoing_server_password, "
3487                 "outgoing_server_secure_connection, "
3488                 "outgoing_server_need_authentication, "
3489                 "outgoing_server_use_same_authenticator, "
3490                 "priority, "
3491                 "keep_local_copy, "
3492                 "req_delivery_receipt, "
3493                 "req_read_receipt, "
3494                 "download_limit, "
3495                 "block_address, "
3496                 "block_subject, "
3497                 "display_name_from, "
3498                 "reply_with_body, "
3499                 "forward_with_files, "
3500                 "add_myname_card, "
3501                 "add_signature, "
3502                 "signature, "
3503                 "add_my_address_to_bcc, "
3504                 "notification_status, "
3505                 "vibrate_status, "
3506                 "display_content_status, "
3507                 "default_ringtone_status, "
3508                 "alert_ringtone_path, "
3509                 "auto_resend_times, "
3510                 "outgoing_server_size_limit, "
3511                 "wifi_auto_download, "
3512                 "pop_before_smtp, "
3513                 "incoming_server_requires_apop, "
3514                 "smime_type, "
3515                 "certificate_path, "
3516                 "cipher_type, "
3517                 "digest_type"
3518         );
3519
3520         sql_len = EM_SAFE_STRLEN(sql_query_string);
3521
3522         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " FROM mail_account_tbl ORDER BY account_id");
3523
3524         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3525
3526         EM_DEBUG_LOG_DEV("After sqlite3_prepare_v2 hStmt = %p", hStmt);
3527         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3528                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3529
3530         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3531         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3532                 ("sqlite3_step fail:%d", rc));
3533         if (rc == SQLITE_DONE) {
3534                 EM_DEBUG_EXCEPTION("no account found...");
3535
3536                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3537                 count = 0;
3538                 ret = true;
3539                 goto FINISH_OFF;
3540         }
3541
3542         if (!(p_data_tbl = (emstorage_account_tbl_t*)malloc(sizeof(emstorage_account_tbl_t) * count))) {
3543                 EM_DEBUG_EXCEPTION("malloc failed...");
3544                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3545                 goto FINISH_OFF;
3546         }
3547         memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * count);
3548         for (i = 0; i < count; i++) {
3549                 /*  get recordset */
3550                 field_index = 0;
3551                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_id), field_index++);
3552                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].account_name), 0, field_index++);
3553                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].logo_icon_path), 0, field_index++);
3554                 _get_stmt_field_data_blob(hStmt, &(p_data_tbl[i].user_data), field_index++);
3555                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].user_data_length), field_index++);
3556                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_svc_id), field_index++);
3557                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_status), field_index++);
3558                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_disabled), field_index++);
3559                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].default_mail_slot_size), field_index++);
3560                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].roaming_option), field_index++);
3561                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].color_label), field_index++);
3562                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_display_name), 0, field_index++);
3563                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_email_address), 0, field_index++);
3564                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].reply_to_address), 0, field_index++);
3565                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].return_address), 0, field_index++);
3566                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].incoming_server_type), field_index++);
3567                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_address), 0, field_index++);
3568                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_port_number), field_index++);
3569                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_user_name), 0, field_index++);
3570                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_password), 0, field_index++);
3571                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_secure_connection), field_index++);
3572                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].incoming_server_authentication_method), field_index++);
3573                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].retrieval_mode), field_index++);
3574                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].keep_mails_on_pop_server_after_download), field_index++);
3575                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].check_interval), field_index++);
3576                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_download_size), field_index++);
3577                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_interval), field_index++);
3578                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_days), field_index++);
3579                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_start_time), field_index++);
3580                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_end_time), field_index++);
3581                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].outgoing_server_type), field_index++);
3582                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_address), 0, field_index++);
3583                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_port_number), field_index++);
3584                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_user_name), 0, field_index++);
3585                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_password), 0, field_index++);
3586                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_secure_connection), field_index++);
3587                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_need_authentication), field_index++);
3588                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_use_same_authenticator), field_index++);
3589                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.priority), field_index++);
3590                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.keep_local_copy), field_index++);
3591                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_delivery_receipt), field_index++);
3592                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_read_receipt), field_index++);
3593                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.download_limit), field_index++);
3594                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_address), field_index++);
3595                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_subject), field_index++);
3596                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.display_name_from), 0, field_index++);
3597                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.reply_with_body), field_index++);
3598                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.forward_with_files), field_index++);
3599                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_myname_card), field_index++);
3600                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_signature), field_index++);
3601                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.signature), 0, field_index++);
3602                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.add_my_address_to_bcc), field_index++);
3603                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.notification_status), field_index++);
3604                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.vibrate_status), field_index++);
3605                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.display_content_status), field_index++);
3606                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.default_ringtone_status), field_index++);
3607                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.alert_ringtone_path), 0, field_index++);
3608                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_resend_times), field_index++);
3609                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_size_limit), field_index++);
3610                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].wifi_auto_download), field_index++);
3611                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].pop_before_smtp), field_index++);
3612                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_requires_apop), field_index++);
3613                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].smime_type), field_index++);
3614                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].certificate_path), 0, field_index++);
3615                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].cipher_type), field_index++);
3616                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].digest_type), field_index++);
3617
3618                 /* EAS passwd is not accessible */
3619                 if (with_password == true && p_data_tbl[i].incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
3620                         /*  get password from the secure storage */
3621                         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3622                         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3623
3624                         EM_SAFE_FREE(p_data_tbl[i].incoming_server_password);
3625                         EM_SAFE_FREE(p_data_tbl[i].outgoing_server_password);
3626
3627                         /*  get password file name */
3628                         error = _get_password_file_name(multi_user_name, p_data_tbl[i].account_id,
3629                                                    recv_password_file_name,
3630                                                    send_password_file_name);
3631                         if (error != EMAIL_ERROR_NONE) {
3632                                 EM_DEBUG_EXCEPTION("_get_password_file_name error [%d]", error);
3633                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3634                                 goto FINISH_OFF;
3635                         }
3636
3637                         /*  read password from secure storage */
3638                         error = _read_password_from_secure_storage(recv_password_file_name,
3639                                           &(p_data_tbl[i].incoming_server_password));
3640                         if (error < 0) {
3641                                 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s] error [%d]",
3642                                                     recv_password_file_name, error);
3643                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3644                                 goto FINISH_OFF;
3645                         }
3646                         error = _read_password_from_secure_storage(send_password_file_name,
3647                                           &(p_data_tbl[i].outgoing_server_password));
3648                         if (error < 0) {
3649                                 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s]  error [%d]",
3650                                                      send_password_file_name, error);
3651                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3652                                 goto FINISH_OFF;
3653                         }
3654                 }
3655
3656                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3657                 EM_DEBUG_LOG_DEV("after sqlite3_step(), i = %d, rc = %d.", i,  rc);
3658                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3659                         ("sqlite3_step fail:%d", rc));
3660         }
3661
3662         ret = true;
3663
3664 FINISH_OFF:
3665         if (ret == true) {
3666                 *account_list = p_data_tbl;
3667                 *select_num = count;
3668                 EM_DEBUG_LOG_DEV("COUNT : %d", count);
3669         } else if (p_data_tbl != NULL)
3670                 emstorage_free_account(&p_data_tbl, count, NULL);
3671
3672         if (hStmt != NULL) {
3673                 rc = sqlite3_finalize(hStmt);
3674                 hStmt = NULL;
3675                 if (rc != SQLITE_OK) {
3676                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
3677                         error = EMAIL_ERROR_DB_FAILURE;
3678                 }
3679         }
3680
3681         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3682
3683         if (err_code != NULL)
3684                 *err_code = error;
3685
3686         EM_DEBUG_FUNC_END("ret [%d]", ret);
3687         return ret;
3688 }
3689
3690 INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(char *multi_user_name,
3691                                                                                                                         char *server_mail_id,
3692                                                                                                                         int mailbox_id,
3693                                                                                                                         emstorage_mail_tbl_t **mail,
3694                                                                                                                         int transaction,
3695                                                                                                                         int *err_code)
3696 {
3697         EM_DEBUG_FUNC_BEGIN_SEC("mailbox_id [%d], server_mail_id[%s], mail[%p], transaction[%d], err_code[%p]",
3698                                                         mailbox_id, server_mail_id, mail, transaction, err_code);
3699
3700         int ret = false, error = EMAIL_ERROR_NONE, result_count;
3701         char conditional_clause[QUERY_SIZE] = {0, };
3702         emstorage_mail_tbl_t* p_data_tbl = NULL;
3703
3704         if (!server_mail_id || !mail) {
3705                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3706                 error = EMAIL_ERROR_INVALID_PARAM;
3707                 goto FINISH_OFF;
3708         }
3709
3710         sqlite3_snprintf(sizeof(conditional_clause), conditional_clause, "WHERE server_mail_id = '%q'", server_mail_id);
3711
3712         if (mailbox_id > 0) {
3713                 SNPRINTF(conditional_clause + strlen(conditional_clause),
3714                                         QUERY_SIZE - strlen(conditional_clause),
3715                                         " AND mailbox_id = %d",
3716                                         mailbox_id);
3717         }
3718
3719         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
3720         if (!emstorage_query_mail_tbl(multi_user_name,
3721                                                                         conditional_clause,
3722                                                                         transaction,
3723                                                                         &p_data_tbl,
3724                                                                         &result_count,
3725                                                                         &error)) {
3726                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3727                 goto FINISH_OFF;
3728         }
3729
3730         ret = true;
3731
3732 FINISH_OFF:
3733
3734         if (ret == true)
3735                 *mail = p_data_tbl;
3736
3737         if (err_code != NULL)
3738                 *err_code = error;
3739
3740         EM_DEBUG_FUNC_END("ret [%d]", ret);
3741         return ret;
3742 }
3743
3744
3745 static int _write_conditional_clause_for_getting_mail_list(int account_id, int mailbox_id, email_email_address_list_t* addr_list, int thread_id, int start_index, int limit_count, int search_type, const char *search_value, email_sort_type_t sorting, bool input_except_delete_flagged_mails, char *conditional_clause_string, int buffer_size, int *err_code)
3746 {
3747         int cur_clause = 0, conditional_clause_count = 0, i = 0;
3748
3749         if (account_id < ALL_ACCOUNT) {
3750                 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3751                 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3752         }
3753
3754         /*  where clause */
3755         if (account_id != ALL_ACCOUNT) {
3756                 cur_clause += (conditional_clause_count++ == 0) ?
3757                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE account_id = %d", account_id) :
3758                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND account_id = %d", account_id);
3759         }
3760
3761         if (mailbox_id > 0) {
3762                 cur_clause += (conditional_clause_count++ == 0) ?
3763                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_id = %d", mailbox_id) :
3764                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_id = %d", mailbox_id);
3765         }
3766
3767         if (thread_id > 0) {
3768                 cur_clause += (conditional_clause_count++ == 0) ?
3769                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_id = %d ", thread_id) :
3770                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_id = %d ", thread_id);
3771         } else if (thread_id == EMAIL_LIST_TYPE_THREAD) {
3772                 cur_clause += (conditional_clause_count++ == 0) ?
3773                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_item_count > 0") :
3774                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_item_count > 0");
3775         } else if (thread_id == EMAIL_LIST_TYPE_LOCAL) {
3776                 cur_clause += (conditional_clause_count++ == 0) ?
3777                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE server_mail_status = 0") :
3778                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND server_mail_status = 0");
3779         } else if (thread_id == EMAIL_LIST_TYPE_UNREAD) {
3780                 cur_clause += (conditional_clause_count++ == 0) ?
3781                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_seen_field == 0") :
3782                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_seen_field == 0");
3783         }
3784
3785         /*  EM_DEBUG_LOG("where clause added [%s]", conditional_clause_string); */
3786         if (addr_list && addr_list->address_count > 0) {
3787                 if (!addr_list->address_type) {
3788                         cur_clause += (conditional_clause_count++ == 0) ?
3789                                 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE email_address_sender IN(\"%s\"", addr_list->address_list[0]) :
3790                                 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND email_address_sender IN(\"%s\"", addr_list->address_list[0]);
3791
3792                         for (i = 1; i < addr_list->address_count; i++)
3793                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3794
3795                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3796                 } else {
3797                         cur_clause += (conditional_clause_count++ == 0) ?
3798                                 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE full_address_to IN(\"%s\") or full_address_cc IN(\"%s\"", addr_list->address_list[0], addr_list->address_list[0]) :
3799                                 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND full_address_to IN(\"%s\") or full_address_cc IN(\"%s\"", addr_list->address_list[0], addr_list->address_list[0]);
3800
3801                         for (i = 1; i < addr_list->address_count; i++)
3802                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3803
3804                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3805                 }
3806         }
3807
3808         if (input_except_delete_flagged_mails) {
3809                 cur_clause += (conditional_clause_count++ == 0) ?
3810                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_deleted_field = 0") :
3811                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_deleted_field = 0");
3812         }
3813
3814         if (search_value) {
3815                 switch (search_type) {
3816                         case EMAIL_SEARCH_FILTER_SUBJECT:
3817                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3818                                         " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3819                                 break;
3820                         case EMAIL_SEARCH_FILTER_SENDER:
3821                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3822                                         " %s  ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3823                                         ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3824                                 break;
3825                         case EMAIL_SEARCH_FILTER_RECIPIENT:
3826                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3827                                         " %s ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3828                                         "       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3829                                         "       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3830                                         ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value);
3831                                 break;
3832                         case EMAIL_SEARCH_FILTER_ALL:
3833                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3834                                         " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\' "
3835                                         "       OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3836                                         "                       OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3837                                         "                       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3838                                         "                       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3839                                         "               ) "
3840                                         "       )"
3841                                         ")", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value, search_value, search_value);
3842                                 break;
3843                 }
3844         }
3845
3846         /*  EM_DEBUG_LOG("where clause [%s]", conditional_clause_string); */
3847         static char sorting_str[][50] = {
3848                          " ORDER BY date_time DESC",                                             /* case EMAIL_SORT_DATETIME_HIGH: */
3849                          " ORDER BY date_time ASC",                                               /* case EMAIL_SORT_DATETIME_LOW: */
3850                          " ORDER BY full_address_from DESC, date_time DESC", /* case EMAIL_SORT_SENDER_HIGH: */
3851                          " ORDER BY full_address_from ASC, date_time DESC",  /* case EMAIL_SORT_SENDER_LOW: */
3852                          " ORDER BY full_address_to DESC, date_time DESC",   /* case EMAIL_SORT_RCPT_HIGH: */
3853                          " ORDER BY full_address_to ASC, date_time DESC",       /* case EMAIL_SORT_RCPT_LOW: */
3854                          " ORDER BY subject DESC, date_time DESC",                 /* case EMAIL_SORT_SUBJECT_HIGH: */
3855                          " ORDER BY subject ASC, date_time DESC",                       /* case EMAIL_SORT_SUBJECT_LOW: */
3856                          " ORDER BY priority DESC, date_time DESC",               /* case EMAIL_SORT_PRIORITY_HIGH: */
3857                          " ORDER BY priority ASC, date_time DESC",                 /* case EMAIL_SORT_PRIORITY_LOW: */
3858                          " ORDER BY attachment_count DESC, date_time DESC",  /* case EMAIL_SORT_ATTACHMENT_HIGH: */
3859                          " ORDER BY attachment_count ASC, date_time DESC",   /* case EMAIL_SORT_ATTACHMENT_LOW: */
3860                          " ORDER BY lock_status DESC, date_time DESC",     /* case EMAIL_SORT_FAVORITE_HIGH: */
3861                          " ORDER BY lock_status ASC, date_time DESC",           /* case EMAIL_SORT_FAVORITE_LOW: */
3862                          " ORDER BY mailbox_id DESC, date_time DESC",     /* case EMAIL_SORT_MAILBOX_ID_HIGH: */
3863                          " ORDER BY mailbox_id ASC, date_time DESC",            /* case EMAIL_SORT_MAILBOX_ID_LOW: */
3864                          " ORDER BY flags_flagged_field DESC, date_time DESC",  /* case EMAIL_SORT_FLAGGED_FLAG_HIGH: */
3865                          " ORDER BY flags_flagged_field ASC, date_time DESC",   /* case EMAIL_SORT_FLAGGED_FLAG_LOW: */
3866                          " ORDER BY flags_seen_field DESC, date_time DESC",     /* case EMAIL_SORT_SEEN_FLAG_HIGH: */
3867                          " ORDER BY flags_seen_field ASC, date_time DESC"       /* case EMAIL_SORT_SEEN_FLAG_LOW: */
3868                          };
3869
3870         if (sorting < EMAIL_SORT_END && sorting >= 0)
3871                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " %s", sorting_str[sorting]);
3872         else
3873                 EM_DEBUG_LOG(" Invalid Sorting order ");
3874
3875         /*  limit clause */
3876         if (start_index != -1 && limit_count != -1)
3877                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " LIMIT %d, %d", start_index, limit_count);
3878
3879         return true;
3880 }
3881
3882
3883 /**
3884   *     emstorage_get_mail_list - Get the mail list information.
3885   *
3886   *
3887   */
3888 INTERNAL_FUNC int emstorage_get_mail_list(char *multi_user_name, int account_id, int mailbox_id, email_email_address_list_t* addr_list, int thread_id, int start_index, int limit_count, int search_type, const char *search_value, email_sort_type_t sorting, int transaction, email_mail_list_item_t** mail_list,  int *result_count,  int *err_code)
3889 {
3890         EM_DEBUG_FUNC_BEGIN();
3891         EM_PROFILE_BEGIN(emstorage_get_mail_list_func);
3892
3893         int ret = false, error = EMAIL_ERROR_NONE;
3894         char conditional_clause_string[QUERY_SIZE] = { 0, };
3895
3896         if (account_id < ALL_ACCOUNT) {
3897                 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3898                 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3899         }
3900         EM_IF_NULL_RETURN_VALUE(result_count, false);
3901
3902         _write_conditional_clause_for_getting_mail_list(account_id, mailbox_id, addr_list, thread_id, start_index, limit_count, search_type, search_value, sorting, true, conditional_clause_string, QUERY_SIZE, &error);
3903
3904         EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
3905
3906         if (!emstorage_query_mail_list(multi_user_name, conditional_clause_string, transaction, mail_list, result_count, &error)) {
3907                 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
3908                 goto FINISH_OFF;
3909         }
3910
3911         ret = true;
3912
3913 FINISH_OFF:
3914         if (err_code != NULL)
3915                 *err_code = error;
3916
3917         EM_PROFILE_END(emstorage_get_mail_list_func);
3918         EM_DEBUG_FUNC_END("ret [%d]", ret);
3919         return ret;
3920 }
3921
3922
3923 /**
3924   *     emstorage_get_mails - Get the Mail list information based on mailbox_name name
3925   *
3926   *
3927   */
3928 INTERNAL_FUNC int emstorage_get_mails(char *multi_user_name, int account_id, int mailbox_id, email_email_address_list_t* addr_list, int thread_id, int start_index, int limit_count, email_sort_type_t sorting,  int transaction, emstorage_mail_tbl_t** mail_list, int *result_count, int *err_code)
3929 {
3930         EM_DEBUG_FUNC_BEGIN();
3931         EM_PROFILE_BEGIN(emStorageGetMails);
3932
3933         int count = 0, ret = false, error = EMAIL_ERROR_NONE;
3934         emstorage_mail_tbl_t *p_data_tbl = NULL;
3935         char conditional_clause_string[QUERY_SIZE] = {0, };
3936
3937         EM_IF_NULL_RETURN_VALUE(mail_list, false);
3938         EM_IF_NULL_RETURN_VALUE(result_count, false);
3939
3940         if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3941                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3942                 if (err_code != NULL)
3943                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3944                 return false;
3945         }
3946
3947         _write_conditional_clause_for_getting_mail_list(account_id, mailbox_id, addr_list, thread_id, start_index, limit_count, 0, NULL, sorting, true, conditional_clause_string, QUERY_SIZE, &error);
3948
3949         EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
3950
3951         if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause_string, transaction, &p_data_tbl, &count,  &error)) {
3952                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3953                 goto FINISH_OFF;
3954         }
3955
3956         ret = true;
3957
3958 FINISH_OFF:
3959
3960         if (ret == true) {
3961                 *mail_list = p_data_tbl;
3962                 *result_count = count;
3963                 EM_DEBUG_LOG("COUNT : %d", count);
3964         } else if (p_data_tbl != NULL)
3965                 emstorage_free_mail(&p_data_tbl, count, NULL);
3966
3967         if (err_code != NULL)
3968                 *err_code = error;
3969
3970         EM_PROFILE_END(emStorageGetMails);
3971         EM_DEBUG_FUNC_END("ret [%d]", ret);
3972         return ret;
3973 }
3974
3975
3976
3977 /**
3978   *     emstorage_get_searched_mail_list - Get the mail list information after filtering
3979   *
3980   *
3981   */
3982 INTERNAL_FUNC int emstorage_get_searched_mail_list(char *multi_user_name, int account_id, int mailbox_id, int thread_id, int search_type, const char *search_value, int start_index, int limit_count, email_sort_type_t sorting, int transaction, email_mail_list_item_t **mail_list,  int *result_count,  int *err_code)
3983 {
3984         EM_DEBUG_FUNC_BEGIN();
3985
3986         int ret = false, error = EMAIL_ERROR_NONE;
3987         char conditional_clause[QUERY_SIZE] = {0, };
3988         char *temp_search_value = NULL;
3989         char *temp_search_value2 = NULL;
3990
3991         EM_IF_NULL_RETURN_VALUE(mail_list, false);
3992         EM_IF_NULL_RETURN_VALUE(result_count, false);
3993
3994         if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
3995                 EM_DEBUG_EXCEPTION("select_num[%p], Mail_list[%p]", result_count, mail_list);
3996                 error = EMAIL_ERROR_INVALID_PARAM;
3997                 goto FINISH_OFF;
3998         }
3999
4000         temp_search_value = em_replace_all_string((char*)search_value, "_", "\\_");
4001         temp_search_value2 = em_replace_all_string(temp_search_value, "%", "\\%");
4002
4003         _write_conditional_clause_for_getting_mail_list(account_id, mailbox_id, NULL, thread_id, start_index, limit_count, search_type, temp_search_value2, sorting, true, conditional_clause, QUERY_SIZE, &error);
4004
4005         EM_DEBUG_LOG("conditional_clause[%s]", conditional_clause);
4006
4007         if (!emstorage_query_mail_list(multi_user_name, conditional_clause, transaction, mail_list, result_count, &error)) {
4008                 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
4009                 goto FINISH_OFF;
4010         }
4011
4012         ret = true;
4013
4014 FINISH_OFF:
4015         EM_DEBUG_LOG("emstorage_get_searched_mail_list finish off");
4016
4017         if (err_code != NULL)
4018                 *err_code = error;
4019
4020         EM_SAFE_FREE(temp_search_value);
4021         EM_SAFE_FREE(temp_search_value2);
4022
4023         EM_DEBUG_FUNC_END("ret [%d]", ret);
4024         return ret;
4025 }
4026
4027
4028 static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name)
4029 {
4030         EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
4031
4032         if (account_id <= 0 || !recv_password_file_name || !send_password_file_name) {
4033                 EM_DEBUG_EXCEPTION("Invalid parameter");
4034                 return EMAIL_ERROR_INVALID_PARAM;
4035         }
4036
4037     EM_DEBUG_LOG("MULTI_USER_NAME : [%s]", multi_user_name);
4038
4039         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
4040                 snprintf(recv_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_recv_%s", account_id, multi_user_name);
4041                 snprintf(send_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_send_%s", account_id, multi_user_name);
4042         } else {
4043                 snprintf(recv_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_recv", account_id);
4044                 snprintf(send_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_send", account_id);
4045         }
4046
4047         EM_DEBUG_FUNC_END();
4048         return EMAIL_ERROR_NONE;
4049 }
4050
4051 static int _read_password_from_secure_storage(char *file_name, char **password)
4052 {
4053         EM_DEBUG_FUNC_BEGIN_SEC("file_name[%s]", file_name);
4054
4055         if (!file_name || !password) {
4056                 EM_DEBUG_EXCEPTION("Invalid Parameter");
4057                 return EMAIL_ERROR_INVALID_PARAM;
4058         }
4059
4060         int ret = EMAIL_ERROR_NONE;
4061
4062         ret = emcore_get_password_in_key_manager(file_name, password);
4063         if (ret != EMAIL_ERROR_NONE) {
4064                 EM_DEBUG_EXCEPTION("emcore_get_password_in_key_manager failed : [%d]", ret);
4065                 goto FINISH_OFF;
4066         }
4067
4068 FINISH_OFF:
4069
4070         EM_DEBUG_FUNC_END("ret [%d]", ret);
4071         return ret;
4072 }
4073
4074 INTERNAL_FUNC int emstorage_get_account_by_id(char *multi_user_name, int account_id, int pulloption, emstorage_account_tbl_t **account, int transaction, int *err_code)
4075 {
4076         EM_DEBUG_FUNC_BEGIN("account_id[%d], pulloption[%d], account[%p], transaction[%d], err_code[%p]", account_id, pulloption, account, transaction, err_code);
4077
4078         if (!account) {
4079                 EM_DEBUG_EXCEPTION("account_id[%d], account[%p]", account_id, account);
4080                 if (err_code != NULL)
4081                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4082                 return false;
4083         }
4084
4085         int ret = false;
4086         int error = EMAIL_ERROR_NONE;
4087         emstorage_account_tbl_t* p_data_tbl = NULL;
4088         DB_STMT hStmt = NULL;
4089         char sql_query_string[QUERY_SIZE] = {0, };
4090         int rc = -1;
4091         int sql_len = 0;
4092         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4093         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4094
4095         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4096
4097         EMSTORAGE_START_READ_TRANSACTION(transaction);
4098
4099         /*  Make query string */
4100         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
4101         sql_len = EM_SAFE_STRLEN(sql_query_string);
4102
4103         if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4104                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4105                         "incoming_server_type,"
4106                         "incoming_server_address,"
4107                         "user_email_address,"
4108                         "incoming_server_user_name,"
4109                         "retrieval_mode,"
4110                         "incoming_server_port_number,"
4111                         "incoming_server_secure_connection,"
4112                         "incoming_server_authentication_method,"
4113                         "outgoing_server_type,"
4114                         "outgoing_server_address,"
4115                         "outgoing_server_port_number,"
4116                         "outgoing_server_need_authentication,"
4117                         "outgoing_server_secure_connection,"
4118                         "outgoing_server_user_name,"
4119                         "user_display_name,"
4120                         "reply_to_address,"
4121                         "return_address,"
4122                         "account_id,"
4123                         "keep_mails_on_pop_server_after_download,"
4124                         "auto_download_size,"
4125                         "peak_interval,"
4126                         "peak_days,"
4127                         "peak_start_time,"
4128                         "peak_end_time,"
4129                         "outgoing_server_use_same_authenticator,"
4130                         "auto_resend_times,"
4131                         "outgoing_server_size_limit,"
4132                         "wifi_auto_download,"
4133                         "pop_before_smtp,"
4134                         "incoming_server_requires_apop,"
4135                         "logo_icon_path,"
4136                         "user_data,"
4137                         "user_data_length,"
4138                         "color_label,"
4139                         "check_interval,"
4140                         "sync_status,");
4141                 sql_len = EM_SAFE_STRLEN(sql_query_string);
4142         }
4143
4144         if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME) {
4145                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " account_name, ");
4146                 sql_len = EM_SAFE_STRLEN(sql_query_string);
4147         }
4148
4149         /*  get from secure storage, not from db */
4150         if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4151                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4152                         "priority,"
4153                         "keep_local_copy,"
4154                         "req_delivery_receipt,"
4155                         "req_read_receipt,"
4156                         "download_limit,"
4157                         "block_address,"
4158                         "block_subject,"
4159                         "display_name_from,"
4160                         "reply_with_body,"
4161                         "forward_with_files,"
4162                         "add_myname_card,"
4163                         "add_signature,"
4164                         "signature,"
4165                         "add_my_address_to_bcc,"
4166                         "notification_status,"
4167                         "vibrate_status,"
4168                         "display_content_status,"
4169                         "default_ringtone_status,"
4170                         "alert_ringtone_path,"
4171                         "account_svc_id,"
4172                         "sync_disabled,"
4173                         "default_mail_slot_size,"
4174                         "roaming_option,"
4175                         "smime_type,"
4176                         "certificate_path,"
4177                         "cipher_type,"
4178                         "digest_type,");
4179
4180                 sql_len = EM_SAFE_STRLEN(sql_query_string);
4181         }
4182
4183         /*  dummy value, FROM WHERE clause */
4184         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "0 FROM mail_account_tbl WHERE account_id = %d", account_id);
4185
4186         /*  FROM clause */
4187         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
4188
4189         /*  execute a sql and count rows */
4190         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4191         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4192                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4193
4194         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4195         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4196                 ("sqlite3_step fail:%d", rc));
4197
4198         if (rc == SQLITE_DONE) {
4199                 EM_DEBUG_EXCEPTION("no matched account found...");
4200                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4201                 goto FINISH_OFF;
4202         }
4203
4204         /*  Assign query result to structure */
4205         if (!(p_data_tbl = (emstorage_account_tbl_t *)malloc(sizeof(emstorage_account_tbl_t) * 1))) {
4206                 EM_DEBUG_EXCEPTION("malloc failed...");
4207                 error = EMAIL_ERROR_OUT_OF_MEMORY;
4208                 goto FINISH_OFF;
4209         }
4210
4211         memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * 1);
4212         int col_index = 0;
4213
4214         if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4215                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_type), col_index++);
4216                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_address),  0, col_index++);
4217                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_email_address), 0, col_index++);
4218                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_user_name), 0, col_index++);
4219                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->retrieval_mode), col_index++);
4220                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_port_number), col_index++);
4221                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_secure_connection), col_index++);
4222                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_authentication_method), col_index++);
4223                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->outgoing_server_type), col_index++);
4224                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_address), 0, col_index++);
4225                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_port_number), col_index++);
4226                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_need_authentication), col_index++);
4227                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_secure_connection), col_index++);
4228                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_user_name), 0, col_index++);
4229                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_display_name), 0, col_index++);
4230                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->reply_to_address), 0, col_index++);
4231                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->return_address), 0, col_index++);
4232                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
4233                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->keep_mails_on_pop_server_after_download), col_index++);
4234                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_download_size), col_index++);
4235                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_interval), col_index++);
4236                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_days), col_index++);
4237                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_start_time), col_index++);
4238                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_end_time), col_index++);
4239                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_use_same_authenticator), col_index++);
4240                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_resend_times), col_index++);
4241                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_size_limit), col_index++);
4242                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->wifi_auto_download), col_index++);
4243                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->pop_before_smtp), col_index++);
4244                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_requires_apop), col_index++);
4245                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->logo_icon_path), 0, col_index++);
4246                 _get_stmt_field_data_blob(hStmt, &p_data_tbl->user_data, col_index++);
4247                 _get_stmt_field_data_int(hStmt, &p_data_tbl->user_data_length, col_index++);
4248                 _get_stmt_field_data_int(hStmt, &p_data_tbl->color_label, col_index++);
4249                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->check_interval), col_index++);
4250                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->sync_status), col_index++);
4251         }
4252
4253         if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME)
4254                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->account_name), 0, col_index++);
4255
4256         if (pulloption & EMAIL_ACC_GET_OPT_PASSWORD) {
4257                 /*  get password file name */
4258                 if ((error = _get_password_file_name(multi_user_name, p_data_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4259                         EM_DEBUG_EXCEPTION("_get_password_file_name failed [%d]", error);
4260                         goto FINISH_OFF;
4261                 }
4262
4263                 /*  read password from secure storage */
4264                 if ((error = _read_password_from_secure_storage(recv_password_file_name, &(p_data_tbl->incoming_server_password))) < 0) {
4265                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4266                         goto FINISH_OFF;
4267                 }
4268
4269                 EM_DEBUG_LOG_DEV("recv_password_file_name[%s], password[%s]", recv_password_file_name,  p_data_tbl->incoming_server_password);
4270
4271                 if (p_data_tbl->outgoing_server_use_same_authenticator == 0) {
4272                         if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl->outgoing_server_password))) < 0) {
4273                                 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4274                                 goto FINISH_OFF;
4275                         }
4276                         EM_DEBUG_LOG_DEV("send_password_file_name[%s], password[%s]", send_password_file_name,  p_data_tbl->outgoing_server_password);
4277                 }
4278         }
4279
4280         if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4281                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.priority), col_index++);
4282                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.keep_local_copy), col_index++);
4283                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_delivery_receipt), col_index++);
4284                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_read_receipt), col_index++);
4285                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.download_limit), col_index++);
4286                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_address), col_index++);
4287                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_subject), col_index++);
4288                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.display_name_from), 0, col_index++);
4289                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.reply_with_body), col_index++);
4290                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.forward_with_files), col_index++);
4291                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_myname_card), col_index++);
4292                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_signature), col_index++);
4293                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.signature), 0, col_index++);
4294                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.add_my_address_to_bcc), col_index++);
4295                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.notification_status), col_index++);
4296                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.vibrate_status), col_index++);
4297                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.display_content_status), col_index++);
4298                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.default_ringtone_status), col_index++);
4299                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.alert_ringtone_path), 0, col_index++);
4300                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_svc_id), col_index++);
4301                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->sync_disabled), col_index++);
4302                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->default_mail_slot_size), col_index++);
4303                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->roaming_option), col_index++);
4304                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->smime_type), col_index++);
4305                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->certificate_path), 0, col_index++);
4306                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->cipher_type), col_index++);
4307                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->digest_type), col_index++);
4308         }
4309
4310         ret = true;
4311
4312 FINISH_OFF:
4313         if (ret == true)
4314                 *account = p_data_tbl;
4315         else {
4316                 if (p_data_tbl)
4317                         emstorage_free_account((emstorage_account_tbl_t **)&p_data_tbl, 1, NULL);
4318         }
4319         if (hStmt != NULL) {
4320                 rc = sqlite3_finalize(hStmt);
4321                 if (rc != SQLITE_OK) {
4322                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
4323                         error = EMAIL_ERROR_DB_FAILURE;
4324                 }
4325         }
4326
4327         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4328
4329         if (err_code != NULL)
4330                 *err_code = error;
4331
4332         EM_DEBUG_FUNC_END("ret [%d]", ret);
4333         return ret;
4334 }
4335
4336 INTERNAL_FUNC int emstorage_get_password_length_of_account(char *multi_user_name, int account_id, int password_type, int *password_length, int *err_code)
4337 {
4338         EM_DEBUG_FUNC_BEGIN("account_id[%d], password_length[%p], err_code[%p]", account_id, password_length, err_code);
4339
4340         if (account_id <= 0 || password_length == NULL) {
4341                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4342                 if (err_code != NULL)
4343                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4344                 return false;
4345         }
4346
4347         int ret = false;
4348         int error = EMAIL_ERROR_NONE;
4349         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4350         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4351         char *temp_password = NULL;
4352
4353
4354         /*  get password file name */
4355         if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4356                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4357                 goto FINISH_OFF;
4358         }
4359
4360         /*  read password from secure storage */
4361         if (password_type == EMAIL_GET_INCOMING_PASSWORD_LENGTH) {
4362                 if ((error = _read_password_from_secure_storage(recv_password_file_name, &temp_password)) < 0 || !temp_password) {
4363                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed...");
4364                         goto FINISH_OFF;
4365                 }
4366         } else if (password_type == EMAIL_GET_OUTGOING_PASSWORD_LENGTH) {
4367                 if ((error = _read_password_from_secure_storage(send_password_file_name, &temp_password)) < 0 || !temp_password) {
4368                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed...");
4369                         goto FINISH_OFF;
4370                 }
4371         } else {
4372                 EM_DEBUG_LOG("Invalid password type");
4373                 if (err_code != NULL)
4374                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4375                 goto FINISH_OFF;
4376         }
4377
4378         *password_length = EM_SAFE_STRLEN(temp_password);
4379
4380         EM_DEBUG_LOG_SEC("recv_password_file_name[%s], *password_length[%d]", recv_password_file_name,  *password_length);
4381
4382         ret = true;
4383
4384 FINISH_OFF:
4385         EM_SAFE_FREE(temp_password);
4386
4387         if (err_code != NULL)
4388                 *err_code = error;
4389
4390         EM_DEBUG_FUNC_END("ret [%d]", ret);
4391         return ret;
4392 }
4393
4394 INTERNAL_FUNC int emstorage_update_account_password(char *multi_user_name, int input_account_id, char *input_incoming_server_password, char *input_outgoing_server_password)
4395 {
4396         EM_DEBUG_FUNC_BEGIN("input_account_id[%d], input_incoming_server_password[%p], input_outgoing_server_password[%p]", input_account_id, input_incoming_server_password, input_outgoing_server_password);
4397
4398         int err = EMAIL_ERROR_NONE;
4399         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4400         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4401
4402         if (input_incoming_server_password == NULL && input_outgoing_server_password == NULL) {
4403                 EM_DEBUG_EXCEPTION_SEC("Invalid param");
4404                 err = EMAIL_ERROR_INVALID_PARAM;
4405                 goto FINISH_OFF;
4406         }
4407
4408         /*  get password file name */
4409         if ((err = _get_password_file_name(multi_user_name,
4410                                                                                 input_account_id,
4411                                                                                 recv_password_file_name,
4412                                                                                 send_password_file_name)) != EMAIL_ERROR_NONE) {
4413                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4414                 goto FINISH_OFF;
4415         }
4416
4417         EM_DEBUG_LOG_SEC("recv_password_file_name [%s] input_incoming_server_password [%s]",
4418                                                 recv_password_file_name, input_incoming_server_password);
4419         EM_DEBUG_LOG_SEC("send_password_file_name [%s] input_outgoing_server_password [%s]",
4420                                                 send_password_file_name, input_outgoing_server_password);
4421
4422         if (input_incoming_server_password) {
4423                 err = emcore_remove_password_in_key_manager(recv_password_file_name);
4424                 if (err != EMAIL_ERROR_NONE) {
4425                         EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", recv_password_file_name);
4426                         goto FINISH_OFF;
4427                 }
4428
4429                 /*  save recv passwords to the secure storage */
4430                 err = emcore_add_password_in_key_manager(recv_password_file_name, input_incoming_server_password);
4431                 if (err != EMAIL_ERROR_NONE) {
4432                         EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4433                         goto FINISH_OFF;
4434                 }
4435         }
4436
4437         if (input_outgoing_server_password) {
4438                 err = emcore_remove_password_in_key_manager(send_password_file_name);
4439                 if (err != EMAIL_ERROR_NONE) {
4440                         EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", send_password_file_name);
4441                         goto FINISH_OFF;
4442                 }
4443
4444                 /*  save send passwords to the secure storage */
4445                 err = emcore_add_password_in_key_manager(send_password_file_name, input_outgoing_server_password);
4446                 if (err != EMAIL_ERROR_NONE) {
4447                         EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4448                         goto FINISH_OFF;
4449                 }
4450         }
4451 FINISH_OFF:
4452
4453         EM_DEBUG_FUNC_END("err [%d]", err);
4454         return err;
4455 }
4456
4457 INTERNAL_FUNC int emstorage_update_account(char *multi_user_name, int account_id, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
4458 {
4459         EM_DEBUG_FUNC_BEGIN("account_id[%d], account[%p], transaction[%d], err_code[%p]", account_id, account_tbl, transaction, err_code);
4460
4461         if (account_id < FIRST_ACCOUNT_ID || !account_tbl) {
4462                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4463                 if (err_code != NULL)
4464                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4465                 return false;
4466         }
4467
4468         int error = EMAIL_ERROR_NONE;
4469         int rc, ret = false;
4470
4471         DB_STMT hStmt = NULL;
4472         char sql_query_string[QUERY_SIZE] = {0, };
4473
4474         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4475
4476         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4477
4478         SNPRINTF(sql_query_string, sizeof(sql_query_string),
4479                 "UPDATE mail_account_tbl SET"
4480                 "  account_name = ?"
4481                 ", logo_icon_path = ?"
4482                 ", user_data = ?"
4483                 ", user_data_length = ?"
4484                 ", account_svc_id = ?"
4485                 ", sync_status = ?"
4486                 ", sync_disabled = ?"
4487                 ", default_mail_slot_size = ?"
4488                 ", roaming_option = ?"
4489                 ", color_label = ?"
4490                 ", user_display_name = ?"
4491                 ", user_email_address = ?"
4492                 ", reply_to_address = ?"
4493                 ", return_address = ?"
4494                 ", incoming_server_type = ?"
4495                 ", incoming_server_address = ?"
4496                 ", incoming_server_port_number = ?"
4497                 ", incoming_server_user_name = ?"
4498                 ", incoming_server_secure_connection = ?"
4499                 ", incoming_server_authentication_method = ?"
4500                 ", retrieval_mode = ?"
4501                 ", keep_mails_on_pop_server_after_download = ?"
4502                 ", check_interval = ?"
4503                 ", auto_download_size = ?"
4504                 ", peak_interval = ?"
4505                 ", peak_days = ?"
4506                 ", peak_start_time = ?"
4507                 ", peak_end_time = ?"
4508                 ", outgoing_server_type = ?"
4509                 ", outgoing_server_address = ?"
4510                 ", outgoing_server_port_number = ?"
4511                 ", outgoing_server_user_name = ?"
4512                 ", outgoing_server_secure_connection = ?"
4513                 ", outgoing_server_need_authentication = ?"
4514                 ", outgoing_server_use_same_authenticator = ?"
4515                 ", priority = ?"
4516                 ", keep_local_copy = ?"
4517                 ", req_delivery_receipt = ?"
4518                 ", req_read_receipt = ?"
4519                 ", download_limit = ?"
4520                 ", block_address = ?"
4521                 ", block_subject = ?"
4522                 ", display_name_from = ?"
4523                 ", reply_with_body = ?"
4524                 ", forward_with_files = ?"
4525                 ", add_myname_card = ?"
4526                 ", add_signature = ?"
4527                 ", signature = ?"
4528                 ", add_my_address_to_bcc = ?"
4529                 ", notification_status = ?"
4530                 ", vibrate_status = ?"
4531                 ", display_content_status = ?"
4532                 ", default_ringtone_status = ?"
4533                 ", alert_ringtone_path = ?"
4534                 ", auto_resend_times = ?"
4535                 ", outgoing_server_size_limit = ?"
4536                 ", wifi_auto_download = ?"
4537                 ", pop_before_smtp = ?"
4538                 ", incoming_server_requires_apop = ?"
4539                 ", smime_type = ?"
4540                 ", certificate_path = ?"
4541                 ", cipher_type = ?"
4542                 ", digest_type = ?"
4543                 ", user_name = ?"
4544                 " WHERE account_id = ?");
4545
4546         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4547         EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
4548         EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
4549         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4550                 ("sqlite3_prepare fail:(%d) %s", rc, sqlite3_errmsg(local_db_handle)));
4551
4552         int i = 0;
4553
4554         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4555         _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4556         _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4557         _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4558         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4559         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4560         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4561         _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4562         _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
4563         _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
4564         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4565         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4566         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4567         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4568         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4569         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4570         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
4571         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4572         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
4573         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
4574         _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
4575         _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
4576         _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
4577         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
4578         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
4579         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
4580         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
4581         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
4582         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
4583         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4584         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
4585         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
4586         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
4587         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
4588         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
4589         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
4590         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
4591         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
4592         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
4593         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
4594         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
4595         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
4596         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4597         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
4598         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
4599         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
4600         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
4601         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
4602         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
4603         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
4604         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
4605         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
4606         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
4607         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4608         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
4609         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
4610         _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
4611         _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
4612         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
4613         _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
4614         _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4615         _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
4616         _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
4617         _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4618         _bind_stmt_field_data_int(hStmt, i++, account_id);
4619
4620         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4621         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
4622                 ("sqlite3_step fail:%d", rc));
4623         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4624                 ("sqlite3_step fail:%d", rc));
4625
4626         /*  validate account existence */
4627         rc = sqlite3_changes(local_db_handle);
4628         if (rc == 0) {
4629                 EM_DEBUG_EXCEPTION("no matched account found...");
4630                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4631                 goto FINISH_OFF;
4632         }
4633
4634         if (account_tbl->incoming_server_password || account_tbl->outgoing_server_password) {
4635                 if ((error = emstorage_update_account_password(multi_user_name, account_id, account_tbl->incoming_server_password, account_tbl->outgoing_server_password)) != EMAIL_ERROR_NONE) {
4636                         EM_DEBUG_EXCEPTION("emstorage_update_account_password failed [%d]", error);
4637                         goto FINISH_OFF;
4638                 }
4639         }
4640
4641         ret = true;
4642
4643 FINISH_OFF:
4644         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4645
4646         if (hStmt != NULL) {
4647                 rc = sqlite3_finalize(hStmt);
4648                 if (rc != SQLITE_OK) {
4649                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
4650                         error = EMAIL_ERROR_DB_FAILURE;
4651                 }
4652         }
4653
4654         if (error == EMAIL_ERROR_NONE) {
4655                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_tbl->account_id, 0, NULL, 0))
4656                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_ACCOUNT_UPDATE] : Notification Failed >>> ");
4657         }
4658
4659         if (err_code != NULL)
4660                 *err_code = error;
4661
4662         EM_DEBUG_FUNC_END("ret [%d]", ret);
4663         return ret;
4664 }
4665
4666 INTERNAL_FUNC int emstorage_set_field_of_accounts_with_integer_value(char *multi_user_name, int account_id, char *field_name, int value, int transaction)
4667 {
4668         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], field_name[%s], value[%d], transaction[%d]", account_id, field_name, value, transaction);
4669         int error = EMAIL_ERROR_NONE;
4670         int result = 0;
4671         char sql_query_string[QUERY_SIZE] = {0, };
4672         sqlite3 *local_db_handle = NULL;
4673
4674         if (!account_id  || !field_name) {
4675                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4676                 return EMAIL_ERROR_INVALID_PARAM;
4677         }
4678
4679         local_db_handle = emstorage_get_db_connection(multi_user_name);
4680
4681         /* Write query string */
4682         SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_account_tbl SET %s = %d WHERE account_id = %d", field_name, value, account_id);
4683
4684         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4685
4686         /* Execute query */
4687         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4688
4689         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4690         if (error != EMAIL_ERROR_NONE) {
4691                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4692                 goto FINISH_OFF;
4693         }
4694
4695         if (sqlite3_changes(local_db_handle) == 0)
4696                 EM_DEBUG_LOG("no mail matched...");
4697
4698
4699 FINISH_OFF:
4700         result = (error == EMAIL_ERROR_NONE) ? true : false;
4701         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, error);
4702
4703         if (error == EMAIL_ERROR_NONE) {
4704                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_id, 0, field_name, value))
4705                         EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_ACCOUNT_UPDATE [%s,%d]", field_name, value);
4706         }
4707         EM_DEBUG_FUNC_END("error [%d]", error);
4708         return error;
4709 }
4710
4711 INTERNAL_FUNC int emstorage_get_sync_status_of_account(char *multi_user_name, int account_id, int *result_sync_status, int *err_code)
4712 {
4713         EM_DEBUG_FUNC_BEGIN("account_id[%d], result_sync_status [%p], err_code[%p]", account_id, result_sync_status, err_code);
4714
4715         if (!result_sync_status) {
4716                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4717                 if (err_code != NULL)
4718                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4719                 return false;
4720         }
4721
4722         int error = EMAIL_ERROR_NONE, rc, ret = false, sync_status, count, i, col_index;
4723         char sql_query_string[QUERY_SIZE] = {0, };
4724         char **result = NULL;
4725         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4726
4727         if (account_id)
4728                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl WHERE account_id = %d", account_id);
4729         else
4730                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl");
4731
4732         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
4733         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
4734                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4735
4736         if (!count) {
4737                 EM_DEBUG_EXCEPTION("no matched account found...");
4738                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4739                 goto FINISH_OFF;
4740         }
4741
4742         col_index = 1;
4743         *result_sync_status = 0;
4744
4745         for (i = 0; i < count; i++) {
4746                 _get_table_field_data_int(result, &sync_status, col_index++);
4747                 *result_sync_status |= sync_status;
4748         }
4749
4750         EM_DEBUG_LOG("sync_status [%d]", sync_status);
4751
4752         sqlite3_free_table(result);
4753
4754         ret = true;
4755
4756 FINISH_OFF:
4757
4758
4759         if (err_code != NULL)
4760                 *err_code = error;
4761
4762         EM_DEBUG_FUNC_END("ret [%d]", ret);
4763         return ret;
4764 }
4765
4766 INTERNAL_FUNC int emstorage_update_sync_status_of_account(char *multi_user_name, int account_id, email_set_type_t set_operator, int sync_status, int transaction, int *err_code)
4767 {
4768         EM_DEBUG_FUNC_BEGIN("account_id[%d], set_operator[%d], sync_status [%d], transaction[%d], err_code[%p]", account_id, set_operator, sync_status, transaction, err_code);
4769
4770         int error = EMAIL_ERROR_NONE, rc, ret = false, set_value = sync_status, result_sync_status;
4771         char sql_query_string[QUERY_SIZE] = {0, };
4772         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4773
4774         if (set_operator != SET_TYPE_SET && account_id) {
4775                 if (!emstorage_get_sync_status_of_account(multi_user_name, account_id, &result_sync_status, &error)) {
4776                         EM_DEBUG_EXCEPTION("emstorage_get_sync_status_of_account failed [%d]", error);
4777                         if (err_code != NULL)
4778                                 *err_code = error;
4779                         return false;
4780                 }
4781                 switch (set_operator) {
4782                         case SET_TYPE_UNION:
4783                                 set_value = result_sync_status | set_value;
4784                                 break;
4785                         case SET_TYPE_MINUS:
4786                                 set_value = result_sync_status & (~set_value);
4787                                 break;
4788                         default:
4789                                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_NOT_SUPPORTED [%d]", set_operator);
4790                                 error = EMAIL_ERROR_NOT_SUPPORTED;
4791                                 break;
4792                 }
4793                 EM_DEBUG_LOG("set_value [%d]", set_value);
4794         }
4795
4796         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4797
4798         if (account_id)
4799                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE account_id = %d", set_value, account_id);
4800         else
4801                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE incoming_server_type <> 5", set_value);
4802
4803         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4804
4805         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4806         if (error != EMAIL_ERROR_NONE) {
4807                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4808                 goto FINISH_OFF;
4809         }
4810
4811         rc = sqlite3_changes(local_db_handle);
4812
4813         if (rc == 0) {
4814                 EM_DEBUG_EXCEPTION("no matched account found...");
4815                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4816                 goto FINISH_OFF;
4817         }
4818
4819         ret = true;
4820
4821 FINISH_OFF:
4822         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4823
4824         if (error == EMAIL_ERROR_NONE) {
4825                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE_SYNC_STATUS, account_id, set_value, NULL, 0))
4826                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_UPDATE_SYNC_STATUS] : Notification failed");
4827         }
4828
4829         if (err_code != NULL)
4830                 *err_code = error;
4831
4832         EM_DEBUG_FUNC_END("ret [%d]", ret);
4833         return ret;
4834 }
4835
4836 INTERNAL_FUNC int emstorage_add_account(char *multi_user_name, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
4837 {
4838         EM_DEBUG_FUNC_BEGIN("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4839
4840         if (!account_tbl) {
4841                 EM_DEBUG_EXCEPTION("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
4842                 if (err_code != NULL)
4843                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4844                 return false;
4845         }
4846
4847         int row_count = 0;
4848         int i = 0;
4849         int rc = -1, ret = false;
4850         int error = EMAIL_ERROR_NONE;
4851         int error_from_ssm = 0;
4852         DB_STMT hStmt = NULL;
4853         char sql_query_string[QUERY_SIZE] = {0, };
4854         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4855         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4856
4857         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4858
4859         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4860
4861         char *sql = "SELECT rowid FROM mail_account_tbl;";
4862         char **result = NULL;
4863
4864         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, &row_count, NULL, NULL), rc);
4865         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
4866                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
4867
4868         if (NULL == result[1]) rc = 1;
4869         else {
4870                 for (i = 1; i <= row_count; i++) {
4871                         if (i != atoi(result[i])) {
4872                                 break;
4873                         }
4874                 }
4875
4876                 rc = i;
4877         }
4878         sqlite3_free_table(result);
4879         result = NULL;
4880
4881         if (rc < 0 && rc > EMAIL_ACCOUNT_MAX) {
4882                 EM_DEBUG_EXCEPTION("OVERFLOWED THE MAX ACCOUNT");
4883                 error = EMAIL_ERROR_ACCOUNT_MAX_COUNT;
4884                 goto FINISH_OFF;
4885         }
4886
4887         account_tbl->account_id = rc;
4888
4889         if ((error = _get_password_file_name(multi_user_name, account_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4890                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4891                 goto FINISH_OFF;
4892         }
4893
4894         EM_DEBUG_LOG(" >>>> ACCOUNT_ID [ %d ] ", account_tbl->account_id);
4895         SNPRINTF(sql_query_string, sizeof(sql_query_string),
4896                 "INSERT INTO mail_account_tbl VALUES "
4897                 "(        "
4898                 "    ? "  /*   account_id */
4899                 "  , ? "  /*   account_name */
4900                 "  , ? "  /*   logo_icon_path */
4901                 "  , ? "  /*   user_data */
4902                 "  , ? "  /*   user_data_length */
4903                 "  , ? "  /*   account_svc_id */
4904                 "  , ? "  /*   sync_status */
4905                 "  , ? "  /*   sync_disabled */
4906                 "  , ? "  /*   default_mail_slot_size */
4907                 "  , ? "  /*   roaming_option */
4908                 "  , ? "  /*   color_label */
4909                 "  , ? "  /*   user_display_name */
4910                 "  , ? "  /*   user_email_address */
4911                 "  , ? "  /*   reply_to_address */
4912                 "  , ? "  /*   return_address */
4913                 "  , ? "  /*   incoming_server_type */
4914                 "  , ? "  /*   incoming_server_address */
4915                 "  , ? "  /*   incoming_server_port_number */
4916                 "  , ? "  /*   incoming_server_user_name */
4917                 "  , ? "  /*   incoming_server_password */
4918                 "  , ? "  /*   incoming_server_secure_connection */
4919                 "  , ? "  /*   incoming_server_authentication_method */
4920                 "  , ? "  /*   retrieval_mode */
4921                 "  , ? "  /*   keep_mails_on_pop_server_after_download */
4922                 "  , ? "  /*   check_interval */
4923                 "  , ? "  /*   auto_download_size */
4924                 "  , ? "  /*   peak_interval */
4925                 "  , ? "  /*   peak_days */
4926                 "  , ? "  /*   peak_start_time */
4927                 "  , ? "  /*   peak_end_time */
4928                 "  , ? "  /*   outgoing_server_type */
4929                 "  , ? "  /*   outgoing_server_address */
4930                 "  , ? "  /*   outgoing_server_port_number */
4931                 "  , ? "  /*   outgoing_server_user_name */
4932                 "  , ? "  /*   outgoing_server_password */
4933                 "  , ? "  /*   outgoing_server_secure_connection */
4934                 "  , ? "  /*   outgoing_server_need_authentication */
4935                 "  , ? "  /*   outgoing_server_use_same_authenticator */
4936                 "  , ? "  /*   priority */
4937                 "  , ? "  /*   keep_local_copy */
4938                 "  , ? "  /*   req_delivery_receipt */
4939                 "  , ? "  /*   req_read_receipt */
4940                 "  , ? "  /*   download_limit */
4941                 "  , ? "  /*   block_address */
4942                 "  , ? "  /*   block_subject */
4943                 "  , ? "  /*   display_name_from */
4944                 "  , ? "  /*   reply_with_body */
4945                 "  , ? "  /*   forward_with_files */
4946                 "  , ? "  /*   add_myname_card */
4947                 "  , ? "  /*   add_signature */
4948                 "  , ? "  /*   signature */
4949                 "  , ? "  /*   add_my_address_to_bcc */
4950                 "  , ? "  /*   auto_resend_times */
4951                 "  , ? "  /*   outgoing_server_size_limit */
4952                 "  , ? "  /*   wifi_auto_download */
4953                 "  , ? "  /*   pop_before_smtp */
4954                 "  , ? "  /*   incoming_server_requires_apop */
4955                 "  , ? "  /*   smime_type */
4956                 "  , ? "  /*   certificate_path */
4957                 "  , ? "  /*   cipher_type */
4958                 "  , ? "  /*   digest_type */
4959                 "  , ? "  /*   notification_status */
4960                 "  , ? "  /*   vibrate_status */
4961                 "  , ? "  /*   display_content_status */
4962                 "  , ? "  /*   default_ringtone_status */
4963                 "  , ? "  /*   alert_ringtone_path */
4964                 "  , ? "  /*   user_name */
4965                 ") ");
4966
4967         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4968         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4969                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4970
4971         i = 0;
4972
4973         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_id);
4974         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4975         _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4976         _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4977         _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4978         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4979         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4980         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4981         _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4982         _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
4983         _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
4984         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4985         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4986         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4987         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4988         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4989         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4990         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
4991         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4992         _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
4993         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
4994         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
4995         _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
4996         _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
4997         _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
4998         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
4999         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
5000         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
5001         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
5002         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
5003         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
5004         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5005         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
5006         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
5007         _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
5008         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
5009         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
5010         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
5011         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
5012         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
5013         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
5014         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
5015         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
5016         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
5017         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
5018         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
5019         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
5020         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
5021         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
5022         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
5023         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
5024         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
5025         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
5026         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
5027         _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
5028         _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
5029         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
5030         _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
5031         _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
5032         _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
5033         _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
5034         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
5035         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
5036         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
5037         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
5038         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5039         _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5040
5041         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5042
5043         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
5044                 ("sqlite3_step fail:%d", rc));
5045         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5046                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
5047
5048
5049         /*  save passwords to the secure storage */
5050         EM_DEBUG_LOG_SEC("save to the secure storage : recv_file[%s], send_file[%s]", recv_password_file_name, send_password_file_name);
5051         error = emcore_add_password_in_key_manager(recv_password_file_name, account_tbl->incoming_server_password);
5052         if (error != EMAIL_ERROR_NONE) {
5053                 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
5054                 goto FINISH_OFF;
5055         }
5056
5057         error = emcore_add_password_in_key_manager(send_password_file_name, account_tbl->outgoing_server_password);
5058         if (error != EMAIL_ERROR_NONE) {
5059                 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
5060                 goto FINISH_OFF;
5061         }
5062
5063         ret = true;
5064
5065 FINISH_OFF:
5066         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5067
5068         if (hStmt != NULL) {
5069                 rc = sqlite3_finalize(hStmt);
5070                 if (rc != SQLITE_OK) {
5071                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5072                         error = EMAIL_ERROR_DB_FAILURE;
5073                 }
5074         }
5075
5076         if (error == EMAIL_ERROR_NONE) {
5077                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_ADD, account_tbl->account_id, 0, NULL, 0))
5078                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_ADD] : Notification failed");
5079         }
5080
5081         if (err_code != NULL)
5082                 *err_code = error;
5083
5084         EM_DEBUG_FUNC_END("ret [%d]", ret);
5085         return ret;
5086 }
5087
5088 INTERNAL_FUNC int emstorage_delete_account(char *multi_user_name, int account_id, int transaction, int *err_code)
5089 {
5090         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
5091
5092         if (account_id < FIRST_ACCOUNT_ID) {
5093                 EM_DEBUG_EXCEPTION(" account_id[%d]", account_id);
5094
5095                 if (err_code != NULL)
5096                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5097                 return false;
5098         }
5099
5100         int rc = -1, ret = false;
5101         int error = EMAIL_ERROR_NONE;
5102
5103         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5104
5105         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5106
5107         /*  TODO : delete password files - file names can be obtained from db or a rule that makes a name */
5108         char sql_query_string[QUERY_SIZE] = {0, };
5109         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5110         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5111
5112         /*  get password file name */
5113         if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
5114                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
5115                 goto FINISH_OFF;
5116         }
5117
5118         /*  delete from db */
5119         memset(sql_query_string, 0x00, sizeof(sql_query_string));
5120         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_account_tbl WHERE account_id = %d", account_id);
5121         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5122         if (error != EMAIL_ERROR_NONE) {
5123                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5124                         goto FINISH_OFF;
5125         }
5126
5127         /*  validate account existence */
5128         rc = sqlite3_changes(local_db_handle);
5129         if (rc == 0) {
5130                 EM_DEBUG_EXCEPTION(" no matched account found...");
5131                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
5132                 goto FINISH_OFF;
5133         }
5134
5135                 /*  delete from secure storage */
5136         error = emcore_remove_password_in_key_manager(recv_password_file_name);
5137         if (error != EMAIL_ERROR_NONE) {
5138                 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5139                 goto FINISH_OFF;
5140         }
5141
5142         error = emcore_remove_password_in_key_manager(send_password_file_name);
5143         if (error != EMAIL_ERROR_NONE) {
5144                 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5145                 goto FINISH_OFF;
5146         }
5147
5148         ret = true;
5149
5150 FINISH_OFF:
5151         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5152
5153         if (err_code != NULL)
5154                 *err_code = error;
5155
5156         EM_DEBUG_FUNC_END("ret [%d]", ret);
5157         return ret;
5158 }
5159
5160
5161 INTERNAL_FUNC int emstorage_free_account(emstorage_account_tbl_t** account_list, int count, int *err_code)
5162 {
5163         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%d], err_code[%p]", account_list, count, err_code);
5164
5165         int ret = false;
5166         int error = EMAIL_ERROR_NONE;
5167
5168         if (count > 0) {
5169                 if (!account_list || !*account_list) {
5170                         EM_DEBUG_EXCEPTION("account_list[%p], count[%d]", account_list, count);
5171                         error = EMAIL_ERROR_INVALID_PARAM;
5172                         goto FINISH_OFF;
5173                 }
5174
5175                 emstorage_account_tbl_t* p = *account_list;
5176                 int i = 0;
5177
5178                 for (; i < count; i++) {
5179                         EM_SAFE_FREE(p[i].account_name);
5180                         EM_SAFE_FREE(p[i].incoming_server_address);
5181                         EM_SAFE_FREE(p[i].user_email_address);
5182                         EM_SAFE_FREE(p[i].user_data);
5183                         EM_SAFE_FREE(p[i].incoming_server_user_name);
5184                         EM_SAFE_FREE(p[i].incoming_server_password);
5185                         EM_SAFE_FREE(p[i].outgoing_server_address);
5186                         EM_SAFE_FREE(p[i].outgoing_server_user_name);
5187                         EM_SAFE_FREE(p[i].outgoing_server_password);
5188                         EM_SAFE_FREE(p[i].user_display_name);
5189                         EM_SAFE_FREE(p[i].reply_to_address);
5190                         EM_SAFE_FREE(p[i].return_address);
5191                         EM_SAFE_FREE(p[i].logo_icon_path);
5192                         EM_SAFE_FREE(p[i].options.display_name_from);
5193                         EM_SAFE_FREE(p[i].options.signature);
5194                         EM_SAFE_FREE(p[i].options.alert_ringtone_path);
5195                         EM_SAFE_FREE(p[i].certificate_path);
5196                         EM_SAFE_FREE(p[i].user_name);
5197                 }
5198
5199                 EM_SAFE_FREE(p);
5200                 *account_list = NULL;
5201         }
5202
5203         ret = true;
5204
5205 FINISH_OFF:
5206         if (err_code != NULL)
5207                 *err_code = error;
5208
5209         EM_DEBUG_FUNC_END("ret [%d]", ret);
5210         return ret;
5211 }
5212
5213 INTERNAL_FUNC int emstorage_get_mailbox_count(char *multi_user_name, int account_id, int local_yn, int *count, int transaction, int *err_code)
5214 {
5215         EM_DEBUG_FUNC_BEGIN("account_id[%d], local_yn[%d], count[%p], transaction[%d], err_code[%p]", account_id, local_yn, count, transaction, err_code);
5216
5217         if ((account_id < FIRST_ACCOUNT_ID) || (count == NULL)) {
5218                 EM_DEBUG_EXCEPTION(" account_list[%d], local_yn[%d], count[%p]", account_id, local_yn, count);
5219
5220                 if (err_code != NULL)
5221                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5222                 return false;
5223         }
5224
5225         int rc = -1, ret = false;
5226         int error = EMAIL_ERROR_NONE;
5227         char sql_query_string[QUERY_SIZE] = {0, };
5228
5229         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5230         EMSTORAGE_START_READ_TRANSACTION(transaction);
5231
5232         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_box_tbl WHERE account_id = %d AND local_yn = %d", account_id, local_yn);
5233
5234         char **result;
5235
5236         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5237         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
5238                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5239
5240         *count = atoi(result[1]);
5241         sqlite3_free_table(result);
5242
5243
5244         ret = true;
5245
5246 FINISH_OFF:
5247
5248         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5249
5250         if (err_code != NULL)
5251                 *err_code = error;
5252
5253         EM_DEBUG_FUNC_END("ret [%d]", ret);
5254         return ret;
5255 }
5256
5257 INTERNAL_FUNC int emstorage_get_mailbox_list(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)
5258 {
5259         EM_DEBUG_FUNC_BEGIN("account_id[%d], local_yn[%d], select_num[%p], mailbox_list[%p], transaction[%d], err_code[%p]", account_id, local_yn, select_num, mailbox_list, transaction, err_code);
5260
5261         if (!select_num || !mailbox_list) {
5262                 EM_DEBUG_EXCEPTION("Invalid parameters");
5263
5264                 if (err_code != NULL)
5265                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5266
5267                 return false;
5268         }
5269
5270         int ret = false;
5271         int error = EMAIL_ERROR_NONE;
5272         char conditional_clause_string[QUERY_SIZE] = {0, };
5273         char ordering_clause_string[QUERY_SIZE] = {0, };
5274
5275         if (account_id == ALL_ACCOUNT) {
5276                 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5277                         SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " WHERE local_yn = %d ", local_yn);
5278         } else {
5279                 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d  ", account_id);
5280                 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5281                         SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " AND local_yn = %d ", local_yn);
5282         }
5283
5284         EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5285
5286         switch (sort_type) {
5287                 case EMAIL_MAILBOX_SORT_BY_NAME_ASC:
5288                         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name ASC");
5289                         break;
5290
5291                 case EMAIL_MAILBOX_SORT_BY_NAME_DSC:
5292                         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name DESC");
5293                         break;
5294
5295                 case EMAIL_MAILBOX_SORT_BY_TYPE_ASC:
5296                         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type ASC");
5297                         break;
5298
5299                 case EMAIL_MAILBOX_SORT_BY_TYPE_DSC:
5300                         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type DEC");
5301                         break;
5302         }
5303
5304         EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5305
5306         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5307                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5308                 goto FINISH_OFF;
5309         }
5310
5311         ret = true;
5312
5313 FINISH_OFF:
5314
5315         if (err_code != NULL)
5316                 *err_code = error;
5317
5318         EM_DEBUG_FUNC_END("ret [%d]", ret);
5319         return ret;
5320 }
5321
5322 INTERNAL_FUNC int emstorage_get_mailbox_list_ex(char *multi_user_name, int account_id, int local_yn, int with_count, int *select_num, emstorage_mailbox_tbl_t **mailbox_list, int transaction, int *err_code)
5323 {
5324         EM_DEBUG_FUNC_BEGIN("account_id[%d], local_yn[%d], select_num[%p], mailbox_list[%p], transaction[%d], err_code[%p]", account_id, local_yn, select_num, mailbox_list, transaction, err_code);
5325
5326         int ret = false;
5327         int error = EMAIL_ERROR_NONE;
5328         int where_clause_count = 0;
5329         char conditional_clause_string[QUERY_SIZE] = {0, };
5330         char ordering_clause_string[QUERY_SIZE] = {0, };
5331
5332         if (!select_num || !mailbox_list) {
5333                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5334                 error = EMAIL_ERROR_INVALID_PARAM;
5335                 goto FINISH_OFF;
5336         }
5337
5338         if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL) {
5339                 SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), "WHERE local_yn = %d ",  local_yn);
5340                 where_clause_count++;
5341         }
5342
5343         if (account_id > 0) {
5344                 if (where_clause_count == 0) {
5345                         SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " WHERE ");
5346                         SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " account_id = %d ", account_id);
5347                 } else
5348                         SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " AND account_id = %d ", account_id);
5349         }
5350
5351         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY CASE WHEN MBT.mailbox_name"
5352                                                                                                  " GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\'"
5353                                                                                                  " THEN 2 ELSE 1 END ASC,"
5354                                                                                                  " MBT.mailbox_name COLLATE CONVERTUTF8 ASC ");
5355         EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5356         EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5357
5358         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 1, 1, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5359                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5360                 goto FINISH_OFF;
5361         }
5362
5363         ret = true;
5364
5365 FINISH_OFF:
5366
5367         if (err_code != NULL)
5368                 *err_code = error;
5369
5370         EM_DEBUG_FUNC_END("ret [%d]", ret);
5371         return ret;
5372 }
5373
5374 INTERNAL_FUNC int emstorage_get_child_mailbox_list(char *multi_user_name, int account_id, char *parent_mailbox_name, int *select_num, emstorage_mailbox_tbl_t **mailbox_list, int transaction, int *err_code)
5375 {
5376         EM_DEBUG_FUNC_BEGIN("account_id[%d], parent_mailbox_name[%p], select_num[%p], mailbox_list[%p], transaction[%d], err_code[%p]", account_id, parent_mailbox_name, select_num, mailbox_list, transaction, err_code);
5377
5378         int ret = false;
5379         int error = EMAIL_ERROR_NONE;
5380         char conditional_clause_string[QUERY_SIZE] = {0, };
5381
5382         if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list || !parent_mailbox_name) {
5383                 EM_DEBUG_EXCEPTION("account_id[%d], parent_mailbox_name[%p], select_num[%p], mailbox_list[%p]", account_id, parent_mailbox_name, select_num, mailbox_list);
5384                 if (err_code != NULL)
5385                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5386                 return false;
5387         }
5388
5389         sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string, "WHERE account_id = %d  AND UPPER(mailbox_name) LIKE UPPER('%q%%')", account_id, parent_mailbox_name);
5390         EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5391
5392         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, " ORDER BY mailbox_name DESC ", 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5393                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5394                 goto FINISH_OFF;
5395         }
5396
5397         ret = true;
5398
5399 FINISH_OFF:
5400
5401         if (err_code != NULL)
5402                 *err_code = error;
5403
5404         EM_DEBUG_FUNC_END("ret [%d]", ret);
5405         return ret;
5406 }
5407
5408 INTERNAL_FUNC int emstorage_get_mailbox_by_modifiable_yn(char *multi_user_name, int account_id, int local_yn, int *select_num, emstorage_mailbox_tbl_t** mailbox_list, int transaction, int *err_code)
5409 {
5410         EM_DEBUG_FUNC_BEGIN("account_id[%d], local_yn[%d], select_num[%p], mailbox_list[%p], transaction[%d], err_code[%p]", account_id, local_yn, select_num, mailbox_list, transaction, err_code);
5411         if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list) {
5412                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5413
5414                 if (err_code != NULL)
5415                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5416
5417                 return false;
5418         }
5419
5420         int ret = false;
5421         int error = EMAIL_ERROR_NONE;
5422         char conditional_clause_string[QUERY_SIZE] = {0, };
5423
5424         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND modifiable_yn = 0", account_id);
5425         EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
5426
5427         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, " ORDER BY mailbox_name", 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5428                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5429                 goto FINISH_OFF;
5430         }
5431
5432         ret = true;
5433
5434 FINISH_OFF:
5435
5436         if (err_code != NULL)
5437                 *err_code = error;
5438
5439         EM_DEBUG_FUNC_END("ret [%d]", ret);
5440         return ret;
5441 }
5442
5443 INTERNAL_FUNC int emstorage_stamp_last_sync_time_of_mailbox(char *multi_user_name, int input_mailbox_id, int input_transaction)
5444 {
5445         EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_transaction [%d]", input_mailbox_id, input_transaction);
5446
5447         int      result_code = false;
5448         int      error = EMAIL_ERROR_NONE;
5449         time_t   current_time = 0;
5450         char     sql_query_string[QUERY_SIZE] = {0, };
5451         sqlite3 *local_db_handle = NULL;
5452
5453         if (!input_mailbox_id) {
5454                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5455                 return EMAIL_ERROR_INVALID_PARAM;
5456         }
5457
5458         time(&current_time);
5459
5460         local_db_handle = emstorage_get_db_connection(multi_user_name);
5461     EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
5462
5463     SNPRINTF(sql_query_string, sizeof(sql_query_string),
5464         "UPDATE mail_box_tbl SET"
5465                 " last_sync_time = %d"
5466                 " WHERE mailbox_id = %d"
5467                 , (int)current_time
5468                 , input_mailbox_id);
5469
5470         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
5471         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5472         if (error != EMAIL_ERROR_NONE) {
5473                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5474                         goto FINISH_OFF;
5475         }
5476
5477 FINISH_OFF:
5478
5479         if (error == EMAIL_ERROR_NONE)
5480                 result_code = true;
5481
5482         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, result_code, error);
5483
5484         EM_DEBUG_FUNC_END("error [%d]", error);
5485         return error;
5486 }
5487
5488 INTERNAL_FUNC int emstorage_get_mailbox_by_name(char *multi_user_name, int account_id, int local_yn, char *mailbox_name, emstorage_mailbox_tbl_t **result_mailbox, int transaction, int *err_code)
5489 {
5490         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], local_yn[%d], mailbox_name[%s], result_mailbox[%p], transaction[%d], err_code[%p]", account_id, local_yn, mailbox_name, result_mailbox, transaction, err_code);
5491         EM_PROFILE_BEGIN(profile_emstorage_get_mailbox_by_name);
5492
5493         if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !result_mailbox) {
5494                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], mailbox_name[%s], result_mailbox[%p]", account_id, local_yn, mailbox_name, result_mailbox);
5495
5496                 if (err_code != NULL)
5497                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5498                 return false;
5499         }
5500
5501         int ret = false;
5502         int error = EMAIL_ERROR_NONE;
5503         int result_count = 0;
5504         char conditional_clause_string[QUERY_SIZE] = {0, };
5505
5506         if (strcmp(mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
5507                 if (!(*result_mailbox = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t)))) {
5508                         EM_DEBUG_EXCEPTION("malloc failed...");
5509                         error = EMAIL_ERROR_OUT_OF_MEMORY;
5510                         goto FINISH_OFF;
5511                 }
5512
5513                 (*result_mailbox)->mailbox_id                 = 0;
5514                 (*result_mailbox)->account_id                 = account_id;
5515                 (*result_mailbox)->local_yn                   = 1;
5516                 (*result_mailbox)->mailbox_name               = EM_SAFE_STRDUP(mailbox_name);
5517                 (*result_mailbox)->mailbox_type               = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;
5518                 (*result_mailbox)->alias                      = EM_SAFE_STRDUP(mailbox_name);
5519                 (*result_mailbox)->deleted_flag               = 0;
5520                 (*result_mailbox)->modifiable_yn              = 1;
5521                 (*result_mailbox)->total_mail_count_on_server = 1;
5522                 (*result_mailbox)->has_archived_mails         = 0;
5523                 (*result_mailbox)->mail_slot_size             = 0x0FFFFFFF;
5524                 (*result_mailbox)->no_select                  = 0;
5525         } else {
5526                 if (local_yn == -1)
5527                         sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string, "WHERE account_id = %d AND mailbox_name = '%q'", account_id, mailbox_name);
5528                 else
5529                         sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string, "WHERE account_id = %d AND local_yn = %d AND mailbox_name = '%q'", account_id, local_yn, mailbox_name);
5530
5531                 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5532
5533                 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5534                         EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5535                         goto FINISH_OFF;
5536                 }
5537         }
5538
5539         ret = true;
5540
5541 FINISH_OFF:
5542
5543         if (err_code != NULL)
5544                 *err_code = error;
5545
5546         EM_PROFILE_END(profile_emstorage_get_mailbox_by_name);
5547         EM_DEBUG_FUNC_END("ret [%d]", ret);
5548         return ret;
5549 }
5550
5551 INTERNAL_FUNC int emstorage_get_mailbox_by_mailbox_type(char *multi_user_name, int account_id, email_mailbox_type_e mailbox_type, emstorage_mailbox_tbl_t **output_mailbox, int transaction, int *err_code)
5552 {
5553         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_type[%d], output_mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox_type, output_mailbox, transaction, err_code);
5554
5555         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_USER_DEFINED) || !output_mailbox) {
5556                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_type[%d], output_mailbox[%p]", account_id, mailbox_type, output_mailbox);
5557                 if (err_code != NULL)
5558                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5559                 return false;
5560         }
5561
5562         int ret = false;
5563         int error = EMAIL_ERROR_NONE;
5564         int result_count = 0;
5565         emstorage_mailbox_tbl_t *result_mailbox = NULL;
5566         char conditional_clause_string[QUERY_SIZE] = {0,};
5567
5568
5569         SNPRINTF(conditional_clause_string, QUERY_SIZE, "WHERE account_id = %d AND mailbox_type = %d ", account_id, mailbox_type);
5570
5571         EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5572
5573         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", true, false, &result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5574                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl error [%d]", error);
5575                 goto FINISH_OFF;
5576         }
5577
5578         ret = true;
5579
5580 FINISH_OFF:
5581         if (ret == true)
5582                 *output_mailbox = result_mailbox;
5583
5584         if (err_code != NULL)
5585                 *err_code = error;
5586
5587         EM_DEBUG_FUNC_END("ret [%d]", ret);
5588         return ret;
5589 }
5590
5591 INTERNAL_FUNC int emstorage_get_mailbox_by_id(char *multi_user_name, int input_mailbox_id, emstorage_mailbox_tbl_t** output_mailbox)
5592 {
5593         EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], output_mailbox[%p]", input_mailbox_id, output_mailbox);
5594
5595         if (input_mailbox_id <= 0 || !output_mailbox) {
5596                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: input_mailbox_id[%d] output_mailbox[%p]", input_mailbox_id, output_mailbox);
5597                 return  EMAIL_ERROR_INVALID_PARAM;
5598         }
5599
5600         int  ret = EMAIL_ERROR_NONE;
5601         int  result_count = 0;
5602         char conditional_clause_string[QUERY_SIZE] = {0, };
5603
5604         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE MBT.mailbox_id = %d", input_mailbox_id);
5605
5606         EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5607
5608         if ((ret = emstorage_query_mailbox_tbl(multi_user_name,
5609                                                                                         conditional_clause_string,
5610                                                                                         "", true, false, output_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5611                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", ret);
5612                 goto FINISH_OFF;
5613         }
5614
5615 FINISH_OFF:
5616
5617         EM_DEBUG_FUNC_END("ret [%d]", ret);
5618         return ret;
5619 }
5620
5621 INTERNAL_FUNC int emstorage_get_mailbox_by_keyword(char *multi_user_name, int account_id, char *keyword, emstorage_mailbox_tbl_t** result_mailbox, int * result_count, int transaction, int *err_code)
5622 {
5623         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], keyword[%s], result_mailbox[%p], transaction[%d], err_code[%p]", account_id, keyword, result_mailbox, transaction, err_code);
5624
5625         if (account_id < 0 || !keyword || !result_mailbox) {
5626                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], keyword[%s], result_mailbox[%p]", account_id, keyword, result_mailbox);
5627
5628                 if (err_code != NULL)
5629                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5630                 return false;
5631         }
5632
5633         int ret = false;
5634         int error = EMAIL_ERROR_NONE;
5635         char conditional_clause_string[QUERY_SIZE] = {0, };
5636
5637         if (account_id == 0)
5638                 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5639                                 "WHERE alias LIKE \'%%%q%%\'", keyword);
5640         else if (account_id > 0)
5641                 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5642                                 "WHERE account_id = %d AND alias LIKE \'%%%q%%\'", account_id, keyword);
5643
5644         EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5645
5646         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, result_count)) != EMAIL_ERROR_NONE) {
5647                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5648                 goto FINISH_OFF;
5649         }
5650
5651         ret = true;
5652
5653 FINISH_OFF:
5654
5655         if (err_code != NULL)
5656                 *err_code = error;
5657
5658         EM_DEBUG_FUNC_END("ret [%d]", ret);
5659         return ret;
5660 }
5661
5662 INTERNAL_FUNC int emstorage_get_mailbox_id_by_mailbox_type(char *multi_user_name, int account_id, email_mailbox_type_e mailbox_type, int *mailbox_id, int transaction, int *err_code)
5663 {
5664         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_type[%d], mailbox_id[%p], transaction[%d], err_code[%p]", account_id, mailbox_type, mailbox_id, transaction, err_code);
5665         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_id) {
5666                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_id[%p]", account_id, mailbox_type, mailbox_id);
5667                 if (err_code != NULL)
5668                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5669                 return false;
5670         }
5671
5672         int rc, ret = false;
5673         int error = EMAIL_ERROR_NONE;
5674         emstorage_account_tbl_t* account = NULL;
5675         DB_STMT hStmt = NULL;
5676         char sql_query_string[QUERY_SIZE] = {0, };
5677
5678         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5679         EMSTORAGE_START_READ_TRANSACTION(transaction);
5680
5681         /*  Check whether the account exists. */
5682         if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
5683                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5684                 goto FINISH_OFF;
5685         }
5686
5687         if (account)
5688                 emstorage_free_account(&account, 1, NULL);
5689
5690         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mailbox_id  FROM mail_box_tbl WHERE account_id = %d AND mailbox_type = %d ", account_id, mailbox_type);
5691
5692         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5693
5694
5695         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5696
5697         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5698                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5699
5700
5701         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5702         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5703                 ("sqlite3_step fail:%d", rc));
5704
5705         if (rc == SQLITE_DONE) {
5706                 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5707                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5708                 goto FINISH_OFF;
5709         }
5710
5711         _get_stmt_field_data_int(hStmt, mailbox_id, 0);
5712
5713         ret = true;
5714
5715 FINISH_OFF:
5716         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5717         if (hStmt != NULL) {
5718                 rc = sqlite3_finalize(hStmt);
5719                 if (rc != SQLITE_OK) {
5720                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5721                         error = EMAIL_ERROR_DB_FAILURE;
5722                 }
5723         }
5724
5725         if (err_code != NULL)
5726                 *err_code = error;
5727
5728         EM_DEBUG_FUNC_END("ret [%d]", ret);
5729         return ret;
5730 }
5731
5732 INTERNAL_FUNC int emstorage_get_mailbox_name_by_mailbox_type(char *multi_user_name, int account_id, email_mailbox_type_e mailbox_type, char **mailbox_name, int transaction, int *err_code)
5733 {
5734         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_type[%d], mailbox_name[%p], transaction[%d], err_code[%p]", account_id, mailbox_type, mailbox_name, transaction, err_code);
5735         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_name) {
5736                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
5737                 if (err_code != NULL)
5738                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5739                 return false;
5740         }
5741
5742         int rc, ret = false;
5743         int error = EMAIL_ERROR_NONE;
5744         emstorage_account_tbl_t* account = NULL;
5745         DB_STMT hStmt = NULL;
5746         char sql_query_string[QUERY_SIZE] = {0, };
5747
5748         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5749         EMSTORAGE_START_READ_TRANSACTION(transaction);
5750
5751         /*  Check whether the account exists. */
5752         if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
5753                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5754                 goto FINISH_OFF;
5755         }
5756
5757         if (account)
5758                 emstorage_free_account(&account, 1, NULL);
5759
5760         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mailbox_name  FROM mail_box_tbl WHERE account_id = %d AND mailbox_type = %d ", account_id, mailbox_type);
5761
5762         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5763
5764
5765         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5766
5767         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5768                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5769
5770
5771         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5772         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5773                 ("sqlite3_step fail:%d", rc));
5774
5775         if (rc == SQLITE_DONE) {
5776                 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5777                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5778                 goto FINISH_OFF;
5779         }
5780
5781         _get_stmt_field_data_string(hStmt, mailbox_name, 0, 0);
5782
5783         ret = true;
5784
5785 FINISH_OFF:
5786         if (hStmt != NULL) {
5787                 rc = sqlite3_finalize(hStmt);
5788                 if (rc != SQLITE_OK) {
5789                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5790                         error = EMAIL_ERROR_DB_FAILURE;
5791                 }
5792         }
5793
5794         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5795
5796         if (err_code != NULL)
5797                 *err_code = error;
5798
5799         EM_DEBUG_FUNC_END("ret [%d]", ret);
5800         return ret;
5801 }
5802
5803 INTERNAL_FUNC int emstorage_update_mailbox_modifiable_yn(char *multi_user_name, int account_id, int local_yn, char *mailbox_name, int modifiable_yn, int transaction, int *err_code)
5804 {
5805         EM_DEBUG_FUNC_BEGIN("account_id [%d], local_yn [%d], mailbox_name [%p], modifiable_yn [%d], transaction [%d], err_code [%p]", account_id, local_yn, mailbox_name, modifiable_yn, transaction, err_code);
5806         int ret = false;
5807         int error = EMAIL_ERROR_NONE;
5808         char sql_query_string[QUERY_SIZE] = {0, };
5809         char *replaced_mailbox_name = NULL;
5810
5811         if (mailbox_name) {
5812                 if (strstr(mailbox_name, "'")) {
5813                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5814                 } else {
5815                         replaced_mailbox_name = strdup(mailbox_name);
5816                 }
5817         }
5818
5819         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5820
5821         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5822
5823         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5824                 "UPDATE mail_box_tbl SET"
5825                 " modifiable_yn = %d"
5826                 " WHERE account_id = %d"
5827                 " AND local_yn = %d"
5828                 " AND mailbox_name = '%s'"
5829                 , modifiable_yn
5830                 , account_id
5831                 , local_yn
5832                 , replaced_mailbox_name);
5833
5834         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5835         if (error != EMAIL_ERROR_NONE) {
5836                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5837                         goto FINISH_OFF;
5838         }
5839
5840         ret = true;
5841
5842 FINISH_OFF:
5843         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5844
5845         EM_SAFE_FREE(replaced_mailbox_name);
5846
5847         if (err_code != NULL)
5848                 *err_code = error;
5849
5850         EM_DEBUG_FUNC_END("ret [%d]", ret);
5851         return ret;
5852
5853 }
5854
5855 INTERNAL_FUNC int emstorage_update_mailbox_total_count(char *multi_user_name,
5856                                                                                                                 int account_id,
5857                                                                                                                 int input_mailbox_id,
5858                                                                                                                 int total_count_on_server,
5859                                                                                                                 int transaction,
5860                                                                                                                 int *err_code)
5861 {
5862         EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], total_count_on_server[%d], "
5863                                                 "transaction[%d], err_code[%p]",
5864                                                 account_id, input_mailbox_id, total_count_on_server,  transaction, err_code);
5865
5866         int ret = false;
5867         int error = EMAIL_ERROR_NONE;
5868         char sql_query_string[QUERY_SIZE] = {0, };
5869
5870         if (account_id <= 0 || input_mailbox_id <= 0) {
5871                 EM_DEBUG_EXCEPTION("account_id[%d], input_mailbox_id[%d]", account_id, input_mailbox_id);
5872                 if (err_code != NULL)
5873                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5874                 EM_DEBUG_FUNC_END("ret [%d]", ret);
5875                 return false;
5876         }
5877
5878         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5879
5880         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5881
5882         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5883                 "UPDATE mail_box_tbl SET"
5884                 " total_mail_count_on_server = %d"
5885                 " WHERE account_id = %d"
5886                 " AND mailbox_id = %d"
5887                 , total_count_on_server
5888                 , account_id
5889                 , input_mailbox_id);
5890
5891         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
5892         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5893         if (error != EMAIL_ERROR_NONE) {
5894                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5895                         goto FINISH_OFF;
5896         }
5897
5898         ret = true;
5899
5900 FINISH_OFF:
5901
5902         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5903
5904         if (err_code != NULL)
5905                 *err_code = error;
5906
5907         EM_DEBUG_FUNC_END("ret [%d]", ret);
5908         return ret;
5909
5910 }
5911
5912 INTERNAL_FUNC int emstorage_update_mailbox(char *multi_user_name, int account_id, int local_yn, int input_mailbox_id, emstorage_mailbox_tbl_t *result_mailbox, int transaction, int *err_code)
5913 {
5914         EM_DEBUG_FUNC_BEGIN("account_id[%d], local_yn[%d], input_mailbox_id[%d], result_mailbox[%p], transaction[%d], err_code[%p]", account_id, local_yn, input_mailbox_id, result_mailbox, transaction, err_code);
5915
5916         if (account_id < FIRST_ACCOUNT_ID || input_mailbox_id <= 0 || !result_mailbox) {
5917                 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], input_mailbox_id[%d], result_mailbox[%p]", account_id, local_yn, input_mailbox_id, result_mailbox);
5918
5919                 if (err_code != NULL)
5920                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5921                 EM_DEBUG_FUNC_END("ret [%d]", EMAIL_ERROR_INVALID_PARAM);
5922                 return false;
5923         }
5924
5925         int rc, ret = false;
5926         int error = EMAIL_ERROR_NONE;
5927         char sql_query_string[QUERY_SIZE] = {0, };
5928         DB_STMT hStmt = NULL;
5929         int i = 0;
5930
5931         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5932         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5933
5934         if (local_yn != -1) {
5935                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5936                         "UPDATE mail_box_tbl SET"
5937                         "  mailbox_id = ?"
5938                         ", mailbox_name = ?"
5939                         ", mailbox_type = ?"
5940                         ", alias = ?"
5941                         ", deleted_flag = ?"
5942                         ", modifiable_yn= ?"
5943                         ", mail_slot_size= ?"
5944                         ", total_mail_count_on_server = ?"
5945                         " WHERE account_id = %d"
5946                         " AND local_yn = %d"
5947                         " AND mailbox_id = '%d'"
5948                         , account_id
5949                         , local_yn
5950                         , input_mailbox_id);
5951         } else {
5952                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
5953                         "UPDATE mail_box_tbl SET"
5954                         "  mailbox_id = ?"
5955                         ", mailbox_name = ?"
5956                         ", mailbox_type = ?"
5957                         ", alias = ?"
5958                         ", deleted_flag = ?"
5959                         ", modifiable_yn= ?"
5960                         ", mail_slot_size= ?"
5961                         ", total_mail_count_on_server = ?"
5962                         " WHERE account_id = %d"
5963                         " AND mailbox_id = '%d'"
5964                         , account_id
5965                         , input_mailbox_id);
5966         }
5967
5968
5969
5970         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5971         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5972                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5973
5974         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_id);
5975         _bind_stmt_field_data_string(hStmt, i++, (char *)result_mailbox->mailbox_name ? result_mailbox->mailbox_name : "", 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
5976         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_type);
5977         _bind_stmt_field_data_string(hStmt, i++, (char *)result_mailbox->alias ? result_mailbox->alias : "", 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
5978         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->deleted_flag);
5979         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->modifiable_yn);
5980         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mail_slot_size);
5981         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->total_mail_count_on_server);
5982
5983
5984         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5985         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
5986                 ("sqlite3_step fail:%d", rc));
5987         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5988                 ("sqlite3_step fail:%d", rc));
5989
5990         ret = true;
5991
5992 FINISH_OFF:
5993         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5994
5995         if (hStmt != NULL) {
5996                 rc = sqlite3_finalize(hStmt);
5997                 if (rc != SQLITE_OK) {
5998                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5999                         error = EMAIL_ERROR_DB_FAILURE;
6000                 }
6001         }
6002
6003         if (err_code != NULL)
6004                 *err_code = error;
6005
6006         EM_DEBUG_FUNC_END("ret [%d]", ret);
6007         return ret;
6008 }
6009
6010 INTERNAL_FUNC int emstorage_update_mailbox_type(char *multi_user_name, int account_id, int local_yn, int input_mailbox_id, email_mailbox_type_e new_mailbox_type, int transaction, int *err_code)
6011 {
6012         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], local_yn[%d], input_mailbox_id[%d], new_mailbox_type[%d], transaction[%d], err_code[%p]", account_id, local_yn, input_mailbox_id, new_mailbox_type, transaction, err_code);
6013
6014         if (account_id < FIRST_ACCOUNT_ID) {
6015                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
6016
6017                 if (err_code != NULL)
6018                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6019                 return false;
6020         }
6021
6022         int rc, ret = false;
6023         int error = EMAIL_ERROR_NONE;
6024         char sql_query_string[QUERY_SIZE] = {0, };
6025
6026         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6027         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6028
6029         EM_DEBUG_LOG("emstorage_update_mailbox_type");
6030
6031         DB_STMT hStmt_box_tbl = NULL;
6032         DB_STMT hStmt_mail_tbl = NULL;
6033         int i = 0;
6034
6035         /*  Update mail_box_tbl */
6036         if (local_yn != -1) {
6037                 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
6038                         "UPDATE mail_box_tbl SET"
6039                         " mailbox_type = ?"
6040                         " WHERE account_id = %d"
6041                         " AND local_yn = %d"
6042                         " AND mailbox_id = '%d'"
6043                         , account_id
6044                         , local_yn
6045                         , input_mailbox_id);
6046         } else {
6047                 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
6048                         "UPDATE mail_box_tbl SET"
6049                         " mailbox_type = ?"
6050                         " WHERE account_id = %d"
6051                         " AND mailbox_id = '%d'"
6052                         , account_id
6053                         , input_mailbox_id);
6054         }
6055
6056         EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
6057
6058         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_box_tbl, NULL), rc);
6059
6060         if (SQLITE_OK != rc) {
6061                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6062                 error = EMAIL_ERROR_DB_FAILURE;
6063                 goto FINISH_OFF;
6064         }
6065
6066         _bind_stmt_field_data_int(hStmt_box_tbl, i++, new_mailbox_type);
6067
6068
6069         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_box_tbl), rc);
6070
6071         if (rc == SQLITE_FULL) {
6072                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6073                 error   = EMAIL_ERROR_MAIL_MEMORY_FULL;
6074                 goto FINISH_OFF;
6075         }
6076
6077         if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
6078                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6079                 error = EMAIL_ERROR_DB_FAILURE;
6080                 goto FINISH_OFF;
6081         }
6082
6083
6084         /*  Update mail_tbl */
6085         i = 0;
6086         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6087                         "UPDATE mail_tbl SET"
6088                         " mailbox_type = ?"
6089                         " WHERE account_id = %d"
6090                         " AND mailbox_id = '%d'"
6091                         , account_id
6092                         , input_mailbox_id);
6093
6094         EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
6095
6096         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_mail_tbl, NULL), rc);
6097         if (SQLITE_OK != rc) {
6098                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6099                 error = EMAIL_ERROR_DB_FAILURE;
6100                 goto FINISH_OFF;
6101         }
6102
6103         _bind_stmt_field_data_int(hStmt_mail_tbl, i++, new_mailbox_type);
6104
6105         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_mail_tbl), rc);
6106         if (rc == SQLITE_FULL) {
6107                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6108                 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
6109                 goto FINISH_OFF;
6110         }
6111
6112         if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
6113                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6114                 error = EMAIL_ERROR_DB_FAILURE;
6115                 goto FINISH_OFF;
6116         }
6117
6118         ret = true;
6119
6120 FINISH_OFF:
6121         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6122
6123         if (hStmt_box_tbl != NULL) {
6124                 rc = sqlite3_finalize(hStmt_box_tbl);
6125                 if (rc != SQLITE_OK) {
6126                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6127                         error = EMAIL_ERROR_DB_FAILURE;
6128                 }
6129         }
6130
6131         if (hStmt_mail_tbl != NULL) {
6132                 rc = sqlite3_finalize(hStmt_mail_tbl);
6133                 if (rc != SQLITE_OK) {
6134                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6135                         error = EMAIL_ERROR_DB_FAILURE;
6136                 }
6137         }
6138
6139         if (err_code != NULL)
6140                 *err_code = error;
6141
6142         EM_DEBUG_FUNC_END("ret [%d]", ret);
6143         return ret;
6144 }
6145
6146 INTERNAL_FUNC int emstorage_set_local_mailbox(char *multi_user_name, int input_mailbox_id, int input_is_local_mailbox, int transaction)
6147 {
6148         EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], new_mailbox_type[%d], transaction[%d], err_code[%p]", input_mailbox_id, input_is_local_mailbox, transaction);
6149
6150         int rc, ret = false;
6151         int error = EMAIL_ERROR_NONE;
6152         char sql_query_string[QUERY_SIZE] = {0, };
6153
6154         if (input_mailbox_id < 0) {
6155                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6156                 return EMAIL_ERROR_INVALID_PARAM;
6157         }
6158
6159         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6160
6161         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6162
6163         EM_DEBUG_LOG("emstorage_update_mailbox_type");
6164
6165         DB_STMT hStmt = NULL;
6166         int i = 0;
6167
6168         /*  Update mail_box_tbl */
6169         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6170                 "UPDATE mail_box_tbl SET"
6171                 " local_yn = ?"
6172                 " WHERE mailbox_id = %d"
6173                 , input_mailbox_id);
6174
6175         EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
6176
6177         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6178         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6179                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6180
6181         _bind_stmt_field_data_int(hStmt, i++, input_is_local_mailbox);
6182
6183         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6184         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
6185                 ("sqlite3_step fail:%d", rc));
6186         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6187                 ("sqlite3_step fail:%d", rc));
6188 /*
6189         if (hStmt != NULL) {
6190                 rc = sqlite3_finalize(hStmt);
6191                 if (rc != SQLITE_OK) {
6192                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6193                         error = EMAIL_ERROR_DB_FAILURE;
6194                 }
6195                 hStmt = NULL;
6196         }
6197 */
6198         ret = true;
6199
6200 FINISH_OFF:
6201         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6202
6203         if (hStmt != NULL) {
6204                 rc = sqlite3_finalize(hStmt);
6205                 if (rc != SQLITE_OK) {
6206                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6207                         error = EMAIL_ERROR_DB_FAILURE;
6208                 }
6209         }
6210
6211         EM_DEBUG_FUNC_END("error [%d]", error);
6212         return error;
6213 }
6214
6215 INTERNAL_FUNC int emstorage_set_field_of_mailbox_with_integer_value(char *multi_user_name, int input_account_id, int *input_mailbox_id_array, int input_mailbox_id_count, char *input_field_name, int input_value, int transaction)
6216 {
6217         EM_DEBUG_FUNC_BEGIN("input_account_id [%d] input_mailbox_id_array[%p] input_mailbox_id_count[%d] input_field_name[%p] input_value[%d] err_code[%p]", input_account_id, input_mailbox_id_array, input_mailbox_id_count, input_field_name, input_value, transaction);
6218         int i = 0;
6219         int err = EMAIL_ERROR_NONE;
6220         int result = false;
6221         int cur_mailbox_id_string = 0;
6222         int mailbox_id_string_buffer_length = 0;
6223         char  sql_query_string[QUERY_SIZE] = {0, };
6224         char *mailbox_id_string_buffer = NULL;
6225         char *parameter_string = NULL;
6226         sqlite3 *local_db_handle = NULL;
6227
6228         if (input_mailbox_id_array == NULL || input_mailbox_id_count == 0 || input_field_name == NULL) {
6229                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6230                 return EMAIL_ERROR_INVALID_PARAM;
6231         }
6232
6233         local_db_handle = emstorage_get_db_connection(multi_user_name);
6234
6235         /* Generating mail id list string */
6236         mailbox_id_string_buffer_length = MAILBOX_ID_STRING_LENGTH * input_mailbox_id_count;
6237
6238         mailbox_id_string_buffer = em_malloc(mailbox_id_string_buffer_length);
6239
6240         if (!mailbox_id_string_buffer) {
6241                 EM_DEBUG_EXCEPTION("em_mallocfailed");
6242                 err = EMAIL_ERROR_OUT_OF_MEMORY;
6243                 goto FINISH_OFF;
6244         }
6245
6246         for (i = 0; i < input_mailbox_id_count; i++)
6247                 cur_mailbox_id_string += SNPRINTF_OFFSET(mailbox_id_string_buffer, cur_mailbox_id_string, mailbox_id_string_buffer_length, "%d,", input_mailbox_id_array[i]);
6248
6249         if (EM_SAFE_STRLEN(mailbox_id_string_buffer) > 1)
6250                 mailbox_id_string_buffer[EM_SAFE_STRLEN(mailbox_id_string_buffer) - 1] = NULL_CHAR;
6251
6252         /* Generating notification parameter string */
6253         parameter_string = em_malloc(mailbox_id_string_buffer_length + EM_SAFE_STRLEN(input_field_name) + 2);
6254
6255         if (!parameter_string) {
6256                 EM_DEBUG_EXCEPTION("em_mallocfailed");
6257                 err = EMAIL_ERROR_OUT_OF_MEMORY;
6258                 goto FINISH_OFF;
6259         }
6260
6261         SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", input_field_name, 0x01, mailbox_id_string_buffer);
6262
6263         /* Write query string */
6264         SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_box_tbl SET %s = %d WHERE mailbox_id in (%s) ", input_field_name, input_value, mailbox_id_string_buffer);
6265
6266         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
6267
6268         /* Execute query */
6269         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
6270         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6271         if (err != EMAIL_ERROR_NONE) {
6272                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
6273                         goto FINISH_OFF;
6274         }
6275
6276         if (sqlite3_changes(local_db_handle) == 0)
6277                 EM_DEBUG_LOG("no mail matched...");
6278
6279         result = true;
6280
6281 FINISH_OFF:
6282         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, err);
6283
6284         if (err == EMAIL_ERROR_NONE && parameter_string) {
6285                 if (!emcore_notify_storage_event(NOTI_MAILBOX_FIELD_UPDATE, input_account_id, 0, parameter_string, input_value))
6286                         EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_MAILBOX_FIELD_UPDATE [%s,%d]",
6287                                                                                          input_field_name, input_value);
6288         }
6289
6290         EM_SAFE_FREE(mailbox_id_string_buffer);
6291         EM_SAFE_FREE(parameter_string);
6292
6293         EM_DEBUG_FUNC_END("err [%d]", err);
6294         return err;
6295 }
6296
6297 INTERNAL_FUNC int emstorage_add_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox_tbl, int transaction, int *err_code)
6298 {
6299         EM_DEBUG_FUNC_BEGIN("mailbox_tbl[%p], transaction[%d], err_code[%p]", mailbox_tbl, transaction, err_code);
6300
6301         if (!mailbox_tbl) {
6302                 if (err_code != NULL)
6303                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6304                 return false;
6305         }
6306
6307         int rc, ret = false;
6308         int error = EMAIL_ERROR_NONE;
6309         DB_STMT hStmt = NULL;
6310         char sql_query_string[QUERY_SIZE] = {0,};
6311         char **result = NULL;
6312         time_t current_time;
6313         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6314         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6315
6316         EM_SAFE_STRCPY(sql_query_string, "SELECT max(rowid) FROM mail_box_tbl;");
6317
6318         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6319         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6320                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6321
6322         time(&current_time);
6323
6324         if (NULL == result[1])
6325                 rc = 1;
6326         else
6327                 rc = atoi(result[1]) + 1;
6328         sqlite3_free_table(result);
6329
6330         memset(sql_query_string, 0, sizeof(char) * QUERY_SIZE);
6331
6332         mailbox_tbl->mailbox_id = rc;
6333
6334         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6335                 "INSERT INTO mail_box_tbl VALUES "
6336                 "(?"    /* mailbox_id */
6337                 ", ?"    /* account_id */
6338                 ", ?"    /* local_yn */
6339                 ", ?"    /* mailbox_name */
6340                 ", ?"    /* mailbox_type */
6341                 ", ?"    /* alias */
6342                 ", ?"    /* deleted_flag */
6343                 ", ?"    /* modifiable_yn */
6344                 ", ?"    /* total_mail_count_on_server */
6345                 ", ?"    /* has_archived_mails */
6346                 ", ?"    /* mail_slot_size */
6347                 ", ?"    /* no_select */
6348                 ", ?"    /* last_sync_time */
6349                 ", ?"    /* eas_data_length */
6350                 ", ?"    /* eas_data */
6351                 ")");
6352
6353
6354         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6355         EM_DEBUG_LOG_DEV("After sqlite3_prepare hStmt = %p", hStmt);
6356         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6357                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6358
6359         int col_index = 0;
6360
6361         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_id);
6362         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->account_id);
6363         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->local_yn);
6364         _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
6365         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_type);
6366         _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->alias, 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
6367         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->deleted_flag);
6368         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->modifiable_yn);
6369         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->total_mail_count_on_server);
6370         _bind_stmt_field_data_int(hStmt, col_index++, 0);
6371         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mail_slot_size);
6372         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->no_select);
6373         _bind_stmt_field_data_int(hStmt, col_index++, current_time);
6374         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->eas_data_length);
6375         _bind_stmt_field_data_blob(hStmt, col_index++, (void*)mailbox_tbl->eas_data, mailbox_tbl->eas_data_length);
6376
6377
6378         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6379         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6380                 ("sqlite3_step fail:%dn", rc));
6381
6382         ret = true;
6383
6384 FINISH_OFF:
6385         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6386         if (hStmt != NULL) {
6387                 rc = sqlite3_finalize(hStmt);
6388                 if (rc != SQLITE_OK) {
6389                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6390                         error = EMAIL_ERROR_DB_FAILURE;
6391                 }
6392         }
6393
6394         if (error == EMAIL_ERROR_NONE) {
6395                 if (!emcore_notify_storage_event(NOTI_MAILBOX_ADD, mailbox_tbl->account_id, mailbox_tbl->mailbox_id,
6396                                                                   mailbox_tbl->mailbox_name, mailbox_tbl->mailbox_type))
6397                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6398         }
6399
6400         if (err_code != NULL)
6401                 *err_code = error;
6402
6403         EM_DEBUG_FUNC_END("ret [%d]", ret);
6404         return ret;
6405 }
6406
6407 INTERNAL_FUNC int emstorage_set_all_mailbox_modifiable_yn(char *multi_user_name, int account_id, int modifiable_yn, int transaction, int *err_code)
6408 {
6409         EM_DEBUG_FUNC_BEGIN("account_id[%d], modifiable_yn[%d], err_code[%p]", account_id, modifiable_yn, err_code);
6410
6411         if (account_id < FIRST_ACCOUNT_ID) {
6412
6413                 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
6414
6415                 if (err_code != NULL)
6416                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6417                 return false;
6418         }
6419         int rc, ret = false;
6420         int error = EMAIL_ERROR_NONE;
6421         char sql_query_string[QUERY_SIZE] = {0,};
6422         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6423         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6424
6425
6426         SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET modifiable_yn = %d WHERE account_id = %d", modifiable_yn, account_id);
6427
6428         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6429         if (error != EMAIL_ERROR_NONE) {
6430                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6431                         goto FINISH_OFF;
6432         }
6433
6434         rc = sqlite3_changes(local_db_handle);
6435         if (rc == 0)
6436                 EM_DEBUG_EXCEPTION("All mailbox_name modifiable_yn set to 0 already");
6437
6438
6439         ret = true;
6440
6441 FINISH_OFF:
6442         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6443
6444         if (err_code != NULL)
6445                 *err_code = error;
6446         EM_DEBUG_FUNC_END("ret [%d]", ret);
6447         return ret;
6448
6449
6450 }
6451
6452 INTERNAL_FUNC int emstorage_delete_mailbox(char *multi_user_name, int account_id, int local_yn, int input_mailbox_id, int transaction, int *err_code)
6453 {
6454         EM_DEBUG_FUNC_BEGIN("account_id[%d], local_yn[%d], input_mailbox_id[%d], transaction[%d], err_code[%p]", account_id, local_yn, input_mailbox_id, transaction, err_code);
6455
6456         if (account_id < FIRST_ACCOUNT_ID) {
6457                 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
6458
6459                 if (err_code != NULL)
6460                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6461                 return false;
6462         }
6463
6464         int rc, ret = false;
6465         int error = EMAIL_ERROR_NONE;
6466         char sql_query_string[QUERY_SIZE] = {0, };
6467         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6468         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6469
6470         if (local_yn == -1)
6471                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_box_tbl WHERE account_id = %d ", account_id);
6472         else
6473                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_box_tbl WHERE account_id = %d AND local_yn = %d ", account_id, local_yn);
6474
6475         if (input_mailbox_id > 0) {             /* 0 means all mailbox */
6476                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(1 + EM_SAFE_STRLEN(sql_query_string)), "AND mailbox_id = %d", input_mailbox_id);
6477         }
6478
6479         EM_DEBUG_LOG_SEC("mailbox sql_query_string [%s]", sql_query_string);
6480         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6481         if (error != EMAIL_ERROR_NONE) {
6482                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6483                         goto FINISH_OFF;
6484         }
6485
6486         rc = sqlite3_changes(local_db_handle);
6487         if (rc == 0) {
6488                 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
6489                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
6490                 ret = true;
6491         }
6492         ret = true;
6493
6494 FINISH_OFF:
6495
6496         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6497
6498         if (error == EMAIL_ERROR_NONE) {
6499                 if (!emcore_notify_storage_event(NOTI_MAILBOX_DELETE, account_id, input_mailbox_id, NULL, 0))
6500                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6501         }
6502
6503         if (err_code != NULL)
6504                 *err_code = error;
6505
6506         EM_DEBUG_FUNC_END("ret [%d]", ret);
6507         return ret;
6508 }
6509
6510 INTERNAL_FUNC int emstorage_free_mailbox(emstorage_mailbox_tbl_t** mailbox_list, int count, int *err_code)
6511 {
6512         EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d], err_code[%p]", mailbox_list, count, err_code);
6513
6514         int ret = false;
6515         int error = EMAIL_ERROR_NONE;
6516
6517         if (count > 0) {
6518                 if (!mailbox_list || !*mailbox_list) {
6519                         EM_DEBUG_EXCEPTION(" mailbox_list[%p], count[%d]", mailbox_list, count);
6520
6521                         error = EMAIL_ERROR_INVALID_PARAM;
6522                         goto FINISH_OFF;
6523                 }
6524
6525                 emstorage_mailbox_tbl_t* p = *mailbox_list;
6526                 int i = 0;
6527
6528                 for (; i < count; i++) {
6529                         EM_SAFE_FREE(p[i].mailbox_name);
6530                         EM_SAFE_FREE(p[i].alias);
6531                         EM_SAFE_FREE(p[i].eas_data); /*valgrind*/
6532                 }
6533
6534                 EM_SAFE_FREE(p); *mailbox_list = NULL;
6535         }
6536
6537         ret = true;
6538
6539 FINISH_OFF:
6540         if (err_code != NULL)
6541                 *err_code = error;
6542
6543         EM_DEBUG_FUNC_END("ret [%d]", ret);
6544         return ret;
6545 }
6546
6547 INTERNAL_FUNC int emstorage_get_count_read_mail_uid(char *multi_user_name, int account_id, char *mailbox_name, int *count, int transaction, int *err_code)
6548 {
6549         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_name[%p], count[%p], transaction[%d], err_code[%p]", account_id, mailbox_name , count,  transaction, err_code);
6550
6551         if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !count) {
6552                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], count[%p], exist[%p]", account_id, mailbox_name, count);
6553
6554                 if (err_code != NULL)
6555                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6556                 return false;
6557         }
6558
6559         int rc = -1, ret = false;
6560         int error = EMAIL_ERROR_NONE;
6561         char sql_query_string[QUERY_SIZE] = {0, };
6562         char *replaced_mailbox_name = NULL;
6563
6564         if (strstr(mailbox_name, "'")) {
6565                 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6566         } else {
6567                 replaced_mailbox_name = EM_SAFE_STRDUP(mailbox_name);
6568         }
6569
6570         EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6571
6572
6573         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6574         EMSTORAGE_START_READ_TRANSACTION(transaction);
6575         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_read_mail_uid_tbl WHERE account_id = %d AND mailbox_name = '%s'  ", account_id, replaced_mailbox_name);
6576         EM_DEBUG_LOG_SEC(">>> SQL [ %s ] ", sql_query_string);
6577
6578         char **result;
6579
6580         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6581         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6582                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6583
6584         *count = atoi(result[1]);
6585         sqlite3_free_table(result);
6586
6587         ret = true;
6588
6589 FINISH_OFF:
6590         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6591
6592         EM_SAFE_FREE(replaced_mailbox_name);
6593
6594         if (err_code != NULL)
6595                 *err_code = error;
6596
6597         EM_DEBUG_FUNC_END("ret [%d]", ret);
6598         return ret;
6599 }
6600
6601 INTERNAL_FUNC int emstorage_check_read_mail_uid(char *multi_user_name, int account_id, char *mailbox_name, char *uid, int *exist, int transaction, int *err_code)
6602 {
6603         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_name[%p], uid[%p], exist[%p], transaction[%d], err_code[%p]", account_id, mailbox_name , uid, exist, transaction, err_code);
6604
6605         if (account_id < FIRST_ACCOUNT_ID || !uid || !exist) {
6606                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], uid[%p], exist[%p]", account_id, mailbox_name , uid, exist);
6607
6608                 if (err_code != NULL)
6609                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6610                 return false;
6611         }
6612
6613         int rc = -1, ret = false;
6614         int error = EMAIL_ERROR_NONE;
6615         char sql_query_string[QUERY_SIZE] = {0, };
6616         char *replaced_mailbox_name = NULL;
6617
6618         EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6619
6620         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6621         EMSTORAGE_START_READ_TRANSACTION(transaction);
6622
6623         if (mailbox_name) {
6624                 if (strstr(mailbox_name, "'")) {
6625                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6626                 } else {
6627                         replaced_mailbox_name = strdup(mailbox_name);
6628                 }
6629
6630                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_read_mail_uid_tbl WHERE account_id = %d AND mailbox_name = '%s' AND server_uid = '%s' ", account_id, replaced_mailbox_name, uid);
6631         } else {
6632                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_read_mail_uid_tbl WHERE account_id = %d AND server_uid = '%s' ", account_id, uid);
6633         }
6634
6635         char **result = NULL;
6636
6637         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6638         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6639                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6640
6641         *exist = atoi(result[1]);
6642         sqlite3_free_table(result);
6643
6644         if (*exist > 0)
6645                 *exist = 1;
6646         else
6647                 *exist = 0;
6648
6649         ret = true;
6650
6651 FINISH_OFF:
6652         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6653
6654         EM_SAFE_FREE(replaced_mailbox_name);
6655
6656         if (err_code != NULL)
6657                 *err_code = error;
6658
6659         EM_DEBUG_FUNC_END("ret [%d]", ret);
6660         return ret;
6661 }
6662
6663 INTERNAL_FUNC int emstorage_get_downloaded_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code)
6664 {
6665         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], err_code[%p]", mail_id, mail, err_code);
6666
6667         if (!mail || mail_id <= 0) {
6668                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
6669                 if (err_code != NULL)
6670                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6671                 return false;
6672         }
6673
6674         int rc, ret = false;
6675         int error = EMAIL_ERROR_NONE;
6676         DB_STMT hStmt = NULL;
6677         char sql_query_string[QUERY_SIZE] = {0, };
6678
6679         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6680         EMSTORAGE_START_READ_TRANSACTION(transaction);
6681
6682         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE local_uid = %d", mail_id);
6683
6684         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6685         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6686
6687         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6688                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6689
6690
6691         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6692         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6693                 ("sqlite3_step fail:%d", rc));
6694
6695         *mail = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
6696         if (*mail == NULL) {
6697                 error = EMAIL_ERROR_OUT_OF_MEMORY;
6698                 EM_DEBUG_EXCEPTION("Memory allocation for mail failed.");
6699                 goto FINISH_OFF;
6700
6701         }
6702         memset(*mail, 0x00, sizeof(emstorage_mail_tbl_t));
6703
6704         _get_stmt_field_data_int(hStmt, &((*mail)->account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6705         _get_stmt_field_data_int(hStmt, &((*mail)->mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6706         _get_stmt_field_data_string(hStmt, &((*mail)->server_mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6707         _get_stmt_field_data_int(hStmt, &((*mail)->mail_id), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6708         _get_stmt_field_data_string(hStmt, &((*mail)->server_mail_id), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6709         _get_stmt_field_data_int(hStmt, &((*mail)->mail_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6710         _get_stmt_field_data_char(hStmt, &((*mail)->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6711
6712         (*mail)->server_mail_status = 1;
6713
6714         ret = true;
6715
6716 FINISH_OFF:
6717
6718         if (hStmt != NULL) {
6719                 rc = sqlite3_finalize(hStmt);
6720                 if (rc != SQLITE_OK) {
6721                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6722                         error = EMAIL_ERROR_DB_FAILURE;
6723                 }
6724         }
6725
6726         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6727
6728         if (err_code != NULL)
6729                 *err_code = error;
6730
6731         EM_DEBUG_FUNC_END("ret [%d]", ret);
6732         return ret;
6733 }
6734
6735 INTERNAL_FUNC int emstorage_get_downloaded_list(char *multi_user_name, int account_id, int mailbox_id, emstorage_read_mail_uid_tbl_t **read_mail_uid, int *count, int transaction, int *err_code)
6736 {
6737         EM_PROFILE_BEGIN(emStorageGetDownloadList);
6738         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], read_mail_uid[%p], count[%p], transaction[%d], err_code[%p]", account_id, mailbox_id, read_mail_uid, count, transaction, err_code);
6739         if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid || !count) {
6740                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_id[%s], read_mail_uid[%p], count[%p]", account_id, mailbox_id, read_mail_uid, count);
6741
6742                 if (err_code != NULL)
6743                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6744                 return false;
6745         }
6746
6747         int rc, ret = false;
6748         int error = EMAIL_ERROR_NONE;
6749
6750         DB_STMT hStmt = NULL;
6751         char sql_query_string[QUERY_SIZE] = {0, };
6752
6753         emstorage_read_mail_uid_tbl_t* p_data_tbl = NULL;
6754         int i = 0;
6755
6756         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6757         EMSTORAGE_START_READ_TRANSACTION(transaction);
6758
6759         if (mailbox_id)
6760                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE account_id = %d AND mailbox_id = %d", account_id, mailbox_id);
6761         else
6762                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
6763
6764         EM_DEBUG_LOG_SEC(" sql_query_string : %s", sql_query_string);
6765
6766
6767
6768         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6769         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6770         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6771                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6772
6773
6774         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6775         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6776                 ("sqlite3_step fail:%d", rc));
6777
6778         char **result;
6779         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL); */
6780         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL), rc);
6781         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6782                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6783
6784         sqlite3_free_table(result);
6785         if (*count == 0) {
6786                 EM_DEBUG_LOG("No mail found in mail_read_mail_uid_tbl");
6787                 ret = true;
6788                 goto FINISH_OFF;
6789         }
6790
6791
6792         if (!(p_data_tbl = (emstorage_read_mail_uid_tbl_t*)malloc(sizeof(emstorage_read_mail_uid_tbl_t) * *count))) {
6793                 EM_DEBUG_EXCEPTION(" malloc failed...");
6794                 error = EMAIL_ERROR_OUT_OF_MEMORY;
6795                 goto FINISH_OFF;
6796         }
6797
6798         memset(p_data_tbl, 0x00, sizeof(emstorage_read_mail_uid_tbl_t)*(*count));
6799
6800         for (i = 0; i < *count; ++i) {
6801                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6802                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6803                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6804                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].local_uid), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6805                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].server_uid), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6806                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rfc822_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6807                 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6808                 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_flagged_field), FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6809
6810                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6811                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6812                         ("sqlite3_step fail:%d", rc));
6813         }
6814
6815         ret = true;
6816
6817 FINISH_OFF:
6818         if (ret == true)
6819                 *read_mail_uid = p_data_tbl;
6820         else if (p_data_tbl)
6821                 emstorage_free_read_mail_uid(&p_data_tbl, *count, NULL);
6822
6823         if (hStmt != NULL) {
6824                 rc = sqlite3_finalize(hStmt);
6825                 if (rc != SQLITE_OK) {
6826                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6827                         error = EMAIL_ERROR_DB_FAILURE;
6828                 }
6829         }
6830
6831         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6832
6833         if (err_code != NULL)
6834                 *err_code = error;
6835
6836         EM_PROFILE_END(emStorageGetDownloadList);
6837         EM_DEBUG_FUNC_END("ret [%d]", ret);
6838         return ret;
6839 }
6840
6841 INTERNAL_FUNC int emstorage_get_downloaded_mail_size(char *multi_user_name, int account_id, char *mailbox_id, int local_uid, char *mailbox_name, char *uid, int *mail_size, int transaction, int *err_code)
6842 {
6843         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%p], locacal_uid[%d], mailbox_name[%p], uid[%p], mail_size[%p], transaction[%d], err_code[%p]", account_id, mailbox_id, local_uid, mailbox_name, uid, mail_size, transaction, err_code);
6844
6845         if (account_id < FIRST_ACCOUNT_ID || !mail_size) {
6846                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_id[%p], locacal_uid[%d], mailbox_name[%p], uid[%p], mail_size[%p]", account_id, mailbox_id, local_uid, mailbox_name, uid, mail_size);
6847
6848                 if (err_code != NULL)
6849                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6850                 return false;
6851         }
6852
6853         int rc, ret = false;
6854         int error = EMAIL_ERROR_NONE;
6855         DB_STMT hStmt = NULL;
6856         char sql_query_string[QUERY_SIZE] = {0, };
6857         char *replaced_mailbox_name = NULL;
6858
6859         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6860         EMSTORAGE_START_READ_TRANSACTION(transaction);
6861
6862         if (mailbox_name) {
6863                 if (strstr(mailbox_name, "'")) {
6864                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6865                 } else {
6866                         replaced_mailbox_name = strdup(mailbox_name);
6867                 }
6868
6869                 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6870
6871                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6872                         "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6873                         "WHERE account_id = %d "
6874                         "AND mailbox_id = '%s' "
6875                         "AND local_uid = %d "
6876                         "AND mailbox_name = '%s' "
6877                         "AND server_uid = '%s'",
6878                         account_id, mailbox_id, local_uid, replaced_mailbox_name, uid);
6879         } else {
6880                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6881                         "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
6882                         "WHERE account_id = %d "
6883                         "AND mailbox_id = '%s' "
6884                         "AND local_uid = %d "
6885                         "AND server_uid = '%s'",
6886                         account_id, mailbox_id, local_uid, uid);
6887         }
6888
6889
6890         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6891         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6892                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6893
6894
6895         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6896         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6897                 ("sqlite3_step fail:%d", rc));
6898
6899         if (rc == SQLITE_DONE) {
6900                 EM_DEBUG_LOG("no matched mail found....");
6901                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
6902                 goto FINISH_OFF;
6903         }
6904
6905         _get_stmt_field_data_int(hStmt, mail_size, 0);
6906
6907         ret = true;
6908
6909 FINISH_OFF:
6910         EM_SAFE_FREE(replaced_mailbox_name);
6911
6912         if (hStmt != NULL) {
6913                 rc = sqlite3_finalize(hStmt);
6914                 if (rc != SQLITE_OK) {
6915                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6916                         error = EMAIL_ERROR_DB_FAILURE;
6917                 }
6918         }
6919
6920         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6921
6922         if (err_code != NULL)
6923                 *err_code = error;
6924
6925         EM_DEBUG_FUNC_END("ret [%d]", ret);
6926         return ret;
6927 }
6928
6929 INTERNAL_FUNC int emstorage_add_downloaded_mail(char *multi_user_name, emstorage_read_mail_uid_tbl_t *read_mail_uid, int transaction, int *err_code)
6930 {
6931         EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], transaction[%d], err_code[%p]", read_mail_uid, transaction, err_code);
6932
6933         if (!read_mail_uid) {
6934                 EM_DEBUG_EXCEPTION("read_mail_uid[%p]", read_mail_uid);
6935                 if (err_code != NULL)
6936                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6937                 return false;
6938         }
6939
6940         int rc, rc2,  ret = false;
6941         int error = EMAIL_ERROR_NONE;
6942         DB_STMT hStmt = NULL;
6943         char sql_query_string[QUERY_SIZE] = {0, };
6944
6945         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6946         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6947
6948         char *sql = "SELECT max(rowid) FROM mail_read_mail_uid_tbl;";
6949         char **result = NULL;
6950
6951
6952         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
6953         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6954                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
6955
6956         if (NULL == result[1]) rc = 1;
6957         else rc = atoi(result[1])+1;
6958         sqlite3_free_table(result);
6959
6960         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6961                 "INSERT INTO mail_read_mail_uid_tbl VALUES "
6962                 "(?"  /* account_id */
6963                 ", ?"  /* mailbox_id */
6964                 ", ?"  /* mailbox_name */
6965                 ", ?"  /* local_uid */
6966                 ", ?"  /* server_uid */
6967                 ", ?"  /* rfc822_size */
6968                 ", ?"  /* sync_status */
6969                 ", ?"  /* flags_seen_field */
6970                 ", ?"  /* flags_flagged_field */
6971                 ", ?)");
6972
6973
6974         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
6975         if (rc2 != SQLITE_OK) {
6976                 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6977                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6978
6979                 error = EMAIL_ERROR_DB_FAILURE;
6980                 goto FINISH_OFF;
6981         }
6982
6983         EM_DEBUG_LOG("account_id[%d] mailbox_id[%d] local_uid [%d]"
6984                    "server_uid[%s] rfc822_size[%d] rc[%d]",
6985          read_mail_uid->account_id, read_mail_uid->mailbox_id, read_mail_uid->local_uid,
6986          read_mail_uid->server_uid, read_mail_uid->rfc822_size, rc);
6987
6988         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->account_id);
6989         _bind_stmt_field_data_int(hStmt, LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->mailbox_id);
6990         _bind_stmt_field_data_int(hStmt, LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->local_uid);
6991         _bind_stmt_field_data_string(hStmt, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL, (char *)read_mail_uid->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6992         _bind_stmt_field_data_string(hStmt, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, (char *)read_mail_uid->server_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
6993         _bind_stmt_field_data_int(hStmt, RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->rfc822_size);
6994         _bind_stmt_field_data_int(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_seen_field);
6995         _bind_stmt_field_data_int(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_flagged_field);
6996         _bind_stmt_field_data_int(hStmt, IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, rc);
6997
6998
6999         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7000         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
7001                 ("sqlite3_step fail:%d", rc));
7002         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7003                 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
7004
7005
7006         ret = true;
7007
7008 FINISH_OFF:
7009         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7010         if (hStmt != NULL) {
7011                 rc = sqlite3_finalize(hStmt);
7012                 if (rc != SQLITE_OK) {
7013                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7014                         error = EMAIL_ERROR_DB_FAILURE;
7015                 }
7016         }
7017
7018         if (err_code != NULL)
7019                 *err_code = error;
7020
7021         EM_DEBUG_FUNC_END("ret [%d]", ret);
7022         return ret;
7023 }
7024
7025 #ifdef __FEATURE_BODY_SEARCH__
7026 INTERNAL_FUNC int emstorage_add_mail_text(char *multi_user_name, emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
7027 {
7028         EM_DEBUG_FUNC_BEGIN("mail_text[%p], transaction[%d], err_code[%p]", mail_text, transaction, err_code);
7029
7030         if (!mail_text) {
7031                 EM_DEBUG_EXCEPTION("mail_text[%p]", mail_text);
7032                 if (err_code != NULL)
7033                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7034                 return false;
7035         }
7036
7037         int rc, rc2,  ret = false;
7038         int error = EMAIL_ERROR_NONE;
7039         DB_STMT hStmt = NULL;
7040         char sql_query_string[QUERY_SIZE] = {0, };
7041
7042         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7043         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7044
7045         char *sql = "SELECT max(rowid) FROM mail_text_tbl;";
7046         char **result = NULL;
7047
7048         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7049         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7050                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7051         sqlite3_free_table(result);
7052
7053         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7054                 "INSERT INTO mail_text_tbl VALUES "
7055                 "(?"
7056                 ", ?"
7057                 ", ?"
7058                 ", ?)");
7059
7060         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
7061         if (rc2 != SQLITE_OK) {
7062                 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7063                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc2, sqlite3_errmsg(local_db_handle));
7064
7065                 error = EMAIL_ERROR_DB_FAILURE;
7066                 goto FINISH_OFF;
7067         }
7068
7069         EM_DEBUG_LOG("mail_id[%d] account_id[%d] mailbox_id[%d]", mail_text->mail_id,
7070                                        mail_text->account_id, mail_text->mailbox_id);
7071         EM_DEBUG_LOG_DEV("body_text VALUE [%s] ", mail_text->body_text);
7072
7073         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mail_id);
7074         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->account_id);
7075         _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mailbox_id);
7076         _bind_stmt_field_data_string(hStmt, BODY_TEXT_IDX_IN_MAIL_TEXT_TBL, (char *)mail_text->body_text, 0, -1);
7077
7078         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7079         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
7080                 ("sqlite3_step fail:%d", rc));
7081         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7082                 ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
7083
7084         ret = true;
7085
7086 FINISH_OFF:
7087         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7088         if (hStmt != NULL) {
7089                 rc = sqlite3_finalize(hStmt);
7090                 if (rc != SQLITE_OK) {
7091                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7092                         error = EMAIL_ERROR_DB_FAILURE;
7093                 }
7094         }
7095
7096         if (err_code != NULL)
7097                 *err_code = error;
7098
7099         EM_DEBUG_FUNC_END("ret [%d]", ret);
7100         return ret;
7101 }
7102 #endif
7103
7104 INTERNAL_FUNC int emstorage_change_read_mail_uid(char *multi_user_name, int account_id, int mailbox_id, int local_uid, char *mailbox_name, char *uid, emstorage_read_mail_uid_tbl_t* read_mail_uid, int transaction, int *err_code)
7105 {
7106         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], local_uid[%d], mailbox_name[%p], uid[%p], read_mail_uid[%p], transaction[%d], err_code[%p]", account_id, mailbox_id, local_uid, mailbox_name, uid, read_mail_uid, transaction, err_code);
7107
7108         if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid) {
7109                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_id[%d], local_uid[%d], mailbox_name[%p], uid[%p], read_mail_uid[%p]", account_id, mailbox_id, local_uid, mailbox_name, uid, read_mail_uid);
7110
7111                 if (err_code != NULL)
7112                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7113                 return false;
7114         }
7115
7116         int rc, ret = false;
7117         int error = EMAIL_ERROR_NONE;
7118         DB_STMT hStmt = NULL;
7119         char sql_query_string[QUERY_SIZE] = {0, };
7120
7121         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7122
7123         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7124
7125         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7126                 "UPDATE mail_read_mail_uid_tbl SET"
7127                 "  account_id = ?"
7128                 ", mailbox_id = ?"
7129                 ", mailbox_name = ?"
7130                 ", local_uid  = ?"
7131                 ", server_uid = ?"
7132                 ", rfc822_size = ?"
7133                 ", flags_seen_field  = ?"
7134                 ", flags_flagged_field  = ?"
7135                 " WHERE account_id = ?"
7136                 " AND mailbox_id  = ?"
7137                 " AND local_uid   = ?"
7138                 " AND mailbox_name= ?"
7139                 " AND server_uid = ?");
7140
7141
7142         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7143         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7144         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7145                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7146
7147
7148         int i = 0;
7149
7150         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->account_id);
7151         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->mailbox_id);
7152         _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7153         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->local_uid);
7154         _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->server_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7155         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->rfc822_size);
7156         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_seen_field);
7157         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_flagged_field);
7158         _bind_stmt_field_data_int(hStmt, i++, account_id);
7159         _bind_stmt_field_data_int(hStmt, i++, mailbox_id);
7160         _bind_stmt_field_data_int(hStmt, i++, local_uid);
7161         _bind_stmt_field_data_string(hStmt, i++, (char*)mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7162         _bind_stmt_field_data_string(hStmt, i++, (char*)uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7163
7164
7165         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7166         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7167                 ("sqlite3_step fail:%d", rc));
7168
7169         ret = true;
7170
7171 FINISH_OFF:
7172         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7173         if (hStmt != NULL) {
7174                 rc = sqlite3_finalize(hStmt);
7175                 if (rc != SQLITE_OK) {
7176                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7177                         error = EMAIL_ERROR_DB_FAILURE;
7178                 }
7179         }
7180
7181         if (err_code != NULL)
7182                 *err_code = error;
7183
7184         EM_DEBUG_FUNC_END("ret [%d]", ret);
7185         return ret;
7186 }
7187
7188 INTERNAL_FUNC int emstorage_remove_downloaded_mail(char *multi_user_name,
7189                                                                                                         int account_id,
7190                                                                                                         int mailbox_id,
7191                                                                                                         char *mailbox_name,
7192                                                                                                         char *uid,
7193                                                                                                         int transaction,
7194                                                                                                         int *err_code)
7195 {
7196         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], mailbox_id[%d], mailbox_name[%s], "
7197                                                         "uid[%s], transaction[%d], err_code[%p]",
7198                                                         account_id, mailbox_id, mailbox_name, uid, transaction, err_code);
7199
7200         if (account_id < FIRST_ACCOUNT_ID) {
7201                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_name[%s], uid[%s]", account_id, mailbox_name, uid);
7202
7203                 if (err_code != NULL)
7204                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7205                 return false;
7206         }
7207
7208         int ret = false;
7209         int error = EMAIL_ERROR_NONE;
7210         char sql_query_string[QUERY_SIZE] = {0, };
7211         char *replaced_mailbox_name = NULL;
7212         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7213
7214         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7215
7216         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7217                                 "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d ", account_id);
7218
7219         if (mailbox_id > 0) {
7220                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7221                                         sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7222                                         "AND mailbox_id = %d ", mailbox_id);
7223         }
7224
7225         if (mailbox_name) {             /*  NULL means all mailbox_name */
7226                 if (strstr(mailbox_name, "'")) {
7227                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
7228                 } else {
7229                         replaced_mailbox_name = strdup(mailbox_name);
7230                 }
7231
7232                 SNPRINTF(sql_query_string+EM_SAFE_STRLEN(sql_query_string),
7233                                         sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7234                                         "AND mailbox_name = '%s' ", replaced_mailbox_name);
7235         }
7236
7237         if (uid) {              /*  NULL means all mail */
7238                 sqlite3_snprintf(sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7239                                                  sql_query_string + EM_SAFE_STRLEN(sql_query_string), "AND server_uid = '%q' ",
7240                                                  uid);
7241         }
7242
7243         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7244         if (error != EMAIL_ERROR_NONE) {
7245                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7246                         goto FINISH_OFF;
7247         }
7248
7249         ret = true;
7250
7251 FINISH_OFF:
7252         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7253
7254         EM_SAFE_FREE(replaced_mailbox_name);
7255
7256         if (err_code != NULL)
7257                 *err_code = error;
7258
7259         EM_DEBUG_FUNC_END("ret [%d]", ret);
7260         return ret;
7261 }
7262
7263 INTERNAL_FUNC int emstorage_free_read_mail_uid(emstorage_read_mail_uid_tbl_t** read_mail_uid, int count, int *err_code)
7264 {
7265         EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], count[%d], err_code[%p]", read_mail_uid, count, err_code);
7266
7267         int ret = false;
7268         int error = EMAIL_ERROR_NONE;
7269
7270         if (count > 0) {
7271                 if (!read_mail_uid || !*read_mail_uid) {
7272                         EM_DEBUG_EXCEPTION(" read_mail_uid[%p], count[%d]", read_mail_uid, count);
7273
7274                         error = EMAIL_ERROR_INVALID_PARAM;
7275                         goto FINISH_OFF;
7276                 }
7277
7278                 emstorage_read_mail_uid_tbl_t* p = *read_mail_uid;
7279                 int i;
7280
7281                 for (i = 0; i < count; i++) {
7282                         EM_SAFE_FREE(p[i].mailbox_name);
7283                         EM_SAFE_FREE(p[i].server_uid);
7284                 }
7285
7286                 EM_SAFE_FREE(p); *read_mail_uid = NULL;
7287         }
7288
7289         ret = true;
7290
7291 FINISH_OFF:
7292         if (err_code != NULL)
7293                 *err_code = error;
7294
7295         EM_DEBUG_FUNC_END("ret [%d]", ret);
7296         return ret;
7297 }
7298
7299 INTERNAL_FUNC int emstorage_get_rule_count_by_account_id(char *multi_user_name, int account_id, int *count, int transaction, int *err_code)
7300 {
7301         EM_DEBUG_FUNC_BEGIN("account_id [%d], count[%p], transaction[%d], err_code[%p]", count, transaction, err_code);
7302
7303         if (!count) {
7304                 EM_DEBUG_EXCEPTION("count[%p]", count);
7305
7306                 if (err_code != NULL)
7307                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7308                 return false;
7309         }
7310
7311         int rc = -1, ret = false;
7312         int error =  EMAIL_ERROR_NONE;
7313         char sql_query_string[QUERY_SIZE] = {0, };
7314
7315         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7316         EMSTORAGE_START_READ_TRANSACTION(transaction);
7317
7318         if (account_id != ALL_ACCOUNT)
7319                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl where account_id = %d", account_id);
7320         else
7321                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl");
7322
7323         char **result;
7324
7325         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7326         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7327                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7328
7329         *count = atoi(result[1]);
7330         sqlite3_free_table(result);
7331
7332         ret = true;
7333
7334 FINISH_OFF:
7335         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7336
7337         if (err_code != NULL)
7338                 *err_code = error;
7339
7340         EM_DEBUG_FUNC_END("ret [%d]", ret);
7341         return ret;
7342 }
7343
7344 INTERNAL_FUNC int emstorage_get_rule(char *multi_user_name, int account_id, int type, int start_idx, int *select_num, int *is_completed, emstorage_rule_tbl_t** rule_list, int transaction, int *err_code)
7345 {
7346         EM_DEBUG_FUNC_BEGIN("account_id[%d], type[%d], start_idx[%d], select_num[%p], is_completed[%p], rule_list[%p], transaction[%d], err_code[%p]", account_id, type, start_idx, select_num, is_completed, rule_list, transaction, err_code);
7347
7348         if (!select_num || !is_completed || !rule_list) {               /*  only global rule supported. */
7349                 EM_DEBUG_EXCEPTION(" account_id[%d], type[%d], start_idx[%d], select_num[%p], is_completed[%p], rule_list[%p]", account_id, type, start_idx, select_num, is_completed, rule_list);
7350
7351                 if (err_code != NULL)
7352                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7353                 return false;
7354         }
7355
7356         int ret = false;
7357         int error = EMAIL_ERROR_NONE;
7358
7359         emstorage_rule_tbl_t* p_data_tbl = NULL;
7360         int i = 0, count = 0;
7361         DB_STMT hStmt = NULL;
7362         char sql_query_string[QUERY_SIZE] = {0, };
7363
7364         int rc;
7365
7366         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7367         EMSTORAGE_START_READ_TRANSACTION(transaction);
7368
7369         if (account_id != ALL_ACCOUNT) {
7370                 if (type)
7371                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d AND type = %d", account_id, type);
7372                 else
7373                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d ORDER BY type", account_id);
7374         } else {
7375                 if (type)
7376                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE type = %d", type);
7377                 else
7378                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl ORDER BY type");
7379         }
7380
7381         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7382 /*      EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt); */
7383         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7384                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7385
7386
7387         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7388         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7389                 ("sqlite3_step fail:%d", rc));
7390
7391         char **result;
7392         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL); */
7393         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
7394         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7395                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7396
7397         sqlite3_free_table(result);
7398
7399         if (count == 0) {
7400                 EM_DEBUG_LOG_DEV("No matching rule found...");
7401                 ret = true;
7402                 error = EMAIL_ERROR_FILTER_NOT_FOUND; /*there is no matched rule*/
7403                 goto FINISH_OFF;
7404         }
7405
7406
7407         if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t) * count))) {
7408                 EM_DEBUG_EXCEPTION(" malloc failed...");
7409
7410                 error = EMAIL_ERROR_OUT_OF_MEMORY;
7411                 goto FINISH_OFF;
7412         }
7413
7414         memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t) * count);
7415
7416         for (i = 0; i < count; i++) {
7417                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7418                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7419                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7420                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].type), TYPE_IDX_IN_MAIL_RULE_TBL);
7421                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7422                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7423                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7424                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7425                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7426                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7427
7428                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7429                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7430                         ("sqlite3_step fail:%d", rc));
7431         }
7432
7433         ret = true;
7434
7435 FINISH_OFF:
7436
7437         EM_DEBUG_LOG("[%d] rules found.", count);
7438
7439         if (ret == true) {
7440                 *rule_list = p_data_tbl;
7441                 *select_num = count;
7442         } else if (p_data_tbl != NULL)
7443                 emstorage_free_rule(&p_data_tbl, count, NULL); /* CID FIX */
7444
7445         if (hStmt != NULL) {
7446                 rc = sqlite3_finalize(hStmt);
7447                 if (rc != SQLITE_OK) {
7448                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7449                         error = EMAIL_ERROR_DB_FAILURE;
7450                 }
7451         }
7452
7453         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7454
7455         if (err_code != NULL)
7456                 *err_code = error;
7457
7458         EM_DEBUG_FUNC_END("ret [%d]", ret);
7459         return ret;
7460 }
7461
7462 INTERNAL_FUNC int emstorage_get_rule_by_id(char *multi_user_name, int rule_id, emstorage_rule_tbl_t** rule, int transaction, int *err_code)
7463 {
7464         EM_DEBUG_FUNC_BEGIN("rule_id[%d], rule[%p], transaction[%d], err_code[%p]", rule_id, rule, transaction, err_code);
7465         int error = EMAIL_ERROR_NONE;
7466         int ret = false;
7467         DB_STMT hStmt = NULL;
7468
7469         if (rule_id <= 0) {
7470                 EM_DEBUG_EXCEPTION("Invalid parameter");
7471                 error = EMAIL_ERROR_INVALID_PARAM;
7472                 goto FINISH_OFF;
7473         }
7474
7475         if (!rule) {
7476                 EM_DEBUG_EXCEPTION("rule_id[%d], rule[%p]", rule_id, rule);
7477                 error = EMAIL_ERROR_INVALID_PARAM;
7478                 goto FINISH_OFF;
7479         }
7480
7481         emstorage_rule_tbl_t* p_data_tbl = NULL;
7482         int rc;
7483
7484         char sql_query_string[QUERY_SIZE] = {0, };
7485         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7486         EMSTORAGE_START_READ_TRANSACTION(transaction);
7487
7488         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7489
7490         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7491         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7492                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7493
7494
7495         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7496         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7497                 ("sqlite3_step fail:%d", rc));
7498
7499         if (rc == SQLITE_DONE) {
7500                 EM_DEBUG_EXCEPTION(" no matched rule found...");
7501                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7502                 goto FINISH_OFF;
7503         }
7504
7505         if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t)))) {
7506                 EM_DEBUG_EXCEPTION(" malloc failed...");
7507                 error = EMAIL_ERROR_OUT_OF_MEMORY;
7508                 goto FINISH_OFF;
7509         }
7510
7511         memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t));
7512         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7513         _get_stmt_field_data_int(hStmt, &(p_data_tbl->rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7514         _get_stmt_field_data_string(hStmt, &(p_data_tbl->filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7515         _get_stmt_field_data_int(hStmt, &(p_data_tbl->type), TYPE_IDX_IN_MAIL_RULE_TBL);
7516         _get_stmt_field_data_string(hStmt, &(p_data_tbl->value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7517         _get_stmt_field_data_string(hStmt, &(p_data_tbl->value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7518         _get_stmt_field_data_int(hStmt, &(p_data_tbl->action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7519         _get_stmt_field_data_int(hStmt, &(p_data_tbl->target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7520         _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7521         _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7522
7523         ret = true;
7524
7525 FINISH_OFF:
7526
7527         if (ret == true)
7528                 *rule = p_data_tbl;
7529
7530         if (hStmt != NULL) {
7531                 rc = sqlite3_finalize(hStmt);
7532                 if (rc != SQLITE_OK) {
7533                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7534                         error = EMAIL_ERROR_DB_FAILURE;
7535                 }
7536         }
7537
7538         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7539
7540         if (err_code != NULL)
7541                 *err_code = error;
7542
7543         EM_DEBUG_FUNC_END("ret [%d]", ret);
7544         return ret;
7545 }
7546
7547 INTERNAL_FUNC int emstorage_change_rule(char *multi_user_name, int rule_id, emstorage_rule_tbl_t* new_rule, int transaction, int *err_code)
7548 {
7549         EM_DEBUG_FUNC_BEGIN("rule_id[%d], new_rule[%p], transaction[%d], err_code[%p]", rule_id, new_rule, transaction, err_code);
7550
7551         if (!new_rule) {                /*  only global rule supported. */
7552                 EM_DEBUG_EXCEPTION("rule_id[%d], new_rule[%p]", rule_id, new_rule);
7553
7554                 if (err_code != NULL)
7555                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7556                 return false;
7557         }
7558
7559         int rc, ret = false;
7560         int error = EMAIL_ERROR_NONE;
7561
7562         DB_STMT hStmt = NULL;
7563         char sql_query_string[QUERY_SIZE] = {0, };
7564         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7565         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7566
7567         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7568                 "UPDATE mail_rule_tbl SET"
7569                 "  filter_name = ?"
7570                 ", type = ?"
7571                 ", value = ?"
7572                 ", value2 = ?"
7573                 ", action_type = ?"
7574                 ", target_mailbox_id = ?"
7575                 ", flag1 = ?"
7576                 ", flag2 = ?"
7577                 ", account_id = ?"
7578                 ", rule_id = ?"
7579                 " WHERE rule_id = %d"
7580                 , rule_id);
7581
7582
7583         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7584         EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
7585         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7586                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7587
7588         int i = 0;
7589
7590         _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->filter_name, 0, FILTER_NAME_LEN_IN_MAIL_RULE_TBL);
7591         _bind_stmt_field_data_int(hStmt, i++, new_rule->type);
7592         _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7593         _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7594         _bind_stmt_field_data_int(hStmt, i++, new_rule->action_type);
7595         _bind_stmt_field_data_int(hStmt, i++, new_rule->target_mailbox_id);
7596         _bind_stmt_field_data_int(hStmt, i++, new_rule->flag1);
7597         _bind_stmt_field_data_int(hStmt, i++, new_rule->flag2);
7598         _bind_stmt_field_data_int(hStmt, i++, new_rule->account_id);
7599         _bind_stmt_field_data_int(hStmt, i++, rule_id);
7600
7601
7602         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7603         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7604                 ("sqlite3_step fail:%d", rc));
7605
7606         ret = true;
7607
7608 FINISH_OFF:
7609         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7610         if (hStmt != NULL) {
7611                 rc = sqlite3_finalize(hStmt);
7612                 if (rc != SQLITE_OK) {
7613                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7614                         error = EMAIL_ERROR_DB_FAILURE;
7615                 }
7616         }
7617
7618         if (err_code != NULL)
7619                 *err_code = error;
7620
7621         EM_DEBUG_FUNC_END("ret [%d]", ret);
7622         return ret;
7623 }
7624
7625 INTERNAL_FUNC int emstorage_find_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7626 {
7627         EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7628
7629         if (!rule) {
7630                 EM_DEBUG_LOG("rule is NULL");
7631                 if (err_code != NULL)
7632                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7633                 return false;
7634         }
7635
7636         DB_STMT hStmt = NULL;
7637         char sql_query_string[QUERY_SIZE] = {0,};
7638         int error = EMAIL_ERROR_NONE;
7639         int rc = 0;
7640         int ret = false;
7641
7642         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7643         EMSTORAGE_START_READ_TRANSACTION(transaction);
7644
7645         switch (rule->action_type) {
7646         case EMAIL_FILTER_MOVE:
7647                 if (rule->type == EMAIL_PRIORITY_SENDER) {
7648                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7649                                 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7650                                 rule->action_type, rule->type, rule->value2);
7651                 } else {
7652                                 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7653                                         "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(filter_name) = UPPER(\'%q\')", rule->action_type, rule->type, rule->filter_name);
7654                 }
7655                 break;
7656         case EMAIL_FILTER_BLOCK:
7657                 if (rule->type == EMAIL_FILTER_FROM)
7658                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7659                                 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7660                                 rule->action_type, rule->type, rule->value2);
7661                 else if (rule->type == EMAIL_FILTER_SUBJECT)
7662                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7663                                 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value) = UPPER(\'%q\')",
7664                                 rule->action_type, rule->type, rule->value);
7665                 else if (rule->type == (EMAIL_FILTER_SUBJECT | EMAIL_FILTER_FROM))
7666                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7667                                 "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND (type = %d AND UPPER(value) = UPPER(\'%q\')) OR (type = %d AND UPPER(value2) = UPPER(\'%q\'))",
7668                                 rule->action_type, EMAIL_FILTER_SUBJECT, rule->value, EMAIL_FILTER_FROM, rule->value2);
7669                 break;
7670
7671         default:
7672                 EM_DEBUG_EXCEPTION("Invalid parameter : rule->action_type[%d]", rule->action_type);
7673                 error = EMAIL_ERROR_INVALID_PARAM;
7674                 goto FINISH_OFF;
7675                 break;
7676         }
7677
7678         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7679         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7680                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7681
7682         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7683         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7684                 ("sqlite3_step fail:%d", rc));
7685
7686         if (rc == SQLITE_DONE) {
7687                 EM_DEBUG_EXCEPTION(" no matched rule found...");
7688                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7689         }
7690
7691         ret = true;
7692
7693 FINISH_OFF:
7694
7695         if (hStmt != NULL) {
7696                 rc = sqlite3_finalize(hStmt);
7697                 if (rc != SQLITE_OK) {
7698                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7699                         error = EMAIL_ERROR_DB_FAILURE;
7700                 }
7701         }
7702
7703         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7704
7705         if (err_code)
7706                 *err_code = error;
7707
7708         EM_DEBUG_FUNC_END("ret [%d]", ret);
7709         return ret;
7710 }
7711
7712 INTERNAL_FUNC int emstorage_add_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7713 {
7714         EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7715
7716         if (!rule) {    /*  only global rule supported. */
7717                 EM_DEBUG_LOG("rule is NULL");
7718                 if (err_code != NULL)
7719                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7720                 return false;
7721         }
7722
7723         int rc, rc_2, ret = false;
7724         int error = EMAIL_ERROR_NONE;
7725         DB_STMT hStmt = NULL;
7726         char sql_query_string[QUERY_SIZE] = {0, };
7727
7728         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7729
7730         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7731
7732         char *sql;
7733         char **result;
7734         sql = "SELECT max(rowid) FROM mail_rule_tbl;";
7735
7736         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7737         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7738                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7739
7740         if (NULL == result[1])
7741                 rc = 1;
7742         else
7743                 rc = atoi(result[1])+1;
7744
7745         sqlite3_free_table(result);
7746
7747         rule->rule_id = rc;
7748
7749         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7750                 "INSERT INTO mail_rule_tbl VALUES "
7751                 "(?"            /*  account id */
7752                 ", ?"           /*  rule_id */
7753                 ", ?"           /*  filter_name */
7754                 ", ?"           /*  type */
7755                 ", ?"           /*  value */
7756                 ", ?"           /*  value2 */
7757                 ", ?"           /*  action_type */
7758                 ", ?"           /*  target_mailbox_id */
7759                 ", ?"           /*  flag1 */
7760                 ", ?)");        /*  flag2 */
7761
7762         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc_2);
7763         if (rc_2 != SQLITE_OK) {
7764                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc_2, sqlite3_errmsg(local_db_handle));
7765                 error = EMAIL_ERROR_DB_FAILURE;
7766                 goto FINISH_OFF;
7767         }
7768
7769         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL, rule->account_id);
7770         _bind_stmt_field_data_int(hStmt, RULE_ID_IDX_IN_MAIL_RULE_TBL, rule->rule_id);
7771         _bind_stmt_field_data_string(hStmt, FILTER_NAME_IDX_IN_MAIL_RULE_TBL, (char*)rule->filter_name, 0, FILTER_NAME_LEN_IN_MAIL_RULE_TBL);
7772         _bind_stmt_field_data_int(hStmt, TYPE_IDX_IN_MAIL_RULE_TBL, rule->type);
7773         _bind_stmt_field_data_string(hStmt, VALUE_IDX_IN_MAIL_RULE_TBL, (char*)rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7774         _bind_stmt_field_data_string(hStmt, VALUE2_IDX_IN_MAIL_RULE_TBL, (char*)rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7775         _bind_stmt_field_data_int(hStmt, ACTION_TYPE_IDX_IN_MAIL_RULE_TBL, rule->action_type);
7776         _bind_stmt_field_data_int(hStmt, TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL, rule->target_mailbox_id);
7777         _bind_stmt_field_data_int(hStmt, FLAG1_IDX_IN_MAIL_RULE_TBL, rule->flag1);
7778         _bind_stmt_field_data_int(hStmt, FLAG2_IDX_IN_MAIL_RULE_TBL, rule->flag2);
7779
7780         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7781         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7782                 ("sqlite3_step fail:%d", rc));
7783
7784         ret = true;
7785
7786 FINISH_OFF:
7787         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7788         if (hStmt != NULL) {
7789                 rc = sqlite3_finalize(hStmt);
7790                 if (rc != SQLITE_OK) {
7791                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7792                         error = EMAIL_ERROR_DB_FAILURE;
7793                 }
7794         }
7795
7796         if (err_code != NULL)
7797                 *err_code = error;
7798
7799         EM_DEBUG_FUNC_END("ret [%d]", ret);
7800         return ret;
7801 }
7802
7803 INTERNAL_FUNC int emstorage_delete_rule(char *multi_user_name, int rule_id, int transaction, int *err_code)
7804 {
7805         EM_DEBUG_FUNC_BEGIN("rule_id[%d], transaction[%d], err_code[%p]", rule_id, transaction, err_code);
7806
7807         if (rule_id <= 0) {             /*  only global rule supported. */
7808                 EM_DEBUG_EXCEPTION("rule_id[%d]", rule_id);
7809
7810                 if (err_code != NULL)
7811                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7812                 return false;
7813         }
7814
7815         int rc, ret = false;
7816         int error = EMAIL_ERROR_NONE;
7817         char sql_query_string[QUERY_SIZE] = {0, };
7818         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7819         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7820
7821         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7822         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7823         if (error != EMAIL_ERROR_NONE) {
7824                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7825                         goto FINISH_OFF;
7826         }
7827
7828         rc = sqlite3_changes(local_db_handle);
7829         if (rc == 0) {
7830                 EM_DEBUG_EXCEPTION(" no matched rule found...");
7831
7832                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7833                 goto FINISH_OFF;
7834         }
7835
7836         ret = true;
7837
7838 FINISH_OFF:
7839         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7840
7841         if (err_code != NULL)
7842                 *err_code = error;
7843
7844         EM_DEBUG_FUNC_END("ret [%d]", ret);
7845         return ret;
7846 }
7847
7848 INTERNAL_FUNC int emstorage_free_rule(emstorage_rule_tbl_t** rule_list, int count, int *err_code)
7849 {
7850         EM_DEBUG_FUNC_BEGIN("rule_list[%p], conut[%d], err_code[%p]", rule_list, count, err_code);
7851
7852         int ret = false;
7853         int error = EMAIL_ERROR_NONE;
7854
7855         if (count > 0) {
7856                 if (!rule_list || !*rule_list) {
7857                         EM_DEBUG_EXCEPTION(" rule_list[%p], conut[%d]", rule_list, count);
7858
7859                         error = EMAIL_ERROR_INVALID_PARAM;
7860                         goto FINISH_OFF;
7861                 }
7862
7863                 emstorage_rule_tbl_t* p = *rule_list;
7864                 int i = 0;
7865
7866                 for (; i < count; i++) {
7867                         EM_SAFE_FREE(p[i].value);
7868                 }
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), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND account_id = %d", account_id);
7939
7940                 if (mailbox_id) {
7941                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND mailbox_id = %d", mailbox_id);
7942                 } else
7943                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND mailbox_type NOT IN (3, 5)");
7944
7945                 char **result;
7946                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7947                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7948                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7949
7950                 *unseen = atoi(result[1]);
7951                 sqlite3_free_table(result);
7952
7953         }
7954 FINISH_OFF:
7955         ret = true;
7956
7957 FINISH_OFF2:
7958
7959 #ifndef USE_PREPARED_QUERY_
7960         if (hStmt != NULL) {
7961                 rc = sqlite3_finalize(hStmt);
7962                 if (rc != SQLITE_OK) {
7963                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7964                         error = EMAIL_ERROR_DB_FAILURE;
7965                 }
7966         }
7967 #endif
7968
7969         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7970
7971         EM_SAFE_FREE(replaced_mailbox_name);
7972
7973         if (err_code != NULL)
7974                 *err_code = error;
7975
7976         EM_DEBUG_FUNC_END("ret [%d]", ret);
7977         return ret;
7978 }
7979
7980 INTERNAL_FUNC int emstorage_get_mail_field_by_id(char *multi_user_name, int mail_id, int type, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
7981 {
7982         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
7983
7984         if (mail_id <= 0 || !mail) {
7985                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
7986                 if (err_code != NULL)
7987                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7988                 return false;
7989         }
7990
7991         int col_index = 0;
7992         emstorage_mail_tbl_t* p_data_tbl = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
7993
7994         if (p_data_tbl == NULL) {
7995                 EM_DEBUG_EXCEPTION("malloc failed...");
7996                 if (err_code != NULL)
7997                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
7998                 return false;
7999         }
8000
8001         memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8002         DB_STMT hStmt = NULL;
8003         char sql_query_string[QUERY_SIZE] = {0, };
8004
8005         int rc, ret = false;
8006         int error = EMAIL_ERROR_NONE;
8007
8008         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8009         EMSTORAGE_START_READ_TRANSACTION(transaction);
8010
8011         switch (type) {
8012                 case RETRIEVE_SUMMARY:
8013                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8014                                 "SELECT account_id, "
8015                                 "mail_id, "
8016                                 "mailbox_id, "
8017                                 "server_mail_status, "
8018                                 "server_mailbox_name, "
8019                                 "server_mail_id, "
8020                                 "file_path_plain, "
8021                                 "file_path_html,"
8022                                 "file_path_mime_entity, "
8023                                 "flags_seen_field, "
8024                                 "save_status, "
8025                                 "lock_status, "
8026                                 "thread_id, "
8027                                 "thread_item_count "
8028                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8029                         break;
8030
8031                 case RETRIEVE_FIELDS_FOR_DELETE:
8032                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8033                                 "SELECT account_id, "
8034                                 "mail_id, "
8035                                 "server_mail_status, "
8036                                 "server_mailbox_name, "
8037                                 "server_mail_id "
8038                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8039                         break;
8040
8041                 case RETRIEVE_ACCOUNT:
8042                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8043                                 "SELECT account_id "
8044                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8045                         break;
8046
8047                 case RETRIEVE_FLAG:
8048                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8049                                 "SELECT account_id, "
8050                                 "flags_seen_field, "
8051                                 "thread_id, "
8052                                 "mailbox_id "
8053                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8054                         break;
8055
8056                 default:
8057                         EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
8058                         error = EMAIL_ERROR_INVALID_PARAM;
8059                         goto FINISH_OFF;
8060         }
8061
8062         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
8063
8064         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8065         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8066                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8067
8068
8069         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8070         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8071                 ("sqlite3_step fail:%d", rc));
8072
8073         if (rc == SQLITE_DONE) {
8074                 EM_DEBUG_LOG("no matched mail found...");
8075                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8076                 goto FINISH_OFF;
8077         }
8078         switch (type) {
8079                 case RETRIEVE_SUMMARY:
8080                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8081                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
8082                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
8083                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
8084                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
8085                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8086                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, col_index++);
8087                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, col_index++);
8088                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, col_index++);
8089                         _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8090                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), col_index++);
8091                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), col_index++);
8092                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8093                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_item_count), col_index++);
8094                         break;
8095
8096                 case RETRIEVE_FIELDS_FOR_DELETE:
8097                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8098                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
8099                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
8100                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
8101                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8102                         break;
8103
8104                 case RETRIEVE_ACCOUNT:
8105                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8106                         break;
8107
8108                 case RETRIEVE_FLAG:
8109                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8110                         _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8111                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8112                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
8113                         break;
8114         }
8115
8116         ret = true;
8117
8118 FINISH_OFF:
8119         if (ret == true)
8120                 *mail = p_data_tbl;
8121         else if (p_data_tbl != NULL)
8122                 emstorage_free_mail(&p_data_tbl,  1, NULL);
8123
8124         if (hStmt != NULL) {
8125                 rc = sqlite3_finalize(hStmt);
8126                 if (rc != SQLITE_OK) {
8127                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8128                         error = EMAIL_ERROR_DB_FAILURE;
8129                 }
8130         }
8131
8132
8133         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8134
8135         if (err_code != NULL)
8136                 *err_code = error;
8137
8138         EM_DEBUG_FUNC_END("ret [%d]", ret);
8139         return ret;
8140 }
8141
8142 INTERNAL_FUNC int emstorage_get_mail_field_by_multiple_mail_id(char *multi_user_name, int mail_ids[], int number_of_mails, int type, emstorage_mail_tbl_t** mail, int transaction, int *err_code)
8143 {
8144         EM_DEBUG_FUNC_BEGIN("mail_ids[%p], number_of_mails [%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_ids, number_of_mails, type, mail, transaction, err_code);
8145
8146         int ret = false;
8147         int error = EMAIL_ERROR_NONE;
8148         int query_string_length = 0;
8149         int i = 0, item_count = 0, rc = -1, field_count, col_index, cur_sql_query_string = 0;
8150         char **result = NULL;
8151         char *sql_query_string = NULL;
8152         emstorage_mail_tbl_t* p_data_tbl = NULL;
8153         sqlite3 *local_db_handle = NULL;
8154
8155         if (number_of_mails <= 0 || !mail_ids) {
8156                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8157                 if (err_code != NULL)
8158                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8159                 return false;
8160         }
8161
8162         p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * number_of_mails);
8163
8164         query_string_length = (sizeof(char) * 8 * number_of_mails) + 512;
8165         sql_query_string = (char*)em_malloc(query_string_length);
8166
8167         if (p_data_tbl == NULL || sql_query_string == NULL) {
8168                 EM_DEBUG_EXCEPTION("malloc failed...");
8169
8170                 EM_SAFE_FREE(p_data_tbl);
8171                 EM_SAFE_FREE(sql_query_string);
8172
8173                 if (err_code != NULL)
8174                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8175                 return false;
8176         }
8177
8178         local_db_handle = emstorage_get_db_connection(multi_user_name);
8179
8180         EMSTORAGE_START_READ_TRANSACTION(transaction);
8181
8182         switch (type) {
8183                 case RETRIEVE_SUMMARY:
8184                         cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8185                                 "SELECT account_id, "
8186                                 "mail_id, "
8187                                 "mailbox_id, "
8188                                 "server_mail_status, "
8189                                 "server_mailbox_name, "
8190                                 "server_mail_id, "
8191                                 "file_path_plain, "
8192                                 "file_path_html, "
8193                                 "file_path_mime_entity, "
8194                                 "subject, "
8195                                 "flags_seen_field, "
8196                                 "save_status, "
8197                                 "lock_status, "
8198                                 "thread_id, "
8199                                 "thread_item_count "
8200                                 "FROM mail_tbl WHERE mail_id in (");
8201                         field_count = 15;
8202                         break;
8203
8204                 case RETRIEVE_FIELDS_FOR_DELETE:
8205                         cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8206                                 "SELECT account_id, "
8207                                 "mail_id, "
8208                                 "server_mail_status, "
8209                                 "server_mailbox_name, "
8210                                 "server_mail_id "
8211                                 "FROM mail_tbl WHERE mail_id in (");
8212                         field_count = 5;
8213                         break;
8214
8215                 case RETRIEVE_ACCOUNT:
8216                         cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8217                                 "SELECT account_id FROM mail_tbl WHERE mail_id in (");
8218                         field_count = 1;
8219                         break;
8220
8221                 case RETRIEVE_FLAG:
8222                         cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8223                                 "SELECT account_id, "
8224                                 "mail_id, "
8225                                 "mailbox_id, "
8226                                 "flags_seen_field, "
8227                                 "thread_id "
8228                                 "FROM mail_tbl WHERE mail_id in (");
8229                         field_count = 5;
8230                         break;
8231
8232                 default:
8233                         EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
8234                         error = EMAIL_ERROR_INVALID_PARAM;
8235                         goto FINISH_OFF;
8236         }
8237
8238         for (i = 0; i < number_of_mails; i++)
8239                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_string_length, "%d,", mail_ids[i]);
8240         sql_query_string[EM_SAFE_STRLEN(sql_query_string) - 1] = ')';
8241
8242         EM_DEBUG_LOG_SEC("Query [%s], Length [%d]", sql_query_string, EM_SAFE_STRLEN(sql_query_string));
8243
8244         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &item_count, 0, NULL), rc);
8245         if (SQLITE_OK != rc && -1 != rc) {
8246                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
8247                 error = EMAIL_ERROR_DB_FAILURE;
8248                 goto FINISH_OFF;
8249         }
8250
8251         if (rc == SQLITE_DONE) {
8252                 EM_DEBUG_LOG("no matched mail found...");
8253                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8254                 goto FINISH_OFF;
8255         }
8256
8257         EM_DEBUG_LOG("item_count [%d]", item_count);
8258
8259         if (number_of_mails != item_count) {
8260                 EM_DEBUG_EXCEPTION("Can't find all emails");
8261                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8262                 goto FINISH_OFF;
8263         }
8264
8265         col_index = field_count;
8266
8267         for (i = 0; i < item_count; i++)        {
8268                 switch (type) {
8269                         case RETRIEVE_SUMMARY:
8270                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8271                                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8272                                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8273                                 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8274                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8275                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8276                                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
8277                                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
8278                                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
8279                                 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 0, col_index++);
8280                                 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8281                                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
8282                                 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
8283                                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8284                                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
8285                                 break;
8286
8287                         case RETRIEVE_FIELDS_FOR_DELETE:
8288                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8289                                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8290                                 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8291                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8292                                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8293                                 break;
8294
8295                         case RETRIEVE_ACCOUNT:
8296                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8297                                 break;
8298
8299                         case RETRIEVE_FLAG:
8300                                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8301                                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8302                                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8303                                 _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8304                                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8305                                 break;
8306                 }
8307         }
8308
8309         ret = true;
8310
8311 FINISH_OFF:
8312         if (ret == true)
8313                 *mail = p_data_tbl;
8314         else
8315                 emstorage_free_mail(&p_data_tbl, number_of_mails, NULL);
8316
8317         if (result)
8318                 sqlite3_free_table(result);
8319
8320         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8321
8322         EM_SAFE_FREE(sql_query_string);
8323
8324         if (err_code != NULL)
8325                 *err_code = error;
8326
8327         EM_DEBUG_FUNC_END("ret [%d]", ret);
8328         return ret;
8329 }
8330
8331 INTERNAL_FUNC int emstorage_get_mail_by_id(char *multi_user_name, int mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code)
8332 {
8333         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8334
8335         if (mail_id <= 0 || !mail) {
8336                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
8337                 if (err_code != NULL)
8338                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8339                 return false;
8340         }
8341
8342         int ret = false, error = EMAIL_ERROR_NONE, count;
8343         char conditional_clause[QUERY_SIZE] = {0, };
8344         emstorage_mail_tbl_t* p_data_tbl = NULL;
8345
8346         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8347         EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8348
8349         if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8350                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8351                 goto FINISH_OFF;
8352         }
8353
8354         ret = true;
8355
8356 FINISH_OFF:
8357         if (ret == true)
8358                 *mail = p_data_tbl;
8359         else if (p_data_tbl != NULL)
8360                 emstorage_free_mail(&p_data_tbl, 1, &error);
8361
8362         if (err_code != NULL)
8363                 *err_code = error;
8364
8365         EM_DEBUG_FUNC_END("ret [%d]", ret);
8366         return ret;
8367 }
8368
8369 #ifdef __FEATURE_BODY_SEARCH__
8370 INTERNAL_FUNC int emstorage_get_mail_text_by_id(char *multi_user_name, int mail_id, emstorage_mail_text_tbl_t **mail_text, int transaction, int *err_code)
8371 {
8372         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
8373
8374         if (mail_id <= 0 || !mail_text) {
8375                 EM_DEBUG_EXCEPTION("mail_id[%d], mail_text[%p]", mail_id, mail_text);
8376                 if (err_code != NULL)
8377                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8378                 return false;
8379         }
8380
8381         int ret = false;
8382         int error = EMAIL_ERROR_NONE;
8383         int count = 0;
8384         char conditional_clause[QUERY_SIZE] = {0, };
8385         emstorage_mail_text_tbl_t *p_data_tbl = NULL;
8386
8387         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8388         EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8389
8390         if (!emstorage_query_mail_text_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8391                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8392                 goto FINISH_OFF;
8393         }
8394
8395         ret = true;
8396
8397 FINISH_OFF:
8398         if (ret == true)
8399                 *mail_text = p_data_tbl;
8400         else if (p_data_tbl != NULL)
8401                 emstorage_free_mail_text(&p_data_tbl, 1, &error);
8402
8403         if (err_code != NULL)
8404                 *err_code = error;
8405
8406         EM_DEBUG_FUNC_END("ret [%d]", ret);
8407         return ret;
8408 }
8409 #endif
8410
8411 INTERNAL_FUNC int emstorage_mail_search_start(char *multi_user_name,
8412                                                                                         emstorage_search_filter_t *search,
8413                                                                                         int account_id,
8414                                                                                         int mailbox_id,
8415                                                                                         int sorting,
8416                                                                                         DB_STMT *search_handle,
8417                                                                                         int *searched,
8418                                                                                         int transaction,
8419                                                                                         int *err_code)
8420 {
8421         EM_DEBUG_FUNC_BEGIN("search[%p], account_id[%d], mailbox_id[%d], sorting[%d], "
8422                                                 "search_handle[%p], searched[%p], transaction[%d], err_code[%p]",
8423                                                 search, account_id, mailbox_id, sorting, search_handle,
8424                                                 searched, transaction, err_code);
8425
8426         if (!search_handle || !searched) {
8427                 if (err_code != NULL)
8428                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8429                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8430                 EM_DEBUG_FUNC_END("false");
8431                 return false;
8432         }
8433
8434         emstorage_search_filter_t* p = search;
8435         int error = EMAIL_ERROR_NONE;
8436         DB_STMT hStmt = NULL;
8437         char sql_query_string[QUERY_SIZE] = {0, };
8438         int rc, ret = false;
8439         int and = false, mail_count = 0;
8440
8441         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8442         EMSTORAGE_START_READ_TRANSACTION(transaction);
8443
8444         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl");
8445
8446         if (account_id != ALL_ACCOUNT) {
8447                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE account_id = %d", account_id);
8448                 and = true;
8449         }
8450
8451         if (mailbox_id) {
8452                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s mailbox_id = %d", and ? "AND" : "WHERE", mailbox_id);
8453                 and = true;
8454         }
8455
8456         while (p) {
8457                 if (p->key_type) {
8458                         if (!strncmp(p->key_type, "subject", strlen("subject"))) {
8459                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s subject LIKE '%%%%%s%%%%'", and ? "AND" : "WHERE", p->key_value);
8460                                 and = true;
8461                         } else if (!strncmp(p->key_type, "full_address_from", strlen("full_address_from"))) {
8462                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s full_address_from LIKE '%%%%%s%%%%'", and ? "AND" : "WHERE", p->key_value);
8463                                 and = true;
8464                         } else if (!strncmp(p->key_type, "full_address_to", strlen("full_address_to"))) {
8465                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s full_address_to LIKE '%%%%%s%%%%'", and ? "AND" : "WHERE", p->key_value);
8466                                 and = true;
8467                         } else if (!strncmp(p->key_type, "email_address", strlen("email_address"))) {
8468                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s email_address_sender = '%s' OR email_address_recipient = '%s'", and ? "AND" : "WHERE", p->key_value, p->key_value);
8469                                 and = true;
8470                         }
8471                         p = p->next;
8472                 }
8473         }
8474
8475         if (sorting)
8476                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " ORDER BY date_time");
8477
8478         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
8479
8480
8481         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8482         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8483                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8484
8485
8486         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8487         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8488                 ("sqlite3_step fail:%d", rc));
8489
8490         char **result;
8491
8492         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &mail_count, NULL, NULL), rc);
8493         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
8494                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8495
8496         sqlite3_free_table(result);
8497
8498         ret = true;
8499
8500 FINISH_OFF:
8501         if (ret == true) {
8502                 *search_handle = hStmt;
8503                 *searched = mail_count;
8504                 EM_DEBUG_LOG("mail_count [%d]", mail_count);
8505         } else {
8506                 if (hStmt != NULL) {
8507                         rc = sqlite3_finalize(hStmt);
8508                         if (rc != SQLITE_OK) {
8509                                 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8510                                 error = EMAIL_ERROR_DB_FAILURE;
8511                         }
8512                 }
8513
8514                 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8515         }
8516
8517         if (err_code != NULL)
8518                 *err_code = error;
8519
8520         EM_DEBUG_FUNC_END("ret [%d]", ret);
8521         return ret;
8522 }
8523
8524 INTERNAL_FUNC int emstorage_mail_search_result(DB_STMT search_handle, emstorage_mail_field_type_t type, void** data, int transaction, int *err_code)
8525 {
8526         EM_DEBUG_FUNC_BEGIN("search_handle[%d], type[%d], data[%p], transaction[%d], err_code[%p]", search_handle, type, data, transaction, err_code);
8527
8528         if (search_handle == 0 || !data) {
8529                 EM_DEBUG_EXCEPTION(" search_handle[%d], type[%d], data[%p]", search_handle, type, data);
8530
8531                 if (err_code != NULL)
8532                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8533                 return false;
8534         }
8535
8536         emstorage_mail_tbl_t* p_data_tbl = NULL;
8537         DB_STMT hStmt = search_handle;
8538         int rc, ret = false;
8539         int error = EMAIL_ERROR_NONE;
8540
8541         switch (type) {
8542                 case RETRIEVE_ID:
8543                         _get_stmt_field_data_int(hStmt, (int *)data, MAIL_ID_IDX_IN_MAIL_TBL);
8544                         break;
8545
8546                 case RETRIEVE_ENVELOPE:
8547                 case RETRIEVE_ALL:
8548                         if (!(p_data_tbl = em_malloc(sizeof(emstorage_mail_tbl_t)))) {
8549                                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
8550                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
8551                                 goto FINISH_OFF;
8552                         }
8553
8554                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8555                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8556                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_size), MAIL_SIZE_IDX_IN_MAIL_TBL);
8557                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8558                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_from), 1, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL);
8559                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_to), 1, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL);
8560                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject), 1, SUBJECT_IDX_IN_MAIL_TBL);
8561                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->body_download_status), BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL);
8562                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL);
8563                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8564                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8565                         _get_stmt_field_data_time_t(hStmt, &(p_data_tbl->date_time), DATETIME_IDX_IN_MAIL_TBL);
8566                         _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL);
8567                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->DRM_status), DRM_STATUS_IDX_IN_MAIL_TBL);
8568                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->priority), PRIORITY_IDX_IN_MAIL_TBL);
8569                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), SAVE_STATUS_IDX_IN_MAIL_TBL);
8570                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), LOCK_STATUS_IDX_IN_MAIL_TBL);
8571                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->report_status), REPORT_STATUS_IDX_IN_MAIL_TBL);
8572                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8573                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->meeting_request_status), MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL);
8574                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->message_class), MESSAGE_CLASS_IDX_IN_MAIL_TBL);
8575                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->digest_type), DIGEST_TYPE_IDX_IN_MAIL_TBL);
8576                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->smime_type), SMIME_TYPE_IDX_IN_MAIL_TBL);
8577                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->scheduled_sending_time), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8578                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->remaining_resend_times), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8579                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->tag_id), TAG_ID_IDX_IN_MAIL_TBL);
8580                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->eas_data_length), EAS_DATA_LENGTH_IDX_IN_MAIL_TBL);
8581                         _get_stmt_field_data_blob(hStmt, (void**)&(p_data_tbl->eas_data), EAS_DATA_IDX_IN_MAIL_TBL);
8582
8583                         if (type == RETRIEVE_ALL) {
8584                                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8585                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8586                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_reply), 1, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL);
8587                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_cc), 1, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL);
8588                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_bcc), 1, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL);
8589                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_return), 1, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL);
8590                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->message_id), 0, MESSAGE_ID_IDX_IN_MAIL_TBL);
8591                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8592                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8593                                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_count), ATTACHMENT_COUNT_IDX_IN_MAIL_TBL);
8594                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8595                                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->replied_time), REPLIED_TIME_IDX_IN_MAIL_TBL);
8596                                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->forwarded_time), FORWARDED_TIME_IDX_IN_MAIL_TBL);
8597                                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->default_charset), 0, DEFAULT_CHARSET_IDX_IN_MAIL_TBL);
8598                         }
8599
8600                         if (p_data_tbl->body_download_status) {
8601                                 struct stat buf;
8602
8603                                 if (p_data_tbl->file_path_html) {
8604                                         if (stat(p_data_tbl->file_path_html, &buf) == -1)
8605                                                 p_data_tbl->body_download_status = 0;
8606                                 } else if (p_data_tbl->file_path_plain) {
8607                                         if (stat(p_data_tbl->file_path_plain, &buf) == -1)
8608                                                 p_data_tbl->body_download_status = 0;
8609                                 } else
8610                                         p_data_tbl->body_download_status = 0;
8611                         }
8612
8613                         *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8614                         break;
8615
8616                 case RETRIEVE_SUMMARY:
8617                         if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
8618                                 EM_DEBUG_EXCEPTION(" malloc failed...");
8619
8620                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
8621                                 goto FINISH_OFF;
8622                         }
8623
8624                         memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8625
8626                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8627                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8628                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8629                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8630                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8631
8632                         *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8633                         break;
8634
8635                 case RETRIEVE_ADDRESS:
8636                         if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
8637                                 EM_DEBUG_EXCEPTION(" malloc failed...");
8638                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
8639                                 goto FINISH_OFF;
8640                         }
8641
8642                         memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8643                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8644                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8645                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8646                         *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8647                         break;
8648
8649                 default:
8650                         break;
8651         }
8652
8653         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8654         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8655                 ("sqlite3_step fail:%d", rc));
8656
8657         ret = true;
8658
8659 FINISH_OFF:
8660
8661         if (err_code != NULL)
8662                 *err_code = error;
8663
8664         if (ret == false && p_data_tbl)
8665                 emstorage_free_mail(&p_data_tbl, 1, NULL);
8666
8667         EM_DEBUG_FUNC_END("ret [%d]", ret);
8668         return ret;
8669 }
8670
8671 INTERNAL_FUNC int emstorage_mail_search_end(DB_STMT search_handle, int transaction, int *err_code)
8672 {
8673         EM_DEBUG_FUNC_BEGIN("search_handle[%d], transaction[%d], err_code[%p]", search_handle, transaction, err_code);
8674
8675         int error = EMAIL_ERROR_NONE;
8676         int rc, ret = false;
8677
8678         if (search_handle == 0) {
8679                 EM_DEBUG_EXCEPTION(" search_handle[%d]", search_handle);
8680                 error = EMAIL_ERROR_INVALID_PARAM;
8681                 goto FINISH_OFF;
8682         }
8683
8684         DB_STMT hStmt = search_handle;
8685
8686         rc = sqlite3_finalize(hStmt);
8687         if (rc != SQLITE_OK) {
8688                 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8689                 error = EMAIL_ERROR_DB_FAILURE;
8690         }
8691
8692         ret = true;
8693
8694 FINISH_OFF:
8695         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8696
8697         if (err_code != NULL)
8698                 *err_code = error;
8699
8700         EM_DEBUG_FUNC_END("ret [%d]", ret);
8701         return ret;
8702 }
8703
8704 INTERNAL_FUNC int emstorage_change_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t *mail, int transaction, int *err_code)
8705 {
8706         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8707
8708         if (mail_id <= 0 || !mail) {
8709                 EM_DEBUG_EXCEPTION(" mail_id[%d], mail[%p]", mail_id, mail);
8710
8711                 if (err_code != NULL)
8712                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8713                 return false;
8714         }
8715
8716         DB_STMT hStmt = NULL;
8717         char sql_query_string[QUERY_SIZE] = {0, };
8718         int rc = -1;
8719         int ret = false;
8720         int error = EMAIL_ERROR_NONE;
8721         int i = 0;
8722         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8723         char mailbox_id_param_string[10] = {0,};
8724
8725         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
8726
8727         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8728                 "UPDATE mail_tbl SET"
8729                 "  mail_id = ?"
8730                 ", account_id = ?"
8731                 ", mailbox_id = ?"
8732                 ", mail_size = ?"
8733                 ", server_mail_status = ?"
8734                 ", server_mailbox_name = ?"
8735                 ", server_mail_id = ?"
8736                 ", reference_mail_id = ?"
8737                 ", full_address_from = ?"
8738                 ", full_address_reply = ?"  /* 10 */
8739                 ", full_address_to = ?"
8740                 ", full_address_cc = ?"
8741                 ", full_address_bcc = ?"
8742                 ", full_address_return = ?"
8743                 ", subject = ?"
8744                 ", body_download_status = ?"
8745                 ", file_path_plain = ?"
8746                 ", file_path_html = ?"
8747                 ", file_path_mime_entity = ?"
8748                 ", date_time = ?"
8749                 ", flags_seen_field      = ?"
8750                 ", flags_deleted_field   = ?"
8751                 ", flags_flagged_field   = ?"
8752                 ", flags_answered_field  = ?"
8753                 ", flags_recent_field    = ?"
8754                 ", flags_draft_field     = ?"
8755                 ", flags_forwarded_field = ?"
8756                 ", DRM_status = ?"
8757                 ", priority = ?"
8758                 ", save_status = ?"
8759                 ", lock_status = ?"
8760                 ", message_id = ?"
8761                 ", report_status = ?"
8762                 ", preview_text = ?"
8763                 ", smime_type = ?"
8764                 ", scheduled_sending_time = ?"
8765                 ", remaining_resend_times = ?"
8766                 ", tag_id = ?"
8767                 ", replied_time = ?"
8768                 ", forwarded_time = ?"
8769                 ", default_charset = ?"
8770                 ", eas_data_length = ?"
8771                 ", eas_data = ?"
8772                 " WHERE mail_id = %d AND account_id != 0 "
8773                 , mail_id);
8774
8775
8776         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8777         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8778                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8779
8780         _bind_stmt_field_data_int(hStmt, i++, mail->mail_id);
8781         _bind_stmt_field_data_int(hStmt, i++, mail->account_id);
8782         _bind_stmt_field_data_int(hStmt, i++, mail->mailbox_id);
8783         _bind_stmt_field_data_int(hStmt, i++, mail->mail_size);
8784         _bind_stmt_field_data_int(hStmt, i++, mail->server_mail_status);
8785         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mailbox_name, 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
8786         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mail_id, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
8787         _bind_stmt_field_data_int(hStmt, i++, mail->reference_mail_id);
8788         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
8789         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
8790         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
8791         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
8792         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
8793         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
8794         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8795         _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
8796         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8797         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8798         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
8799         _bind_stmt_field_data_int(hStmt, i++, mail->date_time);
8800         _bind_stmt_field_data_char(hStmt, i++, mail->flags_seen_field);
8801         _bind_stmt_field_data_char(hStmt, i++, mail->flags_deleted_field);
8802         _bind_stmt_field_data_char(hStmt, i++, mail->flags_flagged_field);
8803         _bind_stmt_field_data_char(hStmt, i++, mail->flags_answered_field);
8804         _bind_stmt_field_data_char(hStmt, i++, mail->flags_recent_field);
8805         _bind_stmt_field_data_char(hStmt, i++, mail->flags_draft_field);
8806         _bind_stmt_field_data_char(hStmt, i++, mail->flags_forwarded_field);
8807         _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
8808         _bind_stmt_field_data_int(hStmt, i++, mail->priority);
8809         _bind_stmt_field_data_int(hStmt, i++, mail->save_status);
8810         _bind_stmt_field_data_int(hStmt, i++, mail->lock_status);
8811         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->message_id, 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
8812         _bind_stmt_field_data_int(hStmt, i++, mail->report_status);
8813         _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8814         _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
8815         _bind_stmt_field_data_int(hStmt, i++, mail->scheduled_sending_time);
8816         _bind_stmt_field_data_int(hStmt, i++, mail->remaining_resend_times);
8817         _bind_stmt_field_data_int(hStmt, i++, mail->tag_id);
8818         _bind_stmt_field_data_int(hStmt, i++, mail->replied_time);
8819         _bind_stmt_field_data_int(hStmt, i++, mail->forwarded_time);
8820         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
8821         _bind_stmt_field_data_int(hStmt, i++, mail->eas_data_length);
8822         _bind_stmt_field_data_blob(hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
8823
8824         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8825         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8826                 ("sqlite3_step fail:%d", rc));
8827
8828         rc = sqlite3_changes(local_db_handle);
8829         if (rc == 0) {
8830                 EM_DEBUG_LOG(" no matched mail found...");
8831                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8832                 goto FINISH_OFF;
8833         }
8834
8835         ret = true;
8836
8837 FINISH_OFF:
8838         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
8839         if (hStmt != NULL) {
8840                 rc = sqlite3_finalize(hStmt);
8841                 if (rc != SQLITE_OK) {
8842                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8843                         error = EMAIL_ERROR_DB_FAILURE;
8844                 }
8845         }
8846
8847
8848         if (error == EMAIL_ERROR_NONE && mail) {
8849                 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
8850                 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail->mail_id, mailbox_id_param_string, 0))
8851                         EM_DEBUG_EXCEPTION("emcore_notify_storage_eventfailed [NOTI_MAIL_UPDATE]");
8852         }
8853
8854         if (err_code != NULL)
8855                 *err_code = error;
8856
8857         EM_DEBUG_FUNC_END("ret [%d]", ret);
8858         return ret;
8859 }
8860
8861 /**
8862   *  emstorage_clean_save_status(int save_status, int  *err_code) - set the all mail status to the set value
8863   *
8864   *
8865   **/
8866 INTERNAL_FUNC int emstorage_clean_save_status(char *multi_user_name, int save_status, int  *err_code)
8867 {
8868         EM_DEBUG_FUNC_BEGIN("save_status[%d], err_code[%p]", save_status, err_code);
8869
8870         EM_IF_NULL_RETURN_VALUE(err_code, false);
8871
8872         int ret = false;
8873         int error = EMAIL_ERROR_NONE;
8874         int rc = 0;
8875         char sql_query_string[QUERY_SIZE] = {0, };
8876         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8877
8878         memset(sql_query_string, 0x00, sizeof(sql_query_string));
8879         SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET save_status = %d WHERE save_status = %d", save_status, EMAIL_MAIL_STATUS_SENDING);
8880
8881         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
8882         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
8883         if (error != EMAIL_ERROR_NONE) {
8884                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
8885                         goto FINISH_OFF;
8886         }
8887
8888         rc = sqlite3_changes(local_db_handle);
8889         if (rc == 0) {
8890                 EM_DEBUG_LOG(" No Matched Mail Exists ");
8891                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8892         }
8893
8894         ret = true;
8895
8896 FINISH_OFF:
8897
8898         if (err_code != NULL)
8899                 *err_code = error;
8900
8901         EM_DEBUG_FUNC_END("ret [%d]", ret);
8902         return ret;
8903 }
8904
8905 INTERNAL_FUNC int emstorage_set_field_of_mails_with_integer_value(char *multi_user_name, int account_id, int mail_ids[], int mail_ids_count, char *field_name, int value, int transaction, int *err_code)
8906 {
8907         EM_DEBUG_FUNC_BEGIN_SEC("account_id [%d], mail_ids[%p], mail_ids_count[%d], field_name[%s], value[%d], transaction[%d], err_code[%p]", account_id, mail_ids, mail_ids_count, field_name, value, transaction, err_code);
8908         int i = 0;
8909         int error = EMAIL_ERROR_NONE;
8910         int ret = false;
8911         int query_size = 0;
8912         int cur_mail_id_string = 0;
8913         int mail_id_string_buffer_length = 0;
8914         int parameter_string_length = 0;
8915         char  *sql_query_string = NULL;
8916         char *mail_id_string_buffer = NULL;
8917         char *parameter_string = NULL;
8918         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8919         email_mail_attribute_type target_mail_attribute_type = 0;
8920
8921         if (!mail_ids  || !field_name || account_id == 0) {
8922                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8923                 if (err_code != NULL)
8924                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8925                 return false;
8926         }
8927
8928         if ((error = emcore_get_attribute_type_by_mail_field_name(field_name, &target_mail_attribute_type)) != EMAIL_ERROR_NONE) {
8929                 EM_DEBUG_EXCEPTION("emstorageemcore_get_attribute_type_by_mail_field_name failed [%d]", error);
8930                 if (err_code != NULL)
8931                         *err_code = error;
8932                 return false;
8933         }
8934
8935         /* Generating mail id list string */
8936         mail_id_string_buffer_length = MAIL_ID_STRING_LENGTH * mail_ids_count;
8937
8938         mail_id_string_buffer = em_malloc(mail_id_string_buffer_length);
8939
8940         if (!mail_id_string_buffer) {
8941                 EM_DEBUG_EXCEPTION("em_mallocfailed");
8942                 if (err_code != NULL)
8943                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8944                 return false;
8945         }
8946
8947         for (i = 0; i < mail_ids_count; i++)
8948                 cur_mail_id_string += SNPRINTF_OFFSET(mail_id_string_buffer, cur_mail_id_string, mail_id_string_buffer_length, "%d,", mail_ids[i]);
8949
8950         if (EM_SAFE_STRLEN(mail_id_string_buffer) > 1)
8951                 mail_id_string_buffer[EM_SAFE_STRLEN(mail_id_string_buffer) - 1] = NULL_CHAR;
8952
8953         /* Generating notification parameter string */
8954         parameter_string_length = mail_id_string_buffer_length + EM_SAFE_STRLEN(field_name) + 2;
8955         parameter_string = em_malloc(parameter_string_length);
8956
8957         if (!parameter_string) {
8958                 EM_DEBUG_EXCEPTION("em_mallocfailed");
8959                 if (err_code != NULL)
8960                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8961                 EM_SAFE_FREE(mail_id_string_buffer);
8962                 return false;
8963         }
8964
8965         SNPRINTF(parameter_string, parameter_string_length, "%s%c%s", field_name, 0x01, mail_id_string_buffer);
8966         query_size = EM_SAFE_STRLEN(mail_id_string_buffer) + EM_SAFE_STRLEN(field_name) + 250;
8967
8968         sql_query_string = em_malloc(query_size);
8969         if (sql_query_string == NULL) {
8970                 EM_DEBUG_EXCEPTION("em_mallocfailed");
8971                 error = EMAIL_ERROR_OUT_OF_MEMORY;
8972                 goto FINISH_OFF;
8973         }
8974         /* Write query string */
8975         SNPRINTF(sql_query_string, query_size, "UPDATE mail_tbl SET %s = %d WHERE mail_id in (%s) AND account_id = %d", field_name, value, mail_id_string_buffer, account_id);
8976
8977         EM_DEBUG_LOG_DEV("sql_query_string [%s]", sql_query_string);
8978
8979         /* Execute query */
8980         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
8981         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
8982         if (error != EMAIL_ERROR_NONE) {
8983                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
8984                         goto FINISH_OFF;
8985         }
8986
8987         if (sqlite3_changes(local_db_handle) == 0)
8988                 EM_DEBUG_LOG("no mail matched...");
8989
8990         ret = true;
8991
8992 FINISH_OFF:
8993         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
8994
8995         if (error == EMAIL_ERROR_NONE && parameter_string) {
8996                 if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, target_mail_attribute_type, parameter_string, value))
8997                         EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_MAIL_FIELD_UPDATE [%s,%d]", field_name, value);
8998         }
8999
9000         EM_SAFE_FREE(mail_id_string_buffer);
9001         EM_SAFE_FREE(parameter_string);
9002         EM_SAFE_FREE(sql_query_string);
9003
9004
9005         if (err_code != NULL)
9006                 *err_code = error;
9007
9008         EM_DEBUG_FUNC_END("error [%d]", error);
9009         return ret;
9010 }
9011
9012 #ifdef __FEATURE_BODY_SEARCH__
9013 INTERNAL_FUNC int emstorage_change_mail_text_field(char *multi_user_name, int mail_id, emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
9014 {
9015         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
9016
9017         if (mail_id <= 0 || !mail_text) {
9018                 EM_DEBUG_EXCEPTION(" mail_id[%d], mail_text[%p]", mail_id, mail_text);
9019                 if (err_code != NULL)
9020                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9021                 return false;
9022         }
9023
9024         int ret = false;
9025         int error = EMAIL_ERROR_NONE;
9026         DB_STMT hStmt = NULL;
9027         char sql_query_string[QUERY_SIZE] = {0, };
9028
9029         int i = 0;
9030         int rc = 0;
9031
9032         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9033         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9034
9035         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9036                 "UPDATE mail_text_tbl SET"
9037                 " body_text = ?"
9038                 " WHERE mail_id = %d AND account_id != 0"
9039                 , mail_id);
9040         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9041
9042         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9043         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9044                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9045
9046         i = 0;
9047         _bind_stmt_field_data_string(hStmt, i++, (char *)mail_text->body_text, 0, -1);
9048
9049         if (hStmt != NULL) {
9050                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9051                 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9052                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9053
9054                 rc = sqlite3_changes(local_db_handle);
9055                 if (rc == 0) {
9056                         EM_DEBUG_LOG(" no matched mail found...");
9057                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
9058                         goto FINISH_OFF;
9059                 }
9060         }
9061
9062         ret = true;
9063
9064 FINISH_OFF:
9065         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9066
9067         if (hStmt != NULL) {
9068                 rc = sqlite3_finalize(hStmt);
9069                 if (rc != SQLITE_OK) {
9070                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9071                         error = EMAIL_ERROR_DB_FAILURE;
9072                 }
9073                 hStmt = NULL;
9074         }
9075
9076         if (err_code != NULL)
9077                 *err_code = error;
9078
9079         EM_DEBUG_FUNC_END("ret [%d]", ret);
9080         return ret;
9081 }
9082 #endif
9083
9084 INTERNAL_FUNC int emstorage_change_mail_field(char *multi_user_name, int mail_id, email_mail_change_type_t type, emstorage_mail_tbl_t *mail, int transaction, int *err_code)
9085 {
9086         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
9087
9088         int ret = false;
9089         int error = EMAIL_ERROR_NONE;
9090         int move_flag = 0;
9091         int rc = 0;
9092         int i = 0;
9093         int mailbox_id = 0;
9094         DB_STMT hStmt = NULL;
9095         char sql_query_string[QUERY_SIZE] = {0, };
9096         char mailbox_id_param_string[10] = {0,};
9097         sqlite3 *local_db_handle = NULL;
9098
9099         if (mail_id <= 0 || !mail) {
9100                 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], mail[%p]", mail_id, type, mail);
9101                 if (err_code != NULL)
9102                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9103                 return false;
9104         }
9105
9106         local_db_handle = emstorage_get_db_connection(multi_user_name);
9107
9108         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9109
9110         switch (type) {
9111                 case APPEND_BODY:
9112                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9113                                 "UPDATE mail_tbl SET"
9114                                 "  body_download_status = ?"
9115                                 ", file_path_plain = ?"
9116                                 ", file_path_html = ?"
9117                                 ", file_path_mime_entity = ?"
9118                                 ", flags_seen_field      = ?"
9119                                 ", flags_deleted_field   = ?"
9120                                 ", flags_flagged_field   = ?"
9121                                 ", flags_answered_field  = ?"
9122                                 ", flags_recent_field    = ?"
9123                                 ", flags_draft_field     = ?"
9124                                 ", flags_forwarded_field = ?"
9125                                 ", DRM_status = ?"
9126                                 ", attachment_count = ?"
9127                                 ", preview_text= ?"
9128                                 ", meeting_request_status = ? "
9129                                 ", message_class = ? "
9130                                 ", digest_type = ? "
9131                                 ", smime_type = ? "
9132                                 " WHERE mail_id = %d AND account_id != 0"
9133                                 , mail_id);
9134
9135
9136                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9137                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9138                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9139                          i = 0;
9140
9141                         _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9142                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9143                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9144                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9145                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_seen_field);
9146                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_deleted_field);
9147                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_flagged_field);
9148                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_answered_field);
9149                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_recent_field);
9150                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_draft_field);
9151                         _bind_stmt_field_data_int(hStmt, i++, mail->flags_forwarded_field);
9152                         _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
9153                         _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9154                         _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9155                         _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
9156                         _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
9157                         _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9158                         _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9159                         break;
9160
9161                 case UPDATE_MAILBOX: {
9162                                 int err;
9163                                 emstorage_mailbox_tbl_t *mailbox_tbl;
9164
9165                                 if ((err = emstorage_get_mailbox_by_id(multi_user_name, mail->mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
9166                                         EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_id failed [%d]", err);
9167                                         goto FINISH_OFF;
9168                                 }
9169
9170                                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9171                                         "UPDATE mail_tbl SET"
9172                                         " mailbox_id = '%d'"
9173                                         ",mailbox_type = '%d'"
9174                                         " WHERE mail_id = %d AND account_id != 0"
9175                                         , mailbox_tbl->mailbox_id
9176                                         , mailbox_tbl->mailbox_type
9177                                         , mail_id);
9178                                         move_flag = 1;
9179
9180
9181                                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9182                                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9183                                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9184
9185                                 i = 0;
9186                                 _bind_stmt_field_data_string(hStmt, i++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
9187
9188                                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL); /*prevent 26251*/
9189
9190                         }
9191                         break;
9192
9193                 case UPDATE_FLAG:
9194                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9195                                 "UPDATE mail_tbl SET"
9196                                 " flags_seen_field      = %d"
9197                                 ",flags_deleted_field   = %d"
9198                                 ",flags_flagged_field   = %d"
9199                                 ",flags_answered_field  = %d"
9200                                 ",flags_recent_field    = %d"
9201                                 ",flags_draft_field     = %d"
9202                                 ",flags_forwarded_field = %d"
9203                                 "  WHERE mail_id = %d AND account_id != 0"
9204                                 , mail->flags_seen_field
9205                                 , mail->flags_deleted_field
9206                                 , mail->flags_flagged_field
9207                                 , mail->flags_answered_field
9208                                 , mail->flags_recent_field
9209                                 , mail->flags_draft_field
9210                                 , mail->flags_forwarded_field
9211                                 , mail_id);
9212                         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9213
9214
9215                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9216                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9217                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9218
9219                         break;
9220
9221                 case UPDATE_EXTRA_FLAG:
9222                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9223                                 "UPDATE mail_tbl SET"
9224                                 "  priority = %d"
9225                                 ", save_status = %d"
9226                                 ", lock_status = %d"
9227                                 ", report_status = %d"
9228                                 ", DRM_status = %d"
9229                                 " WHERE mail_id = %d AND account_id != 0"
9230                                 , mail->priority
9231                                 , mail->save_status
9232                                 , mail->lock_status
9233                                 , mail->report_status
9234                                 , mail->DRM_status
9235                                 , mail_id);
9236                         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9237
9238
9239                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9240                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9241                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9242                         break;
9243
9244                 case UPDATE_STICKY_EXTRA_FLAG:
9245                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9246                                 "UPDATE mail_tbl SET"
9247                                 "  lock_status = %d"
9248                                 "  WHERE mail_id = %d AND account_id != 0"
9249                                 , mail->lock_status
9250                                 , mail_id);
9251                         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9252
9253
9254                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9255                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9256                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9257                         break;
9258
9259                 case UPDATE_MAIL:
9260                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9261                                 "UPDATE mail_tbl SET"
9262                                 "  full_address_from = ?"
9263                                 ", full_address_reply = ?"
9264                                 ", full_address_to = ?"
9265                                 ", full_address_cc = ?"
9266                                 ", full_address_bcc = ?"
9267                                 ", full_address_return = ?"
9268                                 ", subject = ?"
9269                                 ", file_path_plain = ?"
9270                                 ", date_time = ?"
9271                                 ", flags_seen_field = ?"
9272                                 ", flags_deleted_field = ?"
9273                                 ", flags_flagged_field = ?"
9274                                 ", flags_answered_field = ?"
9275                                 ", flags_recent_field = ?"
9276                                 ", flags_draft_field = ?"
9277                                 ", flags_forwarded_field = ?"
9278                                 ", priority = ?"
9279                                 ", save_status = ?"
9280                                 ", lock_status = ?"
9281                                 ", report_status = ?"
9282                                 ", DRM_status = ?"
9283                                 ", file_path_html = ?"
9284                                 ", file_path_mime_entity = ?"
9285                                 ", mail_size = ?"
9286                                 ", preview_text = ?"
9287                                 ", body_download_status = ?"
9288                                 ", attachment_count = ?"
9289                                 ", inline_content_count = ?"
9290                                 ", meeting_request_status = ?"
9291                                 ", message_class = ?"
9292                                 ", digest_type = ?"
9293                                 ", smime_type = ?"
9294                                 ", scheduled_sending_time = ?"
9295                                 ", remaining_resend_times = ?"
9296                                 ", tag_id = ?"
9297                                 ", replied_time = ?"
9298                                 ", forwarded_time = ?"
9299                                 ", default_charset = ?"
9300                                 ", eas_data_length = ?"
9301                                 ", eas_data = ?"
9302                                 " WHERE mail_id = %d AND account_id != 0"
9303                                 , mail_id);
9304
9305
9306                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9307                         EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
9308                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9309                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9310                         i = 0;
9311                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
9312                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
9313                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
9314                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
9315                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
9316                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
9317                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9318                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9319                         _bind_stmt_field_data_time_t(hStmt, i++, mail->date_time);
9320                         _bind_stmt_field_data_char(hStmt, i++, mail->flags_seen_field);
9321                         _bind_stmt_field_data_char(hStmt, i++, mail->flags_deleted_field);
9322                         _bind_stmt_field_data_char(hStmt, i++, mail->flags_flagged_field);
9323                         _bind_stmt_field_data_char(hStmt, i++, mail->flags_answered_field);
9324                         _bind_stmt_field_data_char(hStmt, i++, mail->flags_recent_field);
9325                         _bind_stmt_field_data_char(hStmt, i++, mail->flags_draft_field);
9326                         _bind_stmt_field_data_char(hStmt, i++, mail->flags_forwarded_field);
9327                         _bind_stmt_field_data_int(hStmt, i++, mail->priority);
9328                         _bind_stmt_field_data_int(hStmt, i++, mail->save_status);
9329                         _bind_stmt_field_data_int(hStmt, i++, mail->lock_status);
9330                         _bind_stmt_field_data_int(hStmt, i++, mail->report_status);
9331                         _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
9332                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9333                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9334                         _bind_stmt_field_data_int(hStmt, i++, mail->mail_size);
9335                         _bind_stmt_field_data_nstring(hStmt, i++, (char*)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9336                         _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9337                         _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9338                         _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
9339                         _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
9340                         _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
9341                         _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9342                         _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9343                         _bind_stmt_field_data_int(hStmt, i++, mail->scheduled_sending_time);
9344                         _bind_stmt_field_data_int(hStmt, i++, mail->remaining_resend_times);
9345                         _bind_stmt_field_data_int(hStmt, i++, mail->tag_id);
9346                         _bind_stmt_field_data_int(hStmt, i++, mail->replied_time);
9347                         _bind_stmt_field_data_int(hStmt, i++, mail->forwarded_time);
9348                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
9349                         _bind_stmt_field_data_int(hStmt, i++, mail->eas_data_length);
9350                         _bind_stmt_field_data_blob(hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
9351                         break;
9352
9353                 case UPDATE_DATETIME: {
9354                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9355                                 "UPDATE mail_tbl SET"
9356                                 " date_time = '%ld'"
9357                                 " WHERE mail_id = %d AND account_id != 0"
9358                                 , mail->date_time
9359                                 , mail_id);
9360
9361                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9362                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9363                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9364                         break;
9365                 }
9366
9367                 case UPDATE_FROM_CONTACT_INFO:
9368                         EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_FROM_CONTACT_INFO");
9369                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9370                                 "UPDATE mail_tbl SET"
9371                                 " email_address_sender = ?,"
9372                                 " WHERE mail_id = %d",
9373                                 mail_id);
9374
9375                         hStmt = NULL;
9376
9377                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9378                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9379                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9380                         i = 0;
9381                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9382                         break;
9383
9384                 case UPDATE_TO_CONTACT_INFO:
9385                         EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_TO_CONTACT_INFO");
9386                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9387                                 "UPDATE mail_tbl SET"
9388                                 " email_address_recipient = ?,"
9389                                 " WHERE mail_id = %d",
9390                                 mail_id);
9391
9392                         hStmt = NULL;
9393
9394                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9395                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9396                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9397                         i = 0;
9398                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9399                         break;
9400
9401                         case UPDATE_ALL_CONTACT_INFO:
9402                                 EM_DEBUG_LOG("emstorage_change_mail_field - mail change type is UPDATE_ALL_CONTACT_INFO");
9403                                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9404                                         "UPDATE mail_tbl SET"
9405                                         " email_address_sender = ?,"
9406                                         " email_address_recipient = ?,"
9407                                         " WHERE mail_id = %d",
9408                                         mail_id);
9409
9410                                 hStmt = NULL;
9411
9412                                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9413                                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9414                                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9415                                 i = 0;
9416                                 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9417                                 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9418                                 break;
9419
9420
9421 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
9422                         case UPDATE_PARTIAL_BODY_DOWNLOAD:
9423
9424                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9425                         "UPDATE mail_tbl SET"
9426                         "  body_download_status = ?"
9427                         ", file_path_plain = ?"
9428                         ", file_path_html = ?"
9429                         ", file_path_mime_entity = ?"
9430                         ", attachment_count = ?"
9431                         ", inline_content_count = ?"
9432                         ", preview_text = ?"
9433                         ", digest_type = ?"
9434                         ", smime_type = ?"
9435                         " WHERE mail_id = %d"
9436                         , mail_id);
9437
9438
9439                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9440                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9441                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9442                          i = 0;
9443
9444                         _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9445                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9446                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html,  0, TEXT_2_LEN_IN_MAIL_TBL);
9447                         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9448                         _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9449                         _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
9450                         _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text,    0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9451                         _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9452                         _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9453
9454                         break;
9455
9456 #endif
9457                 case UPDATE_FILE_PATH:
9458                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9459                                 "UPDATE mail_tbl SET"
9460                                 ", file_path_plain = ?"
9461                                 ", file_path_html = ?"
9462                                 ", file_path_mime_entity = ?"
9463                                 " WHERE mail_id = %d"
9464                                 , mail_id);
9465
9466
9467                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9468                         EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
9469                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9470                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9471                         i = 0;
9472                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9473                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9474                         _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9475                         break;
9476
9477                 default:
9478                         EM_DEBUG_LOG(" type[%d]", type);
9479
9480                         error = EMAIL_ERROR_INVALID_PARAM;
9481                         goto FINISH_OFF;
9482         }
9483
9484         if (hStmt != NULL) {
9485
9486         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9487                 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
9488                 ("sqlite3_step fail:%d", rc));
9489                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9490                         ("sqlite3_step fail:%d", rc));
9491                 rc = sqlite3_changes(local_db_handle);
9492                 if (rc == 0) {
9493                         EM_DEBUG_LOG(" no matched mail found...");
9494                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
9495                         goto FINISH_OFF;
9496                 }
9497         }
9498
9499         if (mail->account_id == 0) {
9500                 emstorage_mail_tbl_t* mail_for_account_tbl = NULL;
9501                 if (!emstorage_get_mail_field_by_id(multi_user_name, mail_id, RETRIEVE_ACCOUNT, &mail_for_account_tbl, true, &error) || !mail_for_account_tbl) {
9502                         EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id error [%d]", error);
9503                         goto FINISH_OFF;
9504                 }
9505                 mail->account_id = mail_for_account_tbl->account_id;
9506                 if (mail_for_account_tbl)
9507                         emstorage_free_mail(&mail_for_account_tbl, 1, NULL);
9508         }
9509
9510         ret = true;
9511
9512 FINISH_OFF:
9513         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9514
9515         if (hStmt != NULL) {
9516                 rc = sqlite3_finalize(hStmt);
9517                 if (rc != SQLITE_OK) {
9518                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9519                         error = EMAIL_ERROR_DB_FAILURE;
9520                 }
9521                 hStmt = NULL;
9522         }
9523
9524         if (error == EMAIL_ERROR_NONE &&  move_flag != 1 && transaction) {
9525                 if (!emstorage_get_mailbox_id_by_mailbox_type(multi_user_name, mail->account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &mailbox_id, false, &error))
9526                         EM_DEBUG_EXCEPTION("emstorage_get_mailbox_id_by_mailbox_type error [%d]", error);
9527
9528                 if (mail->mailbox_id == mailbox_id) {
9529                         SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
9530                         if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, mailbox_id_param_string, type))
9531                                 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventerror [ NOTI_MAIL_UPDATE ] >>>> ");
9532                 } else {
9533                         /* h.gahlaut@samsung.com: Jan 10, 2011 Publishing noti to refresh outbox when email sending status changes */
9534                         if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, NULL, type))
9535                                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventerror [ NOTI_MAIL_UPDATE ]");
9536                 }
9537         }
9538
9539         if (err_code != NULL)
9540                 *err_code = error;
9541
9542         EM_DEBUG_FUNC_END("ret [%d]", ret);
9543         return ret;
9544 }
9545
9546 INTERNAL_FUNC int emstorage_increase_mail_id(char *multi_user_name, int *mail_id, int transaction, int *err_code)
9547 {
9548         EM_DEBUG_FUNC_BEGIN("mail_id[%p], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9549
9550         int rc, ret = false;
9551         int error = EMAIL_ERROR_NONE;
9552         int latest_mail_id = 0;
9553         sqlite3 *local_db_handle = NULL;
9554         char *sql = "SELECT MAX(mail_id) FROM mail_tbl;";
9555         char **result = NULL;
9556
9557 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9558         _timedlock_shm_mutex(mapped_for_generating_mail_id, 2);
9559 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9560
9561         ret = vconf_get_int(VCONF_KEY_LATEST_MAIL_ID, &latest_mail_id);
9562         if (ret < 0 || latest_mail_id == 0) {
9563                 EM_DEBUG_LOG("vconf_get_int() failed [%d] or latest_mail_id is zero", ret);
9564
9565         local_db_handle = emstorage_get_db_connection(multi_user_name);
9566
9567                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9568                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
9569                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9570                 if (NULL == result[1])
9571                         rc = 1;
9572                 else
9573                         rc = atoi(result[1]) + 1;
9574
9575                 sqlite3_free_table(result);
9576                 latest_mail_id = rc;
9577         }
9578
9579         latest_mail_id++;
9580
9581         ret = vconf_set_int(VCONF_KEY_LATEST_MAIL_ID, latest_mail_id);
9582
9583         if (mail_id)
9584                 *mail_id = latest_mail_id;
9585
9586 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9587         _unlockshm_mutex(mapped_for_generating_mail_id);
9588 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9589
9590         ret = true;
9591
9592 FINISH_OFF:
9593
9594         if (err_code != NULL)
9595                 *err_code = error;
9596
9597         EM_DEBUG_FUNC_END("ret [%d]", ret);
9598         return ret;
9599 }
9600
9601 INTERNAL_FUNC int emstorage_add_mail(char *multi_user_name, emstorage_mail_tbl_t *mail_tbl_data, int get_id, int transaction, int *err_code)
9602 {
9603         EM_PROFILE_BEGIN(profile_emstorage_add_mail);
9604         EM_DEBUG_FUNC_BEGIN("mail_tbl_data[%p], get_id[%d], transaction[%d], err_code[%p]", mail_tbl_data, get_id, transaction, err_code);
9605
9606         if (!mail_tbl_data) {
9607                 EM_DEBUG_EXCEPTION("mail_tbl_data[%p], get_id[%d]", mail_tbl_data, get_id);
9608                 if (err_code != NULL)
9609                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9610                 return false;
9611         }
9612
9613         int rc, ret = false;
9614         int error = EMAIL_ERROR_NONE;
9615         char sql_query_string[QUERY_SIZE] = {0, };
9616         DB_STMT hStmt = NULL;
9617
9618         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9619
9620         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9621
9622         if (get_id) {
9623                 /*  increase unique id */
9624                 char *sql = "SELECT max(rowid) FROM mail_tbl;";
9625                 char **result;
9626
9627                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9628 /*              EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
9629                         ("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle))); */
9630                 if (rc != SQLITE_OK) {
9631                         EM_DEBUG_EXCEPTION("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle));
9632                         error = EMAIL_ERROR_DB_FAILURE;
9633                         sqlite3_free_table(result);
9634                         goto FINISH_OFF;
9635                 }
9636
9637                 if (NULL == result[1])
9638                         rc = 1;
9639                 else
9640                         rc = atoi(result[1])+1;
9641
9642                 sqlite3_free_table(result);
9643
9644                 mail_tbl_data->mail_id   = rc;
9645                 mail_tbl_data->thread_id = rc;
9646         }
9647
9648         if (mail_tbl_data->date_time == 0)
9649                 mail_tbl_data->date_time = time(NULL);
9650
9651         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9652                 "INSERT INTO mail_tbl VALUES "
9653                 "(?" /*  mail_id */
9654                 ", ?" /*  account_id */
9655                 ", ?" /*  mailbox_id */
9656                 ", ?" /*  mailbox_type */
9657                 ", ?" /*  subject */
9658
9659                 ", ?" /*  date_time */
9660                 ", ?" /*  server_mail_status */
9661                 ", ?" /*  server_mailbox_name */
9662                 ", ?" /*  server_mail_id */
9663                 ", ?" /*  message_id */
9664
9665                 ", ?" /*  reference_mail_id */
9666                 ", ?" /*  full_address_from */
9667                 ", ?" /*  full_address_reply */
9668                 ", ?" /*  full_address_to */
9669                 ", ?" /*  full_address_cc */
9670
9671                 ", ?" /*  full_address_bcc */
9672                 ", ?" /*  full_address_return */
9673                 ", ?" /*  email_address_sender */
9674                 ", ?" /*  email_address_recipient */
9675                 ", ?" /*  alias_sender */
9676
9677                 ", ?" /*  alias_recipient */
9678                 ", ?" /*  body_download_status */
9679                 ", ?" /*  file_path_plain */
9680                 ", ?" /*  file_path_html */
9681                 ", ?" /*  file_path_mime_entity */
9682
9683                 ", ?" /*  mail_size */
9684                 ", ?" /*  flags_seen_field */
9685                 ", ?" /*  flags_deleted_field */
9686                 ", ?" /*  flags_flagged_field */
9687                 ", ?" /*  flags_answered_field */
9688
9689                 ", ?" /*  flags_recent_field */
9690                 ", ?" /*  flags_draft_field */
9691                 ", ?" /*  flags_forwarded_field */
9692                 ", ?" /*  DRM_status */
9693                 ", ?" /*  priority */
9694
9695                 ", ?" /*  save_status */
9696                 ", ?" /*  lock_status */
9697                 ", ?" /*  report_status */
9698                 ", ?" /*  attachment_count */
9699                 ", ?" /*  inline_content_count */
9700
9701                 ", ?" /*  thread_id */
9702                 ", ?" /*  thread_item_count */
9703                 ", ?" /*  preview_text */
9704                 ", ?" /*  meeting_request_status */
9705                 ", ?" /*  message_class */
9706
9707                 ", ?" /*  digest_type */
9708                 ", ?" /*  smime_type */
9709                 ", ?" /*  scheduled_sending_time */
9710                 ", ?" /*  remaining_resend_times */
9711                 ", ?" /*  tag_id */
9712
9713                 ", ?" /*  replied_time */
9714                 ", ?" /*  forwarded_time */
9715                 ", ?" /*  default charset */
9716                 ", ?" /*  eas_data_length */
9717                 ", ?" /*  eas_data */
9718                 ", ?" /*  user_name */
9719                 ")");
9720
9721         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle,
9722                                                    sql_query_string,
9723                                                    EM_SAFE_STRLEN(sql_query_string),
9724                                                    &hStmt,
9725                                                    NULL),
9726                                     rc);
9727         if (rc != SQLITE_OK) {
9728                 EM_DEBUG_EXCEPTION("sqlite3_prepare error [%d] [%s] SQL(%s) ",
9729                              rc, sql_query_string, sqlite3_errmsg(local_db_handle));
9730                 error = EMAIL_ERROR_DB_FAILURE;
9731                 goto FINISH_OFF;
9732         }
9733
9734         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mail_id);
9735         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, mail_tbl_data->account_id);
9736         _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_id);
9737         _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_type);
9738         _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9739
9740         _bind_stmt_field_data_int(hStmt, DATETIME_IDX_IN_MAIL_TBL, mail_tbl_data->date_time);
9741         _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->server_mail_status);
9742         _bind_stmt_field_data_string(hStmt, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->server_mailbox_name, 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
9743         _bind_stmt_field_data_string(hStmt, SERVER_MAIL_ID_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->server_mail_id, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
9744         _bind_stmt_field_data_string(hStmt, MESSAGE_ID_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->message_id, 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
9745
9746         _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, mail_tbl_data->reference_mail_id);
9747         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
9748         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
9749         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
9750         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
9751
9752         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
9753         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
9754         _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9755         _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9756         _bind_stmt_field_data_string(hStmt, ALIAS_SENDER_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->alias_sender, 1, FROM_CONTACT_NAME_LEN_IN_MAIL_TBL);
9757
9758         _bind_stmt_field_data_string(hStmt, ALIAS_RECIPIENT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->alias_recipient, 1, FROM_CONTACT_NAME_LEN_IN_MAIL_TBL);
9759         _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->body_download_status);
9760         _bind_stmt_field_data_string(hStmt, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9761         _bind_stmt_field_data_string(hStmt, FILE_PATH_HTML_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9762         _bind_stmt_field_data_string(hStmt, FILE_PATH_MIME_ENTITY_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9763
9764         _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, mail_tbl_data->mail_size);
9765         _bind_stmt_field_data_int(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_seen_field);
9766         _bind_stmt_field_data_int(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_deleted_field);
9767         _bind_stmt_field_data_int(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_flagged_field);
9768         _bind_stmt_field_data_int(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_answered_field);
9769
9770         _bind_stmt_field_data_int(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_recent_field);
9771         _bind_stmt_field_data_int(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_draft_field);
9772         _bind_stmt_field_data_int(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_forwarded_field);
9773         _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->DRM_status);
9774         _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, mail_tbl_data->priority);
9775
9776         _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->save_status);
9777         _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->lock_status);
9778         _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->report_status);
9779         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->attachment_count);
9780         _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->inline_content_count);
9781
9782         _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, mail_tbl_data->thread_id);
9783         _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->thread_item_count);
9784         _bind_stmt_field_data_nstring(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9785         _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->meeting_request_status);
9786         _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, mail_tbl_data->message_class);
9787
9788         _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->digest_type);
9789         _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->smime_type);
9790         _bind_stmt_field_data_int(hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->scheduled_sending_time);
9791         _bind_stmt_field_data_int(hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, mail_tbl_data->remaining_resend_times);
9792         _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, mail_tbl_data->tag_id);
9793
9794         _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->replied_time);
9795         _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->forwarded_time);
9796         _bind_stmt_field_data_string(hStmt, DEFAULT_CHARSET_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
9797         _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, mail_tbl_data->eas_data_length);
9798         _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, (void*)mail_tbl_data->eas_data, mail_tbl_data->eas_data_length);
9799         _bind_stmt_field_data_string(hStmt, USER_NAME_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->user_name, 0, TEXT_2_LEN_IN_MAIL_TBL);
9800
9801         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9802         if (rc == SQLITE_FULL) {
9803                 EM_DEBUG_EXCEPTION("sqlite3_step error [%d]", rc);
9804                 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
9805                 goto FINISH_OFF;
9806         }
9807         if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
9808                 EM_DEBUG_EXCEPTION("sqlite3_step error [%d]", rc);
9809                 error = EMAIL_ERROR_DB_FAILURE;
9810                 goto FINISH_OFF;
9811         }
9812         ret = true;
9813
9814 FINISH_OFF:
9815         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9816
9817         if (hStmt != NULL) {
9818                 rc = sqlite3_finalize(hStmt);
9819                 if (rc != SQLITE_OK) {
9820                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9821                         error = EMAIL_ERROR_DB_FAILURE;
9822                 }
9823         }
9824
9825         if (err_code != NULL)
9826                 *err_code = error;
9827
9828         EM_PROFILE_END(profile_emstorage_add_mail);
9829         EM_DEBUG_FUNC_END("ret [%d]", ret);
9830         return ret;
9831 }
9832
9833 INTERNAL_FUNC int emstorage_move_multiple_mails_on_db(char *multi_user_name, int input_source_account_id, int input_mailbox_id, int mail_ids[], int number_of_mails, int transaction, int *err_code)
9834 {
9835         EM_DEBUG_FUNC_BEGIN("input_source_account_id [%d], input_mailbox_id [%d], mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", input_source_account_id, input_mailbox_id, mail_ids, number_of_mails, transaction, err_code);
9836
9837         int ret = false, i, cur_conditional_clause = 0;
9838         int error = EMAIL_ERROR_NONE;
9839         int target_account_id;
9840         int conditional_clause_len = 0;
9841         char *sql_query_string = NULL, *conditional_clause = NULL;
9842         emstorage_mailbox_tbl_t *result_mailbox = NULL;
9843         email_mailbox_type_e target_mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED;
9844         char* target_mailbox_name = NULL;
9845
9846         if (!mail_ids || input_mailbox_id <= 0) {
9847                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9848                 if (err_code != NULL)
9849                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9850                 return false;
9851         }
9852
9853         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9854
9855         if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &result_mailbox)) != EMAIL_ERROR_NONE || !result_mailbox) {
9856                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", error);
9857                 if (err_code != NULL)
9858                         *err_code = error;
9859                 return false;
9860         }
9861
9862         if (result_mailbox->mailbox_name) {
9863                 if (strstr(result_mailbox->mailbox_name, "'")) {
9864                         target_mailbox_name = em_replace_all_string(result_mailbox->mailbox_name, "'", "''");
9865                 } else {
9866                         target_mailbox_name = strdup(result_mailbox->mailbox_name);
9867                 }
9868         }
9869
9870         target_mailbox_type = result_mailbox->mailbox_type;
9871         target_account_id   = result_mailbox->account_id;
9872         emstorage_free_mailbox(&result_mailbox, 1, NULL);
9873
9874         conditional_clause_len =  (sizeof(char) * 8 * number_of_mails) + 512;
9875         conditional_clause = em_malloc(conditional_clause_len);
9876         if (conditional_clause == NULL) {
9877                 EM_DEBUG_EXCEPTION("em_mallocfailed");
9878                 error = EMAIL_ERROR_OUT_OF_MEMORY;
9879                 goto FINISH_OFF;
9880         }
9881         cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE mail_id in (");
9882
9883         for (i = 0; i < number_of_mails; i++)
9884                 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
9885
9886         /* prevent 34415 */
9887         char *last_comma = rindex(conditional_clause, ',');
9888         if (last_comma) *last_comma = ')'; /* replace , with) */
9889
9890         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9891
9892         /* Updating a mail_tbl */
9893
9894         sql_query_string = em_malloc(conditional_clause_len);
9895         if (sql_query_string == NULL) {
9896                 EM_DEBUG_EXCEPTION("em_mallocfailed");
9897                 error = EMAIL_ERROR_OUT_OF_MEMORY;
9898                 goto FINISH_OFF;
9899         }
9900         SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_tbl SET mailbox_type = %d, mailbox_id = %d, account_id = %d %s", target_mailbox_type, input_mailbox_id, target_account_id, conditional_clause);
9901         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9902
9903         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9904         if (error != EMAIL_ERROR_NONE) {
9905                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9906                         goto FINISH_OFF;
9907         }
9908
9909         /* Updating a mail_attachment_tbl */
9910         memset(sql_query_string, 0x00, conditional_clause_len);
9911         SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_attachment_tbl SET mailbox_id = '%d', account_id = %d %s", input_mailbox_id, target_account_id, conditional_clause);
9912         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9913         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9914         if (error != EMAIL_ERROR_NONE) {
9915                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9916                         goto FINISH_OFF;
9917         }
9918
9919         /* Updating a mail_meeting_tbl */
9920         memset(sql_query_string, 0x00, conditional_clause_len);
9921         SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_meeting_tbl SET mailbox_id = %d, account_id = %d %s", input_mailbox_id, target_account_id, conditional_clause);
9922         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9923         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9924         if (error != EMAIL_ERROR_NONE) {
9925                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9926                         goto FINISH_OFF;
9927         }
9928
9929 #ifdef __FEATURE_BODY_SEARCH__
9930         /* Updating mail_text_tbl */
9931         memset(sql_query_string, 0x00, conditional_clause_len);
9932         SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_text_tbl SET mailbox_id = %d, account_id = %d %s", input_mailbox_id, target_account_id, conditional_clause);
9933         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9934         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9935         if (error != EMAIL_ERROR_NONE) {
9936                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9937                         goto FINISH_OFF;
9938         }
9939 #endif
9940
9941         /* Updating a mail_read_mail_uid_tbl */
9942         memset(conditional_clause, 0x00, conditional_clause_len);
9943         cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE local_uid in (");
9944
9945         for (i = 0; i < number_of_mails; i++)
9946                 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
9947
9948         /* prevent 34415 */
9949         last_comma = rindex(conditional_clause, ',');
9950         if (last_comma) *last_comma = ')'; /* replace , with) */
9951
9952         memset(sql_query_string, 0x00, conditional_clause_len);
9953         SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_read_mail_uid_tbl SET mailbox_name = '%s', mailbox_id = %d, account_id = %d %s", target_mailbox_name, input_mailbox_id, target_account_id, conditional_clause);
9954         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9955         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9956         if (error != EMAIL_ERROR_NONE) {
9957                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9958                         goto FINISH_OFF;
9959         }
9960
9961         ret = true;
9962
9963 FINISH_OFF:
9964         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9965
9966         EM_SAFE_FREE(target_mailbox_name);
9967         EM_SAFE_FREE(conditional_clause);
9968         EM_SAFE_FREE(sql_query_string);
9969
9970         if (err_code != NULL)
9971                 *err_code = error;
9972
9973         EM_DEBUG_FUNC_END("ret [%d]", ret);
9974         return ret;
9975 }
9976
9977 INTERNAL_FUNC int emstorage_delete_mail(char *multi_user_name, int mail_id, int from_server, int transaction, int *err_code)
9978 {
9979         EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9980
9981         if (!mail_id) {
9982                 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
9983                 if (err_code != NULL)
9984                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9985                 return false;
9986         }
9987
9988         int ret = false;
9989         int error = EMAIL_ERROR_NONE;
9990         char sql_query_string[QUERY_SIZE] = {0, };
9991         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9992         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9993
9994         memset(sql_query_string, 0x00, sizeof(sql_query_string));
9995
9996         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id = %d ", mail_id);
9997         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9998         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9999         if (error != EMAIL_ERROR_NONE) {
10000                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10001                         goto FINISH_OFF;
10002         }
10003
10004         ret = true;
10005
10006 FINISH_OFF:
10007         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10008
10009         if (err_code != NULL)
10010                 *err_code = error;
10011
10012         EM_DEBUG_FUNC_END("ret [%d]", ret);
10013         return ret;
10014 }
10015
10016 INTERNAL_FUNC int emstorage_delete_multiple_mails(char *multi_user_name, int mail_ids[], int number_of_mails, int transaction, int *err_code)
10017 {
10018         EM_DEBUG_FUNC_BEGIN("mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", mail_ids, number_of_mails, transaction, err_code);
10019
10020         int ret = false, i, cur_sql_query_string = 0;
10021         int error = EMAIL_ERROR_NONE;
10022         int query_size = 0;
10023         char *sql_query_string = NULL;
10024
10025         if (!mail_ids) {
10026                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10027                 if (err_code != NULL)
10028                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10029                 return false;
10030         }
10031
10032         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10033         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10034
10035         query_size = (sizeof(char) * 8 * number_of_mails) + 512;
10036         sql_query_string =  em_malloc(query_size);
10037         if (sql_query_string == NULL) {
10038                 EM_DEBUG_EXCEPTION("em_mallocfailed");
10039                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10040                 goto FINISH_OFF;
10041         }
10042
10043         cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_tbl WHERE mail_id in (");
10044
10045         for (i = 0; i < number_of_mails; i++)
10046                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
10047
10048         /* prevent 34414 */
10049         char *last_comma = rindex(sql_query_string, ',');
10050         if (last_comma != NULL) *last_comma = ')'; /* replace , with) */
10051
10052         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10053         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10054         if (error != EMAIL_ERROR_NONE) {
10055                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10056                         goto FINISH_OFF;
10057         }
10058
10059 #ifdef __FEATURE_BODY_SEARCH__
10060         /* delete mail_text from mail_text_tbl */
10061         cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_text_tbl WHERE mail_id in (");
10062
10063         for (i = 0; i < number_of_mails; i++)
10064                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
10065
10066         last_comma = rindex(sql_query_string, ',');
10067         *last_comma = ')'; /* replace , with) */
10068
10069         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10070         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10071         if (error != EMAIL_ERROR_NONE) {
10072                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10073                         goto FINISH_OFF;
10074         }
10075 #endif
10076
10077         ret = true;
10078
10079 FINISH_OFF:
10080         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10081
10082         EM_SAFE_FREE(sql_query_string);
10083         if (err_code != NULL)
10084                 *err_code = error;
10085
10086         EM_DEBUG_FUNC_END("ret [%d]", ret);
10087         return ret;
10088 }
10089
10090 INTERNAL_FUNC int emstorage_delete_mail_by_account(char *multi_user_name, int account_id, int transaction, int *err_code)
10091 {
10092         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
10093
10094         if (account_id < FIRST_ACCOUNT_ID) {
10095                 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
10096
10097                 if (err_code != NULL)
10098                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10099                 return false;
10100         }
10101
10102         int rc, ret = false;
10103         int error = EMAIL_ERROR_NONE;
10104         char sql_query_string[QUERY_SIZE] = {0, };
10105
10106         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10107         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10108
10109         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d", account_id);
10110         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10111         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10112         if (error != EMAIL_ERROR_NONE) {
10113                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10114                         goto FINISH_OFF;
10115         }
10116
10117         rc = sqlite3_changes(local_db_handle);
10118         if (rc == 0) {
10119                 EM_DEBUG_LOG(" no mail found...");
10120                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10121         }
10122
10123         /* Delete all mails  mail_read_mail_uid_tbl table based on account id */
10124         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
10125         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10126         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10127         if (error != EMAIL_ERROR_NONE) {
10128                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10129                         goto FINISH_OFF;
10130         }
10131
10132         rc = sqlite3_changes(local_db_handle);
10133         if (rc == 0) {
10134                 EM_DEBUG_LOG("No mail found...");
10135                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10136         }
10137
10138 #ifdef __FEATURE_BODY_SEARCH__
10139         /* Delete all mail_text in mail_text_tbl table based on account id */
10140         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE account_id = %d", account_id);
10141         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10142         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10143         if (error != EMAIL_ERROR_NONE) {
10144                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10145                         goto FINISH_OFF;
10146         }
10147
10148         rc = sqlite3_changes(local_db_handle);
10149         if (rc == 0) {
10150                 EM_DEBUG_LOG("No mail found...");
10151                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10152         }
10153 #endif
10154
10155         ret = true;
10156
10157 FINISH_OFF:
10158         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10159
10160         if (error == EMAIL_ERROR_NONE) {
10161                 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_WITH_ACCOUNT, account_id, 0 , NULL, 0))
10162                         EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAIL_DELETE_ALL ]");
10163         }
10164
10165         if (err_code != NULL)
10166                 *err_code = error;
10167
10168         EM_DEBUG_FUNC_END("ret [%d]", ret);
10169         return ret;
10170 }
10171
10172 INTERNAL_FUNC int emstorage_delete_mail_by_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox, int transaction, int *err_code)
10173 {
10174         EM_DEBUG_FUNC_BEGIN("mailbox[%p], transaction[%d], err_code[%p]", mailbox, transaction, err_code);
10175
10176         if (mailbox == NULL) {
10177                 EM_DEBUG_EXCEPTION("mailbox [%p]", mailbox);
10178                 if (err_code != NULL)
10179                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10180                 return false;
10181         }
10182
10183         int ret = false;
10184         int error = EMAIL_ERROR_NONE;
10185         char sql_query_string[QUERY_SIZE] = {0, };
10186         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10187         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10188
10189         if (strcmp(mailbox->mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
10190                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d AND mailbox_type = %d", mailbox->account_id, mailbox->mailbox_type);
10191                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10192                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10193                 if (error != EMAIL_ERROR_NONE) {
10194                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10195                         goto FINISH_OFF;
10196                 }
10197
10198                 /* Delete Mails from mail_read_mail_uid_tbl */
10199                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d AND mailbox_name = '%s'", mailbox->account_id, mailbox->mailbox_name);
10200                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10201                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10202                 if (error != EMAIL_ERROR_NONE) {
10203                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10204                         goto FINISH_OFF;
10205                 }
10206         } else {
10207                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d AND mailbox_id = %d", mailbox->account_id, mailbox->mailbox_id);
10208                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10209                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10210                 if (error != EMAIL_ERROR_NONE) {
10211                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10212                         goto FINISH_OFF;
10213                 }
10214
10215                 /* Delete Mails from mail_read_mail_uid_tbl */
10216                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d AND mailbox_id = %d", mailbox->account_id, mailbox->mailbox_id);
10217                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10218                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10219                 if (error != EMAIL_ERROR_NONE) {
10220                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10221                         goto FINISH_OFF;
10222                 }
10223
10224 #ifdef __FEATURE_BODY_SEARCH__
10225                 /* Delete Mails from mail_text_tbl */
10226                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE account_id = %d AND mailbox_id = %d", mailbox->account_id, mailbox->mailbox_id);
10227                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10228                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10229                 if (error != EMAIL_ERROR_NONE) {
10230                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10231                         goto FINISH_OFF;
10232                 }
10233 #endif
10234         }
10235         ret = true;
10236
10237 FINISH_OFF:
10238         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10239
10240         if (error == EMAIL_ERROR_NONE) {
10241                 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_ALL, mailbox->account_id, mailbox->mailbox_id , mailbox->mailbox_name, 0))
10242                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventFailed [ NOTI_MAIL_DELETE_ALL ] >>>> ");
10243         }
10244
10245         if (err_code != NULL)
10246                 *err_code = error;
10247
10248         EM_DEBUG_FUNC_END("ret [%d]", ret);
10249         return ret;
10250 }
10251
10252 INTERNAL_FUNC int emstorage_free_mail(emstorage_mail_tbl_t** mail_list, int count, int *err_code)
10253 {
10254         EM_DEBUG_FUNC_BEGIN("mail_list[%p], count[%d], err_code[%p]", mail_list, count, err_code);
10255
10256         if (count > 0) {
10257                 if ((mail_list == NULL) || (*mail_list == NULL)) {
10258                         EM_DEBUG_EXCEPTION("mail_ilst[%p], count[%d]", mail_list, count);
10259
10260                         if (err_code)
10261                                 *err_code = EMAIL_ERROR_INVALID_PARAM;
10262                         return false;
10263                 }
10264
10265                 emstorage_mail_tbl_t* p = *mail_list;
10266                 int i = 0;
10267
10268                 for (; i < count; i++, p++) {
10269                         EM_SAFE_FREE(p->server_mailbox_name);
10270                         EM_SAFE_FREE(p->server_mail_id);
10271                         EM_SAFE_FREE(p->full_address_from);
10272                         EM_SAFE_FREE(p->full_address_reply);
10273                         EM_SAFE_FREE(p->full_address_to);
10274                         EM_SAFE_FREE(p->full_address_cc);
10275                         EM_SAFE_FREE(p->full_address_bcc);
10276                         EM_SAFE_FREE(p->full_address_return);
10277                         EM_SAFE_FREE(p->subject);
10278                         EM_SAFE_FREE(p->file_path_plain);
10279                         EM_SAFE_FREE(p->file_path_html);
10280                         EM_SAFE_FREE(p->file_path_mime_entity);
10281                         EM_SAFE_FREE(p->message_id);
10282                         EM_SAFE_FREE(p->email_address_sender);
10283                         EM_SAFE_FREE(p->email_address_recipient);
10284                         EM_SAFE_FREE(p->preview_text);
10285                         EM_SAFE_FREE(p->alias_sender);
10286                         EM_SAFE_FREE(p->alias_recipient);
10287                         EM_SAFE_FREE(p->default_charset);
10288                         EM_SAFE_FREE(p->pgp_password);
10289                         EM_SAFE_FREE(p->eas_data);
10290                         EM_SAFE_FREE(p->user_name);
10291                 }
10292                 EM_SAFE_FREE(*mail_list);
10293         }
10294
10295         if (err_code != NULL)
10296                 *err_code = EMAIL_ERROR_NONE;
10297
10298         EM_DEBUG_FUNC_END();
10299         return true;
10300 }
10301
10302 #ifdef __FEATURE_BODY_SEARCH__
10303 INTERNAL_FUNC void emstorage_free_mail_text(emstorage_mail_text_tbl_t** mail_text_list, int count, int *err_code)
10304 {
10305         EM_DEBUG_FUNC_BEGIN("mail_text_list[%p], count[%d], err_code[%p]", mail_text_list, count, err_code);
10306
10307         if (count > 0) {
10308                 if ((mail_text_list == NULL) || (*mail_text_list == NULL)) {
10309                         EM_DEBUG_LOG("Nothing to free: mail_text_list[%p]", mail_text_list);
10310                         return;
10311                 }
10312
10313                 emstorage_mail_text_tbl_t *p = *mail_text_list;
10314                 int i = 0;
10315
10316                 for (; i < count; i++, p++) {
10317                         EM_SAFE_FREE(p->body_text);
10318                 }
10319                 EM_SAFE_FREE(*mail_text_list);
10320         }
10321
10322         EM_DEBUG_FUNC_END();
10323 }
10324 #endif
10325
10326 INTERNAL_FUNC int emstorage_get_attachment_count(char *multi_user_name, int mail_id, int *count, int transaction, int *err_code)
10327 {
10328         EM_DEBUG_FUNC_BEGIN("mail_id[%d], count[%p], transaction[%d], err_code[%p]", mail_id, count, transaction, err_code);
10329
10330         if (mail_id <= 0 || !count) {
10331                 EM_DEBUG_EXCEPTION("mail_id[%d], count[%p]", mail_id, count);
10332                 if (err_code != NULL)
10333                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10334                 return false;
10335         }
10336
10337         int rc = -1, ret = false;
10338         int error = EMAIL_ERROR_NONE;
10339         char sql_query_string[QUERY_SIZE] = {0, };
10340
10341         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10342         EMSTORAGE_START_READ_TRANSACTION(transaction);
10343
10344         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
10345
10346         char **result;
10347
10348         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10349         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10350                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10351
10352         *count = atoi(result[1]);
10353         sqlite3_free_table(result);
10354
10355         ret = true;
10356
10357 FINISH_OFF:
10358         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10359
10360         if (err_code != NULL)
10361                 *err_code = error;
10362
10363         EM_DEBUG_FUNC_END("ret [%d]", ret);
10364         return ret;
10365 }
10366
10367 INTERNAL_FUNC int emstorage_get_attachment_list(char *multi_user_name, int input_mail_id, int input_transaction, emstorage_attachment_tbl_t** output_attachment_list, int *output_attachment_count)
10368 {
10369         EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], input_transaction[%d], output_attachment_list[%p], output_attachment_count[%p]", input_mail_id, input_transaction, output_attachment_list, output_attachment_count);
10370
10371         if (input_mail_id <= 0 || !output_attachment_list || !output_attachment_count) {
10372                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10373                 return EMAIL_ERROR_INVALID_PARAM;
10374         }
10375
10376         int                         error = EMAIL_ERROR_NONE;
10377         int                         i = 0;
10378         int                         rc = -1;
10379         char                      **result = NULL;
10380         char                        sql_query_string[QUERY_SIZE] = {0, };
10381         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10382         DB_STMT hStmt = NULL;
10383         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10384
10385         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
10386         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", input_mail_id);
10387         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10388         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10389                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10390
10391         *output_attachment_count = atoi(result[1]);
10392         sqlite3_free_table(result);
10393
10394         if (*output_attachment_count == 0) {
10395                 error = EMAIL_ERROR_NONE;
10396                 goto FINISH_OFF;
10397         }
10398
10399         p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * (*output_attachment_count));
10400
10401         if (!p_data_tbl) {
10402                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
10403                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10404                 goto FINISH_OFF;
10405         }
10406
10407         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE mail_id = %d ORDER BY attachment_id", input_mail_id);
10408         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
10409
10410         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10411         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10412
10413         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10414         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },  ("sqlite3_step fail:%d", rc));
10415
10416         if (rc == SQLITE_DONE) {
10417                 EM_DEBUG_EXCEPTION("no matched attachment found...");
10418                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10419                 goto FINISH_OFF;
10420         }
10421         for (i = 0; i < *output_attachment_count; i++) {
10422                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10423                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10424                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10425                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10426                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10427                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10428                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10429                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10430                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10431                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10432                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10433                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10434                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10435                 EM_DEBUG_LOG("attachment[%d].attachment_id : %d", i, p_data_tbl[i].attachment_id);
10436                 EM_DEBUG_LOG("attachment_mime_type : %s", p_data_tbl[i].attachment_mime_type);
10437                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10438                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
10439         }
10440
10441 FINISH_OFF:
10442
10443         if (error == EMAIL_ERROR_NONE)
10444                 *output_attachment_list = p_data_tbl;
10445         else if (p_data_tbl != NULL)
10446                 emstorage_free_attachment(&p_data_tbl, *output_attachment_count, NULL);
10447
10448         if (hStmt) {
10449                 rc = sqlite3_finalize(hStmt);
10450                 if (rc != SQLITE_OK) {
10451                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10452                         error = EMAIL_ERROR_DB_FAILURE;
10453                         if (*output_attachment_list)
10454                                 emstorage_free_attachment(output_attachment_list, *output_attachment_count, NULL); /* prevent */
10455                 }
10456         }
10457
10458         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
10459
10460         EM_DEBUG_FUNC_END("error [%d]", error);
10461         return error;
10462 }
10463
10464 INTERNAL_FUNC int emstorage_get_attachment(char *multi_user_name, int attachment_id, emstorage_attachment_tbl_t **attachment, int transaction, int *err_code)
10465 {
10466         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], transaction[%d], err_code[%p]", attachment_id, attachment, transaction, err_code);
10467
10468         if (attachment_id <= 0 || !attachment) {
10469                 EM_DEBUG_EXCEPTION("attachment_id[%d], attachment[%p]", attachment_id, attachment);
10470                 if (err_code != NULL)
10471                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10472                 return false;
10473         }
10474
10475         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10476         int rc, ret = false;
10477         int error = EMAIL_ERROR_NONE;
10478         char sql_query_string[QUERY_SIZE] = {0, };
10479
10480         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10481         EMSTORAGE_START_READ_TRANSACTION(transaction);
10482
10483         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_id = %d",  attachment_id);
10484
10485         sqlite3_stmt* hStmt = NULL;
10486
10487         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10488         EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
10489
10490         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10491                 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10492
10493
10494         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10495         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10496                 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10497
10498         if (rc == SQLITE_DONE) {
10499                 EM_DEBUG_LOG("no matched attachment found...");
10500                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10501                 goto FINISH_OFF;
10502         }
10503
10504         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10505                 EM_DEBUG_EXCEPTION("malloc failed...");
10506                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10507                 goto FINISH_OFF;
10508         }
10509
10510         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10511         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10512         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10513         _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10514         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10515         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10516         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10517         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10518         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10519         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10520         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10521         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10522         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10523
10524 #ifdef __ATTACHMENT_OPTI__
10525                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10526                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10527 #endif
10528
10529         ret = true;
10530
10531 FINISH_OFF:
10532         if (ret == true)
10533                 *attachment = p_data_tbl;
10534
10535         if (hStmt != NULL) {
10536                 rc = sqlite3_finalize(hStmt);
10537                 if (rc != SQLITE_OK) {
10538                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10539                         error = EMAIL_ERROR_DB_FAILURE;
10540                 }
10541         }
10542
10543         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10544
10545         if (err_code != NULL)
10546                 *err_code = error;
10547
10548         EM_DEBUG_FUNC_END("ret [%d]", ret);
10549         return ret;
10550 }
10551
10552 INTERNAL_FUNC int emstorage_get_attachment_nth(char *multi_user_name, int mail_id, int nth, emstorage_attachment_tbl_t **attachment_tbl, int transaction, int *err_code)
10553 {
10554         EM_DEBUG_FUNC_BEGIN("mail_id[%d], nth[%d], attachment_tbl[%p], transaction[%d], err_code[%p]", mail_id, nth, attachment_tbl, transaction, err_code);
10555
10556         if (mail_id <= 0 || nth <= 0 || !attachment_tbl) {
10557                 EM_DEBUG_EXCEPTION(" mail_id[%d], nth[%d], attachment[%p]", mail_id, nth, attachment_tbl);
10558
10559                 if (err_code != NULL)
10560                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10561                 return false;
10562         }
10563
10564         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10565         char *p = NULL;
10566         int rc, ret = false;
10567         int error = EMAIL_ERROR_NONE;
10568         char sql_query_string[QUERY_SIZE] = {0, };
10569
10570         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10571         EMSTORAGE_START_READ_TRANSACTION(transaction);
10572
10573         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE mail_id = %d ORDER BY attachment_id LIMIT %d, 1", mail_id, (nth - 1));
10574         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10575
10576         DB_STMT hStmt = NULL;
10577
10578         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10579         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10580                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10581
10582
10583         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10584         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10585                 ("sqlite3_step fail:%d", rc));
10586
10587         if (rc == SQLITE_DONE) {
10588                 EM_DEBUG_EXCEPTION("no matched attachment found: mail_id[%d] nth[%d]", mail_id, nth);
10589                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10590                 goto FINISH_OFF;
10591         }
10592
10593         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10594                 EM_DEBUG_EXCEPTION(" malloc failed...");
10595                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10596                 goto FINISH_OFF;
10597         }
10598
10599         p_data_tbl->attachment_id = sqlite3_column_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10600         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10601                 p_data_tbl->attachment_name = cpy_str(p);
10602         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10603                 p_data_tbl->attachment_path = cpy_str(p);
10604         if ((p = (char *)sqlite3_column_text(hStmt, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10605                 p_data_tbl->content_id = cpy_str(p);
10606         p_data_tbl->attachment_size = sqlite3_column_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10607         p_data_tbl->mail_id = sqlite3_column_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10608         p_data_tbl->account_id = sqlite3_column_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10609         p_data_tbl->mailbox_id = sqlite3_column_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10610         p_data_tbl->attachment_save_status = sqlite3_column_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10611         p_data_tbl->attachment_drm_type = sqlite3_column_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10612         p_data_tbl->attachment_drm_method = sqlite3_column_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10613         p_data_tbl->attachment_inline_content_status = sqlite3_column_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10614         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10615                 p_data_tbl->attachment_mime_type = cpy_str(p);
10616 #ifdef __ATTACHMENT_OPTI__
10617                 p_data_tbl->encoding = sqlite3_column_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10618                 if ((p = (char *)sqlite3_column_text(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10619                         p_data_tbl->section = cpy_str(p);
10620 #endif
10621         ret = true;
10622
10623 FINISH_OFF:
10624         if (ret == true)
10625                 *attachment_tbl = p_data_tbl;
10626
10627         if (hStmt != NULL) {
10628                 rc = sqlite3_finalize(hStmt);
10629                 if (rc != SQLITE_OK) {
10630                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10631                         error = EMAIL_ERROR_DB_FAILURE;
10632                 }
10633         }
10634
10635         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10636
10637         if (err_code != NULL)
10638                 *err_code = error;
10639
10640         EM_DEBUG_FUNC_END("ret [%d]", ret);
10641         return ret;
10642 }
10643
10644 INTERNAL_FUNC int emstorage_get_attachment_by_attachment_path(char *multi_user_name, char *attachment_path, emstorage_attachment_tbl_t **attachment, int transaction, int *err_code)
10645 {
10646         EM_DEBUG_FUNC_BEGIN("attachment_path[%p], attachment[%p], transaction[%d], err_code[%p]", attachment_path, attachment, transaction, err_code);
10647
10648         if (attachment_path == NULL || !attachment) {
10649                 EM_DEBUG_EXCEPTION("attachment_path[%p], attachment[%p]", attachment_path, attachment);
10650                 if (err_code != NULL)
10651                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10652                 return false;
10653         }
10654
10655         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10656         int rc, ret = false;
10657         int error = EMAIL_ERROR_NONE;
10658         char sql_query_string[QUERY_SIZE] = {0, };
10659
10660         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10661         EMSTORAGE_START_READ_TRANSACTION(transaction);
10662
10663         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_path = '%s'", attachment_path);
10664
10665         sqlite3_stmt* hStmt = NULL;
10666
10667         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10668         EM_DEBUG_LOG_DEV("before sqlite3_prepare hStmt = %p", hStmt);
10669
10670         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10671                 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10672
10673
10674         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10675         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10676                 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10677
10678         if (rc == SQLITE_DONE) {
10679                 EM_DEBUG_LOG("no matched attachment found...");
10680                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10681                 goto FINISH_OFF;
10682         }
10683
10684         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10685                 EM_DEBUG_EXCEPTION("malloc failed...");
10686                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10687                 goto FINISH_OFF;
10688         }
10689
10690         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10691         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10692         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10693         _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10694         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10695         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10696         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10697         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10698         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10699         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10700         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10701         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10702         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10703
10704 #ifdef __ATTACHMENT_OPTI__
10705                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10706                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10707 #endif
10708
10709         ret = true;
10710
10711 FINISH_OFF:
10712         if (ret == true)
10713                 *attachment = p_data_tbl;
10714
10715         if (hStmt != NULL) {
10716                 rc = sqlite3_finalize(hStmt);
10717                 if (rc != SQLITE_OK) {
10718                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10719                         error = EMAIL_ERROR_DB_FAILURE;
10720                 }
10721         }
10722
10723         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10724
10725         if (err_code != NULL)
10726                 *err_code = error;
10727
10728         EM_DEBUG_FUNC_END("ret [%d]", ret);
10729         return ret;
10730 }
10731
10732 INTERNAL_FUNC int emstorage_change_attachment_field(char *multi_user_name, int mail_id, email_mail_change_type_t type, emstorage_attachment_tbl_t *attachment, int transaction, int *err_code)
10733 {
10734         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], attachment[%p], transaction[%d], err_code[%p]", mail_id, type, attachment, transaction, err_code);
10735
10736         if (mail_id <= 0 || !attachment) {
10737                 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], attachment[%p]", mail_id, type, attachment);
10738                 if (err_code != NULL)
10739                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10740                 return false;;
10741         }
10742
10743         int rc, ret = false;
10744         int error = EMAIL_ERROR_NONE;
10745         DB_STMT hStmt = NULL;
10746         char sql_query_string[QUERY_SIZE] = {0, };
10747
10748         int i = 0;
10749
10750         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10751
10752         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10753
10754         switch (type) {
10755                 case UPDATE_MAILBOX:
10756                                 EM_DEBUG_LOG("UPDATE_MAILBOX");
10757                         if (!attachment->mailbox_id) {
10758                                 EM_DEBUG_EXCEPTION(" attachment->mailbox_id[%d]", attachment->mailbox_id);
10759                                 error = EMAIL_ERROR_INVALID_PARAM;
10760                                 goto FINISH_OFF;
10761                         }
10762                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
10763                                 "UPDATE mail_attachment_tbl SET mailbox_id = ? WHERE mail_id = %d", mail_id);
10764
10765                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10766                         EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10767                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10768                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10769
10770                         _bind_stmt_field_data_int(hStmt, i++, attachment->mailbox_id);
10771                         break;
10772
10773                 case UPDATE_SAVENAME:
10774                         EM_DEBUG_LOG("UPDATE_SAVENAME");
10775                         if (!attachment->attachment_path) {
10776                                 EM_DEBUG_EXCEPTION(" attachment->attachment_path[%p]", attachment->attachment_path);
10777                                 error = EMAIL_ERROR_INVALID_PARAM;
10778                                 goto FINISH_OFF;
10779                         }
10780
10781                         SNPRINTF(sql_query_string, sizeof(sql_query_string),
10782                                 "UPDATE mail_attachment_tbl SET"
10783                                 "  attachment_size = ?"
10784                                 ", attachment_save_status = ?"
10785                                 ", attachment_path = ?"
10786                                 " WHERE mail_id = %d"
10787                                 " AND attachment_id = %d"
10788                                 , attachment->mail_id
10789                                 , attachment->attachment_id);
10790
10791
10792                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10793                         EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10794                         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10795                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10796
10797                         _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_size);
10798                         _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_save_status);
10799                         _bind_stmt_field_data_string(hStmt, i++, (char *)attachment->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10800                         break;
10801
10802                 default:
10803                         EM_DEBUG_LOG("type[%d]", type);
10804                         error = EMAIL_ERROR_INVALID_PARAM;
10805                         goto FINISH_OFF;
10806         }
10807         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10808
10809         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10810         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10811                 ("sqlite3_step fail:%d", rc));
10812         ret = true;
10813
10814 FINISH_OFF:
10815
10816         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10817
10818         if (hStmt != NULL) {
10819                 rc = sqlite3_finalize(hStmt);
10820                 if (rc != SQLITE_OK) {
10821                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10822                         error = EMAIL_ERROR_DB_FAILURE;
10823                 }
10824         }
10825
10826         if (err_code != NULL)
10827                 *err_code = error;
10828         EM_DEBUG_FUNC_END("ret [%d]", ret);
10829         return ret;
10830 }
10831
10832 INTERNAL_FUNC int emstorage_rename_mailbox(char *multi_user_name, int input_mailbox_id, char *input_new_mailbox_name, char *input_new_mailbox_alias, void *input_eas_data, int input_eas_data_length, int input_transaction)
10833 {
10834         EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d] input_new_mailbox_name[%p] input_new_mailbox_alias[%p] input_eas_data[%p] input_eas_data_length[%d] input_transaction[%d]", input_mailbox_id, input_new_mailbox_name, input_new_mailbox_alias, input_eas_data, input_eas_data_length, input_transaction);
10835
10836         int rc = 0;
10837         int ret = false;
10838         int error = EMAIL_ERROR_NONE;
10839         int field_idx = 0;
10840         int account_id = 0;
10841         char sql_query_string[QUERY_SIZE] = {0, };
10842         char *replaced_mailbox_name = NULL;
10843         char *replaced_alias = NULL;
10844         sqlite3 *local_db_handle = NULL;
10845         DB_STMT hStmt = NULL;
10846         emstorage_mailbox_tbl_t *old_mailbox_data = NULL;
10847
10848         if (input_mailbox_id <= 0 || !input_new_mailbox_name || !input_new_mailbox_alias) {
10849                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10850                 return EMAIL_ERROR_INVALID_PARAM;
10851         }
10852
10853         if (strstr(input_new_mailbox_name, "'")) {
10854                 replaced_mailbox_name = em_replace_all_string(input_new_mailbox_name, "'", "''");
10855         } else {
10856                 replaced_mailbox_name = strdup(input_new_mailbox_name);
10857         }
10858
10859         if (strstr(input_new_mailbox_alias, "'")) {
10860                 replaced_alias = em_replace_all_string(input_new_mailbox_alias, "'", "''");
10861         } else {
10862                 replaced_alias = strdup(input_new_mailbox_alias);
10863         }
10864
10865         local_db_handle = emstorage_get_db_connection(multi_user_name);
10866
10867         if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &old_mailbox_data)) != EMAIL_ERROR_NONE) {
10868                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", error);
10869                 EM_SAFE_FREE(replaced_mailbox_name);
10870                 EM_SAFE_FREE(replaced_alias);
10871                 return error;
10872         }
10873
10874     if (old_mailbox_data == NULL) {
10875         EM_DEBUG_LOG("old_mailbox_data is NULL");
10876         error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
10877         goto FINISH_OFF;
10878     }
10879
10880         account_id = old_mailbox_data->account_id;
10881
10882         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
10883
10884         if (input_eas_data && input_eas_data_length > 0) {
10885                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10886                         "UPDATE mail_box_tbl SET"
10887                         " mailbox_name = ?"
10888                         ",alias = ?"
10889                         ",eas_data = ?"
10890                         ",eas_data_length = ?"
10891                         " WHERE mailbox_id = ?");
10892
10893                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10894
10895                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10896                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10897
10898                 _bind_stmt_field_data_string(hStmt, field_idx++, input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10899                 _bind_stmt_field_data_string(hStmt, field_idx++, input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10900                 _bind_stmt_field_data_blob(hStmt, field_idx++, input_eas_data, input_eas_data_length);
10901                 _bind_stmt_field_data_int(hStmt, field_idx++, input_eas_data_length);
10902                 _bind_stmt_field_data_int(hStmt, field_idx++, input_mailbox_id);
10903         } else {
10904                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10905                                 "UPDATE mail_box_tbl SET"
10906                                 " mailbox_name = ?"
10907                                 ",alias = ?"
10908                                 " WHERE mailbox_id = ?");
10909
10910                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10911
10912                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10913                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10914
10915                 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
10916                 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10917                 _bind_stmt_field_data_int(hStmt, field_idx++ , input_mailbox_id);
10918         }
10919
10920
10921         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10922         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
10923                 ("sqlite3_step fail:%d", rc));
10924         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10925                 ("sqlite3_step fail:%d", rc));
10926
10927         if (sqlite3_changes(local_db_handle) == 0)
10928                 EM_DEBUG_LOG("no mail_meeting_tbl matched...");
10929
10930         ret = true;
10931
10932 FINISH_OFF:
10933         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, error);
10934         if (error == EMAIL_ERROR_NONE) {
10935                 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME, account_id, input_mailbox_id, input_new_mailbox_name, 0))
10936                         EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAILBOX_RENAME ] >>>> ");
10937         } else {
10938                 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME_FAIL, account_id, input_mailbox_id, input_new_mailbox_name, error))
10939                         EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAILBOX_RENAME_FAIL ] >>>> ");
10940         }
10941
10942         EM_SAFE_FREE(replaced_mailbox_name);
10943         EM_SAFE_FREE(replaced_alias);
10944
10945         if (old_mailbox_data)
10946                 emstorage_free_mailbox(&old_mailbox_data, 1, NULL);
10947
10948         if (hStmt != NULL) {
10949                 rc = sqlite3_finalize(hStmt);
10950                 if (rc != SQLITE_OK) {
10951                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10952                         error = EMAIL_ERROR_DB_FAILURE;
10953                 }
10954         }
10955
10956         EM_DEBUG_FUNC_END("error [%d]", error);
10957         return error;
10958 }
10959
10960 INTERNAL_FUNC int emstorage_get_new_attachment_no(char *multi_user_name, int *attachment_no, int *err_code)
10961 {
10962         EM_DEBUG_FUNC_BEGIN("attachment_no [%p], err_code[%p]", attachment_no, err_code);
10963         int rc, ret = false;
10964         int error = EMAIL_ERROR_NONE;
10965         char *sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
10966         char **result;
10967
10968         if (!attachment_no) {
10969                 EM_DEBUG_EXCEPTION("Invalid attachment");
10970                 if (err_code != NULL)
10971                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10972                 return false;
10973         }
10974
10975         *attachment_no = -1;
10976
10977         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10978
10979
10980         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
10981         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10982                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
10983
10984         if (NULL == result[1])
10985                 rc = 1;
10986         else
10987                 rc = atoi(result[1])+1;
10988
10989         sqlite3_free_table(result);
10990
10991         *attachment_no = rc;
10992         EM_DEBUG_LOG("attachment_no [%d]", *attachment_no);
10993         ret = true;
10994
10995 FINISH_OFF:
10996
10997         if (err_code != NULL)
10998                 *err_code = error;
10999
11000         EM_DEBUG_FUNC_END("ret [%d]", ret);
11001         return ret;
11002 }
11003
11004 INTERNAL_FUNC int emstorage_add_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int iscopy, int transaction, int *err_code)
11005 {
11006         EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], iscopy[%d], transaction[%d], err_code[%p]", attachment_tbl, iscopy, transaction, err_code);
11007
11008         char *sql = NULL;
11009         char **result;
11010         int rc, ret = false;
11011         int error = EMAIL_ERROR_NONE;
11012         char sql_query_string[QUERY_SIZE] = {0, };
11013
11014         DB_STMT hStmt = NULL;
11015         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11016
11017         if (!attachment_tbl) {
11018                 EM_DEBUG_EXCEPTION("attachment_tbl[%p], iscopy[%d]", attachment_tbl, iscopy);
11019                 if (err_code != NULL)
11020                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11021                 return false;
11022         }
11023
11024         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11025
11026         sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
11027
11028         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
11029         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
11030                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
11031
11032         if (NULL == result[1]) rc = 1;
11033         else rc = atoi(result[1]) + 1;
11034         sqlite3_free_table(result);
11035
11036         attachment_tbl->attachment_id = rc;
11037
11038         SNPRINTF(sql_query_string, sizeof(sql_query_string),
11039                 "INSERT INTO mail_attachment_tbl VALUES "
11040                 "(?"    /* attachment_id */
11041                 ", ?"   /* attachment_name */
11042                 ", ?"   /* attachment_path */
11043                 ", ?"   /* content_id */
11044                 ", ?"   /* attachment_size */
11045                 ", ?"   /* mail_id */
11046                 ", ?"   /* account_id */
11047                 ", ?"   /* mailbox_id */
11048                 ", ?"   /* attachment_save_status */
11049                 ", ?"   /* attachment_drm_type */
11050                 ", ?"   /* attachment_drm_method */
11051                 ", ?"   /* attachment_inline_content_status */
11052                 ", ?"   /* attachment_mime_type */
11053 #ifdef __ATTACHMENT_OPTI__
11054                 ", ?"
11055                 ", ?"
11056 #endif
11057                 ")");
11058
11059
11060         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11061         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11062                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11063
11064         _bind_stmt_field_data_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_id);
11065         _bind_stmt_field_data_string(hStmt, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->attachment_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
11066         _bind_stmt_field_data_string(hStmt, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
11067         _bind_stmt_field_data_string(hStmt, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->content_id, 0, CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL);
11068         _bind_stmt_field_data_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_size);
11069         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mail_id);
11070         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->account_id);
11071         _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mailbox_id);
11072         _bind_stmt_field_data_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_save_status);
11073         _bind_stmt_field_data_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_type);
11074         _bind_stmt_field_data_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_method);
11075         _bind_stmt_field_data_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_inline_content_status);
11076         _bind_stmt_field_data_string(hStmt, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->attachment_mime_type, 0, ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL);
11077 #ifdef __ATTACHMENT_OPTI__
11078         _bind_stmt_field_data_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->encoding);
11079         _bind_stmt_field_data_string(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->section, 0, ATTACHMENT_LEN_IN_MAIL_ATTACHMENT_TBL);
11080 #endif
11081
11082
11083         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11084         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
11085                 ("sqlite3_step fail:%d", rc));
11086         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11087                 ("sqlite3_step fail:%d", rc));
11088
11089         rc = sqlite3_changes(local_db_handle);
11090         if (rc == 0) {
11091                 EM_DEBUG_LOG(" no matched mail found...");
11092                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11093                 goto FINISH_OFF;
11094         }
11095
11096         ret = true;
11097
11098 FINISH_OFF:
11099         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11100
11101         if (hStmt != NULL) {
11102                 rc = sqlite3_finalize(hStmt);
11103                 if (rc != SQLITE_OK) {
11104                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11105                         error = EMAIL_ERROR_DB_FAILURE;
11106                 }
11107         }
11108
11109         if (err_code != NULL)
11110         *err_code = error;
11111
11112         EM_DEBUG_FUNC_END("ret [%d]", ret);
11113         return ret;
11114 }
11115
11116 INTERNAL_FUNC int emstorage_update_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int transaction, int *err_code)
11117 {
11118         EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], transaction[%d], err_code[%p]", attachment_tbl, transaction, err_code);
11119
11120         int rc, ret = false, field_idx = 0;
11121         int error = EMAIL_ERROR_NONE;
11122         DB_STMT hStmt = NULL;
11123         char sql_query_string[QUERY_SIZE] = {0, };
11124
11125         if (!attachment_tbl) {
11126                 EM_DEBUG_EXCEPTION(" attachment_tbl[%p] ", attachment_tbl);
11127                 if (err_code)
11128                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11129                 return false;
11130         }
11131
11132         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11133
11134         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11135
11136         SNPRINTF(sql_query_string, sizeof(sql_query_string),
11137                 "UPDATE mail_attachment_tbl SET  "
11138                 "  attachment_name = ?"
11139                 ", attachment_path =  ?"
11140                 ", content_id = ?"
11141                 ", attachment_size = ?"
11142                 ", mail_id = ?"
11143                 ", account_id = ?"
11144                 ", mailbox_id = ?"
11145                 ", attachment_save_status = ?"
11146                 ", attachment_drm_type = ?"
11147                 ", attachment_drm_method = ?"
11148                 ", attachment_inline_content_status = ? "
11149                 ", attachment_mime_type = ? "
11150                 " WHERE attachment_id = ?;");
11151
11152
11153         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11154
11155         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11156                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11157
11158         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
11159         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
11160         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->content_id, 0, CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL);
11161         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_size);
11162         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->mail_id);
11163         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->account_id);
11164         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->mailbox_id);
11165         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_save_status);
11166         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_drm_type);
11167         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_drm_method);
11168         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_inline_content_status);
11169         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_mime_type, 0, ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL);
11170         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_id);
11171
11172
11173         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11174         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
11175                 ("sqlite3_step fail:%d", rc));
11176         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11177                 ("sqlite3_step fail:%d", rc));
11178
11179         rc = sqlite3_changes(local_db_handle);
11180         if (rc == 0) {
11181                 EM_DEBUG_LOG(" no matched mail found...");
11182                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11183                 goto FINISH_OFF;
11184         }
11185
11186         ret = true;
11187
11188 FINISH_OFF:
11189         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11190         if (hStmt != NULL) {
11191                 rc = sqlite3_finalize(hStmt);
11192                 if (rc != SQLITE_OK) {
11193                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11194                         error = EMAIL_ERROR_DB_FAILURE;
11195                 }
11196         }
11197
11198         if (err_code != NULL)
11199         *err_code = error;
11200
11201         EM_DEBUG_FUNC_END("ret [%d]", ret);
11202         return ret;
11203 }
11204
11205 INTERNAL_FUNC int emstorage_delete_attachment_on_db(char *multi_user_name, int attachment_id, int transaction, int *err_code)
11206 {
11207         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], transaction[%d], err_code[%p]", attachment_id, transaction, err_code);
11208
11209         if (attachment_id < 0) {
11210                 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
11211                 if (err_code != NULL)
11212                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11213                 return false;
11214         }
11215
11216         int ret = false;
11217         int error = EMAIL_ERROR_NONE;
11218         char sql_query_string[QUERY_SIZE] = {0, };
11219         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11220
11221         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11222
11223         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
11224
11225         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11226         if (error != EMAIL_ERROR_NONE) {
11227                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11228                         goto FINISH_OFF;
11229         }
11230
11231         ret = true;
11232
11233 FINISH_OFF:
11234         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11235
11236         if (err_code)
11237                 *err_code = error;
11238
11239         EM_DEBUG_FUNC_END("ret [%d]", ret);
11240         return ret;
11241 }
11242
11243 INTERNAL_FUNC int emstorage_delete_all_attachments_of_mail(char *multi_user_name, int mail_id, int transaction, int *err_code)
11244 {
11245         EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
11246         int ret = false;
11247         int error = EMAIL_ERROR_NONE;
11248         char sql_query_string[QUERY_SIZE] = {0, };
11249         sqlite3 *local_db_handle = NULL;
11250
11251         if (mail_id <= 0) {
11252                 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
11253                 if (err_code != NULL)
11254                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11255                 return false;
11256         }
11257
11258         local_db_handle = emstorage_get_db_connection(multi_user_name);
11259
11260         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11261
11262         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
11263         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11264         if (error != EMAIL_ERROR_NONE) {
11265                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11266                         goto FINISH_OFF;
11267         }
11268
11269         ret = true;
11270
11271 FINISH_OFF:
11272         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11273
11274         if (err_code)
11275                 *err_code = error;
11276
11277         EM_DEBUG_FUNC_END("ret [%d]", ret);
11278         return ret;
11279 }
11280
11281 INTERNAL_FUNC int emstorage_delete_attachment_all_on_db(char *multi_user_name, int account_id, char *mailbox, int transaction, int *err_code)
11282 {
11283         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox, transaction, err_code);
11284
11285         int error = EMAIL_ERROR_NONE;
11286         int ret = false;
11287         char sql_query_string[QUERY_SIZE] = {0, };
11288         char *replaced_mailbox = NULL;
11289         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11290
11291         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11292
11293         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl");
11294
11295         if (account_id != ALL_ACCOUNT) /*  '0' means all account */
11296                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE account_id = %d", account_id);
11297
11298         if (mailbox)    /*  NULL means all mailbox_name */ {
11299                 if (strstr(mailbox, "'")) {
11300                         replaced_mailbox = em_replace_all_string(mailbox, "'", "''");
11301                 } else {
11302                         replaced_mailbox = strdup(mailbox);
11303                 }
11304
11305                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s mailbox_name = '%s'", account_id != ALL_ACCOUNT ? "AND" : "WHERE", replaced_mailbox);
11306                 EM_SAFE_FREE(replaced_mailbox); /*prevent 49434*/
11307         }
11308
11309         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11310         if (error != EMAIL_ERROR_NONE) {
11311                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11312                         goto FINISH_OFF;
11313         }
11314
11315         ret = true;
11316
11317 FINISH_OFF:
11318         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11319
11320         if (err_code != NULL)
11321                 *err_code = error;
11322
11323         EM_DEBUG_FUNC_END("ret [%d]", ret);
11324         return ret;
11325 }
11326
11327 INTERNAL_FUNC int emstorage_free_attachment(emstorage_attachment_tbl_t** attachment_tbl_list, int count, int *err_code)
11328 {
11329         EM_DEBUG_FUNC_BEGIN("attachment_tbl_list[%p], count[%d], err_code[%p]", attachment_tbl_list, count, err_code);
11330
11331         if (count > 0) {
11332                 if ((attachment_tbl_list == NULL) || (*attachment_tbl_list == NULL)) {
11333                         EM_DEBUG_LOG("Nothing to free: attachment_tbl_list[%p], count[%d]", attachment_tbl_list, count);
11334                         if (err_code != NULL)
11335                                 *err_code = EMAIL_ERROR_INVALID_PARAM;
11336                         return false;
11337                 }
11338
11339                 emstorage_attachment_tbl_t* p = *attachment_tbl_list;
11340                 int i;
11341
11342                 for (i = 0; i < count; i++) {
11343                         EM_SAFE_FREE(p[i].attachment_name);
11344                         EM_SAFE_FREE(p[i].attachment_path);
11345                         EM_SAFE_FREE(p[i].content_id);
11346                         EM_SAFE_FREE(p[i].attachment_mime_type);
11347 #ifdef __ATTACHMENT_OPTI__
11348                         EM_SAFE_FREE(p[i].section);
11349 #endif
11350                 }
11351
11352                 EM_SAFE_FREE(p);
11353                 *attachment_tbl_list = NULL;
11354         }
11355
11356         if (err_code != NULL)
11357                 *err_code = EMAIL_ERROR_NONE;
11358         EM_DEBUG_FUNC_END();
11359         return true;
11360 }
11361
11362 INTERNAL_FUNC int emstorage_begin_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11363 {
11364         EM_PROFILE_BEGIN(emStorageBeginTransaction);
11365         int ret = true;
11366
11367         _timedlock_shm_mutex(mapped_for_db_lock, 2);
11368
11369         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11370
11371         int rc;
11372         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN immediate;", NULL, NULL, NULL), rc);
11373         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11374                 ("SQL(BEGIN) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11375
11376         if (ret == false) {
11377                 if (err_code != NULL) *err_code = EMAIL_ERROR_DB_FAILURE;
11378         }
11379
11380         EM_PROFILE_END(emStorageBeginTransaction);
11381         EM_DEBUG_FUNC_END("ret [%d]", ret);
11382         return ret;
11383 }
11384
11385 INTERNAL_FUNC int emstorage_commit_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11386 {
11387         EM_DEBUG_FUNC_BEGIN();
11388         int ret = true;
11389         int rc;
11390
11391         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11392
11393         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
11394         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; }, ("SQL(END) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11395
11396         if (ret == false && err_code != NULL)
11397                 *err_code = EMAIL_ERROR_DB_FAILURE;
11398
11399         _unlockshm_mutex(mapped_for_db_lock);
11400
11401         EM_DEBUG_FUNC_END("ret [%d]", ret);
11402         return ret;
11403 }
11404
11405 INTERNAL_FUNC int emstorage_rollback_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11406 {
11407         EM_DEBUG_FUNC_BEGIN();
11408         int ret = true;
11409         int rc;
11410         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11411
11412         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "ROLLBACK;", NULL, NULL, NULL), rc);
11413         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11414                 ("SQL(ROLLBACK) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11415
11416         if (ret == false && err_code != NULL)
11417                 *err_code = EMAIL_ERROR_DB_FAILURE;
11418
11419         _unlockshm_mutex(mapped_for_db_lock);
11420
11421         EM_DEBUG_FUNC_END("ret [%d]", ret);
11422         return ret;
11423 }
11424
11425 INTERNAL_FUNC int emstorage_is_mailbox_full(char *multi_user_name, int account_id, email_mailbox_t *mailbox, int *result, int *err_code)
11426 {
11427         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], result[%p], err_code[%p]", account_id, mailbox, result, err_code);
11428
11429         if (account_id < FIRST_ACCOUNT_ID || !mailbox || !result) {
11430                 if (mailbox)
11431                         EM_DEBUG_EXCEPTION("Invalid Parameter. accoun_id[%d], mailbox[%p]", account_id, mailbox);
11432
11433                 if (err_code != NULL)
11434                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11435
11436                 return false;
11437         }
11438
11439         int ret = false;
11440         int error = EMAIL_ERROR_NONE;
11441         int mail_count = 0;
11442
11443         if (!emstorage_get_mail_count(multi_user_name, account_id, mailbox->mailbox_id, &mail_count, NULL, true, &error)) {
11444                 EM_DEBUG_EXCEPTION("emstorage_get_mail_count failed [%d]", error);
11445                 goto FINISH_OFF;
11446         }
11447
11448         if (mailbox) {
11449                 EM_DEBUG_LOG("mail_count[%d] mail_slot_size[%d]", mail_count, mailbox->mail_slot_size);
11450                 if (mail_count >= mailbox->mail_slot_size)
11451                         *result = true;
11452                 else
11453                         *result = false;
11454
11455                 ret = true;
11456         }
11457
11458         ret = true;
11459 FINISH_OFF:
11460
11461         if (err_code != NULL)
11462                 *err_code = error;
11463
11464         EM_DEBUG_FUNC_END("ret [%d]", ret);
11465         return ret;
11466 }
11467
11468 INTERNAL_FUNC int emstorage_clear_mail_data(char *multi_user_name, int transaction, int *err_code)
11469 {
11470         EM_DEBUG_FUNC_BEGIN("transaction[%d], err_code[%p]", transaction, err_code);
11471
11472         int ret = false;
11473         int error = EMAIL_ERROR_NONE;
11474         char sql_query_string[QUERY_SIZE] = {0, };
11475         const email_db_object_t* tables = _g_db_tables;
11476         const email_db_object_t* indexes = _g_db_indexes;
11477
11478         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11479         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11480
11481         if (!emstorage_delete_dir(MAILHOME, &error)) {
11482                 EM_DEBUG_EXCEPTION(" emstorage_delete_dir failed - %d", error);
11483
11484                 goto FINISH_OFF;
11485         }
11486
11487         mkdir(MAILHOME, DIRECTORY_PERMISSION);
11488         mkdir(MAILTEMP, DIRECTORY_PERMISSION);
11489         chmod(MAILTEMP, 0777);
11490
11491         /*  first clear index. */
11492         while (indexes->object_name) {
11493                 if (indexes->data_flag) {
11494                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP index %s", indexes->object_name);
11495                         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11496                         if (error != EMAIL_ERROR_NONE) {
11497                                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11498                                         goto FINISH_OFF;
11499                         }
11500                 }
11501                 indexes++;
11502         }
11503
11504         while (tables->object_name) {
11505                 if (tables->data_flag) {
11506                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP table %s", tables->object_name);
11507                         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11508                         if (error != EMAIL_ERROR_NONE) {
11509                                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11510                                         goto FINISH_OFF;
11511                         }
11512                 }
11513
11514                 tables++;
11515         }
11516         ret = true;
11517
11518 FINISH_OFF:
11519         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11520
11521         if (err_code != NULL)
11522                 *err_code = error;
11523
11524         EM_DEBUG_FUNC_END("ret [%d]", ret);
11525         return ret;
11526 }
11527 /*======================= DB File Utils =============================================*/
11528 #include <dirent.h>
11529 #include <sys/types.h>
11530 #define  DIR_SEPERATOR "/"
11531
11532 INTERNAL_FUNC char *emstorage_make_directory_path_from_file_path(char *file_name)
11533 {
11534         EM_DEBUG_FUNC_BEGIN("Filename [ %p ]", file_name);
11535         char delims[] = "/";
11536         char *result = NULL;
11537         gchar **token = NULL;
11538
11539         token = g_strsplit_set(file_name, delims, 1);
11540
11541         if (token && token[0]) {
11542                 EM_DEBUG_LOG_SEC(">>>> Directory_name [ %s ]", token[0]);
11543                 result = EM_SAFE_STRDUP(token[0]);
11544         } else
11545                 EM_DEBUG_LOG(">>>> No Need to create Directory");
11546
11547         g_strfreev(token);
11548
11549         return result;
11550 }
11551
11552 INTERNAL_FUNC int emstorage_get_save_name(char *multi_user_name, int account_id, int mail_id, int atch_id,
11553                                                                                         char *fname, char *move_buf, char *path_buf, int maxlen, int *err_code)
11554 {
11555         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], mail_id[%d], atch_id[%d], fname[%s], move_buf[%p], path_buf[%p], err_code[%p]", account_id, mail_id, atch_id, fname, move_buf, path_buf, err_code);
11556         EM_PROFILE_BEGIN(profile_emstorage_get_save_name);
11557
11558         int ret = false;
11559         int error = EMAIL_ERROR_NONE;
11560         char *dir_name = NULL;
11561         char create_dir[1024] = {0};
11562         char *temp_file = NULL;
11563         char *prefix_path = NULL;
11564         char *modified_fname = NULL;
11565
11566         if (!move_buf || !path_buf || account_id < FIRST_ACCOUNT_ID || mail_id < 0 || atch_id < 0) {
11567                 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d], fname[%p], move_buf[%p], path_buf[%p]", account_id, mail_id, atch_id, fname, move_buf, path_buf);
11568                 error = EMAIL_ERROR_INVALID_PARAM;
11569                 goto FINISH_OFF;
11570         }
11571
11572         snprintf(path_buf, 512, "%s", MAILHOME);
11573         snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, account_id);
11574
11575         if (mail_id > 0)
11576                 snprintf(path_buf+EM_SAFE_STRLEN(path_buf),   512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, mail_id);
11577
11578         if (atch_id > 0)
11579                 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, atch_id);
11580
11581         if (fname) {
11582                 temp_file = EM_SAFE_STRDUP(fname);
11583                 if (temp_file && strstr(temp_file, "/")) {
11584                         dir_name = emstorage_make_directory_path_from_file_path(temp_file);
11585                 }
11586         }
11587
11588         if (dir_name) {
11589                 snprintf(create_dir, sizeof(create_dir), "%s%s%s", path_buf, DIR_SEPERATOR, dir_name);
11590                 EM_DEBUG_LOG(">>>>> DIR PATH [%s]", create_dir);
11591                 mkdir(create_dir, DIRECTORY_PERMISSION);
11592                 EM_SAFE_FREE(dir_name);
11593         }
11594
11595         if (fname) {
11596                 EM_DEBUG_LOG_DEV(">>>>> fname [%s]", fname);
11597
11598                 /* Did not allow the slash */
11599                 modified_fname = reg_replace_new(fname, "/", "_");
11600                 EM_DEBUG_LOG("modified_fname : [%s]", modified_fname);
11601
11602                 if (modified_fname == NULL) modified_fname = g_strdup(fname);
11603
11604                 if (EM_SAFE_STRLEN(modified_fname) + EM_SAFE_STRLEN(path_buf) + strlen(DIR_SEPERATOR) > maxlen - 1) {
11605                         char *modified_name = NULL;
11606                         int remain_len  = (maxlen - 1) - EM_SAFE_STRLEN(path_buf) - strlen(DIR_SEPERATOR);
11607
11608                         if (remain_len <= 0) {
11609                                 error = EMAIL_ERROR_MAX_EXCEEDED;
11610                                 goto FINISH_OFF;
11611                         }
11612
11613                         if (remain_len > MAX_FILENAME) {
11614                                 remain_len = MAX_FILENAME;
11615                         }
11616
11617                         modified_name = em_shrink_filename(modified_fname, remain_len);
11618
11619                         if (!modified_name) {
11620                                 error = EMAIL_ERROR_MAX_EXCEEDED;
11621                                 goto FINISH_OFF;
11622                         }
11623
11624                         sprintf(path_buf+EM_SAFE_STRLEN(path_buf), "%s%s", DIR_SEPERATOR, modified_name);
11625                         EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11626                         EM_SAFE_FREE(modified_name);
11627                 } else {
11628                         if (EM_SAFE_STRLEN(modified_fname) > MAX_FILENAME - 1) {
11629                                 char *modified_name = NULL;
11630
11631                                 modified_name = em_shrink_filename(modified_fname, MAX_FILENAME);
11632                                 if (!modified_name) {
11633                                         error = EMAIL_ERROR_MAX_EXCEEDED;
11634                                         goto FINISH_OFF;
11635                                 }
11636
11637                                 sprintf(path_buf+EM_SAFE_STRLEN(path_buf), "%s%s", DIR_SEPERATOR, modified_name);
11638                                 EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11639                                 EM_SAFE_FREE(modified_name);
11640                         } else {
11641                                 sprintf(path_buf+EM_SAFE_STRLEN(path_buf), "%s%s", DIR_SEPERATOR, modified_fname);
11642                         }
11643                 }
11644         }
11645
11646         EM_DEBUG_LOG_SEC(">>>>> path_buf [%s]", path_buf);
11647
11648     if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11649                 error = emcore_get_container_path(multi_user_name, &prefix_path);
11650                 if (error != EMAIL_ERROR_NONE) {
11651                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11652                         goto FINISH_OFF;
11653                 }
11654         sprintf(move_buf, "%s/%s", prefix_path, path_buf);
11655         EM_DEBUG_LOG("move_buf : [%s]", move_buf);
11656     } else {
11657         sprintf(move_buf, "%s", path_buf);
11658         EM_DEBUG_LOG("move_buf : [%s]", move_buf);
11659     }
11660
11661         ret = true;
11662
11663 FINISH_OFF:
11664
11665         EM_SAFE_FREE(temp_file);
11666         EM_SAFE_FREE(prefix_path);
11667         EM_SAFE_FREE(modified_fname);
11668
11669         if (err_code != NULL)
11670                 *err_code = error;
11671
11672         EM_PROFILE_END(profile_emstorage_get_save_name);
11673         EM_DEBUG_FUNC_END("ret [%d]", ret);
11674         return ret;
11675 }
11676
11677 /*
11678 INTERNAL_FUNC int emstorage_get_dele_name(char *multi_user_name, int account_id, int mail_id, int atch_id, char *fname, char *name_buf, int *err_code)
11679 {
11680         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], fname[%p], name_buf[%p], err_code[%p]", account_id, mail_id, atch_id, fname, name_buf, err_code);
11681
11682         if (!name_buf || account_id < FIRST_ACCOUNT_ID) {
11683                 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d], fname[%p], name_buf[%p]", account_id, mail_id, atch_id, fname, name_buf);
11684                 if (err_code != NULL)
11685                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11686                 return false;
11687         }
11688
11689         sprintf(name_buf+EM_SAFE_STRLEN(name_buf),      "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
11690
11691         if (mail_id > 0)
11692                 sprintf(name_buf+EM_SAFE_STRLEN(name_buf),      "%s%d", DIR_SEPERATOR, mail_id);
11693         else
11694                 goto FINISH_OFF;
11695
11696         if (atch_id > 0)
11697                 sprintf(name_buf+EM_SAFE_STRLEN(name_buf),      "%s%d", DIR_SEPERATOR, atch_id);
11698         else
11699                 goto FINISH_OFF;
11700
11701 FINISH_OFF:
11702         sprintf(name_buf+EM_SAFE_STRLEN(name_buf),      ".DELE");
11703
11704         EM_DEBUG_FUNC_END();
11705         return true;
11706 }
11707 */
11708
11709 INTERNAL_FUNC int emstorage_create_dir(char *multi_user_name, int account_id, int mail_id, int atch_id, int *err_code)
11710 {
11711         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], err_code[%p]", account_id, mail_id, atch_id, err_code);
11712         EM_PROFILE_BEGIN(profile_emcore_save_create_dir);
11713         int ret = false;
11714         int error = EMAIL_ERROR_NONE;
11715         char buf[512];
11716         struct stat sbuf;
11717         char *prefix_path = NULL;
11718         char errno_buf[ERRNO_BUF_SIZE] = {0};
11719
11720         memset(buf, 0x00, sizeof(buf));
11721
11722     if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11723                 error = emcore_get_container_path(multi_user_name, &prefix_path);
11724                 if (error != EMAIL_ERROR_NONE) {
11725                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11726                         goto FINISH_OFF;
11727                 }
11728         } else {
11729                 prefix_path = strdup("");
11730         }
11731
11732         if (account_id >= FIRST_ACCOUNT_ID) {
11733                 SNPRINTF(buf, sizeof(buf), "%s%s%s%s%d", prefix_path,
11734                                                                                                 DIR_SEPERATOR,
11735                                                                                                 MAILHOME,
11736                                                                                                 DIR_SEPERATOR,
11737                                                                                                 account_id);
11738
11739                 if (stat(buf, &sbuf) == 0) {
11740                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11741                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11742                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11743                                 goto FINISH_OFF;
11744                         }
11745                 } else {
11746                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11747                                 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11748                                 EM_DEBUG_EXCEPTION("mkdir failed: %s", EM_STRERROR(errno_buf));
11749                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11750                                 if (errno == 28)
11751                                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11752                                 goto FINISH_OFF;
11753                         }
11754
11755                         if (account_id == EML_FOLDER) {
11756                                 chmod(buf, 0777);
11757                         }
11758                 }
11759         }
11760
11761         if (mail_id > 0) {
11762                 int space_left_in_buffer = sizeof(buf) - EM_SAFE_STRLEN(buf);
11763
11764                 if (account_id < FIRST_ACCOUNT_ID) {
11765                         EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11766                         error = EMAIL_ERROR_INVALID_PARAM;
11767                         goto FINISH_OFF;
11768                 }
11769
11770                 if (space_left_in_buffer + 10 > sizeof(buf)) {
11771                         EM_DEBUG_EXCEPTION("Buffer overflowed");
11772                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11773                         goto FINISH_OFF;
11774                 }
11775
11776                 SNPRINTF(buf+EM_SAFE_STRLEN(buf), space_left_in_buffer, "%s%d", DIR_SEPERATOR, mail_id);
11777
11778                 if (stat(buf, &sbuf) == 0) {
11779                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11780                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11781                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11782                                 goto FINISH_OFF;
11783                         }
11784                 } else {
11785                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11786                                 EM_DEBUG_EXCEPTION("mkdir failed [%s]", buf);
11787                                 EM_DEBUG_EXCEPTION("mkdir failed [%d][%s]", errno, EM_STRERROR(errno_buf));
11788                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11789                                 if (errno == 28)
11790                                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11791                                 goto FINISH_OFF;
11792                         }
11793
11794                         if (account_id == EML_FOLDER) {
11795                                 chmod(buf, 0777);
11796                         }
11797                 }
11798         }
11799
11800         if (atch_id > 0) {
11801                 if (account_id < FIRST_ACCOUNT_ID || mail_id <= 0) {
11802                         EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11803                         error = EMAIL_ERROR_INVALID_PARAM;
11804                         goto FINISH_OFF;
11805                 }
11806
11807                 SNPRINTF(buf+EM_SAFE_STRLEN(buf), sizeof(buf)-(EM_SAFE_STRLEN(buf)+1), "%s%d", DIR_SEPERATOR, atch_id);
11808
11809                 if (stat(buf, &sbuf) == 0) {
11810                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11811                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11812                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11813                                 goto FINISH_OFF;
11814                         }
11815                 } else {
11816                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11817                                 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11818                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11819                                 if (errno == 28)
11820                                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11821                                 goto FINISH_OFF;
11822                         }
11823
11824                         if (account_id == EML_FOLDER) {
11825                                 chmod(buf, 0777);
11826                         }
11827                 }
11828         }
11829
11830         ret = true;
11831
11832 FINISH_OFF:
11833
11834         EM_SAFE_FREE(prefix_path);
11835
11836         if (err_code != NULL)
11837                 *err_code = error;
11838
11839         EM_PROFILE_END(profile_emcore_save_create_dir);
11840         EM_DEBUG_FUNC_END("ret [%d]", ret);
11841         return ret;
11842 }
11843
11844 INTERNAL_FUNC int emstorage_copy_file(char *src_file, char *dst_file, int sync_status, int *err_code)
11845 {
11846         EM_DEBUG_FUNC_BEGIN("src_file[%s], dst_file[%s], err_code[%p]", src_file, dst_file, err_code);
11847         EM_DEBUG_LOG("Using the fsync function");
11848         int ret = false;
11849         int error = EMAIL_ERROR_NONE;
11850         struct stat st_buf;
11851
11852         int fp_src = 0;
11853         int fp_dst = 0;
11854         int nread = 0;
11855         char buf[FILE_MAX_BUFFER_SIZE] = {0};
11856         char errno_buf[ERRNO_BUF_SIZE] = {0};
11857
11858         if (!src_file || !dst_file) {
11859                 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
11860
11861                 error = EMAIL_ERROR_INVALID_PARAM;
11862                 goto FINISH_OFF;
11863         }
11864
11865         if (stat(src_file, &st_buf) < 0) {
11866                 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", src_file);
11867
11868                 error = EMAIL_ERROR_SYSTEM_FAILURE;             /* EMAIL_ERROR_INVALID_PATH; */
11869                 goto FINISH_OFF;
11870         }
11871
11872         error = em_open(src_file, O_RDONLY, 0, &fp_src);
11873         if (error != EMAIL_ERROR_NONE) {
11874                         EM_DEBUG_EXCEPTION(">>>> Source Fail em_open %s Failed: %d", src_file, error);
11875                         goto FINISH_OFF;
11876         }
11877
11878         error = em_open(dst_file, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, &fp_dst); /*prevent 24474*/
11879         if (error != EMAIL_ERROR_NONE) {
11880                         EM_DEBUG_EXCEPTION(">>>> Destination Fail em_open %s:  %d", dst_file, error);
11881                         goto FINISH_OFF;
11882         }
11883
11884         memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
11885
11886         while ((nread = read(fp_src, buf, FILE_MAX_BUFFER_SIZE)) > 0) {
11887                 if (nread > 0 && nread <= FILE_MAX_BUFFER_SIZE) {
11888                         EM_DEBUG_LOG("Nread Value [%d]", nread);
11889                         char *buf_ptr;
11890                         ssize_t byte_written = 0;
11891                         size_t remain_byte = nread;
11892                         buf_ptr = buf;
11893                         errno = 0;
11894
11895                         while (remain_byte > 0 && buf_ptr && errno == 0) {
11896                                 byte_written = write(fp_dst, buf_ptr, remain_byte);
11897
11898                                 if (byte_written < 0) {
11899                                         /* interrupted by a signal */
11900                                         if (errno == EINTR) {
11901                                                 errno = 0;
11902                                                 continue;
11903                                         }
11904
11905                                         EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
11906                                         error = EMAIL_ERROR_UNKNOWN;
11907                                         goto FINISH_OFF;
11908                                 }
11909                                 EM_DEBUG_LOG("NWRITTEN [%d]", byte_written);
11910                                 remain_byte -= byte_written;
11911                                 buf_ptr += byte_written;
11912                         }
11913                 }
11914
11915                 memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
11916         }
11917
11918         ret = true;
11919
11920 FINISH_OFF:
11921         EM_SAFE_CLOSE(fp_src);
11922
11923         if (fp_dst >= 0) { /*prevent 24474*/
11924                 if (sync_status) {
11925                         EM_DEBUG_LOG("Before fsync");
11926                         fsync(fp_dst);
11927                 }
11928                 close(fp_dst);
11929         }
11930
11931         if (nread < 0 || error == EMAIL_ERROR_UNKNOWN)
11932                 remove(dst_file);
11933
11934         if (err_code != NULL)
11935                 *err_code = error;
11936         EM_DEBUG_FUNC_END("ret [%d]", ret);
11937         return ret;
11938 }
11939 /* create Directory if user has deleted [deepam.p@samsung.com] */
11940 INTERNAL_FUNC void emstorage_create_dir_if_delete()
11941 {
11942         EM_DEBUG_FUNC_BEGIN();
11943
11944         mkdir(DATA_PATH, DIRECTORY_PERMISSION);
11945         mkdir(EMAILPATH, DIRECTORY_PERMISSION);
11946         mkdir(MAILHOME, DIRECTORY_PERMISSION);
11947         mkdir(MAILTEMP, DIRECTORY_PERMISSION);
11948         chmod(MAILTEMP, 0777);
11949
11950         EM_DEBUG_FUNC_END();
11951 }
11952
11953 static int _get_temp_file_name(char **filename, int *err_code)
11954 {
11955         EM_DEBUG_FUNC_BEGIN("filename[%p], err_code[%p]", filename, err_code);
11956
11957         int ret = false;
11958         int error = EMAIL_ERROR_NONE;
11959
11960         if (filename == NULL) {
11961                 EM_DEBUG_EXCEPTION(" filename[%p]", filename);
11962                 error = EMAIL_ERROR_INVALID_PARAM;
11963                 goto FINISH_OFF;
11964         }
11965
11966         char tempname[512] = {0x00, };
11967         struct timeval tv;
11968
11969         gettimeofday(&tv, NULL);
11970         srand(tv.tv_usec);
11971
11972         SNPRINTF(tempname, sizeof(tempname), "%s%c%d", MAILTEMP, '/', rand());
11973
11974         char *p = EM_SAFE_STRDUP(tempname);
11975         if (p == NULL) {
11976                 EM_DEBUG_EXCEPTION(" strdup failed...");
11977                 error = EMAIL_ERROR_OUT_OF_MEMORY;
11978                 goto FINISH_OFF;
11979         }
11980
11981         *filename = p;
11982
11983         ret = true;
11984
11985 FINISH_OFF:
11986         if (err_code != NULL)
11987                 *err_code = error;
11988
11989         EM_DEBUG_FUNC_END("ret [%d]", ret);
11990         return ret;
11991 }
11992
11993 INTERNAL_FUNC int emstorage_add_content_type(char *file_path, char *char_set, int *err_code)
11994 {
11995         EM_DEBUG_FUNC_BEGIN("File path [ %p ]  Character Set [ %p ] err_code [ %p]", file_path, char_set, err_code);
11996
11997         EM_IF_NULL_RETURN_VALUE(file_path, false);
11998         EM_IF_NULL_RETURN_VALUE(char_set, false);
11999         EM_IF_NULL_RETURN_VALUE(err_code, false);
12000
12001         char *buf =  NULL;
12002         char *buf1 = NULL;
12003         struct stat st_buf;
12004         int buf_size = 0;
12005         char *low_char_set = NULL;
12006         char *match_str = NULL;
12007         int nwritten = 0;
12008         int ret = false;
12009         int error = EMAIL_ERROR_NONE;
12010         int data_count_to_written = 0;
12011         char *temp_file_name = NULL;
12012         int err = 0;
12013         FILE* fp_src = NULL;
12014         FILE* fp_dest = NULL;
12015         int nread = 0;
12016
12017
12018         if (stat(file_path, &st_buf) < 0) {
12019                 EM_DEBUG_EXCEPTION_SEC(" stat(\"%s\") failed...", file_path);
12020                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12021                 goto FINISH_OFF;
12022         }
12023
12024         buf_size =  st_buf.st_size;
12025         EM_DEBUG_LOG(">>>> File Size [ %d ] ", buf_size);
12026         buf = (char *)calloc(1, buf_size+1);
12027
12028         if (!buf) {
12029                 EM_DEBUG_LOG(">>> Memory cannot be allocated ");
12030                 goto FINISH_OFF;
12031         }
12032
12033         error = em_fopen(file_path, "rb", &fp_src);
12034         if (error != EMAIL_ERROR_NONE || fp_src == NULL) {
12035                 EM_DEBUG_EXCEPTION_SEC(" file_path fopen failed - %s [%d]", file_path, error);
12036                 goto FINISH_OFF;
12037         }
12038
12039         if ((nread = fread(buf, 1, buf_size, fp_src)) > 0) {
12040                 if (nread > 0 && nread <= buf_size) {
12041                         EM_DEBUG_LOG(">>>> Nread Value [ %d ] ", nread);
12042
12043                         /**
12044                           *   1.Add check for whether content type is there.
12045                           *   2. If not based on the character set, Append it in File
12046                           **/
12047
12048                         low_char_set = calloc(1, EM_SAFE_STRLEN(char_set) + strlen(" \" /></head>") +1); /*prevent 34359*/
12049                         if (low_char_set == NULL) {
12050                                 EM_DEBUG_EXCEPTION("calloc failed");
12051                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12052                                 goto FINISH_OFF;
12053                         }
12054
12055                         strncat(low_char_set, char_set, EM_SAFE_STRLEN(char_set));
12056                         EM_DEBUG_LOG(">>>> CHAR SET [ %s ] ", low_char_set);
12057                         strncat(low_char_set, " \" /></head>", strlen(" \" /></head>")); /*prevent 34359*/
12058                         EM_DEBUG_LOG(">>> CHARSET [ %s ] ", low_char_set);
12059                         match_str = strstr(buf, CONTENT_TYPE_DATA);
12060
12061                         if (match_str == NULL) {
12062                                 EM_DEBUG_LOG(">>>>emstorage_add_content_type 3 ");
12063                                 if (fp_src != NULL) {
12064                                         fclose(fp_src);
12065                                         fp_src = NULL;
12066                                 }
12067                                 data_count_to_written = EM_SAFE_STRLEN(low_char_set)+strlen(CONTENT_DATA)+1; /*prevent 34359*/
12068                                 buf1 = (char *)calloc(1, data_count_to_written);
12069
12070                                 if (buf1) {
12071                                         strncat(buf1, CONTENT_DATA, strlen(CONTENT_DATA)); /*prevent 34359*/
12072                                         EM_DEBUG_LOG(">>>>> BUF 1 [ %s ] ", buf1);
12073                                         strncat(buf1, low_char_set, EM_SAFE_STRLEN(low_char_set));
12074                                         EM_DEBUG_LOG(">>>> HTML TAG DATA  [ %s ] ", buf1);
12075
12076                                         /* 1. Create a temporary file name */
12077                                         if (!_get_temp_file_name(&temp_file_name, &err)) {
12078                                                         EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed - %d", err);
12079                                                         if (err_code != NULL) *err_code = err;
12080                                                         goto FINISH_OFF;
12081                                         }
12082                                         EM_DEBUG_LOG_SEC(">>>>>>> TEMP APPEND FILE PATH [ %s ] ", temp_file_name);
12083
12084                                         /* Open the Temp file in Append mode */
12085                                         error = em_fopen(temp_file_name, "ab", &fp_dest);
12086                                         if (error != EMAIL_ERROR_NONE) {
12087                                                 EM_DEBUG_EXCEPTION_SEC(" fopen failed - %s [%d]", temp_file_name, error);
12088                                                 goto FINISH_OFF;
12089                                         }
12090
12091                                         /* 2. write the Latest data */
12092                                         nwritten = fwrite(buf1, data_count_to_written-1, 1, fp_dest);
12093
12094                                         if (nwritten > 0) {
12095                                                 EM_DEBUG_LOG(" Latest Data  : [%d ] bytes written ", nwritten);
12096                                                 nwritten = 0;
12097                                                 /* 3. Append old data */
12098                                                 nwritten = fwrite(buf, nread-1, 1, fp_dest);
12099
12100                                                 if (nwritten <= 0) {
12101                                                         EM_DEBUG_EXCEPTION(" Error Occured while writing Old data : [%d ] bytes written ", nwritten);
12102                                                         error = EMAIL_ERROR_SYSTEM_FAILURE;
12103                                                         goto FINISH_OFF;
12104                                                 } else {
12105                                                         EM_DEBUG_LOG(">>>> OLD data appended [ %d ] ", nwritten);
12106
12107                                                         if (!emstorage_move_file(temp_file_name, file_path, false, &err)) {
12108                                                                 EM_DEBUG_EXCEPTION(" emstorage_move_file failed - %d", err);
12109                                                                 goto FINISH_OFF;
12110                                                         }
12111                                                 }
12112
12113                                         } else {
12114                                                 EM_DEBUG_EXCEPTION(" Error Occured while writing New data : [%d ] bytes written ", nwritten);
12115                                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12116                                                 goto FINISH_OFF;
12117                                         }
12118                                 }
12119                         }
12120                 }
12121         }
12122
12123         ret = true;
12124 FINISH_OFF:
12125
12126         EM_SAFE_FREE(buf);
12127         EM_SAFE_FREE(buf1);
12128         EM_SAFE_FREE(low_char_set);
12129         EM_SAFE_FREE(temp_file_name);
12130
12131         if (fp_src != NULL) {
12132                 fclose(fp_src);
12133                 fp_src = NULL;
12134         }
12135
12136         if (fp_dest != NULL) {
12137                 fclose(fp_dest);
12138                 fp_dest = NULL;
12139         }
12140
12141         if (err_code)
12142                 *err_code = error;
12143
12144         EM_DEBUG_FUNC_END("ret [%d]", ret);
12145         return ret;
12146
12147 }
12148
12149 INTERNAL_FUNC int emstorage_move_file(char *src_file, char *dst_file, int sync_status, int *err_code)
12150 {
12151         EM_DEBUG_FUNC_BEGIN("src_file[%p], dst_file[%p], err_code[%p]", src_file, dst_file, err_code);
12152
12153         int ret = false;
12154         int error = EMAIL_ERROR_NONE;
12155         char errno_buf[ERRNO_BUF_SIZE] = {0};
12156
12157         if (src_file == NULL || dst_file == NULL) {
12158                 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
12159                 error = EMAIL_ERROR_INVALID_PARAM;
12160                 goto FINISH_OFF;
12161         }
12162
12163         EM_DEBUG_LOG_SEC("src_file[%s], dst_file[%s]", src_file, dst_file);
12164
12165         if (strcmp(src_file, dst_file) != 0) {
12166                 if (rename(src_file, dst_file) != 0) {
12167                         /* EM_DEBUG_EXCEPTION("%s", strerror(errno)); */
12168                         if (errno == EXDEV) {   /* oldpath and newpath are not on the same mounted file system.  (Linux permits a file system to be mounted at multiple points,  but  rename() */
12169                                 /*  does not work across different mount points, even if the same file system is mounted on both.)       */
12170                                 EM_DEBUG_LOG("oldpath and newpath are not on the same mounted file system.");
12171                                 if (!emstorage_copy_file(src_file, dst_file, sync_status, &error)) {
12172                                         EM_DEBUG_EXCEPTION("emstorage_copy_file failed - %d", error);
12173                                         goto FINISH_OFF;
12174                                 }
12175                                 remove(src_file);
12176                                 EM_DEBUG_LOG("src[%s] removed", src_file);
12177                         } else {
12178                                 if (errno == ENOENT) {
12179                                         struct stat temp_file_stat;
12180                                         if (stat(src_file, &temp_file_stat) < 0) {
12181                                                 EM_DEBUG_EXCEPTION("no src file found [%s] : %s", src_file, EM_STRERROR(errno_buf));
12182                                         }
12183                                         if (stat(dst_file, &temp_file_stat) < 0)
12184                                                 EM_DEBUG_EXCEPTION("no dst file found [%s] : %s", dst_file, EM_STRERROR(errno_buf));
12185
12186                                         error = EMAIL_ERROR_FILE_NOT_FOUND;
12187                                         goto FINISH_OFF;
12188                                 } else {
12189                                         EM_DEBUG_EXCEPTION("rename failed: %s", EM_STRERROR(errno_buf));
12190                                         error = EMAIL_ERROR_SYSTEM_FAILURE;
12191                                         goto FINISH_OFF;
12192                                 }
12193                         }
12194                 }
12195         } else {
12196                 EM_DEBUG_LOG("src[%s] = dst[%s]", src_file, dst_file);
12197         }
12198
12199         ret = true;
12200
12201 FINISH_OFF:
12202         if (err_code != NULL)
12203                 *err_code = error;
12204
12205         EM_DEBUG_FUNC_END("ret [%d]", ret);
12206         return ret;
12207 }
12208
12209 INTERNAL_FUNC int emstorage_delete_file(char *src_file, int *err_code)
12210 {
12211         EM_DEBUG_FUNC_BEGIN("src_file[%p], err_code[%p]", src_file, err_code);
12212
12213         int ret = false;
12214         int error = EMAIL_ERROR_NONE;
12215
12216         if (src_file == NULL) {
12217                 EM_DEBUG_EXCEPTION(" src_file[%p]", src_file);
12218
12219                 error = EMAIL_ERROR_INVALID_PARAM;
12220                 goto FINISH_OFF;
12221         }
12222
12223         if (remove(src_file) != 0) {
12224                 if (errno != ENOENT) {
12225                         EM_DEBUG_EXCEPTION(" remove failed - %d", errno);
12226
12227                         error = EMAIL_ERROR_SYSTEM_FAILURE;
12228                         goto FINISH_OFF;
12229                 } else {
12230                         EM_DEBUG_EXCEPTION(" no file found...");
12231
12232                         error = EMAIL_ERROR_FILE_NOT_FOUND;
12233                 }
12234         }
12235
12236         ret = true;
12237
12238 FINISH_OFF:
12239         if (err_code != NULL)
12240                 *err_code = error;
12241
12242         EM_DEBUG_FUNC_END("ret [%d]", ret);
12243         return ret;
12244 }
12245
12246
12247 INTERNAL_FUNC int emstorage_delete_dir(char *src_dir, int *err_code)
12248 {
12249         EM_DEBUG_FUNC_BEGIN("src_dir[%p], err_code[%p]", src_dir, err_code);
12250
12251         if (src_dir == NULL) {
12252                 EM_DEBUG_EXCEPTION("src_dir[%p]", src_dir);
12253
12254                 if (err_code != NULL)
12255                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12256                 return false;
12257         }
12258
12259         int error = EMAIL_ERROR_NONE;
12260
12261         DIR *dirp;
12262         struct dirent *dp;
12263         struct stat sbuf;
12264         char buf[512];
12265
12266         dirp = opendir(src_dir);
12267
12268         if (dirp == NULL) {
12269                 if (errno == ENOENT) {
12270                         EM_DEBUG_EXCEPTION("directory[%s] does not exist...", src_dir);
12271                         if (err_code != NULL)
12272                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12273                         return true;
12274                 } else {
12275                         EM_DEBUG_EXCEPTION("opendir failed [%s] [%d]", src_dir, errno);
12276                         if (err_code != NULL)
12277                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12278                         return false;
12279                 }
12280         }
12281
12282         while ((dp = readdir(dirp))) {
12283                 if (strncmp(dp->d_name, ".", 1) == 0 || strncmp(dp->d_name, "..", 2) == 0) /* prevent 34360 */
12284                         continue;
12285
12286                 SNPRINTF(buf, sizeof(buf), "%s/%s", src_dir, dp->d_name);
12287
12288                 if (lstat(buf, &sbuf) == 0 || stat(buf, &sbuf) == 0) {
12289                         /*  check directory */
12290                         if ((sbuf.st_mode & S_IFMT) == S_IFDIR) {       /*  directory */
12291                                 /*  recursive call */
12292                                 if (!emstorage_delete_dir(buf, &error)) {
12293                                         closedir(dirp);
12294                                         if (err_code != NULL)
12295                                                 *err_code = error;
12296                                         return false;
12297                                 }
12298                         } else {        /*  file */
12299                                 if (remove(buf) < 0) {
12300                                         EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", buf, errno);
12301                                         closedir(dirp);
12302                                         if (err_code != NULL)
12303                                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12304                                         return false;
12305                                 }
12306                         }
12307                 } else
12308                         EM_DEBUG_EXCEPTION("content does not exist...");
12309         }
12310
12311         closedir(dirp);
12312
12313         EM_DEBUG_LOG_DEV("remove direcotory [%s]", src_dir);
12314
12315         /* EM_DEBUG_FUNC_BEGIN(); */
12316
12317         if (remove(src_dir) < 0) {
12318                 EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", src_dir, errno);
12319                 if (err_code != NULL)
12320                         *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12321                 return false;
12322         }
12323
12324         if (err_code != NULL)
12325                 *err_code = error;
12326
12327         return true;
12328 }
12329
12330 /* faizan.h@samsung.com */
12331 INTERNAL_FUNC int emstorage_update_server_uid(char *multi_user_name,
12332                                                                                                 int mail_id,
12333                                                                                                 char *old_server_uid,
12334                                                                                                 char *new_server_uid,
12335                                                                                                 int *err_code)
12336 {
12337         EM_DEBUG_FUNC_BEGIN("new_server_uid[%s], old_server_uid[%s]", new_server_uid, old_server_uid);
12338         int ret = false;
12339         int error = EMAIL_ERROR_NONE;
12340         int transaction = true;
12341         int temp_strlen = 0;
12342         int and_operation = 0;
12343         char sql_query_string[QUERY_SIZE] = {0, };
12344         char conditional_clause_string[QUERY_SIZE] = {0};
12345
12346         if ((mail_id <= 0 || !old_server_uid) && !new_server_uid) {
12347                 EM_DEBUG_EXCEPTION("Invalid parameters");
12348                 if (err_code)
12349                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12350                 return false;
12351         }
12352
12353         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12354         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12355
12356         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE ");
12357
12358         if (mail_id > 0) {
12359                 temp_strlen = strlen(conditional_clause_string);
12360                 SNPRINTF(conditional_clause_string + temp_strlen, sizeof(conditional_clause_string) - temp_strlen,
12361                                         "mail_id = %d ", mail_id);
12362                 and_operation = 1;
12363         }
12364
12365         if (old_server_uid) {
12366                 temp_strlen = strlen(conditional_clause_string);
12367                 if (!and_operation) {
12368                         sqlite3_snprintf(sizeof(conditional_clause_string) - temp_strlen, conditional_clause_string + temp_strlen,
12369                                                         "server_mail_id = '%q'", old_server_uid);
12370                 } else {
12371                         sqlite3_snprintf(sizeof(conditional_clause_string) - temp_strlen, conditional_clause_string + temp_strlen,
12372                                                         "and server_mail_id = '%q'", old_server_uid);
12373                 }
12374         }
12375
12376         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
12377                                                 "UPDATE mail_tbl SET server_mail_id = '%q' %s", new_server_uid, conditional_clause_string);
12378
12379         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12380         if (error != EMAIL_ERROR_NONE) {
12381                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12382                 goto FINISH_OFF;
12383         }
12384
12385         ret = true;
12386
12387 FINISH_OFF:
12388
12389         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12390
12391         if (err_code != NULL)
12392                 *err_code = error;
12393
12394         EM_DEBUG_FUNC_END("ret [%d]", ret);
12395         return ret;
12396
12397 }
12398
12399 INTERNAL_FUNC int emstorage_update_read_mail_uid(char *multi_user_name, int mail_id, char *new_server_uid, char *mbox_name, int *err_code)
12400 {
12401         EM_DEBUG_FUNC_BEGIN_SEC("mail_id[%d], new_server_uid[%s], mbox_name[%s]", mail_id, new_server_uid, mbox_name);
12402
12403         int ret = false;
12404         int error = EMAIL_ERROR_NONE;
12405         char sql_query_string[QUERY_SIZE] = {0, };
12406         int transaction = true;
12407
12408         if (!mail_id || !new_server_uid || !mbox_name) {
12409                 EM_DEBUG_EXCEPTION("Invalid parameters");
12410                 if (err_code != NULL)
12411                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12412                 return false;
12413         }
12414
12415         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12416         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12417
12418
12419         SNPRINTF(sql_query_string, sizeof(sql_query_string),
12420                  "UPDATE mail_read_mail_uid_tbl SET server_uid=\'%s\', mailbox_id=\'%s\', mailbox_name=\'%s\' WHERE local_uid=%d ", new_server_uid, mbox_name, mbox_name, mail_id);
12421
12422         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12423         if (error != EMAIL_ERROR_NONE) {
12424                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12425                         goto FINISH_OFF;
12426         }
12427
12428         ret     = true;
12429
12430 FINISH_OFF:
12431
12432         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12433
12434         if (err_code != NULL)
12435                 *err_code = error;
12436
12437         EM_DEBUG_FUNC_END("ret [%d]", ret);
12438         return ret;
12439
12440 }
12441
12442 INTERNAL_FUNC int emstorage_update_save_status(char *multi_user_name, int account_id, int *err_code)
12443 {
12444         EM_DEBUG_FUNC_BEGIN();
12445
12446         int ret = false;
12447         int transaction = true;
12448         int error = EMAIL_ERROR_NONE;
12449         char sql_query_string[QUERY_SIZE] = {0,};
12450
12451         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12452
12453         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12454
12455         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12456
12457         if (account_id <= ALL_ACCOUNT)
12458                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET save_status = %d WHERE (save_status = %d or save_status = %d)", EMAIL_MAIL_STATUS_NONE, EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12459         else
12460                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET save_status = %d WHERE (save_status = %d or save_status = %d) and account_id = %d ", EMAIL_MAIL_STATUS_NONE, EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED, account_id);
12461
12462         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12463         if (error != EMAIL_ERROR_NONE) {
12464                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12465                         goto FINISH_OFF;
12466         }
12467
12468         ret = true;
12469
12470 FINISH_OFF:
12471         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12472
12473         if (err_code != NULL)
12474                 *err_code = error;
12475
12476         EM_DEBUG_FUNC_END("ret [%d]", ret);
12477         return ret;
12478
12479 }
12480
12481 int emstorage_get_unread_mailid(char *multi_user_name, int account_id, int vip_mode, int **mail_ids, int *mail_number, int *err_code)
12482 {
12483         EM_DEBUG_FUNC_BEGIN();
12484
12485         if ((!mail_ids) || (account_id <= 0 &&  account_id != -1)) {
12486                 EM_DEBUG_EXCEPTION(" mail_id[%p], account_id[%d] ", mail_ids, account_id);
12487                 if (err_code != NULL)
12488                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12489                 return false;
12490         }
12491
12492         int ret = false;
12493         int rc = -1;
12494         int error = EMAIL_ERROR_NONE;
12495         int count = 0;
12496         int i = 0;
12497         int col_index = 0;
12498         int *p_mail_ids = NULL;
12499         int transaction = false;
12500         char **result = NULL;
12501         char sql_query_string[QUERY_SIZE] = {0, };
12502         char temp_query_string[QUERY_SIZE] = {0,};
12503         char sql_select_query_string[QUERY_SIZE] = {0, };
12504
12505         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12506         EMSTORAGE_START_READ_TRANSACTION(transaction);
12507
12508         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12509
12510         if (account_id == -1)
12511                 SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string), "SELECT mail_id FROM mail_tbl WHERE flags_seen_field = 0 AND (save_status = %d or save_status = %d)", EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12512         else
12513                 SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string), "SELECT mail_id FROM mail_tbl WHERE account_id = %d AND flags_seen_field = 0 AND (save_status = %d or save_status = %d)", account_id, EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12514
12515         if (vip_mode) {
12516                 SNPRINTF(temp_query_string, sizeof(temp_query_string), "%s AND tag_id < 0", sql_select_query_string);
12517         } else {
12518                 SNPRINTF(temp_query_string, sizeof(temp_query_string), "%s", sql_select_query_string);
12519         }
12520
12521         SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s ORDER BY date_time ASC", temp_query_string);
12522
12523         EM_DEBUG_LOG_SEC("query: [%s]", sql_query_string);
12524
12525         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
12526         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
12527                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12528
12529         EM_DEBUG_LOG("Count : %d", count);
12530
12531         if (count == 0) {
12532                 EM_DEBUG_EXCEPTION("no Mails found...");
12533                 ret = false;
12534                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
12535                 goto FINISH_OFF;
12536         }
12537
12538         p_mail_ids = em_malloc(count * sizeof(int));
12539         if (p_mail_ids == NULL) {
12540                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
12541                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12542                 goto FINISH_OFF;
12543         }
12544
12545         col_index = 1;
12546
12547         for (i = 0; i < count; i++) {
12548                 _get_table_field_data_int(result, &(p_mail_ids[i]), col_index++);
12549         }
12550
12551         ret = true;
12552
12553 FINISH_OFF:
12554
12555         if (result)
12556                 sqlite3_free_table(result);
12557
12558         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12559
12560 //      sqlite3_db_release_memory(local_db_handle);
12561
12562         if (ret == true) {
12563                 if (mail_ids != NULL)
12564                         *mail_ids = p_mail_ids;
12565
12566                 if (mail_number != NULL)
12567                         *mail_number = count;
12568         } else {
12569                 EM_SAFE_FREE(p_mail_ids);
12570         }
12571
12572         if (err_code != NULL)
12573                 *err_code = error;
12574
12575         EM_DEBUG_FUNC_END("ret [%d]", ret);
12576         return ret;
12577 }
12578
12579 int setting_system_command(const char *command)
12580
12581  {
12582         int pid = 0, status = 0;
12583         char *const environ[] = { NULL };
12584
12585         if (command == 0)
12586                 return 1;
12587
12588         pid = fork();
12589
12590         if (pid == -1)
12591                 return -1;
12592
12593         if (pid == 0) {
12594                 char *argv[4];
12595
12596                 argv[0] = "sh";
12597                 argv[1] = "-c";
12598                 argv[2] = (char *)command;
12599                 argv[3] = 0;
12600
12601                 execve("/bin/sh", argv, environ);
12602                 abort();
12603         }
12604
12605         do {
12606                 if (waitpid(pid, &status, 0) == -1) {
12607                         if (errno != EINTR)
12608                                 return -1;
12609                 } else {
12610                         return status;
12611                 }
12612         } while (1);
12613
12614         return 0;
12615 }
12616
12617
12618 INTERNAL_FUNC int emstorage_mail_get_total_diskspace_usage(unsigned long *total_usage,  int transaction, int *err_code)
12619 {
12620         EM_DEBUG_FUNC_BEGIN("total_usage[%p],  transaction[%d], err_code[%p]", total_usage, transaction, err_code);
12621
12622         if (!total_usage) {
12623                 EM_DEBUG_EXCEPTION("total_usage[%p]", total_usage);
12624
12625                 if (err_code != NULL)
12626                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12627                 return false;
12628         }
12629
12630         int   ret = false;
12631         int   error = EMAIL_ERROR_NONE;
12632         char  syscmd[256] = {0, };
12633         char  line[256] = {0, };
12634         char *line_from_file = NULL;
12635         FILE *fp = NULL;
12636         unsigned long total_diskusage = 0;
12637
12638         SNPRINTF(syscmd, sizeof(syscmd), "touch %s", SETTING_MEMORY_TEMP_FILE_PATH);
12639         if (setting_system_command(syscmd) == -1) {
12640                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
12641
12642                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12643                 goto FINISH_OFF;
12644         }
12645
12646         SNPRINTF(syscmd, sizeof(syscmd), "du -hsk %s > %s", EMAILPATH, SETTING_MEMORY_TEMP_FILE_PATH);
12647         EM_DEBUG_LOG(" cmd : %s", syscmd);
12648         if (setting_system_command(syscmd) == -1) {
12649                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : Setting > Memory] System Command [%s] is failed", syscmd);
12650
12651                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12652                 goto FINISH_OFF;
12653         }
12654
12655         error = em_fopen(SETTING_MEMORY_TEMP_FILE_PATH, "r", &fp);
12656         if (error != EMAIL_ERROR_NONE) {
12657                 perror(SETTING_MEMORY_TEMP_FILE_PATH);
12658                 goto FINISH_OFF;
12659         }
12660
12661         line_from_file = fgets(line, sizeof(line), fp);
12662
12663         if (line_from_file == NULL) {
12664                 EM_DEBUG_EXCEPTION("fgets failed");
12665                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12666                 goto FINISH_OFF;
12667         }
12668         total_diskusage = strtoul(line, NULL, 10);
12669
12670         memset(syscmd, 0, sizeof(syscmd));
12671         SNPRINTF(syscmd, sizeof(syscmd), "rm -f %s", SETTING_MEMORY_TEMP_FILE_PATH);
12672         if (setting_system_command(syscmd) == -1) {
12673                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage :  [Setting > Memory] System Command [%s] is failed", syscmd);
12674                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12675                 goto FINISH_OFF;
12676         }
12677
12678         EM_DEBUG_LOG("[Setting > Memory] @@@@@ Size of Directory [%s] is %ld KB", EMAILPATH, total_diskusage);
12679
12680         ret = true;
12681
12682 FINISH_OFF:
12683         if (err_code != NULL)
12684                 *err_code = error;
12685
12686         if (ret)
12687                 *total_usage = total_diskusage;
12688         else
12689                 *total_usage = 0;
12690
12691         if (fp) fclose(fp); /* prevent 32730 */
12692
12693         EM_DEBUG_FUNC_END("ret [%d]", ret);
12694         return ret;
12695 }
12696 #define MAILHOME_UTF8    tzplatform_mkpath(TZ_USER_DATA, "email/.email_data/7/348/UTF-8")
12697
12698 INTERNAL_FUNC int emstorage_test(char *multi_user_name, int mail_id, int account_id, char *full_address_to, char *full_address_cc, char *full_address_bcc, int *err_code)
12699 {
12700         DB_STMT hStmt = NULL;
12701         int ret = false;
12702         int error = EMAIL_ERROR_NONE;
12703         int rc = 0;
12704         char sql_query_string[QUERY_SIZE] = {0, };
12705
12706         SNPRINTF(sql_query_string, sizeof(sql_query_string),
12707                 "INSERT INTO mail_tbl VALUES "
12708                 "(?" /*  mail_id */
12709                 ", ?" /*  account_id */
12710                 ", ?" /*  mail_size */
12711                 ", ?" /*  server_mail_status */
12712                 ", ?" /*  server_mailbox_name */
12713                 ", ?" /*  server_mail_id */
12714                 ", ?" /*  reference_mail_id */
12715                 ", ?" /*  full_address_from */
12716                 ", ?" /*  full_address_reply */
12717                 ", ?" /*  full_address_to */
12718                 ", ?" /*  full_address_cc */
12719                 ", ?" /*  full_address_bcc */
12720                 ", ?" /*  full_address_return */
12721                 ", ?" /*  subject */
12722                 ", ?" /*  body_download_status */
12723                 ", ?" /*  file_path_plain */
12724                 ", ?" /*  file_path_html */
12725                 ", ?" /*  date_time */
12726                 ", ?" /*  flags_seen_field */
12727                 ", ?" /*  flags_deleted_field */
12728                 ", ?" /*  flags_flagged_field */
12729                 ", ?" /*  flags_answered_field */
12730                 ", ?" /*  flags_recent_field */
12731                 ", ?" /*  flags_draft_field */
12732                 ", ?" /*  flags_forwarded_field */
12733                 ", ?" /*  DRM_status */
12734                 ", ?" /*  priority */
12735                 ", ?" /*  save_status */
12736                 ", ?" /*  lock_status */
12737                 ", ?" /*  message_id */
12738                 ", ?" /*  report_status */
12739                 ", ?" /*  email_address_sender */
12740                 ", ?" /*  email_address_recipient */
12741                 ", ?" /*  attachment_count */
12742                 ", ?" /*  inline_content_count */
12743                 ", ?" /*  preview_text */
12744                 ", ?" /*  thread_id */
12745                 ", ?" /*  mailbox_type */
12746                 ", ?" /*  alias_sender */
12747                 ", ?" /*  alias_recipient */
12748                 ", ?" /*  thread_item_count */
12749                 ", ?" /*  meeting_request_status */
12750                 ", ?" /*  message_class */
12751                 ", ?" /*  digest_type */
12752                 ", ?" /*  smime_type */
12753                 ", ?" /*  scheduled_sending_time */
12754                 ", ?" /*  remaining_resend_times */
12755                 ", ?" /*  tag_id   */
12756                 ", ?" /*  replied_time */
12757                 ", ?" /*  forwarded_time */
12758                 ", ?" /*  default_charset */
12759                 ", ?" /*  eas_data_length */
12760                 ", ?" /*  eas_data */
12761                 ")");
12762
12763         int transaction = true;
12764         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12765
12766         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12767
12768
12769         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12770         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
12771                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12772
12773         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_id);
12774         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, account_id);
12775         _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, EMAIL_MAILBOX_TYPE_OUTBOX);
12776         _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, "save test - long", 1, SUBJECT_LEN_IN_MAIL_TBL);
12777         _bind_stmt_field_data_string(hStmt, DATETIME_IDX_IN_MAIL_TBL, "20100316052908", 0, DATETIME_LEN_IN_MAIL_TBL);
12778         _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, 0);
12779         _bind_stmt_field_data_string(hStmt, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL, "", 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
12780         _bind_stmt_field_data_string(hStmt, SERVER_MAIL_ID_IDX_IN_MAIL_TBL, "", 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
12781         _bind_stmt_field_data_string(hStmt, MESSAGE_ID_IDX_IN_MAIL_TBL, "", 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
12782         _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, 0);
12783         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL, "<test08@streaming.s3glab.net>", 1, FROM_LEN_IN_MAIL_TBL);
12784         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL, "", 1, REPLY_TO_LEN_IN_MAIL_TBL);
12785         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL, full_address_to, 1, TO_LEN_IN_MAIL_TBL);
12786         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL, full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
12787         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL, full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
12788         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL, "", 1, RETURN_PATH_LEN_IN_MAIL_TBL);
12789         _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL, "<sender_name@sender_host.com>", 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12790         _bind_stmt_field_data_string(hStmt, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL, "<recipient_name@recipient_host.com>", 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12791         _bind_stmt_field_data_string(hStmt, ALIAS_SENDER_IDX_IN_MAIL_TBL, "send_alias", 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12792         _bind_stmt_field_data_string(hStmt, ALIAS_RECIPIENT_IDX_IN_MAIL_TBL, "recipient_alias", 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12793         _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, 1);
12794         _bind_stmt_field_data_string(hStmt, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL, MAILHOME_UTF8, 0, TEXT_1_LEN_IN_MAIL_TBL);
12795         _bind_stmt_field_data_string(hStmt, FILE_PATH_HTML_IDX_IN_MAIL_TBL, "", 0, TEXT_2_LEN_IN_MAIL_TBL);
12796         _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, 4);
12797         _bind_stmt_field_data_char(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, 0);
12798         _bind_stmt_field_data_char(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, 0);
12799         _bind_stmt_field_data_char(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, 0);
12800         _bind_stmt_field_data_char(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, 0);
12801         _bind_stmt_field_data_char(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, 0);
12802         _bind_stmt_field_data_char(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, 0);
12803         _bind_stmt_field_data_char(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, 0);
12804         _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, 0);
12805         _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, 0);
12806         _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, 0);
12807         _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, 0);
12808         _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, 0);
12809         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12810         _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, 0);
12811         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12812         _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, 0);
12813         _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, 0);
12814         _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, "preview body", 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
12815         _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, 0);
12816         _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, 0);
12817         _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, 0);
12818         _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, 0);
12819         _bind_stmt_field_data_int(hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, 0);
12820         _bind_stmt_field_data_int(hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, 0);
12821         _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, 0);
12822         _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, 0);
12823         _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, 0);
12824         _bind_stmt_field_data_string(hStmt, DEFAULT_CHARSET_IDX_IN_MAIL_TBL, "UTF-8", 0, TEXT_2_LEN_IN_MAIL_TBL);
12825         _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, 0);
12826         _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, NULL, 0);
12827
12828         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
12829         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
12830                 ("sqlite3_step fail:%d", rc));
12831         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
12832                 ("sqlite3_step fail:%d", rc));
12833         ret = true;
12834
12835 FINISH_OFF:
12836         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12837         if (hStmt != NULL) {
12838                 rc = sqlite3_finalize(hStmt);
12839                 if (rc != SQLITE_OK) {
12840                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
12841                         error = EMAIL_ERROR_DB_FAILURE;
12842                 }
12843         }
12844
12845         if (err_code != NULL)
12846                 *err_code = error;
12847
12848         EM_DEBUG_FUNC_END("ret [%d]", ret);
12849         return ret;
12850 }
12851
12852
12853 INTERNAL_FUNC int emstorage_get_max_mail_count()
12854 {
12855         return EMAIL_MAIL_MAX_COUNT;
12856 }
12857
12858 #define STRIPPED_SUBJECT_BUFFER_SIZE 4086
12859
12860 INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(char *multi_user_name,
12861                                                                                                                         emstorage_mail_tbl_t *mail_tbl,
12862                                                                                                                         int *thread_id,
12863                                                                                                                         int *result_latest_mail_id_in_thread,
12864                                                                                                                         int *thread_item_count)
12865 {
12866         EM_DEBUG_FUNC_BEGIN("mail_tbl [%p], thread_id [%p], "
12867                                                 "result_latest_mail_id_in_thread [%p], thread_item_count [%p]",
12868                                                 mail_tbl, thread_id, result_latest_mail_id_in_thread, thread_item_count);
12869         EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
12870
12871         int rc = 0, query_size = 0, query_size_account = 0;
12872         int i = 0;
12873         int search_thread = false;
12874         int account_id = 0;
12875         int err_code = EMAIL_ERROR_NONE;
12876         int count = 0, result_thread_id = -1, latest_mail_id_in_thread = -1;
12877         time_t latest_date_time = 0;
12878         char *subject = NULL;
12879         char *p_subject = NULL;
12880         char *sql_query_string = NULL, *sql_account = NULL;
12881         int col_index = 4;
12882         int temp_thread_id = -1;
12883         char *sql_format = "SELECT mail_id, thread_id, date_time, subject "
12884                                                 "FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
12885         char *sql_format_account = " AND account_id = %d ";
12886         char *sql_format_order_by = " ORDER BY thread_id, date_time DESC ";
12887         char **result = NULL;
12888         char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
12889         char stripped_subject2[STRIPPED_SUBJECT_BUFFER_SIZE];
12890
12891         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12892
12893         EM_DEBUG_LOG("subject: [%p], mail_id: [%d]", subject, mail_tbl->mail_id);
12894
12895         EM_IF_NULL_RETURN_VALUE(mail_tbl, EMAIL_ERROR_INVALID_PARAM);
12896         EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
12897         EM_IF_NULL_RETURN_VALUE(result_latest_mail_id_in_thread, EMAIL_ERROR_INVALID_PARAM);
12898         EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
12899
12900         account_id   = mail_tbl->account_id;
12901         subject      = mail_tbl->subject;
12902
12903         EM_DEBUG_LOG_SEC("subject: [%s]", subject);
12904
12905         if (EM_SAFE_STRLEN(subject) == 0 && mail_tbl->mail_id != 0) {
12906                 result_thread_id = mail_tbl->mail_id;
12907                 count = 1;
12908                 goto FINISH_OFF;
12909         }
12910
12911         if (em_find_pos_stripped_subject_for_thread_view(subject,
12912                                                                                                                 stripped_subject,
12913                                                                                                                 STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE)      {
12914                 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
12915                 err_code =  EMAIL_ERROR_UNKNOWN;
12916                 result_thread_id = -1;
12917                 goto FINISH_OFF;
12918         }
12919
12920         EM_DEBUG_LOG_SEC("stripped_subject: [%s]", stripped_subject);
12921
12922         if (EM_SAFE_STRLEN(stripped_subject) == 0) {
12923                 result_thread_id = -1;
12924                 goto FINISH_OFF;
12925         }
12926
12927         EM_DEBUG_LOG_SEC("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s",
12928                                                 EM_SAFE_STRLEN(stripped_subject), stripped_subject);
12929
12930         if (account_id > 0)     {
12931                 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
12932                 sql_account = malloc(query_size_account);
12933                 if (sql_account == NULL) {
12934                         EM_DEBUG_EXCEPTION("malloc for sql_account  is failed %d", query_size_account);
12935                         err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
12936                         goto FINISH_OFF;
12937                 }
12938                 snprintf(sql_account, query_size_account, sql_format_account, account_id);
12939         }
12940
12941         /* prevent 34362 */
12942         query_size = strlen(sql_format) + strlen(stripped_subject)*2 + 50 + query_size_account + strlen(sql_format_order_by); /*  + query_size_mailbox; */
12943         sql_query_string = malloc(query_size);
12944
12945         if (sql_query_string == NULL) {
12946                 EM_DEBUG_EXCEPTION("malloc for sql  is failed %d", query_size);
12947                 err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
12948                 goto FINISH_OFF;
12949         }
12950
12951         sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mail_tbl->mailbox_id);
12952
12953         if (account_id > 0)
12954                 strcat(sql_query_string, sql_account);
12955
12956         strcat(sql_query_string, sql_format_order_by);
12957         strcat(sql_query_string, ";");
12958
12959         EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
12960
12961         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL),
12962                                                                         rc);
12963
12964         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
12965                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12966
12967         EM_DEBUG_LOG("Result rows count : %d", count);
12968
12969         if (count == 0)
12970                 result_thread_id = -1;
12971         else {
12972                 for (i = 0; i < count; i++) {
12973                         EM_SAFE_FREE(p_subject);
12974
12975                         _get_table_field_data_int(result, &latest_mail_id_in_thread, col_index++);
12976                         _get_table_field_data_int(result, &result_thread_id, col_index++);
12977                         _get_table_field_data_time_t(result, &latest_date_time, col_index++);
12978                         _get_table_field_data_string(result, &p_subject, 0, col_index++);
12979
12980                         if (temp_thread_id == result_thread_id)
12981                                 continue;
12982
12983                         temp_thread_id = result_thread_id;
12984
12985                         if (em_find_pos_stripped_subject_for_thread_view(p_subject,
12986                                                                                                                                 stripped_subject2,
12987                                                                                                                                 STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE)      {
12988                                 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
12989                                 err_code = EMAIL_ERROR_UNKNOWN;
12990                                 result_thread_id = -1;
12991                                 goto FINISH_OFF;
12992                         }
12993
12994                         if (g_strcmp0(stripped_subject2, stripped_subject) == 0) {
12995                                 if (latest_date_time < mail_tbl->date_time)
12996                                         *result_latest_mail_id_in_thread = latest_mail_id_in_thread;
12997                                 else
12998                                         *result_latest_mail_id_in_thread = mail_tbl->mail_id;
12999
13000                                 search_thread = true;
13001                                 break;
13002                         }
13003
13004                         if (search_thread) {
13005                                 EM_DEBUG_LOG("latest_mail_id_in_thread [%d], mail_id [%d]",
13006                                                                 latest_mail_id_in_thread, mail_tbl->mail_id);
13007                         } else {
13008                                 result_thread_id = -1;
13009                                 count = 0;
13010                         }
13011                 }
13012
13013         }
13014
13015 FINISH_OFF:
13016
13017         *thread_id = result_thread_id;
13018         *thread_item_count = count;
13019
13020         EM_DEBUG_LOG("Result thread id : %d", *thread_id);
13021         EM_DEBUG_LOG("Result count : %d", *thread_item_count);
13022         EM_DEBUG_LOG("err_code : %d", err_code);
13023
13024         EM_SAFE_FREE(sql_account);
13025         EM_SAFE_FREE(sql_query_string);
13026         EM_SAFE_FREE(p_subject);
13027
13028         sqlite3_free_table(result);
13029
13030         EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
13031
13032         return err_code;
13033 }
13034
13035 INTERNAL_FUNC int emstorage_get_thread_id_from_mailbox(char *multi_user_name, int account_id, int mailbox_id, char *mail_subject, int *thread_id, int *thread_item_count)
13036 {
13037         EM_DEBUG_FUNC_BEGIN("mailbox_id [%d], subject [%p], thread_id [%p], thread_item_count [%p]", mailbox_id, mail_subject, thread_id, thread_item_count);
13038         EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
13039
13040         int rc = 0;
13041         int query_size = 0;
13042         int query_size_account = 0;
13043         int err_code = EMAIL_ERROR_NONE;
13044         int count = 0;
13045         int result_thread_id = -1;
13046         char *sql_query_string = NULL;
13047         char *sql_account = NULL;
13048         char *sql_format = "SELECT thread_id FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
13049         char *sql_format_account = " AND account_id = %d ";
13050         char *sql_format_order_by = " ORDER BY date_time DESC ";
13051         char **result = NULL;
13052         char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
13053         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13054
13055         EM_IF_NULL_RETURN_VALUE(mail_subject, EMAIL_ERROR_INVALID_PARAM);
13056         EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
13057         EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
13058
13059         EM_DEBUG_LOG_SEC("subject: [%s]", mail_subject);
13060
13061         if (em_find_pos_stripped_subject_for_thread_view(mail_subject, stripped_subject, STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE)     {
13062                 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view  is failed");
13063                 err_code =  EMAIL_ERROR_UNKNOWN;
13064                 result_thread_id = -1;
13065                 goto FINISH_OFF;
13066         }
13067
13068         if (EM_SAFE_STRLEN(stripped_subject) == 0) {
13069                 result_thread_id = -1;
13070                 goto FINISH_OFF;
13071         }
13072
13073         EM_DEBUG_LOG("em_find_pos_stripped_subject_for_thread_view returns[len = %d] = %s", EM_SAFE_STRLEN(stripped_subject), stripped_subject);
13074
13075         if (account_id > 0) {
13076                 query_size_account = 3 + EM_SAFE_STRLEN(sql_format_account);
13077                 sql_account = malloc(query_size_account);
13078                 if (sql_account == NULL) {
13079                         EM_DEBUG_EXCEPTION("malloc for sql_account  is failed %d", query_size_account);
13080                         err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
13081                         goto FINISH_OFF;
13082                 }
13083                 snprintf(sql_account, query_size_account, sql_format_account, account_id);
13084         }
13085
13086         query_size = EM_SAFE_STRLEN(sql_format) + EM_SAFE_STRLEN(stripped_subject)*2 + 50 + query_size_account + EM_SAFE_STRLEN(sql_format_order_by); /*  + query_size_mailbox; */
13087         sql_query_string = malloc(query_size);
13088
13089         if (sql_query_string == NULL) {
13090                 EM_DEBUG_EXCEPTION("malloc for sql  is failed %d", query_size);
13091                 err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
13092                 goto FINISH_OFF;
13093         }
13094
13095         sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mailbox_id);
13096
13097         if (account_id > 0)
13098                 strcat(sql_query_string, sql_account);
13099
13100         strcat(sql_query_string, sql_format_order_by);
13101         strcat(sql_query_string, ";");
13102
13103         EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
13104
13105         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
13106
13107         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13108                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13109
13110         EM_DEBUG_LOG("Result rows count : %d", count);
13111
13112         if (count == 0)
13113                 result_thread_id = -1;
13114         else {
13115                 _get_table_field_data_int(result, &result_thread_id, 1);
13116         }
13117
13118 FINISH_OFF:
13119         *thread_id = result_thread_id;
13120         *thread_item_count = count;
13121
13122         EM_DEBUG_LOG("Result thread id : %d", *thread_id);
13123         EM_DEBUG_LOG("Result count : %d", *thread_item_count);
13124         EM_DEBUG_LOG("err_code : %d", err_code);
13125
13126         EM_SAFE_FREE(sql_account);
13127         EM_SAFE_FREE(sql_query_string);
13128
13129         sqlite3_free_table(result);
13130
13131         EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
13132
13133         return err_code;
13134 }
13135
13136 INTERNAL_FUNC int emstorage_get_thread_information(char *multi_user_name, int thread_id, emstorage_mail_tbl_t** mail_tbl, int transaction, int *err_code)
13137 {
13138         EM_DEBUG_FUNC_BEGIN();
13139
13140         int count = 0, ret = false;
13141         int error = EMAIL_ERROR_NONE;
13142         emstorage_mail_tbl_t *p_data_tbl = NULL;
13143         char conditional_clause[QUERY_SIZE] = {0, };
13144
13145         EM_IF_NULL_RETURN_VALUE(mail_tbl, false);
13146
13147         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE thread_id = %d AND thread_item_count > 0", thread_id);
13148         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
13149
13150         if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
13151                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
13152                 goto FINISH_OFF;
13153         }
13154
13155         if (p_data_tbl)
13156                 EM_DEBUG_LOG("thread_id : %d, thread_item_count : %d", p_data_tbl[0].thread_id, p_data_tbl[0].thread_item_count);
13157
13158         ret = true;
13159
13160 FINISH_OFF:
13161         if (ret == true)
13162                 *mail_tbl = p_data_tbl;
13163         else if (p_data_tbl != NULL)
13164                 emstorage_free_mail(&p_data_tbl, 1, NULL);
13165
13166         if (err_code != NULL)
13167                 *err_code = error;
13168
13169         EM_DEBUG_FUNC_END("ret [%d]", ret);
13170         return ret;
13171 }
13172
13173 INTERNAL_FUNC int emstorage_get_sender_list(char *multi_user_name, int account_id, int mailbox_id, int search_type, const char *search_value, email_sort_type_t sorting, email_sender_list_t** sender_list, int *sender_count,  int *err_code)
13174 {
13175         EM_DEBUG_FUNC_BEGIN_SEC("account_id [%d], mailbox_id [%d], search_type [%d], search_value [%p], sorting [%d], sender_list[%p], sender_count[%p] err_code[%p]"
13176                 , account_id , mailbox_id , search_type , search_value , sorting , sender_list, sender_count, err_code);
13177
13178         if ((!sender_list) || (!sender_count)) {
13179                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
13180                 if (err_code != NULL)
13181                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13182                 return false;
13183         }
13184
13185         int rc = -1, ret = false;
13186         int error = EMAIL_ERROR_NONE;
13187         int count = 0;
13188         int i, col_index = 0;
13189         int read_count = 0;
13190         email_sender_list_t *p_sender_list = NULL;
13191         char sql_query_string[QUERY_SIZE] = {0, };
13192         char **result = NULL;
13193         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13194
13195         SNPRINTF(sql_query_string, sizeof(sql_query_string),
13196                 "SELECT email_address_sender, alias_sender, COUNT(email_address_sender), SUM(flags_seen_field = 1) "
13197                 "FROM mail_tbl ");
13198
13199         /*  mailbox_id */
13200         if (mailbox_id)
13201                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE mailbox_id = %d ", mailbox_id);
13202         else    /*  NULL  means all mailbox_name. but except for trash(3), spambox(5), all emails(for GMail, 7) */
13203                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE mailbox_type not in (3, 5, 7, 8) ");
13204
13205         /*  account id */
13206         /*  '0' (ALL_ACCOUNT) means all account */
13207         if (account_id > ALL_ACCOUNT)
13208                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1), " AND account_id = %d ", account_id);
13209
13210         if (search_value) {
13211                 switch (search_type) {
13212                         case EMAIL_SEARCH_FILTER_SUBJECT:
13213                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13214                                         " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\')) ", search_value);
13215                                 break;
13216                         case EMAIL_SEARCH_FILTER_SENDER:
13217                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13218                                         " AND  ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13219                                         ") ", search_value);
13220                                 break;
13221                         case EMAIL_SEARCH_FILTER_RECIPIENT:
13222                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13223                                         " AND ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13224                                         "       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13225                                         "       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13226                                         ") ", search_value, search_value, search_value);
13227                                 break;
13228                         case EMAIL_SEARCH_FILTER_ALL:
13229                                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13230                                         " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') "
13231                                         "       OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13232                                         "                       OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13233                                         "                       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13234                                         "                       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13235                                         "               ) "
13236                                         "       )"
13237                                         ")", search_value, search_value, search_value, search_value, search_value);
13238                                 break;
13239                 }
13240         }
13241
13242
13243         /*  sorting option is not available now. The order of sender list is ascending order by display name */
13244         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
13245                 "GROUP BY email_address_sender "
13246                 "ORDER BY UPPER(alias_sender) ");
13247
13248         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
13249
13250         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
13251         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13252                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13253
13254         EM_DEBUG_LOG("Count of Sender [%d]", count);
13255
13256         if (!(p_sender_list = (email_sender_list_t*)em_malloc(sizeof(email_sender_list_t) * count))) {
13257                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
13258                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13259                 goto FINISH_OFF;
13260         }
13261
13262         col_index = 4;
13263
13264         EM_DEBUG_LOG(">>>> DATA ASSIGN START >>");
13265         for (i = 0; i < count; i++) {
13266                 _get_table_field_data_string(result, &(p_sender_list[i].address), 1, col_index++);
13267                 _get_table_field_data_string(result, &(p_sender_list[i].display_name), 1, col_index++);
13268                 _get_table_field_data_int(result, &(p_sender_list[i].total_count), col_index++);
13269                 _get_table_field_data_int(result, &(read_count), col_index++);
13270                 p_sender_list[i].unread_count = p_sender_list[i].total_count - read_count;              /*  unread count = total - read          */
13271         }
13272         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >>", count);
13273
13274         sqlite3_free_table(result);
13275         result = NULL;
13276
13277         ret = true;
13278
13279 FINISH_OFF:
13280         if (ret == true) {
13281                 *sender_list = p_sender_list;
13282                 *sender_count = count;
13283                 EM_DEBUG_LOG(">>>> COUNT : %d >>", count);
13284         }
13285
13286
13287         if (err_code != NULL)
13288                 *err_code = error;
13289
13290         EM_DEBUG_FUNC_END("ret [%d]", ret);
13291         return ret;
13292 }
13293
13294 INTERNAL_FUNC int emstorage_free_sender_list(email_sender_list_t **sender_list, int count)
13295 {
13296         EM_DEBUG_FUNC_BEGIN("sender_list[%p], count[%d]", sender_list, count);
13297
13298         int err = EMAIL_ERROR_NONE;
13299
13300         if (count > 0) {
13301                 if (!sender_list || !*sender_list) {
13302                         EM_DEBUG_EXCEPTION("sender_list[%p], count[%d]", sender_list, count);
13303                         err = EMAIL_ERROR_INVALID_PARAM;
13304                         return err;
13305                 }
13306
13307                 email_sender_list_t* p = *sender_list;
13308                 int i = 0;
13309
13310                 for (; i < count; i++) {
13311                         EM_SAFE_FREE(p[i].address);
13312                         EM_SAFE_FREE(p[i].display_name);
13313                 }
13314
13315                 EM_SAFE_FREE(p);
13316                 *sender_list = NULL;
13317         }
13318
13319         return err;
13320 }
13321
13322
13323 INTERNAL_FUNC int emstorage_free_address_info_list(email_address_info_list_t **address_info_list)
13324 {
13325         EM_DEBUG_FUNC_BEGIN("address_info_list[%p]", address_info_list);
13326
13327         int err = EMAIL_ERROR_NONE;
13328         email_address_info_t *p_address_info = NULL;
13329         GList *list = NULL;
13330         GList *node = NULL;
13331         int i = 0;
13332
13333         if (!address_info_list || !*address_info_list) {
13334                 EM_DEBUG_EXCEPTION("address_info_list[%p]", address_info_list);
13335                 err = EMAIL_ERROR_INVALID_PARAM;
13336                 return err;
13337         }
13338
13339         /*  delete GLists */
13340         for (i = EMAIL_ADDRESS_TYPE_FROM; i <= EMAIL_ADDRESS_TYPE_BCC; i++) {
13341                 switch (i) {
13342                         case EMAIL_ADDRESS_TYPE_FROM:
13343                                 list = (*address_info_list)->from;
13344                                 break;
13345                         case EMAIL_ADDRESS_TYPE_TO:
13346                                 list = (*address_info_list)->to;
13347                                 break;
13348                         case EMAIL_ADDRESS_TYPE_CC:
13349                                 list = (*address_info_list)->cc;
13350                                 break;
13351                         case EMAIL_ADDRESS_TYPE_BCC:
13352                                 list = (*address_info_list)->bcc;
13353                                 break;
13354                 }
13355
13356                 /*  delete dynamic-allocated memory for each item */
13357                 node = g_list_first(list);
13358                 while (node != NULL) {
13359                         p_address_info = (email_address_info_t*)node->data;
13360                         EM_SAFE_FREE(p_address_info->address);
13361                         EM_SAFE_FREE(p_address_info->display_name);
13362                         EM_SAFE_FREE(node->data);
13363
13364                         node = g_list_next(node);
13365                 }
13366                 g_list_free(list);
13367         }
13368
13369         EM_SAFE_FREE(*address_info_list);
13370         *address_info_list = NULL;
13371
13372         EM_DEBUG_FUNC_END("err [%d]", err);
13373         return err;
13374 }
13375 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
13376
13377 INTERNAL_FUNC int emstorage_add_pbd_activity(char *multi_user_name, email_event_partial_body_thd* local_activity, int *activity_id, int transaction, int *err_code)
13378 {
13379         EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
13380
13381         if (!local_activity || !activity_id) {
13382                 EM_DEBUG_EXCEPTION("local_activity[%p], transaction[%d], activity_id[%p], err_code[%p]", local_activity, activity_id, transaction, err_code);
13383                 if (err_code != NULL)
13384                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13385                 return false;
13386         }
13387
13388         int rc = -1;
13389         int ret = false;
13390         int error = EMAIL_ERROR_NONE;
13391         int i = 0;
13392
13393         char sql_query_string[QUERY_SIZE] = {0, };
13394         DB_STMT hStmt = NULL;
13395         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13396
13397         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13398         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13399         SNPRINTF(sql_query_string, sizeof(sql_query_string),
13400                 "INSERT INTO mail_partial_body_activity_tbl VALUES "
13401                 "("
13402                 "? "  /* Account ID */
13403                 ",?"  /* Local Mail ID */
13404                 ",?"  /* Server mail ID */
13405                 ",?"  /* Activity ID */
13406                 ",?"  /* Activity type*/
13407                 ",?"  /* Mailbox ID*/
13408                 ",?"  /* Mailbox name*/
13409                 ",?"  /* Multi User Name */
13410                 ") ");
13411
13412         char *sql = "SELECT max(rowid) FROM mail_partial_body_activity_tbl;";
13413         char **result = NULL;
13414
13415
13416         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13417
13418         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13419                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13420
13421         if (NULL == result[1]) rc = 1;
13422         else rc = atoi(result[1])+1;
13423         sqlite3_free_table(result);
13424         result = NULL;
13425
13426         *activity_id = local_activity->activity_id = rc;
13427
13428         EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [ %d ], MAIL ID [ %d ], ACTIVITY TYPE [ %d ], SERVER MAIL ID [ %lu ]", \
13429                 local_activity->activity_id, local_activity->mail_id, local_activity->activity_type, local_activity->server_mail_id);
13430
13431         if (local_activity->mailbox_id)
13432                 EM_DEBUG_LOG(" MAILBOX ID [ %d ]", local_activity->mailbox_id);
13433
13434
13435         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13436         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13437                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13438
13439
13440         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
13441         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
13442         _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
13443         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
13444         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
13445         _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
13446         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->mailbox_name, 0, 3999);
13447         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
13448
13449
13450         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13451
13452         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
13453                 ("sqlite3_step fail:%d", rc));
13454         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13455                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
13456
13457         ret = true;
13458
13459 FINISH_OFF:
13460         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13461         if (hStmt != NULL) {
13462                 rc = sqlite3_finalize(hStmt);
13463                 hStmt = NULL;
13464                 if (rc != SQLITE_OK) {
13465                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13466                         error = EMAIL_ERROR_DB_FAILURE;
13467                 }
13468         }
13469
13470         if (err_code != NULL)
13471                 *err_code = error;
13472
13473         EM_DEBUG_FUNC_END("ret [%d]", ret);
13474         return ret;
13475 }
13476
13477 INTERNAL_FUNC int emstorage_get_pbd_mailbox_list(char *multi_user_name, int account_id, int **mailbox_list, int *count, int transaction, int *err_code)
13478 {
13479         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13480
13481         if (account_id < FIRST_ACCOUNT_ID || NULL == mailbox_list || *mailbox_list == NULL || NULL == count) {
13482                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13483                 if (err_code != NULL)
13484                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13485                 return false;
13486         }
13487
13488         int ret = false;
13489         int error = EMAIL_ERROR_NONE;
13490         char **result;
13491         int i = 0, rc = -1;
13492         int *mbox_list = NULL;
13493         DB_STMT hStmt = NULL;
13494         char sql_query_string[QUERY_SIZE] = {0, };
13495
13496         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13497         EMSTORAGE_START_READ_TRANSACTION(transaction);
13498
13499         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13500         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(distinct mailbox_id) FROM mail_partial_body_activity_tbl WHERE account_id = %d order by mailbox_id", account_id);
13501
13502
13503         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13504         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13505                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13506
13507         *count = atoi(result[1]);
13508         sqlite3_free_table(result);
13509
13510         if (!*count) {
13511                 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
13512                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13513                 ret = true;
13514                 goto FINISH_OFF;
13515         }
13516         EM_DEBUG_LOG("Mailbox count = %d", *count);
13517
13518         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13519
13520         /* SNPRINTF(g_sql_query, sizeof(g_sql_query), "SELECT distinct mailbox_name FROM mail_partial_body_activity_tbl WHERE account_id = %d order by activity_id", account_id); */
13521         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct mailbox_id FROM mail_partial_body_activity_tbl WHERE account_id = %d order by mailbox_id", account_id);
13522
13523         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13524
13525
13526         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13527
13528
13529         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13530                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13531
13532
13533         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13534         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13535                 ("sqlite3_step fail:%d", rc));
13536
13537         mbox_list = (int *)em_malloc(sizeof(int) * (*count));
13538         if (NULL == mbox_list) {
13539                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
13540                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13541                 goto FINISH_OFF;
13542         }
13543
13544         memset(mbox_list, 0x00, sizeof(int) * (*count));
13545
13546         for (i = 0; i < (*count); i++) {
13547                 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
13548                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13549                 /* EM_DEBUG_LOG("In emstorage_get_pdb_mailbox_list() loop, After sqlite3_step(), , i = %d, rc = %d.", i,  rc); */
13550                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13551                         ("sqlite3_step fail:%d", rc));
13552                 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
13553         }
13554
13555         ret = true;
13556
13557 FINISH_OFF:
13558         if (ret == true)
13559                 *mailbox_list = mbox_list;
13560         else
13561                 EM_SAFE_FREE(mbox_list);
13562
13563         if (hStmt != NULL) {
13564                 rc = sqlite3_finalize(hStmt);
13565                 hStmt = NULL;
13566                 if (rc != SQLITE_OK) {
13567                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13568                         error = EMAIL_ERROR_DB_FAILURE;
13569                 }
13570         }
13571
13572         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13573         if (err_code != NULL)
13574                 *err_code = error;
13575         EM_DEBUG_FUNC_END("ret [%d]", ret);
13576         return ret;
13577 }
13578
13579 INTERNAL_FUNC int emstorage_get_pbd_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
13580 {
13581         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13582
13583         if (NULL == account_list || NULL == count) {
13584                 EM_DEBUG_EXCEPTION("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13585                 if (err_code != NULL)
13586                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13587                 return false;
13588         }
13589
13590         int ret = false;
13591         int error = EMAIL_ERROR_NONE;
13592         char *sql = "SELECT count(distinct account_id) FROM mail_partial_body_activity_tbl";
13593         char **result;
13594         int i = 0, rc = -1;
13595         int *result_account_list = NULL;
13596         DB_STMT hStmt = NULL;
13597         char sql_query_string[QUERY_SIZE] = {0, };
13598
13599         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13600
13601         EMSTORAGE_START_READ_TRANSACTION(transaction);
13602
13603         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13604         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13605                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13606
13607         *count = atoi(result[1]);
13608         sqlite3_free_table(result);
13609
13610         if (!*count) {
13611                 EM_DEBUG_EXCEPTION("no account found...");
13612                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13613                 ret = true;
13614                 goto FINISH_OFF;
13615         }
13616
13617         EM_DEBUG_LOG("Account count [%d]", *count);
13618
13619         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13620
13621         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_partial_body_activity_tbl");
13622
13623         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13624
13625
13626         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13627
13628         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
13629         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13630                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13631
13632
13633         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13634         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13635                 ("sqlite3_step fail:%d", rc));
13636
13637         if (NULL == (result_account_list = (int *)em_malloc(sizeof(int) * (*count)))) {
13638                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
13639                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13640                 goto FINISH_OFF;
13641         }
13642
13643         memset(result_account_list, 0x00, sizeof(int) * (*count));
13644
13645         for (i = 0; i < (*count); i++) {
13646                 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
13647
13648                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13649                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13650                         ("sqlite3_step fail:%d", rc));
13651                 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
13652         }
13653
13654         ret = true;
13655
13656 FINISH_OFF:
13657         if (ret == true)
13658                 *account_list = result_account_list;
13659         else
13660                 EM_SAFE_FREE(result_account_list);
13661
13662         if (hStmt != NULL) {
13663                 rc = sqlite3_finalize(hStmt);
13664                 hStmt = NULL;
13665                 if (rc != SQLITE_OK) {
13666                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13667                         error = EMAIL_ERROR_DB_FAILURE;
13668                 }
13669         }
13670
13671         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13672         if (err_code != NULL)
13673                 *err_code = error;
13674         EM_DEBUG_FUNC_END("ret [%d]", ret);
13675         return ret;
13676 }
13677
13678 INTERNAL_FUNC int emstorage_get_pbd_activity_data(char *multi_user_name, int account_id, int input_mailbox_id, email_event_partial_body_thd** event_start, int *count, int transaction, int *err_code)
13679 {
13680         EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
13681
13682         if (account_id < FIRST_ACCOUNT_ID || NULL == event_start || 0 == input_mailbox_id || NULL == count) {
13683                 EM_DEBUG_EXCEPTION("account_id[%d], email_event_partial_body_thd[%p], input_mailbox_id[%d], count[%p], err_code[%p]", account_id, event_start, input_mailbox_id, count, err_code);
13684
13685                 if (err_code != NULL)
13686                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13687                 return false;
13688         }
13689
13690         int rc = -1;
13691         int ret = false;
13692         char **result;
13693         int error = EMAIL_ERROR_NONE;
13694         int i = 0;
13695         DB_STMT hStmt = NULL;
13696         email_event_partial_body_thd* event_list = NULL;
13697         char sql_query_string[QUERY_SIZE] = {0, };
13698
13699         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13700
13701         EMSTORAGE_START_READ_TRANSACTION(transaction);
13702
13703         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13704         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl WHERE account_id = %d AND mailbox_id = '%d' order by activity_id", account_id, input_mailbox_id);
13705
13706
13707         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13708         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13709                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13710
13711         *count = atoi(result[1]);
13712         sqlite3_free_table(result);
13713
13714         EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
13715
13716         if (!*count) {
13717                 EM_DEBUG_LOG("No matched activity found in mail_partial_body_activity_tbl");
13718                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
13719                 ret = true;
13720                 goto FINISH_OFF;
13721         }
13722         EM_DEBUG_LOG("Activity Count = %d", *count);
13723
13724         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13725         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_partial_body_activity_tbl WHERE account_id = %d AND mailbox_id = '%d' order by activity_id", account_id, input_mailbox_id);
13726
13727         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13728
13729
13730         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13731
13732         EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
13733         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13734                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13735
13736
13737         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13738         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13739                 ("sqlite3_step fail:%d", rc));
13740
13741         if (!(event_list = (email_event_partial_body_thd*)em_malloc(sizeof(email_event_partial_body_thd) * (*count)))) {
13742                 EM_DEBUG_EXCEPTION("Malloc failed");
13743
13744                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13745                 goto FINISH_OFF;
13746         }
13747         memset(event_list, 0x00, sizeof(email_event_partial_body_thd) * (*count));
13748
13749         for (i = 0; i < (*count); i++) {
13750                 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_IDX_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13751                 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13752                 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13753                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13754                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_type), ACTIVITY_TYPE_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13755                 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13756                 _get_stmt_field_data_string(hStmt, &(event_list[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13757                 _get_stmt_field_data_string(hStmt, &(event_list[i].multi_user_name), 0, MULTI_USER_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13758
13759                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13760                 /* EM_DEBUG_LOG("In emstorage_get_pbd_activity_data() loop, After sqlite3_step(), , i = %d, rc = %d.", i,  rc); */
13761                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13762                         ("sqlite3_step fail:%d", rc));
13763
13764                 event_list[i].event_type = 0;
13765         }
13766
13767         ret = true;
13768
13769 FINISH_OFF:
13770         if (true == ret)
13771           *event_start = event_list;
13772         else {
13773                 for (i = 0; i < (*count); i++)
13774                         emcore_free_partial_body_thd_event(event_list, NULL);
13775                 EM_SAFE_FREE(event_list); /*prevent 54559*/
13776                 *event_start = NULL;
13777                 *count = 0;
13778         }
13779
13780         if (hStmt != NULL) {
13781                 rc = sqlite3_finalize(hStmt);
13782                 hStmt = NULL;
13783                 if (rc != SQLITE_OK) {
13784                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13785                         error = EMAIL_ERROR_DB_FAILURE;
13786                 }
13787         }
13788
13789         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13790         if (err_code != NULL)
13791                 *err_code = error;
13792
13793         EM_DEBUG_FUNC_END("ret [%d]", ret);
13794         return ret;
13795 }
13796
13797 INTERNAL_FUNC int emstorage_delete_pbd_activity(char *multi_user_name, int account_id, int mail_id, int activity_id, int transaction, int *err_code)
13798 {
13799         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d] , activity_id[%d], transaction[%d], err_code[%p]", account_id, mail_id, activity_id, transaction, err_code);
13800
13801
13802         if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
13803                 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], activity_id[%d], transaction[%d], err_code[%p]", account_id, mail_id, activity_id, transaction, err_code);
13804
13805                 if (err_code != NULL)
13806                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13807                 return false;
13808         }
13809
13810         int rc = -1;
13811         int ret = false;
13812         int error = EMAIL_ERROR_NONE;
13813         char sql_query_string[QUERY_SIZE] = {0, };
13814         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13815
13816         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13817         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13818
13819         if (activity_id == 0)
13820                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d AND mail_id = %d", account_id, mail_id);
13821         else
13822                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d AND activity_id = %d", account_id, activity_id);
13823
13824         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13825         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
13826         if (error != EMAIL_ERROR_NONE) {
13827                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
13828                         goto FINISH_OFF;
13829         }
13830
13831         /*  validate activity existence */
13832         rc = sqlite3_changes(local_db_handle);
13833         if (rc == 0) {
13834                 EM_DEBUG_EXCEPTION("No matching activity found");
13835                 error = EMAIL_ERROR_DATA_NOT_FOUND;
13836                 ret = true;
13837                 goto FINISH_OFF;
13838         }
13839
13840         ret = true;
13841
13842 FINISH_OFF:
13843         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13844
13845         if (err_code != NULL)
13846                 *err_code = error;
13847
13848         EM_DEBUG_FUNC_END("ret [%d]", ret);
13849         return ret;
13850 }
13851
13852 INTERNAL_FUNC int emstorage_get_mailbox_pbd_activity_count(char *multi_user_name, int account_id, int input_mailbox_id, int *activity_count, int transaction, int *err_code)
13853 {
13854         EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
13855
13856         if (account_id < FIRST_ACCOUNT_ID || NULL == activity_count || NULL == err_code) {
13857                 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
13858                 if (err_code != NULL)
13859                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13860                 return false;
13861         }
13862         int rc = -1;
13863         int ret = false;
13864         int error = EMAIL_ERROR_NONE;
13865         char sql_query_string[QUERY_SIZE] = {0, };
13866
13867         DB_STMT hStmt = NULL;
13868
13869         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13870
13871         EMSTORAGE_START_READ_TRANSACTION(transaction);
13872         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13873
13874         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl WHERE account_id = %d and mailbox_id = '%d'", account_id, input_mailbox_id);
13875
13876         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13877
13878
13879         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13880         EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
13881         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13882                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13883
13884
13885         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13886         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13887                 ("sqlite3_step fail:%d", rc));
13888
13889         _get_stmt_field_data_int(hStmt, activity_count, 0);
13890
13891         EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
13892
13893         ret = true;
13894
13895 FINISH_OFF:
13896
13897         if (hStmt != NULL) {
13898                 rc = sqlite3_finalize(hStmt);
13899                 hStmt = NULL;
13900                 if (rc != SQLITE_OK) {
13901                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13902                         error = EMAIL_ERROR_DB_FAILURE;
13903                 }
13904         }
13905
13906         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13907
13908         if (err_code != NULL)
13909                 *err_code = error;
13910
13911         EM_DEBUG_FUNC_END("ret [%d]", ret);
13912         return ret;
13913 }
13914
13915 INTERNAL_FUNC int emstorage_get_pbd_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
13916 {
13917         EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
13918
13919         if (NULL == activity_count || NULL == err_code) {
13920                 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
13921                 if (err_code != NULL)
13922                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13923                 return false;
13924         }
13925         int rc = -1;
13926         int ret = false;
13927         int error = EMAIL_ERROR_NONE;
13928         DB_STMT hStmt = NULL;
13929         char sql_query_string[QUERY_SIZE] = {0, };
13930
13931         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13932
13933         EMSTORAGE_START_READ_TRANSACTION(transaction);
13934         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13935
13936         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl;");
13937
13938         EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
13939
13940         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13941         EM_DEBUG_LOG_DEV("  before sqlite3_prepare hStmt = %p", hStmt);
13942         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13943                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13944
13945         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13946         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13947                 ("sqlite3_step fail:%d", rc));
13948
13949         _get_stmt_field_data_int(hStmt, activity_count, 0);
13950
13951         EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
13952
13953         ret = true;
13954
13955 FINISH_OFF:
13956
13957
13958         if (hStmt != NULL) {
13959                 rc = sqlite3_finalize(hStmt);
13960                 hStmt = NULL;
13961                 if (rc != SQLITE_OK) {
13962                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13963                         error = EMAIL_ERROR_DB_FAILURE;
13964                 }
13965         }
13966
13967         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13968         if (err_code != NULL)
13969                 *err_code = error;
13970
13971         EM_DEBUG_FUNC_END("ret [%d]", ret);
13972         return ret;
13973 }
13974
13975 INTERNAL_FUNC int emstorage_delete_full_pbd_activity_data(char *multi_user_name, int account_id, int transaction, int *err_code)
13976 {
13977         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
13978         if (account_id < FIRST_ACCOUNT_ID) {
13979                 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
13980                 if (err_code != NULL)
13981                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13982                 return false;
13983         }
13984
13985         int rc = -1;
13986         int ret = false;
13987         int error = EMAIL_ERROR_NONE;
13988         char sql_query_string[QUERY_SIZE] = {0, };
13989
13990         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13991
13992         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13993         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13994         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d", account_id);
13995
13996         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13997         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
13998         if (error != EMAIL_ERROR_NONE) {
13999                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14000                         goto FINISH_OFF;
14001         }
14002
14003         rc = sqlite3_changes(local_db_handle);
14004         if (rc == 0) {
14005                 EM_DEBUG_EXCEPTION("No matching activities found in mail_partial_body_activity_tbl");
14006                 error = EMAIL_ERROR_DATA_NOT_FOUND;
14007                 ret = true;
14008                 goto FINISH_OFF;
14009         }
14010
14011         ret = true;
14012
14013 FINISH_OFF:
14014
14015         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14016
14017         if (err_code != NULL)
14018                 *err_code = error;
14019
14020         EM_DEBUG_FUNC_END("ret [%d]", ret);
14021         return ret;
14022 }
14023
14024 /*Himanshu[h.gahlaut]-> Added below API to update mail_partial_body_activity_tbl
14025 if a mail is moved before its partial body is downloaded.Currently not used but should be used if mail move from server is enabled*/
14026
14027 INTERNAL_FUNC int emstorage_update_pbd_activity(char *multi_user_name, char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code)
14028 {
14029         EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
14030
14031         int rc = -1, ret = false;
14032         int error = EMAIL_ERROR_NONE;
14033         char sql_query_string[QUERY_SIZE] = {0, };
14034
14035         int transaction = true;
14036
14037         if (!old_server_uid || !new_server_uid || !mbox_name) {
14038                 EM_DEBUG_EXCEPTION("Invalid parameters");
14039                 error = EMAIL_ERROR_INVALID_PARAM;
14040                 return false;
14041         }
14042
14043         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14044
14045         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14046         memset(sql_query_string, 0x00, sizeof(sql_query_string));
14047         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14048                  "UPDATE mail_partial_body_activity_tbl SET server_mail_id = %s , mailbox_name=\'%s\' WHERE server_mail_id = %s ", new_server_uid, mbox_name, old_server_uid);
14049
14050         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14051         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14052         if (error != EMAIL_ERROR_NONE) {
14053                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14054                         goto FINISH_OFF;
14055         }
14056
14057         rc = sqlite3_changes(local_db_handle);
14058         if (rc == 0) {
14059                 EM_DEBUG_LOG("No matching found in mail_partial_body_activity_tbl");
14060         }
14061
14062         ret = true;
14063
14064 FINISH_OFF:
14065         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14066
14067         if (err_code != NULL)
14068                 *err_code = error;
14069
14070         EM_DEBUG_FUNC_END("ret [%d]", ret);
14071         return ret;
14072 }
14073
14074
14075 INTERNAL_FUNC int emstorage_create_file(char *data_string, size_t file_size, char *dst_file_name, int *err_code)
14076 {
14077         EM_DEBUG_FUNC_BEGIN_SEC("file_size[%d] , dst_file_name[%s], err_code[%p]", file_size, dst_file_name, err_code);
14078
14079         int ret = false;
14080         int error = EMAIL_ERROR_NONE;
14081         FILE* fp_dst = NULL;
14082         char errno_buf[ERRNO_BUF_SIZE] = {0};
14083
14084         if (!data_string || !dst_file_name) {
14085                 EM_DEBUG_LOG("data_string[%p], dst_file_name[%p]", data_string, dst_file_name);
14086                 error = EMAIL_ERROR_INVALID_PARAM;
14087                 goto FINISH_OFF;
14088         }
14089
14090         error = em_fopen(dst_file_name, "w", &fp_dst);
14091         if (error != EMAIL_ERROR_NONE) {
14092                 EM_DEBUG_EXCEPTION_SEC("em_fopen failed - %s: %d", dst_file_name, error);
14093                 goto FINISH_OFF;
14094         }
14095
14096         if (fwrite(data_string, 1, file_size, fp_dst) == 0) {
14097                 EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
14098                 error = EMAIL_ERROR_UNKNOWN;
14099                 goto FINISH_OFF;
14100         }
14101
14102         ret = true;
14103
14104 FINISH_OFF:
14105
14106         if (fp_dst != NULL)
14107                 fclose(fp_dst);
14108
14109         if (err_code != NULL)
14110                 *err_code = error;
14111
14112         EM_DEBUG_FUNC_END("ret [%d]", ret);
14113         return ret;
14114 }
14115
14116 #endif /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
14117
14118
14119
14120 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
14121 INTERNAL_FUNC int emstorage_update_read_mail_uid_by_server_uid(char *multi_user_name, char *old_server_uid, char *new_server_uid, char *mbox_name, int *err_code)
14122 {
14123         EM_DEBUG_FUNC_BEGIN();
14124         int rc = -1;
14125         int ret = false;
14126         int error = EMAIL_ERROR_NONE;
14127         char sql_query_string[QUERY_SIZE] = {0, };
14128
14129         int transaction = true;
14130
14131         if (!old_server_uid || !new_server_uid || !mbox_name) {
14132                 EM_DEBUG_EXCEPTION("Invalid parameters");
14133                 if (err_code != NULL)
14134                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14135                 return false;
14136         }
14137
14138         EM_DEBUG_LOG_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
14139
14140         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14141         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14142
14143
14144         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14145                  "UPDATE mail_read_mail_uid_tbl SET server_uid=\'%s\' , mailbox_name=\'%s\' WHERE server_uid=%s ", new_server_uid, mbox_name, old_server_uid);
14146
14147         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
14148         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14149         if (error != EMAIL_ERROR_NONE) {
14150                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14151                         goto FINISH_OFF;
14152         }
14153
14154         rc = sqlite3_changes(local_db_handle);
14155         if (rc == 0) {
14156                 EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
14157                 error = EMAIL_ERROR_DATA_NOT_FOUND;
14158                 goto FINISH_OFF;
14159         }
14160
14161         ret = true;
14162
14163 FINISH_OFF:
14164         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14165
14166         if (err_code != NULL)
14167                 *err_code = error;
14168
14169         EM_DEBUG_FUNC_END("ret [%d]", ret);
14170         return ret;
14171
14172 }
14173
14174
14175 /**
14176  * @fn emstorage_get_id_set_from_mail_ids(int mail_ids[], int mail_id_count, email_id_set_t** server_uids, int *id_set_count, int *err_code);
14177  * Prepare an array of mail_id and corresponding server mail id.
14178  *
14179  *@author                                       h.gahlaut@samsung.com
14180  * @param[in] mail_ids                  Specifies the comma separated string of mail_ids. Maximaum size of string should be less than or equal to (QUERY_SIZE - 88)
14181  *                                                      where 88 is the length of fixed keywords including ending null character in the QUERY to be formed
14182  * @param[out] idset                    Returns the array of mail_id and corresponding server_mail_id sorted by server_mail_ids
14183  * @param[out] id_set_count             Returns the no. of cells in idset array i.e. no. of sets of mail_ids and server_mail_ids
14184  * @param[out] err_code         Returns the error code.
14185  * @remarks                                     An Example of Query to be exexuted in this API:
14186  *                                                      SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (12, 13, 56, 78);
14187  * @return This function returns true on success or false on failure.
14188  */
14189 INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *multi_user_name,
14190                                                                                                                 char *mail_ids,
14191                                                                                                                 email_id_set_t** idset,
14192                                                                                                                 int *id_set_count,
14193                                                                                                                 int *err_code)
14194 {
14195         EM_DEBUG_FUNC_BEGIN();
14196         EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds);
14197
14198         int error = EMAIL_ERROR_NONE;
14199         int ret = false;
14200         email_id_set_t* p_id_set = NULL;
14201         int count = 0;
14202         const int buf_size = QUERY_SIZE;
14203         char sql_query_string[QUERY_SIZE] = {0, };
14204         int space_left_in_query_buffer = buf_size;
14205         int i = 0;
14206         int rc = -1;
14207         char *server_mail_id = NULL;
14208         char **result = NULL;
14209         int col_index = 0;
14210
14211
14212         if (NULL == mail_ids || NULL == idset || NULL == id_set_count) {
14213                 EM_DEBUG_EXCEPTION("Invalid Parameters mail_ids[%p] idset[%p] id_set_count [%p]",
14214                                                         mail_ids, idset, id_set_count);
14215                 if (err_code != NULL)
14216                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14217                 return false;
14218         }
14219
14220         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14221
14222         SNPRINTF(sql_query_string, space_left_in_query_buffer,
14223                         "SELECT local_uid, server_uid FROM mail_read_mail_uid_tbl WHERE local_uid in (%s) ORDER BY server_uid",
14224                         mail_ids);
14225
14226         EM_DEBUG_LOG_SEC("SQL Query formed [%s] ", sql_query_string);
14227
14228         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14229         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14230         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result);
14231                                                 goto FINISH_OFF; },     ("SQL(%s) sqlite3_get_table fail:%d -%s",
14232                                                 sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14233
14234         EM_DEBUG_LOG(" Count of mails [%d ]", count);
14235
14236         if (count <= 0) {
14237                 EM_DEBUG_EXCEPTION("Can't find proper mail");
14238                 error = EMAIL_ERROR_DATA_NOT_FOUND;
14239                 goto FINISH_OFF;
14240         }
14241
14242         if (NULL == (p_id_set = (email_id_set_t*)em_malloc(sizeof(email_id_set_t) * count))) {
14243                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
14244                 error = EMAIL_ERROR_OUT_OF_MEMORY;
14245                 goto FINISH_OFF;
14246         }
14247
14248         col_index = 2;
14249
14250         EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds_Loop);
14251         EM_DEBUG_LOG(">>>> DATA ASSIGN START");
14252         for (i = 0; i < count; i++) {
14253                 _get_table_field_data_int(result, &(p_id_set[i].mail_id), col_index++);
14254                 _get_table_field_data_string(result, &server_mail_id, 1, col_index++);
14255                 if (server_mail_id) {
14256                         p_id_set[i].server_mail_id = strtoul(server_mail_id, NULL, 10);
14257                         EM_SAFE_FREE(server_mail_id);
14258                 }
14259         }
14260         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d]", count);
14261         EM_PROFILE_END(EmStorageGetIdSetFromMailIds_Loop);
14262
14263         sqlite3_free_table(result);
14264         result = NULL;
14265
14266         ret = true;
14267
14268 FINISH_OFF:
14269
14270         if (ret == true) {
14271                 *idset = p_id_set;
14272                 *id_set_count = count;
14273                 EM_DEBUG_LOG(" idset[%p] id_set_count [%d]", *idset, *id_set_count);
14274         } else
14275                 EM_SAFE_FREE(p_id_set);
14276
14277
14278         if (err_code != NULL)
14279                 *err_code = error;
14280
14281         EM_PROFILE_END(EmStorageGetIdSetFromMailIds);
14282
14283         EM_DEBUG_FUNC_END("ret [%d]", ret);
14284         return ret;
14285 }
14286
14287
14288
14289 #endif
14290
14291 INTERNAL_FUNC int emstorage_delete_triggers_from_lucene(char *multi_user_name)
14292 {
14293         EM_DEBUG_FUNC_BEGIN();
14294         int ret = true, transaction = true;
14295         int error = EMAIL_ERROR_NONE;
14296         char sql_query_string[QUERY_SIZE] = {0, };
14297         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14298         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14299
14300         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerDelete;");
14301         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14302         if (error != EMAIL_ERROR_NONE) {
14303                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14304                         goto FINISH_OFF;
14305         }
14306
14307         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerInsert;");
14308         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14309         if (error != EMAIL_ERROR_NONE) {
14310                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14311                         goto FINISH_OFF;
14312         }
14313
14314         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerUpdate;");
14315         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14316         if (error != EMAIL_ERROR_NONE) {
14317                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14318                         goto FINISH_OFF;
14319         }
14320
14321         ret = true;
14322
14323 FINISH_OFF:
14324         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14325
14326         EM_DEBUG_FUNC_END("ret [%d]", ret);
14327         return ret;
14328 }
14329
14330 INTERNAL_FUNC int emstorage_update_tag_id(char *multi_user_name, int old_filter_id, int new_filter_id, int *err_code)
14331 {
14332         EM_DEBUG_FUNC_BEGIN("new_filter_id[%d], old_filter_id[%d]", new_filter_id, old_filter_id);
14333         int ret = false;
14334         int error = EMAIL_ERROR_NONE;
14335         char sql_query_string[QUERY_SIZE] = {0, };
14336         int transaction = true;
14337
14338         if (old_filter_id < 0 || new_filter_id < 0) {
14339                 EM_DEBUG_EXCEPTION("Invalid parameters");
14340                 if (err_code != NULL)
14341                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14342                 return false;
14343         }
14344
14345         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14346
14347         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14348
14349         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14350                  "UPDATE mail_tbl SET tag_id=%d WHERE tag_id=%d ", new_filter_id, old_filter_id);
14351
14352         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14353         if (error != EMAIL_ERROR_NONE) {
14354                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14355                         goto FINISH_OFF;
14356         }
14357
14358         ret = true;
14359
14360 FINISH_OFF:
14361         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14362
14363         if (err_code != NULL)
14364                 *err_code = error;
14365
14366         EM_DEBUG_FUNC_END("ret [%d]", ret);
14367         return ret;
14368 }
14369
14370 INTERNAL_FUNC int emstorage_filter_mails_by_rule(char *multi_user_name, int account_id, int dest_mailbox_id, int dest_mailbox_type, int reset, emstorage_rule_tbl_t *rule, int ** filtered_mail_id_list, int *count_of_mails, int *err_code)
14371 {
14372         EM_DEBUG_FUNC_BEGIN("account_id[%d], dest_mailbox_id[%d] rule[%p], filtered_mail_id_list[%p], count_of_mails[%p], err_code[%p]", account_id, dest_mailbox_id, rule, filtered_mail_id_list, count_of_mails, err_code);
14373
14374         if ((account_id < 0) || (dest_mailbox_id < 0) || (!rule)) {
14375                 EM_DEBUG_EXCEPTION("Invalid Parameter");
14376
14377                 if (err_code != NULL)
14378                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14379                 return false;
14380         }
14381
14382         int rc = -1, ret = false, error = EMAIL_ERROR_NONE;
14383         int count = 0, col_index = 0, i = 0, where_pararaph_length = 0, *mail_list = NULL;
14384         int tag_id = rule->rule_id;
14385         char **result = NULL, *where_pararaph = NULL;
14386         char sql_query_string[QUERY_SIZE] = {0, };
14387         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14388
14389         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl ");
14390
14391         where_pararaph_length = EM_SAFE_STRLEN(rule->value) + 2 * (EM_SAFE_STRLEN(rule->value2)) + 100;
14392         where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
14393         if (where_pararaph == NULL) {
14394                 EM_DEBUG_EXCEPTION("malloc failed for where_pararaph.");
14395                 error = EMAIL_ERROR_OUT_OF_MEMORY;
14396                 goto FINISH_OFF;
14397         }
14398
14399         if (account_id != ALL_ACCOUNT)
14400                 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (0)", account_id);
14401         else
14402                 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_type NOT in (0)");
14403
14404         if (rule->type & EMAIL_FILTER_SUBJECT) {
14405                 if (rule->flag2 == RULE_TYPE_INCLUDES)
14406                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject like \'%%%q%%\'", rule->value);
14407                 else /*  RULE_TYPE_EXACTLY */
14408                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject = \'%q\'", rule->value);
14409         }
14410
14411         if (rule->type & EMAIL_FILTER_FROM) {
14412                 if (rule->flag2 == RULE_TYPE_INCLUDES)
14413                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND full_address_from like \'%%%q%%\'", rule->value2);
14414 #ifdef __FEATURE_COMPARE_DOMAIN__
14415                 else if (rule->flag2 == RULE_TYPE_COMPARE_DOMAIN)
14416                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND (full_address_from like \'@%%%q\' OR full_address_from like \'@%%%q>%%\')", rule->value2, rule->value2);
14417 #endif /*__FEATURE_COMPARE_DOMAIN__ */
14418                 else /*  RULE_TYPE_EXACTLY */
14419                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND full_address_from = \'%q\'", rule->value2);
14420         }
14421
14422         if (rule->type == EMAIL_PRIORITY_SENDER) {
14423                 if (rule->flag2 == RULE_TYPE_INCLUDES)
14424                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND full_address_from like \'%%%q%%\'", rule->value2);
14425                 else /*  RULE_TYPE_EXACTLY */
14426                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND full_address_from = \'%q\' OR email_address_sender = \'%q\'", rule->value2, rule->value2);
14427
14428                 tag_id = PRIORITY_SENDER_TAG_ID;
14429         }
14430
14431         /* prevent 34361 */
14432         if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14433                 strcat(sql_query_string, where_pararaph);
14434
14435         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14436
14437         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14438         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14439         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
14440                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14441
14442         EM_DEBUG_LOG("Count of mails [%d]", count);
14443
14444         if (count) {
14445                 mail_list = malloc(sizeof(int) * count);
14446                 if (mail_list == NULL) {
14447                         EM_DEBUG_EXCEPTION("malloc failed for mail_list.");
14448                         error = EMAIL_ERROR_OUT_OF_MEMORY;
14449                         goto FINISH_OFF;
14450                 }
14451
14452                 col_index = 1;
14453
14454                 for (i = 0; i < count; i++)
14455                         _get_table_field_data_int(result, &(mail_list[i]), col_index++);
14456
14457                 memset(sql_query_string, 0x00, QUERY_SIZE);
14458                 if (reset) {
14459                         switch (rule->action_type) {
14460                         case EMAIL_FILTER_MOVE:
14461                                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = 0 ");
14462                                 break;
14463                         case EMAIL_FILTER_BLOCK:
14464                         default:
14465                                 EM_DEBUG_LOG("Not support : action_type[%d]", rule->action_type);
14466                                 ret = true;
14467                                 goto FINISH_OFF;
14468                         }
14469                 } else {
14470                         switch (rule->action_type) {
14471                         case EMAIL_FILTER_MOVE:
14472                                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = %d ", tag_id);
14473                                 break;
14474                         case EMAIL_FILTER_BLOCK:
14475                                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET mailbox_id = %d, mailbox_type = %d ", dest_mailbox_id, dest_mailbox_type);
14476                                 break;
14477                         default:
14478                                 EM_DEBUG_LOG("Not support");
14479                                 ret = true;
14480                                 goto FINISH_OFF;
14481                         }
14482                 }
14483                 /* prevent 34361 */
14484                 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14485                         strcat(sql_query_string, where_pararaph);
14486
14487                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14488                 if (error != EMAIL_ERROR_NONE) {
14489                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14490                                 goto FINISH_OFF;
14491                 }
14492
14493 #ifdef __FEATURE_BODY_SEARCH__
14494                 /* Updating mail_text_tbl */
14495                 if (rule->action_type == EMAIL_FILTER_BLOCK) {
14496                         memset(sql_query_string, 0x00, QUERY_SIZE);
14497                         SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_text_tbl SET mailbox_id = %d ", dest_mailbox_id);
14498                         if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14499                                 strcat(sql_query_string, where_pararaph);
14500
14501                         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14502                         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14503                         if (error != EMAIL_ERROR_NONE) {
14504                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14505                                 goto FINISH_OFF;
14506                         }
14507                 }
14508 #endif
14509         }
14510
14511         ret = true;
14512
14513 FINISH_OFF:
14514
14515         if (ret) {
14516                 if (filtered_mail_id_list)
14517                         *filtered_mail_id_list = mail_list;
14518
14519                 if (count_of_mails)
14520                         *count_of_mails = count;
14521         } else
14522                 EM_SAFE_FREE(mail_list);
14523
14524         sqlite3_free_table(result);
14525         result = NULL;
14526
14527
14528         EM_SAFE_FREE(where_pararaph);
14529
14530         if (err_code != NULL)
14531                 *err_code = error;
14532
14533         EM_DEBUG_FUNC_END("ret [%d]", ret);
14534         return ret;
14535 }
14536
14537 #define EMAIL_SLOT_UNIT 25
14538 INTERNAL_FUNC int emstorage_set_mail_slot_size(char *multi_user_name, int account_id, int mailbox_id, int new_slot_size, int transaction, int *err_code)
14539 {
14540         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%p] new_slot_size[%d], err_code[%p]", account_id, mailbox_id, new_slot_size, err_code);
14541         int ret = false, err = EMAIL_ERROR_NONE;
14542         int where_pararaph_length = 0;
14543         char *where_pararaph = NULL;
14544         char sql_query_string[QUERY_SIZE] = {0, };
14545         int and = 0;
14546         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14547
14548         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
14549
14550         if (new_slot_size > 0)
14551                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = %d ", new_slot_size);
14552         else if (new_slot_size == 0)
14553                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", EMAIL_SLOT_UNIT);
14554         else
14555                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", new_slot_size * -1);
14556
14557
14558         if (mailbox_id)
14559                 where_pararaph_length = 80;
14560         else
14561                 where_pararaph_length = 50;
14562
14563         if (new_slot_size == 0)
14564                 where_pararaph_length += 70;
14565
14566         where_pararaph = malloc(sizeof(char) * where_pararaph_length);
14567         if (where_pararaph == NULL) {
14568                 EM_DEBUG_EXCEPTION("Memory allocation failed for where_pararaph");
14569                 err = EMAIL_ERROR_OUT_OF_MEMORY;
14570                 goto FINISH_OFF;
14571         }
14572         memset(where_pararaph, 0x00, where_pararaph_length);
14573
14574         if (account_id > ALL_ACCOUNT) {
14575                 and = 1;
14576                 if (mailbox_id)
14577                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_id = %d ", mailbox_id);
14578                 else
14579                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d ", account_id);
14580         }
14581
14582         if (new_slot_size == 0)
14583                 SNPRINTF(where_pararaph + EM_SAFE_STRLEN(where_pararaph), where_pararaph_length - EM_SAFE_STRLEN(where_pararaph), " %s total_mail_count_on_server > mail_slot_size ", (and ? "AND" : "WHERE"));
14584
14585         if (strlen(sql_query_string) + EM_SAFE_STRLEN(where_pararaph) < QUERY_SIZE) /* prevent 34363 */
14586                 strcat(sql_query_string, where_pararaph);
14587         else {
14588                 EM_DEBUG_EXCEPTION("Query buffer overflowed !!!");
14589                 err = EMAIL_ERROR_OUT_OF_MEMORY;
14590                 goto FINISH_OFF;
14591         }
14592
14593         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14594         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14595         if (err != EMAIL_ERROR_NONE) {
14596                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
14597                         goto FINISH_OFF;
14598         }
14599
14600         ret = true;
14601
14602 FINISH_OFF:
14603         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
14604
14605         EM_SAFE_FREE(where_pararaph);
14606
14607         if (err_code != NULL)
14608                 *err_code = err;
14609
14610         EM_DEBUG_FUNC_END("ret [%d]", ret);
14611         return ret;
14612 }
14613
14614 INTERNAL_FUNC int emstorage_add_meeting_request(char *multi_user_name, int account_id, int input_mailbox_id, email_meeting_request_t* meeting_req, int transaction, int *err_code)
14615 {
14616         EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], meeting_req[%p], transaction[%d], err_code[%p]", account_id, input_mailbox_id, meeting_req, transaction, err_code);
14617
14618         if (!meeting_req || meeting_req->mail_id <= 0) {
14619                 if (meeting_req)
14620                 EM_DEBUG_EXCEPTION("mail_id[%]d", meeting_req->mail_id);
14621
14622                 if (err_code != NULL)
14623                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14624
14625                 return false;
14626         }
14627
14628         int rc = -1;
14629         int ret = false;
14630         int error = EMAIL_ERROR_NONE;
14631         DB_STMT hStmt = NULL;
14632         char sql_query_string[QUERY_SIZE] = {0, };
14633         int col_index = 0;
14634         time_t temp_unix_time = 0;
14635
14636         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14637         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14638
14639         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14640                 "INSERT INTO mail_meeting_tbl VALUES "
14641                 "(?"            /*  mail_id */
14642                 ", ?"           /*  account_id */
14643                 ", ?"           /*  mailbox_id */
14644                 ", ?"           /*  meeting_response */
14645                 ", ?"           /*  start_time */
14646                 ", ?"           /*  end_time */
14647                 ", ?"           /*  location */
14648                 ", ?"           /*  global_object_id */
14649                 ", ?"           /*  offset */
14650                 ", ?"           /*  standard_name */
14651                 ", ?"           /*  standard_time_start_date */
14652                 ", ?"           /*  standard_biad */
14653                 ", ?"           /*  daylight_name */
14654                 ", ?"           /*  daylight_time_start_date */
14655                 ", ?"           /*  daylight_bias */
14656                 ")");
14657
14658         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14659         if (rc != SQLITE_OK) {
14660                 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
14661                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
14662
14663                 error = EMAIL_ERROR_DB_FAILURE;
14664                 goto FINISH_OFF;
14665         }
14666
14667         col_index = 0;
14668         /*
14669         EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
14670         EM_DEBUG_LOG_SEC(">>>>> account_id[%d]", account_id);
14671         EM_DEBUG_LOG_SEC(">>>>> mailbox_name[%s]", mailbox_name);
14672         EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
14673         EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
14674         EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
14675         EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
14676         EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
14677         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
14678         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
14679         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
14680         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
14681         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
14682         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
14683         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
14684         */
14685         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->mail_id);
14686         _bind_stmt_field_data_int(hStmt, col_index++, account_id);
14687         _bind_stmt_field_data_int(hStmt, col_index++, input_mailbox_id);
14688         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
14689
14690         temp_unix_time = timegm(&(meeting_req->start_time));
14691         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14692         temp_unix_time = timegm(&(meeting_req->end_time));
14693         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14694
14695         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->location, 0, LOCATION_LEN_IN_MAIL_MEETING_TBL);
14696         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->global_object_id, 0, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
14697
14698         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
14699         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.standard_name, 0, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
14700         temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
14701         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14702         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
14703
14704         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.daylight_name, 0, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
14705         temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
14706         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14707         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
14708
14709
14710         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14711         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
14712                 ("sqlite3_step fail:%d", rc));
14713         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14714                 ("sqlite3_step fail:%d", rc));
14715
14716         ret = true;
14717
14718 FINISH_OFF:
14719         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14720         if (hStmt != NULL) {
14721                 rc = sqlite3_finalize(hStmt);
14722                 if (rc != SQLITE_OK) {
14723                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14724                         error = EMAIL_ERROR_DB_FAILURE;
14725                 }
14726         }
14727
14728         if (err_code != NULL)
14729                 *err_code = error;
14730
14731         EM_DEBUG_FUNC_END("ret [%d]", ret);
14732         return ret;
14733 }
14734
14735 INTERNAL_FUNC int emstorage_query_meeting_request(char *multi_user_name, const char *conditional_clause, email_meeting_request_t **output_meeting_req, int *output_result_count, int transaction)
14736 {
14737         EM_DEBUG_FUNC_BEGIN("conditional_clause[%s] output_meeting_req[%p] output_result_count[%p] transaction[%d]", conditional_clause, output_meeting_req, output_result_count, transaction);
14738
14739         int i = 0;
14740         int col_index = 0;
14741         int rc;
14742         int count = 0;
14743         int dummy = 0;
14744         int err = EMAIL_ERROR_NONE;
14745         char **result = NULL;
14746         char sql_query_string[QUERY_SIZE] = {0, };
14747         email_meeting_request_t* p_temp_meeting_req = NULL;
14748         sqlite3 *local_db_handle = NULL;
14749         time_t temp_unix_time;
14750
14751         if (conditional_clause == NULL || output_meeting_req == NULL || output_result_count == NULL) {
14752                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14753                 err = EMAIL_ERROR_INVALID_PARAM;
14754                 goto FINISH_OFF;
14755         }
14756
14757         col_index = _field_count_of_table[CREATE_TABLE_MAIL_MEETING_TBL];
14758         EM_DEBUG_LOG("col_index [%d]", col_index);
14759
14760         local_db_handle = emstorage_get_db_connection(multi_user_name);
14761
14762         EMSTORAGE_START_READ_TRANSACTION(transaction);
14763
14764         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_meeting_tbl %s", conditional_clause);
14765
14766         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14767
14768         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14769         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
14770                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14771
14772         if (!count) {
14773                 EM_DEBUG_EXCEPTION("No meeting_request found...");
14774                 err = EMAIL_ERROR_DATA_NOT_FOUND;
14775                 goto FINISH_OFF;
14776         }
14777
14778         EM_DEBUG_LOG("There are [%d] meeting requests.", count);
14779         if (!(p_temp_meeting_req = (email_meeting_request_t*)em_malloc(sizeof(email_meeting_request_t) * count))) {
14780                 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
14781                 err = EMAIL_ERROR_OUT_OF_MEMORY;
14782                 goto FINISH_OFF;
14783         }
14784
14785         EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
14786
14787         for (i = 0; i < count; i++) {
14788                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].mail_id), col_index++);
14789                 _get_table_field_data_int(result, &dummy, col_index++); /* account_id. but why should this field exist in DB table? */
14790                 _get_table_field_data_int(result, &dummy, col_index++); /* mailbox_id */
14791                 _get_table_field_data_int(result, (int*)&(p_temp_meeting_req[i].meeting_response), col_index++);
14792                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* start time */
14793                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].start_time));
14794                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* end time */
14795                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].end_time));
14796                 _get_table_field_data_string(result, &p_temp_meeting_req[i].location, 1, col_index++);
14797                 _get_table_field_data_string(result, &p_temp_meeting_req[i].global_object_id, 1, col_index++);
14798                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.offset_from_GMT), col_index++);
14799                 _get_table_field_data_string_without_allocation(result, p_temp_meeting_req[i].time_zone.standard_name, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL, 1, col_index++);
14800                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14801                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.standard_time_start_date));
14802                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.standard_bias), col_index++);
14803                 _get_table_field_data_string_without_allocation(result, p_temp_meeting_req[i].time_zone.daylight_name, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL, 1, col_index++);
14804                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14805                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.daylight_time_start_date));
14806                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.daylight_bias), col_index++);
14807         }
14808
14809
14810 FINISH_OFF:
14811         if (result)
14812                 sqlite3_free_table(result);
14813
14814         if (err == EMAIL_ERROR_NONE) {
14815                 if (p_temp_meeting_req)
14816                         *output_meeting_req = p_temp_meeting_req;
14817                 *output_result_count = count;
14818         } else
14819                 EM_SAFE_FREE(p_temp_meeting_req);
14820
14821         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14822
14823 //      sqlite3_db_release_memory(local_db_handle);
14824
14825         EM_DEBUG_FUNC_END("err [%d]", err);
14826         return err;
14827 }
14828
14829 INTERNAL_FUNC int emstorage_get_meeting_request(char *multi_user_name, int mail_id, email_meeting_request_t ** meeting_req, int transaction, int *err_code)
14830 {
14831         EM_DEBUG_FUNC_BEGIN();
14832
14833         int count = 0;
14834         int ret = false;
14835         int error = EMAIL_ERROR_NONE;
14836         char conditional_clause[QUERY_SIZE] = {0, };
14837
14838         EM_IF_NULL_RETURN_VALUE(meeting_req, false);
14839
14840
14841         SNPRINTF(conditional_clause, QUERY_SIZE, " WHERE mail_id = %d", mail_id);
14842         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
14843
14844         if ((error = emstorage_query_meeting_request(multi_user_name, conditional_clause, meeting_req, &count, transaction)) != EMAIL_ERROR_NONE) {
14845                 EM_DEBUG_EXCEPTION("emstorage_query_meeting_request failed. [%d]", error);
14846                 goto FINISH_OFF;
14847         }
14848
14849         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->mail_id[%d]", (*meeting_req)->mail_id);
14850         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->meeting_response[%d]", (*meeting_req)->meeting_response);
14851         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->start_time[%s]", asctime(&((*meeting_req)->start_time)));
14852         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->end_time[%s]", asctime(&((*meeting_req)->end_time)));
14853         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->location[%s]", (*meeting_req)->location);
14854         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->global_object_id[%s]", (*meeting_req)->global_object_id);
14855         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.offset_from_GMT[%d]", (*meeting_req)->time_zone.offset_from_GMT);
14856         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_name[%s]", (*meeting_req)->time_zone.standard_name);
14857         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.standard_time_start_date)));
14858         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_bias[%d]", (*meeting_req)->time_zone.standard_bias);
14859         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_name[%s]", (*meeting_req)->time_zone.daylight_name);
14860         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.daylight_time_start_date)));
14861         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_bias[%d]", (*meeting_req)->time_zone.daylight_bias);
14862         ret = true;
14863
14864 FINISH_OFF:
14865
14866         if (err_code != NULL)
14867                 *err_code = error;
14868
14869         EM_DEBUG_FUNC_END("ret [%d]", ret);
14870         return ret;
14871 }
14872
14873 INTERNAL_FUNC int emstorage_update_meeting_request(char *multi_user_name, email_meeting_request_t* meeting_req, int transaction, int *err_code)
14874 {
14875         EM_DEBUG_FUNC_BEGIN("meeting_req[%p], transaction[%d], err_code[%p]", meeting_req, transaction, err_code);
14876
14877         int ret = false;
14878         int error = EMAIL_ERROR_NONE;
14879         int rc;
14880         DB_STMT hStmt = NULL;
14881         char sql_query_string[QUERY_SIZE] = {0, };
14882         time_t temp_unix_time = 0;
14883
14884         if (!meeting_req) {
14885                 EM_DEBUG_EXCEPTION("Invalid Parameter!");
14886                 if (err_code != NULL)
14887                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14888                 return false;
14889         }
14890
14891         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14892         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14893
14894         memset(sql_query_string, 0x00, sizeof(sql_query_string));
14895         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14896                 "UPDATE mail_meeting_tbl "
14897                 "SET "
14898                 /* "  account_id = ?, "         //  not update here, this can be changed when move or copy */
14899                 /* "  mailbox_name = ?, "               //  not update here, this can be changed when move or copy */
14900                 "  meeting_response = ?, "
14901                 "  start_time = ?, "
14902                 "  end_time = ?, "
14903                 "  location = ?, "
14904                 "  global_object_id = ?, "
14905                 "  offset = ?, "
14906                 "  standard_name = ?, "
14907                 "  standard_time_start_date = ?, "
14908                 "  standard_bias = ?, "
14909                 "  daylight_name = ?, "
14910                 "  daylight_time_start_date = ?, "
14911                 "  daylight_bias = ? "
14912                 "WHERE mail_id = %d",
14913                 meeting_req->mail_id);
14914
14915         EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
14916
14917         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14918         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14919                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14920 /*
14921         EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
14922         EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
14923         EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
14924         EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
14925         EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
14926         EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
14927         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
14928         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
14929         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
14930         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
14931         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
14932         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
14933         EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
14934 */
14935         int col_index = 0;
14936
14937         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
14938         temp_unix_time = timegm(&(meeting_req->start_time));
14939         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14940         temp_unix_time = timegm(&(meeting_req->end_time));
14941         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14942         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->location, 1, LOCATION_LEN_IN_MAIL_MEETING_TBL);
14943         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->global_object_id, 1, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
14944         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
14945         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.standard_name, 1, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
14946         temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
14947         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14948         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
14949         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.daylight_name, 1, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
14950         temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
14951         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14952         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
14953
14954
14955         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14956         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14957                 ("sqlite3_step fail:%d", rc));
14958         ret = true;
14959
14960
14961 FINISH_OFF:
14962         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14963
14964         if (hStmt != NULL) {
14965                 rc = sqlite3_finalize(hStmt);
14966                 if (rc != SQLITE_OK) {
14967                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14968                         error = EMAIL_ERROR_DB_FAILURE;
14969                 }
14970         }
14971
14972         if (err_code != NULL)
14973                 *err_code = error;
14974
14975         EM_DEBUG_FUNC_END("ret [%d]", ret);
14976         return ret;
14977 }
14978
14979 INTERNAL_FUNC int emstorage_delete_meeting_request(char *multi_user_name, int account_id, int mail_id, int input_mailbox_id, int transaction, int *err_code)
14980 {
14981         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], input_mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mail_id, input_mailbox_id, transaction, err_code);
14982
14983         if (account_id < ALL_ACCOUNT || mail_id < 0) {
14984                 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d]", account_id, mail_id);
14985
14986                 if (err_code != NULL)
14987                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14988                 return false;
14989         }
14990
14991         int ret = false;
14992         int error = EMAIL_ERROR_NONE;
14993         int and = false;
14994         char sql_query_string[QUERY_SIZE] = {0, };
14995
14996         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14997         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14998
14999         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_meeting_tbl ");
15000
15001         if (account_id != ALL_ACCOUNT) {                /*  NOT '0' means a specific account. '0' means all account */
15002                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " WHERE account_id = %d", account_id);
15003                 and = true;
15004         }
15005         if (mail_id > 0) {
15006                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s mail_id = %d", (and ? "AND" : "WHERE"), mail_id);
15007                 and = true;
15008         }
15009         if (input_mailbox_id > 0) {             /*  0 means all mailbox_id */
15010                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " %s mailbox_id = '%d'",  (and ? "AND" : "WHERE"), input_mailbox_id);
15011         }
15012
15013         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15014         if (error != EMAIL_ERROR_NONE) {
15015                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
15016                         goto FINISH_OFF;
15017         }
15018
15019         ret = true;
15020
15021 FINISH_OFF:
15022         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15023
15024         if (err_code)
15025                 *err_code = error;
15026
15027         EM_DEBUG_FUNC_END("ret [%d]", ret);
15028         return ret;
15029 }
15030
15031
15032 INTERNAL_FUNC void emstorage_free_meeting_request(email_meeting_request_t *meeting_req)
15033 {
15034         EM_DEBUG_FUNC_BEGIN();
15035
15036         if (!meeting_req) return;
15037
15038         EM_SAFE_FREE(meeting_req->location);
15039         EM_SAFE_FREE(meeting_req->global_object_id);
15040
15041         EM_DEBUG_FUNC_END();
15042 }
15043
15044 INTERNAL_FUNC int emstorage_get_overflowed_mail_id_list(char *multi_user_name, int account_id, int input_mailbox_id, int mail_slot_size, int **mail_id_list, int *mail_id_count, int transaction, int *err_code)
15045 {
15046         EM_DEBUG_FUNC_BEGIN("account_id [%d], input_mailbox_id [%d], mail_slot_size [%d], mail_id_list [%p], mail_id_count [%p], transaction [%d], err_code [%p]", account_id, input_mailbox_id, mail_slot_size, mail_id_list, mail_id_count, transaction, err_code);
15047         EM_PROFILE_BEGIN(profile_emstorage_get_overflowed_mail_id_list);
15048         char sql_query_string[QUERY_SIZE] = {0, };
15049         char **result = NULL;
15050         int rc = -1, ret = false;
15051         int error = EMAIL_ERROR_NONE;
15052         int counter = 0, col_index = 0;
15053         int result_mail_id_count = 0;
15054         int *result_mail_id_list = NULL;
15055
15056         if (input_mailbox_id <= 0 || !mail_id_list || !mail_id_count || account_id < 1) {
15057                 EM_DEBUG_EXCEPTION("Invalid Parameter");
15058                 if (err_code)
15059                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15060                 return false;
15061         }
15062
15063         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl WHERE account_id = %d AND mailbox_id = %d ORDER BY date_time DESC LIMIT %d, 10000", account_id, input_mailbox_id, mail_slot_size);
15064
15065         EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
15066
15067         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15068         EMSTORAGE_START_READ_TRANSACTION(transaction);
15069
15070         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_mail_id_count, 0, NULL), rc);
15071         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15072                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15073
15074         if (!result_mail_id_count) {
15075                 EM_DEBUG_LOG("No mail found...");
15076                 ret = false;
15077                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
15078                 goto FINISH_OFF;
15079         }
15080
15081         EM_DEBUG_LOG("There are [%d] overflowed mails in mailbox_id [%d]", result_mail_id_count, input_mailbox_id);
15082
15083         if (!(result_mail_id_list = (int *)malloc(sizeof(int) * result_mail_id_count))) {
15084                 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
15085                 error = EMAIL_ERROR_OUT_OF_MEMORY;
15086                 sqlite3_free_table(result);
15087                 goto FINISH_OFF;
15088         }
15089
15090         memset(result_mail_id_list, 0x00, sizeof(int) * result_mail_id_count);
15091
15092         col_index = 1;
15093
15094         for (counter = 0; counter < result_mail_id_count; counter++)
15095                 _get_table_field_data_int(result, result_mail_id_list + counter, col_index++);
15096
15097         ret = true;
15098
15099 FINISH_OFF:
15100         EM_DEBUG_LOG("finish off [%d]", ret);
15101
15102         if (result)
15103                 sqlite3_free_table(result);
15104
15105         if (ret == true) {
15106                 *mail_id_list = result_mail_id_list;
15107                 *mail_id_count = result_mail_id_count;
15108         } else
15109                 EM_SAFE_FREE(result_mail_id_list);
15110
15111         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15112
15113         if (err_code != NULL)
15114                 *err_code = error;
15115
15116         EM_PROFILE_END(profile_emstorage_get_overflowed_mail_id_list);
15117         EM_DEBUG_FUNC_END("ret [%d]", ret);
15118         return ret;
15119 }
15120
15121 INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(char *multi_user_name, int mail_id, int *thread_id, int *err_code)
15122 {
15123         EM_DEBUG_FUNC_BEGIN("mail_id[%d], thread_id[%p], err_code[%p]", mail_id, thread_id, err_code);
15124
15125         int rc = -1, ret = false;
15126         int err = EMAIL_ERROR_NONE;
15127         char sql_query_string[QUERY_SIZE] = {0, };
15128         char **result;
15129         int result_count = 0;
15130
15131         if (mail_id == 0 || thread_id == NULL) {
15132                 EM_DEBUG_EXCEPTION("Invalid Parameter");
15133                 if (err_code)
15134                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15135                 return false;
15136         }
15137
15138         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15139
15140         memset(sql_query_string, 0, QUERY_SIZE);
15141         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT thread_id FROM mail_tbl WHERE mail_id = %d", mail_id);
15142
15143         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
15144         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
15145         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15146                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15147
15148         if (!result_count) {
15149                 EM_DEBUG_LOG("No mail found...");
15150                 ret = false;
15151                 err = EMAIL_ERROR_MAIL_NOT_FOUND;
15152                 /* sqlite3_free_table(result); */
15153                 goto FINISH_OFF;
15154         }
15155
15156         _get_table_field_data_int(result, thread_id, 1);
15157
15158         sqlite3_free_table(result);
15159
15160         ret = true;
15161
15162 FINISH_OFF:
15163
15164         if (err_code != NULL)
15165                 *err_code = err;
15166
15167         EM_DEBUG_FUNC_END("ret [%d]", ret);
15168         return ret;
15169 }
15170
15171 INTERNAL_FUNC int emstorage_update_latest_thread_mail(char *multi_user_name,
15172                                                                                                                 int account_id,
15173                                                                                                                 int mailbox_id,
15174                                                                                                                 int mailbox_type,
15175                                                                                                                 int thread_id,
15176                                                                                                                 int *updated_thread_id,
15177                                                                                                                 int latest_mail_id,
15178                                                                                                                 int thread_item_count,
15179                                                                                                                 int noti_type,
15180                                                                                                                 int transaction,
15181                                                                                                                 int *err_code)
15182 {
15183         EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id [%d], thread_id[%d], updated_thread_id[%p], "
15184                                                 "latest_mail_id [%d], thread_item_count[%d], err_code[%p]",
15185                                                 account_id, mailbox_id, thread_id, updated_thread_id,
15186                                                 latest_mail_id, thread_item_count, err_code);
15187
15188         int rc = -1, ret = false;
15189         int err = EMAIL_ERROR_NONE;
15190         char sql_query_string[QUERY_SIZE] = {0, };
15191         char **result = NULL;
15192         int result_count = 0;
15193
15194         if (thread_id == 0) {
15195                 EM_DEBUG_EXCEPTION("Invalid Parameter");
15196                 if (err_code)
15197                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15198                 return false;
15199         }
15200
15201         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15202
15203         if (thread_item_count == 0 && latest_mail_id == 0) {
15204                 memset(sql_query_string, 0, QUERY_SIZE);
15205                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id, count(*) FROM (SELECT account_id, mail_id, thread_id, mailbox_id FROM mail_tbl ORDER BY date_time) WHERE account_id = %d AND thread_id = %d AND mailbox_id = %d", account_id, thread_id, mailbox_id);
15206
15207                 /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
15208                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
15209                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15210                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15211                 EM_DEBUG_LOG("result_count[%d]", result_count);
15212                 if (result_count == 0) {
15213                         EM_DEBUG_LOG("No mail found...");
15214                         ret = false;
15215                         if (err_code)
15216                                 *err_code =  EMAIL_ERROR_MAIL_NOT_FOUND;
15217                         sqlite3_free_table(result);
15218                         return false;
15219                 }
15220
15221                 _get_table_field_data_int(result, &latest_mail_id, 2);
15222                 _get_table_field_data_int(result, &thread_item_count, 3);
15223
15224                 EM_DEBUG_LOG("latest_mail_id[%d]", latest_mail_id);
15225                 EM_DEBUG_LOG("thread_item_count[%d]", thread_item_count);
15226
15227                 sqlite3_free_table(result);
15228         }
15229
15230         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15231
15232         if (thread_item_count < 0) {
15233                 memset(sql_query_string, 0, QUERY_SIZE);
15234                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = 0 WHERE account_id = %d AND thread_id = %d", account_id, thread_id);
15235                 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15236                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15237                 if (err != EMAIL_ERROR_NONE) {
15238                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15239                                 goto FINISH_OFF;
15240                 }
15241         } else if (thread_id != latest_mail_id) {
15242                 /* Initialize the thread id */
15243                 memset(sql_query_string, 0, QUERY_SIZE);
15244                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = 0, thread_id = %d WHERE account_id = %d AND mailbox_id = %d AND thread_id = %d", latest_mail_id, account_id, mailbox_id, thread_id);
15245                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15246                 if (err != EMAIL_ERROR_NONE) {
15247                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15248                                 goto FINISH_OFF;
15249                 }
15250
15251                 /* update the thread item count */
15252                 memset(sql_query_string, 0, QUERY_SIZE);
15253                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = %d WHERE account_id = %d AND mail_id = %d ", thread_item_count, account_id, latest_mail_id);
15254                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15255                 if (err != EMAIL_ERROR_NONE) {
15256                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15257                                 goto FINISH_OFF;
15258                 }
15259         } else {
15260                 memset(sql_query_string, 0, QUERY_SIZE);
15261                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = %d WHERE account_id = %d AND mail_id = %d ", thread_item_count, account_id, latest_mail_id);
15262                 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15263                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15264                 if (err != EMAIL_ERROR_NONE) {
15265                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15266                                 goto FINISH_OFF;
15267                 }
15268         }
15269         ret = true;
15270
15271 FINISH_OFF:
15272         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15273
15274         if (thread_id != latest_mail_id) {
15275                 if (err == EMAIL_ERROR_NONE) {
15276                         EM_DEBUG_LOG("noti_type[%d]", noti_type);
15277
15278                         if (latest_mail_id > 0 && thread_id > 0 && noti_type > 0) {
15279                                 char mailbox_id_str[25] = {0,};
15280                                 snprintf(mailbox_id_str, sizeof(mailbox_id_str), "%d", mailbox_id);
15281                                 if (!emcore_notify_storage_event(noti_type, thread_id, latest_mail_id, mailbox_id_str, account_id))
15282                                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventfailed [NOTI_THREAD_ID_CHANGED] >>>> ");
15283
15284                                 if (updated_thread_id) *updated_thread_id = latest_mail_id;
15285                         }
15286                 }
15287         } else if (thread_item_count >= 0) {
15288                 if (err == EMAIL_ERROR_NONE) {
15289                         char parameter_string[500] = {0,};
15290                         SNPRINTF(parameter_string, sizeof(parameter_string), "%s%c%d", "thread_item_count", 0x01, latest_mail_id);
15291                         if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT, parameter_string, thread_item_count))
15292                                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventfailed [NOTI_MAIL_FIELD_UPDATE] >>>> ");
15293                 }
15294         }
15295
15296         if (err_code != NULL)
15297                 *err_code = err;
15298
15299         EM_DEBUG_FUNC_END("ret [%d]", ret);
15300         return ret;
15301 }
15302
15303 INTERNAL_FUNC int emstorage_update_thread_id_of_mail(char *multi_user_name, int account_id, int mailbox_id, int mail_id, int thread_id, int thread_item_count, int transaction, int *err_code)
15304 {
15305         EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id [%d], mail_id[%d], thread_id[%d], thread_item_count[%d], err_code[%p]", account_id, mailbox_id, mail_id, thread_id, thread_item_count, err_code);
15306
15307         int ret = false;
15308         int err = EMAIL_ERROR_NONE;
15309         char sql_query_string[QUERY_SIZE] = {0, };
15310
15311         if (thread_id == 0) {
15312                 EM_DEBUG_EXCEPTION("Invalid Parameter");
15313                 if (err_code)
15314                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15315                 return false;
15316         }
15317
15318         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15319
15320         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15321
15322         memset(sql_query_string, 0, QUERY_SIZE);
15323         SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET thread_item_count = %d, thread_id = %d WHERE account_id = %d AND mail_id = %d", thread_item_count, thread_id, account_id, mail_id);
15324         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15325         if (err != EMAIL_ERROR_NONE) {
15326                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15327                 goto FINISH_OFF;
15328         }
15329
15330         ret = true;
15331
15332 FINISH_OFF:
15333         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15334
15335         if (err_code != NULL)
15336                 *err_code = err;
15337
15338         EM_DEBUG_FUNC_END("ret [%d]", ret);
15339         return ret;
15340 }
15341
15342 #ifdef __FEATURE_LOCAL_ACTIVITY__
15343 /**
15344   *     emstorage_add_activity - Add Email Local activity during OFFLINE mode
15345   *
15346   */
15347 INTERNAL_FUNC int emstorage_add_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15348 {
15349         EM_DEBUG_FUNC_BEGIN();
15350
15351         EM_DEBUG_LOG(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15352
15353         int rc = -1, ret = false;
15354         int error = EMAIL_ERROR_NONE;
15355         DB_STMT hStmt = NULL;
15356         char sql_query_string[8192] = { 0x00, };
15357         int i = 0;
15358
15359         if (!local_activity) {
15360                 EM_DEBUG_EXCEPTION(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15361                 if (err_code != NULL)
15362                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15363                 return false;
15364         }
15365
15366         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15367
15368         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15369
15370         memset(sql_query_string, 0x00 , sizeof(sql_query_string));
15371         SNPRINTF(sql_query_string, sizeof(sql_query_string), "INSERT INTO mail_local_activity_tbl VALUES (?, ?, ?, ?, ?, ?, ?)");
15372
15373         EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ] ", local_activity->activity_id);
15374         EM_DEBUG_LOG(">>>>> MAIL ID [ %d ] ", local_activity->mail_id);
15375         EM_DEBUG_LOG(">>>>> ACCOUNT ID [ %d ] ", local_activity->account_id);
15376         EM_DEBUG_LOG(">>>>> ACTIVITY TYPE [ %d ] ", local_activity->activity_type);
15377         EM_DEBUG_LOG_SEC(">>>>> SERVER MAIL ID [ %s ] ", local_activity->server_mailid);
15378         EM_DEBUG_LOG(">>>>> SOURCE MAILBOX [ %s ] ", local_activity->src_mbox);
15379         EM_DEBUG_LOG(">>>>> DEST MAILBOX   [ %s ] ", local_activity->dest_mbox);
15380
15381         EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15382
15383
15384         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15385         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15386                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15387
15388         EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15389
15390         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
15391         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
15392         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
15393         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
15394         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->server_mailid, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
15395         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->src_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15396         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->dest_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15397
15398
15399         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15400
15401         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
15402                 ("sqlite3_step fail:%d", rc));
15403         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15404                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
15405
15406         ret = true;
15407
15408 FINISH_OFF:
15409         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15410
15411         if (hStmt != NULL) {
15412                 rc = sqlite3_finalize(hStmt);
15413                 if (rc != SQLITE_OK) {
15414                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15415                         error = EMAIL_ERROR_DB_FAILURE;
15416                 }
15417         }
15418
15419         if (err_code != NULL)
15420                 *err_code = error;
15421
15422         EM_DEBUG_FUNC_END("ret [%d]", ret);
15423         return ret;
15424 }
15425
15426 /**
15427   *     emstorage_get_activity - Get the Local activity Information
15428   *
15429   *
15430   */
15431 INTERNAL_FUNC int emstorage_get_activity(int account_id, int activityid, emstorage_activity_tbl_t** activity_list, int *select_num, int transaction, int *err_code)
15432 {
15433         EM_DEBUG_FUNC_BEGIN();
15434
15435         int i = 0, count = 0, rc = -1, ret = false;
15436         int error = EMAIL_ERROR_NONE;
15437         emstorage_activity_tbl_t *p_activity_tbl = NULL;
15438         char sql_query_string[1024] = {0x00, };
15439         char **result = NULL;
15440         int col_index ;
15441
15442         EM_IF_NULL_RETURN_VALUE(activity_list, false);
15443         EM_IF_NULL_RETURN_VALUE(select_num, false);
15444
15445
15446         if (!select_num || !activity_list || account_id <= 0 || activityid < 0) {
15447                 EM_DEBUG_LOG(" select_num[%p], activity_list[%p] account_id [%d] activityid [%d] ", select_num, activity_list, account_id, activityid);
15448                 if (err_code)
15449                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15450                 return false;
15451         }
15452
15453         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15454
15455         EMSTORAGE_START_READ_TRANSACTION(transaction);
15456
15457         memset(sql_query_string, 0x00, sizeof(sql_query_string));
15458
15459         if (activityid == ALL_ACTIVITIES) {
15460                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d order by activity_id", account_id);
15461         } else {
15462                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d AND activity_id = %d ", account_id, activityid);
15463         }
15464
15465         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
15466
15467
15468
15469         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15470         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15471                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15472
15473         col_index = 7;
15474
15475         if (!(p_activity_tbl = (emstorage_activity_tbl_t*)em_malloc(sizeof(emstorage_activity_tbl_t) * count))) {
15476                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
15477                 error = EMAIL_ERROR_OUT_OF_MEMORY;
15478                 goto FINISH_OFF;
15479         }
15480
15481
15482         for (i = 0; i < count; i++) {
15483                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15484                 if (result[col_index])
15485                         p_activity_tbl[i].activity_id = atoi(result[col_index++]);
15486
15487                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15488                 if (result[col_index])
15489                         p_activity_tbl[i].account_id = atoi(result[col_index++]);
15490
15491                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15492                 if (result[col_index])
15493                         p_activity_tbl[i].mail_id = atoi(result[col_index++]);
15494
15495                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15496                 if (result[col_index])
15497                         p_activity_tbl[i].activity_type = atoi(result[col_index++]);
15498
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].server_mailid = EM_SAFE_STRDUP(result[col_index++]);
15503                 else
15504                         col_index++;
15505
15506                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15507                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15508                         p_activity_tbl[i].src_mbox = EM_SAFE_STRDUP(result[col_index++]);
15509                 else
15510                         col_index++;
15511
15512                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15513                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15514                         p_activity_tbl[i].dest_mbox = EM_SAFE_STRDUP(result[col_index++]);
15515                 else
15516                         col_index++;
15517
15518         }
15519
15520         if (result)
15521                 sqlite3_free_table(result);
15522
15523         ret = true;
15524
15525 FINISH_OFF:
15526
15527         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15528
15529         if (ret == true) {
15530                 *activity_list = p_activity_tbl;
15531                 *select_num = count;
15532                 EM_DEBUG_LOG(">>>> COUNT : %d >> ", count);
15533         } else if (p_activity_tbl != NULL) {
15534                 emstorage_free_local_activity(&p_activity_tbl, count, NULL);
15535         }
15536
15537
15538         if (err_code != NULL)
15539                 *err_code = error;
15540
15541         EM_DEBUG_FUNC_END("ret [%d]", ret);
15542         return ret;
15543 }
15544
15545
15546 INTERNAL_FUNC int emstorage_get_next_activity_id(int *activity_id, int *err_code)
15547 {
15548
15549         EM_DEBUG_FUNC_BEGIN();
15550
15551         int ret = false;
15552         int err = EMAIL_ERROR_NONE;
15553         int rc = -1;
15554         char *sql = NULL;
15555         char **result = NULL;
15556
15557         if (NULL == activity_id) {
15558                 EM_DEBUG_EXCEPTION(" activity_id[%p]", activity_id);
15559                 if (err_code)
15560                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15561                 return false;
15562         }
15563
15564         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15565
15566         /*  increase unique id */
15567
15568         sql = "SELECT max(rowid) FROM mail_local_activity_tbl;";
15569
15570         /*  rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL); n EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc); */
15571         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15572                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
15573
15574         if (NULL == result[1])
15575                 rc = 1;
15576         else
15577                 rc = atoi(result[1])+1;
15578
15579         *activity_id = rc;
15580
15581         if (result)
15582                 sqlite3_free_table(result);
15583
15584         ret = true;
15585
15586         FINISH_OFF:
15587
15588         if (err_code)
15589                 *err_code = err;
15590
15591         EM_DEBUG_FUNC_END("ret [%d]", ret);
15592         return ret;
15593
15594 }
15595
15596 INTERNAL_FUNC int emstorage_get_activity_id_list(char *multi_user_name, int account_id, int ** activity_id_list, int *activity_id_count, int lowest_activity_type, int highest_activity_type, int transaction, int *err_code)
15597 {
15598
15599         EM_DEBUG_FUNC_BEGIN();
15600
15601         EM_DEBUG_LOG(" account_id[%d], activity_id_list[%p], activity_id_count[%p] err_code[%p]", account_id,  activity_id_list, activity_id_count, err_code);
15602
15603         if (account_id <= 0 || NULL == activity_id_list || NULL == activity_id_count || lowest_activity_type <= 0 || highest_activity_type <= 0) {
15604                 if (err_code != NULL)
15605                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15606                 return false;
15607         }
15608
15609         int ret = false;
15610         int error = EMAIL_ERROR_NONE;
15611         int i = 0, rc = -1, count = 0;
15612         char sql_query_string[1024] = {0x00, };
15613         int *activity_ids = NULL;
15614         int col_index = 0;
15615         char **result = NULL;
15616         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15617         EMSTORAGE_START_READ_TRANSACTION(transaction);
15618
15619         memset(sql_query_string, 0x00, sizeof(sql_query_string));
15620
15621         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct activity_id FROM mail_local_activity_tbl WHERE account_id = %d AND activity_type >= %d AND activity_type <= %d order by activity_id", account_id, lowest_activity_type, highest_activity_type);
15622
15623         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
15624
15625
15626         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15627         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15628                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15629
15630         col_index = 1;
15631
15632         EM_DEBUG_LOG(" Activity COUNT : %d ... ", count);
15633
15634         if (NULL == (activity_ids = (int *)em_malloc(sizeof(int) * count))) {
15635                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
15636                 error = EMAIL_ERROR_OUT_OF_MEMORY;
15637                 goto FINISH_OFF;
15638         }
15639
15640         for (i = 0; i < count; i++) {
15641                 activity_ids[i] = atoi(result[col_index]);
15642                 col_index++;
15643                 EM_DEBUG_LOG("activity_id %d", activity_ids[i]);
15644         }
15645
15646         ret = true;
15647
15648 FINISH_OFF:
15649
15650
15651         if (ret == true) {
15652                 *activity_id_count = count;
15653                 *activity_id_list = activity_ids;
15654         } else if (activity_ids != NULL) /* Prevent defect - 216566 */
15655                 EM_SAFE_FREE(activity_ids);
15656
15657
15658         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15659         if (err_code != NULL) {
15660                 *err_code = error;
15661         }
15662
15663         EM_DEBUG_FUNC_END("ret [%d]", ret);
15664         return ret;
15665 }
15666
15667 INTERNAL_FUNC int emstorage_free_activity_id_list(int *activity_id_list, int *error_code)
15668 {
15669         EM_DEBUG_FUNC_BEGIN();
15670
15671         int error = EMAIL_ERROR_NONE;
15672         int ret = false;
15673
15674         EM_DEBUG_LOG(" activity_id_list [%p]", activity_id_list);
15675
15676         if (NULL == activity_id_list) {
15677                 error = EMAIL_ERROR_INVALID_PARAM;
15678                 goto FINISH_OFF;
15679         } else {
15680                 EM_SAFE_FREE(activity_id_list);
15681         }
15682
15683
15684         ret = true;
15685
15686         FINISH_OFF:
15687
15688         if (NULL != error_code) {
15689                 *error_code = error;
15690         }
15691
15692         EM_DEBUG_FUNC_END("ret [%d]", ret);
15693         return ret;
15694 }
15695
15696 /**
15697  * emstorage_delete_local_activity - Deletes the Local acitivity Generated based on activity_type
15698  * or based on server mail id
15699  *
15700  */
15701 INTERNAL_FUNC int emstorage_delete_local_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15702 {
15703         EM_DEBUG_FUNC_BEGIN();
15704
15705
15706         EM_DEBUG_LOG(" local_activity[%p] ", local_activity);
15707
15708         if (!local_activity) {
15709                 EM_DEBUG_EXCEPTION(" local_activity[%p] ", local_activity);
15710                 if (err_code != NULL)
15711                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15712                 return false;
15713         }
15714
15715         int rc = -1, ret = false;                       /* Prevent_FIX  */
15716         int err = EMAIL_ERROR_NONE;
15717         int query_and = 0;
15718         int query_where = 0;
15719         char sql_query_string[8192] = { 0x00, };
15720         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15721         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15722
15723         memset(sql_query_string, 0x00, sizeof(sql_query_string));
15724
15725         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_local_activity_tbl ");
15726
15727         EM_DEBUG_LOG_SEC(">>> Query [ %s ] ", sql_query_string);
15728
15729         if (local_activity->account_id) {
15730                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15731                 " WHERE account_id = %d ", local_activity->account_id);
15732                 query_and = 1;
15733                 query_where = 1;
15734         }
15735
15736         if (local_activity->server_mailid) {
15737                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15738                 " %s %s server_mailid = '%s' ", query_where ? "" : "WHERE", query_and ? "AND" : "", local_activity->server_mailid);
15739                 query_and = 1;
15740                 query_where = 1;
15741         }
15742
15743
15744         if (local_activity->mail_id) {
15745                 EM_DEBUG_LOG(">>>> MAIL ID [ %d ] , ACTIVITY TYPE [%d ]", local_activity->mail_id, local_activity->activity_type);
15746
15747                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15748                 " %s %s mail_id = %d  ", query_where ? "" : "WHERE", query_and ? "AND" : "", local_activity->mail_id);
15749
15750                 query_and = 1;
15751                 query_where = 1;
15752
15753         }
15754
15755         if (local_activity->activity_type > 0) {
15756                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15757                 " %s %s activity_type = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "" , local_activity->activity_type);
15758         }
15759
15760         if (local_activity->activity_id > 0) {
15761                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15762                 " %s %s activity_id = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "" , local_activity->activity_id);
15763
15764         }
15765
15766         EM_DEBUG_LOG_SEC(">>>>> Query [ %s ] ", sql_query_string);
15767         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15768         if (error != EMAIL_ERROR_NONE) {
15769                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
15770                         goto FINISH_OFF;
15771         }
15772
15773         rc = sqlite3_changes(local_db_handle);
15774         if (rc == 0) {
15775                 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
15776                 err = EMAIL_ERROR_MAILBOX_NOT_FOUND;
15777         }
15778
15779         ret = true;
15780
15781 FINISH_OFF:
15782         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15783
15784         if (hStmt != NULL) {
15785                 rc = sqlite3_finalize(hStmt);
15786                 if (rc != SQLITE_OK) {
15787                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15788                         error = EMAIL_ERROR_DB_FAILURE;
15789                 }
15790         }
15791
15792         if (err_code != NULL)
15793                 *err_code = err;
15794         EM_DEBUG_FUNC_END("ret [%d]", ret);
15795         return ret;
15796 }
15797
15798 /**
15799 *       emstorage_free_local_activity - Free the Local Activity data
15800 */
15801 INTERNAL_FUNC int emstorage_free_local_activity(emstorage_activity_tbl_t **local_activity_list, int count, int *err_code)
15802 {
15803         EM_DEBUG_FUNC_BEGIN();
15804
15805         EM_DEBUG_LOG(" local_activity_list[%p], count[%d], err_code[%p]", local_activity_list, count, err_code);
15806
15807         int ret = false;
15808         int error = EMAIL_ERROR_INVALID_PARAM;
15809
15810         if (count > 0) {
15811                 if (!local_activity_list || !*local_activity_list) {
15812                         EM_DEBUG_EXCEPTION(" local_activity_list[%p], count[%d]", local_activity_list, count);
15813
15814                         error = EMAIL_ERROR_INVALID_PARAM;
15815                         goto FINISH_OFF;
15816                 }
15817
15818                 emstorage_activity_tbl_t* p = *local_activity_list;
15819                 int i = 0;
15820                 if (p) {
15821                         for (; i < count; i++) {
15822                                 EM_SAFE_FREE(p[i].dest_mbox);
15823                                 EM_SAFE_FREE(p[i].src_mbox);
15824                                 EM_SAFE_FREE(p[i].server_mailid);
15825                         }
15826
15827                         EM_SAFE_FREE(p);
15828                         *local_activity_list = NULL;
15829                 }
15830         }
15831
15832         ret = true;
15833
15834 FINISH_OFF:
15835
15836         if (err_code != NULL)
15837                 *err_code = error;
15838
15839         EM_DEBUG_FUNC_END("ret [%d]", ret);
15840         return ret;
15841
15842 }
15843 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
15844
15845
15846 static int _get_key_value_string_for_list_filter_rule(email_list_filter_rule_t *input_list_filter_rule, char **output_key_value_string)
15847 {
15848         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_key_value_string [%p]", input_list_filter_rule, output_key_value_string);
15849
15850         int  ret = EMAIL_ERROR_NONE;
15851         char key_value_string[QUERY_SIZE] = { 0, };
15852         char *temp_key_value_1 = NULL;
15853         char *temp_key_value_2 = NULL;
15854
15855         if (input_list_filter_rule == NULL || output_key_value_string == NULL) {
15856                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15857                 return EMAIL_ERROR_INVALID_PARAM;
15858         }
15859
15860         switch (input_list_filter_rule->target_attribute) {
15861         case EMAIL_MAIL_ATTRIBUTE_MAIL_ID:
15862         case EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID:
15863         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID:
15864         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE:
15865         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_STATUS:
15866         case EMAIL_MAIL_ATTRIBUTE_REFERENCE_MAIL_ID:
15867         case EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS:
15868         case EMAIL_MAIL_ATTRIBUTE_MAIL_SIZE:
15869         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN:
15870         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML:
15871         case EMAIL_MAIL_ATTRIBUTE_FILE_SIZE:
15872         case EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD:
15873         case EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD:
15874         case EMAIL_MAIL_ATTRIBUTE_FLAGS_FLAGGED_FIELD:
15875         case EMAIL_MAIL_ATTRIBUTE_FLAGS_ANSWERED_FIELD:
15876         case EMAIL_MAIL_ATTRIBUTE_FLAGS_RECENT_FIELD:
15877         case EMAIL_MAIL_ATTRIBUTE_FLAGS_DRAFT_FIELD:
15878         case EMAIL_MAIL_ATTRIBUTE_FLAGS_FORWARDED_FIELD:
15879         case EMAIL_MAIL_ATTRIBUTE_DRM_STATUS:
15880         case EMAIL_MAIL_ATTRIBUTE_PRIORITY:
15881         case EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS:
15882         case EMAIL_MAIL_ATTRIBUTE_LOCK_STATUS:
15883         case EMAIL_MAIL_ATTRIBUTE_REPORT_STATUS:
15884         case EMAIL_MAIL_ATTRIBUTE_ATTACHMENT_COUNT:
15885         case EMAIL_MAIL_ATTRIBUTE_INLINE_CONTENT_COUNT:
15886         case EMAIL_MAIL_ATTRIBUTE_THREAD_ID:
15887         case EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT:
15888         case EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS:
15889         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_CLASS:
15890         case EMAIL_MAIL_ATTRIBUTE_DIGEST_TYPE:
15891         case EMAIL_MAIL_ATTRIBUTE_SMIME_TYPE:
15892         case EMAIL_MAIL_ATTRIBUTE_REMAINING_RESEND_TIMES:
15893         case EMAIL_MAIL_ATTRIBUTE_TAG_ID:
15894         case EMAIL_MAIL_ATTRIBUTE_EAS_DATA_LENGTH_TYPE:
15895                 SNPRINTF(key_value_string, QUERY_SIZE, "%d", input_list_filter_rule->key_value.integer_type_value);
15896                 break;
15897
15898         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
15899         case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
15900         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
15901         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
15902         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
15903         case EMAIL_MAIL_ATTRIBUTE_FROM:
15904         case EMAIL_MAIL_ATTRIBUTE_TO:
15905         case EMAIL_MAIL_ATTRIBUTE_CC:
15906         case EMAIL_MAIL_ATTRIBUTE_BCC:
15907         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
15908                 if (input_list_filter_rule->key_value.string_type_value == NULL) {
15909                         EM_DEBUG_EXCEPTION("Invalid string_type_value [%p]", input_list_filter_rule->key_value.string_type_value);
15910                         ret = EMAIL_ERROR_INVALID_PARAM;
15911                         goto FINISH_OFF;
15912                 }
15913
15914                 temp_key_value_1 = input_list_filter_rule->key_value.string_type_value;
15915
15916                 temp_key_value_2 = em_replace_all_string(temp_key_value_1, "_", "\\_");
15917                 temp_key_value_1 = em_replace_all_string(temp_key_value_2, "%", "\\%");
15918
15919                 if (input_list_filter_rule->rule_type == EMAIL_LIST_FILTER_RULE_INCLUDE)
15920                         SNPRINTF(key_value_string, QUERY_SIZE, "\'%%%s%%\'", temp_key_value_1);
15921                 else
15922                         SNPRINTF(key_value_string, QUERY_SIZE, "\'%s\'", temp_key_value_1);
15923                 break;
15924
15925         case EMAIL_MAIL_ATTRIBUTE_DATE_TIME:
15926         case EMAIL_MAIL_ATTRIBUTE_SCHEDULED_SENDING_TIME:
15927         case EMAIL_MAIL_ATTRIBUTE_REPLIED_TIME:
15928         case EMAIL_MAIL_ATTRIBUTE_FORWARDED_TIME:
15929                 SNPRINTF(key_value_string, QUERY_SIZE, "%d", (int)input_list_filter_rule->key_value.datetime_type_value);
15930                 break;
15931
15932         default:
15933                 ret = EMAIL_ERROR_INVALID_PARAM;
15934                 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
15935                 break;
15936         }
15937
15938         if (ret == EMAIL_ERROR_NONE && EM_SAFE_STRLEN(key_value_string) > 0) {
15939                 *output_key_value_string = strdup(key_value_string);
15940         }
15941
15942 FINISH_OFF:
15943
15944         EM_SAFE_FREE(temp_key_value_1);
15945         EM_SAFE_FREE(temp_key_value_2);
15946
15947         EM_DEBUG_FUNC_END("ret [%d]", ret);
15948         return ret;
15949 }
15950
15951 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
15952 static int _get_cert_password_file_name(int index, char *cert_password_file_name)
15953 {
15954         EM_DEBUG_FUNC_BEGIN("index : [%d]", index);
15955
15956         if (index <= 0 || !cert_password_file_name) {
15957                 EM_DEBUG_EXCEPTION("Invalid parameter");
15958                 return EMAIL_ERROR_INVALID_PARAM;
15959         }
15960
15961         sprintf(cert_password_file_name, ".email_cert_%d", index);
15962
15963         EM_DEBUG_FUNC_END();
15964         return EMAIL_ERROR_NONE;
15965 }
15966 #endif
15967
15968 static int _make_filter_rule_string(email_list_filter_rule_t *input_list_filter_rule, char **output_string)
15969 {
15970         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
15971
15972         int   ret = EMAIL_ERROR_NONE;
15973         int   is_alpha = 0;
15974         int   length_field_name = 0;
15975         int   length_value = 0;
15976         char  result_rule_string[QUERY_SIZE] = { 0 , };
15977         char *mod_field_name_string = NULL;
15978         char *mod_value_string = NULL;
15979         char *temp_field_name_string = NULL;
15980         char *temp_key_value_string = NULL;
15981
15982         if (input_list_filter_rule == NULL || output_string == NULL) {
15983                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
15984                 return  EMAIL_ERROR_INVALID_PARAM;
15985         }
15986
15987         temp_field_name_string = emcore_get_mail_field_name_by_attribute_type(input_list_filter_rule->target_attribute);
15988
15989         if (temp_field_name_string == NULL) {
15990                 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
15991                 return EMAIL_ERROR_INVALID_PARAM;
15992         }
15993
15994         if (_get_key_value_string_for_list_filter_rule(input_list_filter_rule, &temp_key_value_string) != EMAIL_ERROR_NONE || temp_key_value_string == NULL) {
15995                 EM_DEBUG_EXCEPTION("_get_key_value_string_for_list_filter_rule failed");
15996                 return EMAIL_ERROR_INVALID_PARAM;
15997         }
15998
15999         length_field_name = EM_SAFE_STRLEN(temp_field_name_string);
16000         length_value      = EM_SAFE_STRLEN(temp_key_value_string);
16001
16002         switch (input_list_filter_rule->target_attribute) {
16003         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
16004         case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
16005         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
16006         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
16007         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
16008         case EMAIL_MAIL_ATTRIBUTE_FROM:
16009         case EMAIL_MAIL_ATTRIBUTE_TO:
16010         case EMAIL_MAIL_ATTRIBUTE_CC:
16011         case EMAIL_MAIL_ATTRIBUTE_BCC:
16012         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
16013                 is_alpha = 1;
16014                 break;
16015         default:
16016                 is_alpha = 0;
16017                 break;
16018         }
16019
16020         if (is_alpha == 1 && input_list_filter_rule->case_sensitivity == false) {
16021                 length_field_name += strlen("UPPER() ");
16022                 length_value      += strlen("UPPER() ");
16023                 mod_field_name_string = em_malloc(sizeof(char) * length_field_name);
16024                 if (mod_field_name_string == NULL) {
16025                         EM_DEBUG_EXCEPTION("em_mallocfailed");
16026                         EM_SAFE_FREE(temp_field_name_string);
16027                         ret = EMAIL_ERROR_OUT_OF_MEMORY;
16028                         goto FINISH_OFF;
16029                 }
16030
16031                 mod_value_string = em_malloc(sizeof(char) * length_value);
16032                 if (mod_value_string == NULL) {
16033                         EM_DEBUG_EXCEPTION("em_mallocfailed");
16034                         EM_SAFE_FREE(temp_field_name_string);
16035                         ret = EMAIL_ERROR_OUT_OF_MEMORY;
16036                         goto FINISH_OFF;
16037                 }
16038
16039                 SNPRINTF(mod_field_name_string, length_field_name, "UPPER(%s)", temp_field_name_string);
16040                 SNPRINTF(mod_value_string,      length_value, "UPPER(%s)", temp_key_value_string);
16041                 EM_SAFE_FREE(temp_key_value_string);
16042         } else {
16043                 mod_field_name_string = strdup(temp_field_name_string);
16044                 mod_value_string      = temp_key_value_string;
16045         }
16046
16047         switch (input_list_filter_rule->rule_type) {
16048         case EMAIL_LIST_FILTER_RULE_EQUAL:
16049                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = %s ", mod_field_name_string, mod_value_string);
16050                 break;
16051         case EMAIL_LIST_FILTER_RULE_NOT_EQUAL:
16052                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s != %s ", mod_field_name_string, mod_value_string);
16053                 break;
16054         case EMAIL_LIST_FILTER_RULE_LESS_THAN:
16055                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s < %s ", mod_field_name_string, mod_value_string);
16056                 break;
16057         case EMAIL_LIST_FILTER_RULE_GREATER_THAN:
16058                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s > %s ", mod_field_name_string, mod_value_string);
16059                 break;
16060         case EMAIL_LIST_FILTER_RULE_LESS_THAN_OR_EQUAL:
16061                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s <= %s ", mod_field_name_string, mod_value_string);
16062                 break;
16063         case EMAIL_LIST_FILTER_RULE_GREATER_THAN_OR_EQUAL:
16064                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s >= %s ", mod_field_name_string, mod_value_string);
16065                 break;
16066         case EMAIL_LIST_FILTER_RULE_INCLUDE:
16067                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s LIKE %s ", mod_field_name_string, mod_value_string);
16068                 break;
16069         case EMAIL_LIST_FILTER_RULE_IN:
16070                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s IN (%s) ", mod_field_name_string, mod_value_string);
16071                 break;
16072         case EMAIL_LIST_FILTER_RULE_NOT_IN:
16073                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s NOT IN (%s) ", mod_field_name_string, mod_value_string);
16074                 break;
16075         default:
16076                 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16077                 ret = EMAIL_ERROR_INVALID_PARAM;
16078                 goto FINISH_OFF;
16079         }
16080
16081         *output_string = strdup(result_rule_string);
16082
16083 FINISH_OFF:
16084         EM_SAFE_FREE(mod_field_name_string);
16085         EM_SAFE_FREE(mod_value_string);
16086
16087         EM_DEBUG_FUNC_END("ret [%d]", ret);
16088         return ret;
16089 }
16090
16091 static int _make_filter_attach_rule_string(char *multi_user_name, email_list_filter_rule_attach_t *input_list_filter_rule, char **output_string)
16092 {
16093         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16094
16095         char *field_name_string = NULL;
16096         char  key_value_string[QUERY_SIZE] = {0,};
16097         char  result_rule_string[QUERY_SIZE] = {0,};
16098         int rc = -1;
16099         int count = 0;
16100         int query_size = 0;
16101         int cur_query = 0;
16102         int col_index = 0;
16103         int error = EMAIL_ERROR_NONE;
16104         char **result = NULL;
16105         char sql_query_string[QUERY_SIZE] = {0,};
16106         char *sql_query_string2 = NULL;
16107         sqlite3 *local_db_handle = NULL;
16108         int *mail_ids = NULL;
16109
16110         if (input_list_filter_rule == NULL || output_string == NULL) {
16111                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16112                 return  EMAIL_ERROR_INVALID_PARAM;
16113         }
16114
16115         field_name_string = EM_SAFE_STRDUP("attachment_name");
16116         SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
16117
16118         switch (input_list_filter_rule->rule_type) {
16119
16120         case EMAIL_LIST_FILTER_RULE_INCLUDE:
16121                 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
16122                 break;
16123
16124         case EMAIL_LIST_FILTER_RULE_MATCH:
16125                 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
16126                 break;
16127
16128         default:
16129                 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16130                 error = EMAIL_ERROR_INVALID_PARAM;
16131                 goto FINISH_OFF;
16132         }
16133
16134         local_db_handle = emstorage_get_db_connection(multi_user_name);
16135
16136         EMSTORAGE_START_READ_TRANSACTION(true);
16137         SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_attachment_tbl %s", result_rule_string);
16138
16139         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16140         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16141                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16142
16143         col_index = 1;
16144
16145         if (!count) {
16146                 EM_DEBUG_LOG("No mail found...");
16147                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
16148                 *output_string = strdup("mail_id IN () ");
16149                 goto FINISH_OFF;
16150         }
16151
16152         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
16153         int i = 0;
16154         if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
16155                 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
16156                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16157                 goto FINISH_OFF;
16158         }
16159
16160         for (i = 0; i < count; i++) {
16161                 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16162                 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16163         }
16164
16165         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >>", count);
16166         sqlite3_free_table(result);
16167         EMSTORAGE_FINISH_READ_TRANSACTION(true);
16168
16169 //      sqlite3_db_release_memory(local_db_handle);
16170
16171
16172         query_size = (10 * count) + strlen("mail_id IN ()  ");
16173
16174         sql_query_string2 = em_malloc(query_size);
16175         if (sql_query_string2 == NULL) {
16176                 EM_DEBUG_EXCEPTION("em_mallocfailed");
16177                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16178                 goto FINISH_OFF;
16179         }
16180
16181         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN (");
16182         for (i = 0; i < count-1; i++) {
16183                 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16184         }
16185         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d) ", mail_ids[count-1]);
16186
16187         *output_string = strdup(sql_query_string2);
16188 FINISH_OFF:
16189
16190         EM_SAFE_FREE(mail_ids); /* prevent */
16191         EM_SAFE_FREE(sql_query_string2);
16192         EM_SAFE_FREE(field_name_string);
16193         EM_DEBUG_FUNC_END("error [%d]", error);
16194         return error;
16195 }
16196
16197 static int _make_filter_fts_rule_string(char *multi_user_name, email_list_filter_rule_fts_t *input_list_filter_rule, char **output_string)
16198 {
16199         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16200         char *field_name_string = NULL;
16201         char key_value_string[QUERY_SIZE] = {0,};
16202         char  result_rule_string[QUERY_SIZE] = {0,};
16203         int rc = -1;
16204         int count = 0;
16205         int col_index = 0;
16206         int query_size = 0;
16207         int error = EMAIL_ERROR_NONE;
16208         char **result = NULL;
16209         char sql_query_string[QUERY_SIZE] = {0,};
16210         char *sql_query_string2 = NULL;
16211         sqlite3 *local_db_handle = NULL;
16212         int *mail_ids = NULL;
16213
16214         if (input_list_filter_rule == NULL || output_string == NULL) {
16215                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16216                 return  EMAIL_ERROR_INVALID_PARAM;
16217         }
16218
16219         field_name_string = EM_SAFE_STRDUP("body_text");
16220         SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
16221
16222         switch (input_list_filter_rule->rule_type) {
16223
16224         case EMAIL_LIST_FILTER_RULE_INCLUDE:
16225                 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
16226                 break;
16227
16228         case EMAIL_LIST_FILTER_RULE_MATCH:
16229                 SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
16230                 break;
16231
16232         default:
16233                 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16234                 error = EMAIL_ERROR_INVALID_PARAM;
16235                 goto FINISH_OFF;
16236         }
16237
16238         local_db_handle = emstorage_get_db_connection(multi_user_name);
16239
16240         EMSTORAGE_START_READ_TRANSACTION(true);
16241         SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_text_tbl %s", result_rule_string);
16242
16243         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16244         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16245                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16246
16247         col_index = 1;
16248
16249         if (!count) {
16250                 EM_DEBUG_LOG("No mail found...");
16251                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
16252                 *output_string = strdup("mail_id IN () ");
16253                 goto FINISH_OFF;
16254         }
16255
16256         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
16257         int i = 0;
16258
16259         if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
16260                 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
16261                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16262                 goto FINISH_OFF;
16263         }
16264
16265         for (i = 0; i < count; i++) {
16266                 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16267                 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16268         }
16269
16270         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >>", count);
16271         sqlite3_free_table(result);
16272         EMSTORAGE_FINISH_READ_TRANSACTION(true);
16273
16274 //      sqlite3_db_release_memory(local_db_handle);
16275
16276
16277         query_size = (10 * count) + strlen("mail_id IN ()  ");
16278         sql_query_string2 = em_malloc(query_size);
16279         if (sql_query_string2 == NULL) {
16280                 EM_DEBUG_EXCEPTION("em_mallocfailed");
16281                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16282                 goto FINISH_OFF;
16283         }
16284         int cur_query = 0;
16285         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN (");
16286         for (i = 0; i < count-1; i++) {
16287                 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16288         }
16289         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d) ", mail_ids[count-1]);
16290
16291         *output_string = strdup(sql_query_string2);
16292
16293 FINISH_OFF:
16294         EM_SAFE_FREE(mail_ids); /* prevent */
16295         EM_SAFE_FREE(sql_query_string2);
16296         EM_SAFE_FREE(field_name_string);
16297         EM_DEBUG_FUNC_END("error [%d]", error);
16298         return error;
16299 }
16300
16301 static int _make_order_rule_string(char *multi_user_name, email_list_sorting_rule_t *input_sorting_rule, char **output_string)
16302 {
16303         EM_DEBUG_FUNC_BEGIN("input_sorting_rule [%p], output_string [%p]", input_sorting_rule, output_string);
16304
16305         char  result_rule_string[QUERY_SIZE] = { 0 , };
16306         int   ret = EMAIL_ERROR_NONE;
16307
16308         emstorage_account_tbl_t *account_tbl_array = NULL;
16309         int count = 0;
16310         int i = 0;
16311         char *result_str = NULL;
16312         char *tmp_str1 = NULL;
16313         char *tmp_str2 = NULL;
16314         char query_per_account[QUERY_SIZE] = { 0 , };
16315
16316         if (input_sorting_rule->force_boolean_check) {
16317                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = 0 ", emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16318         } else
16319                 EM_SAFE_STRCPY(result_rule_string, emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16320
16321         switch (input_sorting_rule->sort_order) {
16322                 case EMAIL_SORT_ORDER_ASCEND:
16323                         EM_SAFE_STRCAT(result_rule_string, " ASC ");
16324                         break;
16325
16326                 case EMAIL_SORT_ORDER_DESCEND:
16327                         EM_SAFE_STRCAT(result_rule_string, " DESC ");
16328                         break;
16329
16330                 case EMAIL_SORT_ORDER_NOCASE_ASCEND:
16331                         EM_SAFE_STRCAT(result_rule_string, " COLLATE NOCASE ASC ");
16332                         break;
16333
16334                 case EMAIL_SORT_ORDER_NOCASE_DESCEND:
16335                         EM_SAFE_STRCAT(result_rule_string, " COLLATE NOCASE DESC ");
16336                         break;
16337
16338                 case EMAIL_SORT_ORDER_TO_CCBCC:
16339                         memset(result_rule_string, 0, QUERY_SIZE);
16340                         if (input_sorting_rule->key_value.string_type_value)
16341                                 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16342                                                 " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 ELSE 2 END ",
16343                                                 input_sorting_rule->key_value.string_type_value);
16344                         break;
16345
16346                 case EMAIL_SORT_ORDER_TO_CC_BCC:
16347                         memset(result_rule_string, 0, QUERY_SIZE);
16348                         if (input_sorting_rule->key_value.string_type_value)
16349                                 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16350                                                 " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 WHEN full_address_cc LIKE \'%%%q%%\' THEN 2 ELSE 3 END ",
16351                                                 input_sorting_rule->key_value.string_type_value, input_sorting_rule->key_value.string_type_value);
16352                         break;
16353
16354                 case EMAIL_SORT_ORDER_TO_CCBCC_ALL:
16355                         if (!emstorage_get_account_list(multi_user_name, &count, &account_tbl_array, true, false, NULL)) {
16356                                 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed");
16357                                 goto FINISH_OFF;
16358                         }
16359
16360                         if (!count) {
16361                                 EM_DEBUG_LOG("No account exist");
16362                                 ret = EMAIL_ERROR_INVALID_PARAM;
16363                                 goto FINISH_OFF;
16364                         }
16365
16366                         for (i = 0; i < count; i++) {
16367                                 if (i > 0 && result_str) {
16368                                         tmp_str2 = result_str;
16369                                         result_str = g_strconcat(tmp_str2, " OR ", NULL);
16370                                         EM_SAFE_FREE(tmp_str2);
16371                                 }
16372
16373                                 memset(query_per_account, 0, QUERY_SIZE);
16374                                 snprintf(query_per_account, QUERY_SIZE,
16375                                                 "(account_id = %d AND full_address_to LIKE \'%%%s%%\')",
16376                                                 account_tbl_array[i].account_id, account_tbl_array[i].user_email_address);
16377
16378                                 tmp_str1 = result_str;
16379                                 if (tmp_str1)
16380                                         result_str = g_strconcat(tmp_str1, query_per_account, NULL);
16381                                 else
16382                                         result_str = g_strdup(query_per_account);
16383                                 EM_SAFE_FREE(tmp_str1);
16384                         }
16385
16386                         snprintf(result_rule_string, QUERY_SIZE,
16387                                         " CASE WHEN %s THEN 1 ELSE 2 END ", result_str);
16388
16389                         EM_SAFE_FREE(result_str);
16390                         if (account_tbl_array)
16391                                 emstorage_free_account(&account_tbl_array, count, NULL);
16392                         break;
16393
16394                 case EMAIL_SORT_ORDER_LOCALIZE_ASCEND:
16395                         memset(result_rule_string, 0, QUERY_SIZE);
16396                         sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16397                                 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END ASC, %s COLLATE NOCASE ASC ",
16398                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16399                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16400                         break;
16401
16402                 case EMAIL_SORT_ORDER_LOCALIZE_DESCEND:
16403                         memset(result_rule_string, 0, QUERY_SIZE);
16404                         sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16405                                 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END DESC, %s COLLATE NOCASE DESC ",
16406                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16407                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16408                         break;
16409
16410                 default:
16411                         EM_DEBUG_EXCEPTION("Invalid sort_order [%d]", input_sorting_rule->sort_order);
16412                         ret = EMAIL_ERROR_INVALID_PARAM;
16413                         goto FINISH_OFF;
16414         }
16415
16416         *output_string = strdup(result_rule_string);
16417
16418 FINISH_OFF:
16419         EM_DEBUG_FUNC_END("ret [%d]", ret);
16420         return ret;
16421 }
16422
16423 INTERNAL_FUNC int emstorage_write_conditional_clause_for_getting_mail_list(char *multi_user_name, email_list_filter_t *input_filter_list, int input_filter_count, email_list_sorting_rule_t *input_sorting_rule_list, int input_sorting_rule_count, int input_start_index, int input_limit_count, char **output_conditional_clause)
16424 {
16425         EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d], input_sorting_rule_list[%p], input_sorting_rule_count [%d], input_start_index [%d], input_limit_count [%d], output_conditional_clause [%p]", input_filter_list, input_filter_count, input_sorting_rule_list, input_sorting_rule_count, input_start_index, input_limit_count, output_conditional_clause);
16426         int ret = EMAIL_ERROR_NONE;
16427         int i = 0;
16428         int string_offset = 0;
16429         int query_size = 0;
16430         int new_query_size = 0;
16431         char *conditional_clause_string = NULL;
16432         char *result_string_for_a_item = NULL;
16433
16434         if ((input_filter_count > 0 && !input_filter_list) || (input_sorting_rule_count > 0 && !input_sorting_rule_list) || output_conditional_clause == NULL) {
16435                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16436                 return EMAIL_ERROR_INVALID_PARAM;
16437         }
16438
16439         conditional_clause_string = em_malloc(QUERY_SIZE);
16440         if (conditional_clause_string == NULL) {
16441                 EM_DEBUG_EXCEPTION("Memory is full");
16442                 return EMAIL_ERROR_OUT_OF_MEMORY;
16443         }
16444
16445         if (input_filter_count > 0) {
16446                 query_size = QUERY_SIZE;
16447                 strcpy(conditional_clause_string, " WHERE ");
16448
16449                 for (i = 0; i < input_filter_count; i++) {
16450                         switch (input_filter_list[i].list_filter_item_type) {
16451                         case EMAIL_LIST_FILTER_ITEM_RULE:
16452                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE", i);
16453                                 _make_filter_rule_string(&(input_filter_list[i].list_filter_item.rule), &result_string_for_a_item);
16454                                 break;
16455
16456                         case EMAIL_LIST_FILTER_ITEM_RULE_FTS:
16457                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_FTS", i);
16458                                 _make_filter_fts_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_fts), &result_string_for_a_item);
16459                                 break;
16460
16461                         case EMAIL_LIST_FILTER_ITEM_RULE_ATTACH:
16462                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_ATTACH", i);
16463                                 _make_filter_attach_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_attach), &result_string_for_a_item);
16464                                 break;
16465
16466                         case EMAIL_LIST_FILTER_ITEM_OPERATOR:
16467                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_OPERATOR", i);
16468                                 switch (input_filter_list[i].list_filter_item.operator_type) {
16469                                 case EMAIL_LIST_FILTER_OPERATOR_AND:
16470                                         result_string_for_a_item = strdup("AND ");
16471                                         break;
16472                                 case EMAIL_LIST_FILTER_OPERATOR_OR:
16473                                         result_string_for_a_item = strdup("OR ");
16474                                         break;
16475                                 case EMAIL_LIST_FILTER_OPERATOR_LEFT_PARENTHESIS:
16476                                         result_string_for_a_item = strdup(" (");
16477                                         break;
16478                                 case EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS:
16479                                         result_string_for_a_item = strdup(") ");
16480                                         break;
16481                                 }
16482                                 break;
16483
16484                         default:
16485                                 EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", input_filter_list[i].list_filter_item_type);
16486                                 ret = EMAIL_ERROR_INVALID_PARAM;
16487                                 goto FINISH_OFF;
16488                         }
16489
16490                         if (result_string_for_a_item == NULL) {
16491                                 EM_DEBUG_EXCEPTION("result_string_for_a_item is null");
16492                                 ret = EMAIL_ERROR_INVALID_PARAM;
16493                                 goto FINISH_OFF;
16494                         }
16495
16496                         if (strlen(conditional_clause_string) + EM_SAFE_STRLEN(result_string_for_a_item) >= query_size) { /* prevent 34364 */
16497                                 EM_DEBUG_LOG("QUERY is too long");
16498                                 new_query_size = EM_SAFE_STRLEN(result_string_for_a_item) + EM_SAFE_STRLEN(conditional_clause_string) + QUERY_SIZE;
16499                                 conditional_clause_string = realloc(conditional_clause_string, new_query_size);
16500                                 if (conditional_clause_string == NULL) {
16501                                         EM_DEBUG_EXCEPTION("realloc failed");
16502                                         ret = EMAIL_ERROR_OUT_OF_MEMORY;
16503                                         goto FINISH_OFF;
16504                                 }
16505
16506                                 query_size = new_query_size;
16507                         }
16508
16509                         strcat(conditional_clause_string, result_string_for_a_item);
16510                         EM_SAFE_FREE(result_string_for_a_item);
16511                 }
16512         }
16513
16514         if (input_sorting_rule_count > 0) {
16515                 strcat(conditional_clause_string, "ORDER BY ");
16516
16517                 for (i = 0; i < input_sorting_rule_count; i++) {
16518                         if ((ret = _make_order_rule_string(multi_user_name, &input_sorting_rule_list[i], &result_string_for_a_item)) != EMAIL_ERROR_NONE) {
16519                                 EM_DEBUG_EXCEPTION("_make_order_rule_string failed. [%d]", ret);
16520                                 goto FINISH_OFF;
16521                         }
16522                         if (i > 0)
16523                                 strcat(conditional_clause_string, ", ");
16524                         strcat(conditional_clause_string, result_string_for_a_item);
16525                         EM_SAFE_FREE(result_string_for_a_item);
16526                 }
16527         }
16528
16529         if (input_start_index != -1 && input_limit_count != -1) {
16530                 string_offset = strlen(conditional_clause_string);
16531                 SNPRINTF_OFFSET(conditional_clause_string, string_offset, query_size, " LIMIT %d, %d", input_start_index, input_limit_count);
16532         }
16533
16534         *output_conditional_clause = strdup(conditional_clause_string);
16535
16536 FINISH_OFF:
16537         EM_SAFE_FREE(result_string_for_a_item);
16538         EM_SAFE_FREE(conditional_clause_string);
16539
16540         EM_DEBUG_FUNC_END("ret [%d]", ret);
16541         return ret;
16542 }
16543
16544 INTERNAL_FUNC int emstorage_free_list_filter(email_list_filter_t **input_filter_list, int input_filter_count)
16545 {
16546         EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d]", input_filter_list, input_filter_count);
16547         int err = EMAIL_ERROR_NONE;
16548         int i = 0;
16549         email_list_filter_t *temp_filter_list = NULL;
16550
16551         EM_IF_NULL_RETURN_VALUE(input_filter_list, EMAIL_ERROR_INVALID_PARAM);
16552
16553         for (i = 0; i < input_filter_count; i++) {
16554                 temp_filter_list = (*input_filter_list) + i;
16555                 if (!temp_filter_list) {
16556                         continue;
16557                 }
16558
16559                 if (temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE) {
16560                         switch (temp_filter_list->list_filter_item.rule.target_attribute) {
16561                         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
16562                         case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
16563                         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
16564                         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
16565                         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
16566                         case EMAIL_MAIL_ATTRIBUTE_FROM:
16567                         case EMAIL_MAIL_ATTRIBUTE_TO:
16568                         case EMAIL_MAIL_ATTRIBUTE_CC:
16569                         case EMAIL_MAIL_ATTRIBUTE_BCC:
16570                         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN:
16571                         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML:
16572                         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
16573                                 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule.key_value.string_type_value);
16574                                 break;
16575                         default:
16576                                 break;
16577                         }
16578                 } else if (temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE_FTS && temp_filter_list->list_filter_item.rule_fts.target_attribute == EMAIL_MAIL_TEXT_ATTRIBUTE_FULL_TEXT) {
16579                                 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_fts.key_value.string_type_value);
16580                 } else if (temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE_ATTACH && temp_filter_list->list_filter_item.rule_attach.target_attribute == EMAIL_MAIL_ATTACH_ATTRIBUTE_ATTACHMENT_NAME) {
16581                                 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_attach.key_value.string_type_value);
16582                 }
16583         }
16584
16585         EM_SAFE_FREE(*input_filter_list);
16586
16587         EM_DEBUG_FUNC_END("err [%d]", err);
16588         return err;
16589 }
16590
16591 /* Tasks --------------------------------------------------------------------------*/
16592 INTERNAL_FUNC int emstorage_add_task(char *multi_user_name, email_task_type_t input_task_type, email_task_priority_t input_task_priority, char *input_task_parameter, int input_task_parameter_length, int input_transaction, int *output_task_id)
16593 {
16594         EM_DEBUG_FUNC_BEGIN("input_task_type [%d] input_task_priority[%p], input_task_parameter[%p] input_task_parameter_length[%d] input_transaction[%d] output_task_id[%p]", input_task_type, input_task_priority, input_task_parameter, input_task_parameter_length, input_transaction, output_task_id);
16595         int ret = 0;
16596         int i = 0;
16597         int task_id = 0;
16598         int err = EMAIL_ERROR_NONE;
16599         int rc = -1;
16600         DB_STMT hStmt = NULL;
16601         char sql_query_string[QUERY_SIZE] = {0, };
16602         sqlite3 *local_db_handle = NULL;
16603         char *sql = "SELECT max(rowid) FROM mail_task_tbl;";
16604         char **result = NULL;
16605
16606         if (input_task_parameter == NULL || output_task_id == NULL) {
16607                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16608                 return EMAIL_ERROR_INVALID_PARAM;
16609         }
16610
16611         local_db_handle = emstorage_get_db_connection(multi_user_name);
16612         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, err);
16613
16614         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
16615         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16616                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
16617
16618         if (NULL == result[1])
16619                 task_id = 1;
16620         else
16621                 task_id = atoi(result[1])+1;
16622
16623         *output_task_id = task_id;
16624
16625         sqlite3_free_table(result);
16626         result = NULL;
16627
16628         SNPRINTF(sql_query_string, sizeof(sql_query_string),
16629                 "INSERT INTO mail_task_tbl VALUES "
16630                 "(        "
16631                 "    ? "  /*   task_id */
16632                 "  , ? "  /*   task_type */
16633                 "  , ? "  /*   task_status */
16634                 "  , ? "  /*   task_priority */
16635                 "  , ? "  /*   task_parameter_length */
16636                 "  , ? "  /*   task_parameter */
16637                 "  , ? "  /*   date_time */
16638                 ") ");
16639
16640         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16641         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16642                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16643
16644         EM_DEBUG_LOG_SEC(">>>> SQL STMT [%s] ", sql_query_string);
16645
16646
16647         _bind_stmt_field_data_int(hStmt, i++, task_id);
16648         _bind_stmt_field_data_int(hStmt, i++, input_task_type);
16649         _bind_stmt_field_data_int(hStmt, i++, EMAIL_TASK_STATUS_WAIT);
16650         _bind_stmt_field_data_int(hStmt, i++, input_task_priority);
16651         _bind_stmt_field_data_int(hStmt, i++, input_task_parameter_length);
16652         _bind_stmt_field_data_blob(hStmt, i++, input_task_parameter, input_task_parameter_length);
16653         _bind_stmt_field_data_int(hStmt, i++, time(NULL));
16654
16655         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16656
16657         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {err = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
16658                 ("sqlite3_step fail:%d", rc));
16659         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16660                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
16661
16662         ret = (err == EMAIL_ERROR_NONE);
16663
16664 FINISH_OFF:
16665         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, err);
16666
16667         if (hStmt != NULL) {
16668                 rc = sqlite3_finalize(hStmt);
16669                 if (rc != SQLITE_OK) {
16670                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16671                         err = EMAIL_ERROR_DB_FAILURE;
16672                 }
16673         }
16674
16675         EM_DEBUG_FUNC_END("err [%d]", err);
16676         return err;
16677 }
16678
16679 INTERNAL_FUNC int emstorage_delete_task(char *multi_user_name, int task_id, int transaction)
16680 {
16681         EM_DEBUG_FUNC_BEGIN("task_id[%d], transaction[%d]", task_id, transaction);
16682         int ret = false;
16683         int err = EMAIL_ERROR_NONE;
16684         char sql_query_string[QUERY_SIZE] = {0, };
16685
16686         sqlite3 *local_db_handle = NULL;
16687
16688         if (task_id < 0) {
16689                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16690                 return EMAIL_ERROR_INVALID_PARAM;
16691         }
16692
16693         local_db_handle = emstorage_get_db_connection(multi_user_name);
16694
16695         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
16696
16697         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_task_tbl WHERE task_id = %d", task_id);
16698         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16699         if (err != EMAIL_ERROR_NONE) {
16700                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16701                         goto FINISH_OFF;
16702         }
16703
16704         ret = true;
16705
16706 FINISH_OFF:
16707         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
16708
16709         EM_DEBUG_FUNC_END("err [%d]", err);
16710         return err;
16711 }
16712
16713 INTERNAL_FUNC int emstorage_update_task_status(char *multi_user_name, int task_id, email_task_status_type_t task_status, int transaction)
16714 {
16715         EM_DEBUG_FUNC_BEGIN("task_id[%d] task_status[%d] transaction[%d]", task_id, task_status, transaction);
16716         int ret = false;
16717         int err = EMAIL_ERROR_NONE;
16718         char sql_query_string[QUERY_SIZE] = {0, };
16719
16720         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16721         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
16722
16723         SNPRINTF(sql_query_string, sizeof(sql_query_string),
16724                 "UPDATE mail_task_tbl SET"
16725                 " task_status = %d"
16726                 " WHERE task_id = %d"
16727                 , task_status
16728                 , task_id);
16729         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16730         if (err != EMAIL_ERROR_NONE) {
16731                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16732                         goto FINISH_OFF;
16733         }
16734
16735         ret = true;
16736
16737 FINISH_OFF:
16738         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
16739
16740         EM_DEBUG_FUNC_END("err [%d]", err);
16741         return err;
16742 }
16743
16744 INTERNAL_FUNC int emstorage_query_task(char *multi_user_name,
16745                                                                         const char *input_conditional_clause,
16746                                                                         const char *input_ordering_clause,
16747                                                                         email_task_t **output_task_list,
16748                                                                         int *output_task_count)
16749 {
16750         EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_ordering_clause [%p], "
16751                                                 "output_task_list[%p], output_task_count[%d]",
16752                                                 input_conditional_clause, input_ordering_clause, output_task_list, output_task_count);
16753         int i = 0, count = 0, rc = -1;
16754         int cur_query = 0;
16755         int field_index = 0;
16756         int err = EMAIL_ERROR_NONE;
16757         email_task_t *task_item_from_tbl = NULL;
16758         char sql_query_string[QUERY_SIZE] = {0, };
16759         char *field_list = "task_id, task_type, task_status, task_priority, task_parameter_length, task_parameter ";
16760         char **result;
16761         sqlite3 *local_db_handle = NULL;
16762         DB_STMT hStmt = NULL;
16763
16764         EM_IF_NULL_RETURN_VALUE(input_conditional_clause, false);
16765         EM_IF_NULL_RETURN_VALUE(output_task_count, false);
16766
16767         local_db_handle = emstorage_get_db_connection(multi_user_name);
16768
16769         SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
16770                                         "SELECT COUNT(*) FROM mail_task_tbl %s", input_conditional_clause);
16771         EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
16772
16773         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
16774         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16775                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16776
16777         count = atoi(result[1]);
16778         sqlite3_free_table(result);
16779
16780         EM_DEBUG_LOG("count = %d", rc);
16781
16782         if (count == 0) {
16783                 EM_DEBUG_EXCEPTION("no task found...");
16784                 err = EMAIL_ERROR_TASK_NOT_FOUND;
16785                 goto FINISH_OFF;
16786         }
16787
16788         SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
16789                                         "SELECT %s FROM mail_task_tbl %s %s", field_list, input_conditional_clause, input_ordering_clause);
16790         EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
16791
16792         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16793
16794         EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
16795         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16796                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16797
16798         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16799         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16800                 ("sqlite3_step fail:%d", rc));
16801
16802         if (rc == SQLITE_DONE) {
16803                 EM_DEBUG_EXCEPTION("no task found...");
16804                 err = EMAIL_ERROR_TASK_NOT_FOUND;
16805                 count = 0;
16806                 goto FINISH_OFF;
16807         }
16808
16809         if (!(task_item_from_tbl = (email_task_t*)em_malloc(sizeof(email_task_t) * count))) {
16810                 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
16811                 err = EMAIL_ERROR_OUT_OF_MEMORY;
16812                 goto FINISH_OFF;
16813         }
16814
16815         for (i = 0; i < count; i++) {
16816                 /*  get recordset */
16817                 field_index = 0;
16818
16819                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_id), field_index++);
16820                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_type), field_index++);
16821                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_status), field_index++);
16822                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_priority), field_index++);
16823                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_parameter_length), field_index++);
16824                 _get_stmt_field_data_blob(hStmt, (void**)&(task_item_from_tbl[i].task_parameter), field_index++);
16825
16826                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16827                 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i,  rc);
16828                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16829                         ("sqlite3_step fail:%d", rc));
16830         }
16831
16832 FINISH_OFF:
16833
16834         if (err == EMAIL_ERROR_NONE) {
16835                 if (output_task_list)
16836                         *output_task_list = task_item_from_tbl;
16837                 *output_task_count = count;
16838         } else {
16839                 if (task_item_from_tbl) {
16840                         for (i = 0; i < count; i++)
16841                                 EM_SAFE_FREE(task_item_from_tbl[i].task_parameter);
16842
16843                         free(task_item_from_tbl);
16844                 }
16845         }
16846
16847         if (hStmt != NULL) {
16848                 rc = sqlite3_finalize(hStmt);
16849                 hStmt = NULL;
16850                 if (rc != SQLITE_OK) {
16851                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16852                         err = EMAIL_ERROR_DB_FAILURE;
16853                 }
16854         }
16855
16856         EM_DEBUG_FUNC_END("err [%d]", err);
16857         return err;
16858 }
16859
16860 INTERNAL_FUNC int emstorage_check_and_update_server_uid_by_message_id(char *multi_user_name, int account_id, email_mailbox_type_e input_mailbox_type, char *message_id, char *server_uid, int *mail_id)
16861 {
16862         EM_DEBUG_FUNC_BEGIN("account_id:[%d], mailbox_type:[%d], message_id:[%s], server_uid:[%s]", account_id, input_mailbox_type, message_id, server_uid);
16863         int err = EMAIL_ERROR_NONE;
16864
16865         if (message_id == NULL) {
16866                 EM_DEBUG_EXCEPTION("Invalid parameter");
16867                 err = EMAIL_ERROR_INVALID_PARAM;
16868                 return err;
16869         }
16870
16871         int rc = -1;
16872         int count = 0;
16873         int temp_mail_id = 0;
16874         int where_pararaph_length = 0;
16875         char *where_pararaph = NULL;
16876         char sql_query_string[QUERY_SIZE] = {0, };
16877         char **result = NULL;
16878         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16879
16880         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id from mail_tbl ");
16881
16882         where_pararaph_length = EM_SAFE_STRLEN(message_id) + 100;
16883         where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
16884         if (where_pararaph == NULL) {
16885                 EM_DEBUG_EXCEPTION("em_mallocfailed");
16886                 err = EMAIL_ERROR_OUT_OF_MEMORY;
16887                 goto FINISH_OFF;
16888         }
16889
16890         if (account_id != ALL_ACCOUNT)
16891                 sqlite3_snprintf(where_pararaph_length, where_pararaph, "WHERE account_id = %d AND mailbox_type = %d AND message_id like '%q'", account_id, input_mailbox_type, message_id);
16892         else
16893                 sqlite3_snprintf(where_pararaph_length, where_pararaph, "WHERE mailbox_type = %d AND message_id like '%q'", input_mailbox_type, message_id);
16894
16895         if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
16896                 strcat(sql_query_string, where_pararaph);
16897
16898         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
16899
16900         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16901         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16902                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16903
16904
16905         EM_DEBUG_LOG("Count of mails [%d]", count);
16906
16907         if (count) {
16908                 _get_table_field_data_int(result, &temp_mail_id, 1);
16909                 EM_DEBUG_LOG("Searched mail_id [%d]", temp_mail_id);
16910
16911                 memset(sql_query_string, 0x00, QUERY_SIZE);
16912                 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string, "UPDATE mail_tbl set server_mail_id = '%q'", server_uid);
16913
16914                 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
16915                         strcat(sql_query_string, where_pararaph);
16916
16917                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16918                 if (err != EMAIL_ERROR_NONE) {
16919                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16920                         goto FINISH_OFF;
16921                 }
16922
16923         } else {
16924                 err = EMAIL_ERROR_MAIL_NOT_FOUND;
16925         }
16926
16927 FINISH_OFF:
16928
16929         sqlite3_free_table(result);
16930         result = NULL;
16931
16932
16933         EM_SAFE_FREE(where_pararaph);
16934
16935         if (mail_id != NULL)
16936                 *mail_id = temp_mail_id;
16937
16938         EM_DEBUG_FUNC_END("err : [%d]", err);
16939         return err;
16940 }
16941 /* Tasks --------------------------------------------------------------------------*/
16942
16943 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
16944 INTERNAL_FUNC int emstorage_add_auto_download_activity(char *multi_user_name, email_event_auto_download *local_activity, int *activity_id, int transaction, int *err_code)
16945 {
16946         EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
16947
16948         if (!local_activity || !activity_id) {
16949                 EM_DEBUG_EXCEPTION("local_activity[%p], activity_id[%p]", local_activity, activity_id);
16950                 if (err_code != NULL)
16951                         *err_code = EMAIL_ERROR_INVALID_PARAM;
16952                 return false;
16953         }
16954
16955         int rc = -1;
16956         int ret = false;
16957         int error = EMAIL_ERROR_NONE;
16958         int i = 0;
16959
16960         char sql_query_string[QUERY_SIZE] = {0, };
16961         DB_STMT hStmt = NULL;
16962
16963         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16964         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
16965
16966         memset(sql_query_string, 0x00, sizeof(sql_query_string));
16967         SNPRINTF(sql_query_string, sizeof(sql_query_string),
16968                 "INSERT INTO mail_auto_download_activity_tbl VALUES "
16969                 "("
16970                 "? "  /* Activity ID */
16971                 ",?"  /* Status */
16972                 ",?"  /* Account ID */
16973                 ",?"  /* Local Mail ID */
16974                 ",?"  /* Server mail ID */
16975                 ",?"  /* Mailbox ID*/
16976                 ",?"  /* Multi USER NAME */
16977                 ") ");
16978
16979         char *sql = "SELECT max(rowid) FROM mail_auto_download_activity_tbl;";
16980         char **result = NULL;
16981
16982
16983         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
16984
16985         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16986                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
16987
16988         if (NULL == result[1]) rc = 1;
16989         else rc = atoi(result[1])+1;
16990         sqlite3_free_table(result);
16991         result = NULL;
16992
16993         *activity_id = local_activity->activity_id = rc;
16994
16995         EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [%d], MAIL ID [%d], SERVER MAIL ID [%lu]",
16996                         local_activity->activity_id, local_activity->mail_id, local_activity->server_mail_id);
16997
16998         if (local_activity->mailbox_id)
16999                 EM_DEBUG_LOG(" MAILBOX ID [%d]", local_activity->mailbox_id);
17000
17001
17002         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17003         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17004                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17005
17006
17007         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
17008         _bind_stmt_field_data_int(hStmt, i++, local_activity->status);
17009         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
17010         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
17011         _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
17012         _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
17013         _bind_stmt_field_data_string(hStmt, i++, (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
17014
17015
17016         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17017
17018         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
17019                 ("sqlite3_step fail:%d", rc));
17020         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17021                 ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
17022
17023         ret = true;
17024
17025 FINISH_OFF:
17026         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17027         if (hStmt != NULL) {
17028                 rc = sqlite3_finalize(hStmt);
17029                 hStmt = NULL;
17030                 if (rc != SQLITE_OK) {
17031                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17032                         error = EMAIL_ERROR_DB_FAILURE;
17033                 }
17034         }
17035
17036         if (err_code != NULL)
17037                 *err_code = error;
17038
17039         EM_DEBUG_FUNC_END("ret [%d]", ret);
17040         return ret;
17041 }
17042
17043
17044 INTERNAL_FUNC int emstorage_delete_auto_download_activity(char *multi_user_name, int account_id, int mail_id, int activity_id, int transaction, int *err_code)
17045 {
17046         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d] , activity_id[%d], transaction[%d], err_code[%p]", account_id, mail_id, activity_id, transaction, err_code);
17047
17048         if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
17049                 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], activity_id[%d], transaction[%d], err_code[%p]", account_id, mail_id, activity_id, transaction, err_code);
17050
17051                 if (err_code != NULL)
17052                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17053                 return false;
17054         }
17055
17056         int rc = -1;
17057         int ret = false;
17058         int error = EMAIL_ERROR_NONE;
17059         char sql_query_string[QUERY_SIZE] = {0, };
17060         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17061
17062         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17063         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17064
17065         if (activity_id == 0)
17066                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d AND mail_id = %d", account_id, mail_id);
17067         else
17068                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d AND activity_id = %d", account_id, activity_id);
17069
17070         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17071         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17072         if (error != EMAIL_ERROR_NONE) {
17073                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17074                 goto FINISH_OFF;
17075         }
17076
17077         /*  validate activity existence */
17078         rc = sqlite3_changes(local_db_handle);
17079         if (rc == 0) {
17080                 EM_DEBUG_EXCEPTION("No matching activity found");
17081                 error = EMAIL_ERROR_DATA_NOT_FOUND;
17082                 ret = true;
17083                 goto FINISH_OFF;
17084         }
17085
17086         ret = true;
17087
17088 FINISH_OFF:
17089         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17090
17091         if (err_code != NULL)
17092                 *err_code = error;
17093
17094         EM_DEBUG_FUNC_END("ret [%d]", ret);
17095         return ret;
17096 }
17097
17098 INTERNAL_FUNC int emstorage_delete_all_auto_download_activity(char *multi_user_name, int account_id, int transaction, int *err_code)
17099 {
17100         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17101
17102         if (account_id < FIRST_ACCOUNT_ID) {
17103                 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17104                 if (err_code != NULL)
17105                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17106                 return false;
17107         }
17108
17109         int rc = -1;
17110         int ret = false;
17111         int error = EMAIL_ERROR_NONE;
17112         char sql_query_string[QUERY_SIZE] = {0, };
17113
17114         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17115
17116         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17117         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17118         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d", account_id);
17119
17120         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17121         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17122         if (error != EMAIL_ERROR_NONE) {
17123                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17124                         goto FINISH_OFF;
17125         }
17126
17127         rc = sqlite3_changes(local_db_handle);
17128         if (rc == 0) {
17129                 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17130                 error = EMAIL_ERROR_DATA_NOT_FOUND;
17131                 ret = true;
17132                 goto FINISH_OFF;
17133         }
17134
17135         ret = true;
17136
17137 FINISH_OFF:
17138         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17139
17140         if (err_code != NULL)
17141                 *err_code = error;
17142
17143         EM_DEBUG_FUNC_END("ret [%d]", ret);
17144         return ret;
17145 }
17146
17147
17148 INTERNAL_FUNC int emstorage_delete_auto_download_activity_by_mailbox(char *multi_user_name, int account_id, int mailbox_id, int transaction, int *err_code)
17149 {
17150         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17151
17152         if (account_id < FIRST_ACCOUNT_ID || mailbox_id < 0) {
17153                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17154                 if (err_code != NULL)
17155                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17156                 return false;
17157         }
17158
17159         int rc = -1;
17160         int ret = false;
17161         int error = EMAIL_ERROR_NONE;
17162         char sql_query_string[QUERY_SIZE] = {0, };
17163
17164         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17165
17166         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17167         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17168         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d AND mailbox_id = %d", account_id, mailbox_id);
17169
17170         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17171         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17172         if (error != EMAIL_ERROR_NONE) {
17173                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17174                         goto FINISH_OFF;
17175         }
17176
17177         rc = sqlite3_changes(local_db_handle);
17178         if (rc == 0) {
17179                 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17180                 error = EMAIL_ERROR_DATA_NOT_FOUND;
17181                 ret = true;
17182                 goto FINISH_OFF;
17183         }
17184
17185         ret = true;
17186
17187 FINISH_OFF:
17188         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17189
17190         if (err_code != NULL)
17191                 *err_code = error;
17192
17193         EM_DEBUG_FUNC_END("ret [%d]", ret);
17194         return ret;
17195 }
17196
17197
17198 INTERNAL_FUNC int emstorage_get_auto_download_activity(char *multi_user_name, int account_id, int input_mailbox_id, email_event_auto_download **event_start, int *count, int transaction, int *err_code)
17199 {
17200         EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
17201
17202         if (account_id < FIRST_ACCOUNT_ID || !event_start || input_mailbox_id <= 0 || !count) {
17203                 EM_DEBUG_EXCEPTION("account_id[%d], event_start[%p], input_mailbox_id[%d], count[%p], err_code[%p]", account_id, event_start, input_mailbox_id, count, err_code);
17204
17205                 if (err_code != NULL)
17206                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17207                 return false;
17208         }
17209
17210         int rc = -1;
17211         int ret = false;
17212         char **result;
17213         int error = EMAIL_ERROR_NONE;
17214         int i = 0;
17215         DB_STMT hStmt = NULL;
17216         email_event_auto_download *event_list = NULL;
17217         char sql_query_string[QUERY_SIZE] = {0, };
17218
17219         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17220
17221         EMSTORAGE_START_READ_TRANSACTION(transaction);
17222
17223         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17224         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl WHERE account_id = %d AND mailbox_id = '%d' order by activity_id", account_id, input_mailbox_id);
17225
17226
17227         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17228         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17229                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17230
17231         *count = atoi(result[1]);
17232         sqlite3_free_table(result);
17233
17234         EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
17235
17236         if (!*count) {
17237                 EM_DEBUG_LOG("No matched activity found in mail_auto_download_activity_tbl");
17238                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
17239                 ret = true;
17240                 goto FINISH_OFF;
17241         }
17242         EM_DEBUG_LOG("Activity Count = %d", *count);
17243
17244         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17245         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_auto_download_activity_tbl WHERE account_id = %d AND mailbox_id = '%d' order by activity_id", account_id, input_mailbox_id);
17246
17247         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17248
17249
17250         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17251
17252         EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
17253         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17254                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17255
17256
17257         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17258         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17259                 ("sqlite3_step fail:%d", rc));
17260
17261         if (!(event_list = (email_event_auto_download *)em_malloc(sizeof(email_event_auto_download)*(*count)))) {
17262                 EM_DEBUG_EXCEPTION("Malloc failed");
17263
17264                 error = EMAIL_ERROR_OUT_OF_MEMORY;
17265                 goto FINISH_OFF;
17266         }
17267
17268         for (i = 0; i < (*count); i++) {
17269                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17270                 _get_stmt_field_data_int(hStmt, &(event_list[i].status), STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17271                 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17272                 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17273                 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17274                 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17275
17276                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17277                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17278                         ("sqlite3_step fail:%d", rc));
17279         }
17280
17281         ret = true;
17282
17283 FINISH_OFF:
17284
17285         if (true == ret)
17286           *event_start = event_list;
17287         else {
17288                 EM_SAFE_FREE(event_list);
17289                 *event_start = NULL;
17290                 *count = 0;
17291         }
17292
17293         if (hStmt != NULL) {
17294                 rc = sqlite3_finalize(hStmt);
17295                 hStmt = NULL;
17296                 if (rc != SQLITE_OK) {
17297                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17298                         error = EMAIL_ERROR_DB_FAILURE;
17299                 }
17300         }
17301
17302         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17303         if (err_code != NULL)
17304                 *err_code = error;
17305
17306         EM_DEBUG_FUNC_END("ret [%d]", ret);
17307         return ret;
17308 }
17309
17310
17311 INTERNAL_FUNC int emstorage_get_auto_download_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
17312 {
17313         EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
17314
17315         if (!activity_count || !err_code) {
17316                 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
17317                 if (err_code != NULL)
17318                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17319                 return false;
17320         }
17321
17322         int rc = -1;
17323         int ret = false;
17324         int error = EMAIL_ERROR_NONE;
17325         DB_STMT hStmt = NULL;
17326         char sql_query_string[QUERY_SIZE] = {0, };
17327
17328         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17329
17330         EMSTORAGE_START_READ_TRANSACTION(transaction);
17331         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17332
17333         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl;");
17334
17335         EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
17336
17337
17338         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17339         EM_DEBUG_LOG_DEV("before sqlite3_prepare hStmt = %p", hStmt);
17340         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17341                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17342
17343
17344         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17345         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17346                 ("sqlite3_step fail:%d", rc));
17347
17348         _get_stmt_field_data_int(hStmt, activity_count, 0);
17349
17350         EM_DEBUG_LOG("counts of activities in activity table [%d]", *activity_count);
17351
17352         ret = true;
17353
17354 FINISH_OFF:
17355
17356         if (hStmt != NULL) {
17357                 rc = sqlite3_finalize(hStmt);
17358                 hStmt = NULL;
17359                 if (rc != SQLITE_OK) {
17360                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17361                         error = EMAIL_ERROR_DB_FAILURE;
17362                 }
17363         }
17364
17365         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17366         if (err_code != NULL)
17367                 *err_code = error;
17368
17369         EM_DEBUG_FUNC_END("ret [%d]", ret);
17370         return ret;
17371 }
17372
17373
17374 INTERNAL_FUNC int emstorage_get_auto_download_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
17375 {
17376         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
17377
17378         if (!account_list || !count) {
17379                 EM_DEBUG_EXCEPTION("account_list[%p], count[%p]", account_list, count);
17380                 if (err_code != NULL)
17381                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17382                 return false;
17383         }
17384
17385         int ret = false;
17386         int error = EMAIL_ERROR_NONE;
17387         char *sql = "SELECT count(distinct account_id) FROM mail_auto_download_activity_tbl";
17388         char **result;
17389         int i = 0, rc = -1;
17390         int *result_account_list = NULL;
17391         DB_STMT hStmt = NULL;
17392         char sql_query_string[QUERY_SIZE] = {0, };
17393         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17394
17395         EMSTORAGE_START_READ_TRANSACTION(transaction);
17396
17397         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
17398         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17399                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
17400
17401         *count = atoi(result[1]);
17402         sqlite3_free_table(result);
17403
17404         if (!*count) {
17405                 EM_DEBUG_EXCEPTION("no account found...");
17406                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
17407                 ret = true;
17408                 goto FINISH_OFF;
17409         }
17410
17411         EM_DEBUG_LOG("Account count [%d]", *count);
17412
17413         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17414
17415         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_auto_download_activity_tbl");
17416
17417         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17418
17419
17420         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17421
17422         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
17423         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17424                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17425
17426
17427         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17428         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17429                 ("sqlite3_step fail:%d", rc));
17430
17431         if (NULL == (result_account_list = (int *)em_malloc(sizeof(int)*(*count)))) {
17432                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
17433                 error = EMAIL_ERROR_OUT_OF_MEMORY;
17434                 goto FINISH_OFF;
17435         }
17436
17437         for (i = 0; i < (*count); i++) {
17438                 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
17439
17440                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17441                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17442                         ("sqlite3_step fail:%d", rc));
17443                 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
17444         }
17445
17446         ret = true;
17447
17448 FINISH_OFF:
17449
17450         if (ret == true)
17451                 *account_list = result_account_list;
17452         else
17453                 EM_SAFE_FREE(result_account_list);
17454
17455         if (hStmt != NULL) {
17456                 rc = sqlite3_finalize(hStmt);
17457                 hStmt = NULL;
17458                 if (rc != SQLITE_OK) {
17459                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17460                         error = EMAIL_ERROR_DB_FAILURE;
17461                 }
17462         }
17463
17464         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17465         if (err_code != NULL)
17466                 *err_code = error;
17467         EM_DEBUG_FUNC_END("ret [%d]", ret);
17468         return ret;
17469 }
17470
17471
17472 INTERNAL_FUNC int emstorage_get_auto_download_mailbox_list(char *multi_user_name, int account_id, int **mailbox_list, int *count, int transaction, int *err_code)
17473 {
17474         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
17475
17476         if (account_id < FIRST_ACCOUNT_ID || !mailbox_list || !count) {
17477                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p]", account_id, mailbox_list, count);
17478                 if (err_code != NULL)
17479                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17480                 return false;
17481         }
17482
17483         int ret = false;
17484         int error = EMAIL_ERROR_NONE;
17485         char **result;
17486         int i = 0, rc = -1;
17487         int *mbox_list = NULL;
17488         DB_STMT hStmt = NULL;
17489         char sql_query_string[QUERY_SIZE] = {0, };
17490
17491         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17492
17493         EMSTORAGE_START_READ_TRANSACTION(transaction);
17494
17495         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17496         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(distinct mailbox_id) FROM mail_auto_download_activity_tbl WHERE account_id = %d order by mailbox_id", account_id);
17497
17498
17499         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17500         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17501                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17502
17503         *count = atoi(result[1]);
17504         sqlite3_free_table(result);
17505
17506         if (!*count) {
17507                 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
17508                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
17509                 ret = true;
17510                 goto FINISH_OFF;
17511         }
17512         EM_DEBUG_LOG("Mailbox count = %d", *count);
17513
17514         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17515
17516         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct mailbox_id FROM mail_auto_download_activity_tbl WHERE account_id = %d order by mailbox_id", account_id);
17517
17518         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
17519
17520
17521         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17522
17523
17524         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17525                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17526
17527
17528         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17529         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17530                 ("sqlite3_step fail:%d", rc));
17531
17532         mbox_list = (int *)em_malloc(sizeof(int)*(*count)); /* prevent */
17533         if (mbox_list == NULL) {
17534                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
17535                 error = EMAIL_ERROR_OUT_OF_MEMORY;
17536                 goto FINISH_OFF;
17537         }
17538
17539         for (i = 0; i < (*count); i++) {
17540                 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
17541                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17542
17543                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17544                         ("sqlite3_step fail:%d", rc));
17545                 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
17546         }
17547
17548         ret = true;
17549
17550 FINISH_OFF:
17551
17552         if (ret == true)
17553                 *mailbox_list = mbox_list;
17554         else
17555                 EM_SAFE_FREE(mbox_list);
17556
17557         if (hStmt != NULL) {
17558                 rc = sqlite3_finalize(hStmt);
17559                 hStmt = NULL;
17560                 if (rc != SQLITE_OK) {
17561                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17562                         error = EMAIL_ERROR_DB_FAILURE;
17563                 }
17564         }
17565
17566         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17567         if (err_code != NULL)
17568                 *err_code = error;
17569         EM_DEBUG_FUNC_END("ret [%d]", ret);
17570         return ret;
17571 }
17572
17573
17574 INTERNAL_FUNC int emstorage_get_auto_download_activity_count_by_mailbox(char *multi_user_name, int account_id, int input_mailbox_id, int *activity_count, int transaction, int *err_code)
17575 {
17576         EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
17577
17578         if (account_id < FIRST_ACCOUNT_ID || !activity_count || !err_code) {
17579                 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
17580                 if (err_code != NULL)
17581                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17582                 return false;
17583         }
17584
17585         int rc = -1;
17586         int ret = false;
17587         int error = EMAIL_ERROR_NONE;
17588         char sql_query_string[QUERY_SIZE] = {0, };
17589         DB_STMT hStmt = NULL;
17590
17591         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17592
17593         EMSTORAGE_START_READ_TRANSACTION(transaction);
17594         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17595         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl WHERE account_id = %d and mailbox_id = '%d'", account_id, input_mailbox_id);
17596
17597         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
17598
17599         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17600         EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
17601         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17602                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17603
17604         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17605         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17606                 ("sqlite3_step fail:%d", rc));
17607
17608         _get_stmt_field_data_int(hStmt, activity_count, 0);
17609
17610         EM_DEBUG_LOG("count of activities in activity table [%d]", *activity_count);
17611
17612         ret = true;
17613
17614 FINISH_OFF:
17615
17616         if (hStmt != NULL) {
17617                 rc = sqlite3_finalize(hStmt);
17618                 hStmt = NULL;
17619                 if (rc != SQLITE_OK) {
17620                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17621                         error = EMAIL_ERROR_DB_FAILURE;
17622                 }
17623         }
17624
17625         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17626
17627         if (err_code != NULL)
17628                 *err_code = error;
17629
17630         EM_DEBUG_FUNC_END("ret [%d]", ret);
17631         return ret;
17632 }
17633
17634
17635 INTERNAL_FUNC int emstorage_update_auto_download_activity(char *multi_user_name, char *old_server_uid, char *new_server_uid, char *mailbox_name, int mailbox_id, int *err_code)
17636 {
17637         EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mailbox_id[%d]", old_server_uid, new_server_uid, mailbox_id);
17638
17639         int rc = -1, ret = false;
17640         int error = EMAIL_ERROR_NONE;
17641         char sql_query_string[QUERY_SIZE] = {0, };
17642         int transaction = true;
17643
17644         if (!old_server_uid || !new_server_uid || (!mailbox_name && mailbox_id < 0)) {
17645                 EM_DEBUG_EXCEPTION("Invalid parameters");
17646                 error = EMAIL_ERROR_INVALID_PARAM;
17647                 return false;
17648         }
17649
17650         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17651
17652         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17653         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17654
17655         if (mailbox_id > 0)
17656                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17657                          "UPDATE mail_auto_download_activity_tbl SET server_mail_id = %s , mailbox_id ='%d' WHERE server_mail_id = %s ", new_server_uid, mailbox_id, old_server_uid);
17658         else if (mailbox_name)
17659                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17660                          "UPDATE mail_auto_download_activity_tbl SET server_mail_id = %s WHERE server_mail_id = %s ", new_server_uid, old_server_uid);
17661
17662         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17663         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17664         if (error != EMAIL_ERROR_NONE) {
17665                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17666                         goto FINISH_OFF;
17667         }
17668
17669         rc = sqlite3_changes(local_db_handle);
17670         if (rc == 0) {
17671                 EM_DEBUG_LOG("No matching found in mail_auto_download_activity_tbl");
17672         }
17673
17674         ret = true;
17675
17676 FINISH_OFF:
17677
17678         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17679
17680         if (err_code != NULL)
17681                 *err_code = error;
17682
17683         EM_DEBUG_FUNC_END("ret [%d]", ret);
17684         return ret;
17685 }
17686
17687 #endif
17688
17689 #ifdef __FEATURE_UPDATE_DB_TABLE_SCHEMA__
17690
17691 typedef struct {
17692         char *column_name;
17693         char *column_type;
17694 } email_column_info_t;
17695
17696 static int get_column_information_from_table_callback(void *arg1, int argc, char **argv, char **input_column_name)
17697 {
17698         EM_DEBUG_FUNC_BEGIN("arg1[%p] argc[%d] argv[%p] column_name[%p]", arg1, argc, argv, input_column_name);
17699
17700         int i = 0;
17701         int validated = 0;
17702         char *column_name = NULL;
17703         char *column_type = NULL;
17704         GList *new_list = *((GList**)arg1);
17705         email_column_info_t *column_info_item = NULL;
17706
17707         for (i = 0; i < argc; ++i) {
17708                 /* EM_DEBUG_LOG("%s = %s", input_column_name[i], argv[i]); */
17709                 if (EM_SAFE_STRCMP(input_column_name[i], "name") == 0) {
17710                         if (column_name)
17711                                 EM_SAFE_FREE(column_name);
17712
17713                         column_name = EM_SAFE_STRDUP(argv[i]);
17714                         validated = 1;
17715                 } else if (EM_SAFE_STRCMP(input_column_name[i], "type") == 0) {
17716                         if (column_type)
17717                                 EM_SAFE_FREE(column_type);
17718
17719                         column_type = EM_SAFE_STRDUP(argv[i]);
17720                 }
17721         }
17722
17723         if (validated) {
17724                 EM_DEBUG_LOG("column_name[%s] column_type[%s]", column_name, column_type);
17725                 column_info_item = em_malloc(sizeof(email_column_info_t));
17726         if (column_info_item == NULL) {
17727             EM_DEBUG_EXCEPTION("em_mallocfailed");
17728             goto FINISH_OFF;
17729         }
17730
17731                 column_info_item->column_name = EM_SAFE_STRDUP(column_name);
17732                 column_info_item->column_type = EM_SAFE_STRDUP(column_type);
17733                 new_list = g_list_append(new_list, (gpointer)column_info_item);
17734                 *((GList**)arg1) = new_list;
17735         }
17736
17737 FINISH_OFF:
17738
17739     EM_SAFE_FREE(column_name);
17740     EM_SAFE_FREE(column_type);
17741
17742         EM_DEBUG_FUNC_END();
17743         return 0;
17744 }
17745
17746 static int emstorage_get_column_information_from_table(char *multi_user_name, const char *input_table_name, GList **output_column_info)
17747 {
17748         EM_DEBUG_FUNC_BEGIN("input_table_name[%p] output_column_info[%p]", input_table_name, output_column_info);
17749         int err = EMAIL_ERROR_NONE;
17750         int result_from_sqlite = 0;
17751         char *error_message_from_sqlite = NULL;
17752         char sql_query_string[QUERY_SIZE] = {0, };
17753         GList *new_list = NULL;
17754         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17755
17756         SNPRINTF(sql_query_string, QUERY_SIZE, "pragma table_info(%s);", input_table_name);
17757
17758         result_from_sqlite = sqlite3_exec(local_db_handle, sql_query_string, get_column_information_from_table_callback, &new_list, &error_message_from_sqlite);
17759
17760         if (result_from_sqlite != SQLITE_OK)
17761                 EM_DEBUG_EXCEPTION("sqlite3_exec returns [%d]", result_from_sqlite);
17762
17763         EM_DEBUG_LOG("new_list[%p] output_column_info[%p]", new_list, output_column_info);
17764
17765         if (new_list && output_column_info) {
17766                 EM_DEBUG_LOG("g_list_length[%d]", g_list_length(new_list));
17767                 *output_column_info = new_list;
17768         }
17769
17770         EM_DEBUG_FUNC_END("err [%d]", err);
17771         return err;
17772 }
17773
17774 static  int emstorage_create_renamed_table(char *multi_user_name, char **input_full_query, int input_query_index, char *input_source_table_name, char *input_new_table_name)
17775 {
17776         EM_DEBUG_FUNC_BEGIN("input_full_query [%p] input_query_index[%d] input_source_table_name[%p] input_new_table_name[%p]", input_full_query, input_query_index, input_source_table_name, input_new_table_name);
17777         int error = EMAIL_ERROR_NONE;
17778         int rc = -1;
17779         sqlite3 *local_db_handle = NULL;
17780         char sql_query_string[QUERY_SIZE] = {0, };
17781
17782         if (input_full_query == NULL || input_source_table_name == NULL || input_new_table_name == NULL) {
17783                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17784                 error = EMAIL_ERROR_INVALID_PARAM;
17785                 goto FINISH_OFF;
17786         }
17787
17788         local_db_handle = emstorage_get_db_connection(multi_user_name);
17789
17790         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17791         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17792
17793         EM_DEBUG_LOG("[%s] will be replaced by [%s]", input_source_table_name, input_new_table_name);
17794
17795         EM_SAFE_STRNCPY(sql_query_string, input_full_query[input_query_index], sizeof(sql_query_string)-1); /*prevent 21984*/
17796         reg_replace(sql_query_string, input_source_table_name, input_new_table_name);
17797
17798         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17799 FINISH_OFF:
17800
17801         if (error == EMAIL_ERROR_NONE) {
17802                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17803         } else {
17804                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17805         }
17806
17807         EM_DEBUG_FUNC_END("error [%d]", error);
17808         return error;
17809 }
17810
17811 static int emstorage_add_column(char *multi_user_name, char *input_table_name, email_column_info_t *input_new_column)
17812 {
17813         EM_DEBUG_FUNC_BEGIN("input_table_name[%p] input_new_column[%p]", input_table_name, input_new_column);
17814         int error = EMAIL_ERROR_NONE;
17815         int rc = -1;
17816         sqlite3 *local_db_handle = NULL;
17817         char sql_query_string[QUERY_SIZE] = {0, };
17818
17819         if (input_table_name == NULL || input_new_column == NULL) {
17820                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17821                 error = EMAIL_ERROR_INVALID_PARAM;
17822                 goto FINISH_OFF;
17823         }
17824
17825         local_db_handle = emstorage_get_db_connection(multi_user_name);
17826
17827         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17828         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17829         SNPRINTF(sql_query_string, QUERY_SIZE, "ALTER TABLE %s ADD COLUMN %s %s;", input_table_name, input_new_column->column_name, input_new_column->column_type);
17830         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17831 FINISH_OFF:
17832
17833         if (error == EMAIL_ERROR_NONE) {
17834                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17835         } else {
17836                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17837         }
17838
17839         EM_DEBUG_FUNC_END("error [%d]", error);
17840         return error;
17841 }
17842
17843 static int emstorage_drop_table(char *multi_user_name, char *input_table_name)
17844 {
17845         EM_DEBUG_FUNC_BEGIN("input_table_name[%p]", input_table_name);
17846         int error = EMAIL_ERROR_NONE;
17847         int rc = -1;
17848         sqlite3 *local_db_handle = NULL;
17849         char sql_query_string[QUERY_SIZE] = {0, };
17850
17851         if (input_table_name == NULL) {
17852                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17853                 error = EMAIL_ERROR_INVALID_PARAM;
17854                 goto FINISH_OFF;
17855         }
17856
17857         local_db_handle = emstorage_get_db_connection(multi_user_name);
17858
17859         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17860         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17861         SNPRINTF(sql_query_string, QUERY_SIZE, "DROP TABLE %s;", input_table_name);
17862         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17863 FINISH_OFF:
17864
17865         if (error == EMAIL_ERROR_NONE) {
17866                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
17867         } else {
17868                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
17869         }
17870
17871         EM_DEBUG_FUNC_END("error [%d]", error);
17872         return error;
17873 }
17874
17875 gint glist_compare_column_name(gconstpointer old_column_info, gconstpointer new_column_info)
17876 {
17877         EM_DEBUG_FUNC_BEGIN("old_column_info[%p] new_column_info[%p]", old_column_info, new_column_info);
17878         email_column_info_t *left_one  = (email_column_info_t*)old_column_info;
17879         email_column_info_t *right_one = (email_column_info_t*)new_column_info;
17880
17881         if (old_column_info == NULL || new_column_info == NULL)
17882                 return -1;
17883
17884         return EM_SAFE_STRCMP((char*)left_one->column_name, (char*)right_one->column_name);
17885 }
17886
17887 INTERNAL_FUNC int emstorage_update_db_table_schema(char *multi_user_name)
17888 {
17889         EM_DEBUG_FUNC_BEGIN();
17890         int i = 0;
17891         int j = 0;
17892         int error = EMAIL_ERROR_NONE;
17893         int query_len = 0;
17894         email_column_info_t *new_column_info = NULL;
17895         email_column_info_t *p_column_info = NULL;
17896         char **create_table_query = NULL;
17897         GList *found_data = NULL;
17898         GList *column_list_of_old_table = NULL;
17899         GList *column_list_of_new_table = NULL;
17900         char table_names[CREATE_TABLE_MAX][2][50] = { { "mail_account_tbl", "mail_account_tbl_new" },
17901                 { "mail_box_tbl", "mail_box_tbl_new" },
17902                 { "mail_read_mail_uid_tbl", "mail_read_mail_uid_tbl_new" },
17903                 { "mail_rule_tbl", "mail_rule_tbl_new" },
17904                 { "mail_tbl", "mail_tbl_new" },
17905                 { "mail_attachment_tbl", "mail_attachment_tbl_new" },
17906 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
17907                 { "mail_partial_body_activity_tbl", "mail_partial_body_activity_tbl_new" },
17908 #else
17909                 { "", "" },
17910 #endif
17911                 { "mail_meeting_tbl", "mail_meeting_tbl_new" },
17912 #ifdef __FEATURE_LOCAL_ACTIVITY__
17913                 { "mail_local_activity_tbl", "mail_local_activity_tbl_new" },
17914 #else
17915                 { "", "" },
17916 #endif
17917                 { "mail_certificate_tbl", "mail_certificate_tbl_new" },
17918                 { "mail_task_tbl", "mail_task_tbl_new" },
17919 #ifdef __FEATURE_BODY_SEARCH__
17920                 { "mail_text_tbl", "mail_text_tbl_new" },
17921 #else
17922                 { "", "" },
17923 #endif
17924
17925 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
17926                 { "mail_auto_download_activity_tbl", "mail_auto_download_activity_tbl_new" }
17927 #else
17928                 { "", "" }
17929 #endif
17930
17931         };
17932
17933         error = emcore_load_query_from_file(EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
17934
17935         if (error != EMAIL_ERROR_NONE) {
17936                 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
17937                 goto FINISH_OFF;
17938         }
17939
17940         if (query_len < CREATE_TABLE_MAX) {
17941                 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
17942                 error = EMAIL_ERROR_SYSTEM_FAILURE;
17943                 goto FINISH_OFF;
17944         }
17945
17946         for (i = CREATE_TABLE_MAIL_ACCOUNT_TBL; i < CREATE_TABLE_MAX; i++) {
17947                 EM_DEBUG_LOG("table [%s] new_table [%s]", table_names[i][0], table_names[i][1]);
17948                 if (EM_SAFE_STRLEN(table_names[i][0]) && EM_SAFE_STRLEN(table_names[i][1])) {
17949                         /* Check existing of _new table */
17950                         emstorage_drop_table(multi_user_name, table_names[i][1]);
17951                         error = emstorage_create_renamed_table(multi_user_name, create_table_query, i, table_names[i][0], table_names[i][1]);
17952                         if (error != EMAIL_ERROR_NONE) {
17953                                 EM_DEBUG_EXCEPTION("emstorage_create_renamed_table failed [%d]", error);
17954                                 goto FINISH_OFF;
17955                         }
17956
17957                         emstorage_get_column_information_from_table(multi_user_name, table_names[i][0], &column_list_of_old_table);
17958                         emstorage_get_column_information_from_table(multi_user_name, table_names[i][1], &column_list_of_new_table);
17959
17960                         /* Compare fields and add new field */
17961                         for (j = 0; j < g_list_length(column_list_of_new_table); j++) {
17962                                 new_column_info = (email_column_info_t*)g_list_nth_data(column_list_of_new_table, j);
17963                                 found_data = g_list_find_custom(column_list_of_old_table, (gconstpointer)new_column_info, glist_compare_column_name);
17964                                 if (found_data == NULL) {
17965                                         /* add new field*/
17966                                         emstorage_add_column(multi_user_name, table_names[i][0], new_column_info);
17967                                 }
17968                         }
17969
17970                         emstorage_drop_table(multi_user_name, table_names[i][1]);
17971                 } else
17972                         EM_DEBUG_LOG("Skipped");
17973         }
17974
17975 FINISH_OFF:
17976         if (create_table_query) {
17977                 int i = 0;
17978                 for (i = 0; i < query_len; i++) {
17979                         if (create_table_query[i]) {
17980                                 EM_SAFE_FREE(create_table_query[i]);
17981                         }
17982                 }
17983                 EM_SAFE_FREE(create_table_query);
17984         }
17985
17986         found_data = g_list_first(column_list_of_old_table);
17987         while (found_data != NULL) {
17988                 p_column_info = (email_column_info_t *)found_data->data;
17989                 EM_SAFE_FREE(p_column_info->column_name);
17990                 EM_SAFE_FREE(p_column_info->column_type);
17991                 EM_SAFE_FREE(p_column_info);
17992
17993                 found_data = g_list_next(found_data);
17994         }
17995         g_list_free(column_list_of_old_table);
17996
17997         found_data = g_list_first(column_list_of_new_table);
17998         while (found_data != NULL) {
17999                 p_column_info = (email_column_info_t *)found_data->data;
18000                 EM_SAFE_FREE(p_column_info->column_name);
18001                 EM_SAFE_FREE(p_column_info->column_type);
18002                 EM_SAFE_FREE(p_column_info);
18003
18004                 found_data = g_list_next(found_data);
18005         }
18006         g_list_free(column_list_of_new_table);
18007
18008         EM_DEBUG_FUNC_END("error [%d]", error);
18009         return error;
18010 }
18011 #endif /* __FEATURE_UPDATE_DB_TABLE_SCHEMA__ */
18012
18013 /*EOF*/