Fixed the build error for gcc-14
[platform/core/messaging/email-service.git] / email-core / email-storage / email-storage.c
1 /*
2  *  email-service
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22
23 /******************************************************************************
24  * File: email-storage.c
25  * Desc: email-service Storage Library on Sqlite3
26  *
27  * Auth:
28  *
29  * History:
30  *      2007.03.02 : created
31  *****************************************************************************/
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <stdbool.h>
37 #include <time.h>
38 #include <sys/types.h>
39 #include <sys/time.h>
40 #include <sys/wait.h>
41 #include <glib.h>
42 #include <sys/stat.h>
43 #include <unistd.h>
44 #include <errno.h>
45 #include <dirent.h>
46 #include <vconf.h>
47 #include <sys/mman.h>
48 #include <fcntl.h>
49 #include <tzplatform_config.h>
50
51 #define __USE_UNIX98
52 #define __USE_GNU
53 #include <pthread.h>
54 #include <grp.h>
55 #include <pwd.h>
56
57 #include "email-internal-types.h"
58 #include "email-convert.h"
59 #include "email-core-utils.h"
60 #include "email-utilities.h"
61 #include "email-storage.h"
62 #include "email-debug-log.h"
63 #include "email-types.h"
64 #include "email-convert.h"
65 #include "email-core-signal.h"
66 #include "email-core-event.h"
67 #include "email-core-container.h"
68 #include "email-core-gmime.h"
69 #include "email-core-key-manager.h"
70
71 #define SETTING_MEMORY_TEMP_FILE_PATH "/tmp/email_tmp_file"
72 #define CONTENT_DATA                  "<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset="
73 #define CONTENT_TYPE_DATA             "<meta http-equiv=\"Content-Type\" content=\"text/html; charset="
74
75 #define FLAG_SEEN       0x01
76 #define FLAG_DELETED    0x02
77 #define FLAG_FLAGGED    0x04
78 #define FLAG_ANSWERED   0x08
79 #define FLAG_OLD        0x10
80 #define FLAG_DRAFT      0x20
81
82 #undef close
83
84 #define ISSUE_ORGANIZATION_LEN_IN_MAIL_CERTIFICATE_TBL  256
85 #define EMAIL_ADDRESS_LEN_IN_MAIL_CERTIFICATE_TBL       128
86 #define SUBJECT_STRING_LEN_IN_MAIL_CERTIFICATE_TBL      1027
87 #define FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL           256
88
89 #define ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL            50
90 #define RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL   50
91 #define EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL              128
92 #define USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL               50
93 #define PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL                50
94 #define SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL     50
95 #define SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL            50
96 #define SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL        50
97 #define DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL            30
98 #define REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL           128
99 #define RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL             128
100 #define LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL          256
101 #define DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL       256
102 #define SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL               256
103 #define MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL                128
104 #define ALIAS_LEN_IN_MAIL_BOX_TBL                       128
105 #define LOCAL_MBOX_LEN_IN_MAIL_READ_MAIL_UID_TBL        128
106 #define MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL      128
107 #define S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL             128
108 #define DATA2_LEN_IN_MAIL_READ_MAIL_UID_TBL             256
109 #define FILTER_NAME_LEN_IN_MAIL_RULE_TBL                256
110 #define VALUE_LEN_IN_MAIL_RULE_TBL                      256
111 #define VALUE2_LEN_IN_MAIL_RULE_TBL                     256
112 #define DEST_MAILBOX_LEN_IN_MAIL_RULE_TBL               128
113 #define MAILBOX_NAME_LEN_IN_MAIL_ATTACHMENT_TBL         128
114 #define ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL      256
115 #define ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL      256
116 #define CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL           256
117 #define ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL 128
118 #define MAILBOX_LEN_IN_MAIL_TBL                         128
119 #define SERVER_MAILBOX_LEN_IN_MAIL_TBL                  128
120 #define SERVER_MAIL_ID_LEN_IN_MAIL_TBL                  128
121 #define FROM_LEN_IN_MAIL_TBL                            256
122 #define SENDER_LEN_IN_MAIL_TBL                          256
123 #define REPLY_TO_LEN_IN_MAIL_TBL                        256
124 #define TO_LEN_IN_MAIL_TBL                              3999
125 #define CC_LEN_IN_MAIL_TBL                              3999
126 #define BCC_LEN_IN_MAIL_TBL                             3999
127 #define RETURN_PATH_LEN_IN_MAIL_TBL                     3999
128 #define SUBJECT_LEN_IN_MAIL_TBL                         1027
129 #define THREAD_TOPIC_LEN_IN_MAIL_TBL                    256
130 #define TEXT_1_LEN_IN_MAIL_TBL                          256
131 #define TEXT_2_LEN_IN_MAIL_TBL                          256
132 #define MIME_ENTITY_LEN_IN_MAIL_TBL                     256
133 #define DATETIME_LEN_IN_MAIL_TBL                        128
134 #define MESSAGE_ID_LEN_IN_MAIL_TBL                      256
135 #define FROM_CONTACT_NAME_LEN_IN_MAIL_TBL               3999
136 #define FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL              3999
137 #define TO_CONTACT_NAME_LEN_IN_MAIL_TBL                 3999
138 #define TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL                3999
139 #define MAILBOX_LEN_IN_MAIL_MEETING_TBL                 128
140 #define LOCATION_LEN_IN_MAIL_MEETING_TBL                1024
141 #define GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL        512
142 #define STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL           32
143 #define DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL           32
144 #define PREVIEWBODY_LEN_IN_MAIL_TBL                     512
145 #define CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL        256
146 #define UID                                             "root"          /* UID = root */
147 #define UID_DEFAULT                                     0               /* UID = root */
148 #define GID                                             "priv_email"    /* GID = priv_email */
149 #define GID_DEFAULT                                     10901           /* GID = priv_email */
150 #define MAX_GRP_BUF_SIZE (1024 * 4)
151
152 /*  this define is used for query to change data (delete, insert, update) */
153 #define EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction_flag, error_code) \
154         do { \
155                 if (transaction_flag) { \
156                         if (emstorage_begin_transaction(multi_user_name, NULL, NULL, &error_code) == false) { \
157                                 EM_DEBUG_EXCEPTION("emstorage_begin_transaction error [%d]", error_code); \
158                                 goto FINISH_OFF; \
159                         } \
160                 } \
161         } while (0)
162
163 /*  this define is used for query to change data (delete, insert, update) */
164 #define EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction_flag, result_code, error_code) \
165         do { \
166                 if (transaction_flag) { \
167                         if (result_code == true) { \
168                                 if (emstorage_commit_transaction(multi_user_name, NULL, NULL, NULL) == false) { \
169                                         EM_DEBUG_EXCEPTION("emstorage_commit_transaction error"); \
170                                         error_code = EMAIL_ERROR_DB_FAILURE; \
171                                         result_code = false; \
172                                 } \
173                         } \
174                         else { \
175                                 if (emstorage_rollback_transaction(multi_user_name, NULL, NULL, NULL) == false) { \
176                                         EM_DEBUG_EXCEPTION("emstorage_rollback_transaction error"); \
177                                         error_code = EMAIL_ERROR_DB_FAILURE; \
178                                 } \
179                         } \
180                 } \
181         } while (0)
182
183 /*  this define is used for query to read (select) */
184 #define EMSTORAGE_START_READ_TRANSACTION(transaction_flag) \
185         if (transaction_flag) { \
186                 /*_timedlock_shm_mutex(mapped_for_db_lock, 2);*/\
187         }
188
189 /*  this define is used for query to read (select) */
190 #define EMSTORAGE_FINISH_READ_TRANSACTION(transaction_flag) \
191         if (transaction_flag) { \
192                 /*_unlockshm_mutex(mapped_for_db_lock);*/\
193         }
194
195 /*  for safety DB operation */
196 static pthread_mutex_t _db_handle_lock = PTHREAD_MUTEX_INITIALIZER;
197
198
199 #define _MULTIPLE_DB_HANDLE
200
201 #ifdef _MULTIPLE_DB_HANDLE
202
203 typedef struct {
204         char *user_name;
205         pthread_t thread_id;
206         sqlite3 *db_handle;
207 } db_handle_t;
208
209 #define MAX_DB_CLIENT 100
210
211 /* static int _db_handle_count = 0; */
212 db_handle_t _db_handle_list[MAX_DB_CLIENT] = {{NULL, 0, 0}, };
213
214
215 sqlite3 *emstorage_get_db_handle(char *multi_user_name)
216 {
217         EM_DEBUG_FUNC_BEGIN();
218         int i;
219         pthread_t current_thread_id = THREAD_SELF();
220         sqlite3 *result_db_handle = NULL;
221
222         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
223                                         /* LCOV_EXCL_START */
224                 ENTER_CRITICAL_SECTION(_db_handle_lock);
225                 for (i = 0; i < MAX_DB_CLIENT; i++) {
226                         if (EM_SAFE_STRCASECMP(_db_handle_list[i].user_name, multi_user_name) != 0)
227                                 continue;
228
229                         if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id)) {
230                                 EM_DEBUG_LOG_DEV("found db handle at [%d]", i);
231                                 result_db_handle = _db_handle_list[i].db_handle;
232                                 break;
233                         }
234                 }
235                 LEAVE_CRITICAL_SECTION(_db_handle_lock);
236                                         /* LCOV_EXCL_STOP */
237         } else {
238                 ENTER_CRITICAL_SECTION(_db_handle_lock);
239                 for (i = 0; i < MAX_DB_CLIENT; i++) {
240                         if (EM_SAFE_STRLEN(_db_handle_list[i].user_name) > 0)
241                                         /* LCOV_EXCL_START */
242                                 continue;
243                                         /* LCOV_EXCL_STOP */
244
245                         if (pthread_equal(current_thread_id, _db_handle_list[i].thread_id)) {
246                                 EM_DEBUG_LOG_DEV("found db handle at [%d]", i);
247                                 result_db_handle = _db_handle_list[i].db_handle;
248                                 break;
249                         }
250                 }
251                 LEAVE_CRITICAL_SECTION(_db_handle_lock);
252         }
253
254         if (!result_db_handle)
255                 EM_DEBUG_LOG("no db_handle for [%lu] found", current_thread_id);
256
257         EM_DEBUG_FUNC_END();
258         return result_db_handle;
259 }
260
261 int emstorage_set_db_handle(char *multi_user_name, sqlite3 *db_handle)
262 {
263         EM_DEBUG_FUNC_BEGIN();
264         int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
265         pthread_t current_thread_id = THREAD_SELF();
266
267         ENTER_CRITICAL_SECTION(_db_handle_lock);
268         for (i = 0; i < MAX_DB_CLIENT; i++)     {
269                 if (_db_handle_list[i].thread_id == 0) {
270                         _db_handle_list[i].thread_id = current_thread_id;
271                         _db_handle_list[i].db_handle = db_handle;
272                         /* Only distinguished container and host  */
273                         _db_handle_list[i].user_name = EM_SAFE_STRDUP(multi_user_name);
274                         EM_DEBUG_LOG("current_thread_id [%lu], index [%d]", current_thread_id, i);
275                         error_code =  EMAIL_ERROR_NONE;
276                         break;
277                 }
278         }
279         LEAVE_CRITICAL_SECTION(_db_handle_lock);
280
281         if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
282                                         /* LCOV_EXCL_START */
283                 EM_DEBUG_EXCEPTION("Exceeded the limitation of db client. Can't find empty slot in _db_handle_list.");
284                                         /* LCOV_EXCL_STOP */
285
286         EM_DEBUG_FUNC_END("error_code [%d]", error_code);
287         return error_code;
288 }
289                                         /* LCOV_EXCL_START */
290 int emstorage_remove_db_handle()
291 {
292         EM_DEBUG_FUNC_BEGIN();
293         int i, error_code = EMAIL_ERROR_MAX_EXCEEDED;
294         ENTER_CRITICAL_SECTION(_db_handle_lock);
295         for (i = 0; i < MAX_DB_CLIENT; i++) {
296                 if (_db_handle_list[i].thread_id == THREAD_SELF()) {
297                         _db_handle_list[i].thread_id = 0;
298                         _db_handle_list[i].db_handle = NULL;
299                         EM_SAFE_FREE(_db_handle_list[i].user_name);
300
301                         EM_DEBUG_LOG("index [%d]", i);
302                         error_code = EMAIL_ERROR_NONE;
303                         break;
304                 }
305         }
306         LEAVE_CRITICAL_SECTION(_db_handle_lock);
307
308         if (error_code == EMAIL_ERROR_MAX_EXCEEDED)
309                 EM_DEBUG_EXCEPTION("Can't find proper thread_id");
310
311         EM_DEBUG_FUNC_END("error_code [%d]", error_code);
312         return error_code;
313 }
314
315 int emstorage_reset_db_handle_list()
316 {
317         EM_DEBUG_FUNC_BEGIN();
318         int i;
319
320         ENTER_CRITICAL_SECTION(_db_handle_lock);
321         for (i = 0; i < MAX_DB_CLIENT; i++) {
322                 _db_handle_list[i].thread_id = 0;
323                 _db_handle_list[i].db_handle = NULL;
324                 EM_SAFE_FREE(_db_handle_list[i].user_name);
325         }
326         LEAVE_CRITICAL_SECTION(_db_handle_lock)
327
328                 EM_DEBUG_FUNC_END();
329         return EMAIL_ERROR_NONE;
330 }
331                                         /* LCOV_EXCL_STOP */
332
333 sqlite3 *emstorage_get_db_connection(char *multi_user_name)
334 {
335                                         /* LCOV_EXCL_START */
336         return emstorage_db_open(multi_user_name, NULL);
337                                         /* LCOV_EXCL_STOP */
338 }
339
340 #else   /*  _MULTIPLE_DB_HANDLE */
341
342 sqlite3 *_db_handle = NULL;
343
344 sqlite3 *emstorage_get_db_connection()
345 {
346         if (NULL == _db_handle)
347                 emstorage_db_open(NULL);
348         return _db_handle;
349 }
350 #endif  /*  _MULTIPLE_DB_HANDLE */
351
352 /* ------------------------------------------------------------------------------ */
353 /*  Mutex using shared memory */
354 typedef struct {
355         pthread_mutex_t mutex;
356         int data;
357 } mmapped_t;
358
359 mmapped_t       *mapped_for_db_lock = NULL;
360 int              shm_fd_for_db_lock = 0;
361
362 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
363 mmapped_t       *mapped_for_generating_mail_id = NULL;
364 int              shm_fd_for_generating_mail_id = 0;
365 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
366
367 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__
368 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
369         do {\
370                 _timedlock_shm_mutex(mapped_for_db_lock, 2);\
371                 return_value = function_call;\
372                 _unlockshm_mutex(mapped_for_db_lock);\
373         } while (0)
374
375 #else /*  __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
376 #define EMSTORAGE_PROTECTED_FUNC_CALL(function_call, return_value) \
377 {  return_value = function_call; }
378 #endif /*  __FEATURE_USE_SHARED_MUTEX_FOR_PROTECTED_FUNC_CALL__ */
379
380                                         /* LCOV_EXCL_START */
381 static int emstorage_exec_query_by_prepare_v2(sqlite3 *local_db_handle, char *query_string)
382 {
383         EM_DEBUG_FUNC_BEGIN("local_db_handle[%p] query_string[%p]", local_db_handle, query_string);
384         int error = EMAIL_ERROR_NONE;
385         int rc = 0;
386         DB_STMT db_statement = NULL;
387
388         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, query_string, EM_SAFE_STRLEN(query_string), &db_statement, NULL), rc);
389         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
390                         ("sqlite3_prepare failed [%d] [%s]", rc, query_string));
391
392         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(db_statement), rc);
393         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
394                         ("sqlite3_step failed [%d] [%s]", rc, query_string));
395
396 FINISH_OFF:
397
398         if (db_statement != NULL) {
399                 rc = sqlite3_finalize(db_statement);
400                 if (rc != SQLITE_OK) {
401                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
402                         error = EMAIL_ERROR_DB_FAILURE;
403                 }
404         }
405
406         return error;
407 }
408
409 INTERNAL_FUNC int emstorage_shm_file_init(const char *shm_file_name)
410 {
411         EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
412         char errno_buf[ERRNO_BUF_SIZE] = {0};
413
414         if (!shm_file_name) {
415                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
416                 return EMAIL_ERROR_INVALID_PARAM;
417         }
418
419         int fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); /*  note: permission is not working */
420         if (fd < 0) {
421                 EM_DEBUG_EXCEPTION("shm_open errno [%d] [%s]", errno, EM_STRERROR(errno_buf));
422                 return EMAIL_ERROR_SYSTEM_FAILURE;
423         }
424
425         fchmod(fd, 0666);
426         EM_DEBUG_LOG("** Create SHM FILE **");
427         if (ftruncate(fd, sizeof(mmapped_t)) != 0) {
428                 EM_DEBUG_EXCEPTION("ftruncate errno [%d]", errno);
429                 return EMAIL_ERROR_SYSTEM_FAILURE;
430         }
431
432         mmapped_t *m = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
433         if (m == MAP_FAILED) {
434                 EM_DEBUG_EXCEPTION("mmap errno [%d]", errno);
435                 EM_SAFE_CLOSE(fd);
436                 return EMAIL_ERROR_SYSTEM_FAILURE;
437         }
438
439         m->data = 0;
440
441         pthread_mutexattr_t mattr;
442         pthread_mutexattr_init(&mattr);
443         pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
444         pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
445         pthread_mutex_init(&(m->mutex), &mattr);
446         pthread_mutexattr_destroy(&mattr);
447
448         pthread_mutex_destroy(&(m->mutex));
449         munmap(m, sizeof(mmapped_t));
450
451         EM_SAFE_CLOSE(fd);
452         EM_DEBUG_FUNC_END();
453         return EMAIL_ERROR_NONE;
454 }
455
456 int emstorage_shm_file_destroy(const char *shm_file_name)
457 {
458         EM_DEBUG_FUNC_BEGIN("shm_file_name [%p]", shm_file_name);
459         char errno_buf[ERRNO_BUF_SIZE] = {0};
460
461         if (!shm_file_name) {
462                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
463                 return EMAIL_ERROR_INVALID_PARAM;
464         }
465
466         if (shm_unlink(shm_file_name) != 0)
467                 EM_DEBUG_EXCEPTION("shm_unlink failed: %s", EM_STRERROR(errno_buf));
468         EM_DEBUG_FUNC_END();
469         return EMAIL_ERROR_NONE;
470 }
471                                         /* LCOV_EXCL_STOP */
472 int _initialize_shm_mutex(const char *shm_file_name, int *param_shm_fd, mmapped_t **param_mapped)
473 {
474         EM_DEBUG_FUNC_BEGIN("shm_file_name [%p] param_shm_fd [%p], param_mapped [%p]", shm_file_name, param_shm_fd, param_mapped);
475         char errno_buf[ERRNO_BUF_SIZE] = {0};
476
477         if (!shm_file_name || !param_shm_fd || !param_mapped) {
478                                         /* LCOV_EXCL_START */
479                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
480                 return EMAIL_ERROR_INVALID_PARAM;
481                                         /* LCOV_EXCL_STOP */
482         }
483
484         if (!(*param_mapped)) {
485                 EM_DEBUG_LOG("** mapping begin **");
486                 if (!(*param_shm_fd)) { /*  open shm_file_name at first. Otherwise, the num of files in /proc/pid/fd will be increasing  */
487                         *param_shm_fd = shm_open(shm_file_name, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
488                         if ((*param_shm_fd) == -1) {
489                                         /* LCOV_EXCL_START */
490                                 EM_DEBUG_EXCEPTION("FAIL: shm_open(): %s", EM_STRERROR(errno_buf));
491                                 return EMAIL_ERROR_SYSTEM_FAILURE;
492                                         /* LCOV_EXCL_STOP */
493                         }
494                 }
495
496                 fchmod((*param_shm_fd), 0666);
497                 EM_DEBUG_LOG("** Create SHM FILE **");
498                 if (ftruncate((*param_shm_fd), sizeof(mmapped_t)) != 0) {
499                                         /* LCOV_EXCL_START */
500                         EM_DEBUG_EXCEPTION("ftruncate errno [%d]", errno);
501                         return EMAIL_ERROR_SYSTEM_FAILURE;
502                                         /* LCOV_EXCL_STOP */
503                 }
504
505                 mmapped_t *tmp = (mmapped_t *)mmap(NULL, sizeof(mmapped_t), PROT_READ|PROT_WRITE, MAP_SHARED, (*param_shm_fd), 0);
506                 if (tmp == MAP_FAILED) {
507                                         /* LCOV_EXCL_START */
508                         EM_DEBUG_EXCEPTION("mmap failed: %s", EM_STRERROR(errno_buf));
509                         return EMAIL_ERROR_SYSTEM_FAILURE;
510                                         /* LCOV_EXCL_STOP */
511                 }
512
513                 tmp->data = 0;
514
515                 pthread_mutexattr_t mattr;
516                 pthread_mutexattr_init(&mattr);
517                 pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED);
518                 pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST_NP);
519                 pthread_mutex_init(&(tmp->mutex), &mattr);
520                 pthread_mutexattr_destroy(&mattr);
521
522                 *param_mapped = tmp;
523         }
524
525         EM_DEBUG_FUNC_END();
526         return EMAIL_ERROR_NONE;
527 }
528                                         /* LCOV_EXCL_START */
529 int _timedlock_shm_mutex(mmapped_t *param_mapped, int sec)
530 {
531         EM_DEBUG_FUNC_BEGIN("param_mapped [%p], sec [%d]", param_mapped, sec);
532
533         if (!param_mapped) {
534                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
535                 return EMAIL_ERROR_INVALID_PARAM;
536         }
537
538         struct timespec abs_time;
539         clock_gettime(CLOCK_REALTIME, &abs_time);
540         abs_time.tv_sec += sec;
541         char errno_buf[ERRNO_BUF_SIZE] = {0};
542
543         int err = pthread_mutex_timedlock(&(param_mapped->mutex), &abs_time);
544
545         if (err == EOWNERDEAD) {
546                 err = pthread_mutex_consistent(&(param_mapped->mutex));
547                 EM_DEBUG_EXCEPTION("Previous owner is dead with lock. Fix mutex : %s", EM_STRERROR(errno_buf));
548         } else if (err != 0) {
549                 EM_DEBUG_EXCEPTION("ERROR : %s", EM_STRERROR(errno_buf));
550                 return err;
551         }
552
553         EM_DEBUG_FUNC_END();
554         return EMAIL_ERROR_NONE;
555 }
556
557 void _unlockshm_mutex(mmapped_t *param_mapped)
558 {
559         EM_DEBUG_FUNC_BEGIN();
560         pthread_mutex_unlock(&(param_mapped->mutex));
561         EM_DEBUG_FUNC_END();
562 }
563                                         /* LCOV_EXCL_STOP */
564 /* ------------------------------------------------------------------------------ */
565
566
567 static int _open_counter = 0;
568
569 static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name);
570 static int _read_password_from_secure_storage(char *file_name, char **password);
571
572 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
573 static int _get_cert_password_file_name(int index, char *cert_password_file_name);
574 #endif
575
576 typedef struct {
577         const char *object_name;
578         unsigned int data_flag;
579 } email_db_object_t;
580
581 static const email_db_object_t _g_db_tables[] = {
582         { "mail_read_mail_uid_tbl", 1},
583         { "mail_tbl", 1},
584         { "mail_attachment_tbl", 1},
585         { NULL,  0},
586 };
587
588 static const email_db_object_t _g_db_indexes[] = {
589         { "mail_read_mail_uid_idx1", 1},
590         { "mail_idx1", 1},
591         { "mail_attachment_idx1", 1},
592         { NULL,  1},
593 };
594
595 enum {
596         CREATE_TABLE_MAIL_ACCOUNT_TBL,
597         CREATE_TABLE_MAIL_BOX_TBL,
598         CREATE_TABLE_MAIL_READ_MAIL_UID_TBL,
599         CREATE_TABLE_MAIL_RULE_TBL,
600         CREATE_TABLE_MAIL_TBL,
601         CREATE_TABLE_MAIL_ATTACHMENT_TBL,
602 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
603         CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL,
604 #else
605         CREATE_TABLE_DUMMY_INDEX1,
606 #endif
607         CREATE_TABLE_MAIL_MEETING_TBL,
608 #ifdef __FEATURE_LOCAL_ACTIVITY__
609         CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL,
610 #else
611         CREATE_TABLE_DUMMY_INDEX2,
612 #endif
613         CREATE_TABLE_MAIL_CERTIFICATE_TBL,
614         CREATE_TABLE_MAIL_TASK_TBL,
615 #ifdef __FEATURE_BODY_SEARCH__
616         CREATE_TABLE_MAIL_TEXT_TBL,
617 #else
618         CREATE_TABLE_DUMMY_INDEX3,
619 #endif
620
621 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
622         CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
623 #else
624         CREATE_TABLE_DUMMY_INDEX4,
625 #endif
626
627         /*CREATE INDEX*/
628         CREATE_TABLE_MAIL_ACCOUNT_IDX,
629         CREATE_TABLE_MAIL_BOX_IDX,
630         CREATE_TABLE_MAIL_READ_MAIL_UID_IDX,
631         CREATE_TABLE_MAIL_IDX,
632         CREATE_TABLE_MAIL_ATTACHMENT_IDX,
633         CREATE_TABLE_MAIL_MEETING_IDX,
634         CREATE_TABLE_MAIL_TASK_IDX,
635         CREATE_TABLE_MAIL_DATETIME_IDX,
636         CREATE_TABLE_MAIL_THREAD_IDX,
637         CREATE_TABLE_MAX,
638 };
639
640 enum {
641         DATA1_IDX_IN_MAIL_ACTIVITY_TBL = 0,
642         TRANSTYPE_IDX_IN_MAIL_ACTIVITY_TBL,
643         FLAG_IDX_IN_MAIL_ACTIVITY_TBL,
644 };
645
646 enum {
647         CERTFICATE_BIND_TYPE_IDX_IN_MAIL_CERTIFICATE_TBL = 0,
648         ISSUE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
649         ISSUE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
650         ISSUE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
651         EXPIRE_YEAR_IDX_IN_MAIL_CERTIFICATE_TBL,
652         EXPIRE_MONTH_IDX_IN_MAIL_CERTIFICATE_TBL,
653         EXPIRE_DAY_IDX_IN_MAIL_CERTIFICATE_TBL,
654         ISSUE_ORGANIZATION_IDX_IN_MAIL_CERTIFICATE_TBL,
655         EMAIL_ADDRESS_IDX_IN_MAIL_CERTIFICATE_TBL,
656         SUBJECT_STRING_IDX_IN_MAIL_CERTIFICATE_TBL,
657         FILE_PATH_IDX_IN_MAIL_CERTIFICATE_TBL,
658 };
659
660 enum {
661         TO_RECIPIENT = 0,
662         CC_RECIPIENT,
663         BCC_RECIPIENT,
664 };
665 enum {
666         ACCOUNT_ID_IDX_IN_MAIL_BOX_TBL = 0,
667         LOCAL_YN_IDX_IN_MAIL_BOX_TBL,
668         MAILBOX_NAME_IDX_IN_MAIL_BOX_TBL,
669         MAILBOX_TYPE_IDX_IN_MAIL_BOX_TBL,
670         ALIAS_IDX_IN_MAIL_BOX_TBL,
671         SYNC_WITH_SERVER_YN_IDX_IN_MAIL_BOX_TBL,
672         MODIFIABLE_YN_IDX_IN_MAIL_BOX_TBL,
673         TOTAL_MAIL_COUNT_ON_SERVER_IDX_IN_MAIL_BOX_TBL,
674         ARCHIVE_IDX_IN_MAIL_BOX_TBL,
675         MAIL_SLOT_SIZE_IDX_IN_MAIL_BOX_TBL,
676 };
677
678 enum {
679         ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL = 0,
680         LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
681         MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL,
682         LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
683         SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL,
684         RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL,
685         SYNC_STATUS_IDX_IN_MAIL_READ_MAIL_UID_TBL,
686         FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL,
687         FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL,
688         IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL,  /* unused */
689 };
690
691 #ifdef __FEATURE_BODY_SEARCH__
692 enum {
693         MAIL_ID_IDX_IN_MAIL_TEXT_TBL = 0,
694         ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL,
695         MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL,
696         BODY_TEXT_IDX_IN_MAIL_TEXT_TBL,
697         FIELD_COUNT_OF_MAIL_TEXT_TBL,
698 };
699 #endif
700
701 enum {
702         ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL = 0,
703         RULE_ID_IDX_IN_MAIL_RULE_TBL,
704         FILTER_NAME_IDX_IN_MAIL_RULE_TBL,
705         TYPE_IDX_IN_MAIL_RULE_TBL,
706         VALUE_IDX_IN_MAIL_RULE_TBL,
707         VALUE2_IDX_IN_MAIL_RULE_TBL,
708         ACTION_TYPE_IDX_IN_MAIL_RULE_TBL,
709         TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL,
710         FLAG1_IDX_IN_MAIL_RULE_TBL,
711         FLAG2_IDX_IN_MAIL_RULE_TBL,
712 };
713
714 enum {
715         MAIL_ID_IDX_IN_MAIL_TBL = 0,
716         ACCOUNT_ID_IDX_IN_MAIL_TBL,
717         MAILBOX_ID_IDX_IN_MAIL_TBL,
718         MAILBOX_TYPE_IDX_IN_MAIL_TBL,
719         SUBJECT_IDX_IN_MAIL_TBL,
720         DATETIME_IDX_IN_MAIL_TBL,
721         SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL,
722         SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL,
723         SERVER_MAIL_ID_IDX_IN_MAIL_TBL,
724         MESSAGE_ID_IDX_IN_MAIL_TBL,
725         REFERENCE_ID_IDX_IN_MAIL_TBL,
726         FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL,
727         FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL,
728         FULL_ADDRESS_TO_IDX_IN_MAIL_TBL,
729         FULL_ADDRESS_CC_IDX_IN_MAIL_TBL,
730         FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL,
731         FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL,
732         EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL,
733         EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL,
734         ALIAS_SENDER_IDX_IN_MAIL_TBL,
735         ALIAS_RECIPIENT_IDX_IN_MAIL_TBL,
736         BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL,
737         FILE_PATH_PLAIN_IDX_IN_MAIL_TBL,
738         FILE_PATH_HTML_IDX_IN_MAIL_TBL,
739         FILE_PATH_MIME_ENTITY_IDX_IN_MAIL_TBL,
740         MAIL_SIZE_IDX_IN_MAIL_TBL,
741         FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL,
742         FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL,
743         FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL,
744         FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL,
745         FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL,
746         FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL,
747         FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL,
748         DRM_STATUS_IDX_IN_MAIL_TBL,
749         PRIORITY_IDX_IN_MAIL_TBL,
750         SAVE_STATUS_IDX_IN_MAIL_TBL,
751         LOCK_STATUS_IDX_IN_MAIL_TBL,
752         REPORT_STATUS_IDX_IN_MAIL_TBL,
753         ATTACHMENT_COUNT_IDX_IN_MAIL_TBL,
754         INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL,
755         THREAD_ID_IDX_IN_MAIL_TBL,
756         THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL,
757         PREVIEW_TEXT_IDX_IN_MAIL_TBL,
758         MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL,
759         MESSAGE_CLASS_IDX_IN_MAIL_TBL,
760         DIGEST_TYPE_IDX_IN_MAIL_TBL,
761         SMIME_TYPE_IDX_IN_MAIL_TBL,
762         SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL,
763         REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL,
764         TAG_ID_IDX_IN_MAIL_TBL,
765         REPLIED_TIME_IDX_IN_MAIL_TBL,
766         FORWARDED_TIME_IDX_IN_MAIL_TBL,
767         DEFAULT_CHARSET_IDX_IN_MAIL_TBL,
768         EAS_DATA_LENGTH_IDX_IN_MAIL_TBL,
769         EAS_DATA_IDX_IN_MAIL_TBL,
770         USER_NAME_IDX_IN_MAIL_TBL,
771         FIELD_COUNT_OF_MAIL_TBL,        /* End of mail_tbl */
772 };
773
774 enum {
775         ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL = 0,
776         ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL,
777         ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL,
778         CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
779         ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL,
780         MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
781         ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
782         MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL,
783         ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
784         ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
785         ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL,
786         ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL,
787         ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL,
788 #ifdef __ATTACHMENT_OPTI__
789         ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL,
790         SECTION_IDX_IN_MAIL_ATTACHMENT_TBL,
791 #endif
792 };
793
794 enum {
795         IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL = 0,
796 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
797         MAIL_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
798         ACCOUNT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
799         ADDRESS_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
800         ADDRESS_IDX_IDX_IN_MAIL_CONTACT_SYNC_TBL,
801 #endif
802         ADDRESS_IDX_IN_MAIL_CONTACT_SYNC_TBL,
803         CONTACT_ID_IDX_IN_MAIL_CONTACT_SYNC_TBL,
804         STORAGE_TYPE_IDX_IN_MAIL_CONTACT_SYNC_TBL,
805         CONTACT_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
806 #ifndef USE_SIMPLE_CONTACT_SYNC_ATTRIBUTES
807         DISPLAY_NAME_IDX_IN_MAIL_CONTACT_SYNC_TBL,
808         FLAG1_IDX_IN_MAIL_CONTACT_SYNC_TBL,
809 #endif
810 };
811
812 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
813 enum {
814         ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL = 0,
815         STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
816         ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
817         MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
818         SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
819         MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
820         MULTI_USER_NAME_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL,
821 };
822 #endif
823
824 /* sowmya.kr 03032010, changes for get list of mails for given addr list */
825 typedef struct _em_mail_id_list {
826         int mail_id;
827         struct _em_mail_id_list *next;
828 } em_mail_id_list;
829
830 static char *g_test_query[] = {
831         /*  1. select mail_account_tbl */
832         "SELECT"
833                 " account_name, "
834                 " incoming_server_type, "
835                 " incoming_server_address, "
836                 " user_email_address, "
837                 " incoming_server_user_name, "
838                 " incoming_server_password, "
839                 " retrieval_mode, "
840                 " incoming_server_port_number, "
841                 " incoming_server_secure_connection, "
842                 " incoming_server_authentication_method,"
843                 " outgoing_server_type, "
844                 " outgoing_server_address, "
845                 " outgoing_server_port_number, "
846                 " outgoing_server_need_authentication, "
847                 " outgoing_server_secure_connection, "
848                 " outgoing_server_user_name, "
849                 " outgoing_server_password, "
850                 " display_name, "
851                 " reply_to_addr, "
852                 " return_addr, "
853                 " account_id, "
854                 " keep_mails_on_pop_server_after_download, "
855                 " auto_resend_times, "
856                 " outgoing_server_size_limit, "
857                 " wifi_auto_download, "
858                 " pop_before_smtp, "
859                 " incoming_server_requires_apop,"
860                 " logo_icon_path, "
861                 " is_preset_account, "
862                 " check_interval, "
863                 " priority, "
864                 " keep_local_copy, "
865                 " req_delivery_receipt, "
866                 " req_read_receipt, "
867                 " download_limit, "
868                 " block_address, "
869                 " block_subject, "
870                 " display_name_from, "
871                 " reply_with_body, "
872                 " forward_with_files, "
873                 " add_myname_card, "
874                 " add_signature, "
875                 " signature"
876                 ", add_my_address_to_bcc"
877                 ", notification_status "
878                 ", vibrate_status "
879                 ", display_content_status "
880                 ", default_ringtone_status "
881                 ", alert_ringtone_path "
882                 ", account_svc_id "
883                 ", index_color "
884                 ", sync_status "
885                 ", sync_disabled "
886                 ", smime_type"
887                 ", certificate_path"
888                 ", cipher_type"
889                 ", digest_type"
890                 " FROM mail_account_tbl",
891         /*  2. select mail_box_tbl */
892         "SELECT "
893                 "   mailbox_id, "
894                 "   account_id, "
895                 "   local_yn,  "
896                 "   mailbox_name,  "
897                 "   mailbox_type,   "
898                 "   alias,  "
899                 "   deleted_flag,  "
900                 "   modifiable_yn,  "
901                 "   total_mail_count_on_server,  "
902                 "   has_archived_mails, "
903                 "   mail_slot_size, "
904                 "   no_select, "
905                 "   last_sync_time "
906                 " FROM mail_box_tbl ",
907         /*  3. select mail_read_mail_uid_tbl */
908         "SELECT  "
909                 "   account_id,  "
910                 "   mailbox_id,  "
911                 "   mailbox_name,  "
912                 "   local_uid,  "
913                 "   server_uid,  "
914                 "   rfc822_size ,  "
915                 "   sync_status,  "
916                 "   flags_seen_field,  "
917                 "   idx_num "
918                 " FROM mail_read_mail_uid_tbl ",
919         /*  4. select mail_rule_tbl */
920         "SELECT "
921                 "   account_id, "
922                 "   rule_id, "
923                 "   filter_name, "
924                 "   type, "
925                 "   value, "
926                 "   value2, "
927                 "   action_type, "
928                 "   target_mailbox_id,  "
929                 "   flag1, "
930                 "   flag2 "
931                 " FROM mail_rule_tbl    ",
932         /*  5. select mail_tbl */
933         "SELECT"
934                 "       mail_id, "
935                 "       account_id, "
936                 "       mailbox_id, "
937                 "       mailbox_name, "
938                 "   mailbox_type, "
939                 "   subject, "
940                 "       date_time, "
941                 "       server_mail_status, "
942                 "       server_mailbox_name, "
943                 "       server_mail_id, "
944                 "   message_id, "
945                 "       reference_mail_id, "
946                 "   full_address_from, "
947                 "   full_address_reply, "
948                 "   full_address_to, "
949                 "   full_address_cc, "
950                 "   full_address_bcc, "
951                 "   full_address_return, "
952                 "   email_address_sender, "
953                 "   email_address_recipient, "
954                 "   alias_sender, "
955                 "   alias_recipient, "
956                 "       body_download_status, "
957                 "       file_path_plain, "
958                 "       file_path_html, "
959                 "   file_path_mime_entity, "
960                 "       mail_size, "
961                 "   flags_seen_field     ,"
962                 "   flags_deleted_field  ,"
963                 "   flags_flagged_field  ,"
964                 "   flags_answered_field ,"
965                 "   flags_recent_field   ,"
966                 "   flags_draft_field    ,"
967                 "   flags_forwarded_field,"
968                 "       DRM_status, "
969                 "       priority, "
970                 "       save_status, "
971                 "       lock_status, "
972                 "       report_status, "
973                 "   attachment_count, "
974                 "       inline_content_count, "
975                 "       thread_id, "
976                 "       thread_item_count, "
977                 "   preview_text, "
978                 "       meeting_request_status, "
979                 "   message_class, "
980                 "   digest_type, "
981                 "   smime_type "
982                 " FROM mail_tbl",
983         /*  6. select mail_attachment_tbl */
984         "SELECT "
985                 "       attachment_id, "
986                 "       attachment_name, "
987                 "       attachment_path, "
988                 "       attachment_size, "
989                 "       mail_id,  "
990                 "       account_id, "
991                 "       mailbox_id, "
992                 "       attachment_save_status,  "
993                 "       attachment_drm_type,  "
994                 "       attachment_drm_method,  "
995                 "       attachment_inline_content_status,  "
996                 "       attachment_mime_type  "
997                 " FROM mail_attachment_tbl ",
998
999 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
1000         "SELECT  "
1001                 "   account_id, "
1002                 "   mail_id, "
1003                 "   server_mail_id, "
1004                 "   activity_id, "
1005                 "   activity_type, "
1006                 "   mailbox_id, "
1007                 "   mailbox_name "
1008                 "   multi_user_name "
1009                 " FROM mail_partial_body_activity_tbl ",
1010 #endif
1011
1012         "SELECT  "
1013                 "   mail_id, "
1014                 "   account_id, "
1015                 "   mailbox_id, "
1016                 "   meeting_response, "
1017                 "   start_time, "
1018                 "   end_time, "
1019                 "   location, "
1020                 "   global_object_id, "
1021                 "   offset, "
1022                 "   standard_name, "
1023                 "   standard_time_start_date, "
1024                 "   standard_bias, "
1025                 "   daylight_name, "
1026                 "   daylight_time_start_date, "
1027                 "   daylight_bias "
1028                 " FROM mail_meeting_tbl ",
1029
1030 #ifdef __FEATURE_LOCAL_ACTIVITY__
1031         "SELECT "
1032                 "       activity_id, "
1033                 "   account_id, "
1034                 "   mail_id, "
1035                 "   activity_type, "
1036                 "   server_mailid, "
1037                 "   src_mbox , "
1038                 "       dest_mbox "
1039                 " FROM mail_local_activity_tbl  ",
1040 #endif
1041         "SELECT "
1042                 "       certificate_id, "
1043                 "   issue_year, "
1044                 "   issue_month, "
1045                 "   issue_day, "
1046                 "   expiration_year, "
1047                 "   expiration_month, "
1048                 "   expiration_day, "
1049                 "   issue_organization_name, "
1050                 "   email_address, "
1051                 "   subject_str, "
1052                 "   filepath, "
1053                 "   password "
1054                 " FROM mail_certificate_tbl     ",
1055         "SELECT "
1056                 "       task_id, "
1057                 "   task_type, "
1058                 "   task_status, "
1059                 "   task_priority, "
1060                 "   task_parameter_length, "
1061                 "   task_parameter , "
1062                 "       date_time "
1063                 " FROM mail_task_tbl    ",
1064 #ifdef __FEATURE_BODY_SEARCH__
1065         "SELECT "
1066                 "       mail_id, "
1067                 "   account_id, "
1068                 "   mailbox_id, "
1069                 "   body_text "
1070                 " FROM mail_text_tbl    ",
1071 #endif
1072 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
1073         "SELECT  "
1074                 "   activity_id, "
1075                 "   status, "
1076                 "   account_id, "
1077                 "   mail_id, "
1078                 "   server_mail_id, "
1079                 "   mailbox_id, "
1080                 "   multi_user_name, "
1081                 " FROM mail_auto_download_activity_tbl ",
1082 #endif
1083         NULL,
1084 };
1085
1086 int _field_count_of_table[CREATE_TABLE_MAX] = { 0, };
1087
1088 static int _get_table_field_data_char(char  **table, char *buf, int index)
1089 {
1090         if ((table == NULL) || (buf == NULL) || (index < 0)) {
1091                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1092                 return false;
1093         }
1094
1095         if (table[index] != NULL) {
1096                 *buf = (char)atoi(table[index]);
1097                 return true;
1098         }
1099
1100         /*  EM_DEBUG_LOG("Empty field. Set as zero"); */
1101
1102         *buf = 0;
1103         return false;
1104 }
1105
1106 static int _get_table_field_data_int(char  **table, int *buf, int index)
1107 {
1108         if ((table == NULL) || (buf == NULL) || (index < 0)) {
1109                                         /* LCOV_EXCL_START */
1110                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1111                 return false;
1112                                         /* LCOV_EXCL_STOP */
1113         }
1114
1115         if (table[index] != NULL) {
1116                 *buf = atoi(table[index]);
1117                 return true;
1118         }
1119
1120         /*  EM_DEBUG_LOG("Empty field. Set as zero"); */
1121                                         /* LCOV_EXCL_START */
1122         *buf = 0;
1123         return false;
1124                                         /* LCOV_EXCL_STOP */
1125 }
1126
1127 static int _get_table_field_data_time_t(char  **table, time_t *buf, int index)
1128 {
1129         if ((table == NULL) || (buf == NULL) || (index < 0)) {
1130                                         /* LCOV_EXCL_START */
1131                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1132                 return false;
1133                                         /* LCOV_EXCL_STOP */
1134         }
1135
1136         if (table[index] != NULL) {
1137                 *buf = (time_t)atol(table[index]);
1138                 return true;
1139         }
1140
1141         /*  EM_DEBUG_LOG("Empty field. Set as zero"); */
1142                                         /* LCOV_EXCL_START */
1143         *buf = 0;
1144         return false;
1145                                         /* LCOV_EXCL_STOP */
1146 }
1147
1148 static int _get_table_field_data_string(char **table, char **buf, int ucs2, int index)
1149 {
1150         int ret = false;
1151
1152         if ((table == NULL) || (buf == NULL) || (index < 0)) {
1153                 EM_DEBUG_EXCEPTION("table[%p], buf[%p], index[%d]", table, buf, index);
1154                 return false;
1155         }
1156
1157         char *pTemp = table[index];
1158         int sLen = 0;
1159         if (pTemp == NULL)
1160                 *buf = NULL;
1161         else {
1162                 sLen = EM_SAFE_STRLEN(pTemp);
1163                 if (sLen) {
1164                         *buf = (char *) em_malloc(sLen + 1);
1165                         if (*buf == NULL) {
1166                                 EM_DEBUG_EXCEPTION("malloc is failed");
1167                                 goto FINISH_OFF;
1168                         }
1169                         strncpy(*buf, pTemp, sLen+1);
1170                         (*buf)[sLen] = '\0';
1171                 } else
1172                         *buf = NULL;
1173         }
1174 #ifdef _PRINT_STORAGE_LOG_
1175         if (*buf)
1176                 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", *buf, index);
1177         else
1178                 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1179 #endif
1180         ret = true;
1181 FINISH_OFF:
1182
1183         return ret;
1184 }
1185
1186 static int _get_table_field_data_string_without_allocation(char **table, char *buf, int buffer_size, int ucs2, int index)
1187 {
1188         if ((table == NULL) || (buf == NULL) || (index < 0)) {
1189                 EM_DEBUG_EXCEPTION(" table[%p], buf[%p], index[%d]", table, buf, index);
1190                 return false;
1191         }
1192
1193         char *pTemp = table[index];
1194
1195         if (pTemp == NULL)
1196                 buf = NULL;
1197         else {
1198                 memset(buf, 0, buffer_size);
1199                 strncpy(buf, pTemp, buffer_size - 1);
1200         }
1201 #ifdef _PRINT_STORAGE_LOG_
1202         if (buf)
1203                 EM_DEBUG_LOG("_get_table_field_data_string - buf[%s], index[%d]", buf, 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_table_field_data_blob(char **table, void **buffer, int buffer_size, int index)
1212 {
1213         if ((table == NULL) || (buffer == NULL) || (index < 0)) {
1214                                         /* LCOV_EXCL_START */
1215                 EM_DEBUG_EXCEPTION(" table[%p], buffer[%p], buffer_size [%d], index[%d]", table, buffer, buffer_size, index);
1216                 return false;
1217                                         /* LCOV_EXCL_STOP */
1218         }
1219
1220         char *temp_buffer = table[index];
1221
1222         if (temp_buffer == NULL)
1223                 buffer = NULL;
1224         else {
1225                                         /* LCOV_EXCL_START */
1226                 *buffer = malloc(buffer_size);
1227                 if (*buffer == NULL) {
1228                         EM_DEBUG_EXCEPTION("allocation failed.");
1229                         return false;
1230                 }
1231                 memset(*buffer, 0, buffer_size);
1232                 memcpy(*buffer, temp_buffer, buffer_size);
1233                                         /* LCOV_EXCL_STOP */
1234         }
1235 #ifdef _PRINT_STORAGE_LOG_
1236         if (buf)
1237                 EM_DEBUG_LOG("_get_table_field_data_string - buffer[%s], index[%d]", buffer, index);
1238         else
1239                 EM_DEBUG_LOG("_get_table_field_data_string - No string got ");
1240 #endif
1241
1242         return true;
1243 }
1244                                         /* LCOV_EXCL_START */
1245 static int _get_stmt_field_data_char(DB_STMT hStmt, char *buf, int index)
1246 {
1247         if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1248                 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1249                 return false;
1250         }
1251
1252         if (sqlite3_column_text(hStmt, index) != NULL) {
1253                 *buf = (char)sqlite3_column_int(hStmt, index);
1254 #ifdef _PRINT_STORAGE_LOG_
1255                 EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1256 #endif
1257                 return true;
1258         }
1259
1260         EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1261
1262         return false;
1263 }
1264                                         /* LCOV_EXCL_STOP */
1265 static int _get_stmt_field_data_int(DB_STMT hStmt, int *buf, int index)
1266 {
1267         if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1268                                         /* LCOV_EXCL_START */
1269                 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1270                 return false;
1271                                         /* LCOV_EXCL_STOP */
1272         }
1273
1274         if (sqlite3_column_text(hStmt, index) != NULL) {
1275                 *buf = sqlite3_column_int(hStmt, index);
1276 #ifdef _PRINT_STORAGE_LOG_
1277                 EM_DEBUG_LOG("_get_stmt_field_data_int [%d]", *buf);
1278 #endif
1279                 return true;
1280         }
1281                                         /* LCOV_EXCL_START */
1282         EM_DEBUG_LOG("sqlite3_column_int fail. index [%d]", index);
1283
1284         return false;
1285                                         /* LCOV_EXCL_STOP */
1286 }
1287                                         /* LCOV_EXCL_START */
1288 static int _get_stmt_field_data_time_t(DB_STMT hStmt, time_t *buf, int index)
1289 {
1290         if ((hStmt == NULL) || (buf == NULL) || (index < 0)) {
1291                 EM_DEBUG_EXCEPTION("buf[%p], index[%d]", buf, index);
1292                 return false;
1293         }
1294
1295         if (sqlite3_column_text(hStmt, index) != NULL) {
1296                 *buf = (time_t)sqlite3_column_int(hStmt, index);
1297 #ifdef _PRINT_STORAGE_LOG_
1298                 EM_DEBUG_LOG("_get_stmt_field_data_time_t [%d]", *buf);
1299 #endif
1300                 return true;
1301         }
1302
1303         EM_DEBUG_LOG("_get_stmt_field_data_time_t fail. index [%d]", index);
1304         return false;
1305 }
1306                                         /* LCOV_EXCL_STOP */
1307 static int _get_stmt_field_data_string(DB_STMT hStmt, char **buf, int ucs2, int index)
1308 {
1309         if (!hStmt || !buf || (index < 0)) { /*prevent 39619*/
1310                 EM_DEBUG_EXCEPTION("No hStmt, buf[%p], index[%d]", buf, index);
1311                 return false;
1312         }
1313
1314         int sLen = 0;
1315         sLen = sqlite3_column_bytes(hStmt, index);
1316
1317 #ifdef _PRINT_STORAGE_LOG_
1318         EM_DEBUG_LOG("_get_stmt_field_data_string sqlite3_column_bytes sLen[%d]", sLen);
1319 #endif
1320
1321         if (sLen > 0) {
1322                 *buf = (char *) em_malloc(sLen + 1);
1323                 if (*buf == NULL) {
1324                         EM_DEBUG_EXCEPTION("em_mallocfailed");
1325                         return false;
1326                 }
1327
1328                 strncpy(*buf, (char *)sqlite3_column_text(hStmt, index), sLen);
1329         } else
1330                 *buf = NULL;
1331
1332 #ifdef _PRINT_STORAGE_LOG_
1333         if (*buf)
1334                 EM_DEBUG_LOG("buf[%s], index[%d]", *buf, index);
1335         else
1336                 EM_DEBUG_LOG("_get_stmt_field_data_string - No string got");
1337 #endif
1338
1339         return false;
1340 }
1341
1342 static void _get_stmt_field_data_blob(DB_STMT hStmt, void **buf, int index)
1343 {
1344         if (!hStmt || !buf || (index < 0)) { /*prevent 39618*/
1345                                         /* LCOV_EXCL_START */
1346                 EM_DEBUG_EXCEPTION("No hStmt, buf[%p], index[%d]", buf, index);
1347                 return;
1348                                         /* LCOV_EXCL_STOP */
1349         }
1350
1351         int sLen = 0;
1352         sLen = sqlite3_column_bytes(hStmt, index);
1353
1354 #ifdef _PRINT_STORAGE_LOG_
1355         EM_DEBUG_LOG("_get_stmt_field_data_blob sqlite3_column_bytes sLen[%d]", sLen);
1356 #endif
1357
1358         if (sLen > 0) {
1359                 *buf = (char *) em_malloc(sLen);
1360                 if (*buf == NULL) {
1361                                         /* LCOV_EXCL_START */
1362                         EM_DEBUG_EXCEPTION("em_mallocfailed");
1363                         return;
1364                                         /* LCOV_EXCL_STOP */
1365                 }
1366
1367                 memcpy(*buf, (void *)sqlite3_column_blob(hStmt, index), sLen);
1368         } else
1369                                         /* LCOV_EXCL_START */
1370                 *buf = NULL;
1371                                         /* LCOV_EXCL_STOP */
1372
1373 }
1374                                         /* LCOV_EXCL_START */
1375 static int _bind_stmt_field_data_char(DB_STMT hStmt, int index, char value)
1376 {
1377         if ((hStmt == NULL) || (index < 0)) {
1378                 EM_DEBUG_EXCEPTION("index[%d]", index);
1379                 return false;
1380         }
1381
1382         int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1383
1384         if (ret != SQLITE_OK) {
1385                 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1386                 return false;
1387         }
1388
1389         return true;
1390 }
1391
1392 static int _bind_stmt_field_data_int(DB_STMT hStmt, int index, int value)
1393 {
1394         if ((hStmt == NULL) || (index < 0)) {
1395                 EM_DEBUG_EXCEPTION("index[%d]", index);
1396                 return false;
1397         }
1398
1399         int ret = sqlite3_bind_int(hStmt, index+1, value);
1400
1401         if (ret != SQLITE_OK) {
1402                 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1403                 return false;
1404         }
1405
1406         return true;
1407 }
1408
1409 static int _bind_stmt_field_data_time_t(DB_STMT hStmt, int index, time_t value)
1410 {
1411         if ((hStmt == NULL) || (index < 0)) {
1412                 EM_DEBUG_EXCEPTION("index[%d]", index);
1413                 return false;
1414         }
1415
1416         int ret = sqlite3_bind_int(hStmt, index+1, (int)value);
1417
1418         if (ret != SQLITE_OK) {
1419                 EM_DEBUG_EXCEPTION("sqlite3_bind_int fail - %d", ret);
1420                 return false;
1421         }
1422
1423         return true;
1424 }
1425                                         /* LCOV_EXCL_STOP */
1426 static int _bind_stmt_field_data_string(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1427 {
1428         if ((hStmt == NULL) || (index < 0)) {
1429                 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1430                 return false;
1431         }
1432
1433 #ifdef _PRINT_STORAGE_LOG_
1434         EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1435 #endif
1436
1437         int ret = 0;
1438         if (value != NULL)
1439                 ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1440         else
1441                 ret = sqlite3_bind_text(hStmt, index+1, "", -1, SQLITE_STATIC);
1442
1443         if (ret != SQLITE_OK) {
1444                 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1445                 return false;
1446         }
1447         return true;
1448 }
1449
1450 static int _bind_stmt_field_data_nstring(DB_STMT hStmt, int index, char *value, int ucs2, int max_len)
1451 {
1452         if ((hStmt == NULL) || (index < 0)) {
1453                 EM_DEBUG_EXCEPTION("index[%d], max_len[%d]", index, max_len);
1454                 return false;
1455         }
1456
1457 #ifdef _PRINT_STORAGE_LOG_
1458         EM_DEBUG_LOG("hStmt = %p, index = %d, max_len = %d, value = [%s]", hStmt, index, max_len, value);
1459 #endif
1460
1461         int ret = 0;
1462         if (value != NULL) {
1463                 if (strlen(value) <= max_len)
1464                         ret = sqlite3_bind_text(hStmt, index+1, value, -1, SQLITE_STATIC);
1465                 else {
1466                         char *buf = (char*)em_malloc(sizeof(char) * (max_len));
1467                         if (buf == NULL) {
1468                                 EM_DEBUG_EXCEPTION("em_mallocfailed");
1469                                 return false;
1470                         }
1471                         snprintf(buf, max_len-1, "%s", value);
1472                         ret = sqlite3_bind_text(hStmt, index+1, buf, -1, SQLITE_TRANSIENT);
1473                         g_free(buf);
1474                 }
1475         } else
1476                 ret = sqlite3_bind_text(hStmt, index+1, "", -1, SQLITE_STATIC);
1477
1478         if (ret != SQLITE_OK) {
1479                 EM_DEBUG_EXCEPTION("sqlite3_bind_text fail [%d]", ret);
1480                 return false;
1481         }
1482         return true;
1483 }
1484
1485                                         /* LCOV_EXCL_START */
1486 static int _bind_stmt_field_data_blob(DB_STMT hStmt, int index, void *blob, int blob_size)
1487 {
1488         if ((hStmt == NULL) || (index < 0)) {
1489                 EM_DEBUG_EXCEPTION("index[%d], blob_size[%d]", index, blob_size);
1490                 return false;
1491         }
1492
1493 #ifdef _PRINT_STORAGE_LOG_
1494         EM_DEBUG_LOG("hStmt = %p, index = %d, blob_size = %d, blob = [%p]", hStmt, index, blob_size, blob);
1495 #endif
1496
1497         int ret = 0;
1498         if (blob_size > 0)
1499                 ret = sqlite3_bind_blob(hStmt, index+1, blob, blob_size, SQLITE_STATIC);
1500         else
1501                 ret = sqlite3_bind_null(hStmt, index+1);
1502
1503         if (ret != SQLITE_OK) {
1504                 EM_DEBUG_EXCEPTION("sqlite3_bind_blob fail [%d]", ret);
1505                 return false;
1506         }
1507         return true;
1508 }
1509
1510 static int emstorage_exec_query_by_prepare_v2_stmt(sqlite3 *local_db_handle, char *query_string, char * target_mailbox_name, int input_mailbox_id, int target_account_id)
1511 {
1512
1513         EM_DEBUG_FUNC_BEGIN("Prepared Statement: local_db_handle[%p] query_string[%p]", local_db_handle, query_string);
1514         int error = EMAIL_ERROR_NONE;
1515         int rc = 0, count = 0;
1516         DB_STMT db_statement = NULL;
1517
1518         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, query_string, EM_SAFE_STRLEN(query_string), &db_statement, NULL), rc);
1519
1520         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
1521                         ("sqlite3_prepare_stmt failed [%d] [%s]", rc, query_string));
1522
1523         _bind_stmt_field_data_string(db_statement, count++, target_mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
1524         _bind_stmt_field_data_int(db_statement, count++, input_mailbox_id);
1525         _bind_stmt_field_data_int(db_statement, count++, target_account_id);
1526
1527
1528
1529         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(db_statement), rc);
1530         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
1531                         ("sqlite3_step_stmt failed [%d] [%s]", rc, query_string));
1532
1533 FINISH_OFF:
1534
1535         if (db_statement != NULL) {
1536                 rc = sqlite3_finalize(db_statement);
1537                 if (rc != SQLITE_OK) {
1538                         EM_DEBUG_EXCEPTION("sqlite3_finalize_stmt error [%d]", rc);
1539                         error = EMAIL_ERROR_DB_FAILURE;
1540                 }
1541         }
1542
1543         return error;
1544
1545
1546 }
1547
1548 static int _delete_temp_file(const char *path)
1549 {
1550         EM_DEBUG_FUNC_BEGIN("path[%p]", path);
1551
1552         DIR *dp = NULL;
1553         struct dirent *entry = NULL;
1554
1555         char buf[1024] = {0x00, };
1556
1557         if ((dp = opendir(path)) == NULL) {
1558                 EM_DEBUG_EXCEPTION("opendir(\"%s\") failed...", path);
1559                 return false;
1560         }
1561
1562         while ((entry = readdir(dp)) != NULL) {
1563                 SNPRINTF(buf, sizeof(buf), "%s/%s", path, entry->d_name);
1564                 if (remove(buf) != 0) {
1565                         EM_DEBUG_EXCEPTION("File Deletion Failed`");
1566                 }
1567         }
1568
1569         closedir(dp);
1570         EM_DEBUG_FUNC_END();
1571         return true;
1572 }
1573
1574 char *cpy_str(char *src)
1575 {
1576         char *p = NULL;
1577
1578         if (src) {
1579                 if (!(p = em_malloc((int)EM_SAFE_STRLEN(src) + 1))) {
1580                         EM_DEBUG_EXCEPTION("mailoc failed...");
1581                         return NULL;
1582                 }
1583                 strncpy(p, src, EM_SAFE_STRLEN(src)+1);
1584         }
1585
1586         return p;
1587 }
1588
1589 static void _emstorage_close_once(void)
1590 {
1591         EM_DEBUG_FUNC_BEGIN();
1592
1593         EM_DEBUG_FUNC_END();
1594 }
1595
1596 INTERNAL_FUNC int emstorage_close(int *err_code)
1597 {
1598         EM_DEBUG_FUNC_BEGIN();
1599
1600         int ret = false;
1601         int error = EMAIL_ERROR_NONE;
1602
1603         if (!emstorage_db_close(NULL, &error))
1604
1605                 if (--_open_counter == 0)
1606                         _emstorage_close_once();
1607
1608         ret = true;
1609
1610         if (err_code != NULL)
1611                 *err_code = error;
1612
1613         EM_DEBUG_FUNC_END("ret [%d]", ret);
1614         return ret;
1615 }
1616
1617 static void *_emstorage_open_once(char *multi_user_name, int *err_code)
1618 {
1619         EM_DEBUG_FUNC_BEGIN();
1620
1621         int error = EMAIL_ERROR_NONE;
1622
1623         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1624                 char buf[MAX_PATH] = {0};
1625                 char *prefix_path = NULL;
1626
1627                 error = emcore_get_container_path(multi_user_name, &prefix_path);
1628                 if (error != EMAIL_ERROR_NONE) {
1629                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
1630                         goto FINISH_OFF;
1631                 }
1632
1633                 memset(buf, 0x00, sizeof(buf));
1634                 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, EMAIL_PATH);
1635                 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
1636                         EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
1637                         error = EMAIL_ERROR_SYSTEM_FAILURE;
1638                         if (errno == 28)
1639                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
1640                         goto FINISH_OFF;
1641                 }
1642
1643                 memset(buf, 0x00, sizeof(buf));
1644                 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAIL_HOME);
1645                 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
1646                         EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
1647                         error = EMAIL_ERROR_SYSTEM_FAILURE;
1648                         if (errno == 28)
1649                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
1650                         goto FINISH_OFF;
1651                 }
1652
1653                 memset(buf, 0x00, sizeof(buf));
1654                 SNPRINTF(buf, sizeof(buf), "%s%s", prefix_path, MAIL_TEMP);
1655                 if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
1656                         EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
1657                         error = EMAIL_ERROR_SYSTEM_FAILURE;
1658                         if (errno == 28)
1659                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
1660                         goto FINISH_OFF;
1661                 }
1662
1663                 _delete_temp_file(buf);
1664                 EM_SAFE_FREE(prefix_path);
1665         } else {
1666                 if (mkdir(DATA_PATH, DIRECTORY_PERMISSION) != 0) {
1667                         EM_DEBUG_EXCEPTION(" mkdir failed ");
1668                         error = EMAIL_ERROR_SYSTEM_FAILURE;
1669                         if (errno == 28)
1670                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
1671                         goto FINISH_OFF;
1672                 }
1673                 if (mkdir(EMAIL_PATH, DIRECTORY_PERMISSION) != 0) {
1674                         EM_DEBUG_EXCEPTION(" mkdir failed ");
1675                         error = EMAIL_ERROR_SYSTEM_FAILURE;
1676                         if (errno == 28)
1677                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
1678                         goto FINISH_OFF;
1679                 }
1680                 if (mkdir(MAIL_HOME, DIRECTORY_PERMISSION) != 0) {
1681                         EM_DEBUG_EXCEPTION(" mkdir failed ");
1682                         error = EMAIL_ERROR_SYSTEM_FAILURE;
1683                         if (errno == 28)
1684                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
1685                         goto FINISH_OFF;
1686                 }
1687                 if (mkdir(MAIL_TEMP, DIRECTORY_PERMISSION) != 0) {
1688                         EM_DEBUG_EXCEPTION(" mkdir failed ");
1689                         error = EMAIL_ERROR_SYSTEM_FAILURE;
1690                         if (errno == 28)
1691                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
1692                         goto FINISH_OFF;
1693                 }
1694
1695                 _delete_temp_file(MAIL_TEMP);
1696         }
1697
1698         if (!emstorage_create_table(multi_user_name, EMAIL_CREATE_DB_NORMAL, &error)) {
1699                 EM_DEBUG_EXCEPTION(" emstorage_create_table failed - %d", error);
1700                 goto FINISH_OFF;
1701         }
1702
1703 FINISH_OFF:
1704
1705
1706         if (err_code != NULL)
1707                 *err_code = error;
1708
1709         return NULL;
1710 }
1711
1712 /*  pData : a parameter which is registered when busy handler is registerd */
1713 /*  count : retry count */
1714 static int _callback_sqlite_busy_handler(void *pData, int count)
1715 {
1716         if (10 - count > 0) {
1717                 struct timespec time = {
1718                         .tv_sec = 0,
1719                         .tv_nsec = (count + 1) * 100 * 1000 * 1000
1720                 };
1721                 EM_DEBUG_LOG("Busy handler called!!: PID[%d] / CNT [%d]", getpid(), count);
1722                 nanosleep(&time, NULL);
1723                 return 1;
1724         } else {
1725                 EM_DEBUG_EXCEPTION("Busy handler will be returned SQLITE_BUSY error PID[%d] / CNT[%d]", getpid(), count);
1726                 return 0;
1727         }
1728 }
1729
1730 static int _callback_collation_utf7_sort(void *data, int length_text_a, const void *text_a,
1731                 int length_text_b, const void *text_b)
1732 {
1733         EM_DEBUG_FUNC_BEGIN();
1734         int result = 0;
1735         char *converted_string_a = NULL;
1736         char *converted_string_b = NULL;
1737
1738         EM_DEBUG_LOG_DEV("text_a : [%s]", text_a);
1739         converted_string_a = emcore_convert_mutf7_to_utf8((char *)text_a);
1740         EM_DEBUG_LOG_DEV("Converted text_a : [%s]", converted_string_a);
1741
1742         EM_DEBUG_LOG_DEV("text_b : [%s]", text_b);
1743         converted_string_b = emcore_convert_mutf7_to_utf8((char *)text_b);
1744         EM_DEBUG_LOG_DEV("Converted text_b : [%s]", converted_string_b);
1745
1746         if (converted_string_a && converted_string_b)
1747                 result = strcmp(converted_string_a, converted_string_b);
1748
1749         EM_SAFE_FREE(converted_string_a);
1750         EM_SAFE_FREE(converted_string_b);
1751
1752         EM_DEBUG_FUNC_END();
1753         return result;
1754 }
1755
1756 static int _delete_all_files_and_directories(char *db_file_path, int *err_code)
1757 {
1758         EM_DEBUG_FUNC_BEGIN();
1759
1760         int error = EMAIL_ERROR_NONE;
1761         int ret = false;
1762
1763         if (!emstorage_delete_file(db_file_path, &error)) {
1764                 if (error != EMAIL_ERROR_FILE_NOT_FOUND) {
1765                         EM_DEBUG_EXCEPTION_SEC("remove failed - %s", EMAIL_SERVICE_DB_FILE_PATH);
1766                         goto FINISH_OFF;
1767                 }
1768         }
1769
1770         if (!emstorage_delete_dir((char *)MAIL_HOME, &error)) {
1771                 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed");
1772                 goto FINISH_OFF;
1773         }
1774
1775         ret = true;
1776
1777 FINISH_OFF:
1778         if (err_code)
1779                 *err_code = error;
1780         EM_DEBUG_FUNC_END();
1781         return ret;
1782 }
1783
1784 static int _recovery_from_malformed_db_file(char *db_file_path, int *err_code)
1785 {
1786         EM_DEBUG_FUNC_BEGIN();
1787
1788         int error = EMAIL_ERROR_NONE;
1789         int ret = false;
1790
1791         /* Delete all files and directories */
1792         if (!_delete_all_files_and_directories(db_file_path, &error)) {
1793                 EM_DEBUG_EXCEPTION("_delete_all_files_and_directories failed [%d]", error);
1794                 goto FINISH_OFF;
1795         }
1796
1797         /* Delete all accounts on MyAccount */
1798
1799         /* Delete all managed connection to DB */
1800         emstorage_reset_db_handle_list();
1801
1802         ret = true;
1803
1804 FINISH_OFF:
1805         if (err_code)
1806                 *err_code = error;
1807         EM_DEBUG_FUNC_END();
1808         return ret;
1809 }
1810
1811 int _xsystem(const char *argv[])
1812 {
1813         int status = 0;
1814         pid_t pid;
1815         pid = fork();
1816
1817         switch (pid) {
1818         case -1:
1819                 perror("fork failed");
1820                 return -1;
1821         case 0:
1822                 if (execvp(argv[0], (char *const *)argv) == -1) {
1823                         perror("execute init db script");
1824                         _exit(-1);
1825                 }
1826                 _exit(2);
1827         default:
1828                 /* parent */
1829                 break;
1830         }
1831
1832         if (waitpid(pid, &status, 0) == -1) {
1833                 perror("waitpid failed");
1834                 return -1;
1835         }
1836
1837         if (WIFSIGNALED(status)) {
1838                 perror("signal");
1839                 return -1;
1840         }
1841
1842         if (!WIFEXITED(status)) {
1843                 perror("should not happen");
1844                 return -1;
1845         }
1846
1847         return WEXITSTATUS(status);
1848 }
1849
1850 #define SCRIPT_INIT_DB "/usr/bin/email-service_init_db.sh"
1851
1852 INTERNAL_FUNC int emstorage_init_db(char *multi_user_name)
1853 {
1854         EM_DEBUG_FUNC_BEGIN();
1855         int err = EMAIL_ERROR_NONE;
1856         char *prefix_path = NULL;
1857         char *output_file_path = NULL;
1858         char temp_file_path[MAX_PATH] = {0};
1859         gid_t email_gid = -1;
1860         uid_t email_uid = -1;
1861         struct group *gr;
1862         struct group buf_group;
1863         struct passwd *pw;
1864         struct passwd buf_passwd;
1865         char buf_gr[MAX_GRP_BUF_SIZE];
1866         char buf_pw[MAX_GRP_BUF_SIZE];
1867         int result = 0;
1868
1869         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1870                 err = emcore_get_container_path(multi_user_name, &prefix_path);
1871                 if (err != EMAIL_ERROR_NONE) {
1872                         if (err != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1873                                 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", err);
1874                                 goto FINISH_OFF;
1875                         }
1876                 }
1877         } else {
1878                 prefix_path = strdup("");
1879         }
1880
1881         if (err == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
1882                 err = emcore_get_canonicalize_path((char *)EMAIL_SERVICE_DB_FILE_PATH, &output_file_path);
1883                 if (err != EMAIL_ERROR_NONE) {
1884                         EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", err);
1885                         goto FINISH_OFF;
1886                 }
1887
1888                 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
1889         } else {
1890                 SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
1891         }
1892
1893         EM_DEBUG_LOG("db file path : [%s]", temp_file_path);
1894
1895         if (!g_file_test(temp_file_path, G_FILE_TEST_EXISTS)) {
1896                 int ret = true;
1897                 const char *argv_script[] = {"/bin/sh", SCRIPT_INIT_DB, NULL};
1898                 ret = _xsystem(argv_script);
1899
1900                 if (ret == -1) {
1901                         EM_DEBUG_EXCEPTION("_xsystem failed");
1902                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1903                 }
1904
1905                 result = getgrnam_r(GID, &buf_group, buf_gr, sizeof(buf_gr), &gr);
1906                 if (result == 0 && NULL != gr)
1907                         email_gid = gr->gr_gid;
1908                 else
1909                         email_gid = GID_DEFAULT;
1910
1911
1912                 result = getpwnam_r(UID, &buf_passwd, buf_pw, sizeof(buf_pw), &pw);
1913                 if (result == 0 && NULL != pw)
1914                         email_uid = pw->pw_uid;
1915                 else
1916                         email_uid = UID_DEFAULT;
1917
1918
1919                 if (chmod(EMAIL_SERVICE_DB_FILE_PATH, 0660) != 0)
1920                         EM_DEBUG_EXCEPTION("Fail to change the permission \n");
1921                 if (chmod(EMAIL_SERVICE_DB_JOURNAL_FILE_PATH, 0660) != 0)
1922                         EM_DEBUG_EXCEPTION("Fail to change the permission \n");
1923                 if (chmod(EMAIL_SQL_PATH, 0660) != 0)
1924                         EM_DEBUG_EXCEPTION("Fail to change the permission \n");
1925
1926                 ret = chown(EMAIL_SERVICE_DB_FILE_PATH, email_uid, email_gid);
1927                 if (0 != ret)
1928                         EM_DEBUG_EXCEPTION("chown() Fail[errno=%d]", errno);
1929                 ret = chown(EMAIL_SERVICE_DB_JOURNAL_FILE_PATH, email_uid, email_gid);
1930                 if (0 != ret)
1931                         EM_DEBUG_EXCEPTION("chown() Fail[errno=%d]", errno);
1932                 ret = chown(EMAIL_SQL_PATH, email_uid, email_gid);
1933                 if (0 != ret)
1934                         EM_DEBUG_EXCEPTION("chown() Fail[errno=%d]", errno);
1935
1936         }
1937
1938 FINISH_OFF:
1939
1940         EM_SAFE_FREE(prefix_path);
1941         EM_SAFE_FREE(output_file_path);
1942
1943         EM_DEBUG_FUNC_END();
1944         return err;
1945 }
1946                                         /* LCOV_EXCL_STOP */
1947 INTERNAL_FUNC int em_db_open(char *db_file_path, sqlite3 **sqlite_handle, int *err_code)
1948 {
1949         EM_DEBUG_FUNC_BEGIN();
1950         int rc = 0;
1951         int error = EMAIL_ERROR_NONE;
1952         int ret = false;
1953
1954         EM_DEBUG_LOG_DEV("*sqlite_handle[%p]", *sqlite_handle);
1955
1956         if (*sqlite_handle) { /*prevent 33351*/
1957                                         /* LCOV_EXCL_START */
1958                 EM_DEBUG_LOG_DEV(">>>>> DB Already Opened......");
1959                 if (err_code != NULL)
1960                         *err_code = error;
1961                 return true;
1962                                         /* LCOV_EXCL_STOP */
1963         }
1964
1965         EM_DEBUG_LOG("DB file path : [%s]", db_file_path);
1966
1967         /*  db open */
1968         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1969         if (SQLITE_OK != rc) {
1970                                         /* LCOV_EXCL_START */
1971                 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1972                 if (SQLITE_PERM == rc || SQLITE_CANTOPEN == rc)
1973                         error = EMAIL_ERROR_PERMISSION_DENIED;
1974                 else
1975                         error = EMAIL_ERROR_DB_FAILURE;
1976
1977                 sqlite3_close(*sqlite_handle);
1978                 *sqlite_handle = NULL;
1979
1980                 if (SQLITE_CORRUPT == rc) /* SQLITE_CORRUPT : The database disk image is malformed */ {/* Recovery DB file */
1981                         EM_DEBUG_LOG("The database disk image is malformed. Trying to remove and create database disk image and directories");
1982                         if (!_recovery_from_malformed_db_file(db_file_path, &error)) {
1983                                 EM_DEBUG_EXCEPTION("_recovery_from_malformed_db_file failed [%d]", error);
1984                                 goto FINISH_OFF;
1985                         }
1986
1987                         EM_DEBUG_LOG("Open DB again");
1988                         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_open(db_file_path, sqlite_handle), rc);
1989                         if (SQLITE_OK != rc) {
1990                                 EM_DEBUG_EXCEPTION("sqlite3_open fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
1991                                 if (SQLITE_PERM == rc)
1992                                         error = EMAIL_ERROR_PERMISSION_DENIED;
1993                                 else
1994                                         error = EMAIL_ERROR_DB_FAILURE;
1995
1996                                 sqlite3_close(*sqlite_handle);
1997                                 *sqlite_handle = NULL;
1998                                 goto FINISH_OFF; /*prevent 33351*/
1999                         }
2000                 } else
2001                         goto FINISH_OFF;
2002                                         /* LCOV_EXCL_STOP */
2003         }
2004
2005         /* register busy handler */
2006         EM_DEBUG_LOG_DEV(">>>>> Register DB Handle to busy handler: *sqlite_handle[%p]", *sqlite_handle);
2007         rc = sqlite3_busy_handler(*sqlite_handle, _callback_sqlite_busy_handler, NULL);  /*  Busy Handler registration, NULL is a parameter which will be passed to handler */
2008         if (SQLITE_OK != rc) {
2009                                         /* LCOV_EXCL_START */
2010                 EM_DEBUG_EXCEPTION("sqlite3_busy_handler fail:%d -%s", rc, sqlite3_errmsg(*sqlite_handle));
2011                 error = EMAIL_ERROR_DB_FAILURE;
2012                 sqlite3_close(*sqlite_handle);
2013                 *sqlite_handle = NULL;
2014                 goto FINISH_OFF;
2015                                         /* LCOV_EXCL_STOP */
2016         }
2017
2018         /* Register collation callback function */
2019         rc = sqlite3_create_collation(*sqlite_handle, "CONVERTUTF8", SQLITE_UTF8, NULL, _callback_collation_utf7_sort);
2020         if (SQLITE_OK != rc) {
2021                                         /* LCOV_EXCL_START */
2022                 EM_DEBUG_EXCEPTION("sqlite3_create_collation failed : [%d][%s]", rc, sqlite3_errmsg(*sqlite_handle));
2023                 error = EMAIL_ERROR_DB_FAILURE;
2024                 sqlite3_close(*sqlite_handle);
2025                 *sqlite_handle = NULL;
2026                 goto FINISH_OFF;
2027                                         /* LCOV_EXCL_STOP */
2028         }
2029
2030         ret = true;
2031
2032 FINISH_OFF:
2033         if (err_code != NULL)
2034                 *err_code = error;
2035
2036         EM_DEBUG_FUNC_END("ret [%d]", ret);
2037         return ret;
2038 }
2039
2040 INTERNAL_FUNC sqlite3* emstorage_db_open(char *multi_user_name, int *err_code)
2041 {
2042         EM_DEBUG_FUNC_BEGIN();
2043
2044         sqlite3 *_db_handle = NULL;
2045
2046         int error = EMAIL_ERROR_NONE;
2047         char *prefix_path = NULL;
2048
2049         _db_handle = emstorage_get_db_handle(multi_user_name);
2050
2051         if (_db_handle == NULL) {
2052                 char *output_file_path = NULL;
2053                 char temp_file_path[MAX_PATH] = {0};
2054
2055                 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
2056                                         /* LCOV_EXCL_START */
2057                         error = emcore_get_container_path(multi_user_name, &prefix_path);
2058                         if (error != EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION && error != EMAIL_ERROR_NONE) {
2059                                 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", error);
2060                                 goto FINISH_OFF;
2061                                         /* LCOV_EXCL_STOP*/
2062                         }
2063                 } else {
2064                         prefix_path = strdup("");
2065                 }
2066
2067                 if (error == EMAIL_ERROR_CONTAINER_NOT_INITIALIZATION) {
2068                                         /* LCOV_EXCL_START */
2069                         if ((error = emcore_get_canonicalize_path((char *)EMAIL_SERVICE_DB_FILE_PATH, &output_file_path)) != EMAIL_ERROR_NONE) {
2070                                 EM_DEBUG_EXCEPTION("emcore_get_canonicalize_path failed : [%d]", error);
2071                                 goto FINISH_OFF;
2072                         }
2073
2074                         SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s", output_file_path);
2075                         EM_SAFE_FREE(output_file_path);
2076                 } else {
2077                         SNPRINTF(temp_file_path, sizeof(temp_file_path), "%s%s", prefix_path, EMAIL_SERVICE_DB_FILE_PATH);
2078                 }
2079                                         /* LCOV_EXCL_STOP */
2080                 if (!em_db_open(temp_file_path, &_db_handle, &error)) {
2081                         EM_DEBUG_EXCEPTION("em_db_open failed[%d]", error);
2082                         goto FINISH_OFF;
2083                 }
2084
2085                 _initialize_shm_mutex(SHM_FILE_FOR_DB_LOCK, &shm_fd_for_db_lock, &mapped_for_db_lock);
2086
2087 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
2088                 _initialize_shm_mutex(SHM_FILE_FOR_MAIL_ID_LOCK, &shm_fd_for_generating_mail_id, &mapped_for_generating_mail_id);
2089 #endif /*__FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
2090
2091                 emstorage_set_db_handle(multi_user_name, _db_handle);
2092
2093                 emstorage_initialize_field_count();
2094         }
2095
2096 FINISH_OFF:
2097
2098         EM_SAFE_FREE(prefix_path);
2099
2100         if (err_code != NULL)
2101                                         /* LCOV_EXCL_START */
2102                 *err_code = error;
2103                                         /* LCOV_EXCL_STOP */
2104
2105         EM_DEBUG_FUNC_END("ret [%p]", _db_handle);
2106         return _db_handle;
2107 }
2108                                         /* LCOV_EXCL_START */
2109 INTERNAL_FUNC int emstorage_db_close(char *multi_user_name, int *err_code)
2110 {
2111         EM_DEBUG_FUNC_BEGIN();
2112 #ifdef _MULTIPLE_DB_HANDLE
2113         sqlite3 *_db_handle = emstorage_get_db_handle(multi_user_name);
2114 #endif
2115
2116         int error = EMAIL_ERROR_NONE;
2117         int ret = false;
2118
2119         if (_db_handle) {
2120                 ret = sqlite3_close(_db_handle);
2121                 if (ret != SQLITE_OK) {
2122                         EM_DEBUG_EXCEPTION(" sqlite3_close fail - %d", ret);
2123                         error = EMAIL_ERROR_DB_FAILURE;
2124                         ret = false;
2125                         goto FINISH_OFF;
2126                 }
2127 #ifdef _MULTIPLE_DB_HANDLE
2128                 emstorage_remove_db_handle();
2129 #endif
2130                 _db_handle = NULL;
2131         }
2132
2133         ret = true;
2134
2135 FINISH_OFF:
2136         if (err_code != NULL)
2137                 *err_code = error;
2138
2139         EM_DEBUG_FUNC_END("ret [%d]", ret);
2140         return ret;
2141 }
2142
2143 INTERNAL_FUNC int emstorage_open(char *multi_user_name, int *err_code)
2144 {
2145         EM_DEBUG_FUNC_BEGIN();
2146
2147         int ret = false;
2148         int error = EMAIL_ERROR_NONE;
2149         int retValue;
2150         char *prefix_path = NULL;
2151         char buf[MAX_PATH] = {0};
2152
2153         if (EM_SAFE_STRLEN(multi_user_name) <= 0) {
2154                 SNPRINTF(buf, sizeof(buf), "%s", DB_PATH);
2155         } else {
2156                 error = emcore_get_container_path(multi_user_name, &prefix_path);
2157                 if (error != EMAIL_ERROR_NONE) {
2158                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
2159                         goto FINISH_OFF;
2160                 }
2161                 SNPRINTF(buf, sizeof(buf), "%s/%s", prefix_path, DB_PATH);
2162         }
2163
2164         if (!g_file_test(buf, G_FILE_TEST_EXISTS)) {
2165                 retValue = mkdir(buf, DIRECTORY_PERMISSION);
2166
2167                 EM_DEBUG_LOG("mkdir return- %d", retValue);
2168                 EM_DEBUG_LOG("emstorage_open - before sqlite3_open - pid = %d", getpid());
2169         }
2170
2171         if (emstorage_db_open(multi_user_name, &error) == NULL) {
2172                 EM_DEBUG_EXCEPTION("emstorage_db_open failed[%d]", error);
2173                 goto FINISH_OFF;
2174         }
2175
2176         if (_open_counter++ == 0)
2177                 _emstorage_open_once(multi_user_name, &error);
2178
2179         ret = true;
2180
2181 FINISH_OFF:
2182
2183         EM_SAFE_FREE(prefix_path);
2184
2185         if (err_code != NULL)
2186                 *err_code = error;
2187
2188         EM_DEBUG_FUNC_END("ret [%d]", ret);
2189         return ret;
2190 }
2191                                         /* LCOV_EXCL_STOP */
2192 static int emstorage_get_field_count_from_create_table_query(char *input_create_table_query, int *output_field_count)
2193 {
2194         EM_DEBUG_FUNC_BEGIN("input_create_table_query[%d], output_field_count[%p]", input_create_table_query, output_field_count);
2195         int err = EMAIL_ERROR_NONE;
2196         int field_count = 0;
2197         char *pos = NULL;
2198
2199         if (input_create_table_query == NULL || output_field_count == NULL) {
2200                                         /* LCOV_EXCL_START */
2201                 err = EMAIL_ERROR_INVALID_PARAM;
2202                 goto FINISH_OFF;
2203                                         /* LCOV_EXCL_STOP */
2204         }
2205
2206         pos = input_create_table_query;
2207
2208         do {
2209                 field_count++;
2210                 if (pos == NULL || *pos == (char)0)
2211                         break;
2212                 pos += 1;
2213                 pos = strchr(pos, ',');
2214         } while (pos);
2215
2216         *output_field_count = field_count;
2217
2218         EM_DEBUG_LOG_DEV("field_count [%d]", field_count);
2219
2220 FINISH_OFF:
2221
2222         EM_DEBUG_FUNC_END("err [%d]", err);
2223         return err;
2224 }
2225
2226 INTERNAL_FUNC int emstorage_initialize_field_count()
2227 {
2228         EM_DEBUG_FUNC_BEGIN();
2229         int err = EMAIL_ERROR_NONE;
2230         int query_len = 0;
2231         char **create_table_query = NULL;
2232
2233         if (_field_count_of_table[CREATE_TABLE_MAIL_ACCOUNT_TBL] != 0) {
2234                                         /* LCOV_EXCL_START */
2235                 err = EMAIL_ERROR_ALREADY_INITIALIZED;
2236                 goto FINISH_OFF;
2237                                         /* LCOV_EXCL_STOP */
2238         }
2239
2240         err = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2241         if (err != EMAIL_ERROR_NONE) {
2242                 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", err);
2243                 goto FINISH_OFF;
2244         }
2245
2246         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]));
2247         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]));
2248         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]));
2249         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]));
2250         emstorage_get_field_count_from_create_table_query(create_table_query[CREATE_TABLE_MAIL_TBL], &(_field_count_of_table[CREATE_TABLE_MAIL_TBL]));
2251         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]));
2252         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]));
2253         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]));
2254 #ifdef __FEATURE_LOCAL_ACTIVITY__
2255         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]));
2256 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2257         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]));
2258         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]));
2259         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]));
2260 FINISH_OFF:
2261
2262         if (create_table_query) {
2263                 int i = 0;
2264                 for (i = 0; i < query_len; i++) {
2265                         if (create_table_query[i])
2266                                 EM_SAFE_FREE(create_table_query[i]);
2267                 }
2268                 EM_SAFE_FREE(create_table_query);
2269         }
2270
2271         EM_DEBUG_FUNC_END("err [%d]", err);
2272         return err;
2273 }
2274                                         /* LCOV_EXCL_START */
2275 INTERNAL_FUNC int emstorage_create_table(char *multi_user_name, emstorage_create_db_type_t type, int *err_code)
2276 {
2277         EM_DEBUG_FUNC_BEGIN();
2278
2279         int error = EMAIL_ERROR_NONE;
2280         int rc = -1, ret = false;
2281         int query_len = 0;
2282         char sql_query_string[QUERY_SIZE] = {0, };
2283         char **create_table_query = NULL;
2284
2285         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2286
2287         error = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
2288         if (error != EMAIL_ERROR_NONE) {
2289                 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
2290                 goto FINISH_OFF2;
2291         }
2292
2293         if (query_len < CREATE_TABLE_MAX) {
2294                 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
2295                 error = EMAIL_ERROR_SYSTEM_FAILURE;
2296                 goto FINISH_OFF2;
2297         }
2298
2299         EM_DEBUG_LOG("local_db_handle = %p.", local_db_handle);
2300
2301         char *sql;
2302         char **result = NULL;
2303
2304         /*  1. create mail_account_tbl */
2305         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_account_tbl';";
2306         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2307         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2308                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2309
2310         EM_DEBUG_LOG("emstorage_create_table - result[1] = %s %s", result[1], result[1]);
2311
2312         if (atoi(result[1]) < 1) {
2313                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2314                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2315                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2316
2317                 EM_DEBUG_LOG("CREATE TABLE mail_account_tbl");
2318                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2319                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2320                 if (error != EMAIL_ERROR_NONE) {
2321                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2322                         goto FINISH_OFF;
2323                 }
2324
2325                 /*  create mail_account_tbl unique index */
2326                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ACCOUNT_IDX]);
2327                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2328                 if (error != EMAIL_ERROR_NONE) {
2329                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2330                         goto FINISH_OFF;
2331                 }
2332
2333                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2334         } /*  mail_account_tbl */
2335         else if (type == EMAIL_CREATE_DB_CHECK) {
2336                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ACCOUNT_TBL], NULL, NULL, NULL), rc);
2337                 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)));
2338         }
2339
2340         sqlite3_free_table(result);
2341         result = NULL;
2342
2343         /*  2. create mail_box_tbl */
2344         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_box_tbl';";
2345
2346         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2347         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2348                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2349
2350         if (atoi(result[1]) < 1) {
2351                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2352                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2353                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2354
2355                 EM_DEBUG_LOG("CREATE TABLE mail_box_tbl");
2356
2357                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_BOX_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2358                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2359                 if (error != EMAIL_ERROR_NONE) {
2360                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2361                         goto FINISH_OFF;
2362                 }
2363
2364                 /*  create mail_local_mailbox_tbl unique index */
2365                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_BOX_IDX]);
2366                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2367                 if (error != EMAIL_ERROR_NONE) {
2368                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2369                         goto FINISH_OFF;
2370                 }
2371
2372                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2373         } /*  mail_box_tbl */
2374         else if (type == EMAIL_CREATE_DB_CHECK) {
2375                 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_BOX_TBL], NULL, NULL, NULL);
2376                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2377                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_BOX_TBL], rc, sqlite3_errmsg(local_db_handle)));
2378         }
2379         sqlite3_free_table(result);
2380         result = NULL;
2381
2382         /*  3. create mail_read_mail_uid_tbl */
2383         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_read_mail_uid_tbl';";
2384         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2385         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2386                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2387
2388
2389         if (atoi(result[1]) < 1) {
2390                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2391                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2392                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2393
2394                 EM_DEBUG_LOG("CREATE TABLE mail_read_mail_uid_tbl");
2395
2396                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2397                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2398                 if (error != EMAIL_ERROR_NONE) {
2399                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2400                         goto FINISH_OFF;
2401                 }
2402
2403                 /*  create mail_read_mail_uid_tbl unique index */
2404                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_READ_MAIL_UID_IDX]);
2405                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2406                 if (error != EMAIL_ERROR_NONE) {
2407                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2408                         goto FINISH_OFF;
2409                 }
2410
2411                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2412         } /*  mail_read_mail_uid_tbl */
2413         else if (type == EMAIL_CREATE_DB_CHECK) {
2414                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], NULL, NULL, NULL), rc);
2415                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2416                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_READ_MAIL_UID_TBL], rc, sqlite3_errmsg(local_db_handle)));
2417         }
2418         sqlite3_free_table(result);
2419         result = NULL;
2420
2421         /*  4. create mail_rule_tbl */
2422         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_rule_tbl';";
2423
2424         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2425         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2426                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2427
2428         if (atoi(result[1]) < 1) {
2429                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2430                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2431                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2432
2433                 EM_DEBUG_LOG("CREATE TABLE mail_rule_tbl");
2434
2435                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_RULE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2436                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2437                 if (error != EMAIL_ERROR_NONE) {
2438                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2439                         goto FINISH_OFF;
2440                 }
2441
2442                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2443         } /*  mail_rule_tbl */
2444         else if (type == EMAIL_CREATE_DB_CHECK) {
2445                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_RULE_TBL], NULL, NULL, NULL), rc);
2446                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2447                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_RULE_TBL], rc, sqlite3_errmsg(local_db_handle)));
2448         }
2449         sqlite3_free_table(result);
2450         result = NULL;
2451
2452         /*  5. create mail_tbl */
2453         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_tbl';";
2454         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2455         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2456                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2457
2458         if (atoi(result[1]) < 1) {
2459                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2460                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2461                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2462                 EM_DEBUG_LOG("CREATE TABLE mail_tbl");
2463
2464                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2465                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2466                 if (error != EMAIL_ERROR_NONE) {
2467                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2468                         goto FINISH_OFF;
2469                 }
2470
2471                 /*  create mail_tbl unique index */
2472                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_IDX]);
2473                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2474                 if (error != EMAIL_ERROR_NONE) {
2475                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2476                         goto FINISH_OFF;
2477                 }
2478
2479                 /*  create mail_tbl index for date_time */
2480                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_DATETIME_IDX]);
2481                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2482                 if (error != EMAIL_ERROR_NONE) {
2483                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2484                         goto FINISH_OFF;
2485                 }
2486
2487                 /*  create mail_tbl index for thread_item_count */
2488                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_THREAD_IDX]);
2489                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2490                 if (error != EMAIL_ERROR_NONE) {
2491                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2492                         goto FINISH_OFF;
2493                 }
2494
2495                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2496                 /*  just one time call */
2497                 /*      EFTSInitFTSIndex(FTS_EMAIL_IDX); */
2498         } /* mail_tbl */
2499         else if (type == EMAIL_CREATE_DB_CHECK) {
2500                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TBL], NULL, NULL, NULL), rc);
2501                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2502                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_TBL], rc, sqlite3_errmsg(local_db_handle)));
2503         }
2504         sqlite3_free_table(result);
2505         result = NULL;
2506
2507         /*  6. create mail_attachment_tbl */
2508         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_attachment_tbl';";
2509         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2510         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2511                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2512
2513         if (atoi(result[1]) < 1) {
2514                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2515                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2516                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2517
2518                 EM_DEBUG_LOG("CREATE TABLE mail_attachment_tbl");
2519
2520                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2521                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2522                 if (error != EMAIL_ERROR_NONE) {
2523                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2524                         goto FINISH_OFF;
2525                 }
2526
2527                 /*  create mail_attachment_tbl unique index */
2528                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_ATTACHMENT_IDX]);
2529                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2530                 if (error != EMAIL_ERROR_NONE) {
2531                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2532                         goto FINISH_OFF;
2533                 }
2534
2535                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2536         } /*  mail_attachment_tbl */
2537         else if (type == EMAIL_CREATE_DB_CHECK) {
2538                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], NULL, NULL, NULL), rc);
2539                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2540                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_ATTACHMENT_TBL], rc, sqlite3_errmsg(local_db_handle)));
2541         }
2542         sqlite3_free_table(result);
2543         result = NULL;
2544
2545 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
2546
2547         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_partial_body_activity_tbl';";
2548         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2549         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2550                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2551
2552         if (atoi(result[1]) < 1) {
2553
2554                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2555                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2556                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2557
2558                 EM_DEBUG_LOG("CREATE TABLE mail_partial_body_activity_tbl");
2559
2560                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2561                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2562                 if (error != EMAIL_ERROR_NONE) {
2563                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2564                         goto FINISH_OFF;
2565                 }
2566
2567                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2568         } /*  mail_rule_tbl */
2569         else if (type == EMAIL_CREATE_DB_CHECK) {
2570                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2571                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2572                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_PARTIAL_BODY_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2573         }
2574         sqlite3_free_table(result);
2575         result = NULL;
2576
2577 #endif /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
2578
2579         /*  create mail_meeting_tbl */
2580         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_meeting_tbl';";
2581         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2582         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2583                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2584
2585         if (atoi(result[1]) < 1) {
2586                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2587                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2588                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2589
2590                 EM_DEBUG_LOG("CREATE TABLE mail_meeting_tbl");
2591
2592                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_MEETING_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2593                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2594                 if (error != EMAIL_ERROR_NONE) {
2595                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2596                         goto FINISH_OFF;
2597                 }
2598
2599                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_MEETING_IDX]);
2600                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2601                 if (error != EMAIL_ERROR_NONE) {
2602                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2603                         goto FINISH_OFF;
2604                 }
2605
2606                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2607         } /*  mail_contact_sync_tbl */
2608         else if (type == EMAIL_CREATE_DB_CHECK) {
2609                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], NULL, NULL, NULL), rc);
2610                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2611                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_MEETING_TBL], rc, sqlite3_errmsg(local_db_handle)));
2612         }
2613         sqlite3_free_table(result);
2614         result = NULL;
2615                                         /* LCOV_EXCL_STOP */
2616 #ifdef __FEATURE_LOCAL_ACTIVITY__
2617
2618         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_local_activity_tbl';";
2619         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2620         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2621                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
2622
2623         if (atoi(result[1]) < 1) {
2624
2625                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2626                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; },
2627                                 ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2628
2629                 EM_DEBUG_LOG(" CREATE TABLE mail_local_activity_tbl");
2630
2631                 SNPRINTF(sql_query_string, sizeof(sql_query_string), create_table_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL]);
2632                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2633                 if (error != EMAIL_ERROR_NONE) {
2634                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2635                         goto FINISH_OFF;
2636                 }
2637
2638                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2639         } /*  mail_rule_tbl */
2640         else if (type == EMAIL_CREATE_DB_CHECK) {
2641                 rc = sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL);
2642                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2643                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2644                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_LOCAL_ACTIVITY_TBL], rc, sqlite3_errmsg(local_db_handle)));
2645         }
2646         sqlite3_free_table(result);
2647         result = NULL;
2648 #endif /*  __FEATURE_LOCAL_ACTIVITY__ */
2649         /*  create mail_certificate_tbl */
2650                                         /* LCOV_EXCL_START */
2651         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_certificate_tbl';";
2652         /*  rc = sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL);   */
2653         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2654         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)));
2655
2656         if (atoi(result[1]) < 1) {
2657                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2658                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2659
2660                 EM_DEBUG_LOG("CREATE TABLE mail_certificate_tbl");
2661
2662                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], sizeof(sql_query_string)-1); /*prevent 21984*/
2663                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2664                 if (error != EMAIL_ERROR_NONE) {
2665                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2666                         goto FINISH_OFF;
2667                 }
2668
2669                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2670         } /*  mail_contact_sync_tbl */
2671         else if (type == EMAIL_CREATE_DB_CHECK) {
2672                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_CERTIFICATE_TBL], NULL, NULL, NULL), rc);
2673                 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)));
2674         }
2675
2676         sqlite3_free_table(result);
2677         result = NULL;
2678
2679         /*  create mail_task_tbl */
2680         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_task_tbl';";
2681         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2682         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)));
2683
2684         if (atoi(result[1]) < 1) {
2685                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2686                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2687
2688                 EM_DEBUG_LOG("CREATE TABLE mail_task_tbl");
2689
2690                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TASK_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2691                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2692                 if (error != EMAIL_ERROR_NONE) {
2693                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2694                         goto FINISH_OFF;
2695                 }
2696
2697                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s", create_table_query[CREATE_TABLE_MAIL_TASK_IDX]);
2698                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2699                 if (error != EMAIL_ERROR_NONE) {
2700                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2701                         goto FINISH_OFF;
2702                 }
2703
2704                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2705         } /*  mail_task_tbl */
2706         else if (type == EMAIL_CREATE_DB_CHECK) {
2707                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TASK_TBL], NULL, NULL, NULL), rc);
2708                 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)));
2709         }
2710
2711         sqlite3_free_table(result);
2712         result = NULL;
2713
2714 #ifdef __FEATURE_BODY_SEARCH__
2715         /*  create mail_text_tbl */
2716         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_text_tbl';";
2717         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2718         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)));
2719
2720         if (atoi(result[1]) < 1) {
2721                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2722                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2723
2724                 EM_DEBUG_LOG("CREATE TABLE mail_text_tbl");
2725
2726                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_TEXT_TBL], sizeof(sql_query_string)-1); /*prevent 21984 */
2727                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2728                 if (error != EMAIL_ERROR_NONE) {
2729                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2730                         goto FINISH_OFF;
2731                 }
2732
2733                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2734         } /*  mail_text_tbl */
2735         else if (type == EMAIL_CREATE_DB_CHECK) {
2736                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_TEXT_TBL], NULL, NULL, NULL), rc);
2737                 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)));
2738         }
2739
2740         sqlite3_free_table(result);
2741         result = NULL;
2742
2743 #endif
2744
2745 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
2746         /*  create mail_auto_download_activity_tbl */
2747         sql = "SELECT count(name) FROM sqlite_master WHERE name='mail_auto_download_activity_tbl';";
2748         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
2749         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)));
2750
2751         if (atoi(result[1]) < 1) {
2752                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
2753                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
2754
2755                 EM_DEBUG_LOG("CREATE TABLE mail_auto_download_activity_tbl");
2756
2757                 EM_SAFE_STRNCPY(sql_query_string, create_table_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], sizeof(sql_query_string)-1);
2758                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
2759                 if (error != EMAIL_ERROR_NONE) {
2760                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
2761                         goto FINISH_OFF;
2762                 }
2763
2764                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2765         } /*  mail_auto_download_activity_tbl */
2766         else if (type == EMAIL_CREATE_DB_CHECK) {
2767                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, g_test_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], NULL, NULL, NULL), rc);
2768
2769                 if(g_test_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL] != NULL) {
2770                         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, \
2771                                 ("SQL(%s) exec fail:%d -%s", g_test_query[CREATE_TABLE_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL], \
2772                                          rc, sqlite3_errmsg(local_db_handle)));
2773                 }
2774         }
2775
2776         sqlite3_free_table(result);
2777         result = NULL;
2778 #endif
2779
2780
2781         ret = true;
2782
2783 FINISH_OFF:
2784
2785         if (result) sqlite3_free_table(result);
2786
2787         if (ret == true) {
2788                 EMSTORAGE_PROTECTED_FUNC_CALL(
2789                         sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
2790         } else {
2791                 EMSTORAGE_PROTECTED_FUNC_CALL(
2792                         sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
2793         }
2794
2795 FINISH_OFF2:
2796         if (create_table_query) {
2797                 int i = 0;
2798                 for (i = 0; i < query_len; i++) {
2799                         if (create_table_query[i])
2800                                 EM_SAFE_FREE(create_table_query[i]);
2801                 }
2802                 EM_SAFE_FREE(create_table_query);
2803         }
2804
2805         if (err_code != NULL)
2806                 *err_code = error;
2807
2808         EM_DEBUG_FUNC_END("ret [%d]", ret);
2809         return ret;
2810 }
2811
2812 /* Query series --------------------------------------------------------------*/
2813 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)
2814 {
2815         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);
2816         int rc = -1;
2817         int query_size = 0;
2818         int error = EMAIL_ERROR_NONE;
2819         DB_STMT hStmt = NULL;
2820         char *sql_query_string = NULL;
2821         char **result;
2822         sqlite3 *local_db_handle = NULL;
2823
2824         if (!input_conditional_clause || (!output_total_mail_count && !output_unseen_mail_count)) {
2825                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2826                 return EMAIL_ERROR_INVALID_PARAM;
2827         }
2828
2829         query_size = EM_SAFE_STRLEN(input_conditional_clause) + QUERY_SIZE;
2830         sql_query_string = em_malloc(query_size);
2831         if (sql_query_string == NULL) {
2832                 EM_DEBUG_EXCEPTION("em_mallocfailed");
2833                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2834                 goto FINISH_OFF;
2835         }
2836
2837         local_db_handle = emstorage_get_db_connection(multi_user_name);
2838
2839         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2840
2841         SNPRINTF(sql_query_string, query_size, "SELECT COUNT(*) FROM mail_tbl");
2842         EM_SAFE_STRNCAT(sql_query_string, (char*)input_conditional_clause, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
2843
2844         if (output_total_mail_count) {
2845                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
2846                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2847                                 ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
2848
2849                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
2850                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
2851                                 ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
2852                 _get_stmt_field_data_int(hStmt, output_total_mail_count, 0);
2853         }
2854
2855         if (output_unseen_mail_count) {
2856                 EM_SAFE_STRNCAT(sql_query_string, " AND flags_seen_field = 0 ", query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
2857
2858                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
2859                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2860                                 ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
2861
2862                 *output_unseen_mail_count = atoi(result[1]);
2863                 sqlite3_free_table(result);
2864         }
2865
2866 FINISH_OFF:
2867
2868         if (hStmt != NULL) {
2869                 rc = sqlite3_finalize(hStmt);
2870                 if (rc != SQLITE_OK) {
2871                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
2872                         error = EMAIL_ERROR_DB_FAILURE;
2873                 }
2874         }
2875
2876         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2877
2878         EM_SAFE_FREE(sql_query_string);
2879
2880         EM_DEBUG_FUNC_END("error [%d]", error);
2881         return error;
2882 }
2883
2884 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)
2885 {
2886         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);
2887
2888         int      i = 0;
2889         int      count = 0;
2890         int      rc = -1;
2891         int      cur_query = 0;
2892         int      col_index;
2893         int      query_size = 0;
2894         int      error = EMAIL_ERROR_NONE;
2895         int     *result_mail_id_list = NULL;
2896         char   **result = NULL;
2897         char     *sql_query_string = NULL;
2898         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2899
2900         EM_IF_NULL_RETURN_VALUE(input_conditional_clause, EMAIL_ERROR_INVALID_PARAM);
2901         EM_IF_NULL_RETURN_VALUE(output_mail_id_list, EMAIL_ERROR_INVALID_PARAM);
2902         EM_IF_NULL_RETURN_VALUE(output_mail_id_count, EMAIL_ERROR_INVALID_PARAM);
2903
2904         query_size = strlen(input_conditional_clause) + strlen("SELECT mail_id FROM mail_tbl ") + 10;  // 10 is extra space
2905         sql_query_string = em_malloc(query_size);
2906         if (sql_query_string == NULL) {
2907                 EM_DEBUG_EXCEPTION("em_mallocfailed");
2908                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2909                 goto FINISH_OFF;
2910         }
2911
2912         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
2913
2914         /* Composing query */
2915         SNPRINTF_OFFSET(sql_query_string, cur_query, query_size, "SELECT mail_id FROM mail_tbl ");
2916         EM_SAFE_STRNCAT(sql_query_string, (char*)input_conditional_clause, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
2917
2918         EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
2919
2920         /* Performing query */
2921         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
2922         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
2923                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
2924
2925         col_index = 1;
2926
2927         /*  to get mail list */
2928         if (count == 0) {
2929                 EM_DEBUG_LOG("No mail found...");
2930                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
2931                 goto FINISH_OFF;
2932         }
2933
2934         EM_DEBUG_LOG("There are [%d] mails.", count);
2935
2936         if (!(result_mail_id_list = (int*)em_malloc(sizeof(int) * count))) {
2937                 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
2938                 error = EMAIL_ERROR_OUT_OF_MEMORY;
2939                 goto FINISH_OFF;
2940         }
2941
2942         EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
2943
2944         for (i = 0; i < count; i++)
2945                 _get_table_field_data_int(result, result_mail_id_list + i, col_index++);
2946
2947         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >> ", count);
2948
2949         *output_mail_id_list  = result_mail_id_list;
2950         *output_mail_id_count = count;
2951
2952 FINISH_OFF:
2953
2954         if (result)
2955                 sqlite3_free_table(result);
2956
2957         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
2958
2959         EM_SAFE_FREE(sql_query_string);
2960
2961         if (error != EMAIL_ERROR_NONE)
2962                 EM_SAFE_FREE(result_mail_id_list);
2963
2964         EM_DEBUG_FUNC_END("error [%d]", error);
2965         return error;
2966 }
2967
2968 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)
2969 {
2970         EM_DEBUG_FUNC_BEGIN();
2971         EM_PROFILE_BEGIN(emstorage_query_mail_list_func);
2972
2973         int i = 0, count = 0, rc = -1, to_get_count = (result_mail_list) ? 0 : 1;
2974         int sql_query_string_length = 0;
2975         int local_inline_content_count = 0, local_attachment_count = 0;
2976         int cur_query = 0, base_count = 0, col_index;
2977         int ret = false, error = EMAIL_ERROR_NONE;
2978         char *date_time_string = NULL;
2979         char **result = NULL;
2980         char *field_mail_id = "mail_id";
2981         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 ";
2982         char *select_query_form = "SELECT %s FROM mail_tbl ";
2983         char *target_field = NULL;
2984         char *sql_query_string = NULL;
2985         email_mail_list_item_t *mail_list_item_from_tbl = NULL;
2986         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
2987
2988         EM_IF_NULL_RETURN_VALUE(conditional_clause, false);
2989         EM_IF_NULL_RETURN_VALUE(result_count, false);
2990
2991         EMSTORAGE_START_READ_TRANSACTION(transaction);
2992
2993         /*  select clause */
2994         if (to_get_count) /*  count only */
2995                 target_field = field_mail_id;
2996         else /* mail list in plain form */
2997                 target_field = field_all;
2998
2999         sql_query_string_length = EM_SAFE_STRLEN(select_query_form) + EM_SAFE_STRLEN(target_field) + EM_SAFE_STRLEN(conditional_clause);
3000
3001         if (sql_query_string_length)
3002                 sql_query_string = em_malloc(sql_query_string_length);
3003
3004         if (sql_query_string == NULL) {
3005                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
3006                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3007                 goto FINISH_OFF;
3008         }
3009
3010         cur_query += SNPRINTF_OFFSET(sql_query_string, cur_query, sql_query_string_length, select_query_form, target_field);
3011
3012         strncat(sql_query_string, conditional_clause, sql_query_string_length - cur_query);
3013
3014         EM_DEBUG_LOG_DEV("query[%s]", sql_query_string);
3015
3016         /*  performing query */
3017         EM_PROFILE_BEGIN(emstorage_query_mail_list_performing_query);
3018         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3019         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3020                         ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
3021         EM_PROFILE_END(emstorage_query_mail_list_performing_query);
3022
3023         if (!base_count)
3024                 base_count = ({
3025                                 int i = 0;
3026                                 char *tmp = NULL;
3027                                 for (tmp = field_all; tmp && *(tmp + 1); tmp = index(tmp + 1, ',')) i++ ;
3028                                 i;
3029                                 });
3030
3031         col_index = base_count;
3032
3033         EM_DEBUG_LOG_DEV("base_count [%d]", base_count);
3034
3035         if (to_get_count) {
3036                 /*  to get count */
3037                 if (!count) {
3038                         EM_DEBUG_LOG_DEV("No mail found...");
3039                         ret = false;
3040                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
3041                         goto FINISH_OFF;
3042                 }
3043                 EM_DEBUG_LOG_DEV("There are [%d] mails.", count);
3044         } else {
3045                 /*  to get mail list */
3046                 if (!count) {
3047                         EM_DEBUG_LOG_DEV("No mail found...");
3048                         ret = false;
3049                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
3050                         goto FINISH_OFF;
3051                 }
3052
3053                 EM_DEBUG_LOG_DEV("There are [%d] mails.", count);
3054                 if (!(mail_list_item_from_tbl = (email_mail_list_item_t*)em_malloc(sizeof(email_mail_list_item_t) * count))) {
3055                         EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
3056                         error = EMAIL_ERROR_OUT_OF_MEMORY;
3057                         goto FINISH_OFF;
3058                 }
3059
3060                 EM_PROFILE_BEGIN(emstorage_query_mail_list_loop);
3061                 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >> ");
3062                 for (i = 0; i < count; i++) {
3063                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_id), col_index++);
3064                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].account_id), col_index++);
3065                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mailbox_id), col_index++);
3066                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].mailbox_type), col_index++);
3067                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].full_address_from, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
3068                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_sender, MAX_EMAIL_ADDRESS_LENGTH, 1, col_index++);
3069                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].email_address_recipient, STRING_LENGTH_FOR_DISPLAY,  1, col_index++);
3070                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].subject, STRING_LENGTH_FOR_DISPLAY, 1, col_index++);
3071                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].body_download_status), col_index++);
3072                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].mail_size), col_index++);
3073                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_seen_field), col_index++);
3074                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_deleted_field), col_index++);
3075                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_flagged_field), col_index++);
3076                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_answered_field), col_index++);
3077                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_recent_field), col_index++);
3078                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_draft_field), col_index++);
3079                         _get_table_field_data_char(result, &(mail_list_item_from_tbl[i].flags_forwarded_field), col_index++);
3080                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].DRM_status), col_index++);
3081                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].priority), col_index++);
3082                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].save_status), col_index++);
3083                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].lock_status), col_index++);
3084                         _get_table_field_data_int(result, &local_attachment_count, col_index++);
3085                         _get_table_field_data_int(result, &local_inline_content_count, col_index++);
3086                         _get_table_field_data_time_t(result, &(mail_list_item_from_tbl[i].date_time), col_index++);
3087                         _get_table_field_data_string_without_allocation(result, mail_list_item_from_tbl[i].preview_text, MAX_PREVIEW_TEXT_LENGTH, 1, col_index++);
3088                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_id), col_index++);
3089                         _get_table_field_data_int(result, &(mail_list_item_from_tbl[i].thread_item_count), col_index++);
3090                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].meeting_request_status), col_index++);
3091                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].message_class), col_index++);
3092                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].smime_type), col_index++);
3093                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].scheduled_sending_time), col_index++);
3094                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].remaining_resend_times), col_index++);
3095                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].tag_id), col_index++);
3096                         _get_table_field_data_int(result, (int*)&(mail_list_item_from_tbl[i].eas_data_length), col_index++);
3097                         _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++);
3098
3099                         mail_list_item_from_tbl[i].attachment_count = (local_attachment_count > 0) ? 1 : 0;
3100                 }
3101                 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >> ", count);
3102                 EM_PROFILE_END(emstorage_query_mail_list_loop);
3103         }
3104
3105         ret = true;
3106
3107 FINISH_OFF:
3108         EM_DEBUG_LOG("MAIL_COUNT [%d]", count);
3109
3110         if (result)
3111                 sqlite3_free_table(result);
3112
3113         if (to_get_count)
3114                 *result_count = count;
3115         else {
3116                 if (ret == true) {
3117                         if (result_mail_list)
3118                                 *result_mail_list = mail_list_item_from_tbl;
3119                         else
3120                                 EM_SAFE_FREE(mail_list_item_from_tbl);
3121                         *result_count = count;
3122                 } else
3123                         EM_SAFE_FREE(mail_list_item_from_tbl);
3124         }
3125
3126         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3127
3128         //      sqlite3_db_release_memory(local_db_handle);
3129
3130         EM_SAFE_FREE(sql_query_string);
3131         EM_SAFE_FREE(date_time_string);
3132
3133         if (err_code != NULL)
3134                 *err_code = error;
3135
3136         EM_PROFILE_END(emstorage_query_mail_list_func);
3137         EM_DEBUG_FUNC_END("ret [%d]", ret);
3138         return ret;
3139 }
3140
3141                                         /* LCOV_EXCL_STOP */
3142 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)
3143 {
3144         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);
3145
3146         if (!conditional_clause || !result_mail_tbl || !result_count) {
3147                                         /* LCOV_EXCL_START */
3148                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: [%p] [%p] [%p]", conditional_clause, result_mail_tbl, result_mail_tbl);
3149                 if (err_code != NULL)
3150                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3151                 return false;
3152                                         /* LCOV_EXCL_STOP */
3153         }
3154
3155         int i, col_index = FIELD_COUNT_OF_MAIL_TBL, rc, ret = false, count;
3156         int error = EMAIL_ERROR_NONE;
3157         char **result = NULL, sql_query_string[QUERY_SIZE] = {0, };
3158         emstorage_mail_tbl_t* p_data_tbl = NULL;
3159         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3160
3161         EMSTORAGE_START_READ_TRANSACTION(transaction);
3162
3163         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl %s", conditional_clause);
3164
3165         EM_DEBUG_LOG_DEV("Query[%s]", sql_query_string);
3166
3167         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3168         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3169                         ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string));
3170
3171         if (!count) {
3172                                         /* LCOV_EXCL_START */
3173                 EM_DEBUG_LOG("No mail found...");
3174                 ret = false;
3175                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
3176                 goto FINISH_OFF;
3177                                         /* LCOV_EXCL_STOP */
3178         }
3179
3180         EM_DEBUG_LOG("There are [%d] mails.", count);
3181         if (!(p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * count))) {
3182                 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
3183                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3184                 goto FINISH_OFF;
3185         }
3186
3187         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >> ");
3188         for (i = 0; i < count; i++) {
3189                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
3190                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3191                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3192                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_type), col_index++);
3193                 _get_table_field_data_string(result, &(p_data_tbl[i].subject), 1, col_index++);
3194                 _get_table_field_data_time_t (result, &(p_data_tbl[i].date_time), col_index++);
3195                 _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
3196                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
3197                 _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
3198                 _get_table_field_data_string(result, &(p_data_tbl[i].message_id), 0, col_index++);
3199                 _get_table_field_data_int(result, &(p_data_tbl[i].reference_mail_id), col_index++);
3200                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_from), 1, col_index++);
3201                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_reply), 1, col_index++);
3202                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_to), 1, col_index++);
3203                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_cc), 1, col_index++);
3204                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_bcc), 1, col_index++);
3205                 _get_table_field_data_string(result, &(p_data_tbl[i].full_address_return), 1, col_index++);
3206                 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_sender), 1, col_index++);
3207                 _get_table_field_data_string(result, &(p_data_tbl[i].email_address_recipient), 1, col_index++);
3208                 _get_table_field_data_string(result, &(p_data_tbl[i].alias_sender), 1, col_index++);
3209                 _get_table_field_data_string(result, &(p_data_tbl[i].alias_recipient), 1, col_index++);
3210                 _get_table_field_data_int(result, &(p_data_tbl[i].body_download_status), col_index++);
3211                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
3212                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
3213                 _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
3214                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_size), col_index++);
3215                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_seen_field), col_index++);
3216                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_deleted_field), col_index++);
3217                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_flagged_field), col_index++);
3218                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_answered_field), col_index++);
3219                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_recent_field), col_index++);
3220                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_draft_field), col_index++);
3221                 _get_table_field_data_char  (result, &(p_data_tbl[i].flags_forwarded_field), col_index++);
3222                 _get_table_field_data_int(result, &(p_data_tbl[i].DRM_status), col_index++);
3223                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].priority), col_index++);
3224                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
3225                 _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
3226                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].report_status), col_index++);
3227                 _get_table_field_data_int(result, &(p_data_tbl[i].attachment_count), col_index++);
3228                 _get_table_field_data_int(result, &(p_data_tbl[i].inline_content_count), col_index++);
3229                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
3230                 _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
3231                 _get_table_field_data_string(result, &(p_data_tbl[i].preview_text), 1, col_index++);
3232                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].meeting_request_status), col_index++);
3233                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].message_class), col_index++);
3234                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].digest_type), col_index++);
3235                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].smime_type), col_index++);
3236                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].scheduled_sending_time), col_index++);
3237                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].remaining_resend_times), col_index++);
3238                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].tag_id), col_index++);
3239                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].replied_time), col_index++);
3240                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].forwarded_time), col_index++);
3241                 _get_table_field_data_string(result, &(p_data_tbl[i].default_charset), 0, col_index++);
3242                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3243                 _get_table_field_data_blob(result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3244                 _get_table_field_data_string(result, &(p_data_tbl[i].user_name), 0, col_index++);
3245         }
3246
3247         ret = true;
3248
3249 FINISH_OFF:
3250         if (result)
3251                 sqlite3_free_table(result);
3252
3253         if (ret) {
3254                 *result_mail_tbl = p_data_tbl;
3255                 *result_count = count;
3256         } else {
3257                                         /* LCOV_EXCL_START */
3258                 *result_mail_tbl = NULL;
3259                 *result_count = 0;
3260                                         /* LCOV_EXCL_STOP */
3261         }
3262
3263         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3264
3265         //      sqlite3_db_release_memory(local_db_handle);
3266
3267         if (err_code != NULL)
3268                 *err_code = error;
3269
3270         EM_DEBUG_FUNC_END("ret [%d]", ret);
3271         return ret;
3272 }
3273
3274 #ifdef __FEATURE_BODY_SEARCH__
3275                                         /* LCOV_EXCL_START */
3276 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)
3277 {
3278         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);
3279
3280         if (!conditional_clause || !result_mail_text_tbl || !result_count) { /*prevent 50930*/
3281                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3282                 if (err_code != NULL)
3283                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3284                 return false;
3285         }
3286
3287         int i = 0;
3288         int rc = 0;
3289         int ret = false;
3290         int count = 0;
3291         int col_index = FIELD_COUNT_OF_MAIL_TEXT_TBL;
3292         int error = EMAIL_ERROR_NONE;
3293         char **result = NULL;
3294         char sql_query_string[QUERY_SIZE] = {0, };
3295         emstorage_mail_text_tbl_t* p_data_tbl = NULL;
3296         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3297
3298         EMSTORAGE_START_READ_TRANSACTION(transaction);
3299
3300         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_text_tbl %s", conditional_clause);
3301
3302         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
3303
3304         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
3305         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3306                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3307
3308         if (!count) {
3309                 EM_DEBUG_LOG("No mail found...");
3310                 ret = false;
3311                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
3312                 goto FINISH_OFF;
3313         }
3314
3315         EM_DEBUG_LOG("There are [%d] mails.", count);
3316         if (!(p_data_tbl = (emstorage_mail_text_tbl_t *)em_malloc(sizeof(emstorage_mail_text_tbl_t) * count))) {
3317                 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_text_tbl_t failed...");
3318                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3319                 goto FINISH_OFF;
3320         }
3321
3322         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
3323         for (i = 0; i < count; i++) {
3324                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
3325                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3326                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3327                 _get_table_field_data_string(result, &(p_data_tbl[i].body_text), 0, col_index++);
3328         }
3329
3330         ret = true;
3331
3332 FINISH_OFF:
3333         if (result)
3334                 sqlite3_free_table(result);
3335
3336         if (ret == true) {
3337                 *result_mail_text_tbl = p_data_tbl;
3338                 *result_count = count;
3339         } else
3340                 EM_SAFE_FREE(p_data_tbl);
3341
3342         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3343
3344         //      sqlite3_db_release_memory(local_db_handle);
3345
3346         if (err_code != NULL)
3347                 *err_code = error;
3348                                         /* LCOV_EXCL_STOP */
3349         EM_DEBUG_FUNC_END("ret [%d]", ret);
3350         return ret;
3351 }
3352 #endif
3353
3354 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)
3355 {
3356         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);
3357
3358         int i = 0;
3359         int rc;
3360         int count = 0;
3361         int col_index = 0;
3362         int error = EMAIL_ERROR_NONE;
3363         int read_count = 0;
3364         int total_count = 0;
3365         char **result;
3366         char sql_query_string[QUERY_SIZE] = {0, };
3367         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 ";
3368         emstorage_mailbox_tbl_t* p_data_tbl = NULL;
3369         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3370
3371         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
3372
3373         if (input_get_mail_count == 0) {        
3374                                         /* LCOV_EXCL_START */
3375         /* without mail count */
3376                 col_index = 15;
3377                 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);
3378                                         /* LCOV_EXCL_STOP */
3379         } else {        /* with read count and total count */
3380                 col_index = 17;
3381                 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);
3382         }
3383
3384         EM_DEBUG_LOG_DEV("query[%s]", sql_query_string);
3385
3386         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
3387         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3388                         ("sqlite3_get_table failed [%d] [%s]", rc, sql_query_string))
3389
3390                 EM_DEBUG_LOG_DEV("result count [%d]", count);
3391
3392         if (count == 0) {
3393                                         /* LCOV_EXCL_START */
3394                 EM_DEBUG_LOG_SEC("Can't find mailbox query[%s]", sql_query_string);
3395                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
3396                 goto FINISH_OFF;
3397                                         /* LCOV_EXCL_STOP */
3398         }
3399
3400         if ((p_data_tbl = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t) * count)) == NULL) {
3401                                         /* LCOV_EXCL_START */
3402                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
3403                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3404                 goto FINISH_OFF;
3405                                         /* LCOV_EXCL_STOP */
3406         }
3407
3408         for (i = 0; i < count; i++) {
3409                 _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
3410                 _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
3411                 _get_table_field_data_int(result, &(p_data_tbl[i].local_yn), col_index++);
3412                 _get_table_field_data_string(result, &(p_data_tbl[i].mailbox_name), 0, col_index++);
3413                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].mailbox_type), col_index++);
3414                 _get_table_field_data_string(result, &(p_data_tbl[i].alias), 0, col_index++);
3415                 _get_table_field_data_int(result, &(p_data_tbl[i].deleted_flag), col_index++);
3416                 _get_table_field_data_int(result, &(p_data_tbl[i].modifiable_yn), col_index++);
3417                 _get_table_field_data_int(result, &(p_data_tbl[i].total_mail_count_on_server), col_index++);
3418                 _get_table_field_data_int(result, &(p_data_tbl[i].has_archived_mails), col_index++);
3419                 _get_table_field_data_int(result, &(p_data_tbl[i].mail_slot_size), col_index++);
3420                 _get_table_field_data_int(result, &(p_data_tbl[i].no_select), col_index++);
3421                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].last_sync_time), col_index++);
3422                 _get_table_field_data_int(result, (int*)&(p_data_tbl[i].eas_data_length), col_index++);
3423                 _get_table_field_data_blob(result, (void**)&(p_data_tbl[i].eas_data), p_data_tbl[i].eas_data_length, col_index++);
3424
3425                 if (input_get_mail_count == 1) {
3426                         _get_table_field_data_int(result, &(total_count), col_index++);
3427                         p_data_tbl[i].total_mail_count_on_local = total_count;
3428                         _get_table_field_data_int(result, &(read_count), col_index++);
3429                         p_data_tbl[i].unread_count = total_count - read_count;                  /*  return unread count, NOT  */
3430                 }
3431         }
3432
3433
3434 FINISH_OFF:
3435         if (result)
3436                 sqlite3_free_table(result);
3437
3438         if (error == EMAIL_ERROR_NONE) {
3439                 *output_mailbox_list = p_data_tbl;
3440                 *output_mailbox_count = count;
3441                 EM_DEBUG_LOG("Mailbox Count [%d]", count);
3442         }
3443
3444         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
3445
3446         //      sqlite3_db_release_memory(local_db_handle);
3447
3448         EM_DEBUG_FUNC_END("error [%d]", error);
3449         return error;
3450 }
3451
3452 /* Query series --------------------------------------------------------------*/
3453                                         /* LCOV_EXCL_START */
3454 INTERNAL_FUNC int emstorage_check_duplicated_account(char *multi_user_name, email_account_t* account, int transaction, int *err_code)
3455 {
3456         EM_DEBUG_FUNC_BEGIN();
3457         int rc = -1, ret = false;
3458         int error = EMAIL_ERROR_NONE;
3459         char **result;
3460         int count;
3461         char sql_query_string[QUERY_SIZE] = {0, };
3462         sqlite3 *local_db_handle = NULL;
3463
3464         if (account == NULL || account->user_email_address == NULL || account->incoming_server_user_name == NULL || account->incoming_server_address == NULL ||
3465                         account->outgoing_server_user_name == NULL || account->outgoing_server_address == NULL) {
3466                 if (err_code != NULL)
3467                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3468                 return false;
3469         }
3470
3471         local_db_handle = emstorage_get_db_connection(multi_user_name);
3472
3473         EMSTORAGE_START_READ_TRANSACTION(transaction);
3474
3475         SNPRINTF(sql_query_string, sizeof(sql_query_string),
3476                         "SELECT COUNT(*) FROM mail_account_tbl "
3477                         " WHERE "
3478                         " user_email_address = '%s' AND "
3479                         " incoming_server_user_name = '%s' AND "
3480                         " incoming_server_type = %d AND "
3481                         " incoming_server_address = '%s' AND "
3482                         " outgoing_server_user_name = '%s' AND "
3483                         " outgoing_server_type = %d AND "
3484                         " outgoing_server_address = '%s'; ",
3485                         account->user_email_address,
3486                         account->incoming_server_user_name, account->incoming_server_type, account->incoming_server_address,
3487                         account->outgoing_server_user_name, account->outgoing_server_type, account->outgoing_server_address
3488                         );
3489         EM_DEBUG_LOG_SEC("Query[%s]", sql_query_string);
3490         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
3491         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3492                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3493
3494         count = atoi(result[1]);
3495         sqlite3_free_table(result);
3496
3497         EM_DEBUG_LOG("Count of Duplicated Account Information: [%d]", count);
3498
3499         if (count == 0) {       /*  not duplicated account */
3500                 ret = true;
3501                 EM_DEBUG_LOG_SEC("NOT duplicated account: user_email_address[%s]", account->user_email_address);
3502         } else {        /*  duplicated account */
3503                 ret = false;
3504                 EM_DEBUG_LOG_SEC("The same account already exists. Duplicated account: user_email_address[%s]", account->user_email_address);
3505                 error = EMAIL_ERROR_ALREADY_EXISTS;
3506         }
3507
3508 FINISH_OFF:
3509
3510         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3511
3512         if (err_code != NULL)
3513                 *err_code = error;
3514
3515         EM_DEBUG_FUNC_END("ret [%d]", ret);
3516         return ret;
3517
3518 }
3519
3520 INTERNAL_FUNC int emstorage_get_account_count(char *multi_user_name, int *count, int transaction, int *err_code)
3521 {
3522         EM_DEBUG_FUNC_BEGIN();
3523
3524         if (!count) {
3525                 if (err_code != NULL)
3526                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3527                 return false;
3528         }
3529
3530         int rc = -1, ret = false;
3531         int error = EMAIL_ERROR_NONE;
3532
3533         DB_STMT hStmt = NULL;
3534         char sql_query_string[QUERY_SIZE] = {0, };
3535
3536
3537         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
3538         EMSTORAGE_START_READ_TRANSACTION(transaction);
3539         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_account_tbl");
3540         EM_DEBUG_LOG_SEC("SQL STMT [%s]", sql_query_string);
3541         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string,
3542                                 EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3543         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
3544         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3545                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3546
3547         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3548         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3549                         ("sqlite3_step fail:%d", rc));
3550
3551         *count = sqlite3_column_int(hStmt, 0);
3552
3553         ret = true;
3554
3555 FINISH_OFF:
3556
3557         if (hStmt != NULL) {
3558                 rc = sqlite3_finalize(hStmt);
3559                 hStmt = NULL;
3560                 if (rc != SQLITE_OK) {
3561                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
3562                         error = EMAIL_ERROR_DB_FAILURE;
3563                 }
3564         }
3565
3566         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3567
3568         if (err_code != NULL)
3569                 *err_code = error;
3570
3571         EM_DEBUG_FUNC_END("ret [%d]", ret);
3572         return ret;
3573 }
3574
3575 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)
3576 {
3577         EM_DEBUG_FUNC_BEGIN();
3578
3579         int i = 0, count = 0, rc = -1, ret = false;
3580         int field_index = 0;
3581         int sql_len = 0;
3582         int error = EMAIL_ERROR_NONE;
3583         emstorage_account_tbl_t *p_data_tbl = NULL;
3584         char sql_query_string[QUERY_SIZE] = {0, };
3585         char *sql = "SELECT count(*) FROM mail_account_tbl;";
3586         char **result;
3587         sqlite3 *local_db_handle = NULL;
3588         DB_STMT hStmt = NULL;
3589
3590         if (!select_num || !account_list) {
3591                 EM_DEBUG_EXCEPTION("select_num[%p], account_list[%p]", select_num, account_list);
3592                 if (err_code != NULL)
3593                         *err_code = EMAIL_ERROR_INVALID_PARAM;
3594                 return false;
3595         }
3596
3597         local_db_handle = emstorage_get_db_connection(multi_user_name);
3598
3599         EMSTORAGE_START_READ_TRANSACTION(transaction);
3600
3601         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
3602         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
3603                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
3604
3605         count = atoi(result[1]);
3606         sqlite3_free_table(result);
3607
3608         EM_DEBUG_LOG_DEV("count = %d", rc);
3609
3610         if (count <= 0) {
3611                 EM_DEBUG_LOG("no account found...");
3612                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3613                 ret = true;
3614                 goto FINISH_OFF;
3615         }
3616         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
3617         sql_len = EM_SAFE_STRLEN(sql_query_string);
3618
3619         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
3620                         "account_id, "
3621                         "account_name, "
3622                         "logo_icon_path, "
3623                         "user_data, "
3624                         "user_data_length, "
3625                         "account_svc_id, "
3626                         "sync_status, "
3627                         "sync_disabled, "
3628                         "default_mail_slot_size, "
3629                         "roaming_option, "
3630                         "color_label, "
3631                         "user_display_name, "
3632                         "user_email_address, "
3633                         "reply_to_address, "
3634                         "return_address, "
3635                         "incoming_server_type, "
3636                         "incoming_server_address, "
3637                         "incoming_server_port_number, "
3638                         "incoming_server_user_name, "
3639                         "incoming_server_password, "
3640                         "incoming_server_secure_connection, "
3641                         "incoming_server_authentication_method, "
3642                         "retrieval_mode, "
3643                         "keep_mails_on_pop_server_after_download, "
3644                         "check_interval, "
3645                         "auto_download_size, "
3646                         "peak_interval, "
3647                         "peak_days, "
3648                         "peak_start_time, "
3649                         "peak_end_time, "
3650                         "outgoing_server_type, "
3651                         "outgoing_server_address, "
3652                         "outgoing_server_port_number, "
3653                         "outgoing_server_user_name, "
3654                         "outgoing_server_password, "
3655                         "outgoing_server_secure_connection, "
3656                         "outgoing_server_need_authentication, "
3657                         "outgoing_server_use_same_authenticator, "
3658                         "priority, "
3659                         "keep_local_copy, "
3660                         "req_delivery_receipt, "
3661                         "req_read_receipt, "
3662                         "download_limit, "
3663                         "block_address, "
3664                         "block_subject, "
3665                         "display_name_from, "
3666                         "reply_with_body, "
3667                         "forward_with_files, "
3668                         "add_myname_card, "
3669                         "add_signature, "
3670                         "signature, "
3671                         "add_my_address_to_bcc, "
3672                         "notification_status, "
3673                         "vibrate_status, "
3674                         "display_content_status, "
3675                         "default_ringtone_status, "
3676                         "alert_ringtone_path, "
3677                         "auto_resend_times, "
3678                         "outgoing_server_size_limit, "
3679                         "wifi_auto_download, "
3680                         "pop_before_smtp, "
3681                         "incoming_server_requires_apop, "
3682                         "smime_type, "
3683                         "certificate_path, "
3684                         "cipher_type, "
3685                         "digest_type"
3686                         );
3687
3688         sql_len = EM_SAFE_STRLEN(sql_query_string);
3689
3690         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " FROM mail_account_tbl ORDER BY account_id");
3691
3692         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
3693
3694         EM_DEBUG_LOG_DEV("After sqlite3_prepare_v2 hStmt = %p", hStmt);
3695         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3696                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
3697
3698         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3699         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3700                         ("sqlite3_step fail:%d", rc));
3701         if (rc == SQLITE_DONE) {
3702                 EM_DEBUG_EXCEPTION("no account found...");
3703
3704                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
3705                 count = 0;
3706                 ret = true;
3707                 goto FINISH_OFF;
3708         }
3709
3710         if (!(p_data_tbl = (emstorage_account_tbl_t*)malloc(sizeof(emstorage_account_tbl_t) * count))) {
3711                 EM_DEBUG_EXCEPTION("malloc failed...");
3712                 error = EMAIL_ERROR_OUT_OF_MEMORY;
3713                 goto FINISH_OFF;
3714         }
3715         memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * count);
3716         for (i = 0; i < count; i++) {
3717                 /*  get recordset */
3718                 field_index = 0;
3719                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_id), field_index++);
3720                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].account_name), 0, field_index++);
3721                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].logo_icon_path), 0, field_index++);
3722                 _get_stmt_field_data_blob(hStmt, &(p_data_tbl[i].user_data), field_index++);
3723                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].user_data_length), field_index++);
3724                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].account_svc_id), field_index++);
3725                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_status), field_index++);
3726                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].sync_disabled), field_index++);
3727                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].default_mail_slot_size), field_index++);
3728                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].roaming_option), field_index++);
3729                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].color_label), field_index++);
3730                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_display_name), 0, field_index++);
3731                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].user_email_address), 0, field_index++);
3732                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].reply_to_address), 0, field_index++);
3733                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].return_address), 0, field_index++);
3734                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].incoming_server_type), field_index++);
3735                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_address), 0, field_index++);
3736                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_port_number), field_index++);
3737                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_user_name), 0, field_index++);
3738                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].incoming_server_password), 0, field_index++);
3739                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_secure_connection), field_index++);
3740                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].incoming_server_authentication_method), field_index++);
3741                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].retrieval_mode), field_index++);
3742                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].keep_mails_on_pop_server_after_download), field_index++);
3743                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].check_interval), field_index++);
3744                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_download_size), field_index++);
3745                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_interval), field_index++);
3746                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_days), field_index++);
3747                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_start_time), field_index++);
3748                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].peak_end_time), field_index++);
3749                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].outgoing_server_type), field_index++);
3750                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_address), 0, field_index++);
3751                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_port_number), field_index++);
3752                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_user_name), 0, field_index++);
3753                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].outgoing_server_password), 0, field_index++);
3754                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_secure_connection), field_index++);
3755                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_need_authentication), field_index++);
3756                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_use_same_authenticator), field_index++);
3757                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.priority), field_index++);
3758                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.keep_local_copy), field_index++);
3759                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_delivery_receipt), field_index++);
3760                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.req_read_receipt), field_index++);
3761                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.download_limit), field_index++);
3762                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_address), field_index++);
3763                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.block_subject), field_index++);
3764                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.display_name_from), 0, field_index++);
3765                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.reply_with_body), field_index++);
3766                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.forward_with_files), field_index++);
3767                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_myname_card), field_index++);
3768                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.add_signature), field_index++);
3769                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.signature), 0, field_index++);
3770                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl[i].options.add_my_address_to_bcc), field_index++);
3771                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.notification_status), field_index++);
3772                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.vibrate_status), field_index++);
3773                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.display_content_status), field_index++);
3774                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].options.default_ringtone_status), field_index++);
3775                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].options.alert_ringtone_path), 0, field_index++);
3776                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].auto_resend_times), field_index++);
3777                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].outgoing_server_size_limit), field_index++);
3778                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].wifi_auto_download), field_index++);
3779                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].pop_before_smtp), field_index++);
3780                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].incoming_server_requires_apop), field_index++);
3781                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].smime_type), field_index++);
3782                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].certificate_path), 0, field_index++);
3783                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].cipher_type), field_index++);
3784                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl[i].digest_type), field_index++);
3785
3786                 /* EAS passwd is not accessible */
3787                 if (with_password == true && p_data_tbl[i].incoming_server_type != EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
3788                         /*  get password from the secure storage */
3789                         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3790                         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH] = {0};
3791
3792                         EM_SAFE_FREE(p_data_tbl[i].incoming_server_password);
3793                         EM_SAFE_FREE(p_data_tbl[i].outgoing_server_password);
3794
3795                         /*  get password file name */
3796                         error = _get_password_file_name(multi_user_name, p_data_tbl[i].account_id,
3797                                         recv_password_file_name,
3798                                         send_password_file_name);
3799                         if (error != EMAIL_ERROR_NONE) {
3800                                 EM_DEBUG_EXCEPTION("_get_password_file_name error [%d]", error);
3801                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3802                                 goto FINISH_OFF;
3803                         }
3804
3805                         /*  read password from secure storage */
3806                         error = _read_password_from_secure_storage(recv_password_file_name,
3807                                         &(p_data_tbl[i].incoming_server_password));
3808                         if (error < 0) {
3809                                 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s] error [%d]",
3810                                                 recv_password_file_name, error);
3811                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3812                                 goto FINISH_OFF;
3813                         }
3814                         error = _read_password_from_secure_storage(send_password_file_name,
3815                                         &(p_data_tbl[i].outgoing_server_password));
3816                         if (error < 0) {
3817                                 EM_DEBUG_EXCEPTION("_read_password_from_secure_storage()[%s]  error [%d]",
3818                                                 send_password_file_name, error);
3819                                 error = EMAIL_ERROR_SECURED_STORAGE_FAILURE;
3820                                 goto FINISH_OFF;
3821                         }
3822                 }
3823
3824                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
3825                 EM_DEBUG_LOG_DEV("after sqlite3_step(), i = %d, rc = %d.", i,  rc);
3826                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
3827                                 ("sqlite3_step fail:%d", rc));
3828         }
3829
3830         ret = true;
3831
3832 FINISH_OFF:
3833         if (ret == true) {
3834                 *account_list = p_data_tbl;
3835                 *select_num = count;
3836                 EM_DEBUG_LOG_DEV("COUNT : %d", count);
3837         } else if (p_data_tbl != NULL)
3838                 emstorage_free_account(&p_data_tbl, count, NULL);
3839
3840         if (hStmt != NULL) {
3841                 rc = sqlite3_finalize(hStmt);
3842                 hStmt = NULL;
3843                 if (rc != SQLITE_OK) {
3844                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
3845                         error = EMAIL_ERROR_DB_FAILURE;
3846                 }
3847         }
3848
3849         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
3850
3851         if (err_code != NULL)
3852                 *err_code = error;
3853
3854         EM_DEBUG_FUNC_END("ret [%d]", ret);
3855         return ret;
3856 }
3857
3858 INTERNAL_FUNC int emstorage_get_maildata_by_servermailid(char *multi_user_name,
3859                 char *server_mail_id,
3860                 int mailbox_id,
3861                 emstorage_mail_tbl_t **mail,
3862                 int transaction,
3863                 int *err_code)
3864 {
3865         EM_DEBUG_FUNC_BEGIN_SEC("mailbox_id [%d], server_mail_id[%s], mail[%p], transaction[%d], err_code[%p]",
3866                         mailbox_id, server_mail_id, mail, transaction, err_code);
3867
3868         int ret = false, error = EMAIL_ERROR_NONE, result_count;
3869         char conditional_clause[QUERY_SIZE] = {0, };
3870         emstorage_mail_tbl_t* p_data_tbl = NULL;
3871
3872         if (!server_mail_id || !mail) {
3873                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3874                 error = EMAIL_ERROR_INVALID_PARAM;
3875                 goto FINISH_OFF;
3876         }
3877
3878         sqlite3_snprintf(sizeof(conditional_clause), conditional_clause, "WHERE server_mail_id = '%q'", server_mail_id);
3879
3880         if (mailbox_id > 0) {
3881                 SNPRINTF(conditional_clause + strlen(conditional_clause),
3882                                 QUERY_SIZE - strlen(conditional_clause),
3883                                 " AND mailbox_id = %d",
3884                                 mailbox_id);
3885         }
3886
3887         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
3888         if (!emstorage_query_mail_tbl(multi_user_name,
3889                                 conditional_clause,
3890                                 transaction,
3891                                 &p_data_tbl,
3892                                 &result_count,
3893                                 &error)) {
3894                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
3895                 goto FINISH_OFF;
3896         }
3897
3898         ret = true;
3899
3900 FINISH_OFF:
3901
3902         if (ret == true)
3903                 *mail = p_data_tbl;
3904
3905         if (err_code != NULL)
3906                 *err_code = error;
3907
3908         EM_DEBUG_FUNC_END("ret [%d]", ret);
3909         return ret;
3910 }
3911
3912
3913 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)
3914 {
3915         int cur_clause = 0, conditional_clause_count = 0, i = 0;
3916
3917         if (account_id < ALL_ACCOUNT) {
3918                 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
3919                 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
3920         }
3921
3922         /*  where clause */
3923         if (account_id != ALL_ACCOUNT) {
3924                 cur_clause += (conditional_clause_count++ == 0) ?
3925                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE account_id = %d", account_id) :
3926                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND account_id = %d", account_id);
3927         }
3928
3929         if (mailbox_id > 0) {
3930                 cur_clause += (conditional_clause_count++ == 0) ?
3931                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE mailbox_id = %d", mailbox_id) :
3932                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND mailbox_id = %d", mailbox_id);
3933         }
3934
3935         if (thread_id > 0) {
3936                 cur_clause += (conditional_clause_count++ == 0) ?
3937                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_id = %d ", thread_id) :
3938                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_id = %d ", thread_id);
3939         } else if (thread_id == EMAIL_LIST_TYPE_THREAD) {
3940                 cur_clause += (conditional_clause_count++ == 0) ?
3941                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE thread_item_count > 0") :
3942                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND thread_item_count > 0");
3943         } else if (thread_id == EMAIL_LIST_TYPE_LOCAL) {
3944                 cur_clause += (conditional_clause_count++ == 0) ?
3945                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE server_mail_status = 0") :
3946                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND server_mail_status = 0");
3947         } else if (thread_id == EMAIL_LIST_TYPE_UNREAD) {
3948                 cur_clause += (conditional_clause_count++ == 0) ?
3949                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_seen_field == 0") :
3950                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_seen_field == 0");
3951         }
3952
3953         /*  EM_DEBUG_LOG("where clause added [%s]", conditional_clause_string); */
3954         if (addr_list && addr_list->address_count > 0) {
3955                 if (!addr_list->address_type) {
3956                         cur_clause += (conditional_clause_count++ == 0) ?
3957                                 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE email_address_sender IN(\"%s\"", addr_list->address_list[0]) :
3958                                 SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND email_address_sender IN(\"%s\"", addr_list->address_list[0]);
3959
3960                         for (i = 1; i < addr_list->address_count; i++)
3961                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3962
3963                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3964                 } else {
3965                         cur_clause += (conditional_clause_count++ == 0) ?
3966                                 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]) :
3967                                 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]);
3968
3969                         for (i = 1; i < addr_list->address_count; i++)
3970                                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ",\"%s\"", addr_list->address_list[i]);
3971
3972                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, ")");
3973                 }
3974         }
3975
3976         if (input_except_delete_flagged_mails) {
3977                 cur_clause += (conditional_clause_count++ == 0) ?
3978                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " WHERE flags_deleted_field = 0") :
3979                         SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause, " AND flags_deleted_field = 0");
3980         }
3981
3982         if (search_value) {
3983                 switch (search_type) {
3984                 case EMAIL_SEARCH_FILTER_SUBJECT:
3985                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3986                                         " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3987                         break;
3988                 case EMAIL_SEARCH_FILTER_SENDER:
3989                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3990                                         " %s  ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3991                                         ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value);
3992                         break;
3993                 case EMAIL_SEARCH_FILTER_RECIPIENT:
3994                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
3995                                         " %s ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3996                                         "       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3997                                         "       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
3998                                         ") ", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value);
3999                         break;
4000                 case EMAIL_SEARCH_FILTER_ALL:
4001                         cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size - cur_clause,
4002                                         " %s (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\' "
4003                                         "       OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
4004                                         "                       OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
4005                                         "                       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
4006                                         "                       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\') ESCAPE '\\') "
4007                                         "               ) "
4008                                         "       )"
4009                                         ")", conditional_clause_count++ ? "AND" : "WHERE", search_value, search_value, search_value, search_value, search_value);
4010                         break;
4011                 }
4012         }
4013
4014         /*  EM_DEBUG_LOG("where clause [%s]", conditional_clause_string); */
4015         static char sorting_str[][50] = {
4016                 " ORDER BY date_time DESC",                                              /* case EMAIL_SORT_DATETIME_HIGH: */
4017                 " ORDER BY date_time ASC",                                                /* case EMAIL_SORT_DATETIME_LOW: */
4018                 " ORDER BY full_address_from DESC, date_time DESC", /* case EMAIL_SORT_SENDER_HIGH: */
4019                 " ORDER BY full_address_from ASC, date_time DESC",  /* case EMAIL_SORT_SENDER_LOW: */
4020                 " ORDER BY full_address_to DESC, date_time DESC",   /* case EMAIL_SORT_RCPT_HIGH: */
4021                 " ORDER BY full_address_to ASC, date_time DESC",        /* case EMAIL_SORT_RCPT_LOW: */
4022                 " ORDER BY subject DESC, date_time DESC",                  /* case EMAIL_SORT_SUBJECT_HIGH: */
4023                 " ORDER BY subject ASC, date_time DESC",                        /* case EMAIL_SORT_SUBJECT_LOW: */
4024                 " ORDER BY priority DESC, date_time DESC",                /* case EMAIL_SORT_PRIORITY_HIGH: */
4025                 " ORDER BY priority ASC, date_time DESC",                  /* case EMAIL_SORT_PRIORITY_LOW: */
4026                 " ORDER BY attachment_count DESC, date_time DESC",  /* case EMAIL_SORT_ATTACHMENT_HIGH: */
4027                 " ORDER BY attachment_count ASC, date_time DESC",   /* case EMAIL_SORT_ATTACHMENT_LOW: */
4028                 " ORDER BY lock_status DESC, date_time DESC",      /* case EMAIL_SORT_FAVORITE_HIGH: */
4029                 " ORDER BY lock_status ASC, date_time DESC",            /* case EMAIL_SORT_FAVORITE_LOW: */
4030                 " ORDER BY mailbox_id DESC, date_time DESC",      /* case EMAIL_SORT_MAILBOX_ID_HIGH: */
4031                 " ORDER BY mailbox_id ASC, date_time DESC",             /* case EMAIL_SORT_MAILBOX_ID_LOW: */
4032                 " ORDER BY flags_flagged_field DESC, date_time DESC",  /* case EMAIL_SORT_FLAGGED_FLAG_HIGH: */
4033                 " ORDER BY flags_flagged_field ASC, date_time DESC",   /* case EMAIL_SORT_FLAGGED_FLAG_LOW: */
4034                 " ORDER BY flags_seen_field DESC, date_time DESC",     /* case EMAIL_SORT_SEEN_FLAG_HIGH: */
4035                 " ORDER BY flags_seen_field ASC, date_time DESC"       /* case EMAIL_SORT_SEEN_FLAG_LOW: */
4036         };
4037
4038         if (sorting < EMAIL_SORT_END)
4039                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " %s", sorting_str[sorting]);
4040         else
4041                 EM_DEBUG_LOG(" Invalid Sorting order ");
4042
4043         /*  limit clause */
4044         if (start_index != -1 && limit_count != -1)
4045                 cur_clause += SNPRINTF_OFFSET(conditional_clause_string, cur_clause, buffer_size, " LIMIT %d, %d", start_index, limit_count);
4046
4047         return true;
4048 }
4049
4050
4051 /**
4052  *      emstorage_get_mail_list - Get the mail list information.
4053  *
4054  *
4055  */
4056 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)
4057 {
4058         EM_DEBUG_FUNC_BEGIN();
4059         EM_PROFILE_BEGIN(emstorage_get_mail_list_func);
4060
4061         int ret = false, error = EMAIL_ERROR_NONE;
4062         char conditional_clause_string[QUERY_SIZE] = { 0, };
4063
4064         if (account_id < ALL_ACCOUNT) {
4065                 EM_DEBUG_EXCEPTION("Invalid account_id [%d]", account_id);
4066                 EM_RETURN_ERR_CODE(err_code, EMAIL_ERROR_INVALID_PARAM, false);
4067         }
4068         EM_IF_NULL_RETURN_VALUE(result_count, false);
4069
4070         _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);
4071
4072         EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
4073
4074         if (!emstorage_query_mail_list(multi_user_name, conditional_clause_string, transaction, mail_list, result_count, &error)) {
4075                 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
4076                 goto FINISH_OFF;
4077         }
4078
4079         ret = true;
4080
4081 FINISH_OFF:
4082         if (err_code != NULL)
4083                 *err_code = error;
4084
4085         EM_PROFILE_END(emstorage_get_mail_list_func);
4086         EM_DEBUG_FUNC_END("ret [%d]", ret);
4087         return ret;
4088 }
4089
4090
4091 /**
4092  *      emstorage_get_mails - Get the Mail list information based on mailbox_name name
4093  *
4094  *
4095  */
4096 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)
4097 {
4098         EM_DEBUG_FUNC_BEGIN();
4099         EM_PROFILE_BEGIN(emStorageGetMails);
4100
4101         int count = 0, ret = false, error = EMAIL_ERROR_NONE;
4102         emstorage_mail_tbl_t *p_data_tbl = NULL;
4103         char conditional_clause_string[QUERY_SIZE] = {0, };
4104
4105         EM_IF_NULL_RETURN_VALUE(mail_list, false);
4106         EM_IF_NULL_RETURN_VALUE(result_count, false);
4107
4108         if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
4109                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4110                 if (err_code != NULL)
4111                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4112                 return false;
4113         }
4114
4115         _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);
4116
4117         EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
4118
4119         if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause_string, transaction, &p_data_tbl, &count,  &error)) {
4120                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
4121                 goto FINISH_OFF;
4122         }
4123
4124         ret = true;
4125
4126 FINISH_OFF:
4127
4128         if (ret == true) {
4129                 *mail_list = p_data_tbl;
4130                 *result_count = count;
4131                 EM_DEBUG_LOG("COUNT : %d", count);
4132         } else if (p_data_tbl != NULL)
4133                 emstorage_free_mail(&p_data_tbl, count, NULL);
4134
4135         if (err_code != NULL)
4136                 *err_code = error;
4137
4138         EM_PROFILE_END(emStorageGetMails);
4139         EM_DEBUG_FUNC_END("ret [%d]", ret);
4140         return ret;
4141 }
4142
4143
4144
4145 /**
4146  *      emstorage_get_searched_mail_list - Get the mail list information after filtering
4147  *
4148  *
4149  */
4150 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)
4151 {
4152         EM_DEBUG_FUNC_BEGIN();
4153
4154         int ret = false, error = EMAIL_ERROR_NONE;
4155         char conditional_clause[QUERY_SIZE] = {0, };
4156         char *temp_search_value = NULL;
4157         char *temp_search_value2 = NULL;
4158
4159         EM_IF_NULL_RETURN_VALUE(mail_list, false);
4160         EM_IF_NULL_RETURN_VALUE(result_count, false);
4161
4162         if (!result_count || !mail_list || account_id < ALL_ACCOUNT) {
4163                 EM_DEBUG_EXCEPTION("select_num[%p], Mail_list[%p]", result_count, mail_list);
4164                 error = EMAIL_ERROR_INVALID_PARAM;
4165                 goto FINISH_OFF;
4166         }
4167
4168         temp_search_value = em_replace_all_string((char*)search_value, "_", "\\_");
4169         temp_search_value2 = em_replace_all_string(temp_search_value, "%", "\\%");
4170
4171         _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);
4172
4173         EM_DEBUG_LOG("conditional_clause[%s]", conditional_clause);
4174
4175         if (!emstorage_query_mail_list(multi_user_name, conditional_clause, transaction, mail_list, result_count, &error)) {
4176                 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", error);
4177                 goto FINISH_OFF;
4178         }
4179
4180         ret = true;
4181
4182 FINISH_OFF:
4183         EM_DEBUG_LOG("emstorage_get_searched_mail_list finish off");
4184
4185         if (err_code != NULL)
4186                 *err_code = error;
4187
4188         EM_SAFE_FREE(temp_search_value);
4189         EM_SAFE_FREE(temp_search_value2);
4190
4191         EM_DEBUG_FUNC_END("ret [%d]", ret);
4192         return ret;
4193 }
4194
4195
4196 static int _get_password_file_name(char *multi_user_name, int account_id, char *recv_password_file_name, char *send_password_file_name)
4197 {
4198         EM_DEBUG_FUNC_BEGIN("account_id[%d]", account_id);
4199
4200         if (account_id <= 0 || !recv_password_file_name || !send_password_file_name) {
4201                 EM_DEBUG_EXCEPTION("Invalid parameter");
4202                 return EMAIL_ERROR_INVALID_PARAM;
4203         }
4204
4205         EM_DEBUG_LOG("MULTI_USER_NAME : [%s]", multi_user_name);
4206
4207         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
4208                 snprintf(recv_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_recv_%s", account_id, multi_user_name);
4209                 snprintf(send_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_send_%s", account_id, multi_user_name);
4210         } else {
4211                 snprintf(recv_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_recv", account_id);
4212                 snprintf(send_password_file_name, MAX_PW_FILE_NAME_LENGTH, ".email_account_%d_send", account_id);
4213         }
4214
4215         EM_DEBUG_FUNC_END();
4216         return EMAIL_ERROR_NONE;
4217 }
4218
4219 static int _read_password_from_secure_storage(char *file_name, char **password)
4220 {
4221         EM_DEBUG_FUNC_BEGIN_SEC("file_name[%s]", file_name);
4222
4223         if (!file_name || !password) {
4224                 EM_DEBUG_EXCEPTION("Invalid Parameter");
4225                 return EMAIL_ERROR_INVALID_PARAM;
4226         }
4227
4228         int ret = EMAIL_ERROR_NONE;
4229
4230         ret = emcore_get_password_in_key_manager(file_name, password);
4231         if (ret != EMAIL_ERROR_NONE) {
4232                 EM_DEBUG_EXCEPTION("emcore_get_password_in_key_manager failed : [%d]", ret);
4233                 goto FINISH_OFF;
4234         }
4235
4236 FINISH_OFF:
4237
4238         EM_DEBUG_FUNC_END("ret [%d]", ret);
4239         return ret;
4240 }
4241                                         /* LCOV_EXCL_STOP*/
4242 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)
4243 {
4244         EM_DEBUG_FUNC_BEGIN("account_id[%d], pulloption[%d], account[%p], transaction[%d], err_code[%p]", account_id, pulloption, account, transaction, err_code);
4245
4246         if (!account) {
4247                                         /* LCOV_EXCL_START */
4248                 EM_DEBUG_EXCEPTION("account_id[%d], account[%p]", account_id, account);
4249                 if (err_code != NULL)
4250                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4251                 return false;
4252                                         /* LCOV_EXCL_STOP */
4253         }
4254
4255         int ret = false;
4256         int error = EMAIL_ERROR_NONE;
4257         emstorage_account_tbl_t* p_data_tbl = NULL;
4258         DB_STMT hStmt = NULL;
4259         char sql_query_string[QUERY_SIZE] = {0, };
4260         int rc = -1;
4261         int sql_len = 0;
4262         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4263         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4264
4265         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4266
4267         EMSTORAGE_START_READ_TRANSACTION(transaction);
4268
4269         /*  Make query string */
4270         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT ");
4271         sql_len = EM_SAFE_STRLEN(sql_query_string);
4272
4273         if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4274                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4275                                 "incoming_server_type,"
4276                                 "incoming_server_address,"
4277                                 "user_email_address,"
4278                                 "incoming_server_user_name,"
4279                                 "retrieval_mode,"
4280                                 "incoming_server_port_number,"
4281                                 "incoming_server_secure_connection,"
4282                                 "incoming_server_authentication_method,"
4283                                 "outgoing_server_type,"
4284                                 "outgoing_server_address,"
4285                                 "outgoing_server_port_number,"
4286                                 "outgoing_server_need_authentication,"
4287                                 "outgoing_server_secure_connection,"
4288                                 "outgoing_server_user_name,"
4289                                 "user_display_name,"
4290                                 "reply_to_address,"
4291                                 "return_address,"
4292                                 "account_id,"
4293                                 "keep_mails_on_pop_server_after_download,"
4294                                 "auto_download_size,"
4295                                 "peak_interval,"
4296                                 "peak_days,"
4297                                 "peak_start_time,"
4298                                 "peak_end_time,"
4299                                 "outgoing_server_use_same_authenticator,"
4300                                 "auto_resend_times,"
4301                                 "outgoing_server_size_limit,"
4302                                 "wifi_auto_download,"
4303                                 "pop_before_smtp,"
4304                                 "incoming_server_requires_apop,"
4305                                 "logo_icon_path,"
4306                                 "user_data,"
4307                                 "user_data_length,"
4308                                 "color_label,"
4309                                 "check_interval,"
4310                                 "sync_status,");
4311                 sql_len = EM_SAFE_STRLEN(sql_query_string);
4312         }
4313
4314         if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME) {
4315                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, " account_name, ");
4316                 sql_len = EM_SAFE_STRLEN(sql_query_string);
4317         }
4318
4319         /*  get from secure storage, not from db */
4320         if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4321                 SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len,
4322                                 "priority,"
4323                                 "keep_local_copy,"
4324                                 "req_delivery_receipt,"
4325                                 "req_read_receipt,"
4326                                 "download_limit,"
4327                                 "block_address,"
4328                                 "block_subject,"
4329                                 "display_name_from,"
4330                                 "reply_with_body,"
4331                                 "forward_with_files,"
4332                                 "add_myname_card,"
4333                                 "add_signature,"
4334                                 "signature,"
4335                                 "add_my_address_to_bcc,"
4336                                 "notification_status,"
4337                                 "vibrate_status,"
4338                                 "display_content_status,"
4339                                 "default_ringtone_status,"
4340                                 "alert_ringtone_path,"
4341                                 "account_svc_id,"
4342                                 "sync_disabled,"
4343                                 "default_mail_slot_size,"
4344                                 "roaming_option,"
4345                                 "smime_type,"
4346                                 "certificate_path,"
4347                                 "cipher_type,"
4348                                 "digest_type,");
4349
4350                 sql_len = EM_SAFE_STRLEN(sql_query_string);
4351         }
4352
4353         /*  dummy value, FROM WHERE clause */
4354         SNPRINTF(sql_query_string + sql_len, sizeof(sql_query_string) - sql_len, "0 FROM mail_account_tbl WHERE account_id = %d", account_id);
4355
4356         /*  FROM clause */
4357         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
4358
4359         /*  execute a sql and count rows */
4360         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4361         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4362                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4363
4364         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4365         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4366                         ("sqlite3_step fail:%d", rc));
4367
4368         if (rc == SQLITE_DONE) {
4369                 EM_DEBUG_EXCEPTION("no matched account found...");
4370                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4371                 goto FINISH_OFF;
4372         }
4373
4374         /*  Assign query result to structure */
4375         if (!(p_data_tbl = (emstorage_account_tbl_t *)malloc(sizeof(emstorage_account_tbl_t) * 1))) {
4376                 EM_DEBUG_EXCEPTION("malloc failed...");
4377                 error = EMAIL_ERROR_OUT_OF_MEMORY;
4378                 goto FINISH_OFF;
4379         }
4380
4381         memset(p_data_tbl, 0x00, sizeof(emstorage_account_tbl_t) * 1);
4382         int col_index = 0;
4383
4384         if (pulloption & EMAIL_ACC_GET_OPT_DEFAULT) {
4385                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_type), col_index++);
4386                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_address),  0, col_index++);
4387                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_email_address), 0, col_index++);
4388                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->incoming_server_user_name), 0, col_index++);
4389                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->retrieval_mode), col_index++);
4390                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_port_number), col_index++);
4391                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_secure_connection), col_index++);
4392                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->incoming_server_authentication_method), col_index++);
4393                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->outgoing_server_type), col_index++);
4394                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_address), 0, col_index++);
4395                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_port_number), col_index++);
4396                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_need_authentication), col_index++);
4397                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_secure_connection), col_index++);
4398                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->outgoing_server_user_name), 0, col_index++);
4399                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->user_display_name), 0, col_index++);
4400                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->reply_to_address), 0, col_index++);
4401                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->return_address), 0, col_index++);
4402                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
4403                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->keep_mails_on_pop_server_after_download), col_index++);
4404                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_download_size), col_index++);
4405                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_interval), col_index++);
4406                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_days), col_index++);
4407                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_start_time), col_index++);
4408                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->peak_end_time), col_index++);
4409                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_use_same_authenticator), col_index++);
4410                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->auto_resend_times), col_index++);
4411                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->outgoing_server_size_limit), col_index++);
4412                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->wifi_auto_download), col_index++);
4413                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->pop_before_smtp), col_index++);
4414                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->incoming_server_requires_apop), col_index++);
4415                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->logo_icon_path), 0, col_index++);
4416                 _get_stmt_field_data_blob(hStmt, &p_data_tbl->user_data, col_index++);
4417                 _get_stmt_field_data_int(hStmt, &p_data_tbl->user_data_length, col_index++);
4418                 _get_stmt_field_data_int(hStmt, &p_data_tbl->color_label, col_index++);
4419                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->check_interval), col_index++);
4420                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->sync_status), col_index++);
4421         }
4422
4423         if (pulloption & EMAIL_ACC_GET_OPT_ACCOUNT_NAME)
4424                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->account_name), 0, col_index++);
4425
4426         if (pulloption & EMAIL_ACC_GET_OPT_PASSWORD) {
4427                 /*  get password file name */
4428                 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) {
4429                         EM_DEBUG_EXCEPTION("_get_password_file_name failed [%d]", error);
4430                         goto FINISH_OFF;
4431                 }
4432
4433                 /*  read password from secure storage */
4434                 if ((error = _read_password_from_secure_storage(recv_password_file_name, &(p_data_tbl->incoming_server_password))) < 0) {
4435                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4436                         goto FINISH_OFF;
4437                 }
4438
4439                 EM_DEBUG_LOG_DEV("recv_password_file_name[%s], password[%s]", recv_password_file_name,  p_data_tbl->incoming_server_password);
4440
4441                 if (p_data_tbl->outgoing_server_use_same_authenticator == 0) {
4442                         if ((error = _read_password_from_secure_storage(send_password_file_name, &(p_data_tbl->outgoing_server_password))) < 0) {
4443                                 EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage failed [%d]", error);
4444                                 goto FINISH_OFF;
4445                         }
4446                         EM_DEBUG_LOG_DEV("send_password_file_name[%s], password[%s]", send_password_file_name,  p_data_tbl->outgoing_server_password);
4447                 }
4448         }
4449
4450         if (pulloption & EMAIL_ACC_GET_OPT_OPTIONS) {
4451                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.priority), col_index++);
4452                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.keep_local_copy), col_index++);
4453                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_delivery_receipt), col_index++);
4454                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.req_read_receipt), col_index++);
4455                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.download_limit), col_index++);
4456                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_address), col_index++);
4457                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.block_subject), col_index++);
4458                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.display_name_from), 0, col_index++);
4459                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.reply_with_body), col_index++);
4460                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.forward_with_files), col_index++);
4461                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_myname_card), col_index++);
4462                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->options.add_signature), col_index++);
4463                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.signature), 0, col_index++);
4464                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.add_my_address_to_bcc), col_index++);
4465                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.notification_status), col_index++);
4466                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.vibrate_status), col_index++);
4467                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.display_content_status), col_index++);
4468                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->options.default_ringtone_status), col_index++);
4469                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->options.alert_ringtone_path), 0, col_index++);
4470                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_svc_id), col_index++);
4471                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->sync_disabled), col_index++);
4472                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->default_mail_slot_size), col_index++);
4473                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->roaming_option), col_index++);
4474                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->smime_type), col_index++);
4475                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->certificate_path), 0, col_index++);
4476                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->cipher_type), col_index++);
4477                 _get_stmt_field_data_int(hStmt, (int *)&(p_data_tbl->digest_type), col_index++);
4478         }
4479
4480         ret = true;
4481
4482 FINISH_OFF:
4483         if (ret == true)
4484                 *account = p_data_tbl;
4485         else {
4486                 if (p_data_tbl)
4487                         emstorage_free_account((emstorage_account_tbl_t **)&p_data_tbl, 1, NULL);
4488         }
4489         if (hStmt != NULL) {
4490                 rc = sqlite3_finalize(hStmt);
4491                 if (rc != SQLITE_OK) {
4492                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
4493                         error = EMAIL_ERROR_DB_FAILURE;
4494                 }
4495         }
4496
4497         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
4498
4499         if (err_code != NULL)
4500                 *err_code = error;
4501
4502         EM_DEBUG_FUNC_END("ret [%d]", ret);
4503         return ret;
4504 }
4505                                         /* LCOV_EXCL_START */
4506 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)
4507 {
4508         EM_DEBUG_FUNC_BEGIN("account_id[%d], password_length[%p], err_code[%p]", account_id, password_length, err_code);
4509
4510         if (account_id <= 0 || password_length == NULL) {
4511                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4512                 if (err_code != NULL)
4513                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4514                 return false;
4515         }
4516
4517         int ret = false;
4518         int error = EMAIL_ERROR_NONE;
4519         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4520         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4521         char *temp_password = NULL;
4522
4523
4524         /*  get password file name */
4525         if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
4526                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4527                 goto FINISH_OFF;
4528         }
4529
4530         /*  read password from secure storage */
4531         if (password_type == EMAIL_GET_INCOMING_PASSWORD_LENGTH) {
4532                 if ((error = _read_password_from_secure_storage(recv_password_file_name, &temp_password)) < 0 || !temp_password) {
4533                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed...");
4534                         goto FINISH_OFF;
4535                 }
4536         } else if (password_type == EMAIL_GET_OUTGOING_PASSWORD_LENGTH) {
4537                 if ((error = _read_password_from_secure_storage(send_password_file_name, &temp_password)) < 0 || !temp_password) {
4538                         EM_DEBUG_EXCEPTION(" _read_password_from_secure_storage()  failed...");
4539                         goto FINISH_OFF;
4540                 }
4541         } else {
4542                 EM_DEBUG_LOG("Invalid password type");
4543                 if (err_code != NULL)
4544                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4545                 goto FINISH_OFF;
4546         }
4547
4548         *password_length = EM_SAFE_STRLEN(temp_password);
4549
4550         EM_DEBUG_LOG_SEC("recv_password_file_name[%s], *password_length[%d]", recv_password_file_name,  *password_length);
4551
4552         ret = true;
4553
4554 FINISH_OFF:
4555         EM_SAFE_FREE(temp_password);
4556
4557         if (err_code != NULL)
4558                 *err_code = error;
4559
4560         EM_DEBUG_FUNC_END("ret [%d]", ret);
4561         return ret;
4562 }
4563
4564 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)
4565 {
4566         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);
4567
4568         int err = EMAIL_ERROR_NONE;
4569         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4570         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
4571
4572         if (input_incoming_server_password == NULL && input_outgoing_server_password == NULL) {
4573                 EM_DEBUG_EXCEPTION_SEC("Invalid param");
4574                 err = EMAIL_ERROR_INVALID_PARAM;
4575                 goto FINISH_OFF;
4576         }
4577
4578         /*  get password file name */
4579         if ((err = _get_password_file_name(multi_user_name,
4580                                         input_account_id,
4581                                         recv_password_file_name,
4582                                         send_password_file_name)) != EMAIL_ERROR_NONE) {
4583                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
4584                 goto FINISH_OFF;
4585         }
4586
4587         if (input_incoming_server_password) {
4588                 EM_DEBUG_LOG_SEC("recv_password_file_name [%s] input_incoming_server_password [%s]",
4589                                 recv_password_file_name, input_incoming_server_password);
4590         }
4591         if (input_outgoing_server_password) {
4592                 EM_DEBUG_LOG_SEC("send_password_file_name [%s] input_outgoing_server_password [%s]",
4593                                 send_password_file_name, input_outgoing_server_password);
4594         }
4595
4596         if (input_incoming_server_password) {
4597                 err = emcore_remove_password_in_key_manager(recv_password_file_name);
4598                 if (err != EMAIL_ERROR_NONE) {
4599                         EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", recv_password_file_name);
4600                         goto FINISH_OFF;
4601                 }
4602
4603                 /*  save recv passwords to the secure storage */
4604                 err = emcore_add_password_in_key_manager(recv_password_file_name, input_incoming_server_password);
4605                 if (err != EMAIL_ERROR_NONE) {
4606                         EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4607                         goto FINISH_OFF;
4608                 }
4609         }
4610
4611         if (input_outgoing_server_password) {
4612                 err = emcore_remove_password_in_key_manager(send_password_file_name);
4613                 if (err != EMAIL_ERROR_NONE) {
4614                         EM_DEBUG_EXCEPTION_SEC("emcore_remove_password_in_key_manager: file[%s]", send_password_file_name);
4615                         goto FINISH_OFF;
4616                 }
4617
4618                 /*  save send passwords to the secure storage */
4619                 err = emcore_add_password_in_key_manager(send_password_file_name, input_outgoing_server_password);
4620                 if (err != EMAIL_ERROR_NONE) {
4621                         EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", err);
4622                         goto FINISH_OFF;
4623                 }
4624         }
4625 FINISH_OFF:
4626
4627         EM_DEBUG_FUNC_END("err [%d]", err);
4628         return err;
4629 }
4630
4631 INTERNAL_FUNC int emstorage_update_account(char *multi_user_name, int account_id, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
4632 {
4633         EM_DEBUG_FUNC_BEGIN("account_id[%d], account[%p], transaction[%d], err_code[%p]", account_id, account_tbl, transaction, err_code);
4634
4635         if (account_id < FIRST_ACCOUNT_ID || !account_tbl) {
4636                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4637                 if (err_code != NULL)
4638                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4639                 return false;
4640         }
4641
4642         int error = EMAIL_ERROR_NONE;
4643         int rc, ret = false;
4644
4645         DB_STMT hStmt = NULL;
4646         char sql_query_string[QUERY_SIZE] = {0, };
4647
4648         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4649
4650         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4651
4652         SNPRINTF(sql_query_string, sizeof(sql_query_string),
4653                         "UPDATE mail_account_tbl SET"
4654                         "  account_name = ?"
4655                         ", logo_icon_path = ?"
4656                         ", user_data = ?"
4657                         ", user_data_length = ?"
4658                         ", account_svc_id = ?"
4659                         ", sync_status = ?"
4660                         ", sync_disabled = ?"
4661                         ", default_mail_slot_size = ?"
4662                         ", roaming_option = ?"
4663                         ", color_label = ?"
4664                         ", user_display_name = ?"
4665                         ", user_email_address = ?"
4666                         ", reply_to_address = ?"
4667                         ", return_address = ?"
4668                         ", incoming_server_type = ?"
4669                         ", incoming_server_address = ?"
4670                         ", incoming_server_port_number = ?"
4671                         ", incoming_server_user_name = ?"
4672                         ", incoming_server_secure_connection = ?"
4673                         ", incoming_server_authentication_method = ?"
4674                         ", retrieval_mode = ?"
4675                         ", keep_mails_on_pop_server_after_download = ?"
4676                         ", check_interval = ?"
4677                         ", auto_download_size = ?"
4678                         ", peak_interval = ?"
4679                         ", peak_days = ?"
4680                         ", peak_start_time = ?"
4681                         ", peak_end_time = ?"
4682                         ", outgoing_server_type = ?"
4683                         ", outgoing_server_address = ?"
4684                         ", outgoing_server_port_number = ?"
4685                         ", outgoing_server_user_name = ?"
4686                         ", outgoing_server_secure_connection = ?"
4687                         ", outgoing_server_need_authentication = ?"
4688                         ", outgoing_server_use_same_authenticator = ?"
4689                         ", priority = ?"
4690                         ", keep_local_copy = ?"
4691                         ", req_delivery_receipt = ?"
4692                         ", req_read_receipt = ?"
4693                         ", download_limit = ?"
4694                         ", block_address = ?"
4695                         ", block_subject = ?"
4696                         ", display_name_from = ?"
4697                         ", reply_with_body = ?"
4698                         ", forward_with_files = ?"
4699                         ", add_myname_card = ?"
4700                         ", add_signature = ?"
4701                         ", signature = ?"
4702                         ", add_my_address_to_bcc = ?"
4703                         ", notification_status = ?"
4704                         ", vibrate_status = ?"
4705                         ", display_content_status = ?"
4706                         ", default_ringtone_status = ?"
4707                         ", alert_ringtone_path = ?"
4708                         ", auto_resend_times = ?"
4709                         ", outgoing_server_size_limit = ?"
4710                         ", wifi_auto_download = ?"
4711                         ", pop_before_smtp = ?"
4712                         ", incoming_server_requires_apop = ?"
4713                         ", smime_type = ?"
4714                         ", certificate_path = ?"
4715                         ", cipher_type = ?"
4716                         ", digest_type = ?"
4717                         ", user_name = ?"
4718                         " WHERE account_id = ?");
4719
4720         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
4721         EM_DEBUG_LOG("After sqlite3_prepare hStmt = %p", hStmt);
4722         EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
4723         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4724                         ("sqlite3_prepare fail:(%d) %s", rc, sqlite3_errmsg(local_db_handle)));
4725
4726         int i = 0;
4727
4728         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4729         _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4730         _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
4731         _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
4732         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
4733         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
4734         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
4735         _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
4736         _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
4737         _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
4738         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4739         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4740         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4741         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4742         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
4743         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4744         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
4745         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
4746         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
4747         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
4748         _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
4749         _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
4750         _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
4751         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
4752         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
4753         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
4754         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
4755         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
4756         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
4757         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
4758         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
4759         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
4760         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
4761         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
4762         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
4763         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
4764         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
4765         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
4766         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
4767         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
4768         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
4769         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
4770         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4771         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
4772         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
4773         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
4774         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
4775         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
4776         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
4777         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
4778         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
4779         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
4780         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
4781         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4782         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
4783         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
4784         _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
4785         _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
4786         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
4787         _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
4788         _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
4789         _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
4790         _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
4791         _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
4792         _bind_stmt_field_data_int(hStmt, i++, account_id);
4793
4794         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
4795         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
4796                         ("sqlite3_step fail:%d", rc));
4797         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
4798                         ("sqlite3_step fail:%d", rc));
4799
4800         /*  validate account existence */
4801         rc = sqlite3_changes(local_db_handle);
4802         if (rc == 0) {
4803                 EM_DEBUG_EXCEPTION("no matched account found...");
4804                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4805                 goto FINISH_OFF;
4806         }
4807
4808         if (account_tbl->incoming_server_password || account_tbl->outgoing_server_password) {
4809                 if ((error = emstorage_update_account_password(multi_user_name, account_id, account_tbl->incoming_server_password, account_tbl->outgoing_server_password)) != EMAIL_ERROR_NONE) {
4810                         EM_DEBUG_EXCEPTION("emstorage_update_account_password failed [%d]", error);
4811                         goto FINISH_OFF;
4812                 }
4813         }
4814
4815         ret = true;
4816
4817 FINISH_OFF:
4818         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4819
4820         if (hStmt != NULL) {
4821                 rc = sqlite3_finalize(hStmt);
4822                 if (rc != SQLITE_OK) {
4823                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
4824                         error = EMAIL_ERROR_DB_FAILURE;
4825                 }
4826         }
4827
4828         if (error == EMAIL_ERROR_NONE) {
4829                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_tbl->account_id, 0, NULL, 0))
4830                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_event[ NOTI_ACCOUNT_UPDATE] : Notification Failed >>> ");
4831         }
4832
4833         if (err_code != NULL)
4834                 *err_code = error;
4835
4836         EM_DEBUG_FUNC_END("ret [%d]", ret);
4837         return ret;
4838 }
4839
4840 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)
4841 {
4842         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], field_name[%s], value[%d], transaction[%d]", account_id, field_name, value, transaction);
4843         int error = EMAIL_ERROR_NONE;
4844         int result = 0;
4845         char sql_query_string[QUERY_SIZE] = {0, };
4846         sqlite3 *local_db_handle = NULL;
4847
4848         if (!account_id  || !field_name) {
4849                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4850                 return EMAIL_ERROR_INVALID_PARAM;
4851         }
4852
4853         local_db_handle = emstorage_get_db_connection(multi_user_name);
4854
4855         /* Write query string */
4856         SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_account_tbl SET %s = %d WHERE account_id = %d", field_name, value, account_id);
4857
4858         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4859
4860         /* Execute query */
4861         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4862
4863         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4864         if (error != EMAIL_ERROR_NONE) {
4865                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4866                 goto FINISH_OFF;
4867         }
4868
4869         if (sqlite3_changes(local_db_handle) == 0)
4870                 EM_DEBUG_LOG("no mail matched...");
4871
4872
4873 FINISH_OFF:
4874         result = (error == EMAIL_ERROR_NONE) ? true : false;
4875         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, error);
4876
4877         if (error == EMAIL_ERROR_NONE) {
4878                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE, account_id, 0, field_name, value))
4879                         EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_ACCOUNT_UPDATE [%s,%d]", field_name, value);
4880         }
4881         EM_DEBUG_FUNC_END("error [%d]", error);
4882         return error;
4883 }
4884
4885 INTERNAL_FUNC int emstorage_get_sync_status_of_account(char *multi_user_name, int account_id, int *result_sync_status, int *err_code)
4886 {
4887         EM_DEBUG_FUNC_BEGIN("account_id[%d], result_sync_status [%p], err_code[%p]", account_id, result_sync_status, err_code);
4888
4889         if (!result_sync_status) {
4890                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4891                 if (err_code != NULL)
4892                         *err_code = EMAIL_ERROR_INVALID_PARAM;
4893                 return false;
4894         }
4895
4896         int error = EMAIL_ERROR_NONE, rc, ret = false, sync_status = 0, count, i, col_index;
4897         char sql_query_string[QUERY_SIZE] = {0, };
4898         char **result = NULL;
4899         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4900
4901         if (account_id)
4902                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl WHERE account_id = %d", account_id);
4903         else
4904                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT sync_status FROM mail_account_tbl");
4905
4906         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
4907         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
4908                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
4909
4910         if (!count) {
4911                 EM_DEBUG_EXCEPTION("no matched account found...");
4912                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4913                 goto FINISH_OFF;
4914         }
4915
4916         col_index = 1;
4917         *result_sync_status = 0;
4918
4919         for (i = 0; i < count; i++) {
4920                 _get_table_field_data_int(result, &sync_status, col_index++);
4921                 *result_sync_status |= sync_status;
4922         }
4923
4924         EM_DEBUG_LOG("sync_status [%d]", sync_status);
4925
4926         sqlite3_free_table(result);
4927
4928         ret = true;
4929
4930 FINISH_OFF:
4931
4932
4933         if (err_code != NULL)
4934                 *err_code = error;
4935
4936         EM_DEBUG_FUNC_END("ret [%d]", ret);
4937         return ret;
4938 }
4939
4940 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)
4941 {
4942         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);
4943
4944         int error = EMAIL_ERROR_NONE, rc, ret = false, set_value = sync_status, result_sync_status;
4945         char sql_query_string[QUERY_SIZE] = {0, };
4946         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
4947
4948         if (set_operator != SET_TYPE_SET && account_id) {
4949                 if (!emstorage_get_sync_status_of_account(multi_user_name, account_id, &result_sync_status, &error)) {
4950                         EM_DEBUG_EXCEPTION("emstorage_get_sync_status_of_account failed [%d]", error);
4951                         if (err_code != NULL)
4952                                 *err_code = error;
4953                         return false;
4954                 }
4955                 switch (set_operator) {
4956                 case SET_TYPE_UNION:
4957                         set_value = result_sync_status | set_value;
4958                         break;
4959                 case SET_TYPE_MINUS:
4960                         set_value = result_sync_status & (~set_value);
4961                         break;
4962                 default:
4963                         EM_DEBUG_EXCEPTION("EMAIL_ERROR_NOT_SUPPORTED [%d]", set_operator);
4964                         error = EMAIL_ERROR_NOT_SUPPORTED;
4965                         break;
4966                 }
4967                 EM_DEBUG_LOG("set_value [%d]", set_value);
4968         }
4969
4970         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
4971
4972         if (account_id)
4973                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE account_id = %d", set_value, account_id);
4974         else
4975                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_account_tbl SET sync_status = %d WHERE incoming_server_type <> 5", set_value);
4976
4977         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
4978
4979         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
4980         if (error != EMAIL_ERROR_NONE) {
4981                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
4982                 goto FINISH_OFF;
4983         }
4984
4985         rc = sqlite3_changes(local_db_handle);
4986
4987         if (rc == 0) {
4988                 EM_DEBUG_EXCEPTION("no matched account found...");
4989                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
4990                 goto FINISH_OFF;
4991         }
4992
4993         ret = true;
4994
4995 FINISH_OFF:
4996         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
4997
4998         if (error == EMAIL_ERROR_NONE) {
4999                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_UPDATE_SYNC_STATUS, account_id, set_value, NULL, 0))
5000                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_UPDATE_SYNC_STATUS] : Notification failed");
5001         }
5002
5003         if (err_code != NULL)
5004                 *err_code = error;
5005
5006         EM_DEBUG_FUNC_END("ret [%d]", ret);
5007         return ret;
5008 }
5009
5010 INTERNAL_FUNC int emstorage_add_account(char *multi_user_name, emstorage_account_tbl_t *account_tbl, int transaction, int *err_code)
5011 {
5012         EM_DEBUG_FUNC_BEGIN("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
5013
5014         if (!account_tbl) {
5015                 EM_DEBUG_EXCEPTION("account[%p], transaction[%d], err_code[%p]", account_tbl, transaction, err_code);
5016                 if (err_code != NULL)
5017                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5018                 return false;
5019         }
5020
5021         int row_count = 0;
5022         int i = 0;
5023         int rc = -1, ret = false;
5024         int error = EMAIL_ERROR_NONE;
5025         DB_STMT hStmt = NULL;
5026         char sql_query_string[QUERY_SIZE] = {0, };
5027         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5028         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5029
5030         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5031
5032         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5033
5034         char *sql = "SELECT rowid FROM mail_account_tbl;";
5035         char **result = NULL;
5036
5037         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, &row_count, NULL, NULL), rc);
5038         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
5039                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
5040
5041         if (row_count == 0 || NULL == result[1]) rc = 1;
5042         else {
5043                 for (i = 1; i <= row_count; i++) {
5044                         if (i != atoi(result[i]))
5045                                 break;
5046                 }
5047
5048                 rc = i;
5049         }
5050         sqlite3_free_table(result);
5051         result = NULL;
5052
5053         if (rc < 0 || rc > EMAIL_ACCOUNT_MAX) {
5054                 EM_DEBUG_EXCEPTION("OVERFLOWED THE MAX ACCOUNT");
5055                 error = EMAIL_ERROR_ACCOUNT_MAX_COUNT;
5056                 goto FINISH_OFF;
5057         }
5058
5059         account_tbl->account_id = rc;
5060
5061         if ((error = _get_password_file_name(multi_user_name, account_tbl->account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
5062                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
5063                 goto FINISH_OFF;
5064         }
5065
5066         EM_DEBUG_LOG(" >>>> ACCOUNT_ID [ %d ] ", account_tbl->account_id);
5067         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5068                         "INSERT INTO mail_account_tbl VALUES "
5069                         "(        "
5070                         "    ? "  /*   account_id */
5071                         "  , ? "  /*   account_name */
5072                         "  , ? "  /*   logo_icon_path */
5073                         "  , ? "  /*   user_data */
5074                         "  , ? "  /*   user_data_length */
5075                         "  , ? "  /*   account_svc_id */
5076                         "  , ? "  /*   sync_status */
5077                         "  , ? "  /*   sync_disabled */
5078                         "  , ? "  /*   default_mail_slot_size */
5079                         "  , ? "  /*   roaming_option */
5080                         "  , ? "  /*   color_label */
5081                         "  , ? "  /*   user_display_name */
5082                         "  , ? "  /*   user_email_address */
5083                         "  , ? "  /*   reply_to_address */
5084                         "  , ? "  /*   return_address */
5085                         "  , ? "  /*   incoming_server_type */
5086                         "  , ? "  /*   incoming_server_address */
5087                         "  , ? "  /*   incoming_server_port_number */
5088                         "  , ? "  /*   incoming_server_user_name */
5089                         "  , ? "  /*   incoming_server_password */
5090                         "  , ? "  /*   incoming_server_secure_connection */
5091                         "  , ? "  /*   incoming_server_authentication_method */
5092                         "  , ? "  /*   retrieval_mode */
5093                         "  , ? "  /*   keep_mails_on_pop_server_after_download */
5094                         "  , ? "  /*   check_interval */
5095                         "  , ? "  /*   auto_download_size */
5096                         "  , ? "  /*   peak_interval */
5097                         "  , ? "  /*   peak_days */
5098                         "  , ? "  /*   peak_start_time */
5099                         "  , ? "  /*   peak_end_time */
5100                         "  , ? "  /*   outgoing_server_type */
5101                         "  , ? "  /*   outgoing_server_address */
5102                         "  , ? "  /*   outgoing_server_port_number */
5103                         "  , ? "  /*   outgoing_server_user_name */
5104                         "  , ? "  /*   outgoing_server_password */
5105                         "  , ? "  /*   outgoing_server_secure_connection */
5106                         "  , ? "  /*   outgoing_server_need_authentication */
5107                         "  , ? "  /*   outgoing_server_use_same_authenticator */
5108                         "  , ? "  /*   priority */
5109                         "  , ? "  /*   keep_local_copy */
5110                         "  , ? "  /*   req_delivery_receipt */
5111                         "  , ? "  /*   req_read_receipt */
5112                         "  , ? "  /*   download_limit */
5113                         "  , ? "  /*   block_address */
5114                         "  , ? "  /*   block_subject */
5115                         "  , ? "  /*   display_name_from */
5116                         "  , ? "  /*   reply_with_body */
5117                         "  , ? "  /*   forward_with_files */
5118                         "  , ? "  /*   add_myname_card */
5119                         "  , ? "  /*   add_signature */
5120                         "  , ? "  /*   signature */
5121                         "  , ? "  /*   add_my_address_to_bcc */
5122                         "  , ? "  /*   auto_resend_times */
5123                         "  , ? "  /*   outgoing_server_size_limit */
5124                         "  , ? "  /*   wifi_auto_download */
5125                         "  , ? "  /*   pop_before_smtp */
5126                         "  , ? "  /*   incoming_server_requires_apop */
5127                         "  , ? "  /*   smime_type */
5128                         "  , ? "  /*   certificate_path */
5129                         "  , ? "  /*   cipher_type */
5130                         "  , ? "  /*   digest_type */
5131                         "  , ? "  /*   notification_status */
5132                         "  , ? "  /*   vibrate_status */
5133                         "  , ? "  /*   display_content_status */
5134                         "  , ? "  /*   default_ringtone_status */
5135                         "  , ? "  /*   alert_ringtone_path */
5136                         "  , ? "  /*   user_name */
5137                         ") ");
5138
5139         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5140         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5141                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5142
5143         i = 0;
5144
5145         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_id);
5146         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->account_name, 0, ACCOUNT_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
5147         _bind_stmt_field_data_string(hStmt, i++, account_tbl->logo_icon_path, 0, LOGO_ICON_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5148         _bind_stmt_field_data_blob(hStmt, i++, account_tbl->user_data, account_tbl->user_data_length);
5149         _bind_stmt_field_data_int(hStmt, i++, account_tbl->user_data_length);
5150         _bind_stmt_field_data_int(hStmt, i++, account_tbl->account_svc_id);
5151         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_status);
5152         _bind_stmt_field_data_int(hStmt, i++, account_tbl->sync_disabled);
5153         _bind_stmt_field_data_int(hStmt, i++, account_tbl->default_mail_slot_size);
5154         _bind_stmt_field_data_int(hStmt, i++, account_tbl->roaming_option);
5155         _bind_stmt_field_data_int(hStmt, i++, account_tbl->color_label);
5156         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_display_name, 0, DISPLAY_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
5157         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->user_email_address, 0, EMAIL_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5158         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->reply_to_address, 0, REPLY_TO_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5159         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->return_address, 0, RETURN_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5160         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_type);
5161         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_address, 0, RECEIVING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5162         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_port_number);
5163         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->incoming_server_user_name, 0, USER_NAME_LEN_IN_MAIL_ACCOUNT_TBL);
5164         _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
5165         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_secure_connection);
5166         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_authentication_method);
5167         _bind_stmt_field_data_int(hStmt, i++, account_tbl->retrieval_mode);
5168         _bind_stmt_field_data_int(hStmt, i++, account_tbl->keep_mails_on_pop_server_after_download);
5169         _bind_stmt_field_data_int(hStmt, i++, account_tbl->check_interval);
5170         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_download_size);
5171         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_interval);
5172         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_days);
5173         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_start_time);
5174         _bind_stmt_field_data_int(hStmt, i++, account_tbl->peak_end_time);
5175         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_type);
5176         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_address, 0, SENDING_SERVER_ADDR_LEN_IN_MAIL_ACCOUNT_TBL);
5177         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_port_number);
5178         _bind_stmt_field_data_string(hStmt, i++, (char *)account_tbl->outgoing_server_user_name, 0, SENDING_USER_LEN_IN_MAIL_ACCOUNT_TBL);
5179         _bind_stmt_field_data_string(hStmt, i++, (char *)"", 0, SENDING_PASSWORD_LEN_IN_MAIL_ACCOUNT_TBL);
5180         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_secure_connection);
5181         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_need_authentication);
5182         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_use_same_authenticator);
5183         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.priority);
5184         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.keep_local_copy);
5185         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_delivery_receipt);
5186         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.req_read_receipt);
5187         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.download_limit);
5188         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_address);
5189         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.block_subject);
5190         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.display_name_from, 0, DISPLAY_NAME_FROM_LEN_IN_MAIL_ACCOUNT_TBL);
5191         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.reply_with_body);
5192         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.forward_with_files);
5193         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_myname_card);
5194         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_signature);
5195         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.signature, 0, SIGNATURE_LEN_IN_MAIL_ACCOUNT_TBL);
5196         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.add_my_address_to_bcc);
5197         _bind_stmt_field_data_int(hStmt, i++, account_tbl->auto_resend_times);
5198         _bind_stmt_field_data_int(hStmt, i++, account_tbl->outgoing_server_size_limit);
5199         _bind_stmt_field_data_int(hStmt, i++, account_tbl->wifi_auto_download);
5200         _bind_stmt_field_data_int(hStmt, i++, account_tbl->pop_before_smtp);
5201         _bind_stmt_field_data_int(hStmt, i++, account_tbl->incoming_server_requires_apop);
5202         _bind_stmt_field_data_int(hStmt, i++, account_tbl->smime_type);
5203         _bind_stmt_field_data_string(hStmt, i++, account_tbl->certificate_path, 0, FILE_NAME_LEN_IN_MAIL_CERTIFICATE_TBL);
5204         _bind_stmt_field_data_int(hStmt, i++, account_tbl->cipher_type);
5205         _bind_stmt_field_data_int(hStmt, i++, account_tbl->digest_type);
5206         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.notification_status);
5207         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.vibrate_status);
5208         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.display_content_status);
5209         _bind_stmt_field_data_int(hStmt, i++, account_tbl->options.default_ringtone_status);
5210         _bind_stmt_field_data_string(hStmt, i++, account_tbl->options.alert_ringtone_path, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5211         _bind_stmt_field_data_string(hStmt, i++, account_tbl->user_name, 0, CERTIFICATE_PATH_LEN_IN_MAIL_ACCOUNT_TBL);
5212
5213         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5214
5215         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
5216                         ("sqlite3_step fail:%d", rc));
5217         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5218                         ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
5219
5220
5221         /*  save passwords to the secure storage */
5222         EM_DEBUG_LOG_SEC("save to the secure storage : recv_file[%s], send_file[%s]", recv_password_file_name, send_password_file_name);
5223         error = emcore_add_password_in_key_manager(recv_password_file_name, account_tbl->incoming_server_password);
5224         if (error != EMAIL_ERROR_NONE) {
5225                 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
5226                 goto FINISH_OFF;
5227         }
5228
5229         error = emcore_add_password_in_key_manager(send_password_file_name, account_tbl->outgoing_server_password);
5230         if (error != EMAIL_ERROR_NONE) {
5231                 EM_DEBUG_EXCEPTION("emcore_add_password_in_key_manager failed : [%d]", error);
5232                 goto FINISH_OFF;
5233         }
5234
5235         ret = true;
5236
5237 FINISH_OFF:
5238         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5239
5240         if (hStmt != NULL) {
5241                 rc = sqlite3_finalize(hStmt);
5242                 if (rc != SQLITE_OK) {
5243                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5244                         error = EMAIL_ERROR_DB_FAILURE;
5245                 }
5246         }
5247
5248         if (error == EMAIL_ERROR_NONE) {
5249                 if (!emcore_notify_storage_event(NOTI_ACCOUNT_ADD, account_tbl->account_id, 0, NULL, 0))
5250                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[NOTI_ACCOUNT_ADD] : Notification failed");
5251         }
5252
5253         if (err_code != NULL)
5254                 *err_code = error;
5255
5256         EM_DEBUG_FUNC_END("ret [%d]", ret);
5257         return ret;
5258 }
5259
5260 INTERNAL_FUNC int emstorage_delete_account(char *multi_user_name, int account_id, int transaction, int *err_code)
5261 {
5262         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
5263
5264         if (account_id < FIRST_ACCOUNT_ID) {
5265                 EM_DEBUG_EXCEPTION(" account_id[%d]", account_id);
5266
5267                 if (err_code != NULL)
5268                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5269                 return false;
5270         }
5271
5272         int rc = -1, ret = false;
5273         int error = EMAIL_ERROR_NONE;
5274
5275         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5276
5277         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5278
5279         /*  TODO : delete password files - file names can be obtained from db or a rule that makes a name */
5280         char sql_query_string[QUERY_SIZE] = {0, };
5281         char recv_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5282         char send_password_file_name[MAX_PW_FILE_NAME_LENGTH];
5283
5284         /*  get password file name */
5285         if ((error = _get_password_file_name(multi_user_name, account_id, recv_password_file_name, send_password_file_name)) != EMAIL_ERROR_NONE) {
5286                 EM_DEBUG_EXCEPTION("_get_password_file_name failed.");
5287                 goto FINISH_OFF;
5288         }
5289
5290         /*  delete from db */
5291         memset(sql_query_string, 0x00, sizeof(sql_query_string));
5292         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_account_tbl WHERE account_id = %d", account_id);
5293         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5294         if (error != EMAIL_ERROR_NONE) {
5295                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5296                 goto FINISH_OFF;
5297         }
5298
5299         /*  validate account existence */
5300         rc = sqlite3_changes(local_db_handle);
5301         if (rc == 0) {
5302                 EM_DEBUG_EXCEPTION(" no matched account found...");
5303                 error = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
5304                 goto FINISH_OFF;
5305         }
5306
5307         /*  delete from secure storage */
5308         error = emcore_remove_password_in_key_manager(recv_password_file_name);
5309         if (error != EMAIL_ERROR_NONE) {
5310                 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5311                 goto FINISH_OFF;
5312         }
5313
5314         error = emcore_remove_password_in_key_manager(send_password_file_name);
5315         if (error != EMAIL_ERROR_NONE) {
5316                 EM_DEBUG_EXCEPTION("emcore_remove_password_in_key_manager failed : [%d]", error);
5317                 goto FINISH_OFF;
5318         }
5319                                         /* LCOV_EXCL_STOP */
5320         ret = true;
5321
5322 FINISH_OFF:
5323         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
5324
5325         if (err_code != NULL)
5326                 *err_code = error;
5327
5328         EM_DEBUG_FUNC_END("ret [%d]", ret);
5329         return ret;
5330 }
5331
5332
5333 INTERNAL_FUNC int emstorage_free_account(emstorage_account_tbl_t** account_list, int count, int *err_code)
5334 {
5335         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%d], err_code[%p]", account_list, count, err_code);
5336
5337         int ret = false;
5338         int error = EMAIL_ERROR_NONE;
5339
5340         if (count > 0) {
5341                 if (!account_list || !*account_list) {
5342                         EM_DEBUG_EXCEPTION("account_list[%p], count[%d]", account_list, count);
5343                         error = EMAIL_ERROR_INVALID_PARAM;
5344                         goto FINISH_OFF;
5345                 }
5346
5347                 emstorage_account_tbl_t* p = *account_list;
5348                 int i = 0;
5349
5350                 for (; i < count; i++) {
5351                         EM_SAFE_FREE(p[i].account_name);
5352                         EM_SAFE_FREE(p[i].incoming_server_address);
5353                         EM_SAFE_FREE(p[i].user_email_address);
5354                         EM_SAFE_FREE(p[i].user_data);
5355                         EM_SAFE_FREE(p[i].incoming_server_user_name);
5356                         EM_SAFE_FREE(p[i].incoming_server_password);
5357                         EM_SAFE_FREE(p[i].outgoing_server_address);
5358                         EM_SAFE_FREE(p[i].outgoing_server_user_name);
5359                         EM_SAFE_FREE(p[i].outgoing_server_password);
5360                         EM_SAFE_FREE(p[i].user_display_name);
5361                         EM_SAFE_FREE(p[i].reply_to_address);
5362                         EM_SAFE_FREE(p[i].return_address);
5363                         EM_SAFE_FREE(p[i].logo_icon_path);
5364                         EM_SAFE_FREE(p[i].options.display_name_from);
5365                         EM_SAFE_FREE(p[i].options.signature);
5366                         EM_SAFE_FREE(p[i].options.alert_ringtone_path);
5367                         EM_SAFE_FREE(p[i].certificate_path);
5368                         EM_SAFE_FREE(p[i].user_name);
5369                 }
5370
5371                 EM_SAFE_FREE(p);
5372                 *account_list = NULL;
5373         }
5374
5375         ret = true;
5376
5377 FINISH_OFF:
5378         if (err_code != NULL)
5379                 *err_code = error;
5380
5381         EM_DEBUG_FUNC_END("ret [%d]", ret);
5382         return ret;
5383 }
5384                                         /* LCOV_EXCL_START */
5385 INTERNAL_FUNC int emstorage_get_mailbox_count(char *multi_user_name, int account_id, int local_yn, int *count, int transaction, int *err_code)
5386 {
5387         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);
5388
5389         if ((account_id < FIRST_ACCOUNT_ID) || (count == NULL)) {
5390                 EM_DEBUG_EXCEPTION(" account_list[%d], local_yn[%d], count[%p]", account_id, local_yn, count);
5391
5392                 if (err_code != NULL)
5393                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5394                 return false;
5395         }
5396
5397         int rc = -1, ret = false;
5398         int error = EMAIL_ERROR_NONE;
5399         char sql_query_string[QUERY_SIZE] = {0, };
5400
5401         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5402         EMSTORAGE_START_READ_TRANSACTION(transaction);
5403
5404         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);
5405
5406         char **result;
5407
5408         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
5409         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
5410                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5411
5412         *count = atoi(result[1]);
5413         sqlite3_free_table(result);
5414
5415
5416         ret = true;
5417
5418 FINISH_OFF:
5419
5420         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5421
5422         if (err_code != NULL)
5423                 *err_code = error;
5424
5425         EM_DEBUG_FUNC_END("ret [%d]", ret);
5426         return ret;
5427 }
5428
5429 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)
5430 {
5431         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);
5432
5433         if (!select_num || !mailbox_list) {
5434                 EM_DEBUG_EXCEPTION("Invalid parameters");
5435
5436                 if (err_code != NULL)
5437                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5438
5439                 return false;
5440         }
5441
5442         int ret = false;
5443         int error = EMAIL_ERROR_NONE;
5444         char conditional_clause_string[QUERY_SIZE] = {0, };
5445         char ordering_clause_string[QUERY_SIZE] = {0, };
5446
5447         if (account_id == ALL_ACCOUNT) {
5448                 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5449                         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);
5450         } else {
5451                 SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d  ", account_id);
5452                 if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL)
5453                         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);
5454         }
5455
5456         EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5457
5458         switch (sort_type) {
5459         case EMAIL_MAILBOX_SORT_BY_NAME_ASC:
5460                 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name ASC");
5461                 break;
5462
5463         case EMAIL_MAILBOX_SORT_BY_NAME_DSC:
5464                 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_name DESC");
5465                 break;
5466
5467         case EMAIL_MAILBOX_SORT_BY_TYPE_ASC:
5468                 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type ASC");
5469                 break;
5470
5471         case EMAIL_MAILBOX_SORT_BY_TYPE_DSC:
5472                 SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY mailbox_type DEC");
5473                 break;
5474         }
5475
5476         EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5477
5478         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 0, transaction, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5479                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5480                 goto FINISH_OFF;
5481         }
5482
5483         ret = true;
5484
5485 FINISH_OFF:
5486
5487         if (err_code != NULL)
5488                 *err_code = error;
5489
5490         EM_DEBUG_FUNC_END("ret [%d]", ret);
5491         return ret;
5492 }
5493
5494 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)
5495 {
5496         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);
5497
5498         int ret = false;
5499         int error = EMAIL_ERROR_NONE;
5500         int where_clause_count = 0;
5501         char conditional_clause_string[QUERY_SIZE] = {0, };
5502         char ordering_clause_string[QUERY_SIZE] = {0, };
5503
5504         if (!select_num || !mailbox_list) {
5505                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5506                 error = EMAIL_ERROR_INVALID_PARAM;
5507                 goto FINISH_OFF;
5508         }
5509
5510         if (local_yn == EMAIL_MAILBOX_FROM_SERVER || local_yn == EMAIL_MAILBOX_FROM_LOCAL) {
5511                 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);
5512                 where_clause_count++;
5513         }
5514
5515         if (account_id > 0) {
5516                 if (where_clause_count == 0) {
5517                         SNPRINTF(conditional_clause_string + EM_SAFE_STRLEN(conditional_clause_string), sizeof(conditional_clause_string)-(EM_SAFE_STRLEN(conditional_clause_string)+1), " WHERE ");
5518                         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);
5519                 } else
5520                         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);
5521         }
5522
5523         SNPRINTF(ordering_clause_string, QUERY_SIZE, " ORDER BY CASE WHEN MBT.mailbox_name"
5524                         " GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\'"
5525                         " THEN 2 ELSE 1 END ASC,"
5526                         " MBT.mailbox_name COLLATE CONVERTUTF8 ASC ");
5527         EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5528         EM_DEBUG_LOG("ordering_clause_string[%s]", ordering_clause_string);
5529
5530         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, ordering_clause_string, 1, 1, mailbox_list, select_num)) != EMAIL_ERROR_NONE) {
5531                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5532                 goto FINISH_OFF;
5533         }
5534
5535         ret = true;
5536
5537 FINISH_OFF:
5538
5539         if (err_code != NULL)
5540                 *err_code = error;
5541
5542         EM_DEBUG_FUNC_END("ret [%d]", ret);
5543         return ret;
5544 }
5545
5546 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)
5547 {
5548         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);
5549
5550         int ret = false;
5551         int error = EMAIL_ERROR_NONE;
5552         char conditional_clause_string[QUERY_SIZE] = {0, };
5553
5554         if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list || !parent_mailbox_name) {
5555                 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);
5556                 if (err_code != NULL)
5557                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5558                 return false;
5559         }
5560
5561         sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string, "WHERE account_id = %d  AND (UPPER(mailbox_name) = UPPER('%q') OR UPPER(mailbox_name) LIKE UPPER('%q/%%'))", account_id, parent_mailbox_name, parent_mailbox_name);
5562         EM_DEBUG_LOG("conditional_clause_string[%s]", conditional_clause_string);
5563
5564         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) {
5565                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5566                 goto FINISH_OFF;
5567         }
5568
5569         ret = true;
5570
5571 FINISH_OFF:
5572
5573
5574         if (err_code != NULL)
5575                 *err_code = error;
5576
5577         EM_DEBUG_FUNC_END("ret [%d]", ret);
5578         return ret;
5579 }
5580
5581 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)
5582 {
5583         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);
5584         if (account_id < FIRST_ACCOUNT_ID || !select_num || !mailbox_list) {
5585                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5586
5587                 if (err_code != NULL)
5588                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5589
5590                 return false;
5591         }
5592
5593         int ret = false;
5594         int error = EMAIL_ERROR_NONE;
5595         char conditional_clause_string[QUERY_SIZE] = {0, };
5596
5597         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE account_id = %d AND modifiable_yn = 0", account_id);
5598         EM_DEBUG_LOG("conditional_clause_string [%s]", conditional_clause_string);
5599
5600         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) {
5601                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5602                 goto FINISH_OFF;
5603         }
5604
5605         ret = true;
5606
5607 FINISH_OFF:
5608
5609         if (err_code != NULL)
5610                 *err_code = error;
5611
5612         EM_DEBUG_FUNC_END("ret [%d]", ret);
5613         return ret;
5614 }
5615
5616 INTERNAL_FUNC int emstorage_stamp_last_sync_time_of_mailbox(char *multi_user_name, int input_mailbox_id, int input_transaction)
5617 {
5618         EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_transaction [%d]", input_mailbox_id, input_transaction);
5619
5620         int      result_code = false;
5621         int      error = EMAIL_ERROR_NONE;
5622         time_t   current_time = 0;
5623         char     sql_query_string[QUERY_SIZE] = {0, };
5624         sqlite3 *local_db_handle = NULL;
5625
5626         if (!input_mailbox_id) {
5627                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5628                 return EMAIL_ERROR_INVALID_PARAM;
5629         }
5630
5631         time(&current_time);
5632
5633         local_db_handle = emstorage_get_db_connection(multi_user_name);
5634         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
5635
5636         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5637                         "UPDATE mail_box_tbl SET"
5638                         " last_sync_time = %d"
5639                         " WHERE mailbox_id = %d"
5640                         , (int)current_time
5641                         , input_mailbox_id);
5642
5643         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
5644         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
5645         if (error != EMAIL_ERROR_NONE) {
5646                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
5647                 goto FINISH_OFF;
5648         }
5649
5650 FINISH_OFF:
5651
5652         if (error == EMAIL_ERROR_NONE)
5653                 result_code = true;
5654
5655         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, result_code, error);
5656
5657         EM_DEBUG_FUNC_END("error [%d]", error);
5658         return error;
5659 }
5660
5661 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)
5662 {
5663         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);
5664         EM_PROFILE_BEGIN(profile_emstorage_get_mailbox_by_name);
5665
5666         if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !result_mailbox) {
5667                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], mailbox_name[%s], result_mailbox[%p]", account_id, local_yn, mailbox_name, result_mailbox);
5668
5669                 if (err_code != NULL)
5670                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5671                 return false;
5672         }
5673
5674         int ret = false;
5675         int error = EMAIL_ERROR_NONE;
5676         int result_count = 0;
5677         char conditional_clause_string[QUERY_SIZE] = {0, };
5678
5679         if (strcmp(mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
5680                 if (!(*result_mailbox = (emstorage_mailbox_tbl_t*)em_malloc(sizeof(emstorage_mailbox_tbl_t)))) {
5681                         EM_DEBUG_EXCEPTION("malloc failed...");
5682                         error = EMAIL_ERROR_OUT_OF_MEMORY;
5683                         goto FINISH_OFF;
5684                 }
5685
5686                 (*result_mailbox)->mailbox_id                 = 0;
5687                 (*result_mailbox)->account_id                 = account_id;
5688                 (*result_mailbox)->local_yn                   = 1;
5689                 (*result_mailbox)->mailbox_name               = EM_SAFE_STRDUP(mailbox_name);
5690                 (*result_mailbox)->mailbox_type               = EMAIL_MAILBOX_TYPE_SEARCH_RESULT;
5691                 (*result_mailbox)->alias                      = EM_SAFE_STRDUP(mailbox_name);
5692                 (*result_mailbox)->deleted_flag               = 0;
5693                 (*result_mailbox)->modifiable_yn              = 1;
5694                 (*result_mailbox)->total_mail_count_on_server = 1;
5695                 (*result_mailbox)->has_archived_mails         = 0;
5696                 (*result_mailbox)->mail_slot_size             = 0x0FFFFFFF;
5697                 (*result_mailbox)->no_select                  = 0;
5698         } else {
5699                 if (local_yn == -1)
5700                         sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string, "WHERE account_id = %d AND mailbox_name = '%q'", account_id, mailbox_name);
5701                 else
5702                         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);
5703
5704                 EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5705
5706                 if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5707                         EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5708                         goto FINISH_OFF;
5709                 }
5710         }
5711                                         /* LCOV_EXCL_STOP */
5712         ret = true;
5713
5714 FINISH_OFF:
5715
5716         if (err_code != NULL)
5717                 *err_code = error;
5718
5719         EM_PROFILE_END(profile_emstorage_get_mailbox_by_name);
5720         EM_DEBUG_FUNC_END("ret [%d]", ret);
5721         return ret;
5722 }
5723
5724 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)
5725 {
5726         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);
5727
5728         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_USER_DEFINED) || !output_mailbox) {
5729                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_type[%d], output_mailbox[%p]", account_id, mailbox_type, output_mailbox);
5730                 if (err_code != NULL)
5731                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5732                 return false;
5733         }
5734
5735         int ret = false;
5736         int error = EMAIL_ERROR_NONE;
5737         int result_count = 0;
5738         emstorage_mailbox_tbl_t *result_mailbox = NULL;
5739         char conditional_clause_string[QUERY_SIZE] = {0,};
5740
5741
5742         SNPRINTF(conditional_clause_string, QUERY_SIZE, "WHERE account_id = %d AND mailbox_type = %d ", account_id, mailbox_type);
5743
5744         EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5745
5746         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", true, false, &result_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5747                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl error [%d]", error);
5748                 goto FINISH_OFF;
5749         }
5750
5751         ret = true;
5752
5753 FINISH_OFF:
5754         if (ret == true)
5755                 *output_mailbox = result_mailbox;
5756
5757         if (err_code != NULL)
5758                 *err_code = error;
5759
5760         EM_DEBUG_FUNC_END("ret [%d]", ret);
5761         return ret;
5762 }
5763                                         /* LCOV_EXCL_START */
5764 INTERNAL_FUNC int emstorage_get_mailbox_by_id(char *multi_user_name, int input_mailbox_id, emstorage_mailbox_tbl_t** output_mailbox)
5765 {
5766         EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], output_mailbox[%p]", input_mailbox_id, output_mailbox);
5767
5768         if (input_mailbox_id <= 0 || !output_mailbox) {
5769                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM: input_mailbox_id[%d] output_mailbox[%p]", input_mailbox_id, output_mailbox);
5770                 return  EMAIL_ERROR_INVALID_PARAM;
5771         }
5772
5773         int  ret = EMAIL_ERROR_NONE;
5774         int  result_count = 0;
5775         char conditional_clause_string[QUERY_SIZE] = {0, };
5776
5777         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE MBT.mailbox_id = %d", input_mailbox_id);
5778
5779         EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5780
5781         if ((ret = emstorage_query_mailbox_tbl(multi_user_name,
5782                                         conditional_clause_string,
5783                                         "", true, false, output_mailbox, &result_count)) != EMAIL_ERROR_NONE) {
5784                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", ret);
5785                 goto FINISH_OFF;
5786         }
5787
5788 FINISH_OFF:
5789
5790         EM_DEBUG_FUNC_END("ret [%d]", ret);
5791         return ret;
5792 }
5793
5794 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)
5795 {
5796         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);
5797
5798         if (account_id < 0 || !keyword || !result_mailbox) {
5799                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], keyword[%s], result_mailbox[%p]", account_id, keyword, result_mailbox);
5800
5801                 if (err_code != NULL)
5802                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5803                 return false;
5804         }
5805
5806         int ret = false;
5807         int error = EMAIL_ERROR_NONE;
5808         char conditional_clause_string[QUERY_SIZE] = {0, };
5809
5810         if (account_id == 0)
5811                 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5812                                 "WHERE alias LIKE \'%%%q%%\'", keyword);
5813         else if (account_id > 0)
5814                 sqlite3_snprintf(sizeof(conditional_clause_string), conditional_clause_string,
5815                                 "WHERE account_id = %d AND alias LIKE \'%%%q%%\'", account_id, keyword);
5816
5817         EM_DEBUG_LOG("conditional_clause_string = [%s]", conditional_clause_string);
5818
5819         if ((error = emstorage_query_mailbox_tbl(multi_user_name, conditional_clause_string, "", 0, transaction, result_mailbox, result_count)) != EMAIL_ERROR_NONE) {
5820                 EM_DEBUG_EXCEPTION("emstorage_query_mailbox_tbl failed [%d]", error);
5821                 goto FINISH_OFF;
5822         }
5823
5824         ret = true;
5825
5826 FINISH_OFF:
5827
5828         if (err_code != NULL)
5829                 *err_code = error;
5830
5831         EM_DEBUG_FUNC_END("ret [%d]", ret);
5832         return ret;
5833 }
5834
5835 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)
5836 {
5837         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);
5838         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_id) {
5839                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_id[%p]", account_id, mailbox_type, mailbox_id);
5840                 if (err_code != NULL)
5841                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5842                 return false;
5843         }
5844
5845         int rc, ret = false;
5846         int error = EMAIL_ERROR_NONE;
5847         emstorage_account_tbl_t* account = NULL;
5848         DB_STMT hStmt = NULL;
5849         char sql_query_string[QUERY_SIZE] = {0, };
5850
5851         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5852         EMSTORAGE_START_READ_TRANSACTION(transaction);
5853
5854         /*  Check whether the account exists. */
5855         if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
5856                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5857                 goto FINISH_OFF;
5858         }
5859
5860         if (account)
5861                 emstorage_free_account(&account, 1, NULL);
5862
5863         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);
5864
5865         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5866
5867
5868         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5869
5870         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5871                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5872
5873
5874         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5875         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5876                         ("sqlite3_step fail:%d", rc));
5877
5878         if (rc == SQLITE_DONE) {
5879                 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5880                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5881                 goto FINISH_OFF;
5882         }
5883
5884         _get_stmt_field_data_int(hStmt, mailbox_id, 0);
5885
5886         ret = true;
5887
5888 FINISH_OFF:
5889         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5890         if (hStmt != NULL) {
5891                 rc = sqlite3_finalize(hStmt);
5892                 if (rc != SQLITE_OK) {
5893                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5894                         error = EMAIL_ERROR_DB_FAILURE;
5895                 }
5896         }
5897
5898         if (err_code != NULL)
5899                 *err_code = error;
5900
5901         EM_DEBUG_FUNC_END("ret [%d]", ret);
5902         return ret;
5903 }
5904
5905 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)
5906 {
5907         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);
5908         if (account_id < FIRST_ACCOUNT_ID || (mailbox_type < EMAIL_MAILBOX_TYPE_INBOX || mailbox_type > EMAIL_MAILBOX_TYPE_ALL_EMAILS) || !mailbox_name) {
5909                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_type[%d], mailbox_name[%p]", account_id, mailbox_type, mailbox_name);
5910                 if (err_code != NULL)
5911                         *err_code = EMAIL_ERROR_INVALID_PARAM;
5912                 return false;
5913         }
5914
5915         int rc, ret = false;
5916         int error = EMAIL_ERROR_NONE;
5917         emstorage_account_tbl_t* account = NULL;
5918         DB_STMT hStmt = NULL;
5919         char sql_query_string[QUERY_SIZE] = {0, };
5920
5921         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5922         EMSTORAGE_START_READ_TRANSACTION(transaction);
5923
5924         /*  Check whether the account exists. */
5925         if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_ACCOUNT_NAME,  &account, true, &error) || !account) {
5926                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed - %d", error);
5927                 goto FINISH_OFF;
5928         }
5929
5930         if (account)
5931                 emstorage_free_account(&account, 1, NULL);
5932
5933         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);
5934
5935         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
5936
5937
5938         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
5939
5940         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5941                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
5942
5943
5944         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
5945         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
5946                         ("sqlite3_step fail:%d", rc));
5947
5948         if (rc == SQLITE_DONE) {
5949                 EM_DEBUG_EXCEPTION("no matched mailbox_name found...");
5950                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
5951                 goto FINISH_OFF;
5952         }
5953
5954         _get_stmt_field_data_string(hStmt, mailbox_name, 0, 0);
5955
5956         ret = true;
5957
5958 FINISH_OFF:
5959         if (hStmt != NULL) {
5960                 rc = sqlite3_finalize(hStmt);
5961                 if (rc != SQLITE_OK) {
5962                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
5963                         error = EMAIL_ERROR_DB_FAILURE;
5964                 }
5965         }
5966
5967         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
5968
5969         if (err_code != NULL)
5970                 *err_code = error;
5971
5972         EM_DEBUG_FUNC_END("ret [%d]", ret);
5973         return ret;
5974 }
5975
5976 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)
5977 {
5978         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);
5979         int ret = false;
5980         int error = EMAIL_ERROR_NONE;
5981         char sql_query_string[QUERY_SIZE] = {0, };
5982         char *replaced_mailbox_name = NULL;
5983
5984         if (mailbox_name) {
5985                 if (strstr(mailbox_name, "'"))
5986                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
5987                 else
5988                         replaced_mailbox_name = strdup(mailbox_name);
5989         }
5990
5991         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
5992
5993         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
5994
5995         SNPRINTF(sql_query_string, sizeof(sql_query_string),
5996                         "UPDATE mail_box_tbl SET"
5997                         " modifiable_yn = %d"
5998                         " WHERE account_id = %d"
5999                         " AND local_yn = %d"
6000                         " AND mailbox_name = '%s'"
6001                         , modifiable_yn
6002                         , account_id
6003                         , local_yn
6004                         , replaced_mailbox_name);
6005
6006         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6007         if (error != EMAIL_ERROR_NONE) {
6008                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6009                 goto FINISH_OFF;
6010         }
6011
6012         ret = true;
6013
6014 FINISH_OFF:
6015         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6016
6017         EM_SAFE_FREE(replaced_mailbox_name);
6018
6019         if (err_code != NULL)
6020                 *err_code = error;
6021
6022         EM_DEBUG_FUNC_END("ret [%d]", ret);
6023         return ret;
6024
6025 }
6026
6027 INTERNAL_FUNC int emstorage_update_mailbox_total_count(char *multi_user_name,
6028                 int account_id,
6029                 int input_mailbox_id,
6030                 int total_count_on_server,
6031                 int transaction,
6032                 int *err_code)
6033 {
6034         EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], total_count_on_server[%d], "
6035                         "transaction[%d], err_code[%p]",
6036                         account_id, input_mailbox_id, total_count_on_server,  transaction, err_code);
6037
6038         int ret = false;
6039         int error = EMAIL_ERROR_NONE;
6040         char sql_query_string[QUERY_SIZE] = {0, };
6041
6042         if (account_id <= 0 || input_mailbox_id <= 0) {
6043                 EM_DEBUG_EXCEPTION("account_id[%d], input_mailbox_id[%d]", account_id, input_mailbox_id);
6044                 if (err_code != NULL)
6045                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6046                 EM_DEBUG_FUNC_END("ret [%d]", ret);
6047                 return false;
6048         }
6049
6050         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6051
6052         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6053
6054         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6055                         "UPDATE mail_box_tbl SET"
6056                         " total_mail_count_on_server = %d"
6057                         " WHERE account_id = %d"
6058                         " AND mailbox_id = %d"
6059                         , total_count_on_server
6060                         , account_id
6061                         , input_mailbox_id);
6062
6063         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
6064         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6065         if (error != EMAIL_ERROR_NONE) {
6066                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6067                 goto FINISH_OFF;
6068         }
6069
6070         ret = true;
6071
6072 FINISH_OFF:
6073
6074         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6075
6076         if (err_code != NULL)
6077                 *err_code = error;
6078
6079         EM_DEBUG_FUNC_END("ret [%d]", ret);
6080         return ret;
6081
6082 }
6083
6084 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)
6085 {
6086         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);
6087
6088         if (account_id < FIRST_ACCOUNT_ID || input_mailbox_id <= 0 || !result_mailbox) {
6089                 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);
6090
6091                 if (err_code != NULL)
6092                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6093                 EM_DEBUG_FUNC_END("ret [%d]", EMAIL_ERROR_INVALID_PARAM);
6094                 return false;
6095         }
6096
6097         int rc, ret = false;
6098         int error = EMAIL_ERROR_NONE;
6099         char sql_query_string[QUERY_SIZE] = {0, };
6100         DB_STMT hStmt = NULL;
6101         int i = 0;
6102
6103         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6104         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6105
6106         if (local_yn != -1) {
6107                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6108                                 "UPDATE mail_box_tbl SET"
6109                                 "  mailbox_id = ?"
6110                                 ", mailbox_name = ?"
6111                                 ", mailbox_type = ?"
6112                                 ", alias = ?"
6113                                 ", deleted_flag = ?"
6114                                 ", modifiable_yn= ?"
6115                                 ", mail_slot_size= ?"
6116                                 ", total_mail_count_on_server = ?"
6117                                 " WHERE account_id = %d"
6118                                 " AND local_yn = %d"
6119                                 " AND mailbox_id = '%d'"
6120                                 , account_id
6121                                 , local_yn
6122                                 , input_mailbox_id);
6123         } else {
6124                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
6125                                 "UPDATE mail_box_tbl SET"
6126                                 "  mailbox_id = ?"
6127                                 ", mailbox_name = ?"
6128                                 ", mailbox_type = ?"
6129                                 ", alias = ?"
6130                                 ", deleted_flag = ?"
6131                                 ", modifiable_yn= ?"
6132                                 ", mail_slot_size= ?"
6133                                 ", total_mail_count_on_server = ?"
6134                                 " WHERE account_id = %d"
6135                                 " AND mailbox_id = '%d'"
6136                                 , account_id
6137                                 , input_mailbox_id);
6138         }
6139
6140
6141
6142         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6143         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6144                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6145
6146         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_id);
6147         _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);
6148         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mailbox_type);
6149         _bind_stmt_field_data_string(hStmt, i++, (char *)result_mailbox->alias ? result_mailbox->alias : "", 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
6150         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->deleted_flag);
6151         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->modifiable_yn);
6152         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->mail_slot_size);
6153         _bind_stmt_field_data_int(hStmt, i++, result_mailbox->total_mail_count_on_server);
6154
6155
6156         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6157         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
6158                         ("sqlite3_step fail:%d", rc));
6159         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6160                         ("sqlite3_step fail:%d", rc));
6161
6162         ret = true;
6163
6164 FINISH_OFF:
6165         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6166
6167         if (hStmt != NULL) {
6168                 rc = sqlite3_finalize(hStmt);
6169                 if (rc != SQLITE_OK) {
6170                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6171                         error = EMAIL_ERROR_DB_FAILURE;
6172                 }
6173         }
6174
6175         if (err_code != NULL)
6176                 *err_code = error;
6177
6178         EM_DEBUG_FUNC_END("ret [%d]", ret);
6179         return ret;
6180 }
6181
6182 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)
6183 {
6184         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);
6185
6186         if (account_id < FIRST_ACCOUNT_ID) {
6187                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
6188
6189                 if (err_code != NULL)
6190                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6191                 return false;
6192         }
6193
6194         int rc, ret = false;
6195         int error = EMAIL_ERROR_NONE;
6196         char sql_query_string[QUERY_SIZE] = {0, };
6197
6198         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6199         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6200
6201         EM_DEBUG_LOG("emstorage_update_mailbox_type");
6202
6203         DB_STMT hStmt_box_tbl = NULL;
6204         DB_STMT hStmt_mail_tbl = NULL;
6205         int i = 0;
6206
6207         /*  Update mail_box_tbl */
6208         if (local_yn != -1) {
6209                 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
6210                                 "UPDATE mail_box_tbl SET"
6211                                 " mailbox_type = ?"
6212                                 " WHERE account_id = %d"
6213                                 " AND local_yn = %d"
6214                                 " AND mailbox_id = '%d'"
6215                                 , account_id
6216                                 , local_yn
6217                                 , input_mailbox_id);
6218         } else {
6219                 SNPRINTF(sql_query_string, sizeof(sql_query_string)-1,
6220                                 "UPDATE mail_box_tbl SET"
6221                                 " mailbox_type = ?"
6222                                 " WHERE account_id = %d"
6223                                 " AND mailbox_id = '%d'"
6224                                 , account_id
6225                                 , input_mailbox_id);
6226         }
6227
6228         EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
6229
6230         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_box_tbl, NULL), rc);
6231
6232         if (SQLITE_OK != rc) {
6233                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6234                 error = EMAIL_ERROR_DB_FAILURE;
6235                 goto FINISH_OFF;
6236         }
6237
6238         _bind_stmt_field_data_int(hStmt_box_tbl, i++, new_mailbox_type);
6239
6240
6241         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_box_tbl), rc);
6242
6243         if (rc == SQLITE_FULL) {
6244                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6245                 error   = EMAIL_ERROR_MAIL_MEMORY_FULL;
6246                 goto FINISH_OFF;
6247         }
6248
6249         if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
6250                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6251                 error = EMAIL_ERROR_DB_FAILURE;
6252                 goto FINISH_OFF;
6253         }
6254
6255
6256         /*  Update mail_tbl */
6257         i = 0;
6258         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6259                         "UPDATE mail_tbl SET"
6260                         " mailbox_type = ?"
6261                         " WHERE account_id = %d"
6262                         " AND mailbox_id = '%d'"
6263                         , account_id
6264                         , input_mailbox_id);
6265
6266         EM_DEBUG_LOG_SEC("SQL[%s]", sql_query_string);
6267
6268         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt_mail_tbl, NULL), rc);
6269         if (SQLITE_OK != rc) {
6270                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
6271                 error = EMAIL_ERROR_DB_FAILURE;
6272                 goto FINISH_OFF;
6273         }
6274
6275         _bind_stmt_field_data_int(hStmt_mail_tbl, i++, new_mailbox_type);
6276
6277         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt_mail_tbl), rc);
6278         if (rc == SQLITE_FULL) {
6279                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6280                 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
6281                 goto FINISH_OFF;
6282         }
6283
6284         if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
6285                 EM_DEBUG_EXCEPTION("sqlite3_step fail:%d", rc);
6286                 error = EMAIL_ERROR_DB_FAILURE;
6287                 goto FINISH_OFF;
6288         }
6289
6290         ret = true;
6291
6292 FINISH_OFF:
6293         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6294
6295         if (hStmt_box_tbl != NULL) {
6296                 rc = sqlite3_finalize(hStmt_box_tbl);
6297                 if (rc != SQLITE_OK) {
6298                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6299                         error = EMAIL_ERROR_DB_FAILURE;
6300                 }
6301         }
6302
6303         if (hStmt_mail_tbl != NULL) {
6304                 rc = sqlite3_finalize(hStmt_mail_tbl);
6305                 if (rc != SQLITE_OK) {
6306                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6307                         error = EMAIL_ERROR_DB_FAILURE;
6308                 }
6309         }
6310
6311         if (err_code != NULL)
6312                 *err_code = error;
6313
6314         EM_DEBUG_FUNC_END("ret [%d]", ret);
6315         return ret;
6316 }
6317
6318 INTERNAL_FUNC int emstorage_set_local_mailbox(char *multi_user_name, int input_mailbox_id, int input_is_local_mailbox, int transaction)
6319 {
6320         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);
6321
6322         int rc, ret = false;
6323         int error = EMAIL_ERROR_NONE;
6324         char sql_query_string[QUERY_SIZE] = {0, };
6325
6326         if (input_mailbox_id < 0) {
6327                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6328                 return EMAIL_ERROR_INVALID_PARAM;
6329         }
6330
6331         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6332
6333         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6334
6335         EM_DEBUG_LOG("emstorage_update_mailbox_type");
6336
6337         DB_STMT hStmt = NULL;
6338         int i = 0;
6339
6340         /*  Update mail_box_tbl */
6341         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6342                         "UPDATE mail_box_tbl SET"
6343                         " local_yn = ?"
6344                         " WHERE mailbox_id = %d"
6345                         , input_mailbox_id);
6346
6347         EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
6348
6349         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6350         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6351                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6352
6353         _bind_stmt_field_data_int(hStmt, i++, input_is_local_mailbox);
6354
6355         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6356         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
6357                         ("sqlite3_step fail:%d", rc));
6358         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6359                         ("sqlite3_step fail:%d", rc));
6360         /*
6361            if (hStmt != NULL) {
6362            rc = sqlite3_finalize(hStmt);
6363            if (rc != SQLITE_OK) {
6364            EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6365            error = EMAIL_ERROR_DB_FAILURE;
6366            }
6367            hStmt = NULL;
6368            }
6369            */
6370         ret = true;
6371
6372 FINISH_OFF:
6373         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6374
6375         if (hStmt != NULL) {
6376                 rc = sqlite3_finalize(hStmt);
6377                 if (rc != SQLITE_OK) {
6378                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6379                         error = EMAIL_ERROR_DB_FAILURE;
6380                 }
6381         }
6382
6383         EM_DEBUG_FUNC_END("error [%d]", error);
6384         return error;
6385 }
6386
6387 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)
6388 {
6389         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);
6390         int i = 0;
6391         int err = EMAIL_ERROR_NONE;
6392         int result = false;
6393         int cur_mailbox_id_string = 0;
6394         int mailbox_id_string_buffer_length = 0;
6395         char  sql_query_string[QUERY_SIZE] = {0, };
6396         char *mailbox_id_string_buffer = NULL;
6397         char *parameter_string = NULL;
6398         sqlite3 *local_db_handle = NULL;
6399
6400         if (input_mailbox_id_array == NULL || input_mailbox_id_count == 0 || input_field_name == NULL) {
6401                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
6402                 return EMAIL_ERROR_INVALID_PARAM;
6403         }
6404
6405         local_db_handle = emstorage_get_db_connection(multi_user_name);
6406
6407         /* Generating mail id list string */
6408         mailbox_id_string_buffer_length = MAILBOX_ID_STRING_LENGTH * input_mailbox_id_count;
6409
6410         mailbox_id_string_buffer = em_malloc(mailbox_id_string_buffer_length);
6411
6412         if (!mailbox_id_string_buffer) {
6413                 EM_DEBUG_EXCEPTION("em_mallocfailed");
6414                 err = EMAIL_ERROR_OUT_OF_MEMORY;
6415                 goto FINISH_OFF;
6416         }
6417
6418         for (i = 0; i < input_mailbox_id_count; i++)
6419                 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]);
6420
6421         if (EM_SAFE_STRLEN(mailbox_id_string_buffer) > 1)
6422                 mailbox_id_string_buffer[EM_SAFE_STRLEN(mailbox_id_string_buffer) - 1] = NULL_CHAR;
6423
6424         /* Generating notification parameter string */
6425         parameter_string = em_malloc(mailbox_id_string_buffer_length + EM_SAFE_STRLEN(input_field_name) + 2);
6426
6427         if (!parameter_string) {
6428                 EM_DEBUG_EXCEPTION("em_mallocfailed");
6429                 err = EMAIL_ERROR_OUT_OF_MEMORY;
6430                 goto FINISH_OFF;
6431         }
6432
6433         SNPRINTF(parameter_string, QUERY_SIZE, "%s%c%s", input_field_name, 0x01, mailbox_id_string_buffer);
6434
6435         /* Write query string */
6436         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);
6437
6438         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
6439
6440         /* Execute query */
6441         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
6442         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6443         if (err != EMAIL_ERROR_NONE) {
6444                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
6445                 goto FINISH_OFF;
6446         }
6447
6448         if (sqlite3_changes(local_db_handle) == 0)
6449                 EM_DEBUG_LOG("no mail matched...");
6450
6451         result = true;
6452
6453 FINISH_OFF:
6454         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, result, err);
6455
6456         if (err == EMAIL_ERROR_NONE && parameter_string) {
6457                 if (!emcore_notify_storage_event(NOTI_MAILBOX_FIELD_UPDATE, input_account_id, 0, parameter_string, input_value))
6458                         EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_MAILBOX_FIELD_UPDATE [%s,%d]",
6459                                         input_field_name, input_value);
6460         }
6461
6462         EM_SAFE_FREE(mailbox_id_string_buffer);
6463         EM_SAFE_FREE(parameter_string);
6464
6465         EM_DEBUG_FUNC_END("err [%d]", err);
6466         return err;
6467 }
6468
6469 INTERNAL_FUNC int emstorage_add_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox_tbl, int transaction, int *err_code)
6470 {
6471         EM_DEBUG_FUNC_BEGIN("mailbox_tbl[%p], transaction[%d], err_code[%p]", mailbox_tbl, transaction, err_code);
6472
6473         if (!mailbox_tbl) {
6474                 if (err_code != NULL)
6475                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6476                 return false;
6477         }
6478
6479         int rc, ret = false;
6480         int error = EMAIL_ERROR_NONE;
6481         DB_STMT hStmt = NULL;
6482         char sql_query_string[QUERY_SIZE] = {0,};
6483         char **result = NULL;
6484         time_t current_time;
6485         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6486         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6487
6488         EM_SAFE_STRNCPY(sql_query_string, "SELECT max(rowid) FROM mail_box_tbl;", sizeof(sql_query_string));
6489
6490         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6491         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6492                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6493
6494         time(&current_time);
6495
6496         if (NULL == result[1])
6497                 rc = 1;
6498         else
6499                 rc = atoi(result[1]) + 1;
6500         sqlite3_free_table(result);
6501
6502         memset(sql_query_string, 0, sizeof(char) * QUERY_SIZE);
6503
6504         mailbox_tbl->mailbox_id = rc;
6505
6506         SNPRINTF(sql_query_string, sizeof(sql_query_string),
6507                         "INSERT INTO mail_box_tbl VALUES "
6508                         "(?"    /* mailbox_id */
6509                         ", ?"    /* account_id */
6510                         ", ?"    /* local_yn */
6511                         ", ?"    /* mailbox_name */
6512                         ", ?"    /* mailbox_type */
6513                         ", ?"    /* alias */
6514                         ", ?"    /* deleted_flag */
6515                         ", ?"    /* modifiable_yn */
6516                         ", ?"    /* total_mail_count_on_server */
6517                         ", ?"    /* has_archived_mails */
6518                         ", ?"    /* mail_slot_size */
6519                         ", ?"    /* no_select */
6520                         ", ?"    /* last_sync_time */
6521                         ", ?"    /* eas_data_length */
6522                         ", ?"    /* eas_data */
6523                         ")");
6524
6525
6526         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6527         EM_DEBUG_LOG_DEV("After sqlite3_prepare hStmt = %p", hStmt);
6528         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6529                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6530
6531         int col_index = 0;
6532
6533         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_id);
6534         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->account_id);
6535         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->local_yn);
6536         _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
6537         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mailbox_type);
6538         _bind_stmt_field_data_string(hStmt, col_index++, (char *)mailbox_tbl->alias, 0, ALIAS_LEN_IN_MAIL_BOX_TBL);
6539         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->deleted_flag);
6540         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->modifiable_yn);
6541         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->total_mail_count_on_server);
6542         _bind_stmt_field_data_int(hStmt, col_index++, 0);
6543         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->mail_slot_size);
6544         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->no_select);
6545         _bind_stmt_field_data_int(hStmt, col_index++, current_time);
6546         _bind_stmt_field_data_int(hStmt, col_index++, mailbox_tbl->eas_data_length);
6547         _bind_stmt_field_data_blob(hStmt, col_index++, (void*)mailbox_tbl->eas_data, mailbox_tbl->eas_data_length);
6548
6549
6550         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6551         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6552                         ("sqlite3_step fail:%dn", rc));
6553
6554         ret = true;
6555
6556 FINISH_OFF:
6557         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6558         if (hStmt != NULL) {
6559                 rc = sqlite3_finalize(hStmt);
6560                 if (rc != SQLITE_OK) {
6561                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6562                         error = EMAIL_ERROR_DB_FAILURE;
6563                 }
6564         }
6565
6566         if (error == EMAIL_ERROR_NONE) {
6567                 if (!emcore_notify_storage_event(NOTI_MAILBOX_ADD, mailbox_tbl->account_id, mailbox_tbl->mailbox_id,
6568                                         mailbox_tbl->mailbox_name, mailbox_tbl->mailbox_type))
6569                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6570         }
6571
6572         if (err_code != NULL)
6573                 *err_code = error;
6574
6575         EM_DEBUG_FUNC_END("ret [%d]", ret);
6576         return ret;
6577 }
6578
6579 INTERNAL_FUNC int emstorage_set_all_mailbox_modifiable_yn(char *multi_user_name, int account_id, int modifiable_yn, int transaction, int *err_code)
6580 {
6581         EM_DEBUG_FUNC_BEGIN("account_id[%d], modifiable_yn[%d], err_code[%p]", account_id, modifiable_yn, err_code);
6582
6583         if (account_id < FIRST_ACCOUNT_ID) {
6584
6585                 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
6586
6587                 if (err_code != NULL)
6588                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6589                 return false;
6590         }
6591         int rc, ret = false;
6592         int error = EMAIL_ERROR_NONE;
6593         char sql_query_string[QUERY_SIZE] = {0,};
6594         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6595         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6596
6597
6598         SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET modifiable_yn = %d WHERE account_id = %d", modifiable_yn, account_id);
6599
6600         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6601         if (error != EMAIL_ERROR_NONE) {
6602                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6603                 goto FINISH_OFF;
6604         }
6605
6606         rc = sqlite3_changes(local_db_handle);
6607         if (rc == 0)
6608                 EM_DEBUG_EXCEPTION("All mailbox_name modifiable_yn set to 0 already");
6609
6610
6611         ret = true;
6612
6613 FINISH_OFF:
6614         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6615
6616         if (err_code != NULL)
6617                 *err_code = error;
6618         EM_DEBUG_FUNC_END("ret [%d]", ret);
6619         return ret;
6620
6621
6622 }
6623
6624 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)
6625 {
6626         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);
6627
6628         if (account_id < FIRST_ACCOUNT_ID) {
6629                 EM_DEBUG_EXCEPTION(" account_id[%d], local_yn[%d], input_mailbox_id[%d]", account_id, local_yn, input_mailbox_id);
6630
6631                 if (err_code != NULL)
6632                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6633                 return false;
6634         }
6635
6636         int rc, ret = false;
6637         int error = EMAIL_ERROR_NONE;
6638         char sql_query_string[QUERY_SIZE] = {0, };
6639         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6640         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
6641
6642         if (local_yn == -1)
6643                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_box_tbl WHERE account_id = %d ", account_id);
6644         else
6645                 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);
6646
6647         if (input_mailbox_id > 0) {             /* 0 means all mailbox */
6648                 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);
6649         }
6650
6651         EM_DEBUG_LOG_SEC("mailbox sql_query_string [%s]", sql_query_string);
6652         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
6653         if (error != EMAIL_ERROR_NONE) {
6654                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
6655                 goto FINISH_OFF;
6656         }
6657
6658         rc = sqlite3_changes(local_db_handle);
6659         if (rc == 0) {
6660                 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
6661                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
6662                 ret = true;
6663         }
6664         ret = true;
6665
6666 FINISH_OFF:
6667
6668         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
6669
6670         if (error == EMAIL_ERROR_NONE) {
6671                 if (!emcore_notify_storage_event(NOTI_MAILBOX_DELETE, account_id, input_mailbox_id, NULL, 0))
6672                         EM_DEBUG_EXCEPTION("emcore_notify_storage_event[ NOTI_MAILBOX_ADD] : Notification Failed");
6673         }
6674
6675         if (err_code != NULL)
6676                 *err_code = error;
6677
6678         EM_DEBUG_FUNC_END("ret [%d]", ret);
6679         return ret;
6680 }
6681                                         /* LCOV_EXCL_STOP */
6682 INTERNAL_FUNC int emstorage_free_mailbox(emstorage_mailbox_tbl_t** mailbox_list, int count, int *err_code)
6683 {
6684         EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d], err_code[%p]", mailbox_list, count, err_code);
6685
6686         int ret = false;
6687         int error = EMAIL_ERROR_NONE;
6688
6689         if (count > 0) {
6690                 if (!mailbox_list || !*mailbox_list) {
6691                         EM_DEBUG_EXCEPTION(" mailbox_list[%p], count[%d]", mailbox_list, count);
6692
6693                         error = EMAIL_ERROR_INVALID_PARAM;
6694                         goto FINISH_OFF;
6695                 }
6696
6697                 emstorage_mailbox_tbl_t* p = *mailbox_list;
6698                 int i = 0;
6699
6700                 for (; i < count; i++) {
6701                         EM_SAFE_FREE(p[i].mailbox_name);
6702                         EM_SAFE_FREE(p[i].alias);
6703                         EM_SAFE_FREE(p[i].eas_data); /*valgrind*/
6704                 }
6705
6706                 EM_SAFE_FREE(p); *mailbox_list = NULL;
6707         }
6708
6709         ret = true;
6710
6711 FINISH_OFF:
6712         if (err_code != NULL)
6713                 *err_code = error;
6714
6715         EM_DEBUG_FUNC_END("ret [%d]", ret);
6716         return ret;
6717 }
6718                                         /* LCOV_EXCL_START */
6719 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)
6720 {
6721         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);
6722
6723         if (account_id < FIRST_ACCOUNT_ID || !mailbox_name || !count) {
6724                 EM_DEBUG_EXCEPTION("Invalid params");
6725
6726                 if (err_code != NULL)
6727                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6728                 return false;
6729         }
6730
6731         int rc = -1, ret = false;
6732         int error = EMAIL_ERROR_NONE;
6733         char sql_query_string[QUERY_SIZE] = {0, };
6734         char *replaced_mailbox_name = NULL;
6735
6736         if (strstr(mailbox_name, "'"))
6737                 replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6738         else
6739                 replaced_mailbox_name = EM_SAFE_STRDUP(mailbox_name);
6740
6741         EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
6742
6743
6744         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6745         EMSTORAGE_START_READ_TRANSACTION(transaction);
6746         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);
6747         EM_DEBUG_LOG_SEC(">>> SQL [ %s ] ", sql_query_string);
6748
6749         char **result;
6750
6751         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6752         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6753                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6754
6755         *count = atoi(result[1]);
6756         sqlite3_free_table(result);
6757
6758         ret = true;
6759
6760 FINISH_OFF:
6761         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6762
6763         EM_SAFE_FREE(replaced_mailbox_name);
6764
6765         if (err_code != NULL)
6766                 *err_code = error;
6767
6768         EM_DEBUG_FUNC_END("ret [%d]", ret);
6769         return ret;
6770 }
6771
6772 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)
6773 {
6774         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);
6775
6776         if (account_id < FIRST_ACCOUNT_ID || !uid || !exist) {
6777                 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_name[%p], uid[%p], exist[%p]", account_id, mailbox_name , uid, exist);
6778
6779                 if (err_code != NULL)
6780                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6781                 return false;
6782         }
6783
6784         int rc = -1, ret = false;
6785         int error = EMAIL_ERROR_NONE;
6786         char sql_query_string[QUERY_SIZE] = {0, };
6787         char *replaced_mailbox_name = NULL;
6788
6789         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6790         EMSTORAGE_START_READ_TRANSACTION(transaction);
6791
6792         if (mailbox_name) {
6793                 if (strstr(mailbox_name, "'"))
6794                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
6795                 else
6796                         replaced_mailbox_name = strdup(mailbox_name);
6797
6798                 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);
6799         } else {
6800                 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);
6801         }
6802
6803         char **result = NULL;
6804
6805         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
6806         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6807                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6808
6809         *exist = atoi(result[1]);
6810         sqlite3_free_table(result);
6811
6812         if (*exist > 0)
6813                 *exist = 1;
6814         else
6815                 *exist = 0;
6816
6817         ret = true;
6818
6819 FINISH_OFF:
6820         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6821
6822         EM_SAFE_FREE(replaced_mailbox_name);
6823
6824         if (err_code != NULL)
6825                 *err_code = error;
6826
6827         EM_DEBUG_FUNC_END("ret [%d]", ret);
6828         return ret;
6829 }
6830
6831 INTERNAL_FUNC int emstorage_get_downloaded_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t **mail, int transaction, int *err_code)
6832 {
6833         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], err_code[%p]", mail_id, mail, err_code);
6834
6835         if (!mail || mail_id <= 0) {
6836                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
6837                 if (err_code != NULL)
6838                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6839                 return false;
6840         }
6841
6842         int rc, ret = false;
6843         int error = EMAIL_ERROR_NONE;
6844         DB_STMT hStmt = NULL;
6845         char sql_query_string[QUERY_SIZE] = {0, };
6846
6847         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6848         EMSTORAGE_START_READ_TRANSACTION(transaction);
6849
6850         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE local_uid = %d", mail_id);
6851
6852         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6853         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6854
6855         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6856                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6857
6858
6859         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6860         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6861                         ("sqlite3_step fail:%d", rc));
6862
6863         *mail = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
6864         if (*mail == NULL) {
6865                 error = EMAIL_ERROR_OUT_OF_MEMORY;
6866                 EM_DEBUG_EXCEPTION("Memory allocation for mail failed.");
6867                 goto FINISH_OFF;
6868
6869         }
6870         memset(*mail, 0x00, sizeof(emstorage_mail_tbl_t));
6871
6872         _get_stmt_field_data_int(hStmt, &((*mail)->account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6873         _get_stmt_field_data_int(hStmt, &((*mail)->mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6874         _get_stmt_field_data_string(hStmt, &((*mail)->server_mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6875         _get_stmt_field_data_int(hStmt, &((*mail)->mail_id), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6876         _get_stmt_field_data_string(hStmt, &((*mail)->server_mail_id), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6877         _get_stmt_field_data_int(hStmt, &((*mail)->mail_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6878         _get_stmt_field_data_char(hStmt, &((*mail)->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6879
6880         (*mail)->server_mail_status = 1;
6881
6882         ret = true;
6883
6884 FINISH_OFF:
6885
6886         if (hStmt != NULL) {
6887                 rc = sqlite3_finalize(hStmt);
6888                 if (rc != SQLITE_OK) {
6889                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6890                         error = EMAIL_ERROR_DB_FAILURE;
6891                 }
6892         }
6893
6894         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
6895
6896         if (err_code != NULL)
6897                 *err_code = error;
6898
6899         EM_DEBUG_FUNC_END("ret [%d]", ret);
6900         return ret;
6901 }
6902
6903 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)
6904 {
6905         EM_PROFILE_BEGIN(emStorageGetDownloadList);
6906         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);
6907         if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid || !count) {
6908                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_id[%d], read_mail_uid[%p], count[%p]", account_id, mailbox_id, read_mail_uid, count);
6909
6910                 if (err_code != NULL)
6911                         *err_code = EMAIL_ERROR_INVALID_PARAM;
6912                 return false;
6913         }
6914
6915         int rc, ret = false;
6916         int error = EMAIL_ERROR_NONE;
6917
6918         DB_STMT hStmt = NULL;
6919         char sql_query_string[QUERY_SIZE] = {0, };
6920
6921         emstorage_read_mail_uid_tbl_t* p_data_tbl = NULL;
6922         int i = 0;
6923
6924         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
6925         EMSTORAGE_START_READ_TRANSACTION(transaction);
6926
6927         if (mailbox_id)
6928                 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);
6929         else
6930                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
6931
6932         EM_DEBUG_LOG_SEC(" sql_query_string : %s", sql_query_string);
6933
6934
6935
6936         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
6937         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
6938         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6939                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6940
6941
6942         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6943         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6944                         ("sqlite3_step fail:%d", rc));
6945
6946         char **result;
6947         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL); */
6948         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, count, NULL, NULL), rc);
6949         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
6950                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
6951
6952         sqlite3_free_table(result);
6953         if (*count == 0) {
6954                 EM_DEBUG_LOG("No mail found in mail_read_mail_uid_tbl");
6955                 ret = true;
6956                 goto FINISH_OFF;
6957         }
6958
6959
6960         if (!(p_data_tbl = (emstorage_read_mail_uid_tbl_t*)malloc(sizeof(emstorage_read_mail_uid_tbl_t) * *count))) {
6961                 EM_DEBUG_EXCEPTION(" malloc failed...");
6962                 error = EMAIL_ERROR_OUT_OF_MEMORY;
6963                 goto FINISH_OFF;
6964         }
6965
6966         memset(p_data_tbl, 0x00, sizeof(emstorage_read_mail_uid_tbl_t)*(*count));
6967
6968         for (i = 0; i < *count; ++i) {
6969                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6970                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id), LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6971                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6972                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].local_uid), LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6973                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].server_uid), 0, SERVER_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6974                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rfc822_size), RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6975                 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6976                 _get_stmt_field_data_char(hStmt, &(p_data_tbl[i].flags_flagged_field), FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL);
6977
6978                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
6979                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
6980                                 ("sqlite3_step fail:%d", rc));
6981         }
6982
6983         ret = true;
6984
6985 FINISH_OFF:
6986         if (ret == true)
6987                 *read_mail_uid = p_data_tbl;
6988         else if (p_data_tbl)
6989                 emstorage_free_read_mail_uid(&p_data_tbl, *count, NULL);
6990
6991         if (hStmt != NULL) {
6992                 rc = sqlite3_finalize(hStmt);
6993                 if (rc != SQLITE_OK) {
6994                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
6995                         error = EMAIL_ERROR_DB_FAILURE;
6996                 }
6997         }
6998
6999         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7000
7001         if (err_code != NULL)
7002                 *err_code = error;
7003
7004         EM_PROFILE_END(emStorageGetDownloadList);
7005         EM_DEBUG_FUNC_END("ret [%d]", ret);
7006         return ret;
7007 }
7008
7009 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)
7010 {
7011         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);
7012
7013         if (account_id < FIRST_ACCOUNT_ID || !mail_size) {
7014                 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);
7015
7016                 if (err_code != NULL)
7017                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7018                 return false;
7019         }
7020
7021         int rc, ret = false;
7022         int error = EMAIL_ERROR_NONE;
7023         DB_STMT hStmt = NULL;
7024         char sql_query_string[QUERY_SIZE] = {0, };
7025         char *replaced_mailbox_name = NULL;
7026
7027         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7028         EMSTORAGE_START_READ_TRANSACTION(transaction);
7029
7030         if (mailbox_name) {
7031                 if (strstr(mailbox_name, "'"))
7032                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
7033                 else
7034                         replaced_mailbox_name = strdup(mailbox_name);
7035
7036                 EM_DEBUG_LOG_SEC("replaced_mailbox_name : [%s]", replaced_mailbox_name);
7037
7038                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7039                                 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
7040                                 "WHERE account_id = %d "
7041                                 "AND mailbox_id = '%s' "
7042                                 "AND local_uid = %d "
7043                                 "AND mailbox_name = '%s' "
7044                                 "AND server_uid = '%s'",
7045                                 account_id, mailbox_id, local_uid, replaced_mailbox_name, uid);
7046         } else {
7047                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
7048                                 "SELECT IFNULL(MAX(data1), 0) FROM mail_read_mail_uid_tbl "
7049                                 "WHERE account_id = %d "
7050                                 "AND mailbox_id = '%s' "
7051                                 "AND local_uid = %d "
7052                                 "AND server_uid = '%s'",
7053                                 account_id, mailbox_id, local_uid, uid);
7054         }
7055
7056
7057         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7058         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7059                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7060
7061
7062         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7063         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7064                         ("sqlite3_step fail:%d", rc));
7065
7066         if (rc == SQLITE_DONE) {
7067                 EM_DEBUG_LOG("no matched mail found....");
7068                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
7069                 goto FINISH_OFF;
7070         }
7071
7072         _get_stmt_field_data_int(hStmt, mail_size, 0);
7073
7074         ret = true;
7075
7076 FINISH_OFF:
7077         EM_SAFE_FREE(replaced_mailbox_name);
7078
7079         if (hStmt != NULL) {
7080                 rc = sqlite3_finalize(hStmt);
7081                 if (rc != SQLITE_OK) {
7082                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7083                         error = EMAIL_ERROR_DB_FAILURE;
7084                 }
7085         }
7086
7087         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7088
7089         if (err_code != NULL)
7090                 *err_code = error;
7091
7092         EM_DEBUG_FUNC_END("ret [%d]", ret);
7093         return ret;
7094 }
7095
7096 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)
7097 {
7098         EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], transaction[%d], err_code[%p]", read_mail_uid, transaction, err_code);
7099
7100         if (!read_mail_uid) {
7101                 EM_DEBUG_EXCEPTION("read_mail_uid[%p]", read_mail_uid);
7102                 if (err_code != NULL)
7103                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7104                 return false;
7105         }
7106
7107         int rc, rc2,  ret = false;
7108         int error = EMAIL_ERROR_NONE;
7109         DB_STMT hStmt = NULL;
7110         char sql_query_string[QUERY_SIZE] = {0, };
7111
7112         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7113         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7114
7115         char *sql = "SELECT max(rowid) FROM mail_read_mail_uid_tbl;";
7116         char **result = NULL;
7117
7118
7119         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7120         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7121                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7122
7123         if (NULL == result[1]) rc = 1;
7124         else rc = atoi(result[1])+1;
7125         sqlite3_free_table(result);
7126
7127         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7128                         "INSERT INTO mail_read_mail_uid_tbl VALUES "
7129                         "(?"  /* account_id */
7130                         ", ?"  /* mailbox_id */
7131                         ", ?"  /* mailbox_name */
7132                         ", ?"  /* local_uid */
7133                         ", ?"  /* server_uid */
7134                         ", ?"  /* rfc822_size */
7135                         ", ?"  /* sync_status */
7136                         ", ?"  /* flags_seen_field */
7137                         ", ?"  /* flags_flagged_field */
7138                         ", ?)");
7139
7140
7141         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
7142         if (rc2 != SQLITE_OK) {
7143                 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7144                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
7145
7146                 error = EMAIL_ERROR_DB_FAILURE;
7147                 goto FINISH_OFF;
7148         }
7149
7150         EM_DEBUG_LOG("account_id[%d] mailbox_id[%d] local_uid [%d]"
7151                         "server_uid[%s] rfc822_size[%d] rc[%d]",
7152                         read_mail_uid->account_id, read_mail_uid->mailbox_id, read_mail_uid->local_uid,
7153                         read_mail_uid->server_uid, read_mail_uid->rfc822_size, rc);
7154
7155         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->account_id);
7156         _bind_stmt_field_data_int(hStmt, LOCAL_MAILBOX_ID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->mailbox_id);
7157         _bind_stmt_field_data_int(hStmt, LOCAL_UID_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->local_uid);
7158         _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);
7159         _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);
7160         _bind_stmt_field_data_int(hStmt, RFC822_SIZE_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->rfc822_size);
7161         _bind_stmt_field_data_int(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_seen_field);
7162         _bind_stmt_field_data_int(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_READ_MAIL_UID_TBL, read_mail_uid->flags_flagged_field);
7163         _bind_stmt_field_data_int(hStmt, IDX_NUM_IDX_IN_MAIL_READ_MAIL_UID_TBL, rc);
7164
7165
7166         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7167         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
7168                         ("sqlite3_step fail:%d", rc));
7169         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7170                         ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
7171
7172
7173         ret = true;
7174
7175 FINISH_OFF:
7176         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7177         if (hStmt != NULL) {
7178                 rc = sqlite3_finalize(hStmt);
7179                 if (rc != SQLITE_OK) {
7180                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7181                         error = EMAIL_ERROR_DB_FAILURE;
7182                 }
7183         }
7184
7185         if (err_code != NULL)
7186                 *err_code = error;
7187
7188         EM_DEBUG_FUNC_END("ret [%d]", ret);
7189         return ret;
7190 }
7191
7192 #ifdef __FEATURE_BODY_SEARCH__
7193 INTERNAL_FUNC int emstorage_add_mail_text(char *multi_user_name, emstorage_mail_text_tbl_t* mail_text, int transaction, int *err_code)
7194 {
7195         EM_DEBUG_FUNC_BEGIN("mail_text[%p], transaction[%d], err_code[%p]", mail_text, transaction, err_code);
7196
7197         if (!mail_text) {
7198                 EM_DEBUG_EXCEPTION("mail_text[%p]", mail_text);
7199                 if (err_code != NULL)
7200                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7201                 return false;
7202         }
7203
7204         int rc, rc2,  ret = false;
7205         int error = EMAIL_ERROR_NONE;
7206         DB_STMT hStmt = NULL;
7207         char sql_query_string[QUERY_SIZE] = {0, };
7208
7209         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7210         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7211
7212         char *sql = "SELECT max(rowid) FROM mail_text_tbl;";
7213         char **result = NULL;
7214
7215         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7216         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7217                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7218         sqlite3_free_table(result);
7219
7220         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7221                         "INSERT INTO mail_text_tbl VALUES "
7222                         "(?"
7223                         ", ?"
7224                         ", ?"
7225                         ", ?)");
7226
7227         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc2);
7228         if (rc2 != SQLITE_OK) {
7229                 EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7230                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc2, sqlite3_errmsg(local_db_handle));
7231
7232                 error = EMAIL_ERROR_DB_FAILURE;
7233                 goto FINISH_OFF;
7234         }
7235
7236         EM_DEBUG_LOG("mail_id[%d] account_id[%d] mailbox_id[%d]", mail_text->mail_id,
7237                         mail_text->account_id, mail_text->mailbox_id);
7238         EM_DEBUG_LOG_DEV("body_text VALUE [%s] ", mail_text->body_text);
7239
7240         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mail_id);
7241         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->account_id);
7242         _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TEXT_TBL, mail_text->mailbox_id);
7243         _bind_stmt_field_data_string(hStmt, BODY_TEXT_IDX_IN_MAIL_TEXT_TBL, (char *)mail_text->body_text, 0, -1);
7244
7245         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7246         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
7247                         ("sqlite3_step fail:%d", rc));
7248         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7249                         ("sqlite3_step fail[%d] [%s]", rc, sqlite3_errmsg(local_db_handle)));
7250
7251         ret = true;
7252
7253 FINISH_OFF:
7254         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7255         if (hStmt != NULL) {
7256                 rc = sqlite3_finalize(hStmt);
7257                 if (rc != SQLITE_OK) {
7258                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7259                         error = EMAIL_ERROR_DB_FAILURE;
7260                 }
7261         }
7262
7263         if (err_code != NULL)
7264                 *err_code = error;
7265
7266         EM_DEBUG_FUNC_END("ret [%d]", ret);
7267         return ret;
7268 }
7269 #endif
7270
7271 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)
7272 {
7273         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);
7274
7275         if (account_id < FIRST_ACCOUNT_ID || !read_mail_uid) {
7276                 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);
7277
7278                 if (err_code != NULL)
7279                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7280                 return false;
7281         }
7282
7283         int rc, ret = false;
7284         int error = EMAIL_ERROR_NONE;
7285         DB_STMT hStmt = NULL;
7286         char sql_query_string[QUERY_SIZE] = {0, };
7287
7288         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7289
7290         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7291
7292         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7293                         "UPDATE mail_read_mail_uid_tbl SET"
7294                         "  account_id = ?"
7295                         ", mailbox_id = ?"
7296                         ", mailbox_name = ?"
7297                         ", local_uid  = ?"
7298                         ", server_uid = ?"
7299                         ", rfc822_size = ?"
7300                         ", flags_seen_field  = ?"
7301                         ", flags_flagged_field  = ?"
7302                         " WHERE account_id = ?"
7303                         " AND mailbox_id  = ?"
7304                         " AND local_uid   = ?"
7305                         " AND mailbox_name= ?"
7306                         " AND server_uid = ?");
7307
7308
7309         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7310         EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt);
7311         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7312                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7313
7314
7315         int i = 0;
7316
7317         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->account_id);
7318         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->mailbox_id);
7319         _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7320         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->local_uid);
7321         _bind_stmt_field_data_string(hStmt, i++, (char*)read_mail_uid->server_uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7322         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->rfc822_size);
7323         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_seen_field);
7324         _bind_stmt_field_data_int(hStmt, i++, read_mail_uid->flags_flagged_field);
7325         _bind_stmt_field_data_int(hStmt, i++, account_id);
7326         _bind_stmt_field_data_int(hStmt, i++, mailbox_id);
7327         _bind_stmt_field_data_int(hStmt, i++, local_uid);
7328         _bind_stmt_field_data_string(hStmt, i++, (char*)mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7329         _bind_stmt_field_data_string(hStmt, i++, (char*)uid, 0, S_UID_LEN_IN_MAIL_READ_MAIL_UID_TBL);
7330
7331
7332         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7333         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7334                         ("sqlite3_step fail:%d", rc));
7335
7336         ret = true;
7337
7338 FINISH_OFF:
7339         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7340         if (hStmt != NULL) {
7341                 rc = sqlite3_finalize(hStmt);
7342                 if (rc != SQLITE_OK) {
7343                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7344                         error = EMAIL_ERROR_DB_FAILURE;
7345                 }
7346         }
7347
7348         if (err_code != NULL)
7349                 *err_code = error;
7350
7351         EM_DEBUG_FUNC_END("ret [%d]", ret);
7352         return ret;
7353 }
7354
7355 INTERNAL_FUNC int emstorage_remove_downloaded_mail(char *multi_user_name,
7356                 int account_id,
7357                 int mailbox_id,
7358                 char *mailbox_name,
7359                 char *uid,
7360                 int transaction,
7361                 int *err_code)
7362 {
7363         EM_DEBUG_FUNC_BEGIN_SEC("account_id[%d], mailbox_id[%d], mailbox_name[%s], "
7364                         "uid[%s], transaction[%d], err_code[%p]",
7365                         account_id, mailbox_id, mailbox_name, uid, transaction, err_code);
7366
7367         if (account_id < FIRST_ACCOUNT_ID) {
7368                 EM_DEBUG_EXCEPTION_SEC(" account_id[%d], mailbox_name[%s], uid[%s]", account_id, mailbox_name, uid);
7369
7370                 if (err_code != NULL)
7371                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7372                 return false;
7373         }
7374
7375         int ret = false;
7376         int error = EMAIL_ERROR_NONE;
7377         char sql_query_string[QUERY_SIZE] = {0, };
7378         char *replaced_mailbox_name = NULL;
7379         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7380
7381         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7382
7383         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7384                         "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d ", account_id);
7385
7386         if (mailbox_id > 0) {
7387                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
7388                                 sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7389                                 "AND mailbox_id = %d ", mailbox_id);
7390         }
7391
7392         if (mailbox_name) {             /*  NULL means all mailbox_name */
7393                 if (strstr(mailbox_name, "'"))
7394                         replaced_mailbox_name = em_replace_all_string(mailbox_name, "'", "''");
7395                 else
7396                         replaced_mailbox_name = strdup(mailbox_name);
7397
7398                 SNPRINTF(sql_query_string+EM_SAFE_STRLEN(sql_query_string),
7399                                 sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7400                                 "AND mailbox_name = '%s' ", replaced_mailbox_name);
7401         }
7402
7403         if (uid) {              /*  NULL means all mail */
7404                 sqlite3_snprintf(sizeof(sql_query_string) - (1 + EM_SAFE_STRLEN(sql_query_string)),
7405                                 sql_query_string + EM_SAFE_STRLEN(sql_query_string), "AND server_uid = '%q' ",
7406                                 uid);
7407         }
7408
7409         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7410         if (error != EMAIL_ERROR_NONE) {
7411                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7412                 goto FINISH_OFF;
7413         }
7414
7415         ret = true;
7416
7417 FINISH_OFF:
7418         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7419
7420         EM_SAFE_FREE(replaced_mailbox_name);
7421
7422         if (err_code != NULL)
7423                 *err_code = error;
7424
7425         EM_DEBUG_FUNC_END("ret [%d]", ret);
7426         return ret;
7427 }
7428
7429 INTERNAL_FUNC int emstorage_free_read_mail_uid(emstorage_read_mail_uid_tbl_t** read_mail_uid, int count, int *err_code)
7430 {
7431         EM_DEBUG_FUNC_BEGIN("read_mail_uid[%p], count[%d], err_code[%p]", read_mail_uid, count, err_code);
7432
7433         int ret = false;
7434         int error = EMAIL_ERROR_NONE;
7435
7436         if (count > 0) {
7437                 if (!read_mail_uid || !*read_mail_uid) {
7438                         EM_DEBUG_EXCEPTION(" read_mail_uid[%p], count[%d]", read_mail_uid, count);
7439
7440                         error = EMAIL_ERROR_INVALID_PARAM;
7441                         goto FINISH_OFF;
7442                 }
7443
7444                 emstorage_read_mail_uid_tbl_t* p = *read_mail_uid;
7445                 int i;
7446
7447                 for (i = 0; i < count; i++) {
7448                         EM_SAFE_FREE(p[i].mailbox_name);
7449                         EM_SAFE_FREE(p[i].server_uid);
7450                 }
7451
7452                 EM_SAFE_FREE(p); *read_mail_uid = NULL;
7453         }
7454
7455         ret = true;
7456
7457 FINISH_OFF:
7458         if (err_code != NULL)
7459                 *err_code = error;
7460
7461         EM_DEBUG_FUNC_END("ret [%d]", ret);
7462         return ret;
7463 }
7464
7465 INTERNAL_FUNC int emstorage_get_rule_count_by_account_id(char *multi_user_name, int account_id, int *count, int transaction, int *err_code)
7466 {
7467         EM_DEBUG_FUNC_BEGIN("account_id [%d], count[%p], transaction[%d], err_code[%p]", count, transaction, err_code);
7468
7469         if (!count) {
7470                 EM_DEBUG_EXCEPTION("count[%p]", count);
7471
7472                 if (err_code != NULL)
7473                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7474                 return false;
7475         }
7476
7477         int rc = -1, ret = false;
7478         int error =  EMAIL_ERROR_NONE;
7479         char sql_query_string[QUERY_SIZE] = {0, };
7480
7481         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7482         EMSTORAGE_START_READ_TRANSACTION(transaction);
7483
7484         if (account_id != ALL_ACCOUNT)
7485                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl where account_id = %d", account_id);
7486         else
7487                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_rule_tbl");
7488
7489         char **result;
7490
7491         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
7492         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7493                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7494
7495         *count = atoi(result[1]);
7496         sqlite3_free_table(result);
7497
7498         ret = true;
7499
7500 FINISH_OFF:
7501         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7502
7503         if (err_code != NULL)
7504                 *err_code = error;
7505
7506         EM_DEBUG_FUNC_END("ret [%d]", ret);
7507         return ret;
7508 }
7509
7510 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)
7511 {
7512         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);
7513
7514         if (!select_num || !is_completed || !rule_list) {               /*  only global rule supported. */
7515                 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);
7516
7517                 if (err_code != NULL)
7518                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7519                 return false;
7520         }
7521
7522         int ret = false;
7523         int error = EMAIL_ERROR_NONE;
7524
7525         emstorage_rule_tbl_t* p_data_tbl = NULL;
7526         int i = 0, count = 0;
7527         DB_STMT hStmt = NULL;
7528         char sql_query_string[QUERY_SIZE] = {0, };
7529
7530         int rc;
7531
7532         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7533         EMSTORAGE_START_READ_TRANSACTION(transaction);
7534
7535         if (account_id != ALL_ACCOUNT) {
7536                 if (type)
7537                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d AND type = %d", account_id, type);
7538                 else
7539                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE account_id = %d ORDER BY type", account_id);
7540         } else {
7541                 if (type)
7542                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE type = %d", type);
7543                 else
7544                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl ORDER BY type");
7545         }
7546
7547         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7548         /*      EM_DEBUG_LOG("sqlite3_prepare hStmt = %p", hStmt); */
7549         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7550                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7551
7552
7553         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7554         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7555                         ("sqlite3_step fail:%d", rc));
7556
7557         char **result;
7558         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL); */
7559         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
7560         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7561                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7562
7563         sqlite3_free_table(result);
7564
7565         if (count == 0) {
7566                 EM_DEBUG_LOG_DEV("No matching rule found...");
7567                 ret = true;
7568                 error = EMAIL_ERROR_FILTER_NOT_FOUND; /*there is no matched rule*/
7569                 goto FINISH_OFF;
7570         }
7571
7572
7573         if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t) * count))) {
7574                 EM_DEBUG_EXCEPTION(" malloc failed...");
7575
7576                 error = EMAIL_ERROR_OUT_OF_MEMORY;
7577                 goto FINISH_OFF;
7578         }
7579
7580         memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t) * count);
7581
7582         for (i = 0; i < count; i++) {
7583                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7584                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7585                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7586                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].type), TYPE_IDX_IN_MAIL_RULE_TBL);
7587                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7588                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7589                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7590                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7591                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7592                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7593
7594                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7595                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7596                                 ("sqlite3_step fail:%d", rc));
7597         }
7598
7599         ret = true;
7600
7601 FINISH_OFF:
7602
7603         EM_DEBUG_LOG("[%d] rules found.", count);
7604
7605         if (ret == true) {
7606                 *rule_list = p_data_tbl;
7607                 *select_num = count;
7608         } else if (p_data_tbl != NULL)
7609                 emstorage_free_rule(&p_data_tbl, count, NULL); /* CID FIX */
7610
7611         if (hStmt != NULL) {
7612                 rc = sqlite3_finalize(hStmt);
7613                 if (rc != SQLITE_OK) {
7614                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7615                         error = EMAIL_ERROR_DB_FAILURE;
7616                 }
7617         }
7618
7619         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7620
7621         if (err_code != NULL)
7622                 *err_code = error;
7623
7624         EM_DEBUG_FUNC_END("ret [%d]", ret);
7625         return ret;
7626 }
7627
7628 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)
7629 {
7630         EM_DEBUG_FUNC_BEGIN("rule_id[%d], rule[%p], transaction[%d], err_code[%p]", rule_id, rule, transaction, err_code);
7631         int error = EMAIL_ERROR_NONE;
7632         int ret = false;
7633         DB_STMT hStmt = NULL;
7634
7635         if (rule_id <= 0) {
7636                 EM_DEBUG_EXCEPTION("Invalid parameter");
7637                 error = EMAIL_ERROR_INVALID_PARAM;
7638                 goto FINISH_OFF;
7639         }
7640
7641         if (!rule) {
7642                 EM_DEBUG_EXCEPTION("rule_id[%d], rule[%p]", rule_id, rule);
7643                 error = EMAIL_ERROR_INVALID_PARAM;
7644                 goto FINISH_OFF;
7645         }
7646
7647         emstorage_rule_tbl_t* p_data_tbl = NULL;
7648         int rc;
7649
7650         char sql_query_string[QUERY_SIZE] = {0, };
7651         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7652         EMSTORAGE_START_READ_TRANSACTION(transaction);
7653
7654         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7655
7656         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7657         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7658                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7659
7660
7661         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7662         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7663                         ("sqlite3_step fail:%d", rc));
7664
7665         if (rc == SQLITE_DONE) {
7666                 EM_DEBUG_EXCEPTION(" no matched rule found...");
7667                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7668                 goto FINISH_OFF;
7669         }
7670
7671         if (!(p_data_tbl = (emstorage_rule_tbl_t*)malloc(sizeof(emstorage_rule_tbl_t)))) {
7672                 EM_DEBUG_EXCEPTION(" malloc failed...");
7673                 error = EMAIL_ERROR_OUT_OF_MEMORY;
7674                 goto FINISH_OFF;
7675         }
7676
7677         memset(p_data_tbl, 0x00, sizeof(emstorage_rule_tbl_t));
7678         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL);
7679         _get_stmt_field_data_int(hStmt, &(p_data_tbl->rule_id), RULE_ID_IDX_IN_MAIL_RULE_TBL);
7680         _get_stmt_field_data_string(hStmt, &(p_data_tbl->filter_name), 0, FILTER_NAME_IDX_IN_MAIL_RULE_TBL);
7681         _get_stmt_field_data_int(hStmt, &(p_data_tbl->type), TYPE_IDX_IN_MAIL_RULE_TBL);
7682         _get_stmt_field_data_string(hStmt, &(p_data_tbl->value), 0, VALUE_IDX_IN_MAIL_RULE_TBL);
7683         _get_stmt_field_data_string(hStmt, &(p_data_tbl->value2), 0, VALUE2_IDX_IN_MAIL_RULE_TBL);
7684         _get_stmt_field_data_int(hStmt, &(p_data_tbl->action_type), ACTION_TYPE_IDX_IN_MAIL_RULE_TBL);
7685         _get_stmt_field_data_int(hStmt, &(p_data_tbl->target_mailbox_id), TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL);
7686         _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag1), FLAG1_IDX_IN_MAIL_RULE_TBL);
7687         _get_stmt_field_data_int(hStmt, &(p_data_tbl->flag2), FLAG2_IDX_IN_MAIL_RULE_TBL);
7688
7689         ret = true;
7690
7691 FINISH_OFF:
7692
7693         if (ret == true)
7694                 *rule = p_data_tbl;
7695
7696         if (hStmt != NULL) {
7697                 rc = sqlite3_finalize(hStmt);
7698                 if (rc != SQLITE_OK) {
7699                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7700                         error = EMAIL_ERROR_DB_FAILURE;
7701                 }
7702         }
7703
7704         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7705
7706         if (err_code != NULL)
7707                 *err_code = error;
7708
7709         EM_DEBUG_FUNC_END("ret [%d]", ret);
7710         return ret;
7711 }
7712
7713 INTERNAL_FUNC int emstorage_change_rule(char *multi_user_name, int rule_id, emstorage_rule_tbl_t* new_rule, int transaction, int *err_code)
7714 {
7715         EM_DEBUG_FUNC_BEGIN("rule_id[%d], new_rule[%p], transaction[%d], err_code[%p]", rule_id, new_rule, transaction, err_code);
7716
7717         if (!new_rule) {                /*  only global rule supported. */
7718                 EM_DEBUG_EXCEPTION("rule_id[%d], new_rule[%p]", rule_id, new_rule);
7719
7720                 if (err_code != NULL)
7721                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7722                 return false;
7723         }
7724
7725         int rc, ret = false;
7726         int error = EMAIL_ERROR_NONE;
7727
7728         DB_STMT hStmt = NULL;
7729         char sql_query_string[QUERY_SIZE] = {0, };
7730         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7731         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7732
7733         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7734                         "UPDATE mail_rule_tbl SET"
7735                         "  filter_name = ?"
7736                         ", type = ?"
7737                         ", value = ?"
7738                         ", value2 = ?"
7739                         ", action_type = ?"
7740                         ", target_mailbox_id = ?"
7741                         ", flag1 = ?"
7742                         ", flag2 = ?"
7743                         ", account_id = ?"
7744                         ", rule_id = ?"
7745                         " WHERE rule_id = %d"
7746                         , rule_id);
7747
7748
7749         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7750         EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
7751         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7752                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7753
7754         int i = 0;
7755
7756         _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->filter_name, 0, FILTER_NAME_LEN_IN_MAIL_RULE_TBL);
7757         _bind_stmt_field_data_int(hStmt, i++, new_rule->type);
7758         _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7759         _bind_stmt_field_data_string(hStmt, i++, (char *)new_rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7760         _bind_stmt_field_data_int(hStmt, i++, new_rule->action_type);
7761         _bind_stmt_field_data_int(hStmt, i++, new_rule->target_mailbox_id);
7762         _bind_stmt_field_data_int(hStmt, i++, new_rule->flag1);
7763         _bind_stmt_field_data_int(hStmt, i++, new_rule->flag2);
7764         _bind_stmt_field_data_int(hStmt, i++, new_rule->account_id);
7765         _bind_stmt_field_data_int(hStmt, i++, rule_id);
7766
7767
7768         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7769         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7770                         ("sqlite3_step fail:%d", rc));
7771
7772         ret = true;
7773
7774 FINISH_OFF:
7775         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7776         if (hStmt != NULL) {
7777                 rc = sqlite3_finalize(hStmt);
7778                 if (rc != SQLITE_OK) {
7779                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7780                         error = EMAIL_ERROR_DB_FAILURE;
7781                 }
7782         }
7783
7784         if (err_code != NULL)
7785                 *err_code = error;
7786
7787         EM_DEBUG_FUNC_END("ret [%d]", ret);
7788         return ret;
7789 }
7790
7791 INTERNAL_FUNC int emstorage_find_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7792 {
7793         EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7794
7795         if (!rule) {
7796                 EM_DEBUG_LOG("rule is NULL");
7797                 if (err_code != NULL)
7798                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7799                 return false;
7800         }
7801
7802         DB_STMT hStmt = NULL;
7803         char sql_query_string[QUERY_SIZE] = {0,};
7804         int error = EMAIL_ERROR_NONE;
7805         int rc = 0;
7806         int ret = false;
7807
7808         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7809         EMSTORAGE_START_READ_TRANSACTION(transaction);
7810
7811         switch (rule->action_type) {
7812         case EMAIL_FILTER_MOVE:
7813                 if (rule->type == EMAIL_PRIORITY_SENDER) {
7814                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7815                                         "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7816                                         rule->action_type, rule->type, rule->value2);
7817                 } else {
7818                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7819                                         "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);
7820                 }
7821                 break;
7822         case EMAIL_FILTER_BLOCK:
7823                 if (rule->type == EMAIL_FILTER_FROM)
7824                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7825                                         "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value2) = UPPER(\'%q\')",
7826                                         rule->action_type, rule->type, rule->value2);
7827                 else if (rule->type == EMAIL_FILTER_SUBJECT)
7828                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7829                                         "SELECT rule_id FROM mail_rule_tbl WHERE action_type = %d AND type = %d AND UPPER(value) = UPPER(\'%q\')",
7830                                         rule->action_type, rule->type, rule->value);
7831                 else if (rule->type == (EMAIL_FILTER_SUBJECT | EMAIL_FILTER_FROM))
7832                         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
7833                                         "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\'))",
7834                                         rule->action_type, EMAIL_FILTER_SUBJECT, rule->value, EMAIL_FILTER_FROM, rule->value2);
7835                 break;
7836
7837         default:
7838                 EM_DEBUG_EXCEPTION("Invalid parameter : rule->action_type[%d]", rule->action_type);
7839                 error = EMAIL_ERROR_INVALID_PARAM;
7840                 goto FINISH_OFF;
7841                 break;
7842         }
7843
7844         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
7845         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7846                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
7847
7848         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7849         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7850                         ("sqlite3_step fail:%d", rc));
7851
7852         if (rc == SQLITE_DONE) {
7853                 EM_DEBUG_EXCEPTION(" no matched rule found...");
7854                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7855         }
7856
7857         ret = true;
7858
7859 FINISH_OFF:
7860
7861         if (hStmt != NULL) {
7862                 rc = sqlite3_finalize(hStmt);
7863                 if (rc != SQLITE_OK) {
7864                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7865                         error = EMAIL_ERROR_DB_FAILURE;
7866                 }
7867         }
7868
7869         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
7870
7871         if (err_code)
7872                 *err_code = error;
7873
7874         EM_DEBUG_FUNC_END("ret [%d]", ret);
7875         return ret;
7876 }
7877
7878 INTERNAL_FUNC int emstorage_add_rule(char *multi_user_name, emstorage_rule_tbl_t* rule, int transaction, int *err_code)
7879 {
7880         EM_DEBUG_FUNC_BEGIN("rule[%p], transaction[%d], err_code[%p]", rule, transaction, err_code);
7881
7882         if (!rule) {    /*  only global rule supported. */
7883                 EM_DEBUG_LOG("rule is NULL");
7884                 if (err_code != NULL)
7885                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7886                 return false;
7887         }
7888
7889         int rc, rc_2, ret = false;
7890         int error = EMAIL_ERROR_NONE;
7891         DB_STMT hStmt = NULL;
7892         char sql_query_string[QUERY_SIZE] = {0, };
7893
7894         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7895
7896         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7897
7898         char *sql;
7899         char **result;
7900         sql = "SELECT max(rowid) FROM mail_rule_tbl;";
7901
7902         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
7903         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
7904                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
7905
7906         if (NULL == result[1])
7907                 rc = 1;
7908         else
7909                 rc = atoi(result[1])+1;
7910
7911         sqlite3_free_table(result);
7912
7913         rule->rule_id = rc;
7914
7915         SNPRINTF(sql_query_string, sizeof(sql_query_string),
7916                         "INSERT INTO mail_rule_tbl VALUES "
7917                         "(?"            /*  account id */
7918                         ", ?"           /*  rule_id */
7919                         ", ?"           /*  filter_name */
7920                         ", ?"           /*  type */
7921                         ", ?"           /*  value */
7922                         ", ?"           /*  value2 */
7923                         ", ?"           /*  action_type */
7924                         ", ?"           /*  target_mailbox_id */
7925                         ", ?"           /*  flag1 */
7926                         ", ?)");        /*  flag2 */
7927
7928         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc_2);
7929         if (rc_2 != SQLITE_OK) {
7930                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc_2, sqlite3_errmsg(local_db_handle));
7931                 error = EMAIL_ERROR_DB_FAILURE;
7932                 goto FINISH_OFF;
7933         }
7934
7935         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_RULE_TBL, rule->account_id);
7936         _bind_stmt_field_data_int(hStmt, RULE_ID_IDX_IN_MAIL_RULE_TBL, rule->rule_id);
7937         _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);
7938         _bind_stmt_field_data_int(hStmt, TYPE_IDX_IN_MAIL_RULE_TBL, rule->type);
7939         _bind_stmt_field_data_string(hStmt, VALUE_IDX_IN_MAIL_RULE_TBL, (char*)rule->value, 0, VALUE_LEN_IN_MAIL_RULE_TBL);
7940         _bind_stmt_field_data_string(hStmt, VALUE2_IDX_IN_MAIL_RULE_TBL, (char*)rule->value2, 0, VALUE2_LEN_IN_MAIL_RULE_TBL);
7941         _bind_stmt_field_data_int(hStmt, ACTION_TYPE_IDX_IN_MAIL_RULE_TBL, rule->action_type);
7942         _bind_stmt_field_data_int(hStmt, TARGET_MAILBOX_ID_IDX_IN_MAIL_RULE_TBL, rule->target_mailbox_id);
7943         _bind_stmt_field_data_int(hStmt, FLAG1_IDX_IN_MAIL_RULE_TBL, rule->flag1);
7944         _bind_stmt_field_data_int(hStmt, FLAG2_IDX_IN_MAIL_RULE_TBL, rule->flag2);
7945
7946         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
7947         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
7948                         ("sqlite3_step fail:%d", rc));
7949
7950         ret = true;
7951
7952 FINISH_OFF:
7953         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
7954         if (hStmt != NULL) {
7955                 rc = sqlite3_finalize(hStmt);
7956                 if (rc != SQLITE_OK) {
7957                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
7958                         error = EMAIL_ERROR_DB_FAILURE;
7959                 }
7960         }
7961
7962         if (err_code != NULL)
7963                 *err_code = error;
7964
7965         EM_DEBUG_FUNC_END("ret [%d]", ret);
7966         return ret;
7967 }
7968
7969 INTERNAL_FUNC int emstorage_delete_rule(char *multi_user_name, int rule_id, int transaction, int *err_code)
7970 {
7971         EM_DEBUG_FUNC_BEGIN("rule_id[%d], transaction[%d], err_code[%p]", rule_id, transaction, err_code);
7972
7973         if (rule_id <= 0) {             /*  only global rule supported. */
7974                 EM_DEBUG_EXCEPTION("rule_id[%d]", rule_id);
7975
7976                 if (err_code != NULL)
7977                         *err_code = EMAIL_ERROR_INVALID_PARAM;
7978                 return false;
7979         }
7980
7981         int rc, ret = false;
7982         int error = EMAIL_ERROR_NONE;
7983         char sql_query_string[QUERY_SIZE] = {0, };
7984         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
7985         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
7986
7987         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_rule_tbl WHERE rule_id = %d", rule_id);
7988         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
7989         if (error != EMAIL_ERROR_NONE) {
7990                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
7991                 goto FINISH_OFF;
7992         }
7993
7994         rc = sqlite3_changes(local_db_handle);
7995         if (rc == 0) {
7996                 EM_DEBUG_EXCEPTION(" no matched rule found...");
7997
7998                 error = EMAIL_ERROR_FILTER_NOT_FOUND;
7999                 goto FINISH_OFF;
8000         }
8001
8002         ret = true;
8003
8004 FINISH_OFF:
8005         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
8006
8007         if (err_code != NULL)
8008                 *err_code = error;
8009
8010         EM_DEBUG_FUNC_END("ret [%d]", ret);
8011         return ret;
8012 }
8013
8014 INTERNAL_FUNC int emstorage_free_rule(emstorage_rule_tbl_t** rule_list, int count, int *err_code)
8015 {
8016         EM_DEBUG_FUNC_BEGIN("rule_list[%p], conut[%d], err_code[%p]", rule_list, count, err_code);
8017
8018         int ret = false;
8019         int error = EMAIL_ERROR_NONE;
8020
8021         if (count > 0) {
8022                 if (!rule_list || !*rule_list) {
8023                         EM_DEBUG_EXCEPTION(" rule_list[%p], conut[%d]", rule_list, count);
8024
8025                         error = EMAIL_ERROR_INVALID_PARAM;
8026                         goto FINISH_OFF;
8027                 }
8028
8029                 emstorage_rule_tbl_t* p = *rule_list;
8030                 int i = 0;
8031
8032                 for (; i < count; i++)
8033                         EM_SAFE_FREE(p[i].value);
8034
8035                 EM_SAFE_FREE(p); *rule_list = NULL;
8036         }
8037
8038 FINISH_OFF:
8039         if (err_code != NULL)
8040                 *err_code = error;
8041
8042         EM_DEBUG_FUNC_END("ret [%d]", ret);
8043         return ret;
8044 }
8045
8046 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)
8047 {
8048         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);
8049
8050         if (!total && !unseen) {
8051                 EM_DEBUG_EXCEPTION(" accoun_id[%d], mailbox_id[%d], total[%p], unseen[%p]", account_id, mailbox_id, total, unseen);
8052                 if (err_code != NULL)
8053                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8054                 return false;
8055         }
8056
8057         int rc = -1, ret = false;
8058         int error = EMAIL_ERROR_NONE;
8059         DB_STMT hStmt = NULL;
8060         char sql_query_string[QUERY_SIZE] = {0, };
8061         char *replaced_mailbox_name = NULL;
8062
8063         memset(&sql_query_string, 0x00, sizeof(sql_query_string));
8064         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8065         EMSTORAGE_START_READ_TRANSACTION(transaction);
8066
8067         if (total) {
8068                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl");
8069
8070                 if (account_id != ALL_ACCOUNT) {
8071                         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);
8072                         if (mailbox_id)
8073                                 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);
8074                 } else if (mailbox_id)
8075                         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);
8076
8077 #ifdef USE_GET_RECORD_COUNT_API
8078                 char **result;
8079
8080                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
8081                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF2; },
8082                                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8083
8084                 *total = atoi(result[1]);
8085                 sqlite3_free_table(result);
8086 #else
8087
8088                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8089                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF2; },
8090                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8091
8092                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8093                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF2; },
8094                                 ("sqlite3_step fail:%d", rc));
8095                 _get_stmt_field_data_int(hStmt, total, 0);
8096 #endif          /*  USE_GET_RECORD_COUNT_API */
8097         }
8098
8099         if (unseen) {
8100                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_tbl WHERE flags_seen_field = 0");               /*  fSEEN = 0x01 */
8101
8102                 if (account_id != ALL_ACCOUNT) {
8103                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
8104                                 sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND account_id = %d", account_id);
8105                 }
8106
8107                 if (mailbox_id) {
8108                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
8109                                 sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND mailbox_id = %d", mailbox_id);
8110                 } else
8111                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string),
8112                                 sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1), " AND mailbox_type NOT IN (3, 5)");
8113
8114                 char **result;
8115                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
8116                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc,
8117                                 { error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
8118                                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8119
8120                 *unseen = atoi(result[1]);
8121                 sqlite3_free_table(result);
8122
8123         }
8124 FINISH_OFF:
8125         ret = true;
8126
8127 FINISH_OFF2:
8128
8129 #ifndef USE_PREPARED_QUERY_
8130         if (hStmt != NULL) {
8131                 rc = sqlite3_finalize(hStmt);
8132                 if (rc != SQLITE_OK) {
8133                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8134                         error = EMAIL_ERROR_DB_FAILURE;
8135                 }
8136         }
8137 #endif
8138
8139         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8140
8141         EM_SAFE_FREE(replaced_mailbox_name);
8142
8143         if (err_code != NULL)
8144                 *err_code = error;
8145
8146         EM_DEBUG_FUNC_END("ret [%d]", ret);
8147         return ret;
8148 }
8149
8150 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)
8151 {
8152         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
8153
8154         if (mail_id <= 0 || !mail) {
8155                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
8156                 if (err_code != NULL)
8157                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8158                 return false;
8159         }
8160
8161         int col_index = 0;
8162         emstorage_mail_tbl_t* p_data_tbl = (emstorage_mail_tbl_t*)malloc(sizeof(emstorage_mail_tbl_t));
8163
8164         if (p_data_tbl == NULL) {
8165                 EM_DEBUG_EXCEPTION("malloc failed...");
8166                 if (err_code != NULL)
8167                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8168                 return false;
8169         }
8170
8171         memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8172         DB_STMT hStmt = NULL;
8173         char sql_query_string[QUERY_SIZE] = {0, };
8174
8175         int rc, ret = false;
8176         int error = EMAIL_ERROR_NONE;
8177
8178         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8179         EMSTORAGE_START_READ_TRANSACTION(transaction);
8180
8181         switch (type) {
8182         case RETRIEVE_SUMMARY:
8183                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8184                                 "SELECT account_id, "
8185                                 "mail_id, "
8186                                 "mailbox_id, "
8187                                 "server_mail_status, "
8188                                 "server_mailbox_name, "
8189                                 "server_mail_id, "
8190                                 "file_path_plain, "
8191                                 "file_path_html,"
8192                                 "file_path_mime_entity, "
8193                                 "flags_seen_field, "
8194                                 "save_status, "
8195                                 "lock_status, "
8196                                 "thread_id, "
8197                                 "thread_item_count "
8198                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8199                 break;
8200
8201         case RETRIEVE_FIELDS_FOR_DELETE:
8202                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8203                                 "SELECT account_id, "
8204                                 "mail_id, "
8205                                 "server_mail_status, "
8206                                 "server_mailbox_name, "
8207                                 "server_mail_id "
8208                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8209                 break;
8210
8211         case RETRIEVE_ACCOUNT:
8212                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8213                                 "SELECT account_id "
8214                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8215                 break;
8216
8217         case RETRIEVE_FLAG:
8218                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
8219                                 "SELECT account_id, "
8220                                 "flags_seen_field, "
8221                                 "thread_id, "
8222                                 "mailbox_id "
8223                                 "FROM mail_tbl WHERE mail_id = %d", mail_id);
8224                 break;
8225
8226         default:
8227                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
8228                 error = EMAIL_ERROR_INVALID_PARAM;
8229                 goto FINISH_OFF;
8230         }
8231
8232         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
8233
8234         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8235         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8236                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8237
8238
8239         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8240         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8241                         ("sqlite3_step fail:%d", rc));
8242
8243         if (rc == SQLITE_DONE) {
8244                 EM_DEBUG_LOG("no matched mail found...");
8245                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8246                 goto FINISH_OFF;
8247         }
8248         switch (type) {
8249         case RETRIEVE_SUMMARY:
8250                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8251                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
8252                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
8253                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
8254                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
8255                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8256                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, col_index++);
8257                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, col_index++);
8258                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, col_index++);
8259                 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8260                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), col_index++);
8261                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), col_index++);
8262                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8263                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_item_count), col_index++);
8264                 break;
8265
8266         case RETRIEVE_FIELDS_FOR_DELETE:
8267                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8268                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), col_index++);
8269                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), col_index++);
8270                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, col_index++);
8271                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, col_index++);
8272                 break;
8273
8274         case RETRIEVE_ACCOUNT:
8275                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8276                 break;
8277
8278         case RETRIEVE_FLAG:
8279                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), col_index++);
8280                 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), col_index++);
8281                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->thread_id), col_index++);
8282                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), col_index++);
8283                 break;
8284         }
8285
8286         ret = true;
8287
8288 FINISH_OFF:
8289         if (ret == true)
8290                 *mail = p_data_tbl;
8291         else if (p_data_tbl != NULL)
8292                 emstorage_free_mail(&p_data_tbl,  1, NULL);
8293
8294         if (hStmt != NULL) {
8295                 rc = sqlite3_finalize(hStmt);
8296                 if (rc != SQLITE_OK) {
8297                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8298                         error = EMAIL_ERROR_DB_FAILURE;
8299                 }
8300         }
8301
8302
8303         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8304
8305         if (err_code != NULL)
8306                 *err_code = error;
8307
8308         EM_DEBUG_FUNC_END("ret [%d]", ret);
8309         return ret;
8310 }
8311
8312 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)
8313 {
8314         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);
8315
8316         int ret = false;
8317         int error = EMAIL_ERROR_NONE;
8318         int query_string_length = 0;
8319         int i = 0, item_count = 0, rc = -1, field_count, col_index, cur_sql_query_string = 0;
8320         char **result = NULL;
8321         char *sql_query_string = NULL;
8322         emstorage_mail_tbl_t* p_data_tbl = NULL;
8323         sqlite3 *local_db_handle = NULL;
8324
8325         if (number_of_mails <= 0 || !mail_ids) {
8326                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8327                 if (err_code != NULL)
8328                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8329                 return false;
8330         }
8331
8332         p_data_tbl = (emstorage_mail_tbl_t*)em_malloc(sizeof(emstorage_mail_tbl_t) * number_of_mails);
8333
8334         query_string_length = (sizeof(char) * 8 * number_of_mails) + 512;
8335         sql_query_string = (char*)em_malloc(query_string_length);
8336
8337         if (p_data_tbl == NULL || sql_query_string == NULL) {
8338                 EM_DEBUG_EXCEPTION("malloc failed...");
8339
8340                 EM_SAFE_FREE(p_data_tbl);
8341                 EM_SAFE_FREE(sql_query_string);
8342
8343                 if (err_code != NULL)
8344                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
8345                 return false;
8346         }
8347
8348         local_db_handle = emstorage_get_db_connection(multi_user_name);
8349
8350         EMSTORAGE_START_READ_TRANSACTION(transaction);
8351
8352         switch (type) {
8353         case RETRIEVE_SUMMARY:
8354                 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8355                                 "SELECT account_id, "
8356                                 "mail_id, "
8357                                 "mailbox_id, "
8358                                 "server_mail_status, "
8359                                 "server_mailbox_name, "
8360                                 "server_mail_id, "
8361                                 "file_path_plain, "
8362                                 "file_path_html, "
8363                                 "file_path_mime_entity, "
8364                                 "subject, "
8365                                 "flags_seen_field, "
8366                                 "save_status, "
8367                                 "lock_status, "
8368                                 "thread_id, "
8369                                 "thread_item_count "
8370                                 "FROM mail_tbl WHERE mail_id in (");
8371                 field_count = 15;
8372                 break;
8373
8374         case RETRIEVE_FIELDS_FOR_DELETE:
8375                 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8376                                 "SELECT account_id, "
8377                                 "mail_id, "
8378                                 "server_mail_status, "
8379                                 "server_mailbox_name, "
8380                                 "server_mail_id "
8381                                 "FROM mail_tbl WHERE mail_id in (");
8382                 field_count = 5;
8383                 break;
8384
8385         case RETRIEVE_ACCOUNT:
8386                 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8387                                 "SELECT account_id FROM mail_tbl WHERE mail_id in (");
8388                 field_count = 1;
8389                 break;
8390
8391         case RETRIEVE_FLAG:
8392                 cur_sql_query_string = SNPRINTF(sql_query_string, query_string_length,
8393                                 "SELECT account_id, "
8394                                 "mail_id, "
8395                                 "mailbox_id, "
8396                                 "flags_seen_field, "
8397                                 "thread_id "
8398                                 "FROM mail_tbl WHERE mail_id in (");
8399                 field_count = 5;
8400                 break;
8401
8402         default:
8403                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : type [%d]", type);
8404                 error = EMAIL_ERROR_INVALID_PARAM;
8405                 goto FINISH_OFF;
8406         }
8407
8408         for (i = 0; i < number_of_mails; i++)
8409                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_string_length, "%d,", mail_ids[i]);
8410         sql_query_string[EM_SAFE_STRLEN(sql_query_string) - 1] = ')';
8411
8412         EM_DEBUG_LOG_SEC("Query [%s], Length [%zu]", sql_query_string, EM_SAFE_STRLEN(sql_query_string));
8413
8414         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &item_count, 0, NULL), rc);
8415         if (SQLITE_OK != rc && -1 != rc) {
8416                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
8417                 error = EMAIL_ERROR_DB_FAILURE;
8418                 goto FINISH_OFF;
8419         }
8420         EM_DEBUG_LOG("item_count [%d]", item_count);
8421
8422         if (number_of_mails != item_count) {
8423                 EM_DEBUG_EXCEPTION("Can't find all emails");
8424                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8425                 goto FINISH_OFF;
8426         }
8427
8428         col_index = field_count;
8429
8430         for (i = 0; i < item_count; i++)        {
8431                 switch (type) {
8432                 case RETRIEVE_SUMMARY:
8433                         _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8434                         _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8435                         _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8436                         _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8437                         _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8438                         _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8439                         _get_table_field_data_string(result, &(p_data_tbl[i].file_path_plain), 0, col_index++);
8440                         _get_table_field_data_string(result, &(p_data_tbl[i].file_path_html), 0, col_index++);
8441                         _get_table_field_data_string(result, &(p_data_tbl[i].file_path_mime_entity), 0, col_index++);
8442                         _get_table_field_data_string(result, &(p_data_tbl[i].subject), 0, col_index++);
8443                         _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8444                         _get_table_field_data_int(result, (int*)&(p_data_tbl[i].save_status), col_index++);
8445                         _get_table_field_data_int(result, &(p_data_tbl[i].lock_status), col_index++);
8446                         _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8447                         _get_table_field_data_int(result, &(p_data_tbl[i].thread_item_count), col_index++);
8448                         break;
8449
8450                 case RETRIEVE_FIELDS_FOR_DELETE:
8451                         _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8452                         _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8453                         _get_table_field_data_int(result, &(p_data_tbl[i].server_mail_status), col_index++);
8454                         _get_table_field_data_string(result, &(p_data_tbl[i].server_mailbox_name), 0, col_index++);
8455                         _get_table_field_data_string(result, &(p_data_tbl[i].server_mail_id), 0, col_index++);
8456                         break;
8457
8458                 case RETRIEVE_ACCOUNT:
8459                         _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8460                         break;
8461
8462                 case RETRIEVE_FLAG:
8463                         _get_table_field_data_int(result, &(p_data_tbl[i].account_id), col_index++);
8464                         _get_table_field_data_int(result, &(p_data_tbl[i].mail_id), col_index++);
8465                         _get_table_field_data_int(result, &(p_data_tbl[i].mailbox_id), col_index++);
8466                         _get_table_field_data_char(result, &(p_data_tbl[i].flags_seen_field), col_index++);
8467                         _get_table_field_data_int(result, &(p_data_tbl[i].thread_id), col_index++);
8468                         break;
8469                 }
8470         }
8471
8472         ret = true;
8473
8474 FINISH_OFF:
8475         if (ret == true)
8476                 *mail = p_data_tbl;
8477         else
8478                 emstorage_free_mail(&p_data_tbl, number_of_mails, NULL);
8479
8480         if (result)
8481                 sqlite3_free_table(result);
8482
8483         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8484
8485         EM_SAFE_FREE(sql_query_string);
8486
8487         if (err_code != NULL)
8488                 *err_code = error;
8489
8490         EM_DEBUG_FUNC_END("ret [%d]", ret);
8491         return ret;
8492 }
8493                                         /* LCOV_EXCL_STOP */
8494 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)
8495 {
8496         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8497
8498         if (mail_id <= 0 || !mail) {
8499                 EM_DEBUG_EXCEPTION("mail_id[%d], mail[%p]", mail_id, mail);
8500                 if (err_code != NULL)
8501                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8502                 return false;
8503         }
8504
8505         int ret = false, error = EMAIL_ERROR_NONE, count;
8506         char conditional_clause[QUERY_SIZE] = {0, };
8507         emstorage_mail_tbl_t* p_data_tbl = NULL;
8508
8509         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8510         EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8511
8512         if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8513                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8514                 goto FINISH_OFF;
8515         }
8516
8517         ret = true;
8518
8519 FINISH_OFF:
8520         if (ret == true)
8521                 *mail = p_data_tbl;
8522         else if (p_data_tbl != NULL)
8523                 emstorage_free_mail(&p_data_tbl, 1, &error);
8524
8525         if (err_code != NULL)
8526                 *err_code = error;
8527
8528         EM_DEBUG_FUNC_END("ret [%d]", ret);
8529         return ret;
8530 }
8531
8532 #ifdef __FEATURE_BODY_SEARCH__
8533                                         /* LCOV_EXCL_START */
8534 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)
8535 {
8536         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
8537
8538         if (mail_id <= 0 || !mail_text) {
8539                 EM_DEBUG_EXCEPTION("mail_id[%d], mail_text[%p]", mail_id, mail_text);
8540                 if (err_code != NULL)
8541                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8542                 return false;
8543         }
8544
8545         int ret = false;
8546         int error = EMAIL_ERROR_NONE;
8547         int count = 0;
8548         char conditional_clause[QUERY_SIZE] = {0, };
8549         emstorage_mail_text_tbl_t *p_data_tbl = NULL;
8550
8551         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE mail_id = %d", mail_id);
8552         EM_DEBUG_LOG_SEC("query = [%s]", conditional_clause);
8553
8554         if (!emstorage_query_mail_text_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
8555                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl [%d]", error);
8556                 goto FINISH_OFF;
8557         }
8558
8559         ret = true;
8560
8561 FINISH_OFF:
8562         if (ret == true)
8563                 *mail_text = p_data_tbl;
8564         else if (p_data_tbl != NULL)
8565                 emstorage_free_mail_text(&p_data_tbl, 1, &error);
8566
8567         if (err_code != NULL)
8568                 *err_code = error;
8569
8570         EM_DEBUG_FUNC_END("ret [%d]", ret);
8571         return ret;
8572 }
8573 #endif
8574
8575 INTERNAL_FUNC int emstorage_mail_search_start(char *multi_user_name,
8576                 emstorage_search_filter_t *search,
8577                 int account_id,
8578                 int mailbox_id,
8579                 int sorting,
8580                 DB_STMT *search_handle,
8581                 int *searched,
8582                 int transaction,
8583                 int *err_code)
8584 {
8585         EM_DEBUG_FUNC_BEGIN("search[%p], account_id[%d], mailbox_id[%d], sorting[%d], "
8586                         "search_handle[%p], searched[%p], transaction[%d], err_code[%p]",
8587                         search, account_id, mailbox_id, sorting, search_handle,
8588                         searched, transaction, err_code);
8589
8590         if (!search_handle || !searched) {
8591                 if (err_code != NULL)
8592                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8593                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
8594                 EM_DEBUG_FUNC_END("false");
8595                 return false;
8596         }
8597
8598         emstorage_search_filter_t* p = search;
8599         int error = EMAIL_ERROR_NONE;
8600         DB_STMT hStmt = NULL;
8601         char sql_query_string[QUERY_SIZE] = {0, };
8602         int rc, ret = false;
8603         int and = false, mail_count = 0;
8604
8605         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8606         EMSTORAGE_START_READ_TRANSACTION(transaction);
8607
8608         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_tbl");
8609
8610         if (account_id != ALL_ACCOUNT) {
8611                 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);
8612                 and = true;
8613         }
8614
8615         if (mailbox_id) {
8616                 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);
8617                 and = true;
8618         }
8619
8620         while (p) {
8621                 if (p->key_type) {
8622                         if (!strncmp(p->key_type, "subject", strlen("subject"))) {
8623                                 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);
8624                                 and = true;
8625                         } else if (!strncmp(p->key_type, "full_address_from", strlen("full_address_from"))) {
8626                                 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);
8627                                 and = true;
8628                         } else if (!strncmp(p->key_type, "full_address_to", strlen("full_address_to"))) {
8629                                 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);
8630                                 and = true;
8631                         } else if (!strncmp(p->key_type, "email_address", strlen("email_address"))) {
8632                                 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);
8633                                 and = true;
8634                         }
8635                         p = p->next;
8636                 }
8637         }
8638
8639         if (sorting)
8640                 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");
8641
8642         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
8643
8644
8645         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8646         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8647                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8648
8649
8650         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8651         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8652                         ("sqlite3_step fail:%d", rc));
8653
8654         char **result;
8655
8656         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &mail_count, NULL, NULL), rc);
8657         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
8658                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8659
8660         sqlite3_free_table(result);
8661
8662         ret = true;
8663
8664 FINISH_OFF:
8665         if (ret == true) {
8666                 *search_handle = hStmt;
8667                 *searched = mail_count;
8668                 EM_DEBUG_LOG("mail_count [%d]", mail_count);
8669         } else {
8670                 if (hStmt != NULL) {
8671                         rc = sqlite3_finalize(hStmt);
8672                         if (rc != SQLITE_OK) {
8673                                 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8674                                 error = EMAIL_ERROR_DB_FAILURE;
8675                         }
8676                 }
8677
8678                 EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8679         }
8680
8681         if (err_code != NULL)
8682                 *err_code = error;
8683
8684         EM_DEBUG_FUNC_END("ret [%d]", ret);
8685         return ret;
8686 }
8687
8688 INTERNAL_FUNC int emstorage_mail_search_result(DB_STMT search_handle, emstorage_mail_field_type_t type, void** data, int transaction, int *err_code)
8689 {
8690         EM_DEBUG_FUNC_BEGIN("search_handle[%d], type[%d], data[%p], transaction[%d], err_code[%p]", search_handle, type, data, transaction, err_code);
8691
8692         if (search_handle == 0 || !data) {
8693                 EM_DEBUG_EXCEPTION("No Search_handle type[%d], data[%p]", type, data);
8694
8695                 if (err_code != NULL)
8696                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8697                 return false;
8698         }
8699
8700         emstorage_mail_tbl_t* p_data_tbl = NULL;
8701         DB_STMT hStmt = search_handle;
8702         int rc, ret = false;
8703         int error = EMAIL_ERROR_NONE;
8704
8705         switch (type) {
8706         case RETRIEVE_ID:
8707                 _get_stmt_field_data_int(hStmt, (int *)data, MAIL_ID_IDX_IN_MAIL_TBL);
8708                 break;
8709
8710         case RETRIEVE_ENVELOPE:
8711         case RETRIEVE_ALL:
8712                 if (!(p_data_tbl = em_malloc(sizeof(emstorage_mail_tbl_t)))) {
8713                         EM_DEBUG_EXCEPTION(" em_mallocfailed...");
8714                         error = EMAIL_ERROR_OUT_OF_MEMORY;
8715                         goto FINISH_OFF;
8716                 }
8717
8718                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8719                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8720                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_size), MAIL_SIZE_IDX_IN_MAIL_TBL);
8721                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8722                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_from), 1, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL);
8723                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_to), 1, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL);
8724                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->subject), 1, SUBJECT_IDX_IN_MAIL_TBL);
8725                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->body_download_status), BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL);
8726                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_plain), 0, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL);
8727                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_html), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8728                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->file_path_mime_entity), 0, FILE_PATH_HTML_IDX_IN_MAIL_TBL);
8729                 _get_stmt_field_data_time_t(hStmt, &(p_data_tbl->date_time), DATETIME_IDX_IN_MAIL_TBL);
8730                 _get_stmt_field_data_char(hStmt, &(p_data_tbl->flags_seen_field), FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL);
8731                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->DRM_status), DRM_STATUS_IDX_IN_MAIL_TBL);
8732                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->priority), PRIORITY_IDX_IN_MAIL_TBL);
8733                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->save_status), SAVE_STATUS_IDX_IN_MAIL_TBL);
8734                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->lock_status), LOCK_STATUS_IDX_IN_MAIL_TBL);
8735                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->report_status), REPORT_STATUS_IDX_IN_MAIL_TBL);
8736                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8737                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->meeting_request_status), MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL);
8738                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->message_class), MESSAGE_CLASS_IDX_IN_MAIL_TBL);
8739                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->digest_type), DIGEST_TYPE_IDX_IN_MAIL_TBL);
8740                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->smime_type), SMIME_TYPE_IDX_IN_MAIL_TBL);
8741                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->scheduled_sending_time), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8742                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->remaining_resend_times), SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL);
8743                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->tag_id), TAG_ID_IDX_IN_MAIL_TBL);
8744                 _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->eas_data_length), EAS_DATA_LENGTH_IDX_IN_MAIL_TBL);
8745                 _get_stmt_field_data_blob(hStmt, (void**)&(p_data_tbl->eas_data), EAS_DATA_IDX_IN_MAIL_TBL);
8746
8747                 if (type == RETRIEVE_ALL) {
8748                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8749                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8750                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_reply), 1, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL);
8751                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_cc), 1, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL);
8752                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_bcc), 1, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL);
8753                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->full_address_return), 1, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL);
8754                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->message_id), 0, MESSAGE_ID_IDX_IN_MAIL_TBL);
8755                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8756                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8757                         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_count), ATTACHMENT_COUNT_IDX_IN_MAIL_TBL);
8758                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->preview_text), 1, PREVIEW_TEXT_IDX_IN_MAIL_TBL);
8759                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->replied_time), REPLIED_TIME_IDX_IN_MAIL_TBL);
8760                         _get_stmt_field_data_int(hStmt, (int*)&(p_data_tbl->forwarded_time), FORWARDED_TIME_IDX_IN_MAIL_TBL);
8761                         _get_stmt_field_data_string(hStmt, &(p_data_tbl->default_charset), 0, DEFAULT_CHARSET_IDX_IN_MAIL_TBL);
8762                 }
8763
8764                 if (p_data_tbl->body_download_status) {
8765                         struct stat buf;
8766
8767                         if (p_data_tbl->file_path_html) {
8768                                 if (stat(p_data_tbl->file_path_html, &buf) == -1)
8769                                         p_data_tbl->body_download_status = 0;
8770                         } else if (p_data_tbl->file_path_plain) {
8771                                 if (stat(p_data_tbl->file_path_plain, &buf) == -1)
8772                                         p_data_tbl->body_download_status = 0;
8773                         } else
8774                                 p_data_tbl->body_download_status = 0;
8775                 }
8776
8777                 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8778                 break;
8779
8780         case RETRIEVE_SUMMARY:
8781                 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
8782                         EM_DEBUG_EXCEPTION(" malloc failed...");
8783
8784                         error = EMAIL_ERROR_OUT_OF_MEMORY;
8785                         goto FINISH_OFF;
8786                 }
8787
8788                 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8789
8790                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8791                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_TBL);
8792                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->server_mail_status), SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL);
8793                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mailbox_name), 0, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL);
8794                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->server_mail_id), 0, SERVER_MAIL_ID_IDX_IN_MAIL_TBL);
8795
8796                 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8797                 break;
8798
8799         case RETRIEVE_ADDRESS:
8800                 if (!(p_data_tbl = malloc(sizeof(emstorage_mail_tbl_t)))) {
8801                         EM_DEBUG_EXCEPTION(" malloc failed...");
8802                         error = EMAIL_ERROR_OUT_OF_MEMORY;
8803                         goto FINISH_OFF;
8804                 }
8805
8806                 memset(p_data_tbl, 0x00, sizeof(emstorage_mail_tbl_t));
8807                 _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_TBL);
8808                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_sender), 1, EMAIL_ADDRESS_SENDER_IDX_IN_MAIL_TBL);
8809                 _get_stmt_field_data_string(hStmt, &(p_data_tbl->email_address_recipient), 1, EMAIL_ADDRESS_RECIPIENT_IDX_IN_MAIL_TBL);
8810                 *((emstorage_mail_tbl_t**)data) = p_data_tbl;
8811                 break;
8812
8813         default:
8814                 break;
8815         }
8816
8817         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8818         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8819                         ("sqlite3_step fail:%d", rc));
8820
8821         ret = true;
8822
8823 FINISH_OFF:
8824
8825         if (err_code != NULL)
8826                 *err_code = error;
8827
8828         if (ret == false && p_data_tbl)
8829                 emstorage_free_mail(&p_data_tbl, 1, NULL);
8830
8831         EM_DEBUG_FUNC_END("ret [%d]", ret);
8832         return ret;
8833 }
8834
8835 INTERNAL_FUNC int emstorage_mail_search_end(DB_STMT search_handle, int transaction, int *err_code)
8836 {
8837         EM_DEBUG_FUNC_BEGIN("search_handle[%d], transaction[%d], err_code[%p]", search_handle, transaction, err_code);
8838
8839         int error = EMAIL_ERROR_NONE;
8840         int rc, ret = false;
8841
8842         if (search_handle == 0) {
8843                 EM_DEBUG_EXCEPTION("No search_handle");
8844                 error = EMAIL_ERROR_INVALID_PARAM;
8845                 goto FINISH_OFF;
8846         }
8847
8848         DB_STMT hStmt = search_handle;
8849
8850         rc = sqlite3_finalize(hStmt);
8851         if (rc != SQLITE_OK) {
8852                 EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
8853                 error = EMAIL_ERROR_DB_FAILURE;
8854         }
8855
8856         ret = true;
8857
8858 FINISH_OFF:
8859         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
8860
8861         if (err_code != NULL)
8862                 *err_code = error;
8863
8864         EM_DEBUG_FUNC_END("ret [%d]", ret);
8865         return ret;
8866 }
8867
8868 INTERNAL_FUNC int emstorage_change_mail(char *multi_user_name, int mail_id, emstorage_mail_tbl_t *mail, int transaction, int *err_code)
8869 {
8870         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, mail, transaction, err_code);
8871
8872         if (mail_id <= 0 || !mail) {
8873                 EM_DEBUG_EXCEPTION(" mail_id[%d], mail[%p]", mail_id, mail);
8874
8875                 if (err_code != NULL)
8876                         *err_code = EMAIL_ERROR_INVALID_PARAM;
8877                 return false;
8878         }
8879
8880         DB_STMT hStmt = NULL;
8881         char sql_query_string[QUERY_SIZE] = {0, };
8882         int rc = -1;
8883         int ret = false;
8884         int error = EMAIL_ERROR_NONE;
8885         int i = 0;
8886         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
8887         char mailbox_id_param_string[10] = {0,};
8888
8889         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
8890
8891         SNPRINTF(sql_query_string, sizeof(sql_query_string),
8892                         "UPDATE mail_tbl SET"
8893                         "  mail_id = ?"
8894                         ", account_id = ?"
8895                         ", mailbox_id = ?"
8896                         ", mail_size = ?"
8897                         ", server_mail_status = ?"
8898                         ", server_mailbox_name = ?"
8899                         ", server_mail_id = ?"
8900                         ", reference_mail_id = ?"
8901                         ", full_address_from = ?"
8902                         ", full_address_reply = ?"  /* 10 */
8903                         ", full_address_to = ?"
8904                         ", full_address_cc = ?"
8905                         ", full_address_bcc = ?"
8906                         ", full_address_return = ?"
8907                         ", subject = ?"
8908                         ", body_download_status = ?"
8909                         ", file_path_plain = ?"
8910                         ", file_path_html = ?"
8911                         ", file_path_mime_entity = ?"
8912                         ", date_time = ?"
8913                         ", flags_seen_field      = ?"
8914                         ", flags_deleted_field   = ?"
8915                         ", flags_flagged_field   = ?"
8916                         ", flags_answered_field  = ?"
8917                         ", flags_recent_field    = ?"
8918                         ", flags_draft_field     = ?"
8919                         ", flags_forwarded_field = ?"
8920                         ", DRM_status = ?"
8921                         ", priority = ?"
8922                         ", save_status = ?"
8923                         ", lock_status = ?"
8924                         ", message_id = ?"
8925                         ", report_status = ?"
8926                         ", preview_text = ?"
8927                         ", smime_type = ?"
8928                         ", scheduled_sending_time = ?"
8929                         ", remaining_resend_times = ?"
8930                         ", tag_id = ?"
8931                         ", replied_time = ?"
8932                         ", forwarded_time = ?"
8933                         ", default_charset = ?"
8934                         ", eas_data_length = ?"
8935                         ", eas_data = ?"
8936                         " WHERE mail_id = %d AND account_id != 0 "
8937                         , mail_id);
8938
8939
8940         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
8941         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8942                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
8943
8944         _bind_stmt_field_data_int(hStmt, i++, mail->mail_id);
8945         _bind_stmt_field_data_int(hStmt, i++, mail->account_id);
8946         _bind_stmt_field_data_int(hStmt, i++, mail->mailbox_id);
8947         _bind_stmt_field_data_int(hStmt, i++, mail->mail_size);
8948         _bind_stmt_field_data_int(hStmt, i++, mail->server_mail_status);
8949         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mailbox_name, 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
8950         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->server_mail_id, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
8951         _bind_stmt_field_data_int(hStmt, i++, mail->reference_mail_id);
8952         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
8953         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
8954         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
8955         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
8956         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
8957         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
8958         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
8959         _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
8960         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
8961         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
8962         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
8963         _bind_stmt_field_data_int(hStmt, i++, mail->date_time);
8964         _bind_stmt_field_data_char(hStmt, i++, mail->flags_seen_field);
8965         _bind_stmt_field_data_char(hStmt, i++, mail->flags_deleted_field);
8966         _bind_stmt_field_data_char(hStmt, i++, mail->flags_flagged_field);
8967         _bind_stmt_field_data_char(hStmt, i++, mail->flags_answered_field);
8968         _bind_stmt_field_data_char(hStmt, i++, mail->flags_recent_field);
8969         _bind_stmt_field_data_char(hStmt, i++, mail->flags_draft_field);
8970         _bind_stmt_field_data_char(hStmt, i++, mail->flags_forwarded_field);
8971         _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
8972         _bind_stmt_field_data_int(hStmt, i++, mail->priority);
8973         _bind_stmt_field_data_int(hStmt, i++, mail->save_status);
8974         _bind_stmt_field_data_int(hStmt, i++, mail->lock_status);
8975         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->message_id, 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
8976         _bind_stmt_field_data_int(hStmt, i++, mail->report_status);
8977         _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
8978         _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
8979         _bind_stmt_field_data_int(hStmt, i++, mail->scheduled_sending_time);
8980         _bind_stmt_field_data_int(hStmt, i++, mail->remaining_resend_times);
8981         _bind_stmt_field_data_int(hStmt, i++, mail->tag_id);
8982         _bind_stmt_field_data_int(hStmt, i++, mail->replied_time);
8983         _bind_stmt_field_data_int(hStmt, i++, mail->forwarded_time);
8984         _bind_stmt_field_data_string(hStmt, i++, (char *)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
8985         _bind_stmt_field_data_int(hStmt, i++, mail->eas_data_length);
8986         _bind_stmt_field_data_blob(hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
8987
8988         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
8989         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
8990                         ("sqlite3_step fail:%d", rc));
8991
8992         rc = sqlite3_changes(local_db_handle);
8993         if (rc == 0) {
8994                 EM_DEBUG_LOG(" no matched mail found...");
8995                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
8996                 goto FINISH_OFF;
8997         }
8998
8999         ret = true;
9000
9001 FINISH_OFF:
9002         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9003         if (hStmt != NULL) {
9004                 rc = sqlite3_finalize(hStmt);
9005                 if (rc != SQLITE_OK) {
9006                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9007                         error = EMAIL_ERROR_DB_FAILURE;
9008                 }
9009         }
9010
9011
9012         if (error == EMAIL_ERROR_NONE && mail) {
9013                 SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
9014                 if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail->mail_id, mailbox_id_param_string, 0))
9015                         EM_DEBUG_EXCEPTION("emcore_notify_storage_eventfailed [NOTI_MAIL_UPDATE]");
9016         }
9017
9018         if (err_code != NULL)
9019                 *err_code = error;
9020
9021         EM_DEBUG_FUNC_END("ret [%d]", ret);
9022         return ret;
9023 }
9024
9025 /**
9026  *  emstorage_clean_save_status(int save_status, int  *err_code) - set the all mail status to the set value
9027  *
9028  *
9029  **/
9030 INTERNAL_FUNC int emstorage_clean_save_status(char *multi_user_name, int save_status, int  *err_code)
9031 {
9032         EM_DEBUG_FUNC_BEGIN("save_status[%d], err_code[%p]", save_status, err_code);
9033
9034         EM_IF_NULL_RETURN_VALUE(err_code, false);
9035
9036         int ret = false;
9037         int error = EMAIL_ERROR_NONE;
9038         int rc = 0;
9039         char sql_query_string[QUERY_SIZE] = {0, };
9040         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9041
9042         memset(sql_query_string, 0x00, sizeof(sql_query_string));
9043         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);
9044
9045         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9046         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9047         if (error != EMAIL_ERROR_NONE) {
9048                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9049                 goto FINISH_OFF;
9050         }
9051
9052         rc = sqlite3_changes(local_db_handle);
9053         if (rc == 0) {
9054                 EM_DEBUG_LOG(" No Matched Mail Exists ");
9055                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
9056         }
9057
9058         ret = true;
9059
9060 FINISH_OFF:
9061
9062         if (err_code != NULL)
9063                 *err_code = error;
9064
9065         EM_DEBUG_FUNC_END("ret [%d]", ret);
9066         return ret;
9067 }
9068
9069 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)
9070 {
9071         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);
9072         int i = 0;
9073         int error = EMAIL_ERROR_NONE;
9074         int ret = false;
9075         int query_size = 0;
9076         int cur_mail_id_string = 0;
9077         int mail_id_string_buffer_length = 0;
9078         int parameter_string_length = 0;
9079         char  *sql_query_string = NULL;
9080         char *mail_id_string_buffer = NULL;
9081         char *parameter_string = NULL;
9082         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9083         email_mail_attribute_type target_mail_attribute_type = 0;
9084
9085         if (!mail_ids  || !field_name || account_id == 0) {
9086                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
9087                 if (err_code != NULL)
9088                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9089                 return false;
9090         }
9091
9092         if ((error = emcore_get_attribute_type_by_mail_field_name(field_name, &target_mail_attribute_type)) != EMAIL_ERROR_NONE) {
9093                 EM_DEBUG_EXCEPTION("emstorageemcore_get_attribute_type_by_mail_field_name failed [%d]", error);
9094                 if (err_code != NULL)
9095                         *err_code = error;
9096                 return false;
9097         }
9098
9099         /* Generating mail id list string */
9100         mail_id_string_buffer_length = MAIL_ID_STRING_LENGTH * mail_ids_count;
9101
9102         mail_id_string_buffer = em_malloc(mail_id_string_buffer_length);
9103
9104         if (!mail_id_string_buffer) {
9105                 EM_DEBUG_EXCEPTION("em_mallocfailed");
9106                 if (err_code != NULL)
9107                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
9108                 return false;
9109         }
9110
9111         for (i = 0; i < mail_ids_count; i++)
9112                 cur_mail_id_string += SNPRINTF_OFFSET(mail_id_string_buffer, cur_mail_id_string, mail_id_string_buffer_length, "%d,", mail_ids[i]);
9113
9114         if (EM_SAFE_STRLEN(mail_id_string_buffer) > 1)
9115                 mail_id_string_buffer[EM_SAFE_STRLEN(mail_id_string_buffer) - 1] = NULL_CHAR;
9116
9117         /* Generating notification parameter string */
9118         parameter_string_length = mail_id_string_buffer_length + EM_SAFE_STRLEN(field_name) + 2;
9119         parameter_string = em_malloc(parameter_string_length);
9120
9121         if (!parameter_string) {
9122                 EM_DEBUG_EXCEPTION("em_mallocfailed");
9123                 if (err_code != NULL)
9124                         *err_code = EMAIL_ERROR_OUT_OF_MEMORY;
9125                 EM_SAFE_FREE(mail_id_string_buffer);
9126                 return false;
9127         }
9128
9129         SNPRINTF(parameter_string, parameter_string_length, "%s%c%s", field_name, 0x01, mail_id_string_buffer);
9130         query_size = EM_SAFE_STRLEN(mail_id_string_buffer) + EM_SAFE_STRLEN(field_name) + 250;
9131
9132         sql_query_string = em_malloc(query_size);
9133         if (sql_query_string == NULL) {
9134                 EM_DEBUG_EXCEPTION("em_mallocfailed");
9135                 error = EMAIL_ERROR_OUT_OF_MEMORY;
9136                 goto FINISH_OFF;
9137         }
9138         /* Write query string */
9139         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);
9140
9141         EM_DEBUG_LOG_DEV("sql_query_string [%s]", sql_query_string);
9142
9143         /* Execute query */
9144         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9145         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
9146         if (error != EMAIL_ERROR_NONE) {
9147                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
9148                 goto FINISH_OFF;
9149         }
9150
9151         if (sqlite3_changes(local_db_handle) == 0)
9152                 EM_DEBUG_LOG("no mail matched...");
9153
9154         ret = true;
9155
9156 FINISH_OFF:
9157         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9158
9159         if (error == EMAIL_ERROR_NONE && parameter_string) {
9160                 if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, target_mail_attribute_type, parameter_string, value))
9161                         EM_DEBUG_EXCEPTION_SEC("emcore_notify_storage_eventfailed : NOTI_MAIL_FIELD_UPDATE [%s,%d]", field_name, value);
9162         }
9163
9164         EM_SAFE_FREE(mail_id_string_buffer);
9165         EM_SAFE_FREE(parameter_string);
9166         EM_SAFE_FREE(sql_query_string);
9167
9168
9169         if (err_code != NULL)
9170                 *err_code = error;
9171
9172         EM_DEBUG_FUNC_END("error [%d]", error);
9173         return ret;
9174 }
9175
9176 #ifdef __FEATURE_BODY_SEARCH__
9177 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)
9178 {
9179         EM_DEBUG_FUNC_BEGIN("mail_id[%d], mail_text[%p], transaction[%d], err_code[%p]", mail_id, mail_text, transaction, err_code);
9180
9181         if (mail_id <= 0 || !mail_text) {
9182                 EM_DEBUG_EXCEPTION(" mail_id[%d], mail_text[%p]", mail_id, mail_text);
9183                 if (err_code != NULL)
9184                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9185                 return false;
9186         }
9187
9188         int ret = false;
9189         int error = EMAIL_ERROR_NONE;
9190         DB_STMT hStmt = NULL;
9191         char sql_query_string[QUERY_SIZE] = {0, };
9192
9193         int i = 0;
9194         int rc = 0;
9195
9196         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9197         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9198
9199         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9200                         "UPDATE mail_text_tbl SET"
9201                         " body_text = ?"
9202                         " WHERE mail_id = %d AND account_id != 0"
9203                         , mail_id);
9204         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9205
9206         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9207         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9208                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9209
9210         i = 0;
9211         _bind_stmt_field_data_string(hStmt, i++, (char *)mail_text->body_text, 0, -1);
9212
9213         if (hStmt != NULL) {
9214                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9215                 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9216                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
9217
9218                 rc = sqlite3_changes(local_db_handle);
9219                 if (rc == 0) {
9220                         EM_DEBUG_LOG(" no matched mail found...");
9221                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
9222                         goto FINISH_OFF;
9223                 }
9224         }
9225
9226         ret = true;
9227
9228 FINISH_OFF:
9229         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9230
9231         if (hStmt != NULL) {
9232                 rc = sqlite3_finalize(hStmt);
9233                 if (rc != SQLITE_OK) {
9234                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9235                         error = EMAIL_ERROR_DB_FAILURE;
9236                 }
9237                 hStmt = NULL;
9238         }
9239
9240         if (err_code != NULL)
9241                 *err_code = error;
9242
9243         EM_DEBUG_FUNC_END("ret [%d]", ret);
9244         return ret;
9245 }
9246 #endif
9247
9248 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)
9249 {
9250         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], mail[%p], transaction[%d], err_code[%p]", mail_id, type, mail, transaction, err_code);
9251
9252         int ret = false;
9253         int error = EMAIL_ERROR_NONE;
9254         int move_flag = 0;
9255         int rc = 0;
9256         int i = 0;
9257         int mailbox_id = 0;
9258         DB_STMT hStmt = NULL;
9259         char sql_query_string[QUERY_SIZE] = {0, };
9260         char mailbox_id_param_string[10] = {0,};
9261         sqlite3 *local_db_handle = NULL;
9262
9263         if (mail_id <= 0 || !mail) {
9264                 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], mail[%p]", mail_id, type, mail);
9265                 if (err_code != NULL)
9266                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9267                 return false;
9268         }
9269
9270         local_db_handle = emstorage_get_db_connection(multi_user_name);
9271
9272         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9273
9274         switch (type) {
9275         case APPEND_BODY:
9276                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
9277                                 "UPDATE mail_tbl SET"
9278                                 "  body_download_status = ?"
9279                                 ", file_path_plain = ?"
9280                                 ", file_path_html = ?"
9281                                 ", file_path_mime_entity = ?"
9282                                 ", flags_seen_field      = ?"
9283                                 ", flags_deleted_field   = ?"
9284                                 ", flags_flagged_field   = ?"
9285                                 ", flags_answered_field  = ?"
9286                                 ", flags_recent_field    = ?"
9287                                 ", flags_draft_field     = ?"
9288                                 ", flags_forwarded_field = ?"
9289                                 ", DRM_status = ?"
9290                                 ", attachment_count = ?"
9291                                 ", preview_text= ?"
9292                                 ", meeting_request_status = ? "
9293                                 ", message_class = ? "
9294                                 ", digest_type = ? "
9295                                 ", smime_type = ? "
9296                                 " WHERE mail_id = %d AND account_id != 0"
9297                                 , mail_id);
9298
9299
9300                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9301                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9302                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9303                 i = 0;
9304
9305                 _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9306                 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9307                 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9308                 _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9309                 _bind_stmt_field_data_int(hStmt, i++, mail->flags_seen_field);
9310                 _bind_stmt_field_data_int(hStmt, i++, mail->flags_deleted_field);
9311                 _bind_stmt_field_data_int(hStmt, i++, mail->flags_flagged_field);
9312                 _bind_stmt_field_data_int(hStmt, i++, mail->flags_answered_field);
9313                 _bind_stmt_field_data_int(hStmt, i++, mail->flags_recent_field);
9314                 _bind_stmt_field_data_int(hStmt, i++, mail->flags_draft_field);
9315                 _bind_stmt_field_data_int(hStmt, i++, mail->flags_forwarded_field);
9316                 _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
9317                 _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9318                 _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text, 0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9319                 _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
9320                 _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
9321                 _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9322                 _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9323                 break;
9324
9325         case UPDATE_MAILBOX: {
9326                                                          int err;
9327                                                          emstorage_mailbox_tbl_t *mailbox_tbl;
9328
9329                                                          if ((err = emstorage_get_mailbox_by_id(multi_user_name, mail->mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
9330                                                                  EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_id failed [%d]", err);
9331                                                                  goto FINISH_OFF;
9332                                                          }
9333
9334                                                          SNPRINTF(sql_query_string, sizeof(sql_query_string),
9335                                                                          "UPDATE mail_tbl SET"
9336                                                                          " mailbox_id = '%d'"
9337                                                                          ",mailbox_type = '%d'"
9338                                                                          " WHERE mail_id = %d AND account_id != 0"
9339                                                                          , mailbox_tbl->mailbox_id
9340                                                                          , mailbox_tbl->mailbox_type
9341                                                                          , mail_id);
9342                                                          move_flag = 1;
9343
9344
9345                                                          EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9346                                                          EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9347                                                                          ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9348
9349                                                          i = 0;
9350                                                          _bind_stmt_field_data_string(hStmt, i++, (char *)mailbox_tbl->mailbox_name, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
9351
9352                                                          emstorage_free_mailbox(&mailbox_tbl, 1, NULL); /*prevent 26251*/
9353
9354                                                  }
9355                                                  break;
9356
9357         case UPDATE_FLAG:
9358                                                  SNPRINTF(sql_query_string, sizeof(sql_query_string),
9359                                                                  "UPDATE mail_tbl SET"
9360                                                                  " flags_seen_field      = %d"
9361                                                                  ",flags_deleted_field   = %d"
9362                                                                  ",flags_flagged_field   = %d"
9363                                                                  ",flags_answered_field  = %d"
9364                                                                  ",flags_recent_field    = %d"
9365                                                                  ",flags_draft_field     = %d"
9366                                                                  ",flags_forwarded_field = %d"
9367                                                                  "  WHERE mail_id = %d AND account_id != 0"
9368                                                                  , mail->flags_seen_field
9369                                                                  , mail->flags_deleted_field
9370                                                                  , mail->flags_flagged_field
9371                                                                  , mail->flags_answered_field
9372                                                                  , mail->flags_recent_field
9373                                                                  , mail->flags_draft_field
9374                                                                  , mail->flags_forwarded_field
9375                                                                  , mail_id);
9376                                                  EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9377
9378
9379                                                  EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9380                                                  EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9381                                                                  ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9382
9383                                                  break;
9384
9385         case UPDATE_EXTRA_FLAG:
9386                                                  SNPRINTF(sql_query_string, sizeof(sql_query_string),
9387                                                                  "UPDATE mail_tbl SET"
9388                                                                  "  priority = %d"
9389                                                                  ", save_status = %d"
9390                                                                  ", lock_status = %d"
9391                                                                  ", report_status = %d"
9392                                                                  ", DRM_status = %d"
9393                                                                  " WHERE mail_id = %d AND account_id != 0"
9394                                                                  , mail->priority
9395                                                                  , mail->save_status
9396                                                                  , mail->lock_status
9397                                                                  , mail->report_status
9398                                                                  , mail->DRM_status
9399                                                                  , mail_id);
9400                                                  EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9401
9402
9403                                                  EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9404                                                  EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9405                                                                  ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9406                                                  break;
9407
9408         case UPDATE_STICKY_EXTRA_FLAG:
9409                                                  SNPRINTF(sql_query_string, sizeof(sql_query_string),
9410                                                                  "UPDATE mail_tbl SET"
9411                                                                  "  lock_status = %d"
9412                                                                  "  WHERE mail_id = %d AND account_id != 0"
9413                                                                  , mail->lock_status
9414                                                                  , mail_id);
9415                                                  EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
9416
9417
9418                                                  EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9419                                                  EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9420                                                                  ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9421                                                  break;
9422
9423         case UPDATE_MAIL:
9424                                                  SNPRINTF(sql_query_string, sizeof(sql_query_string),
9425                                                                  "UPDATE mail_tbl SET"
9426                                                                  "  full_address_from = ?"
9427                                                                  ", full_address_reply = ?"
9428                                                                  ", full_address_to = ?"
9429                                                                  ", full_address_cc = ?"
9430                                                                  ", full_address_bcc = ?"
9431                                                                  ", full_address_return = ?"
9432                                                                  ", subject = ?"
9433                                                                  ", file_path_plain = ?"
9434                                                                  ", date_time = ?"
9435                                                                  ", flags_seen_field = ?"
9436                                                                  ", flags_deleted_field = ?"
9437                                                                  ", flags_flagged_field = ?"
9438                                                                  ", flags_answered_field = ?"
9439                                                                  ", flags_recent_field = ?"
9440                                                                  ", flags_draft_field = ?"
9441                                                                  ", flags_forwarded_field = ?"
9442                                                                  ", priority = ?"
9443                                                                  ", save_status = ?"
9444                                                                  ", lock_status = ?"
9445                                                                  ", report_status = ?"
9446                                                                  ", DRM_status = ?"
9447                                                                  ", file_path_html = ?"
9448                                                                  ", file_path_mime_entity = ?"
9449                                                                  ", mail_size = ?"
9450                                                                  ", preview_text = ?"
9451                                                                  ", body_download_status = ?"
9452                                                                  ", attachment_count = ?"
9453                                                                  ", inline_content_count = ?"
9454                                                                  ", meeting_request_status = ?"
9455                                                                  ", message_class = ?"
9456                                                                  ", digest_type = ?"
9457                                                                  ", smime_type = ?"
9458                                                                  ", scheduled_sending_time = ?"
9459                                                                  ", remaining_resend_times = ?"
9460                                                                  ", tag_id = ?"
9461                                                                  ", replied_time = ?"
9462                                                                  ", forwarded_time = ?"
9463                                                                  ", default_charset = ?"
9464                                                                  ", eas_data_length = ?"
9465                                                                  ", eas_data = ?"
9466                                                                  " WHERE mail_id = %d AND account_id != 0"
9467                                                                  , mail_id);
9468
9469
9470                                                  EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9471                                                  EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
9472                                                  EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9473                                                                  ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9474                                                  i = 0;
9475                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_from, 1, FROM_LEN_IN_MAIL_TBL);
9476                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_reply, 1, REPLY_TO_LEN_IN_MAIL_TBL);
9477                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_to, 1, TO_LEN_IN_MAIL_TBL);
9478                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
9479                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
9480                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->full_address_return, 1, RETURN_PATH_LEN_IN_MAIL_TBL);
9481                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9482                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9483                                                  _bind_stmt_field_data_time_t(hStmt, i++, mail->date_time);
9484                                                  _bind_stmt_field_data_char(hStmt, i++, mail->flags_seen_field);
9485                                                  _bind_stmt_field_data_char(hStmt, i++, mail->flags_deleted_field);
9486                                                  _bind_stmt_field_data_char(hStmt, i++, mail->flags_flagged_field);
9487                                                  _bind_stmt_field_data_char(hStmt, i++, mail->flags_answered_field);
9488                                                  _bind_stmt_field_data_char(hStmt, i++, mail->flags_recent_field);
9489                                                  _bind_stmt_field_data_char(hStmt, i++, mail->flags_draft_field);
9490                                                  _bind_stmt_field_data_char(hStmt, i++, mail->flags_forwarded_field);
9491                                                  _bind_stmt_field_data_int(hStmt, i++, mail->priority);
9492                                                  _bind_stmt_field_data_int(hStmt, i++, mail->save_status);
9493                                                  _bind_stmt_field_data_int(hStmt, i++, mail->lock_status);
9494                                                  _bind_stmt_field_data_int(hStmt, i++, mail->report_status);
9495                                                  _bind_stmt_field_data_int(hStmt, i++, mail->DRM_status);
9496                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9497                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9498                                                  _bind_stmt_field_data_int(hStmt, i++, mail->mail_size);
9499                                                  _bind_stmt_field_data_nstring(hStmt, i++, (char*)mail->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9500                                                  _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9501                                                  _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9502                                                  _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
9503                                                  _bind_stmt_field_data_int(hStmt, i++, mail->meeting_request_status);
9504                                                  _bind_stmt_field_data_int(hStmt, i++, mail->message_class);
9505                                                  _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9506                                                  _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9507                                                  _bind_stmt_field_data_int(hStmt, i++, mail->scheduled_sending_time);
9508                                                  _bind_stmt_field_data_int(hStmt, i++, mail->remaining_resend_times);
9509                                                  _bind_stmt_field_data_int(hStmt, i++, mail->tag_id);
9510                                                  _bind_stmt_field_data_int(hStmt, i++, mail->replied_time);
9511                                                  _bind_stmt_field_data_int(hStmt, i++, mail->forwarded_time);
9512                                                  _bind_stmt_field_data_string(hStmt, i++, (char*)mail->default_charset, 0, TEXT_2_LEN_IN_MAIL_TBL);
9513                                                  _bind_stmt_field_data_int(hStmt, i++, mail->eas_data_length);
9514                                                  _bind_stmt_field_data_blob(hStmt, i++, (void*)mail->eas_data, mail->eas_data_length);
9515                                                  break;
9516
9517         case UPDATE_DATETIME: {
9518                                                           SNPRINTF(sql_query_string, sizeof(sql_query_string),
9519                                                                           "UPDATE mail_tbl SET"
9520                                                                           " date_time = '%ld'"
9521                                                                           " WHERE mail_id = %d AND account_id != 0"
9522                                                                           , mail->date_time
9523                                                                           , mail_id);
9524
9525                                                           EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9526                                                           EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9527                                                                           ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9528                                                           break;
9529                                                   }
9530
9531         case UPDATE_FROM_CONTACT_INFO:
9532                                                   EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_FROM_CONTACT_INFO");
9533                                                   SNPRINTF(sql_query_string, sizeof(sql_query_string),
9534                                                                   "UPDATE mail_tbl SET"
9535                                                                   " email_address_sender = ?,"
9536                                                                   " WHERE mail_id = %d",
9537                                                                   mail_id);
9538
9539                                                   hStmt = NULL;
9540
9541                                                   EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9542                                                   EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9543                                                                   ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9544                                                   i = 0;
9545                                                   _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9546                                                   break;
9547
9548         case UPDATE_TO_CONTACT_INFO:
9549                                                   EM_DEBUG_LOG("NVARCHAR : emstorage_change_mail_field - mail change type is UPDATE_TO_CONTACT_INFO");
9550                                                   SNPRINTF(sql_query_string, sizeof(sql_query_string),
9551                                                                   "UPDATE mail_tbl SET"
9552                                                                   " email_address_recipient = ?,"
9553                                                                   " WHERE mail_id = %d",
9554                                                                   mail_id);
9555
9556                                                   hStmt = NULL;
9557
9558                                                   EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9559                                                   EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9560                                                                   ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9561                                                   i = 0;
9562                                                   _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9563                                                   break;
9564
9565         case UPDATE_ALL_CONTACT_INFO:
9566                                                   EM_DEBUG_LOG("emstorage_change_mail_field - mail change type is UPDATE_ALL_CONTACT_INFO");
9567                                                   SNPRINTF(sql_query_string, sizeof(sql_query_string),
9568                                                                   "UPDATE mail_tbl SET"
9569                                                                   " email_address_sender = ?,"
9570                                                                   " email_address_recipient = ?,"
9571                                                                   " WHERE mail_id = %d",
9572                                                                   mail_id);
9573
9574                                                   hStmt = NULL;
9575
9576                                                   EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9577                                                   EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9578                                                                   ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9579                                                   i = 0;
9580                                                   _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_sender, 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9581                                                   _bind_stmt_field_data_string(hStmt, i++, (char *)mail->email_address_recipient, 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
9582                                                   break;
9583
9584
9585 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
9586         case UPDATE_PARTIAL_BODY_DOWNLOAD:
9587
9588                                                   SNPRINTF(sql_query_string, sizeof(sql_query_string),
9589                                                                   "UPDATE mail_tbl SET"
9590                                                                   "  body_download_status = ?"
9591                                                                   ", file_path_plain = ?"
9592                                                                   ", file_path_html = ?"
9593                                                                   ", file_path_mime_entity = ?"
9594                                                                   ", attachment_count = ?"
9595                                                                   ", inline_content_count = ?"
9596                                                                   ", preview_text = ?"
9597                                                                   ", digest_type = ?"
9598                                                                   ", smime_type = ?"
9599                                                                   " WHERE mail_id = %d"
9600                                                                   , mail_id);
9601
9602
9603                                                   EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9604                                                   EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9605                                                                   ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9606                                                   i = 0;
9607
9608                                                   _bind_stmt_field_data_int(hStmt, i++, mail->body_download_status);
9609                                                   _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9610                                                   _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_html,  0, TEXT_2_LEN_IN_MAIL_TBL);
9611                                                   _bind_stmt_field_data_string(hStmt, i++, (char *)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9612                                                   _bind_stmt_field_data_int(hStmt, i++, mail->attachment_count);
9613                                                   _bind_stmt_field_data_int(hStmt, i++, mail->inline_content_count);
9614                                                   _bind_stmt_field_data_nstring(hStmt, i++, (char *)mail->preview_text,    0, PREVIEWBODY_LEN_IN_MAIL_TBL);
9615                                                   _bind_stmt_field_data_int(hStmt, i++, mail->digest_type);
9616                                                   _bind_stmt_field_data_int(hStmt, i++, mail->smime_type);
9617
9618                                                   break;
9619
9620 #endif
9621         case UPDATE_FILE_PATH:
9622                                                   SNPRINTF(sql_query_string, sizeof(sql_query_string),
9623                                                                   "UPDATE mail_tbl SET"
9624                                                                   ", file_path_plain = ?"
9625                                                                   ", file_path_html = ?"
9626                                                                   ", file_path_mime_entity = ?"
9627                                                                   " WHERE mail_id = %d"
9628                                                                   , mail_id);
9629
9630
9631                                                   EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
9632                                                   EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
9633                                                   EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9634                                                                   ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
9635                                                   i = 0;
9636                                                   _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_plain, 0, TEXT_1_LEN_IN_MAIL_TBL);
9637                                                   _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_html, 0, TEXT_2_LEN_IN_MAIL_TBL);
9638                                                   _bind_stmt_field_data_string(hStmt, i++, (char*)mail->file_path_mime_entity, 0, MIME_ENTITY_LEN_IN_MAIL_TBL);
9639                                                   break;
9640
9641         default:
9642                                                   EM_DEBUG_LOG(" type[%d]", type);
9643
9644                                                   error = EMAIL_ERROR_INVALID_PARAM;
9645                                                   goto FINISH_OFF;
9646         }
9647
9648         if (hStmt != NULL) {
9649
9650                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9651                 EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
9652                                 ("sqlite3_step fail:%d", rc));
9653                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
9654                                 ("sqlite3_step fail:%d", rc));
9655                 rc = sqlite3_changes(local_db_handle);
9656                 if (rc == 0) {
9657                         EM_DEBUG_LOG(" no matched mail found...");
9658                         error = EMAIL_ERROR_MAIL_NOT_FOUND;
9659                         goto FINISH_OFF;
9660                 }
9661         }
9662
9663         if (mail->account_id == 0) {
9664                 emstorage_mail_tbl_t* mail_for_account_tbl = NULL;
9665                 if (!emstorage_get_mail_field_by_id(multi_user_name, mail_id, RETRIEVE_ACCOUNT, &mail_for_account_tbl, true, &error) || !mail_for_account_tbl) {
9666                         EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id error [%d]", error);
9667                         goto FINISH_OFF;
9668                 }
9669                 mail->account_id = mail_for_account_tbl->account_id;
9670                 if (mail_for_account_tbl)
9671                         emstorage_free_mail(&mail_for_account_tbl, 1, NULL);
9672         }
9673
9674         ret = true;
9675
9676 FINISH_OFF:
9677         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9678
9679         if (hStmt != NULL) {
9680                 rc = sqlite3_finalize(hStmt);
9681                 if (rc != SQLITE_OK) {
9682                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9683                         error = EMAIL_ERROR_DB_FAILURE;
9684                 }
9685                 hStmt = NULL;
9686         }
9687
9688         if (error == EMAIL_ERROR_NONE &&  move_flag != 1 && transaction) {
9689                 if (!emstorage_get_mailbox_id_by_mailbox_type(multi_user_name, mail->account_id, EMAIL_MAILBOX_TYPE_SENTBOX, &mailbox_id, false, &error))
9690                         EM_DEBUG_EXCEPTION("emstorage_get_mailbox_id_by_mailbox_type error [%d]", error);
9691
9692                 if (mail->mailbox_id == mailbox_id) {
9693                         SNPRINTF(mailbox_id_param_string, 10, "%d", mail->mailbox_id);
9694                         if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, mailbox_id_param_string, type))
9695                                 EM_DEBUG_EXCEPTION("emcore_notify_storage_eventerror [ NOTI_MAIL_UPDATE ] >>>> ");
9696                 } else {
9697                         /* h.gahlaut@samsung.com: Jan 10, 2011 Publishing noti to refresh outbox when email sending status changes */
9698                         if (!emcore_notify_storage_event(NOTI_MAIL_UPDATE, mail->account_id, mail_id, NULL, type))
9699                                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventerror [ NOTI_MAIL_UPDATE ]");
9700                 }
9701         }
9702
9703         if (err_code != NULL)
9704                 *err_code = error;
9705
9706         EM_DEBUG_FUNC_END("ret [%d]", ret);
9707         return ret;
9708 }
9709
9710 INTERNAL_FUNC int emstorage_increase_mail_id(char *multi_user_name, int *mail_id, int transaction, int *err_code)
9711 {
9712         EM_DEBUG_FUNC_BEGIN("mail_id[%p], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
9713
9714         int rc, ret = false;
9715         int error = EMAIL_ERROR_NONE;
9716         int latest_mail_id = 0;
9717         sqlite3 *local_db_handle = NULL;
9718         char *sql = "SELECT MAX(mail_id) FROM mail_tbl;";
9719         char **result = NULL;
9720
9721 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9722         _timedlock_shm_mutex(mapped_for_generating_mail_id, 2);
9723 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9724
9725         ret = vconf_get_int(VCONF_KEY_LATEST_MAIL_ID, &latest_mail_id);
9726         if (ret < 0 || latest_mail_id == 0) {
9727                 EM_DEBUG_LOG("vconf_get_int() failed [%d] or latest_mail_id is zero", ret);
9728
9729                 local_db_handle = emstorage_get_db_connection(multi_user_name);
9730
9731                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9732                 EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
9733                                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
9734                 if (NULL == result[1])
9735                         rc = 1;
9736                 else
9737                         rc = atoi(result[1]) + 1;
9738
9739                 sqlite3_free_table(result);
9740                 latest_mail_id = rc;
9741         }
9742
9743         latest_mail_id++;
9744
9745         ret = vconf_set_int(VCONF_KEY_LATEST_MAIL_ID, latest_mail_id);
9746
9747         if (mail_id)
9748                 *mail_id = latest_mail_id;
9749
9750         ret = true;
9751
9752 FINISH_OFF:
9753
9754 #ifdef __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__
9755                 _unlockshm_mutex(mapped_for_generating_mail_id);
9756 #endif /* __FEATURE_USE_SHARED_MUTEX_FOR_GENERATING_MAIL_ID__ */
9757
9758         if (err_code != NULL)
9759                 *err_code = error;
9760
9761         EM_DEBUG_FUNC_END("ret [%d]", ret);
9762         return ret;
9763 }
9764
9765 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)
9766 {
9767         EM_PROFILE_BEGIN(profile_emstorage_add_mail);
9768         EM_DEBUG_FUNC_BEGIN("mail_tbl_data[%p], get_id[%d], transaction[%d], err_code[%p]", mail_tbl_data, get_id, transaction, err_code);
9769
9770         if (!mail_tbl_data) {
9771                 EM_DEBUG_EXCEPTION("mail_tbl_data[%p], get_id[%d]", mail_tbl_data, get_id);
9772                 if (err_code != NULL)
9773                         *err_code = EMAIL_ERROR_INVALID_PARAM;
9774                 return false;
9775         }
9776
9777         int rc, ret = false;
9778         int error = EMAIL_ERROR_NONE;
9779         char sql_query_string[QUERY_SIZE] = {0, };
9780         DB_STMT hStmt = NULL;
9781
9782         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
9783
9784         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
9785
9786         if (get_id) {
9787                 /*  increase unique id */
9788                 char *sql = "SELECT max(rowid) FROM mail_tbl;";
9789                 char **result;
9790
9791                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
9792                 /*              EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
9793                                 ("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle))); */
9794                 if (rc != SQLITE_OK) {
9795                         EM_DEBUG_EXCEPTION("SQL[%s] sqlite3_get_table fail[%d] [%s]", sql, rc, sqlite3_errmsg(local_db_handle));
9796                         error = EMAIL_ERROR_DB_FAILURE;
9797                         sqlite3_free_table(result);
9798                         goto FINISH_OFF;
9799                 }
9800
9801                 if (NULL == result[1])
9802                         rc = 1;
9803                 else
9804                         rc = atoi(result[1])+1;
9805
9806                 sqlite3_free_table(result);
9807
9808                 mail_tbl_data->mail_id   = rc;
9809                 mail_tbl_data->thread_id = rc;
9810         }
9811
9812         if (mail_tbl_data->date_time == 0)
9813                 mail_tbl_data->date_time = time(NULL);
9814
9815         SNPRINTF(sql_query_string, sizeof(sql_query_string),
9816                         "INSERT INTO mail_tbl VALUES "
9817                         "(?" /*  mail_id */
9818                         ", ?" /*  account_id */
9819                         ", ?" /*  mailbox_id */
9820                         ", ?" /*  mailbox_type */
9821                         ", ?" /*  subject */
9822
9823                         ", ?" /*  date_time */
9824                         ", ?" /*  server_mail_status */
9825                         ", ?" /*  server_mailbox_name */
9826                         ", ?" /*  server_mail_id */
9827                         ", ?" /*  message_id */
9828
9829                         ", ?" /*  reference_mail_id */
9830                         ", ?" /*  full_address_from */
9831                         ", ?" /*  full_address_reply */
9832                         ", ?" /*  full_address_to */
9833                         ", ?" /*  full_address_cc */
9834
9835                         ", ?" /*  full_address_bcc */
9836                         ", ?" /*  full_address_return */
9837                         ", ?" /*  email_address_sender */
9838                         ", ?" /*  email_address_recipient */
9839                         ", ?" /*  alias_sender */
9840
9841                         ", ?" /*  alias_recipient */
9842                         ", ?" /*  body_download_status */
9843                         ", ?" /*  file_path_plain */
9844                         ", ?" /*  file_path_html */
9845                         ", ?" /*  file_path_mime_entity */
9846
9847                         ", ?" /*  mail_size */
9848                         ", ?" /*  flags_seen_field */
9849                         ", ?" /*  flags_deleted_field */
9850                         ", ?" /*  flags_flagged_field */
9851                         ", ?" /*  flags_answered_field */
9852
9853                         ", ?" /*  flags_recent_field */
9854                         ", ?" /*  flags_draft_field */
9855                         ", ?" /*  flags_forwarded_field */
9856                         ", ?" /*  DRM_status */
9857                         ", ?" /*  priority */
9858
9859                         ", ?" /*  save_status */
9860                         ", ?" /*  lock_status */
9861                         ", ?" /*  report_status */
9862                         ", ?" /*  attachment_count */
9863                         ", ?" /*  inline_content_count */
9864
9865                         ", ?" /*  thread_id */
9866                         ", ?" /*  thread_item_count */
9867                         ", ?" /*  preview_text */
9868                         ", ?" /*  meeting_request_status */
9869                         ", ?" /*  message_class */
9870
9871                         ", ?" /*  digest_type */
9872                         ", ?" /*  smime_type */
9873                         ", ?" /*  scheduled_sending_time */
9874                         ", ?" /*  remaining_resend_times */
9875                         ", ?" /*  tag_id */
9876
9877                         ", ?" /*  replied_time */
9878                         ", ?" /*  forwarded_time */
9879                         ", ?" /*  default charset */
9880                         ", ?" /*  eas_data_length */
9881                         ", ?" /*  eas_data */
9882                         ", ?" /*  user_name */
9883                         ")");
9884
9885         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle,
9886                                 sql_query_string,
9887                                 EM_SAFE_STRLEN(sql_query_string),
9888                                 &hStmt,
9889                                 NULL),
9890                         rc);
9891         if (rc != SQLITE_OK) {
9892                 EM_DEBUG_EXCEPTION("sqlite3_prepare error [%d] [%s] SQL(%s) ",
9893                                 rc, sql_query_string, sqlite3_errmsg(local_db_handle));
9894                 error = EMAIL_ERROR_DB_FAILURE;
9895                 goto FINISH_OFF;
9896         }
9897
9898         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mail_id);
9899         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, mail_tbl_data->account_id);
9900         _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_id);
9901         _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->mailbox_type);
9902         _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->subject, 1, SUBJECT_LEN_IN_MAIL_TBL);
9903
9904         _bind_stmt_field_data_int(hStmt, DATETIME_IDX_IN_MAIL_TBL, mail_tbl_data->date_time);
9905         _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->server_mail_status);
9906         _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);
9907         _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);
9908         _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);
9909
9910         _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, mail_tbl_data->reference_mail_id);
9911         _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);
9912         _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);
9913         _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);
9914         _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);
9915
9916         _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);
9917         _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);
9918         _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);
9919         _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);
9920         _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);
9921
9922         _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);
9923         _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->body_download_status);
9924         _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);
9925         _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);
9926         _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);
9927
9928         _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, mail_tbl_data->mail_size);
9929         _bind_stmt_field_data_int(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_seen_field);
9930         _bind_stmt_field_data_int(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_deleted_field);
9931         _bind_stmt_field_data_int(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_flagged_field);
9932         _bind_stmt_field_data_int(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_answered_field);
9933
9934         _bind_stmt_field_data_int(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_recent_field);
9935         _bind_stmt_field_data_int(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_draft_field);
9936         _bind_stmt_field_data_int(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, mail_tbl_data->flags_forwarded_field);
9937         _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->DRM_status);
9938         _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, mail_tbl_data->priority);
9939
9940         _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->save_status);
9941         _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->lock_status);
9942         _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->report_status);
9943         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->attachment_count);
9944         _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->inline_content_count);
9945
9946         _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, mail_tbl_data->thread_id);
9947         _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, mail_tbl_data->thread_item_count);
9948         _bind_stmt_field_data_nstring(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, (char*)mail_tbl_data->preview_text, 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
9949         _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, mail_tbl_data->meeting_request_status);
9950         _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, mail_tbl_data->message_class);
9951
9952         _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->digest_type);
9953         _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, mail_tbl_data->smime_type);
9954         _bind_stmt_field_data_int(hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->scheduled_sending_time);
9955         _bind_stmt_field_data_int(hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, mail_tbl_data->remaining_resend_times);
9956         _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, mail_tbl_data->tag_id);
9957
9958         _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->replied_time);
9959         _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, mail_tbl_data->forwarded_time);
9960         _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);
9961         _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, mail_tbl_data->eas_data_length);
9962         _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, (void*)mail_tbl_data->eas_data, mail_tbl_data->eas_data_length);
9963         _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);
9964
9965         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
9966         if (rc == SQLITE_FULL) {
9967                 EM_DEBUG_EXCEPTION("sqlite3_step error [%d]", rc);
9968                 error = EMAIL_ERROR_MAIL_MEMORY_FULL;
9969                 goto FINISH_OFF;
9970         }
9971         if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
9972                 EM_DEBUG_EXCEPTION("sqlite3_step error [%d]", rc);
9973                 error = EMAIL_ERROR_DB_FAILURE;
9974                 goto FINISH_OFF;
9975         }
9976         ret = true;
9977
9978 FINISH_OFF:
9979         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
9980
9981         if (hStmt != NULL) {
9982                 rc = sqlite3_finalize(hStmt);
9983                 if (rc != SQLITE_OK) {
9984                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
9985                         error = EMAIL_ERROR_DB_FAILURE;
9986                 }
9987         }
9988
9989         if (err_code != NULL)
9990                 *err_code = error;
9991
9992         EM_PROFILE_END(profile_emstorage_add_mail);
9993         EM_DEBUG_FUNC_END("ret [%d]", ret);
9994         return ret;
9995 }
9996
9997 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)
9998 {
9999         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);
10000
10001         int ret = false, i, cur_conditional_clause = 0;
10002         int error = EMAIL_ERROR_NONE;
10003         int target_account_id;
10004         int conditional_clause_len = 0;
10005         char *sql_query_string = NULL, *conditional_clause = NULL;
10006         emstorage_mailbox_tbl_t *result_mailbox = NULL;
10007         email_mailbox_type_e target_mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED;
10008         char* target_mailbox_name = NULL;
10009
10010         if (!mail_ids || input_mailbox_id <= 0) {
10011                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10012                 if (err_code != NULL)
10013                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10014                 return false;
10015         }
10016
10017         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10018
10019         if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &result_mailbox)) != EMAIL_ERROR_NONE || !result_mailbox) {
10020                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", error);
10021                 if (err_code != NULL)
10022                         *err_code = error;
10023                 return false;
10024         }
10025
10026         if (result_mailbox->mailbox_name) {
10027                 if (strstr(result_mailbox->mailbox_name, "'"))
10028                         target_mailbox_name = em_replace_all_string(result_mailbox->mailbox_name, "'", "''");
10029                 else
10030                         target_mailbox_name = strdup(result_mailbox->mailbox_name);
10031         }
10032
10033         target_mailbox_type = result_mailbox->mailbox_type;
10034         target_account_id   = result_mailbox->account_id;
10035         emstorage_free_mailbox(&result_mailbox, 1, NULL);
10036
10037         conditional_clause_len =  (sizeof(char) * 8 * number_of_mails) + 512;
10038         conditional_clause = em_malloc(conditional_clause_len);
10039         if (conditional_clause == NULL) {
10040                 EM_DEBUG_EXCEPTION("em_mallocfailed");
10041                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10042                 goto FINISH_OFF;
10043         }
10044         cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE mail_id in (");
10045
10046         for (i = 0; i < number_of_mails; i++)
10047                 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
10048
10049         /* prevent 34415 */
10050         char *last_comma = rindex(conditional_clause, ',');
10051         if (last_comma) *last_comma = ')'; /* replace , with) */
10052
10053         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10054
10055         /* Updating a mail_tbl */
10056
10057         sql_query_string = em_malloc(conditional_clause_len);
10058         if (sql_query_string == NULL) {
10059                 EM_DEBUG_EXCEPTION("em_mallocfailed");
10060                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10061                 goto FINISH_OFF;
10062         }
10063         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);
10064         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10065
10066         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10067         if (error != EMAIL_ERROR_NONE) {
10068                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10069                 goto FINISH_OFF;
10070         }
10071
10072         /* Updating a mail_attachment_tbl */
10073         memset(sql_query_string, 0x00, conditional_clause_len);
10074         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);
10075         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10076         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10077         if (error != EMAIL_ERROR_NONE) {
10078                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10079                 goto FINISH_OFF;
10080         }
10081
10082         /* Updating a mail_meeting_tbl */
10083         memset(sql_query_string, 0x00, conditional_clause_len);
10084         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);
10085         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10086         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10087         if (error != EMAIL_ERROR_NONE) {
10088                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10089                 goto FINISH_OFF;
10090         }
10091
10092 #ifdef __FEATURE_BODY_SEARCH__
10093         /* Updating mail_text_tbl */
10094         memset(sql_query_string, 0x00, conditional_clause_len);
10095         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);
10096         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10097         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10098         if (error != EMAIL_ERROR_NONE) {
10099                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10100                 goto FINISH_OFF;
10101         }
10102 #endif
10103
10104         /* Updating a mail_read_mail_uid_tbl */
10105         memset(conditional_clause, 0x00, conditional_clause_len);
10106         cur_conditional_clause = SNPRINTF(conditional_clause, conditional_clause_len, "WHERE local_uid in (");
10107
10108         for (i = 0; i < number_of_mails; i++)
10109                 cur_conditional_clause += SNPRINTF_OFFSET(conditional_clause, cur_conditional_clause, conditional_clause_len, "%d,", mail_ids[i]);
10110
10111         /* prevent 34415 */
10112         last_comma = rindex(conditional_clause, ',');
10113         if (last_comma) *last_comma = ')'; /* replace , with) */
10114
10115         memset(sql_query_string, 0x00, conditional_clause_len);
10116         SNPRINTF(sql_query_string, conditional_clause_len, "UPDATE mail_read_mail_uid_tbl SET mailbox_name = ? mailbox_id = ? account_id = ? %s", conditional_clause);
10117
10118         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10119         error = emstorage_exec_query_by_prepare_v2_stmt(local_db_handle, sql_query_string, target_mailbox_name, input_mailbox_id, target_account_id);
10120         if (error != EMAIL_ERROR_NONE) {
10121                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2_stmt failed:[%d]", error);
10122                 goto FINISH_OFF;
10123         }
10124
10125         ret = true;
10126
10127 FINISH_OFF:
10128         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10129
10130         EM_SAFE_FREE(target_mailbox_name);
10131         EM_SAFE_FREE(conditional_clause);
10132         EM_SAFE_FREE(sql_query_string);
10133
10134         if (err_code != NULL)
10135                 *err_code = error;
10136
10137         EM_DEBUG_FUNC_END("ret [%d]", ret);
10138         return ret;
10139 }
10140
10141 INTERNAL_FUNC int emstorage_delete_mail(char *multi_user_name, int mail_id, int from_server, int transaction, int *err_code)
10142 {
10143         EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
10144
10145         if (!mail_id) {
10146                 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
10147                 if (err_code != NULL)
10148                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10149                 return false;
10150         }
10151
10152         int ret = false;
10153         int error = EMAIL_ERROR_NONE;
10154         char sql_query_string[QUERY_SIZE] = {0, };
10155         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10156         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10157
10158         memset(sql_query_string, 0x00, sizeof(sql_query_string));
10159
10160         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE mail_id = %d ", mail_id);
10161         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10162         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10163         if (error != EMAIL_ERROR_NONE) {
10164                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10165                 goto FINISH_OFF;
10166         }
10167
10168         ret = true;
10169
10170 FINISH_OFF:
10171         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10172
10173         if (err_code != NULL)
10174                 *err_code = error;
10175
10176         EM_DEBUG_FUNC_END("ret [%d]", ret);
10177         return ret;
10178 }
10179
10180 INTERNAL_FUNC int emstorage_delete_multiple_mails(char *multi_user_name, int mail_ids[], int number_of_mails, int transaction, int *err_code)
10181 {
10182         EM_DEBUG_FUNC_BEGIN("mail_ids[%p], number_of_mails [%d], transaction[%d], err_code[%p]", mail_ids, number_of_mails, transaction, err_code);
10183
10184         int ret = false, i, cur_sql_query_string = 0;
10185         int error = EMAIL_ERROR_NONE;
10186         int query_size = 0;
10187         char *sql_query_string = NULL;
10188
10189         if (!mail_ids) {
10190                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10191                 if (err_code != NULL)
10192                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10193                 return false;
10194         }
10195
10196         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10197         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10198
10199         query_size = (sizeof(char) * 8 * number_of_mails) + 512;
10200         sql_query_string =  em_malloc(query_size);
10201         if (sql_query_string == NULL) {
10202                 EM_DEBUG_EXCEPTION("em_mallocfailed");
10203                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10204                 goto FINISH_OFF;
10205         }
10206
10207         cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_tbl WHERE mail_id in (");
10208
10209         for (i = 0; i < number_of_mails; i++)
10210                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
10211
10212         /* prevent 34414 */
10213         char *last_comma = rindex(sql_query_string, ',');
10214         if (last_comma != NULL) *last_comma = ')'; /* replace , with) */
10215
10216         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10217         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10218         if (error != EMAIL_ERROR_NONE) {
10219                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10220                 goto FINISH_OFF;
10221         }
10222
10223 #ifdef __FEATURE_BODY_SEARCH__
10224         /* delete mail_text from mail_text_tbl */
10225         cur_sql_query_string = SNPRINTF(sql_query_string, query_size, "DELETE FROM mail_text_tbl WHERE mail_id in (");
10226
10227         for (i = 0; i < number_of_mails; i++)
10228                 cur_sql_query_string += SNPRINTF_OFFSET(sql_query_string, cur_sql_query_string, query_size, "%d,", mail_ids[i]);
10229
10230         last_comma = rindex(sql_query_string, ',');
10231         *last_comma = ')'; /* replace , with) */
10232
10233         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10234         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10235         if (error != EMAIL_ERROR_NONE) {
10236                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10237                 goto FINISH_OFF;
10238         }
10239 #endif
10240
10241         ret = true;
10242
10243 FINISH_OFF:
10244         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10245
10246         EM_SAFE_FREE(sql_query_string);
10247         if (err_code != NULL)
10248                 *err_code = error;
10249
10250         EM_DEBUG_FUNC_END("ret [%d]", ret);
10251         return ret;
10252 }
10253
10254 INTERNAL_FUNC int emstorage_delete_mail_by_account(char *multi_user_name, int account_id, int transaction, int *err_code)
10255 {
10256         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
10257
10258         if (account_id < FIRST_ACCOUNT_ID) {
10259                 EM_DEBUG_EXCEPTION("account_id[%d]", account_id);
10260
10261                 if (err_code != NULL)
10262                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10263                 return false;
10264         }
10265
10266         int rc, ret = false;
10267         int error = EMAIL_ERROR_NONE;
10268         char sql_query_string[QUERY_SIZE] = {0, };
10269
10270         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10271         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10272
10273         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_tbl WHERE account_id = %d", account_id);
10274         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10275         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10276         if (error != EMAIL_ERROR_NONE) {
10277                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10278                 goto FINISH_OFF;
10279         }
10280
10281         rc = sqlite3_changes(local_db_handle);
10282         if (rc == 0) {
10283                 EM_DEBUG_LOG(" no mail found...");
10284                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10285         }
10286
10287         /* Delete all mails  mail_read_mail_uid_tbl table based on account id */
10288         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_read_mail_uid_tbl WHERE account_id = %d", account_id);
10289         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10290         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10291         if (error != EMAIL_ERROR_NONE) {
10292                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10293                 goto FINISH_OFF;
10294         }
10295
10296         rc = sqlite3_changes(local_db_handle);
10297         if (rc == 0) {
10298                 EM_DEBUG_LOG("No mail found...");
10299                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10300         }
10301
10302 #ifdef __FEATURE_BODY_SEARCH__
10303         /* Delete all mail_text in mail_text_tbl table based on account id */
10304         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_text_tbl WHERE account_id = %d", account_id);
10305         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10306         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10307         if (error != EMAIL_ERROR_NONE) {
10308                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10309                 goto FINISH_OFF;
10310         }
10311
10312         rc = sqlite3_changes(local_db_handle);
10313         if (rc == 0) {
10314                 EM_DEBUG_LOG("No mail found...");
10315                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
10316         }
10317 #endif
10318
10319         ret = true;
10320
10321 FINISH_OFF:
10322         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10323
10324         if (error == EMAIL_ERROR_NONE) {
10325                 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_WITH_ACCOUNT, account_id, 0 , NULL, 0))
10326                         EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAIL_DELETE_ALL ]");
10327         }
10328
10329         if (err_code != NULL)
10330                 *err_code = error;
10331
10332         EM_DEBUG_FUNC_END("ret [%d]", ret);
10333         return ret;
10334 }
10335
10336 INTERNAL_FUNC int emstorage_delete_mail_by_mailbox(char *multi_user_name, emstorage_mailbox_tbl_t *mailbox, int transaction, int *err_code)
10337 {
10338         EM_DEBUG_FUNC_BEGIN("mailbox[%p], transaction[%d], err_code[%p]", mailbox, transaction, err_code);
10339
10340         if (mailbox == NULL) {
10341                 EM_DEBUG_EXCEPTION("mailbox [%p]", mailbox);
10342                 if (err_code != NULL)
10343                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10344                 return false;
10345         }
10346
10347         int ret = false;
10348         int error = EMAIL_ERROR_NONE;
10349         char sql_query_string[QUERY_SIZE] = {0, };
10350         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10351         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10352
10353         if (strcmp(mailbox->mailbox_name, EMAIL_SEARCH_RESULT_MAILBOX_NAME) == 0) {
10354                 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);
10355                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10356                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10357                 if (error != EMAIL_ERROR_NONE) {
10358                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10359                         goto FINISH_OFF;
10360                 }
10361
10362                 /* Delete Mails from mail_read_mail_uid_tbl */
10363                 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);
10364                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10365                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10366                 if (error != EMAIL_ERROR_NONE) {
10367                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10368                         goto FINISH_OFF;
10369                 }
10370         } else {
10371                 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);
10372                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10373                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10374                 if (error != EMAIL_ERROR_NONE) {
10375                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10376                         goto FINISH_OFF;
10377                 }
10378
10379                 /* Delete Mails from mail_read_mail_uid_tbl */
10380                 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);
10381                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10382                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10383                 if (error != EMAIL_ERROR_NONE) {
10384                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10385                         goto FINISH_OFF;
10386                 }
10387
10388 #ifdef __FEATURE_BODY_SEARCH__
10389                 /* Delete Mails from mail_text_tbl */
10390                 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);
10391                 EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
10392                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
10393                 if (error != EMAIL_ERROR_NONE) {
10394                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
10395                         goto FINISH_OFF;
10396                 }
10397 #endif
10398         }
10399         ret = true;
10400
10401 FINISH_OFF:
10402         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10403
10404         if (error == EMAIL_ERROR_NONE) {
10405                 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_ALL, mailbox->account_id, mailbox->mailbox_id , mailbox->mailbox_name, 0))
10406                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventFailed [ NOTI_MAIL_DELETE_ALL ] >>>> ");
10407         }
10408
10409         if (err_code != NULL)
10410                 *err_code = error;
10411
10412         EM_DEBUG_FUNC_END("ret [%d]", ret);
10413         return ret;
10414 }
10415                                         /* LCOV_EXCL_STOP */
10416 INTERNAL_FUNC int emstorage_free_mail(emstorage_mail_tbl_t** mail_list, int count, int *err_code)
10417 {
10418         EM_DEBUG_FUNC_BEGIN("mail_list[%p], count[%d], err_code[%p]", mail_list, count, err_code);
10419
10420         if (count > 0) {
10421                 if ((mail_list == NULL) || (*mail_list == NULL)) {
10422                         EM_DEBUG_EXCEPTION("mail_ilst[%p], count[%d]", mail_list, count);
10423
10424                         if (err_code)
10425                                 *err_code = EMAIL_ERROR_INVALID_PARAM;
10426                         return false;
10427                 }
10428
10429                 emstorage_mail_tbl_t* p = *mail_list;
10430                 int i = 0;
10431
10432                 for (; i < count; i++, p++) {
10433                         EM_SAFE_FREE(p->server_mailbox_name);
10434                         EM_SAFE_FREE(p->server_mail_id);
10435                         EM_SAFE_FREE(p->full_address_from);
10436                         EM_SAFE_FREE(p->full_address_reply);
10437                         EM_SAFE_FREE(p->full_address_to);
10438                         EM_SAFE_FREE(p->full_address_cc);
10439                         EM_SAFE_FREE(p->full_address_bcc);
10440                         EM_SAFE_FREE(p->full_address_return);
10441                         EM_SAFE_FREE(p->subject);
10442                         EM_SAFE_FREE(p->file_path_plain);
10443                         EM_SAFE_FREE(p->file_path_html);
10444                         EM_SAFE_FREE(p->file_path_mime_entity);
10445                         EM_SAFE_FREE(p->message_id);
10446                         EM_SAFE_FREE(p->email_address_sender);
10447                         EM_SAFE_FREE(p->email_address_recipient);
10448                         EM_SAFE_FREE(p->preview_text);
10449                         EM_SAFE_FREE(p->alias_sender);
10450                         EM_SAFE_FREE(p->alias_recipient);
10451                         EM_SAFE_FREE(p->default_charset);
10452                         EM_SAFE_FREE(p->pgp_password);
10453                         EM_SAFE_FREE(p->eas_data);
10454                         EM_SAFE_FREE(p->user_name);
10455                 }
10456                 EM_SAFE_FREE(*mail_list);
10457         }
10458
10459         if (err_code != NULL)
10460                 *err_code = EMAIL_ERROR_NONE;
10461
10462         EM_DEBUG_FUNC_END();
10463         return true;
10464 }
10465
10466 #ifdef __FEATURE_BODY_SEARCH__
10467                                         /* LCOV_EXCL_START */
10468 INTERNAL_FUNC void emstorage_free_mail_text(emstorage_mail_text_tbl_t** mail_text_list, int count, int *err_code)
10469 {
10470         EM_DEBUG_FUNC_BEGIN("mail_text_list[%p], count[%d], err_code[%p]", mail_text_list, count, err_code);
10471
10472         if (count > 0) {
10473                 if ((mail_text_list == NULL) || (*mail_text_list == NULL)) {
10474                         EM_DEBUG_LOG("Nothing to free: mail_text_list[%p]", mail_text_list);
10475                         return;
10476                 }
10477
10478                 emstorage_mail_text_tbl_t *p = *mail_text_list;
10479                 int i = 0;
10480
10481                 for (; i < count; i++, p++)
10482                         EM_SAFE_FREE(p->body_text);
10483
10484                 EM_SAFE_FREE(*mail_text_list);
10485         }
10486
10487         EM_DEBUG_FUNC_END();
10488 }
10489 #endif
10490
10491 INTERNAL_FUNC int emstorage_get_attachment_count(char *multi_user_name, int mail_id, int *count, int transaction, int *err_code)
10492 {
10493         EM_DEBUG_FUNC_BEGIN("mail_id[%d], count[%p], transaction[%d], err_code[%p]", mail_id, count, transaction, err_code);
10494
10495         if (mail_id <= 0 || !count) {
10496                 EM_DEBUG_EXCEPTION("mail_id[%d], count[%p]", mail_id, count);
10497                 if (err_code != NULL)
10498                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10499                 return false;
10500         }
10501
10502         int rc = -1, ret = false;
10503         int error = EMAIL_ERROR_NONE;
10504         char sql_query_string[QUERY_SIZE] = {0, };
10505
10506         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10507         EMSTORAGE_START_READ_TRANSACTION(transaction);
10508
10509         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
10510
10511         char **result;
10512
10513         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10514         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10515                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10516
10517         *count = atoi(result[1]);
10518         sqlite3_free_table(result);
10519
10520         ret = true;
10521
10522 FINISH_OFF:
10523         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10524
10525         if (err_code != NULL)
10526                 *err_code = error;
10527
10528         EM_DEBUG_FUNC_END("ret [%d]", ret);
10529         return ret;
10530 }
10531
10532 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)
10533 {
10534         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);
10535
10536         if (input_mail_id <= 0 || !output_attachment_list || !output_attachment_count) {
10537                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
10538                 return EMAIL_ERROR_INVALID_PARAM;
10539         }
10540
10541         int                         error = EMAIL_ERROR_NONE;
10542         int                         i = 0;
10543         int                         rc = -1;
10544         char                      **result = NULL;
10545         char                        sql_query_string[QUERY_SIZE] = {0, };
10546         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10547         DB_STMT hStmt = NULL;
10548         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10549
10550         EMSTORAGE_START_READ_TRANSACTION(input_transaction);
10551         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT COUNT(*) FROM mail_attachment_tbl WHERE mail_id = %d", input_mail_id);
10552         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
10553         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
10554                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10555
10556         *output_attachment_count = atoi(result[1]);
10557         sqlite3_free_table(result);
10558
10559         if (*output_attachment_count == 0) {
10560                 error = EMAIL_ERROR_NONE;
10561                 goto FINISH_OFF;
10562         }
10563
10564         p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * (*output_attachment_count));
10565
10566         if (!p_data_tbl) {
10567                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
10568                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10569                 goto FINISH_OFF;
10570         }
10571
10572         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE mail_id = %d ORDER BY attachment_id", input_mail_id);
10573         EM_DEBUG_LOG_SEC("sql_query_string [%s]", sql_query_string);
10574
10575         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10576         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10577
10578         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10579         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },  ("sqlite3_step fail:%d", rc));
10580
10581         if (rc == SQLITE_DONE) {
10582                 EM_DEBUG_EXCEPTION("no matched attachment found...");
10583                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10584                 goto FINISH_OFF;
10585         }
10586         for (i = 0; i < *output_attachment_count; i++) {
10587                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10588                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10589                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10590                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10591                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10592                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10593                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10594                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10595                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10596                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10597                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10598                 _get_stmt_field_data_int(hStmt, &(p_data_tbl[i].attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10599                 _get_stmt_field_data_string(hStmt, &(p_data_tbl[i].attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10600                 EM_DEBUG_LOG("attachment[%d].attachment_id : %d", i, p_data_tbl[i].attachment_id);
10601                 EM_DEBUG_LOG("attachment_mime_type : %s", p_data_tbl[i].attachment_mime_type);
10602                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10603                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; }, ("sqlite3_step fail:%d", rc));
10604         }
10605
10606 FINISH_OFF:
10607
10608         if (error == EMAIL_ERROR_NONE)
10609                 *output_attachment_list = p_data_tbl;
10610         else if (p_data_tbl != NULL)
10611                 emstorage_free_attachment(&p_data_tbl, *output_attachment_count, NULL);
10612
10613         if (hStmt) {
10614                 rc = sqlite3_finalize(hStmt);
10615                 if (rc != SQLITE_OK) {
10616                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10617                         error = EMAIL_ERROR_DB_FAILURE;
10618                         if (*output_attachment_list)
10619                                 emstorage_free_attachment(output_attachment_list, *output_attachment_count, NULL); /* prevent */
10620                 }
10621         }
10622
10623         EMSTORAGE_FINISH_READ_TRANSACTION(input_transaction);
10624
10625         EM_DEBUG_FUNC_END("error [%d]", error);
10626         return error;
10627 }
10628
10629 INTERNAL_FUNC int emstorage_get_attachment(char *multi_user_name, int attachment_id, emstorage_attachment_tbl_t **attachment, int transaction, int *err_code)
10630 {
10631         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], transaction[%d], err_code[%p]", attachment_id, attachment, transaction, err_code);
10632
10633         if (attachment_id <= 0 || !attachment) {
10634                 EM_DEBUG_EXCEPTION("attachment_id[%d], attachment[%p]", attachment_id, attachment);
10635                 if (err_code != NULL)
10636                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10637                 return false;
10638         }
10639
10640         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10641         int rc, ret = false;
10642         int error = EMAIL_ERROR_NONE;
10643         char sql_query_string[QUERY_SIZE] = {0, };
10644
10645         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10646         EMSTORAGE_START_READ_TRANSACTION(transaction);
10647
10648         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_id = %d",  attachment_id);
10649
10650         sqlite3_stmt* hStmt = NULL;
10651
10652         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10653         EM_DEBUG_LOG_DEV(" before sqlite3_prepare hStmt = %p", hStmt);
10654
10655         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10656                         ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10657
10658
10659         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10660         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10661                         ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10662
10663         if (rc == SQLITE_DONE) {
10664                 EM_DEBUG_LOG("no matched attachment found...");
10665                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10666                 goto FINISH_OFF;
10667         }
10668
10669         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10670                 EM_DEBUG_EXCEPTION("malloc failed...");
10671                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10672                 goto FINISH_OFF;
10673         }
10674
10675         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10676         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10677         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10678         _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10679         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10680         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10681         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10682         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10683         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10684         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10685         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10686         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10687         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10688
10689 #ifdef __ATTACHMENT_OPTI__
10690         _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10691         _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10692 #endif
10693
10694         ret = true;
10695
10696 FINISH_OFF:
10697         if (ret == true)
10698                 *attachment = p_data_tbl;
10699
10700         if (hStmt != NULL) {
10701                 rc = sqlite3_finalize(hStmt);
10702                 if (rc != SQLITE_OK) {
10703                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10704                         error = EMAIL_ERROR_DB_FAILURE;
10705                 }
10706         }
10707
10708         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10709
10710         if (err_code != NULL)
10711                 *err_code = error;
10712
10713         EM_DEBUG_FUNC_END("ret [%d]", ret);
10714         return ret;
10715 }
10716
10717 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)
10718 {
10719         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);
10720
10721         if (mail_id <= 0 || nth <= 0 || !attachment_tbl) {
10722                 EM_DEBUG_EXCEPTION(" mail_id[%d], nth[%d], attachment[%p]", mail_id, nth, attachment_tbl);
10723
10724                 if (err_code != NULL)
10725                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10726                 return false;
10727         }
10728
10729         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10730         char *p = NULL;
10731         int rc, ret = false;
10732         int error = EMAIL_ERROR_NONE;
10733         char sql_query_string[QUERY_SIZE] = {0, };
10734
10735         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10736         EMSTORAGE_START_READ_TRANSACTION(transaction);
10737
10738         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));
10739         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10740
10741         DB_STMT hStmt = NULL;
10742
10743         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10744         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10745                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10746
10747
10748         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10749         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10750                         ("sqlite3_step fail:%d", rc));
10751
10752         if (rc == SQLITE_DONE) {
10753                 EM_DEBUG_EXCEPTION("no matched attachment found: mail_id[%d] nth[%d]", mail_id, nth);
10754                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10755                 goto FINISH_OFF;
10756         }
10757
10758         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10759                 EM_DEBUG_EXCEPTION(" malloc failed...");
10760                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10761                 goto FINISH_OFF;
10762         }
10763
10764         p_data_tbl->attachment_id = sqlite3_column_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10765         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10766                 p_data_tbl->attachment_name = cpy_str(p);
10767         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10768                 p_data_tbl->attachment_path = cpy_str(p);
10769         if ((p = (char *)sqlite3_column_text(hStmt, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10770                 p_data_tbl->content_id = cpy_str(p);
10771         p_data_tbl->attachment_size = sqlite3_column_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10772         p_data_tbl->mail_id = sqlite3_column_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10773         p_data_tbl->account_id = sqlite3_column_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10774         p_data_tbl->mailbox_id = sqlite3_column_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10775         p_data_tbl->attachment_save_status = sqlite3_column_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10776         p_data_tbl->attachment_drm_type = sqlite3_column_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10777         p_data_tbl->attachment_drm_method = sqlite3_column_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10778         p_data_tbl->attachment_inline_content_status = sqlite3_column_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10779         if ((p = (char *)sqlite3_column_text(hStmt, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10780                 p_data_tbl->attachment_mime_type = cpy_str(p);
10781 #ifdef __ATTACHMENT_OPTI__
10782         p_data_tbl->encoding = sqlite3_column_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10783         if ((p = (char *)sqlite3_column_text(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL)) && (int)EM_SAFE_STRLEN(p))
10784                 p_data_tbl->section = cpy_str(p);
10785 #endif
10786         ret = true;
10787
10788 FINISH_OFF:
10789         if (ret == true)
10790                 *attachment_tbl = p_data_tbl;
10791
10792         if (hStmt != NULL) {
10793                 rc = sqlite3_finalize(hStmt);
10794                 if (rc != SQLITE_OK) {
10795                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10796                         error = EMAIL_ERROR_DB_FAILURE;
10797                 }
10798         }
10799
10800         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10801
10802         if (err_code != NULL)
10803                 *err_code = error;
10804
10805         EM_DEBUG_FUNC_END("ret [%d]", ret);
10806         return ret;
10807 }
10808
10809 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)
10810 {
10811         EM_DEBUG_FUNC_BEGIN("attachment_path[%p], attachment[%p], transaction[%d], err_code[%p]", attachment_path, attachment, transaction, err_code);
10812
10813         if (attachment_path == NULL || !attachment) {
10814                 EM_DEBUG_EXCEPTION("attachment_path[%p], attachment[%p]", attachment_path, attachment);
10815                 if (err_code != NULL)
10816                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10817                 return false;
10818         }
10819
10820         emstorage_attachment_tbl_t* p_data_tbl = NULL;
10821         int rc, ret = false;
10822         int error = EMAIL_ERROR_NONE;
10823         char sql_query_string[QUERY_SIZE] = {0, };
10824
10825         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10826         EMSTORAGE_START_READ_TRANSACTION(transaction);
10827
10828         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_attachment_tbl WHERE attachment_path = '%s'", attachment_path);
10829
10830         sqlite3_stmt* hStmt = NULL;
10831
10832         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10833         EM_DEBUG_LOG_DEV("before sqlite3_prepare hStmt = %p", hStmt);
10834
10835         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10836                         ("sqlite3_prepare failed [%d] [%s]", rc, sql_query_string));
10837
10838
10839         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10840         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10841                         ("sqlite3_step failed [%d] [%s]", rc, sql_query_string));
10842
10843         if (rc == SQLITE_DONE) {
10844                 EM_DEBUG_LOG("no matched attachment found...");
10845                 error = EMAIL_ERROR_ATTACHMENT_NOT_FOUND;
10846                 goto FINISH_OFF;
10847         }
10848
10849         if (!(p_data_tbl = (emstorage_attachment_tbl_t*)em_malloc(sizeof(emstorage_attachment_tbl_t) * 1))) {
10850                 EM_DEBUG_EXCEPTION("malloc failed...");
10851                 error = EMAIL_ERROR_OUT_OF_MEMORY;
10852                 goto FINISH_OFF;
10853         }
10854
10855         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_id), ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10856         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_name), 0, ATTACHMENT_NAME_IDX_IN_MAIL_ATTACHMENT_TBL);
10857         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_path), 0, ATTACHMENT_PATH_IDX_IN_MAIL_ATTACHMENT_TBL);
10858         _get_stmt_field_data_string(hStmt, &(p_data_tbl->content_id), 0, CONTENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10859         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_size), ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL);
10860         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mail_id), MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10861         _get_stmt_field_data_int(hStmt, &(p_data_tbl->account_id), ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10862         _get_stmt_field_data_int(hStmt, &(p_data_tbl->mailbox_id), MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL);
10863         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_save_status), ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10864         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_type), ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10865         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_drm_method), ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL);
10866         _get_stmt_field_data_int(hStmt, &(p_data_tbl->attachment_inline_content_status), ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL);
10867         _get_stmt_field_data_string(hStmt, &(p_data_tbl->attachment_mime_type), 0, ATTACHMENT_MIME_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL);
10868
10869 #ifdef __ATTACHMENT_OPTI__
10870         _get_stmt_field_data_int(hStmt, &(p_data_tbl->encoding), ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL);
10871         _get_stmt_field_data_string(hStmt, &(p_data_tbl->section), 0, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL);
10872 #endif
10873
10874         ret = true;
10875
10876 FINISH_OFF:
10877         if (ret == true)
10878                 *attachment = p_data_tbl;
10879
10880         if (hStmt != NULL) {
10881                 rc = sqlite3_finalize(hStmt);
10882                 if (rc != SQLITE_OK) {
10883                         EM_DEBUG_EXCEPTION("sqlite3_finalize failed [%d]", rc);
10884                         error = EMAIL_ERROR_DB_FAILURE;
10885                 }
10886         }
10887
10888         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
10889
10890         if (err_code != NULL)
10891                 *err_code = error;
10892
10893         EM_DEBUG_FUNC_END("ret [%d]", ret);
10894         return ret;
10895 }
10896
10897 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)
10898 {
10899         EM_DEBUG_FUNC_BEGIN("mail_id[%d], type[%d], attachment[%p], transaction[%d], err_code[%p]", mail_id, type, attachment, transaction, err_code);
10900
10901         if (mail_id <= 0 || !attachment) {
10902                 EM_DEBUG_EXCEPTION(" mail_id[%d], type[%d], attachment[%p]", mail_id, type, attachment);
10903                 if (err_code != NULL)
10904                         *err_code = EMAIL_ERROR_INVALID_PARAM;
10905                 return false;;
10906         }
10907
10908         int rc, ret = false;
10909         int error = EMAIL_ERROR_NONE;
10910         DB_STMT hStmt = NULL;
10911         char sql_query_string[QUERY_SIZE] = {0, };
10912
10913         int i = 0;
10914
10915         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
10916
10917         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
10918
10919         switch (type) {
10920         case UPDATE_MAILBOX:
10921                 EM_DEBUG_LOG("UPDATE_MAILBOX");
10922                 if (!attachment->mailbox_id) {
10923                         EM_DEBUG_EXCEPTION(" attachment->mailbox_id[%d]", attachment->mailbox_id);
10924                         error = EMAIL_ERROR_INVALID_PARAM;
10925                         goto FINISH_OFF;
10926                 }
10927                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10928                                 "UPDATE mail_attachment_tbl SET mailbox_id = ? WHERE mail_id = %d", mail_id);
10929
10930                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10931                 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10932                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10933                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10934
10935                 _bind_stmt_field_data_int(hStmt, i++, attachment->mailbox_id);
10936                 break;
10937
10938         case UPDATE_SAVENAME:
10939                 EM_DEBUG_LOG("UPDATE_SAVENAME");
10940                 if (!attachment->attachment_path) {
10941                         EM_DEBUG_EXCEPTION(" attachment->attachment_path[%p]", attachment->attachment_path);
10942                         error = EMAIL_ERROR_INVALID_PARAM;
10943                         goto FINISH_OFF;
10944                 }
10945
10946                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
10947                                 "UPDATE mail_attachment_tbl SET"
10948                                 "  attachment_size = ?"
10949                                 ", attachment_save_status = ?"
10950                                 ", attachment_path = ?"
10951                                 " WHERE mail_id = %d"
10952                                 " AND attachment_id = %d"
10953                                 , attachment->mail_id
10954                                 , attachment->attachment_id);
10955
10956
10957                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
10958                 EM_DEBUG_LOG(" Before sqlite3_prepare hStmt = %p", hStmt);
10959                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10960                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
10961
10962                 _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_size);
10963                 _bind_stmt_field_data_int(hStmt, i++, attachment->attachment_save_status);
10964                 _bind_stmt_field_data_string(hStmt, i++, (char *)attachment->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
10965                 break;
10966
10967         default:
10968                 EM_DEBUG_LOG("type[%d]", type);
10969                 error = EMAIL_ERROR_INVALID_PARAM;
10970                 goto FINISH_OFF;
10971         }
10972         EM_DEBUG_LOG_SEC("query = [%s]", sql_query_string);
10973
10974         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
10975         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
10976                         ("sqlite3_step fail:%d", rc));
10977         ret = true;
10978
10979 FINISH_OFF:
10980
10981         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
10982
10983         if (hStmt != NULL) {
10984                 rc = sqlite3_finalize(hStmt);
10985                 if (rc != SQLITE_OK) {
10986                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
10987                         error = EMAIL_ERROR_DB_FAILURE;
10988                 }
10989         }
10990
10991         if (err_code != NULL)
10992                 *err_code = error;
10993         EM_DEBUG_FUNC_END("ret [%d]", ret);
10994         return ret;
10995 }
10996
10997 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)
10998 {
10999         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);
11000
11001         int rc = 0;
11002         int ret = false;
11003         int error = EMAIL_ERROR_NONE;
11004         int field_idx = 0;
11005         int account_id = 0;
11006         char sql_query_string[QUERY_SIZE] = {0, };
11007         char *replaced_mailbox_name = NULL;
11008         char *replaced_alias = NULL;
11009         sqlite3 *local_db_handle = NULL;
11010         DB_STMT hStmt = NULL;
11011         emstorage_mailbox_tbl_t *old_mailbox_data = NULL;
11012
11013         if (input_mailbox_id <= 0 || !input_new_mailbox_name || !input_new_mailbox_alias) {
11014                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
11015                 return EMAIL_ERROR_INVALID_PARAM;
11016         }
11017
11018         if (strstr(input_new_mailbox_name, "'"))
11019                 replaced_mailbox_name = em_replace_all_string(input_new_mailbox_name, "'", "''");
11020         else
11021                 replaced_mailbox_name = strdup(input_new_mailbox_name);
11022
11023         if (strstr(input_new_mailbox_alias, "'"))
11024                 replaced_alias = em_replace_all_string(input_new_mailbox_alias, "'", "''");
11025         else
11026                 replaced_alias = strdup(input_new_mailbox_alias);
11027
11028         local_db_handle = emstorage_get_db_connection(multi_user_name);
11029
11030         if ((error = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &old_mailbox_data)) != EMAIL_ERROR_NONE) {
11031                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", error);
11032                 EM_SAFE_FREE(replaced_mailbox_name);
11033                 EM_SAFE_FREE(replaced_alias);
11034                 return error;
11035         }
11036
11037         if (old_mailbox_data == NULL) {
11038                 EM_DEBUG_LOG("old_mailbox_data is NULL");
11039                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
11040                 goto FINISH_OFF;
11041         }
11042
11043         account_id = old_mailbox_data->account_id;
11044
11045         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, error);
11046
11047         if (input_eas_data && input_eas_data_length > 0) {
11048                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11049                                 "UPDATE mail_box_tbl SET"
11050                                 " mailbox_name = ?"
11051                                 ",alias = ?"
11052                                 ",eas_data = ?"
11053                                 ",eas_data_length = ?"
11054                                 " WHERE mailbox_id = ?");
11055
11056                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11057
11058                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11059                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11060
11061                 _bind_stmt_field_data_string(hStmt, field_idx++, input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
11062                 _bind_stmt_field_data_string(hStmt, field_idx++, input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
11063                 _bind_stmt_field_data_blob(hStmt, field_idx++, input_eas_data, input_eas_data_length);
11064                 _bind_stmt_field_data_int(hStmt, field_idx++, input_eas_data_length);
11065                 _bind_stmt_field_data_int(hStmt, field_idx++, input_mailbox_id);
11066         } else {
11067                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
11068                                 "UPDATE mail_box_tbl SET"
11069                                 " mailbox_name = ?"
11070                                 ",alias = ?"
11071                                 " WHERE mailbox_id = ?");
11072
11073                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11074
11075                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11076                                 ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11077
11078                 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
11079                 _bind_stmt_field_data_string(hStmt, field_idx++ , input_new_mailbox_alias, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
11080                 _bind_stmt_field_data_int(hStmt, field_idx++ , input_mailbox_id);
11081         }
11082
11083
11084         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11085         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
11086                         ("sqlite3_step fail:%d", rc));
11087         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11088                         ("sqlite3_step fail:%d", rc));
11089
11090         if (sqlite3_changes(local_db_handle) == 0)
11091                 EM_DEBUG_LOG("no mail_meeting_tbl matched...");
11092
11093         ret = true;
11094
11095 FINISH_OFF:
11096         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, error);
11097         if (error == EMAIL_ERROR_NONE) {
11098                 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME, account_id, input_mailbox_id, input_new_mailbox_name, 0))
11099                         EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAILBOX_RENAME ] >>>> ");
11100         } else {
11101                 if (!emcore_notify_storage_event(NOTI_MAILBOX_RENAME_FAIL, account_id, input_mailbox_id, input_new_mailbox_name, error))
11102                         EM_DEBUG_EXCEPTION("emcore_notify_storage_eventFailed [ NOTI_MAILBOX_RENAME_FAIL ] >>>> ");
11103         }
11104
11105         EM_SAFE_FREE(replaced_mailbox_name);
11106         EM_SAFE_FREE(replaced_alias);
11107
11108         if (old_mailbox_data)
11109                 emstorage_free_mailbox(&old_mailbox_data, 1, NULL);
11110
11111         if (hStmt != NULL) {
11112                 rc = sqlite3_finalize(hStmt);
11113                 if (rc != SQLITE_OK) {
11114                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11115                         error = EMAIL_ERROR_DB_FAILURE;
11116                 }
11117         }
11118
11119         EM_DEBUG_FUNC_END("error [%d]", error);
11120         return error;
11121 }
11122
11123 INTERNAL_FUNC int emstorage_get_new_attachment_no(char *multi_user_name, int *attachment_no, int *err_code)
11124 {
11125         EM_DEBUG_FUNC_BEGIN("attachment_no [%p], err_code[%p]", attachment_no, err_code);
11126         int rc, ret = false;
11127         int error = EMAIL_ERROR_NONE;
11128         char *sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
11129         char **result;
11130
11131         if (!attachment_no) {
11132                 EM_DEBUG_EXCEPTION("Invalid attachment");
11133                 if (err_code != NULL)
11134                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11135                 return false;
11136         }
11137
11138         *attachment_no = -1;
11139
11140         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11141
11142
11143         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
11144         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
11145                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
11146
11147         if (NULL == result[1])
11148                 rc = 1;
11149         else
11150                 rc = atoi(result[1])+1;
11151
11152         sqlite3_free_table(result);
11153
11154         *attachment_no = rc;
11155         EM_DEBUG_LOG("attachment_no [%d]", *attachment_no);
11156         ret = true;
11157
11158 FINISH_OFF:
11159
11160         if (err_code != NULL)
11161                 *err_code = error;
11162
11163         EM_DEBUG_FUNC_END("ret [%d]", ret);
11164         return ret;
11165 }
11166
11167 INTERNAL_FUNC int emstorage_add_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int iscopy, int transaction, int *err_code)
11168 {
11169         EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], iscopy[%d], transaction[%d], err_code[%p]", attachment_tbl, iscopy, transaction, err_code);
11170
11171         char *sql = NULL;
11172         char **result;
11173         int rc, ret = false;
11174         int error = EMAIL_ERROR_NONE;
11175         char sql_query_string[QUERY_SIZE] = {0, };
11176
11177         DB_STMT hStmt = NULL;
11178         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11179
11180         if (!attachment_tbl) {
11181                 EM_DEBUG_EXCEPTION("attachment_tbl[%p], iscopy[%d]", attachment_tbl, iscopy);
11182                 if (err_code != NULL)
11183                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11184                 return false;
11185         }
11186
11187         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11188
11189         sql = "SELECT max(rowid) FROM mail_attachment_tbl;";
11190
11191         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
11192         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
11193                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
11194
11195         if (NULL == result[1]) rc = 1;
11196         else rc = atoi(result[1]) + 1;
11197         sqlite3_free_table(result);
11198
11199         attachment_tbl->attachment_id = rc;
11200
11201         SNPRINTF(sql_query_string, sizeof(sql_query_string),
11202                         "INSERT INTO mail_attachment_tbl VALUES "
11203                         "(?"    /* attachment_id */
11204                         ", ?"   /* attachment_name */
11205                         ", ?"   /* attachment_path */
11206                         ", ?"   /* content_id */
11207                         ", ?"   /* attachment_size */
11208                         ", ?"   /* mail_id */
11209                         ", ?"   /* account_id */
11210                         ", ?"   /* mailbox_id */
11211                         ", ?"   /* attachment_save_status */
11212                         ", ?"   /* attachment_drm_type */
11213                         ", ?"   /* attachment_drm_method */
11214                         ", ?"   /* attachment_inline_content_status */
11215                         ", ?"   /* attachment_mime_type */
11216 #ifdef __ATTACHMENT_OPTI__
11217                         ", ?"
11218                         ", ?"
11219 #endif
11220                         ")");
11221
11222
11223         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11224         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11225                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11226
11227         _bind_stmt_field_data_int(hStmt, ATTACHMENT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_id);
11228         _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);
11229         _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);
11230         _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);
11231         _bind_stmt_field_data_int(hStmt, ATTACHMENT_SIZE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_size);
11232         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mail_id);
11233         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->account_id);
11234         _bind_stmt_field_data_int(hStmt, MAILBOX_ID_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->mailbox_id);
11235         _bind_stmt_field_data_int(hStmt, ATTACHMENT_SAVE_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_save_status);
11236         _bind_stmt_field_data_int(hStmt, ATTACHMENT_DRM_TYPE_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_type);
11237         _bind_stmt_field_data_int(hStmt, ATTACHMENT_DRM_METHOD_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_drm_method);
11238         _bind_stmt_field_data_int(hStmt, ATTACHMENT_INLINE_CONTENT_STATUS_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->attachment_inline_content_status);
11239         _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);
11240 #ifdef __ATTACHMENT_OPTI__
11241         _bind_stmt_field_data_int(hStmt, ENCODING_IDX_IN_MAIL_ATTACHMENT_TBL, attachment_tbl->encoding);
11242         _bind_stmt_field_data_string(hStmt, SECTION_IDX_IN_MAIL_ATTACHMENT_TBL, (char*)attachment_tbl->section, 0, ATTACHMENT_LEN_IN_MAIL_ATTACHMENT_TBL);
11243 #endif
11244
11245
11246         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11247         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
11248                         ("sqlite3_step fail:%d", rc));
11249         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11250                         ("sqlite3_step fail:%d", rc));
11251
11252         rc = sqlite3_changes(local_db_handle);
11253         if (rc == 0) {
11254                 EM_DEBUG_LOG(" no matched mail found...");
11255                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11256                 goto FINISH_OFF;
11257         }
11258
11259         ret = true;
11260
11261 FINISH_OFF:
11262         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11263
11264         if (hStmt != NULL) {
11265                 rc = sqlite3_finalize(hStmt);
11266                 if (rc != SQLITE_OK) {
11267                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11268                         error = EMAIL_ERROR_DB_FAILURE;
11269                 }
11270         }
11271
11272         if (err_code != NULL)
11273                 *err_code = error;
11274
11275         EM_DEBUG_FUNC_END("ret [%d]", ret);
11276         return ret;
11277 }
11278
11279 INTERNAL_FUNC int emstorage_update_attachment(char *multi_user_name, emstorage_attachment_tbl_t *attachment_tbl, int transaction, int *err_code)
11280 {
11281         EM_DEBUG_FUNC_BEGIN("attachment_tbl[%p], transaction[%d], err_code[%p]", attachment_tbl, transaction, err_code);
11282
11283         int rc, ret = false, field_idx = 0;
11284         int error = EMAIL_ERROR_NONE;
11285         DB_STMT hStmt = NULL;
11286         char sql_query_string[QUERY_SIZE] = {0, };
11287
11288         if (!attachment_tbl) {
11289                 EM_DEBUG_EXCEPTION(" attachment_tbl[%p] ", attachment_tbl);
11290                 if (err_code)
11291                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11292                 return false;
11293         }
11294
11295         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11296
11297         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11298
11299         SNPRINTF(sql_query_string, sizeof(sql_query_string),
11300                         "UPDATE mail_attachment_tbl SET  "
11301                         "  attachment_name = ?"
11302                         ", attachment_path =  ?"
11303                         ", content_id = ?"
11304                         ", attachment_size = ?"
11305                         ", mail_id = ?"
11306                         ", account_id = ?"
11307                         ", mailbox_id = ?"
11308                         ", attachment_save_status = ?"
11309                         ", attachment_drm_type = ?"
11310                         ", attachment_drm_method = ?"
11311                         ", attachment_inline_content_status = ? "
11312                         ", attachment_mime_type = ? "
11313                         " WHERE attachment_id = ?;");
11314
11315
11316         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
11317
11318         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11319                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
11320
11321         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_name, 0, ATTACHMENT_NAME_LEN_IN_MAIL_ATTACHMENT_TBL);
11322         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_path, 0, ATTACHMENT_PATH_LEN_IN_MAIL_ATTACHMENT_TBL);
11323         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->content_id, 0, CONTENT_ID_LEN_IN_MAIL_ATTACHMENT_TBL);
11324         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_size);
11325         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->mail_id);
11326         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->account_id);
11327         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->mailbox_id);
11328         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_save_status);
11329         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_drm_type);
11330         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_drm_method);
11331         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_inline_content_status);
11332         _bind_stmt_field_data_string(hStmt, field_idx++ , (char*)attachment_tbl->attachment_mime_type, 0, ATTACHMENT_MIME_TYPE_LEN_IN_MAIL_ATTACHMENT_TBL);
11333         _bind_stmt_field_data_int(hStmt, field_idx++ , attachment_tbl->attachment_id);
11334
11335
11336         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
11337         EM_DEBUG_DB_EXEC((SQLITE_FULL == rc), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
11338                         ("sqlite3_step fail:%d", rc));
11339         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
11340                         ("sqlite3_step fail:%d", rc));
11341
11342         rc = sqlite3_changes(local_db_handle);
11343         if (rc == 0) {
11344                 EM_DEBUG_LOG(" no matched mail found...");
11345                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
11346                 goto FINISH_OFF;
11347         }
11348
11349         ret = true;
11350
11351 FINISH_OFF:
11352         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11353         if (hStmt != NULL) {
11354                 rc = sqlite3_finalize(hStmt);
11355                 if (rc != SQLITE_OK) {
11356                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
11357                         error = EMAIL_ERROR_DB_FAILURE;
11358                 }
11359         }
11360
11361         if (err_code != NULL)
11362                 *err_code = error;
11363
11364         EM_DEBUG_FUNC_END("ret [%d]", ret);
11365         return ret;
11366 }
11367
11368 INTERNAL_FUNC int emstorage_delete_attachment_on_db(char *multi_user_name, int attachment_id, int transaction, int *err_code)
11369 {
11370         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], transaction[%d], err_code[%p]", attachment_id, transaction, err_code);
11371
11372         if (attachment_id < 0) {
11373                 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
11374                 if (err_code != NULL)
11375                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11376                 return false;
11377         }
11378
11379         int ret = false;
11380         int error = EMAIL_ERROR_NONE;
11381         char sql_query_string[QUERY_SIZE] = {0, };
11382         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11383
11384         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11385
11386         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE attachment_id = %d", attachment_id);
11387
11388         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11389         if (error != EMAIL_ERROR_NONE) {
11390                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11391                 goto FINISH_OFF;
11392         }
11393
11394         ret = true;
11395
11396 FINISH_OFF:
11397         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11398
11399         if (err_code)
11400                 *err_code = error;
11401
11402         EM_DEBUG_FUNC_END("ret [%d]", ret);
11403         return ret;
11404 }
11405
11406 INTERNAL_FUNC int emstorage_delete_all_attachments_of_mail(char *multi_user_name, int mail_id, int transaction, int *err_code)
11407 {
11408         EM_DEBUG_FUNC_BEGIN("mail_id[%d], transaction[%d], err_code[%p]", mail_id, transaction, err_code);
11409         int ret = false;
11410         int error = EMAIL_ERROR_NONE;
11411         char sql_query_string[QUERY_SIZE] = {0, };
11412         sqlite3 *local_db_handle = NULL;
11413
11414         if (mail_id <= 0) {
11415                 EM_DEBUG_EXCEPTION("mail_id[%d]", mail_id);
11416                 if (err_code != NULL)
11417                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11418                 return false;
11419         }
11420
11421         local_db_handle = emstorage_get_db_connection(multi_user_name);
11422
11423         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11424
11425         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl WHERE mail_id = %d", mail_id);
11426         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11427         if (error != EMAIL_ERROR_NONE) {
11428                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11429                 goto FINISH_OFF;
11430         }
11431
11432         ret = true;
11433
11434 FINISH_OFF:
11435         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11436
11437         if (err_code)
11438                 *err_code = error;
11439
11440         EM_DEBUG_FUNC_END("ret [%d]", ret);
11441         return ret;
11442 }
11443
11444 INTERNAL_FUNC int emstorage_delete_attachment_all_on_db(char *multi_user_name, int account_id, char *mailbox, int transaction, int *err_code)
11445 {
11446         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], transaction[%d], err_code[%p]", account_id, mailbox, transaction, err_code);
11447
11448         int error = EMAIL_ERROR_NONE;
11449         int ret = false;
11450         char sql_query_string[QUERY_SIZE] = {0, };
11451         char *replaced_mailbox = NULL;
11452         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11453
11454         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11455
11456         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_attachment_tbl");
11457
11458         if (account_id != ALL_ACCOUNT) /*  '0' means all account */
11459                 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);
11460
11461         if (mailbox) /*  NULL means all mailbox_name */ {
11462                 if (strstr(mailbox, "'"))
11463                         replaced_mailbox = em_replace_all_string(mailbox, "'", "''");
11464                 else
11465                         replaced_mailbox = strdup(mailbox);
11466
11467                 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);
11468                 EM_SAFE_FREE(replaced_mailbox); /*prevent 49434*/
11469         }
11470
11471         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11472         if (error != EMAIL_ERROR_NONE) {
11473                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11474                 goto FINISH_OFF;
11475         }
11476
11477         ret = true;
11478
11479 FINISH_OFF:
11480         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11481
11482         if (err_code != NULL)
11483                 *err_code = error;
11484
11485         EM_DEBUG_FUNC_END("ret [%d]", ret);
11486         return ret;
11487 }
11488
11489 INTERNAL_FUNC int emstorage_free_attachment(emstorage_attachment_tbl_t** attachment_tbl_list, int count, int *err_code)
11490 {
11491         EM_DEBUG_FUNC_BEGIN("attachment_tbl_list[%p], count[%d], err_code[%p]", attachment_tbl_list, count, err_code);
11492
11493         if (count > 0) {
11494                 if ((attachment_tbl_list == NULL) || (*attachment_tbl_list == NULL)) {
11495                         EM_DEBUG_LOG("Nothing to free: attachment_tbl_list[%p], count[%d]", attachment_tbl_list, count);
11496                         if (err_code != NULL)
11497                                 *err_code = EMAIL_ERROR_INVALID_PARAM;
11498                         return false;
11499                 }
11500
11501                 emstorage_attachment_tbl_t* p = *attachment_tbl_list;
11502                 int i;
11503
11504                 for (i = 0; i < count; i++) {
11505                         EM_SAFE_FREE(p[i].attachment_name);
11506                         EM_SAFE_FREE(p[i].attachment_path);
11507                         EM_SAFE_FREE(p[i].content_id);
11508                         EM_SAFE_FREE(p[i].attachment_mime_type);
11509 #ifdef __ATTACHMENT_OPTI__
11510                         EM_SAFE_FREE(p[i].section);
11511 #endif
11512                 }
11513
11514                 EM_SAFE_FREE(p);
11515                 *attachment_tbl_list = NULL;
11516         }
11517
11518         if (err_code != NULL)
11519                 *err_code = EMAIL_ERROR_NONE;
11520         EM_DEBUG_FUNC_END();
11521         return true;
11522 }
11523
11524 INTERNAL_FUNC int emstorage_begin_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11525 {
11526         EM_DEBUG_FUNC_BEGIN();
11527         EM_PROFILE_BEGIN(emStorageBeginTransaction);
11528         int ret = true;
11529
11530         _timedlock_shm_mutex(mapped_for_db_lock, 2);
11531
11532         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11533
11534         int rc;
11535         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN immediate;", NULL, NULL, NULL), rc);
11536         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11537                         ("SQL(BEGIN) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11538
11539         if (ret == false) {
11540                 if (err_code != NULL) *err_code = EMAIL_ERROR_DB_FAILURE;
11541         }
11542
11543         EM_PROFILE_END(emStorageBeginTransaction);
11544         EM_DEBUG_FUNC_END("ret [%d]", ret);
11545         return ret;
11546 }
11547
11548 INTERNAL_FUNC int emstorage_commit_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11549 {
11550         EM_DEBUG_FUNC_BEGIN();
11551         int ret = true;
11552         int rc;
11553
11554         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11555
11556         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
11557         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; }, ("SQL(END) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11558
11559         if (ret == false && err_code != NULL)
11560                 *err_code = EMAIL_ERROR_DB_FAILURE;
11561
11562         _unlockshm_mutex(mapped_for_db_lock);
11563
11564         EM_DEBUG_FUNC_END("ret [%d]", ret);
11565         return ret;
11566 }
11567
11568 INTERNAL_FUNC int emstorage_rollback_transaction(char *multi_user_name, void *d1, void *d2, int *err_code)
11569 {
11570         EM_DEBUG_FUNC_BEGIN();
11571         int ret = true;
11572         int rc;
11573         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11574
11575         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "ROLLBACK;", NULL, NULL, NULL), rc);
11576         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {ret = false; },
11577                         ("SQL(ROLLBACK) exec error:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
11578
11579         if (ret == false && err_code != NULL)
11580                 *err_code = EMAIL_ERROR_DB_FAILURE;
11581
11582         _unlockshm_mutex(mapped_for_db_lock);
11583
11584         EM_DEBUG_FUNC_END("ret [%d]", ret);
11585         return ret;
11586 }
11587
11588 INTERNAL_FUNC int emstorage_is_mailbox_full(char *multi_user_name, int account_id, email_mailbox_t *mailbox, int *result, int *err_code)
11589 {
11590         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], result[%p], err_code[%p]", account_id, mailbox, result, err_code);
11591
11592         if (account_id < FIRST_ACCOUNT_ID || !mailbox || !result) {
11593                 if (mailbox)
11594                         EM_DEBUG_EXCEPTION("Invalid Parameter. accoun_id[%d], mailbox[%p]", account_id, mailbox);
11595
11596                 if (err_code != NULL)
11597                         *err_code = EMAIL_ERROR_INVALID_PARAM;
11598
11599                 return false;
11600         }
11601
11602         int ret = false;
11603         int error = EMAIL_ERROR_NONE;
11604         int mail_count = 0;
11605
11606         if (!emstorage_get_mail_count(multi_user_name, account_id, mailbox->mailbox_id, &mail_count, NULL, true, &error)) {
11607                 EM_DEBUG_EXCEPTION("emstorage_get_mail_count failed [%d]", error);
11608                 goto FINISH_OFF;
11609         }
11610
11611         if (mailbox) {
11612                 EM_DEBUG_LOG("mail_count[%d] mail_slot_size[%d]", mail_count, mailbox->mail_slot_size);
11613                 if (mail_count >= mailbox->mail_slot_size)
11614                         *result = true;
11615                 else
11616                         *result = false;
11617
11618                 ret = true;
11619         }
11620
11621         ret = true;
11622 FINISH_OFF:
11623
11624         if (err_code != NULL)
11625                 *err_code = error;
11626
11627         EM_DEBUG_FUNC_END("ret [%d]", ret);
11628         return ret;
11629 }
11630
11631 INTERNAL_FUNC int emstorage_clear_mail_data(char *multi_user_name, int transaction, int *err_code)
11632 {
11633         EM_DEBUG_FUNC_BEGIN("transaction[%d], err_code[%p]", transaction, err_code);
11634
11635         int ret = false;
11636         int error = EMAIL_ERROR_NONE;
11637         char sql_query_string[QUERY_SIZE] = {0, };
11638         const email_db_object_t* tables = _g_db_tables;
11639         const email_db_object_t* indexes = _g_db_indexes;
11640
11641         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
11642         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
11643
11644         if (!emstorage_delete_dir((char *)MAIL_HOME, &error)) {
11645                 EM_DEBUG_EXCEPTION(" emstorage_delete_dir failed - %d", error);
11646
11647                 goto FINISH_OFF;
11648         }
11649
11650         if (mkdir(MAIL_HOME, DIRECTORY_PERMISSION) != 0) {
11651                 EM_DEBUG_EXCEPTION(" mkdir failed ");
11652                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11653                 if (errno == 28)
11654                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11655                 goto FINISH_OFF;
11656         }
11657         if (mkdir(MAIL_TEMP, DIRECTORY_PERMISSION) != 0) {
11658                 EM_DEBUG_EXCEPTION(" mkdir failed ");
11659                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11660                 if (errno == 28)
11661                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11662                 goto FINISH_OFF;
11663         }
11664         if (chmod(MAIL_TEMP, 0777) != 0)
11665                 EM_DEBUG_EXCEPTION("Fail to change the permission \n");
11666
11667         /*  first clear index. */
11668         while (indexes->object_name) {
11669                 if (indexes->data_flag) {
11670                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP index %s", indexes->object_name);
11671                         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11672                         if (error != EMAIL_ERROR_NONE) {
11673                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11674                                 goto FINISH_OFF;
11675                         }
11676                 }
11677                 indexes++;
11678         }
11679
11680         while (tables->object_name) {
11681                 if (tables->data_flag) {
11682                         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP table %s", tables->object_name);
11683                         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
11684                         if (error != EMAIL_ERROR_NONE) {
11685                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
11686                                 goto FINISH_OFF;
11687                         }
11688                 }
11689
11690                 tables++;
11691         }
11692         ret = true;
11693
11694 FINISH_OFF:
11695         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
11696
11697         if (err_code != NULL)
11698                 *err_code = error;
11699
11700         EM_DEBUG_FUNC_END("ret [%d]", ret);
11701         return ret;
11702 }
11703 /*======================= DB File Utils =============================================*/
11704 #include <dirent.h>
11705 #include <sys/types.h>
11706 #define  DIR_SEPERATOR "/"
11707
11708 INTERNAL_FUNC char *emstorage_make_directory_path_from_file_path(char *file_name)
11709 {
11710         EM_DEBUG_FUNC_BEGIN("Filename [ %p ]", file_name);
11711         char delims[] = "/";
11712         char *result = NULL;
11713         gchar **token = NULL;
11714
11715         token = g_strsplit_set(file_name, delims, 1);
11716
11717         if (token && token[0]) {
11718                 EM_DEBUG_LOG_SEC(">>>> Directory_name [ %s ]", token[0]);
11719                 result = EM_SAFE_STRDUP(token[0]);
11720         } else
11721                 EM_DEBUG_LOG(">>>> No Need to create Directory");
11722
11723         g_strfreev(token);
11724
11725         return result;
11726 }
11727
11728 INTERNAL_FUNC int emstorage_get_save_name(char *multi_user_name, int account_id, int mail_id, int atch_id,
11729                 char *fname, char *move_buf, char *path_buf, int maxlen, int *err_code)
11730 {
11731         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);
11732         EM_PROFILE_BEGIN(profile_emstorage_get_save_name);
11733
11734         int ret = false;
11735         int error = EMAIL_ERROR_NONE;
11736         char *dir_name = NULL;
11737         char create_dir[1024] = {0};
11738         char *temp_file = NULL;
11739         char *prefix_path = NULL;
11740         char *modified_fname = NULL;
11741
11742         if (!move_buf || !path_buf || account_id < FIRST_ACCOUNT_ID || mail_id < 0 || atch_id < 0) {
11743                 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);
11744                 error = EMAIL_ERROR_INVALID_PARAM;
11745                 goto FINISH_OFF;
11746         }
11747
11748         snprintf(path_buf, 512, "%s", MAIL_HOME);
11749         snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, account_id);
11750
11751         if (mail_id > 0)
11752                 snprintf(path_buf+EM_SAFE_STRLEN(path_buf),   512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, mail_id);
11753
11754         if (atch_id > 0)
11755                 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf), "%s%d", DIR_SEPERATOR, atch_id);
11756
11757         if (fname) {
11758                 temp_file = EM_SAFE_STRDUP(fname);
11759                 if (temp_file && strstr(temp_file, "/"))
11760                         dir_name = emstorage_make_directory_path_from_file_path(temp_file);
11761         }
11762
11763         if (dir_name) {
11764                 snprintf(create_dir, sizeof(create_dir), "%s%s%s", path_buf, DIR_SEPERATOR, dir_name);
11765                 EM_DEBUG_LOG(">>>>> DIR PATH [%s]", create_dir);
11766                 ret = mkdir(create_dir, DIRECTORY_PERMISSION);
11767                 EM_DEBUG_LOG("mkdir result = [%d]", ret);
11768                 EM_SAFE_FREE(dir_name);
11769         }
11770
11771         if (fname) {
11772                 EM_DEBUG_LOG_DEV(">>>>> fname [%s]", fname);
11773
11774                 /* Did not allow the slash */
11775                 modified_fname = reg_replace_new(fname, "/", "_");
11776                 EM_DEBUG_LOG_SEC("modified_fname : [%s]", modified_fname);
11777
11778                 if (modified_fname == NULL) modified_fname = g_strdup(fname);
11779
11780                 if (EM_SAFE_STRLEN(modified_fname) + EM_SAFE_STRLEN(path_buf) + strlen(DIR_SEPERATOR) > maxlen - 1) {
11781                         char *modified_name = NULL;
11782                         int remain_len  = (maxlen - 1) - EM_SAFE_STRLEN(path_buf) - strlen(DIR_SEPERATOR);
11783
11784                         if (remain_len <= 0) {
11785                                 error = EMAIL_ERROR_MAX_EXCEEDED;
11786                                 goto FINISH_OFF;
11787                         }
11788
11789                         if (remain_len > MAX_FILENAME)
11790                                 remain_len = MAX_FILENAME;
11791
11792                         modified_name = em_shrink_filename(modified_fname, remain_len);
11793
11794                         if (!modified_name) {
11795                                 error = EMAIL_ERROR_MAX_EXCEEDED;
11796                                 goto FINISH_OFF;
11797                         }
11798
11799                         snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),
11800                                 "%s%s", DIR_SEPERATOR, modified_name);
11801                         EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11802                         EM_SAFE_FREE(modified_name);
11803                 } else {
11804                         if (EM_SAFE_STRLEN(modified_fname) > MAX_FILENAME - 1) {
11805                                 char *modified_name = NULL;
11806
11807                                 modified_name = em_shrink_filename(modified_fname, MAX_FILENAME);
11808                                 if (!modified_name) {
11809                                         error = EMAIL_ERROR_MAX_EXCEEDED;
11810                                         goto FINISH_OFF;
11811                                 }
11812
11813                                 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),
11814                                         "%s%s", DIR_SEPERATOR, modified_name);
11815                                 EM_DEBUG_LOG(">>>>> Modified fname [%s]", modified_name);
11816                                 EM_SAFE_FREE(modified_name);
11817                         } else {
11818                                 snprintf(path_buf+EM_SAFE_STRLEN(path_buf), 512 - EM_SAFE_STRLEN(path_buf),
11819                                         "%s%s", DIR_SEPERATOR, modified_fname);
11820                         }
11821                 }
11822         }
11823
11824         EM_DEBUG_LOG_SEC(">>>>> path_buf [%s]", path_buf);
11825
11826         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11827                 error = emcore_get_container_path(multi_user_name, &prefix_path);
11828                 if (error != EMAIL_ERROR_NONE) {
11829                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11830                         goto FINISH_OFF;
11831                 }
11832                 snprintf(move_buf, 512, "%s/%s", prefix_path, path_buf);
11833                 EM_DEBUG_LOG_SEC("move_buf : [%s]", move_buf);
11834         } else {
11835                 snprintf(move_buf, 512, "%s", path_buf);
11836                 EM_DEBUG_LOG_SEC("move_buf : [%s]", move_buf);
11837         }
11838
11839         ret = true;
11840
11841 FINISH_OFF:
11842
11843         EM_SAFE_FREE(temp_file);
11844         EM_SAFE_FREE(prefix_path);
11845         EM_SAFE_FREE(modified_fname);
11846
11847         if (err_code != NULL)
11848                 *err_code = error;
11849
11850         EM_PROFILE_END(profile_emstorage_get_save_name);
11851         EM_DEBUG_FUNC_END("ret [%d]", ret);
11852         return ret;
11853 }
11854
11855 /*
11856    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)
11857    {
11858    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);
11859
11860    if (!name_buf || account_id < FIRST_ACCOUNT_ID) {
11861    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);
11862    if (err_code != NULL)
11863  *err_code = EMAIL_ERROR_INVALID_PARAM;
11864  return false;
11865  }
11866
11867  sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%s%d", MAILHOME, DIR_SEPERATOR, account_id);
11868
11869  if (mail_id > 0)
11870  sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, mail_id);
11871  else
11872  goto FINISH_OFF;
11873
11874  if (atch_id > 0)
11875  sprintf(name_buf+EM_SAFE_STRLEN(name_buf), "%s%d", DIR_SEPERATOR, atch_id);
11876  else
11877  goto FINISH_OFF;
11878
11879 FINISH_OFF:
11880 sprintf(name_buf+EM_SAFE_STRLEN(name_buf), ".DELE");
11881
11882 EM_DEBUG_FUNC_END();
11883 return true;
11884 }
11885 */
11886
11887 INTERNAL_FUNC int emstorage_create_dir(char *multi_user_name, int account_id, int mail_id, int atch_id, int *err_code)
11888 {
11889         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], atch_id[%d], err_code[%p]", account_id, mail_id, atch_id, err_code);
11890         EM_PROFILE_BEGIN(profile_emcore_save_create_dir);
11891         int ret = false;
11892         int error = EMAIL_ERROR_NONE;
11893         char buf[512];
11894         struct stat sbuf;
11895         char *prefix_path = NULL;
11896         char errno_buf[ERRNO_BUF_SIZE] = {0};
11897
11898         memset(buf, 0x00, sizeof(buf));
11899
11900         if (EM_SAFE_STRLEN(multi_user_name) > 0) {
11901                 error = emcore_get_container_path(multi_user_name, &prefix_path);
11902                 if (error != EMAIL_ERROR_NONE) {
11903                         EM_DEBUG_EXCEPTION("emcore_get_container_path failed : [%d]", error);
11904                         goto FINISH_OFF;
11905                 }
11906         } else {
11907                 prefix_path = strdup("");
11908         }
11909
11910         if (account_id >= FIRST_ACCOUNT_ID) {
11911                 SNPRINTF(buf, sizeof(buf), "%s%s%s%s%d", prefix_path,
11912                                 DIR_SEPERATOR,
11913                                 MAIL_HOME,
11914                                 DIR_SEPERATOR,
11915                                 account_id);
11916
11917                 if (stat(buf, &sbuf) == 0) {
11918                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11919                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11920                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11921                                 goto FINISH_OFF;
11922                         }
11923                 } else {
11924                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11925                                 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11926                                 EM_DEBUG_EXCEPTION("mkdir failed: %s", EM_STRERROR(errno_buf));
11927                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11928                                 if (errno == 28)
11929                                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11930                                 goto FINISH_OFF;
11931                         }
11932
11933                         if (account_id == EML_FOLDER)
11934                                 chmod(buf, 0777);
11935                 }
11936         }
11937
11938         if (mail_id > 0) {
11939                 int space_left_in_buffer = sizeof(buf) - EM_SAFE_STRLEN(buf);
11940
11941                 if (account_id < FIRST_ACCOUNT_ID) {
11942                         EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11943                         error = EMAIL_ERROR_INVALID_PARAM;
11944                         goto FINISH_OFF;
11945                 }
11946
11947                 if (space_left_in_buffer + 10 > sizeof(buf)) {
11948                         EM_DEBUG_EXCEPTION("Buffer overflowed");
11949                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11950                         goto FINISH_OFF;
11951                 }
11952
11953                 SNPRINTF(buf+EM_SAFE_STRLEN(buf), space_left_in_buffer, "%s%d", DIR_SEPERATOR, mail_id);
11954
11955                 if (stat(buf, &sbuf) == 0) {
11956                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11957                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11958                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11959                                 goto FINISH_OFF;
11960                         }
11961                 } else {
11962                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11963                                 EM_DEBUG_EXCEPTION("mkdir failed [%s]", buf);
11964                                 EM_DEBUG_EXCEPTION("mkdir failed [%d][%s]", errno, EM_STRERROR(errno_buf));
11965                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11966                                 if (errno == 28)
11967                                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11968                                 goto FINISH_OFF;
11969                         }
11970
11971                         if (account_id == EML_FOLDER)
11972                                 if (chmod(buf, 0777) !=0)
11973                                         EM_DEBUG_EXCEPTION("chmod failed ");
11974                 }
11975         }
11976
11977         if (atch_id > 0) {
11978                 if (account_id < FIRST_ACCOUNT_ID || mail_id <= 0) {
11979                         EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d], atch_id[%d]", account_id, mail_id, atch_id);
11980                         error = EMAIL_ERROR_INVALID_PARAM;
11981                         goto FINISH_OFF;
11982                 }
11983
11984                 SNPRINTF(buf+EM_SAFE_STRLEN(buf), sizeof(buf)-(EM_SAFE_STRLEN(buf)+1), "%s%d", DIR_SEPERATOR, atch_id);
11985
11986                 if (stat(buf, &sbuf) == 0) {
11987                         if ((sbuf.st_mode & S_IFMT) != S_IFDIR) {
11988                                 EM_DEBUG_EXCEPTION(" a object which isn't directory aleady exists");
11989                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11990                                 goto FINISH_OFF;
11991                         }
11992                 } else {
11993                         if (mkdir(buf, DIRECTORY_PERMISSION) != 0) {
11994                                 EM_DEBUG_EXCEPTION(" mkdir failed [%s]", buf);
11995                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
11996                                 if (errno == 28)
11997                                         error = EMAIL_ERROR_OUT_OF_MEMORY;
11998                                 goto FINISH_OFF;
11999                         }
12000
12001                         if (account_id == EML_FOLDER)
12002                                 if (chmod(buf, 0777) != 0)
12003                                         EM_DEBUG_EXCEPTION("Fail to change the permission \n");
12004                 }
12005         }
12006
12007         ret = true;
12008
12009 FINISH_OFF:
12010
12011         EM_SAFE_FREE(prefix_path);
12012
12013         if (err_code != NULL)
12014                 *err_code = error;
12015
12016         EM_PROFILE_END(profile_emcore_save_create_dir);
12017         EM_DEBUG_FUNC_END("ret [%d]", ret);
12018         return ret;
12019 }
12020
12021 INTERNAL_FUNC int emstorage_copy_file(char *src_file, char *dst_file, int sync_status, int *err_code)
12022 {
12023         EM_DEBUG_FUNC_BEGIN_SEC("src_file[%s], dst_file[%s], err_code[%p]", src_file, dst_file, err_code);
12024         EM_DEBUG_LOG("Using the fsync function");
12025         int ret = false;
12026         int error = EMAIL_ERROR_NONE;
12027         struct stat st_buf;
12028
12029         int fp_src = 0;
12030         int fp_dst = 0;
12031         int nread = 0;
12032         char buf[FILE_MAX_BUFFER_SIZE] = {0};
12033         char errno_buf[ERRNO_BUF_SIZE] = {0};
12034
12035         if (!src_file || !dst_file) {
12036                 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
12037
12038                 error = EMAIL_ERROR_INVALID_PARAM;
12039                 goto FINISH_OFF;
12040         }
12041
12042         if (stat(src_file, &st_buf) < 0) {
12043                 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", src_file);
12044
12045                 error = EMAIL_ERROR_SYSTEM_FAILURE;             /* EMAIL_ERROR_INVALID_PATH; */
12046                 goto FINISH_OFF;
12047         }
12048
12049         error = em_open(src_file, O_RDONLY, 0, &fp_src);
12050         if (error != EMAIL_ERROR_NONE) {
12051                 EM_DEBUG_EXCEPTION(">>>> Source Fail em_open %s Failed: %d", src_file, error);
12052                 goto FINISH_OFF;
12053         }
12054
12055         error = em_open(dst_file, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH, &fp_dst); /*prevent 24474*/
12056         if (error != EMAIL_ERROR_NONE) {
12057                 EM_DEBUG_EXCEPTION(">>>> Destination Fail em_open %s:  %d", dst_file, error);
12058                 goto FINISH_OFF;
12059         }
12060
12061         memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
12062
12063         while ((nread = read(fp_src, buf, FILE_MAX_BUFFER_SIZE)) > 0) {
12064                 if (nread > 0 && nread <= FILE_MAX_BUFFER_SIZE) {
12065                         EM_DEBUG_LOG("Nread Value [%d]", nread);
12066                         char *buf_ptr;
12067                         ssize_t byte_written = 0;
12068                         size_t remain_byte = nread;
12069                         buf_ptr = buf;
12070                         errno = 0;
12071
12072                         while (remain_byte > 0 && buf_ptr && errno == 0) {
12073                                 byte_written = write(fp_dst, buf_ptr, remain_byte);
12074
12075                                 if (byte_written < 0) {
12076                                         /* interrupted by a signal */
12077                                         if (errno == EINTR) {
12078                                                 errno = 0;
12079                                                 continue;
12080                                         }
12081
12082                                         EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
12083                                         error = EMAIL_ERROR_UNKNOWN;
12084                                         goto FINISH_OFF;
12085                                 }
12086                                 EM_DEBUG_LOG("NWRITTEN [%zu]", byte_written);
12087                                 remain_byte -= byte_written;
12088                                 buf_ptr += byte_written;
12089                         }
12090                 }
12091
12092                 memset(buf, 0x00, FILE_MAX_BUFFER_SIZE);
12093         }
12094
12095         ret = true;
12096
12097 FINISH_OFF:
12098         EM_SAFE_CLOSE(fp_src);
12099
12100         if (fp_dst >= 0) { /*prevent 24474*/
12101                 if (sync_status) {
12102                         EM_DEBUG_LOG("Before fsync");
12103                         fsync(fp_dst);
12104                 }
12105                 close(fp_dst);
12106         }
12107
12108         if (nread < 0 || error == EMAIL_ERROR_UNKNOWN)
12109                 remove(dst_file);
12110
12111         if (err_code != NULL)
12112                 *err_code = error;
12113         EM_DEBUG_FUNC_END("ret [%d]", ret);
12114         return ret;
12115 }
12116 /* create Directory if user has deleted [deepam.p@samsung.com] */
12117 INTERNAL_FUNC void emstorage_create_dir_if_delete()
12118 {
12119         EM_DEBUG_FUNC_BEGIN();
12120
12121         if (mkdir(DATA_PATH, DIRECTORY_PERMISSION) != 0)
12122                 EM_DEBUG_EXCEPTION("Fail to create the directory \n");
12123         if (mkdir(EMAIL_PATH, DIRECTORY_PERMISSION) != 0)
12124                 EM_DEBUG_EXCEPTION("Fail to create the directory \n");
12125         if (mkdir(MAIL_HOME, DIRECTORY_PERMISSION) != 0)
12126                 EM_DEBUG_EXCEPTION("Fail to create the directory \n");
12127         if (mkdir(MAIL_TEMP, DIRECTORY_PERMISSION) != 0)
12128                 EM_DEBUG_EXCEPTION("Fail to create the directory \n");
12129         if (chmod(MAIL_TEMP, 0777) != 0)
12130                 EM_DEBUG_EXCEPTION("Fail to change the permission \n");
12131
12132         EM_DEBUG_FUNC_END();
12133 }
12134
12135 static int _get_temp_file_name(char **filename, int *err_code)
12136 {
12137         EM_DEBUG_FUNC_BEGIN("filename[%p], err_code[%p]", filename, err_code);
12138
12139         int ret = false;
12140         int error = EMAIL_ERROR_NONE;
12141
12142         if (filename == NULL) {
12143                 EM_DEBUG_EXCEPTION(" filename[%p]", filename);
12144                 error = EMAIL_ERROR_INVALID_PARAM;
12145                 goto FINISH_OFF;
12146         }
12147
12148         char tempname[512] = {0x00, };
12149         struct timeval tv;
12150
12151         gettimeofday(&tv, NULL);
12152         srand(tv.tv_usec);
12153         unsigned int seed = time(NULL);
12154         SNPRINTF(tempname, sizeof(tempname), "%s%c%d", MAIL_TEMP, '/', rand_r(&seed));
12155
12156         char *p = EM_SAFE_STRDUP(tempname);
12157         if (p == NULL) {
12158                 EM_DEBUG_EXCEPTION(" strdup failed...");
12159                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12160                 goto FINISH_OFF;
12161         }
12162
12163         *filename = p;
12164
12165         ret = true;
12166
12167 FINISH_OFF:
12168         if (err_code != NULL)
12169                 *err_code = error;
12170
12171         EM_DEBUG_FUNC_END("ret [%d]", ret);
12172         return ret;
12173 }
12174
12175 INTERNAL_FUNC int emstorage_add_content_type(char *file_path, char *char_set, int *err_code)
12176 {
12177         EM_DEBUG_FUNC_BEGIN("File path [ %p ]  Character Set [ %p ] err_code [ %p]", file_path, char_set, err_code);
12178
12179         EM_IF_NULL_RETURN_VALUE(file_path, false);
12180         EM_IF_NULL_RETURN_VALUE(char_set, false);
12181         EM_IF_NULL_RETURN_VALUE(err_code, false);
12182
12183         char *buf =  NULL;
12184         char *buf1 = NULL;
12185         struct stat st_buf;
12186         int buf_size = 0;
12187         char *low_char_set = NULL;
12188         char *match_str = NULL;
12189         int nwritten = 0;
12190         int ret = false;
12191         int error = EMAIL_ERROR_NONE;
12192         int data_count_to_written = 0;
12193         char *temp_file_name = NULL;
12194         int err = 0;
12195         FILE* fp_src = NULL;
12196         FILE* fp_dest = NULL;
12197         int nread = 0;
12198
12199
12200         if (stat(file_path, &st_buf) < 0) {
12201                 EM_DEBUG_EXCEPTION_SEC(" stat(\"%s\") failed...", file_path);
12202                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12203                 goto FINISH_OFF;
12204         }
12205
12206         buf_size =  st_buf.st_size;
12207         EM_DEBUG_LOG(">>>> File Size [ %d ] ", buf_size);
12208         buf = (char *)calloc(1, buf_size+1);
12209
12210         if (!buf) {
12211                 EM_DEBUG_LOG(">>> Memory cannot be allocated ");
12212                 goto FINISH_OFF;
12213         }
12214
12215         error = em_fopen(file_path, "rb", &fp_src);
12216         if (error != EMAIL_ERROR_NONE || fp_src == NULL) {
12217                 EM_DEBUG_EXCEPTION_SEC(" file_path fopen failed - %s [%d]", file_path, error);
12218                 goto FINISH_OFF;
12219         }
12220
12221         if ((nread = fread(buf, 1, buf_size, fp_src)) > 0) {
12222                 if (nread > 0 && nread <= buf_size) {
12223                         EM_DEBUG_LOG(">>>> Nread Value [ %d ] ", nread);
12224
12225                         /**
12226                          *   1.Add check for whether content type is there.
12227                          *   2. If not based on the character set, Append it in File
12228                          **/
12229
12230                         low_char_set = calloc(1, EM_SAFE_STRLEN(char_set) + strlen(" \" /></head>") +1); /*prevent 34359*/
12231                         if (low_char_set == NULL) {
12232                                 EM_DEBUG_EXCEPTION("calloc failed");
12233                                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12234                                 goto FINISH_OFF;
12235                         }
12236
12237                         strncat(low_char_set, char_set, sizeof(low_char_set)-EM_SAFE_STRLEN(low_char_set)-1);
12238                         EM_DEBUG_LOG(">>>> CHAR SET [ %s ] ", low_char_set);
12239                         strncat(low_char_set, " \" /></head>", sizeof(low_char_set)-EM_SAFE_STRLEN(low_char_set)-1); /*prevent 34359*/
12240                         EM_DEBUG_LOG(">>> CHARSET [ %s ] ", low_char_set);
12241                         buf[nread] = '\0';
12242
12243                         match_str = strstr(buf, CONTENT_TYPE_DATA);
12244
12245                         if (match_str == NULL) {
12246                                 EM_DEBUG_LOG(">>>>emstorage_add_content_type 3 ");
12247                                 if (fp_src != NULL) {
12248                                         fclose(fp_src);
12249                                         fp_src = NULL;
12250                                 }
12251                                 data_count_to_written = EM_SAFE_STRLEN(low_char_set)+strlen(CONTENT_DATA)+1; /*prevent 34359*/
12252                                 buf1 = (char *)calloc(1, data_count_to_written);
12253
12254                                 if (buf1) {
12255                                         strncat(buf1, CONTENT_DATA, sizeof(buf1)-EM_SAFE_STRLEN(buf1)-1); /*prevent 34359*/
12256                                         EM_DEBUG_LOG(">>>>> BUF 1 [ %s ] ", buf1);
12257                                         strncat(buf1, low_char_set, sizeof(buf1)-EM_SAFE_STRLEN(buf1)-1);
12258                                         EM_DEBUG_LOG(">>>> HTML TAG DATA  [ %s ] ", buf1);
12259
12260                                         /* 1. Create a temporary file name */
12261                                         if (!_get_temp_file_name(&temp_file_name, &err)) {
12262                                                 EM_DEBUG_EXCEPTION(" emcore_get_temp_file_name failed - %d", err);
12263                                                 if (err_code != NULL) *err_code = err;
12264                                                 goto FINISH_OFF;
12265                                         }
12266                                         EM_DEBUG_LOG_SEC(">>>>>>> TEMP APPEND FILE PATH [ %s ] ", temp_file_name);
12267
12268                                         /* Open the Temp file in Append mode */
12269                                         error = em_fopen(temp_file_name, "ab", &fp_dest);
12270                                         if (error != EMAIL_ERROR_NONE) {
12271                                                 EM_DEBUG_EXCEPTION_SEC(" fopen failed - %s [%d]", temp_file_name, error);
12272                                                 goto FINISH_OFF;
12273                                         }
12274
12275                                         /* 2. write the Latest data */
12276                                         nwritten = fwrite(buf1, data_count_to_written-1, 1, fp_dest);
12277
12278                                         if (nwritten > 0) {
12279                                                 EM_DEBUG_LOG(" Latest Data  : [%d ] bytes written ", nwritten);
12280                                                 nwritten = 0;
12281                                                 /* 3. Append old data */
12282                                                 nwritten = fwrite(buf, nread-1, 1, fp_dest);
12283
12284                                                 if (nwritten <= 0) {
12285                                                         EM_DEBUG_EXCEPTION(" Error Occured while writing Old data : [%d ] bytes written ", nwritten);
12286                                                         error = EMAIL_ERROR_SYSTEM_FAILURE;
12287                                                         goto FINISH_OFF;
12288                                                 } else {
12289                                                         EM_DEBUG_LOG(">>>> OLD data appended [ %d ] ", nwritten);
12290
12291                                                         if (!emstorage_move_file(temp_file_name, file_path, false, &err)) {
12292                                                                 EM_DEBUG_EXCEPTION(" emstorage_move_file failed - %d", err);
12293                                                                 goto FINISH_OFF;
12294                                                         }
12295                                                 }
12296
12297                                         } else {
12298                                                 EM_DEBUG_EXCEPTION(" Error Occured while writing New data : [%d ] bytes written ", nwritten);
12299                                                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12300                                                 goto FINISH_OFF;
12301                                         }
12302                                 }
12303                         }
12304                 }
12305         }
12306
12307         ret = true;
12308 FINISH_OFF:
12309
12310         EM_SAFE_FREE(buf);
12311         EM_SAFE_FREE(buf1);
12312         EM_SAFE_FREE(low_char_set);
12313         EM_SAFE_FREE(temp_file_name);
12314
12315         if (fp_src != NULL) {
12316                 fclose(fp_src);
12317                 fp_src = NULL;
12318         }
12319
12320         if (fp_dest != NULL) {
12321                 fclose(fp_dest);
12322                 fp_dest = NULL;
12323         }
12324
12325         if (err_code)
12326                 *err_code = error;
12327
12328         EM_DEBUG_FUNC_END("ret [%d]", ret);
12329         return ret;
12330
12331 }
12332
12333 INTERNAL_FUNC int emstorage_move_file(char *src_file, char *dst_file, int sync_status, int *err_code)
12334 {
12335         EM_DEBUG_FUNC_BEGIN("src_file[%p], dst_file[%p], err_code[%p]", src_file, dst_file, err_code);
12336
12337         int ret = false;
12338         int error = EMAIL_ERROR_NONE;
12339         char errno_buf[ERRNO_BUF_SIZE] = {0};
12340
12341         if (src_file == NULL || dst_file == NULL) {
12342                 EM_DEBUG_EXCEPTION("src_file[%p], dst_file[%p]", src_file, dst_file);
12343                 error = EMAIL_ERROR_INVALID_PARAM;
12344                 goto FINISH_OFF;
12345         }
12346
12347         EM_DEBUG_LOG_SEC("src_file[%s], dst_file[%s]", src_file, dst_file);
12348
12349         if (strcmp(src_file, dst_file) != 0) {
12350                 EM_DEBUG_LOG("oldpath and newpath are not on the same mounted file system.");
12351                 if (!emstorage_copy_file(src_file, dst_file, sync_status, &error)) {
12352                         EM_DEBUG_EXCEPTION("emstorage_copy_file failed - %d", error);
12353
12354                         struct stat temp_file_stat;
12355                         if (stat(src_file, &temp_file_stat) < 0)
12356                                 EM_DEBUG_EXCEPTION("no src file found [%s] : %s", src_file, EM_STRERROR(errno_buf));
12357
12358                         if (stat(dst_file, &temp_file_stat) < 0)
12359                                 EM_DEBUG_EXCEPTION("no dst file found [%s] : %s", dst_file, EM_STRERROR(errno_buf));
12360
12361                         error = EMAIL_ERROR_FILE_NOT_FOUND;
12362                         goto FINISH_OFF;
12363
12364
12365                 }
12366                 remove(src_file);
12367                 EM_DEBUG_LOG("src[%s] removed", src_file);
12368         } else {
12369                 EM_DEBUG_LOG("src[%s] = dst[%s]", src_file, dst_file);
12370         }
12371
12372         ret = true;
12373
12374 FINISH_OFF:
12375         if (err_code != NULL)
12376                 *err_code = error;
12377
12378         EM_DEBUG_FUNC_END("ret [%d]", ret);
12379         return ret;
12380 }
12381
12382 INTERNAL_FUNC int emstorage_delete_file(char *src_file, int *err_code)
12383 {
12384         EM_DEBUG_FUNC_BEGIN("src_file[%p], err_code[%p]", src_file, err_code);
12385
12386         int ret = false;
12387         int error = EMAIL_ERROR_NONE;
12388
12389         if (src_file == NULL) {
12390                 EM_DEBUG_EXCEPTION(" src_file[%p]", src_file);
12391
12392                 error = EMAIL_ERROR_INVALID_PARAM;
12393                 goto FINISH_OFF;
12394         }
12395
12396         if (remove(src_file) != 0) {
12397                 if (errno != ENOENT) {
12398                         EM_DEBUG_EXCEPTION(" remove failed - %d", errno);
12399
12400                         error = EMAIL_ERROR_SYSTEM_FAILURE;
12401                         goto FINISH_OFF;
12402                 } else {
12403                         EM_DEBUG_EXCEPTION(" no file found...");
12404
12405                         error = EMAIL_ERROR_FILE_NOT_FOUND;
12406                 }
12407         }
12408
12409         ret = true;
12410
12411 FINISH_OFF:
12412         if (err_code != NULL)
12413                 *err_code = error;
12414
12415         EM_DEBUG_FUNC_END("ret [%d]", ret);
12416         return ret;
12417 }
12418
12419
12420 INTERNAL_FUNC int emstorage_delete_dir(char *src_dir, int *err_code)
12421 {
12422         EM_DEBUG_FUNC_BEGIN("src_dir[%p], err_code[%p]", src_dir, err_code);
12423
12424         if (src_dir == NULL) {
12425                 EM_DEBUG_EXCEPTION("src_dir[%p]", src_dir);
12426
12427                 if (err_code != NULL)
12428                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12429                 return false;
12430         }
12431
12432         int error = EMAIL_ERROR_NONE;
12433
12434         DIR *dirp;
12435         struct dirent *dp;
12436         struct stat sbuf;
12437         char buf[512];
12438
12439         dirp = opendir(src_dir);
12440
12441         if (dirp == NULL) {
12442                 if (errno == ENOENT) {
12443                         EM_DEBUG_EXCEPTION("directory[%s] does not exist...", src_dir);
12444                         if (err_code != NULL)
12445                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12446                         return true;
12447                 } else {
12448                         EM_DEBUG_EXCEPTION("opendir failed [%s] [%d]", src_dir, errno);
12449                         if (err_code != NULL)
12450                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12451                         return false;
12452                 }
12453         }
12454
12455         while ((dp = readdir(dirp))) {
12456                 if (strncmp(dp->d_name, ".", 1) == 0 || strncmp(dp->d_name, "..", 2) == 0) /* prevent 34360 */
12457                         continue;
12458
12459                 SNPRINTF(buf, sizeof(buf), "%s/%s", src_dir, dp->d_name);
12460
12461                 if (lstat(buf, &sbuf) == 0 || stat(buf, &sbuf) == 0) {
12462                         /*  check directory */
12463                         if ((sbuf.st_mode & S_IFMT) == S_IFDIR) {       /*  directory */
12464                                 /*  recursive call */
12465                                 if (!emstorage_delete_dir(buf, &error)) {
12466                                         closedir(dirp);
12467                                         if (err_code != NULL)
12468                                                 *err_code = error;
12469                                         return false;
12470                                 }
12471                         } else {        /*  file */
12472                                 if (remove(buf) < 0) {
12473                                         EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", buf, errno);
12474                                         closedir(dirp);
12475                                         if (err_code != NULL)
12476                                                 *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12477                                         return false;
12478                                 }
12479                         }
12480                 } else
12481                         EM_DEBUG_EXCEPTION("content does not exist...");
12482         }
12483
12484         closedir(dirp);
12485
12486         EM_DEBUG_LOG_DEV("remove direcotory [%s]", src_dir);
12487
12488         /* EM_DEBUG_FUNC_BEGIN(); */
12489
12490         if (remove(src_dir) < 0) {
12491                 EM_DEBUG_EXCEPTION("remove failed [%s] [%d]", src_dir, errno);
12492                 if (err_code != NULL)
12493                         *err_code = EMAIL_ERROR_SYSTEM_FAILURE;
12494                 return false;
12495         }
12496
12497         if (err_code != NULL)
12498                 *err_code = error;
12499
12500         return true;
12501 }
12502
12503 /* faizan.h@samsung.com */
12504 INTERNAL_FUNC int emstorage_update_server_uid(char *multi_user_name,
12505                 int mail_id,
12506                 char *old_server_uid,
12507                 char *new_server_uid,
12508                 int *err_code)
12509 {
12510         EM_DEBUG_FUNC_BEGIN("new_server_uid[%s], old_server_uid[%s]", new_server_uid, old_server_uid);
12511         int ret = false;
12512         int error = EMAIL_ERROR_NONE;
12513         int transaction = true;
12514         int temp_strlen = 0;
12515         int and_operation = 0;
12516         char sql_query_string[QUERY_SIZE] = {0, };
12517         char conditional_clause_string[QUERY_SIZE] = {0};
12518
12519         if ((mail_id <= 0 || !old_server_uid) && !new_server_uid) {
12520                 EM_DEBUG_EXCEPTION("Invalid parameters");
12521                 if (err_code)
12522                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12523                 return false;
12524         }
12525
12526         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12527         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12528
12529         SNPRINTF(conditional_clause_string, sizeof(conditional_clause_string), "WHERE ");
12530
12531         if (mail_id > 0) {
12532                 temp_strlen = strlen(conditional_clause_string);
12533                 SNPRINTF(conditional_clause_string + temp_strlen, sizeof(conditional_clause_string) - temp_strlen,
12534                                 "mail_id = %d ", mail_id);
12535                 and_operation = 1;
12536         }
12537
12538         if (old_server_uid) {
12539                 temp_strlen = strlen(conditional_clause_string);
12540                 if (!and_operation) {
12541                         sqlite3_snprintf(sizeof(conditional_clause_string) - temp_strlen, conditional_clause_string + temp_strlen,
12542                                         "server_mail_id = '%q'", old_server_uid);
12543                 } else {
12544                         sqlite3_snprintf(sizeof(conditional_clause_string) - temp_strlen, conditional_clause_string + temp_strlen,
12545                                         "and server_mail_id = '%q'", old_server_uid);
12546                 }
12547         }
12548
12549         sqlite3_snprintf(sizeof(sql_query_string), sql_query_string,
12550                         "UPDATE mail_tbl SET server_mail_id = '%q' %s", new_server_uid, conditional_clause_string);
12551
12552         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12553         if (error != EMAIL_ERROR_NONE) {
12554                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12555                 goto FINISH_OFF;
12556         }
12557
12558         ret = true;
12559
12560 FINISH_OFF:
12561
12562         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12563
12564         if (err_code != NULL)
12565                 *err_code = error;
12566
12567         EM_DEBUG_FUNC_END("ret [%d]", ret);
12568         return ret;
12569
12570 }
12571
12572 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)
12573 {
12574         EM_DEBUG_FUNC_BEGIN_SEC("mail_id[%d], new_server_uid[%s], mbox_name[%s]", mail_id, new_server_uid, mbox_name);
12575
12576         int ret = false;
12577         int error = EMAIL_ERROR_NONE;
12578         char sql_query_string[QUERY_SIZE] = {0, };
12579         int transaction = true;
12580
12581         if (!mail_id || !new_server_uid || !mbox_name) {
12582                 EM_DEBUG_EXCEPTION("Invalid parameters");
12583                 if (err_code != NULL)
12584                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12585                 return false;
12586         }
12587
12588         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12589         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12590
12591
12592         SNPRINTF(sql_query_string, sizeof(sql_query_string),
12593                         "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);
12594
12595         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12596         if (error != EMAIL_ERROR_NONE) {
12597                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12598                 goto FINISH_OFF;
12599         }
12600
12601         ret     = true;
12602
12603 FINISH_OFF:
12604
12605         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12606
12607         if (err_code != NULL)
12608                 *err_code = error;
12609
12610         EM_DEBUG_FUNC_END("ret [%d]", ret);
12611         return ret;
12612
12613 }
12614
12615 INTERNAL_FUNC int emstorage_update_save_status(char *multi_user_name, int account_id, int *err_code)
12616 {
12617         EM_DEBUG_FUNC_BEGIN();
12618
12619         int ret = false;
12620         int transaction = true;
12621         int error = EMAIL_ERROR_NONE;
12622         char sql_query_string[QUERY_SIZE] = {0,};
12623
12624         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12625
12626         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12627
12628         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12629
12630         if (account_id <= ALL_ACCOUNT)
12631                 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);
12632         else
12633                 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);
12634
12635         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
12636         if (error != EMAIL_ERROR_NONE) {
12637                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
12638                 goto FINISH_OFF;
12639         }
12640
12641         ret = true;
12642
12643 FINISH_OFF:
12644         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
12645
12646         if (err_code != NULL)
12647                 *err_code = error;
12648
12649         EM_DEBUG_FUNC_END("ret [%d]", ret);
12650         return ret;
12651
12652 }
12653
12654 int emstorage_get_unread_mailid(char *multi_user_name, int account_id, int vip_mode, int **mail_ids, int *mail_number, int *err_code)
12655 {
12656         EM_DEBUG_FUNC_BEGIN();
12657
12658         if ((!mail_ids) || (account_id <= 0 &&  account_id != -1)) {
12659                 EM_DEBUG_EXCEPTION(" mail_id[%p], account_id[%d] ", mail_ids, account_id);
12660                 if (err_code != NULL)
12661                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12662                 return false;
12663         }
12664
12665         int ret = false;
12666         int rc = -1;
12667         int error = EMAIL_ERROR_NONE;
12668         int count = 0;
12669         int i = 0;
12670         int col_index = 0;
12671         int *p_mail_ids = NULL;
12672         int transaction = false;
12673         char **result = NULL;
12674         char sql_query_string[QUERY_SIZE] = {0, };
12675         char temp_query_string[QUERY_SIZE] = {0,};
12676         char sql_select_query_string[QUERY_SIZE] = {0, };
12677         int wrn = 0;
12678
12679         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12680         EMSTORAGE_START_READ_TRANSACTION(transaction);
12681
12682         memset(sql_query_string, 0x00, sizeof(sql_query_string));
12683
12684         if (account_id == -1) {
12685                 SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string),
12686                         "SELECT mail_id FROM mail_tbl WHERE flags_seen_field = 0 AND (save_status = %d or save_status = %d)",
12687                         EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12688         } else {
12689                 SNPRINTF(sql_select_query_string, sizeof(sql_select_query_string),
12690                         "SELECT mail_id FROM mail_tbl WHERE account_id = %d AND flags_seen_field = 0 AND (save_status = %d or save_status = %d)",
12691                         account_id, EMAIL_MAIL_STATUS_NOTI_WAITED, EMAIL_MAIL_STATUS_RECEIVED);
12692         }
12693
12694         if (vip_mode) {
12695                 wrn = SNPRINTF(temp_query_string, sizeof(temp_query_string),
12696                         "%s AND tag_id < 0", sql_select_query_string);
12697         } else {
12698                 SNPRINTF(temp_query_string, sizeof(temp_query_string), "%s", sql_select_query_string);
12699         }
12700
12701         if(wrn < 0){
12702                 EM_DEBUG_LOG("WARNING: temp query string is truncated (%s)", temp_query_string);
12703         }
12704
12705         wrn = SNPRINTF(sql_query_string, sizeof(sql_query_string), "%s ORDER BY date_time ASC", temp_query_string);
12706
12707         if(wrn < 0){
12708                 EM_DEBUG_LOG("WARNING: sql query string is truncated (%s)", sql_query_string);
12709         }
12710
12711         EM_DEBUG_LOG_SEC("query: [%s]", sql_query_string);
12712
12713         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
12714         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
12715                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12716
12717         EM_DEBUG_LOG("Count : %d", count);
12718
12719         if (count == 0) {
12720                 EM_DEBUG_EXCEPTION("no Mails found...");
12721                 ret = false;
12722                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
12723                 goto FINISH_OFF;
12724         }
12725
12726         p_mail_ids = em_malloc(count * sizeof(int));
12727         if (p_mail_ids == NULL) {
12728                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
12729                 error = EMAIL_ERROR_OUT_OF_MEMORY;
12730                 goto FINISH_OFF;
12731         }
12732
12733         col_index = 1;
12734
12735         for (i = 0; i < count; i++)
12736                 _get_table_field_data_int(result, &(p_mail_ids[i]), col_index++);
12737
12738         ret = true;
12739
12740 FINISH_OFF:
12741
12742         if (result)
12743                 sqlite3_free_table(result);
12744
12745         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
12746
12747         //      sqlite3_db_release_memory(local_db_handle);
12748
12749         if (ret == true) {
12750                 if (mail_ids != NULL)
12751                         *mail_ids = p_mail_ids;
12752
12753                 if (mail_number != NULL)
12754                         *mail_number = count;
12755         } else {
12756                 EM_SAFE_FREE(p_mail_ids);
12757         }
12758
12759         if (err_code != NULL)
12760                 *err_code = error;
12761
12762         EM_DEBUG_FUNC_END("ret [%d]", ret);
12763         return ret;
12764 }
12765
12766 int setting_system_command(const char *command)
12767
12768 {
12769         int pid = 0, status = 0;
12770         char *const environ[] = { NULL };
12771
12772         if (command == 0)
12773                 return 1;
12774
12775         pid = fork();
12776
12777         if (pid == -1)
12778                 return -1;
12779
12780         if (pid == 0) {
12781                 char *argv[4];
12782
12783                 argv[0] = "sh";
12784                 argv[1] = "-c";
12785                 argv[2] = (char *)command;
12786                 argv[3] = 0;
12787
12788                 execve("/bin/sh", argv, environ);
12789                 abort();
12790         }
12791
12792         do {
12793                 if (waitpid(pid, &status, 0) == -1) {
12794                         if (errno != EINTR)
12795                                 return -1;
12796                 } else {
12797                         return status;
12798                 }
12799         } while (1);
12800
12801         return 0;
12802 }
12803
12804
12805 INTERNAL_FUNC int emstorage_mail_get_total_diskspace_usage(unsigned long *total_usage,  int transaction, int *err_code)
12806 {
12807         EM_DEBUG_FUNC_BEGIN("total_usage[%p],  transaction[%d], err_code[%p]", total_usage, transaction, err_code);
12808
12809         if (!total_usage) {
12810                 EM_DEBUG_EXCEPTION("total_usage[%p]", total_usage);
12811
12812                 if (err_code != NULL)
12813                         *err_code = EMAIL_ERROR_INVALID_PARAM;
12814                 return false;
12815         }
12816
12817         int   ret = false;
12818         int   error = EMAIL_ERROR_NONE;
12819         char  syscmd[256] = {0, };
12820         char  line[256] = {0, };
12821         char *line_from_file = NULL;
12822         FILE *fp = NULL;
12823         unsigned long total_diskusage = 0;
12824
12825         SNPRINTF(syscmd, sizeof(syscmd), "touch %s", SETTING_MEMORY_TEMP_FILE_PATH);
12826         if (setting_system_command(syscmd) == -1) {
12827                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : [Setting > Memory] System Command [%s] is failed", syscmd);
12828
12829                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12830                 goto FINISH_OFF;
12831         }
12832
12833         SNPRINTF(syscmd, sizeof(syscmd), "du -hsk %s > %s", EMAIL_PATH, SETTING_MEMORY_TEMP_FILE_PATH);
12834         EM_DEBUG_LOG(" cmd : %s", syscmd);
12835         if (setting_system_command(syscmd) == -1) {
12836                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage : Setting > Memory] System Command [%s] is failed", syscmd);
12837
12838                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12839                 goto FINISH_OFF;
12840         }
12841
12842         error = em_fopen(SETTING_MEMORY_TEMP_FILE_PATH, "r", &fp);
12843         if (error != EMAIL_ERROR_NONE) {
12844                 perror(SETTING_MEMORY_TEMP_FILE_PATH);
12845                 goto FINISH_OFF;
12846         }
12847
12848         line_from_file = fgets(line, sizeof(line), fp);
12849
12850         if (line_from_file == NULL) {
12851                 EM_DEBUG_EXCEPTION("fgets failed");
12852                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12853                 goto FINISH_OFF;
12854         }
12855         total_diskusage = strtoul(line, NULL, 10);
12856
12857         memset(syscmd, 0, sizeof(syscmd));
12858         SNPRINTF(syscmd, sizeof(syscmd), "rm -f %s", SETTING_MEMORY_TEMP_FILE_PATH);
12859         if (setting_system_command(syscmd) == -1) {
12860                 EM_DEBUG_EXCEPTION("emstorage_mail_get_total_diskspace_usage :  [Setting > Memory] System Command [%s] is failed", syscmd);
12861                 error = EMAIL_ERROR_SYSTEM_FAILURE;
12862                 goto FINISH_OFF;
12863         }
12864
12865         EM_DEBUG_LOG("[Setting > Memory] @@@@@ Size of Directory [%s] is %ld KB", EMAIL_PATH, total_diskusage);
12866
12867         ret = true;
12868
12869 FINISH_OFF:
12870         if (err_code != NULL)
12871                 *err_code = error;
12872
12873         if (ret)
12874                 *total_usage = total_diskusage;
12875         else
12876                 *total_usage = 0;
12877
12878         if (fp) fclose(fp); /* prevent 32730 */
12879
12880         EM_DEBUG_FUNC_END("ret [%d]", ret);
12881         return ret;
12882 }
12883 #define MAILHOME_UTF8    tzplatform_mkpath(TZ_USER_DATA, "email/.email_data/7/348/UTF-8")
12884
12885 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)
12886 {
12887         DB_STMT hStmt = NULL;
12888         int ret = false;
12889         int error = EMAIL_ERROR_NONE;
12890         int rc = 0;
12891         char sql_query_string[QUERY_SIZE] = {0, };
12892
12893         SNPRINTF(sql_query_string, sizeof(sql_query_string),
12894                         "INSERT INTO mail_tbl VALUES "
12895                         "(?" /*  mail_id */
12896                         ", ?" /*  account_id */
12897                         ", ?" /*  mail_size */
12898                         ", ?" /*  server_mail_status */
12899                         ", ?" /*  server_mailbox_name */
12900                         ", ?" /*  server_mail_id */
12901                         ", ?" /*  reference_mail_id */
12902                         ", ?" /*  full_address_from */
12903                         ", ?" /*  full_address_reply */
12904                         ", ?" /*  full_address_to */
12905                         ", ?" /*  full_address_cc */
12906                         ", ?" /*  full_address_bcc */
12907                         ", ?" /*  full_address_return */
12908                         ", ?" /*  subject */
12909                         ", ?" /*  body_download_status */
12910                         ", ?" /*  file_path_plain */
12911                         ", ?" /*  file_path_html */
12912                         ", ?" /*  date_time */
12913                         ", ?" /*  flags_seen_field */
12914                         ", ?" /*  flags_deleted_field */
12915                         ", ?" /*  flags_flagged_field */
12916                         ", ?" /*  flags_answered_field */
12917                         ", ?" /*  flags_recent_field */
12918                         ", ?" /*  flags_draft_field */
12919                         ", ?" /*  flags_forwarded_field */
12920                         ", ?" /*  DRM_status */
12921                         ", ?" /*  priority */
12922                         ", ?" /*  save_status */
12923                         ", ?" /*  lock_status */
12924                         ", ?" /*  message_id */
12925                         ", ?" /*  report_status */
12926                         ", ?" /*  email_address_sender */
12927                         ", ?" /*  email_address_recipient */
12928                         ", ?" /*  attachment_count */
12929                         ", ?" /*  inline_content_count */
12930                         ", ?" /*  preview_text */
12931                         ", ?" /*  thread_id */
12932                         ", ?" /*  mailbox_type */
12933                         ", ?" /*  alias_sender */
12934                         ", ?" /*  alias_recipient */
12935                         ", ?" /*  thread_item_count */
12936                         ", ?" /*  meeting_request_status */
12937                         ", ?" /*  message_class */
12938                         ", ?" /*  digest_type */
12939                         ", ?" /*  smime_type */
12940                         ", ?" /*  scheduled_sending_time */
12941                         ", ?" /*  remaining_resend_times */
12942                         ", ?" /*  tag_id   */
12943                         ", ?" /*  replied_time */
12944                         ", ?" /*  forwarded_time */
12945                         ", ?" /*  default_charset */
12946                         ", ?" /*  eas_data_length */
12947                         ", ?" /*  eas_data */
12948                         ")");
12949
12950         int transaction = true;
12951         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
12952
12953         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
12954
12955
12956         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
12957         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
12958                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
12959
12960         _bind_stmt_field_data_int(hStmt, MAIL_ID_IDX_IN_MAIL_TBL, mail_id);
12961         _bind_stmt_field_data_int(hStmt, ACCOUNT_ID_IDX_IN_MAIL_TBL, account_id);
12962         _bind_stmt_field_data_int(hStmt, MAILBOX_TYPE_IDX_IN_MAIL_TBL, EMAIL_MAILBOX_TYPE_OUTBOX);
12963         _bind_stmt_field_data_string(hStmt, SUBJECT_IDX_IN_MAIL_TBL, "save test - long", 1, SUBJECT_LEN_IN_MAIL_TBL);
12964         _bind_stmt_field_data_string(hStmt, DATETIME_IDX_IN_MAIL_TBL, "20100316052908", 0, DATETIME_LEN_IN_MAIL_TBL);
12965         _bind_stmt_field_data_int(hStmt, SERVER_MAIL_STATUS_IDX_IN_MAIL_TBL, 0);
12966         _bind_stmt_field_data_string(hStmt, SERVER_MAILBOX_NAME_IDX_IN_MAIL_TBL, "", 0, SERVER_MAILBOX_LEN_IN_MAIL_TBL);
12967         _bind_stmt_field_data_string(hStmt, SERVER_MAIL_ID_IDX_IN_MAIL_TBL, "", 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
12968         _bind_stmt_field_data_string(hStmt, MESSAGE_ID_IDX_IN_MAIL_TBL, "", 0, MESSAGE_ID_LEN_IN_MAIL_TBL);
12969         _bind_stmt_field_data_int(hStmt, REFERENCE_ID_IDX_IN_MAIL_TBL, 0);
12970         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_FROM_IDX_IN_MAIL_TBL, "<test08@streaming.s3glab.net>", 1, FROM_LEN_IN_MAIL_TBL);
12971         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_REPLY_IDX_IN_MAIL_TBL, "", 1, REPLY_TO_LEN_IN_MAIL_TBL);
12972         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_TO_IDX_IN_MAIL_TBL, full_address_to, 1, TO_LEN_IN_MAIL_TBL);
12973         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_CC_IDX_IN_MAIL_TBL, full_address_cc, 1, CC_LEN_IN_MAIL_TBL);
12974         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_BCC_IDX_IN_MAIL_TBL, full_address_bcc, 1, BCC_LEN_IN_MAIL_TBL);
12975         _bind_stmt_field_data_string(hStmt, FULL_ADDRESS_RETURN_IDX_IN_MAIL_TBL, "", 1, RETURN_PATH_LEN_IN_MAIL_TBL);
12976         _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);
12977         _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);
12978         _bind_stmt_field_data_string(hStmt, ALIAS_SENDER_IDX_IN_MAIL_TBL, "send_alias", 1, FROM_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12979         _bind_stmt_field_data_string(hStmt, ALIAS_RECIPIENT_IDX_IN_MAIL_TBL, "recipient_alias", 1, TO_EMAIL_ADDRESS_LEN_IN_MAIL_TBL);
12980         _bind_stmt_field_data_int(hStmt, BODY_DOWNLOAD_STATUS_IDX_IN_MAIL_TBL, 1);
12981         _bind_stmt_field_data_string(hStmt, FILE_PATH_PLAIN_IDX_IN_MAIL_TBL, (char *)MAILHOME_UTF8, 0, TEXT_1_LEN_IN_MAIL_TBL);
12982         _bind_stmt_field_data_string(hStmt, FILE_PATH_HTML_IDX_IN_MAIL_TBL, "", 0, TEXT_2_LEN_IN_MAIL_TBL);
12983         _bind_stmt_field_data_int(hStmt, MAIL_SIZE_IDX_IN_MAIL_TBL, 4);
12984         _bind_stmt_field_data_char(hStmt, FLAGS_SEEN_FIELD_IDX_IN_MAIL_TBL, 0);
12985         _bind_stmt_field_data_char(hStmt, FLAGS_DELETED_FIELD_IDX_IN_MAIL_TBL, 0);
12986         _bind_stmt_field_data_char(hStmt, FLAGS_FLAGGED_FIELD_IDX_IN_MAIL_TBL, 0);
12987         _bind_stmt_field_data_char(hStmt, FLAGS_ANSWERED_FIELD_IDX_IN_MAIL_TBL, 0);
12988         _bind_stmt_field_data_char(hStmt, FLAGS_RECENT_FIELD_IDX_IN_MAIL_TBL, 0);
12989         _bind_stmt_field_data_char(hStmt, FLAGS_DRAFT_FIELD_IDX_IN_MAIL_TBL, 0);
12990         _bind_stmt_field_data_char(hStmt, FLAGS_FORWARDED_FIELD_IDX_IN_MAIL_TBL, 0);
12991         _bind_stmt_field_data_int(hStmt, DRM_STATUS_IDX_IN_MAIL_TBL, 0);
12992         _bind_stmt_field_data_int(hStmt, PRIORITY_IDX_IN_MAIL_TBL, 0);
12993         _bind_stmt_field_data_int(hStmt, SAVE_STATUS_IDX_IN_MAIL_TBL, 0);
12994         _bind_stmt_field_data_int(hStmt, LOCK_STATUS_IDX_IN_MAIL_TBL, 0);
12995         _bind_stmt_field_data_int(hStmt, REPORT_STATUS_IDX_IN_MAIL_TBL, 0);
12996         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12997         _bind_stmt_field_data_int(hStmt, INLINE_CONTENT_COUNT_IDX_IN_MAIL_TBL, 0);
12998         _bind_stmt_field_data_int(hStmt, ATTACHMENT_COUNT_IDX_IN_MAIL_TBL, 0);
12999         _bind_stmt_field_data_int(hStmt, THREAD_ID_IDX_IN_MAIL_TBL, 0);
13000         _bind_stmt_field_data_int(hStmt, THREAD_ITEM_COUNT_IDX_IN_MAIL_TBL, 0);
13001         _bind_stmt_field_data_string(hStmt, PREVIEW_TEXT_IDX_IN_MAIL_TBL, "preview body", 1, PREVIEWBODY_LEN_IN_MAIL_TBL);
13002         _bind_stmt_field_data_int(hStmt, MEETING_REQUEST_STATUS_IDX_IN_MAIL_TBL, 0);
13003         _bind_stmt_field_data_int(hStmt, MESSAGE_CLASS_IDX_IN_MAIL_TBL, 0);
13004         _bind_stmt_field_data_int(hStmt, DIGEST_TYPE_IDX_IN_MAIL_TBL, 0);
13005         _bind_stmt_field_data_int(hStmt, SMIME_TYPE_IDX_IN_MAIL_TBL, 0);
13006         _bind_stmt_field_data_int(hStmt, SCHEDULED_SENDING_TIME_IDX_IN_MAIL_TBL, 0);
13007         _bind_stmt_field_data_int(hStmt, REMAINING_RESEND_TIMES_IDX_IN_MAIL_TBL, 0);
13008         _bind_stmt_field_data_int(hStmt, TAG_ID_IDX_IN_MAIL_TBL, 0);
13009         _bind_stmt_field_data_int(hStmt, REPLIED_TIME_IDX_IN_MAIL_TBL, 0);
13010         _bind_stmt_field_data_int(hStmt, FORWARDED_TIME_IDX_IN_MAIL_TBL, 0);
13011         _bind_stmt_field_data_string(hStmt, DEFAULT_CHARSET_IDX_IN_MAIL_TBL, "UTF-8", 0, TEXT_2_LEN_IN_MAIL_TBL);
13012         _bind_stmt_field_data_int(hStmt, EAS_DATA_LENGTH_IDX_IN_MAIL_TBL, 0);
13013         _bind_stmt_field_data_blob(hStmt, EAS_DATA_IDX_IN_MAIL_TBL, NULL, 0);
13014
13015         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13016         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
13017                         ("sqlite3_step fail:%d", rc));
13018         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13019                         ("sqlite3_step fail:%d", rc));
13020         ret = true;
13021
13022 FINISH_OFF:
13023         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13024         if (hStmt != NULL) {
13025                 rc = sqlite3_finalize(hStmt);
13026                 if (rc != SQLITE_OK) {
13027                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13028                         error = EMAIL_ERROR_DB_FAILURE;
13029                 }
13030         }
13031
13032         if (err_code != NULL)
13033                 *err_code = error;
13034
13035         EM_DEBUG_FUNC_END("ret [%d]", ret);
13036         return ret;
13037 }
13038
13039
13040 INTERNAL_FUNC int emstorage_get_max_mail_count()
13041 {
13042         return EMAIL_MAIL_MAX_COUNT;
13043 }
13044
13045 #define STRIPPED_SUBJECT_BUFFER_SIZE 4086
13046
13047 INTERNAL_FUNC int emstorage_get_thread_id_of_thread_mails(char *multi_user_name,
13048                 emstorage_mail_tbl_t *mail_tbl,
13049                 int *thread_id,
13050                 int *result_latest_mail_id_in_thread,
13051                 int *thread_item_count)
13052 {
13053         EM_DEBUG_FUNC_BEGIN("mail_tbl [%p], thread_id [%p], "
13054                         "result_latest_mail_id_in_thread [%p], thread_item_count [%p]",
13055                         mail_tbl, thread_id, result_latest_mail_id_in_thread, thread_item_count);
13056         EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
13057
13058         int rc = 0, query_size = 0, query_size_account = 0;
13059         int i = 0;
13060         int search_thread = false;
13061         int account_id = 0;
13062         int err_code = EMAIL_ERROR_NONE;
13063         int count = 0, result_thread_id = -1, latest_mail_id_in_thread = -1;
13064         time_t latest_date_time = 0;
13065         char *subject = NULL;
13066         char *p_subject = NULL;
13067         char *sql_query_string = NULL, *sql_account = NULL;
13068         int col_index = 4;
13069         int temp_thread_id = -1;
13070         char *sql_format = "SELECT mail_id, thread_id, date_time, subject "
13071                 "FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
13072         char *sql_format_account = " AND account_id = %d ";
13073         char *sql_format_order_by = " ORDER BY thread_id, date_time DESC ";
13074         char **result = NULL;
13075         char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
13076         char stripped_subject2[STRIPPED_SUBJECT_BUFFER_SIZE];
13077
13078         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13079
13080         EM_DEBUG_LOG("subject: [%p], mail_id: [%d]", subject, mail_tbl->mail_id);
13081
13082         EM_IF_NULL_RETURN_VALUE(mail_tbl, EMAIL_ERROR_INVALID_PARAM);
13083         EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
13084         EM_IF_NULL_RETURN_VALUE(result_latest_mail_id_in_thread, EMAIL_ERROR_INVALID_PARAM);
13085         EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
13086
13087         account_id   = mail_tbl->account_id;
13088         subject      = mail_tbl->subject;
13089
13090         EM_DEBUG_LOG_SEC("subject: [%s]", subject);
13091
13092         if (EM_SAFE_STRLEN(subject) == 0 && mail_tbl->mail_id != 0) {
13093                 result_thread_id = mail_tbl->mail_id;
13094                 count = 1;
13095                 goto FINISH_OFF;
13096         }
13097
13098         if (em_find_pos_stripped_subject_for_thread_view(subject,
13099                                 stripped_subject,
13100                                 STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE)      {
13101                 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
13102                 err_code =  EMAIL_ERROR_UNKNOWN;
13103                 result_thread_id = -1;
13104                 goto FINISH_OFF;
13105         }
13106
13107         EM_DEBUG_LOG_SEC("stripped_subject: [%s]", stripped_subject);
13108
13109         if (EM_SAFE_STRLEN(stripped_subject) == 0) {
13110                 result_thread_id = -1;
13111                 goto FINISH_OFF;
13112         }
13113
13114         EM_DEBUG_LOG_SEC("em_find_pos_stripped_subject_for_thread_view returns[len = %zu] = %s",
13115                         EM_SAFE_STRLEN(stripped_subject), stripped_subject);
13116
13117         if (account_id > 0) {
13118                 query_size_account = 10 + EM_SAFE_STRLEN(sql_format_account);
13119                 sql_account = malloc(query_size_account);
13120                 if (sql_account == NULL) {
13121                         EM_DEBUG_EXCEPTION("malloc for sql_account  is failed %d", query_size_account);
13122                         err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
13123                         goto FINISH_OFF;
13124                 }
13125                 snprintf(sql_account, query_size_account, sql_format_account, account_id);
13126         }
13127
13128         /* prevent 34362 */
13129         query_size = strlen(sql_format) + strlen(stripped_subject)*2 + 50 + query_size_account + strlen(sql_format_order_by); /*  + query_size_mailbox; */
13130         sql_query_string = malloc(query_size);
13131
13132         if (sql_query_string == NULL) {
13133                 EM_DEBUG_EXCEPTION("malloc for sql  is failed %d", query_size);
13134                 err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
13135                 goto FINISH_OFF;
13136         }
13137
13138         sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mail_tbl->mailbox_id);
13139
13140         if (account_id > 0)
13141                 EM_SAFE_STRNCAT(sql_query_string, sql_account, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13142
13143         EM_SAFE_STRNCAT(sql_query_string, sql_format_order_by, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13144         EM_SAFE_STRNCAT(sql_query_string, ";", query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13145
13146         EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
13147
13148         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL),
13149                         rc);
13150
13151         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13152                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13153
13154         EM_DEBUG_LOG("Result rows count : %d", count);
13155
13156         if (count == 0)
13157                 result_thread_id = -1;
13158         else {
13159                 for (i = 0; i < count; i++) {
13160                         EM_SAFE_FREE(p_subject);
13161
13162                         _get_table_field_data_int(result, &latest_mail_id_in_thread, col_index++);
13163                         _get_table_field_data_int(result, &result_thread_id, col_index++);
13164                         _get_table_field_data_time_t(result, &latest_date_time, col_index++);
13165                         _get_table_field_data_string(result, &p_subject, 0, col_index++);
13166
13167                         if (temp_thread_id == result_thread_id)
13168                                 continue;
13169
13170                         temp_thread_id = result_thread_id;
13171
13172                         if (em_find_pos_stripped_subject_for_thread_view(p_subject,
13173                                                 stripped_subject2,
13174                                                 STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE)      {
13175                                 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view is failed");
13176                                 err_code = EMAIL_ERROR_UNKNOWN;
13177                                 result_thread_id = -1;
13178                                 goto FINISH_OFF;
13179                         }
13180
13181                         if (g_strcmp0(stripped_subject2, stripped_subject) == 0) {
13182                                 if (latest_date_time < mail_tbl->date_time)
13183                                         *result_latest_mail_id_in_thread = latest_mail_id_in_thread;
13184                                 else
13185                                         *result_latest_mail_id_in_thread = mail_tbl->mail_id;
13186
13187                                 search_thread = true;
13188                         }
13189
13190                         if (search_thread) {
13191                                 EM_DEBUG_LOG("latest_mail_id_in_thread [%d], mail_id [%d]",
13192                                                 latest_mail_id_in_thread, mail_tbl->mail_id);
13193                                 break;
13194                         } else {
13195                                 result_thread_id = -1;
13196                                 count = 0;
13197                         }
13198                 }
13199
13200         }
13201
13202 FINISH_OFF:
13203
13204         *thread_id = result_thread_id;
13205         *thread_item_count = count;
13206
13207         EM_DEBUG_LOG("Result thread id : %d", *thread_id);
13208         EM_DEBUG_LOG("Result count : %d", *thread_item_count);
13209         EM_DEBUG_LOG("err_code : %d", err_code);
13210
13211         EM_SAFE_FREE(sql_account);
13212         EM_SAFE_FREE(sql_query_string);
13213         EM_SAFE_FREE(p_subject);
13214
13215         sqlite3_free_table(result);
13216
13217         EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
13218
13219         return err_code;
13220 }
13221
13222 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)
13223 {
13224         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);
13225         EM_PROFILE_BEGIN(profile_emstorage_get_thread_id_of_thread_mails);
13226
13227         int rc = 0;
13228         int query_size = 0;
13229         int query_size_account = 0;
13230         int err_code = EMAIL_ERROR_NONE;
13231         int count = 0;
13232         int result_thread_id = -1;
13233         char *sql_query_string = NULL;
13234         char *sql_account = NULL;
13235         char *sql_format = "SELECT thread_id FROM mail_tbl WHERE subject like \'%%%q\' AND mailbox_id = %d";
13236         char *sql_format_account = " AND account_id = %d ";
13237         char *sql_format_order_by = " ORDER BY date_time DESC ";
13238         char **result = NULL;
13239         char stripped_subject[STRIPPED_SUBJECT_BUFFER_SIZE];
13240         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13241
13242         EM_IF_NULL_RETURN_VALUE(mail_subject, EMAIL_ERROR_INVALID_PARAM);
13243         EM_IF_NULL_RETURN_VALUE(thread_id, EMAIL_ERROR_INVALID_PARAM);
13244         EM_IF_NULL_RETURN_VALUE(thread_item_count, EMAIL_ERROR_INVALID_PARAM);
13245
13246         EM_DEBUG_LOG_SEC("subject: [%s]", mail_subject);
13247
13248         if (em_find_pos_stripped_subject_for_thread_view(mail_subject, stripped_subject, STRIPPED_SUBJECT_BUFFER_SIZE) != EMAIL_ERROR_NONE)     {
13249                 EM_DEBUG_EXCEPTION("em_find_pos_stripped_subject_for_thread_view  is failed");
13250                 err_code =  EMAIL_ERROR_UNKNOWN;
13251                 result_thread_id = -1;
13252                 goto FINISH_OFF;
13253         }
13254
13255         if (EM_SAFE_STRLEN(stripped_subject) == 0) {
13256                 result_thread_id = -1;
13257                 goto FINISH_OFF;
13258         }
13259
13260         EM_DEBUG_LOG("em_find_pos_stripped_subject_for_thread_view returns[len = %zu] = %s", EM_SAFE_STRLEN(stripped_subject), stripped_subject);
13261
13262         if (account_id > 0) {
13263                 query_size_account = 10 + EM_SAFE_STRLEN(sql_format_account);
13264                 sql_account = malloc(query_size_account);
13265                 if (sql_account == NULL) {
13266                         EM_DEBUG_EXCEPTION("malloc for sql_account  is failed %d", query_size_account);
13267                         err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
13268                         goto FINISH_OFF;
13269                 }
13270                 snprintf(sql_account, query_size_account, sql_format_account, account_id);
13271         }
13272
13273         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; */
13274         sql_query_string = malloc(query_size);
13275
13276         if (sql_query_string == NULL) {
13277                 EM_DEBUG_EXCEPTION("malloc for sql  is failed %d", query_size);
13278                 err_code =  EMAIL_ERROR_OUT_OF_MEMORY;
13279                 goto FINISH_OFF;
13280         }
13281
13282         sqlite3_snprintf(query_size, sql_query_string, sql_format, stripped_subject, mailbox_id);
13283
13284         if (account_id > 0)
13285                 EM_SAFE_STRNCAT(sql_query_string, sql_account, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13286         EM_SAFE_STRNCAT(sql_query_string, sql_format_order_by, query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13287         EM_SAFE_STRNCAT(sql_query_string, ";", query_size - EM_SAFE_STRLEN(sql_query_string) - 1);
13288
13289         EM_DEBUG_LOG_SEC("Query : %s", sql_query_string);
13290
13291         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
13292
13293         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err_code = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13294                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13295
13296         EM_DEBUG_LOG("Result rows count : %d", count);
13297
13298         if (count == 0)
13299                 result_thread_id = -1;
13300         else
13301                 _get_table_field_data_int(result, &result_thread_id, 1);
13302
13303 FINISH_OFF:
13304         *thread_id = result_thread_id;
13305         *thread_item_count = count;
13306
13307         EM_DEBUG_LOG("Result thread id : %d", *thread_id);
13308         EM_DEBUG_LOG("Result count : %d", *thread_item_count);
13309         EM_DEBUG_LOG("err_code : %d", err_code);
13310
13311         EM_SAFE_FREE(sql_account);
13312         EM_SAFE_FREE(sql_query_string);
13313
13314         sqlite3_free_table(result);
13315
13316         EM_PROFILE_END(profile_emstorage_get_thread_id_of_thread_mails);
13317
13318         return err_code;
13319 }
13320
13321 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)
13322 {
13323         EM_DEBUG_FUNC_BEGIN();
13324
13325         int count = 0, ret = false;
13326         int error = EMAIL_ERROR_NONE;
13327         emstorage_mail_tbl_t *p_data_tbl = NULL;
13328         char conditional_clause[QUERY_SIZE] = {0, };
13329
13330         EM_IF_NULL_RETURN_VALUE(mail_tbl, false);
13331
13332         SNPRINTF(conditional_clause, QUERY_SIZE, "WHERE thread_id = %d AND thread_item_count > 0", thread_id);
13333         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
13334
13335         if (!emstorage_query_mail_tbl(multi_user_name, conditional_clause, transaction, &p_data_tbl, &count, &error)) {
13336                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl failed [%d]", error);
13337                 goto FINISH_OFF;
13338         }
13339
13340         if (p_data_tbl)
13341                 EM_DEBUG_LOG("thread_id : %d, thread_item_count : %d", p_data_tbl[0].thread_id, p_data_tbl[0].thread_item_count);
13342
13343         ret = true;
13344
13345 FINISH_OFF:
13346         if (ret == true)
13347                 *mail_tbl = p_data_tbl;
13348         else if (p_data_tbl != NULL)
13349                 emstorage_free_mail(&p_data_tbl, 1, NULL);
13350
13351         if (err_code != NULL)
13352                 *err_code = error;
13353
13354         EM_DEBUG_FUNC_END("ret [%d]", ret);
13355         return ret;
13356 }
13357
13358 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)
13359 {
13360         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]"
13361                         , account_id , mailbox_id , search_type , search_value , sorting , sender_list, sender_count, err_code);
13362
13363         if ((!sender_list) || (!sender_count)) {
13364                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
13365                 if (err_code != NULL)
13366                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13367                 return false;
13368         }
13369
13370         int rc = -1, ret = false;
13371         int error = EMAIL_ERROR_NONE;
13372         int count = 0;
13373         int i, col_index = 0;
13374         int read_count = 0;
13375         email_sender_list_t *p_sender_list = NULL;
13376         char sql_query_string[QUERY_SIZE] = {0, };
13377         char **result = NULL;
13378         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13379
13380         SNPRINTF(sql_query_string, sizeof(sql_query_string),
13381                         "SELECT email_address_sender, alias_sender, COUNT(email_address_sender), SUM(flags_seen_field = 1) "
13382                         "FROM mail_tbl ");
13383
13384         /*  mailbox_id */
13385         if (mailbox_id)
13386                 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);
13387         else    /*  NULL  means all mailbox_name. but except for trash(3), spambox(5), all emails(for GMail, 7) */
13388                 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) ");
13389
13390         /*  account id */
13391         /*  '0' (ALL_ACCOUNT) means all account */
13392         if (account_id > ALL_ACCOUNT)
13393                 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);
13394
13395         if (search_value) {
13396                 switch (search_type) {
13397                 case EMAIL_SEARCH_FILTER_SUBJECT:
13398                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13399                                         " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\')) ", search_value);
13400                         break;
13401                 case EMAIL_SEARCH_FILTER_SENDER:
13402                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13403                                         " AND  ((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13404                                         ") ", search_value);
13405                         break;
13406                 case EMAIL_SEARCH_FILTER_RECIPIENT:
13407                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13408                                         " AND ((UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13409                                         "       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13410                                         "       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13411                                         ") ", search_value, search_value, search_value);
13412                         break;
13413                 case EMAIL_SEARCH_FILTER_ALL:
13414                         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), QUERY_SIZE-(EM_SAFE_STRLEN(sql_query_string)+1),
13415                                         " AND (UPPER(subject) LIKE UPPER(\'%%%%%s%%%%\') "
13416                                         "       OR (((UPPER(full_address_from) LIKE UPPER(\'%%%%%s%%%%\')) "
13417                                         "                       OR (UPPER(full_address_to) LIKE UPPER(\'%%%%%s%%%%\')) "
13418                                         "                       OR (UPPER(full_address_cc) LIKE UPPER(\'%%%%%s%%%%\')) "
13419                                         "                       OR (UPPER(full_address_bcc) LIKE UPPER(\'%%%%%s%%%%\')) "
13420                                         "               ) "
13421                                         "       )"
13422                                         ")", search_value, search_value, search_value, search_value, search_value);
13423                         break;
13424                 }
13425         }
13426
13427
13428         /*  sorting option is not available now. The order of sender list is ascending order by display name */
13429         SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
13430                         "GROUP BY email_address_sender "
13431                         "ORDER BY UPPER(alias_sender) ");
13432
13433         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
13434
13435         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
13436         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13437                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13438
13439         EM_DEBUG_LOG("Count of Sender [%d]", count);
13440
13441         if (!(p_sender_list = (email_sender_list_t*)em_malloc(sizeof(email_sender_list_t) * count))) {
13442                 EM_DEBUG_EXCEPTION("em_mallocfailed...");
13443                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13444                 goto FINISH_OFF;
13445         }
13446
13447         col_index = 4;
13448
13449         EM_DEBUG_LOG(">>>> DATA ASSIGN START >>");
13450         for (i = 0; i < count; i++) {
13451                 _get_table_field_data_string(result, &(p_sender_list[i].address), 1, col_index++);
13452                 _get_table_field_data_string(result, &(p_sender_list[i].display_name), 1, col_index++);
13453                 _get_table_field_data_int(result, &(p_sender_list[i].total_count), col_index++);
13454                 _get_table_field_data_int(result, &(read_count), col_index++);
13455                 p_sender_list[i].unread_count = p_sender_list[i].total_count - read_count;              /*  unread count = total - read          */
13456         }
13457         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d] >>", count);
13458
13459         sqlite3_free_table(result);
13460         result = NULL;
13461
13462         ret = true;
13463
13464 FINISH_OFF:
13465         if (ret == true) {
13466                 *sender_list = p_sender_list;
13467                 *sender_count = count;
13468                 EM_DEBUG_LOG(">>>> COUNT : %d >>", count);
13469         }
13470
13471
13472         if (err_code != NULL)
13473                 *err_code = error;
13474
13475         EM_DEBUG_FUNC_END("ret [%d]", ret);
13476         return ret;
13477 }
13478
13479 INTERNAL_FUNC int emstorage_free_sender_list(email_sender_list_t **sender_list, int count)
13480 {
13481         EM_DEBUG_FUNC_BEGIN("sender_list[%p], count[%d]", sender_list, count);
13482
13483         int err = EMAIL_ERROR_NONE;
13484
13485         if (count > 0) {
13486                 if (!sender_list || !*sender_list) {
13487                         EM_DEBUG_EXCEPTION("sender_list[%p], count[%d]", sender_list, count);
13488                         err = EMAIL_ERROR_INVALID_PARAM;
13489                         return err;
13490                 }
13491
13492                 email_sender_list_t* p = *sender_list;
13493                 int i = 0;
13494
13495                 for (; i < count; i++) {
13496                         EM_SAFE_FREE(p[i].address);
13497                         EM_SAFE_FREE(p[i].display_name);
13498                 }
13499
13500                 EM_SAFE_FREE(p);
13501                 *sender_list = NULL;
13502         }
13503
13504         return err;
13505 }
13506
13507
13508 INTERNAL_FUNC int emstorage_free_address_info_list(email_address_info_list_t **address_info_list)
13509 {
13510         EM_DEBUG_FUNC_BEGIN("address_info_list[%p]", address_info_list);
13511
13512         int err = EMAIL_ERROR_NONE;
13513         email_address_info_t *p_address_info = NULL;
13514         GList *list = NULL;
13515         GList *node = NULL;
13516         int i = 0;
13517
13518         if (!address_info_list || !*address_info_list) {
13519                 EM_DEBUG_EXCEPTION("address_info_list[%p]", address_info_list);
13520                 err = EMAIL_ERROR_INVALID_PARAM;
13521                 return err;
13522         }
13523
13524         /*  delete GLists */
13525         for (i = EMAIL_ADDRESS_TYPE_FROM; i <= EMAIL_ADDRESS_TYPE_BCC; i++) {
13526                 switch (i) {
13527                 case EMAIL_ADDRESS_TYPE_FROM:
13528                         list = (*address_info_list)->from;
13529                         break;
13530                 case EMAIL_ADDRESS_TYPE_TO:
13531                         list = (*address_info_list)->to;
13532                         break;
13533                 case EMAIL_ADDRESS_TYPE_CC:
13534                         list = (*address_info_list)->cc;
13535                         break;
13536                 case EMAIL_ADDRESS_TYPE_BCC:
13537                         list = (*address_info_list)->bcc;
13538                         break;
13539                 }
13540
13541                 /*  delete dynamic-allocated memory for each item */
13542                 node = g_list_first(list);
13543                 while (node != NULL) {
13544                         p_address_info = (email_address_info_t*)node->data;
13545                         EM_SAFE_FREE(p_address_info->address);
13546                         EM_SAFE_FREE(p_address_info->display_name);
13547                         EM_SAFE_FREE(node->data);
13548
13549                         node = g_list_next(node);
13550                 }
13551                 g_list_free(list);
13552         }
13553
13554         EM_SAFE_FREE(*address_info_list);
13555         *address_info_list = NULL;
13556
13557         EM_DEBUG_FUNC_END("err [%d]", err);
13558         return err;
13559 }
13560 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
13561
13562 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)
13563 {
13564         EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
13565
13566         if (!local_activity || !activity_id) {
13567                 EM_DEBUG_EXCEPTION("local_activity[%p], transaction[%d], activity_id[%p], err_code[%p]", local_activity, transaction, activity_id, err_code);
13568                 if (err_code != NULL)
13569                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13570                 return false;
13571         }
13572
13573         int rc = -1;
13574         int ret = false;
13575         int error = EMAIL_ERROR_NONE;
13576         int i = 0;
13577
13578         char sql_query_string[QUERY_SIZE] = {0, };
13579         DB_STMT hStmt = NULL;
13580         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13581
13582         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
13583         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13584         SNPRINTF(sql_query_string, sizeof(sql_query_string),
13585                         "INSERT INTO mail_partial_body_activity_tbl VALUES "
13586                         "("
13587                         "? "  /* Account ID */
13588                         ",?"  /* Local Mail ID */
13589                         ",?"  /* Server mail ID */
13590                         ",?"  /* Activity ID */
13591                         ",?"  /* Activity type*/
13592                         ",?"  /* Mailbox ID*/
13593                         ",?"  /* Mailbox name*/
13594                         ",?"  /* Multi User Name */
13595                         ") ");
13596
13597         char *sql = "SELECT max(rowid) FROM mail_partial_body_activity_tbl;";
13598         char **result = NULL;
13599
13600
13601         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13602
13603         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13604                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13605
13606         if (NULL == result[1]) rc = 1;
13607         else rc = atoi(result[1])+1;
13608         sqlite3_free_table(result);
13609         result = NULL;
13610
13611         *activity_id = local_activity->activity_id = rc;
13612
13613         EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [ %d ], MAIL ID [ %d ], ACTIVITY TYPE [ %d ], SERVER MAIL ID [ %lu ]", \
13614                         local_activity->activity_id, local_activity->mail_id, local_activity->activity_type, local_activity->server_mail_id);
13615
13616         if (local_activity->mailbox_id)
13617                 EM_DEBUG_LOG(" MAILBOX ID [ %d ]", local_activity->mailbox_id);
13618
13619
13620         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13621         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13622                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13623
13624
13625         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
13626         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
13627         _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
13628         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
13629         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
13630         _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
13631         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->mailbox_name, 0, 3999);
13632         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
13633
13634
13635         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13636
13637         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
13638                         ("sqlite3_step fail:%d", rc));
13639         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13640                         ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
13641
13642         ret = true;
13643
13644 FINISH_OFF:
13645         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
13646         if (hStmt != NULL) {
13647                 rc = sqlite3_finalize(hStmt);
13648                 hStmt = NULL;
13649                 if (rc != SQLITE_OK) {
13650                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13651                         error = EMAIL_ERROR_DB_FAILURE;
13652                 }
13653         }
13654
13655         if (err_code != NULL)
13656                 *err_code = error;
13657
13658         EM_DEBUG_FUNC_END("ret [%d]", ret);
13659         return ret;
13660 }
13661
13662 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)
13663 {
13664         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13665
13666         if (account_id < FIRST_ACCOUNT_ID || NULL == mailbox_list || *mailbox_list == NULL || NULL == count) {
13667                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
13668                 if (err_code != NULL)
13669                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13670                 return false;
13671         }
13672
13673         int ret = false;
13674         int error = EMAIL_ERROR_NONE;
13675         char **result;
13676         int i = 0, rc = -1;
13677         int *mbox_list = NULL;
13678         DB_STMT hStmt = NULL;
13679         char sql_query_string[QUERY_SIZE] = {0, };
13680
13681         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13682         EMSTORAGE_START_READ_TRANSACTION(transaction);
13683
13684         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13685         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);
13686
13687
13688         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13689         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13690                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13691
13692         *count = atoi(result[1]);
13693         sqlite3_free_table(result);
13694
13695         if (*count <= 0) {
13696                 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
13697                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13698                 ret = true;
13699                 goto FINISH_OFF;
13700         }
13701         EM_DEBUG_LOG("Mailbox count = %d", *count);
13702
13703         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13704
13705         /* 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); */
13706         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);
13707
13708         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
13709
13710
13711         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13712
13713
13714         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13715                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13716
13717
13718         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13719         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13720                         ("sqlite3_step fail:%d", rc));
13721
13722         mbox_list = (int *)em_malloc(sizeof(int) * (*count));
13723         if (NULL == mbox_list) {
13724                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
13725                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13726                 goto FINISH_OFF;
13727         }
13728
13729         memset(mbox_list, 0x00, sizeof(int) * (*count));
13730
13731         for (i = 0; i < (*count); i++) {
13732                 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
13733                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13734                 /* EM_DEBUG_LOG("In emstorage_get_pdb_mailbox_list() loop, After sqlite3_step(), , i = %d, rc = %d.", i,  rc); */
13735                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13736                                 ("sqlite3_step fail:%d", rc));
13737                 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
13738         }
13739
13740         ret = true;
13741
13742 FINISH_OFF:
13743         if (ret == true)
13744                 *mailbox_list = mbox_list;
13745         else
13746                 EM_SAFE_FREE(mbox_list);
13747
13748         if (hStmt != NULL) {
13749                 rc = sqlite3_finalize(hStmt);
13750                 hStmt = NULL;
13751                 if (rc != SQLITE_OK) {
13752                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13753                         error = EMAIL_ERROR_DB_FAILURE;
13754                 }
13755         }
13756
13757         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13758         if (err_code != NULL)
13759                 *err_code = error;
13760         EM_DEBUG_FUNC_END("ret [%d]", ret);
13761         return ret;
13762 }
13763
13764 INTERNAL_FUNC int emstorage_get_pbd_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
13765 {
13766         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13767
13768         if (NULL == account_list || NULL == count) {
13769                 EM_DEBUG_EXCEPTION("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
13770                 if (err_code != NULL)
13771                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13772                 return false;
13773         }
13774
13775         int ret = false;
13776         int error = EMAIL_ERROR_NONE;
13777         char *sql = "SELECT count(distinct account_id) FROM mail_partial_body_activity_tbl";
13778         char **result;
13779         int i = 0, rc = -1;
13780         int *result_account_list = NULL;
13781         DB_STMT hStmt = NULL;
13782         char sql_query_string[QUERY_SIZE] = {0, };
13783
13784         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13785
13786         EMSTORAGE_START_READ_TRANSACTION(transaction);
13787
13788         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
13789         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13790                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
13791
13792         *count = atoi(result[1]);
13793         sqlite3_free_table(result);
13794
13795         if (*count <= 0) {
13796                 EM_DEBUG_EXCEPTION("no account found...");
13797                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
13798                 ret = true;
13799                 goto FINISH_OFF;
13800         }
13801
13802         EM_DEBUG_LOG("Account count [%d]", *count);
13803
13804         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13805
13806         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_partial_body_activity_tbl");
13807
13808         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13809
13810
13811         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13812
13813         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
13814         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13815                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13816
13817
13818         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13819         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13820                         ("sqlite3_step fail:%d", rc));
13821
13822         if (NULL == (result_account_list = (int *)em_malloc(sizeof(int) * (*count)))) {
13823                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
13824                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13825                 goto FINISH_OFF;
13826         }
13827
13828         memset(result_account_list, 0x00, sizeof(int) * (*count));
13829
13830         for (i = 0; i < (*count); i++) {
13831                 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
13832
13833                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13834                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13835                                 ("sqlite3_step fail:%d", rc));
13836                 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
13837         }
13838
13839         ret = true;
13840
13841 FINISH_OFF:
13842         if (ret == true)
13843                 *account_list = result_account_list;
13844         else
13845                 EM_SAFE_FREE(result_account_list);
13846
13847         if (hStmt != NULL) {
13848                 rc = sqlite3_finalize(hStmt);
13849                 hStmt = NULL;
13850                 if (rc != SQLITE_OK) {
13851                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13852                         error = EMAIL_ERROR_DB_FAILURE;
13853                 }
13854         }
13855
13856         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13857         if (err_code != NULL)
13858                 *err_code = error;
13859         EM_DEBUG_FUNC_END("ret [%d]", ret);
13860         return ret;
13861 }
13862
13863 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)
13864 {
13865         EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
13866
13867         if (account_id < FIRST_ACCOUNT_ID || NULL == event_start || 0 == input_mailbox_id || NULL == count) {
13868                 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);
13869
13870                 if (err_code != NULL)
13871                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13872                 return false;
13873         }
13874
13875         int rc = -1;
13876         int ret = false;
13877         char **result;
13878         int error = EMAIL_ERROR_NONE;
13879         int i = 0;
13880         DB_STMT hStmt = NULL;
13881         email_event_partial_body_thd* event_list = NULL;
13882         char sql_query_string[QUERY_SIZE] = {0, };
13883
13884         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
13885
13886         EMSTORAGE_START_READ_TRANSACTION(transaction);
13887
13888         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13889         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);
13890
13891
13892         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
13893         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
13894                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13895
13896         *count = atoi(result[1]);
13897         sqlite3_free_table(result);
13898
13899         EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
13900
13901         if (*count <= 0) {
13902                 EM_DEBUG_LOG("No matched activity found in mail_partial_body_activity_tbl");
13903                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
13904                 ret = true;
13905                 goto FINISH_OFF;
13906         }
13907         EM_DEBUG_LOG("Activity Count = %d", *count);
13908
13909         memset(sql_query_string, 0x00, sizeof(sql_query_string));
13910         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);
13911
13912         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
13913
13914
13915         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
13916
13917         EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
13918         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13919                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
13920
13921
13922         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13923         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13924                         ("sqlite3_step fail:%d", rc));
13925
13926         if (!(event_list = (email_event_partial_body_thd*)em_malloc(sizeof(email_event_partial_body_thd) * (*count)))) {
13927                 EM_DEBUG_EXCEPTION("Malloc failed");
13928
13929                 error = EMAIL_ERROR_OUT_OF_MEMORY;
13930                 goto FINISH_OFF;
13931         }
13932         memset(event_list, 0x00, sizeof(email_event_partial_body_thd) * (*count));
13933
13934         for (i = 0; i < (*count); i++) {
13935                 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_IDX_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13936                 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13937                 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13938                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13939                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_type), ACTIVITY_TYPE_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13940                 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13941                 _get_stmt_field_data_string(hStmt, &(event_list[i].mailbox_name), 0, MAILBOX_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13942                 _get_stmt_field_data_string(hStmt, &(event_list[i].multi_user_name), 0, MULTI_USER_NAME_IDX_IN_MAIL_PARTIAL_BODY_ACTIVITY_TBL);
13943
13944                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
13945                 /* EM_DEBUG_LOG("In emstorage_get_pbd_activity_data() loop, After sqlite3_step(), , i = %d, rc = %d.", i,  rc); */
13946                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
13947                                 ("sqlite3_step fail:%d", rc));
13948
13949                 event_list[i].event_type = 0;
13950         }
13951
13952         ret = true;
13953
13954 FINISH_OFF:
13955         if (true == ret)
13956                 *event_start = event_list;
13957         else {
13958                 for (i = 0; i < (*count); i++)
13959                         emcore_free_partial_body_thd_event(event_list, NULL);
13960                 EM_SAFE_FREE(event_list); /*prevent 54559*/
13961                 *event_start = NULL;
13962                 *count = 0;
13963         }
13964
13965         if (hStmt != NULL) {
13966                 rc = sqlite3_finalize(hStmt);
13967                 hStmt = NULL;
13968                 if (rc != SQLITE_OK) {
13969                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
13970                         error = EMAIL_ERROR_DB_FAILURE;
13971                 }
13972         }
13973
13974         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
13975         if (err_code != NULL)
13976                 *err_code = error;
13977
13978         EM_DEBUG_FUNC_END("ret [%d]", ret);
13979         return ret;
13980 }
13981
13982 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)
13983 {
13984         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);
13985
13986
13987         if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
13988                 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);
13989
13990                 if (err_code != NULL)
13991                         *err_code = EMAIL_ERROR_INVALID_PARAM;
13992                 return false;
13993         }
13994
13995         int rc = -1;
13996         int ret = false;
13997         int error = EMAIL_ERROR_NONE;
13998         char sql_query_string[QUERY_SIZE] = {0, };
13999         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14000
14001         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14002         memset(sql_query_string, 0x00, sizeof(sql_query_string));
14003
14004         if (activity_id == 0)
14005                 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);
14006         else
14007                 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);
14008
14009         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14010         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14011         if (error != EMAIL_ERROR_NONE) {
14012                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14013                 goto FINISH_OFF;
14014         }
14015
14016         /*  validate activity existence */
14017         rc = sqlite3_changes(local_db_handle);
14018         if (rc == 0) {
14019                 EM_DEBUG_EXCEPTION("No matching activity found");
14020                 error = EMAIL_ERROR_DATA_NOT_FOUND;
14021                 ret = true;
14022                 goto FINISH_OFF;
14023         }
14024
14025         ret = true;
14026
14027 FINISH_OFF:
14028         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14029
14030         if (err_code != NULL)
14031                 *err_code = error;
14032
14033         EM_DEBUG_FUNC_END("ret [%d]", ret);
14034         return ret;
14035 }
14036
14037 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)
14038 {
14039         EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
14040
14041         if (account_id < FIRST_ACCOUNT_ID || NULL == activity_count || NULL == err_code) {
14042                 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
14043                 if (err_code != NULL)
14044                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14045                 return false;
14046         }
14047         int rc = -1;
14048         int ret = false;
14049         int error = EMAIL_ERROR_NONE;
14050         char sql_query_string[QUERY_SIZE] = {0, };
14051
14052         DB_STMT hStmt = NULL;
14053
14054         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14055
14056         EMSTORAGE_START_READ_TRANSACTION(transaction);
14057         memset(sql_query_string, 0x00, sizeof(sql_query_string));
14058
14059         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);
14060
14061         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
14062
14063
14064         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14065         EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
14066         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14067                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14068
14069
14070         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14071         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14072                         ("sqlite3_step fail:%d", rc));
14073
14074         _get_stmt_field_data_int(hStmt, activity_count, 0);
14075
14076         EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
14077
14078         ret = true;
14079
14080 FINISH_OFF:
14081
14082         if (hStmt != NULL) {
14083                 rc = sqlite3_finalize(hStmt);
14084                 hStmt = NULL;
14085                 if (rc != SQLITE_OK) {
14086                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14087                         error = EMAIL_ERROR_DB_FAILURE;
14088                 }
14089         }
14090
14091         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14092
14093         if (err_code != NULL)
14094                 *err_code = error;
14095
14096         EM_DEBUG_FUNC_END("ret [%d]", ret);
14097         return ret;
14098 }
14099
14100 INTERNAL_FUNC int emstorage_get_pbd_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
14101 {
14102         EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
14103
14104         if (NULL == activity_count || NULL == err_code) {
14105                 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
14106                 if (err_code != NULL)
14107                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14108                 return false;
14109         }
14110         int rc = -1;
14111         int ret = false;
14112         int error = EMAIL_ERROR_NONE;
14113         DB_STMT hStmt = NULL;
14114         char sql_query_string[QUERY_SIZE] = {0, };
14115
14116         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14117
14118         EMSTORAGE_START_READ_TRANSACTION(transaction);
14119         memset(sql_query_string, 0x00, sizeof(sql_query_string));
14120
14121         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_partial_body_activity_tbl;");
14122
14123         EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
14124
14125         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14126         EM_DEBUG_LOG_DEV("  before sqlite3_prepare hStmt = %p", hStmt);
14127         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14128                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14129
14130         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14131         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14132                         ("sqlite3_step fail:%d", rc));
14133
14134         _get_stmt_field_data_int(hStmt, activity_count, 0);
14135
14136         EM_DEBUG_LOG("No. of activities in activity table [%d]", *activity_count);
14137
14138         ret = true;
14139
14140 FINISH_OFF:
14141
14142
14143         if (hStmt != NULL) {
14144                 rc = sqlite3_finalize(hStmt);
14145                 hStmt = NULL;
14146                 if (rc != SQLITE_OK) {
14147                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14148                         error = EMAIL_ERROR_DB_FAILURE;
14149                 }
14150         }
14151
14152         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
14153         if (err_code != NULL)
14154                 *err_code = error;
14155
14156         EM_DEBUG_FUNC_END("ret [%d]", ret);
14157         return ret;
14158 }
14159
14160 INTERNAL_FUNC int emstorage_delete_full_pbd_activity_data(char *multi_user_name, int account_id, int transaction, int *err_code)
14161 {
14162         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
14163         if (account_id < FIRST_ACCOUNT_ID) {
14164                 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
14165                 if (err_code != NULL)
14166                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14167                 return false;
14168         }
14169
14170         int rc = -1;
14171         int ret = false;
14172         int error = EMAIL_ERROR_NONE;
14173         char sql_query_string[QUERY_SIZE] = {0, };
14174
14175         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14176
14177         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14178         memset(sql_query_string, 0x00, sizeof(sql_query_string));
14179         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_partial_body_activity_tbl WHERE account_id = %d", account_id);
14180
14181         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14182         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14183         if (error != EMAIL_ERROR_NONE) {
14184                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14185                 goto FINISH_OFF;
14186         }
14187
14188         rc = sqlite3_changes(local_db_handle);
14189         if (rc == 0) {
14190                 EM_DEBUG_EXCEPTION("No matching activities found in mail_partial_body_activity_tbl");
14191                 error = EMAIL_ERROR_DATA_NOT_FOUND;
14192                 ret = true;
14193                 goto FINISH_OFF;
14194         }
14195
14196         ret = true;
14197
14198 FINISH_OFF:
14199
14200         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14201
14202         if (err_code != NULL)
14203                 *err_code = error;
14204
14205         EM_DEBUG_FUNC_END("ret [%d]", ret);
14206         return ret;
14207 }
14208
14209 /*Himanshu[h.gahlaut]-> Added below API to update mail_partial_body_activity_tbl
14210   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*/
14211
14212 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)
14213 {
14214         EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
14215
14216         int rc = -1, ret = false;
14217         int error = EMAIL_ERROR_NONE;
14218         char sql_query_string[QUERY_SIZE] = {0, };
14219
14220         int transaction = true;
14221
14222         if (!old_server_uid || !new_server_uid || !mbox_name) {
14223                 EM_DEBUG_EXCEPTION("Invalid parameters");
14224                 error = EMAIL_ERROR_INVALID_PARAM;
14225                 return false;
14226         }
14227
14228         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14229
14230         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14231         memset(sql_query_string, 0x00, sizeof(sql_query_string));
14232         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14233                         "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);
14234
14235         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14236         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14237         if (error != EMAIL_ERROR_NONE) {
14238                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14239                 goto FINISH_OFF;
14240         }
14241
14242         rc = sqlite3_changes(local_db_handle);
14243         if (rc == 0)
14244                 EM_DEBUG_LOG("No matching found in mail_partial_body_activity_tbl");
14245
14246         ret = true;
14247
14248 FINISH_OFF:
14249         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14250
14251         if (err_code != NULL)
14252                 *err_code = error;
14253
14254         EM_DEBUG_FUNC_END("ret [%d]", ret);
14255         return ret;
14256 }
14257
14258
14259 INTERNAL_FUNC int emstorage_create_file(char *data_string, size_t file_size, char *dst_file_name, int *err_code)
14260 {
14261         EM_DEBUG_FUNC_BEGIN_SEC("file_size[%d] , dst_file_name[%s], err_code[%p]", file_size, dst_file_name, err_code);
14262
14263         int ret = false;
14264         int error = EMAIL_ERROR_NONE;
14265         FILE* fp_dst = NULL;
14266         char errno_buf[ERRNO_BUF_SIZE] = {0};
14267
14268         if (!data_string || !dst_file_name) {
14269                 EM_DEBUG_LOG("data_string[%p], dst_file_name[%p]", data_string, dst_file_name);
14270                 error = EMAIL_ERROR_INVALID_PARAM;
14271                 goto FINISH_OFF;
14272         }
14273
14274         error = em_fopen(dst_file_name, "w", &fp_dst);
14275         if (error != EMAIL_ERROR_NONE) {
14276                 EM_DEBUG_EXCEPTION_SEC("em_fopen failed - %s: %d", dst_file_name, error);
14277                 goto FINISH_OFF;
14278         }
14279
14280         if (fwrite(data_string, 1, file_size, fp_dst) == 0) {
14281                 EM_DEBUG_EXCEPTION("fwrite failed: %s", EM_STRERROR(errno_buf));
14282                 error = EMAIL_ERROR_UNKNOWN;
14283                 goto FINISH_OFF;
14284         }
14285
14286         ret = true;
14287
14288 FINISH_OFF:
14289
14290         if (fp_dst != NULL)
14291                 fclose(fp_dst);
14292
14293         if (err_code != NULL)
14294                 *err_code = error;
14295
14296         EM_DEBUG_FUNC_END("ret [%d]", ret);
14297         return ret;
14298 }
14299
14300 #endif /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
14301
14302
14303
14304 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
14305 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)
14306 {
14307         EM_DEBUG_FUNC_BEGIN();
14308         int rc = -1;
14309         int ret = false;
14310         int error = EMAIL_ERROR_NONE;
14311         char sql_query_string[QUERY_SIZE] = {0, };
14312
14313         int transaction = true;
14314
14315         if (!old_server_uid || !new_server_uid || !mbox_name) {
14316                 EM_DEBUG_EXCEPTION("Invalid parameters");
14317                 if (err_code != NULL)
14318                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14319                 return false;
14320         }
14321
14322         EM_DEBUG_LOG_SEC("old_server_uid[%s], new_server_uid[%s], mbox_name[%s]", old_server_uid, new_server_uid, mbox_name);
14323
14324         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14325         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14326
14327
14328         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14329                         "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);
14330
14331         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
14332         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14333         if (error != EMAIL_ERROR_NONE) {
14334                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14335                 goto FINISH_OFF;
14336         }
14337
14338         rc = sqlite3_changes(local_db_handle);
14339         if (rc == 0) {
14340                 EM_DEBUG_EXCEPTION("No matching found in mail_partial_body_activity_tbl");
14341                 error = EMAIL_ERROR_DATA_NOT_FOUND;
14342                 goto FINISH_OFF;
14343         }
14344
14345         ret = true;
14346
14347 FINISH_OFF:
14348         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14349
14350         if (err_code != NULL)
14351                 *err_code = error;
14352
14353         EM_DEBUG_FUNC_END("ret [%d]", ret);
14354         return ret;
14355
14356 }
14357
14358
14359 /**
14360  * @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);
14361  * Prepare an array of mail_id and corresponding server mail id.
14362  *
14363  * @author                                      h.gahlaut@samsung.com
14364  * @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)
14365  *                                                      where 88 is the length of fixed keywords including ending null character in the QUERY to be formed
14366  * @param[out] idset                    Returns the array of mail_id and corresponding server_mail_id sorted by server_mail_ids
14367  * @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
14368  * @param[out] err_code         Returns the error code.
14369  * @remarks                                     An Example of Query to be exexuted in this API:
14370  *                                                      SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (12, 13, 56, 78);
14371  * @return This function returns true on success or false on failure.
14372  */
14373 INTERNAL_FUNC int emstorage_get_id_set_from_mail_ids(char *multi_user_name,
14374                 char *mail_ids,
14375                 email_id_set_t** idset,
14376                 int *id_set_count,
14377                 int *err_code)
14378 {
14379         EM_DEBUG_FUNC_BEGIN();
14380         EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds);
14381
14382         int error = EMAIL_ERROR_NONE;
14383         int ret = false;
14384         email_id_set_t* p_id_set = NULL;
14385         int count = 0;
14386         const int buf_size = QUERY_SIZE;
14387         char sql_query_string[QUERY_SIZE] = {0, };
14388         int space_left_in_query_buffer = buf_size;
14389         int i = 0;
14390         int rc = -1;
14391         char *server_mail_id = NULL;
14392         char **result = NULL;
14393         int col_index = 0;
14394
14395
14396         if (NULL == mail_ids || NULL == idset || NULL == id_set_count) {
14397                 EM_DEBUG_EXCEPTION("Invalid Parameters mail_ids[%p] idset[%p] id_set_count [%p]",
14398                                 mail_ids, idset, id_set_count);
14399                 if (err_code != NULL)
14400                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14401                 return false;
14402         }
14403
14404         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14405
14406         SNPRINTF(sql_query_string, space_left_in_query_buffer,
14407                         "SELECT local_uid, server_uid FROM mail_read_mail_uid_tbl WHERE local_uid in (%s) ORDER BY server_uid",
14408                         mail_ids);
14409
14410         EM_DEBUG_LOG_SEC("SQL Query formed [%s] ", sql_query_string);
14411
14412         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14413         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14414         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result);
14415                         goto FINISH_OFF; },     ("SQL(%s) sqlite3_get_table fail:%d -%s",
14416                                 sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14417
14418         EM_DEBUG_LOG(" Count of mails [%d ]", count);
14419
14420         if (count <= 0) {
14421                 EM_DEBUG_EXCEPTION("Can't find proper mail");
14422                 error = EMAIL_ERROR_DATA_NOT_FOUND;
14423                 goto FINISH_OFF;
14424         }
14425
14426         if (NULL == (p_id_set = (email_id_set_t*)em_malloc(sizeof(email_id_set_t) * count))) {
14427                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
14428                 error = EMAIL_ERROR_OUT_OF_MEMORY;
14429                 goto FINISH_OFF;
14430         }
14431
14432         col_index = 2;
14433
14434         EM_PROFILE_BEGIN(EmStorageGetIdSetFromMailIds_Loop);
14435         EM_DEBUG_LOG(">>>> DATA ASSIGN START");
14436         for (i = 0; i < count; i++) {
14437                 _get_table_field_data_int(result, &(p_id_set[i].mail_id), col_index++);
14438                 _get_table_field_data_string(result, &server_mail_id, 1, col_index++);
14439                 if (server_mail_id) {
14440                         p_id_set[i].server_mail_id = strtoul(server_mail_id, NULL, 10);
14441                         EM_SAFE_FREE(server_mail_id);
14442                 }
14443         }
14444         EM_DEBUG_LOG(">>>> DATA ASSIGN END [count : %d]", count);
14445         EM_PROFILE_END(EmStorageGetIdSetFromMailIds_Loop);
14446
14447         sqlite3_free_table(result);
14448         result = NULL;
14449
14450         ret = true;
14451
14452 FINISH_OFF:
14453
14454         if (ret == true) {
14455                 *idset = p_id_set;
14456                 *id_set_count = count;
14457                 EM_DEBUG_LOG(" idset[%p] id_set_count [%d]", *idset, *id_set_count);
14458         } else
14459                 EM_SAFE_FREE(p_id_set);
14460
14461
14462         if (err_code != NULL)
14463                 *err_code = error;
14464
14465         EM_PROFILE_END(EmStorageGetIdSetFromMailIds);
14466
14467         EM_DEBUG_FUNC_END("ret [%d]", ret);
14468         return ret;
14469 }
14470
14471
14472
14473 #endif
14474
14475 INTERNAL_FUNC int emstorage_delete_triggers_from_lucene(char *multi_user_name)
14476 {
14477         EM_DEBUG_FUNC_BEGIN();
14478         int ret = true, transaction = true;
14479         int error = EMAIL_ERROR_NONE;
14480         char sql_query_string[QUERY_SIZE] = {0, };
14481         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14482         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14483
14484         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerDelete;");
14485         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14486         if (error != EMAIL_ERROR_NONE) {
14487                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14488                 goto FINISH_OFF;
14489         }
14490
14491         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerInsert;");
14492         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14493         if (error != EMAIL_ERROR_NONE) {
14494                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14495                 goto FINISH_OFF;
14496         }
14497
14498         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DROP TRIGGER triggerUpdate;");
14499         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14500         if (error != EMAIL_ERROR_NONE) {
14501                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14502                 goto FINISH_OFF;
14503         }
14504
14505         ret = true;
14506
14507 FINISH_OFF:
14508         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14509
14510         EM_DEBUG_FUNC_END("ret [%d]", ret);
14511         return ret;
14512 }
14513
14514 INTERNAL_FUNC int emstorage_update_tag_id(char *multi_user_name, int old_filter_id, int new_filter_id, int *err_code)
14515 {
14516         EM_DEBUG_FUNC_BEGIN("new_filter_id[%d], old_filter_id[%d]", new_filter_id, old_filter_id);
14517         int ret = false;
14518         int error = EMAIL_ERROR_NONE;
14519         char sql_query_string[QUERY_SIZE] = {0, };
14520         int transaction = true;
14521
14522         if (old_filter_id < 0 || new_filter_id < 0) {
14523                 EM_DEBUG_EXCEPTION("Invalid parameters");
14524                 if (err_code != NULL)
14525                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14526                 return false;
14527         }
14528
14529         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14530
14531         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14532
14533         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14534                         "UPDATE mail_tbl SET tag_id=%d WHERE tag_id=%d ", new_filter_id, old_filter_id);
14535
14536         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14537         if (error != EMAIL_ERROR_NONE) {
14538                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14539                 goto FINISH_OFF;
14540         }
14541
14542         ret = true;
14543
14544 FINISH_OFF:
14545         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14546
14547         if (err_code != NULL)
14548                 *err_code = error;
14549
14550         EM_DEBUG_FUNC_END("ret [%d]", ret);
14551         return ret;
14552 }
14553
14554 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)
14555 {
14556         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);
14557
14558         if ((account_id < 0) || (dest_mailbox_id < 0) || (!rule)) {
14559                 EM_DEBUG_EXCEPTION("Invalid Parameter");
14560
14561                 if (err_code != NULL)
14562                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14563                 return false;
14564         }
14565
14566         int rc = -1, ret = false, error = EMAIL_ERROR_NONE;
14567         int count = 0, col_index = 0, i = 0, where_pararaph_length = 0, *mail_list = NULL;
14568         int tag_id = rule->rule_id;
14569         char **result = NULL, *where_pararaph = NULL;
14570         char sql_query_string[QUERY_SIZE] = {0, };
14571         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14572
14573         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id FROM mail_tbl ");
14574
14575         where_pararaph_length = EM_SAFE_STRLEN(rule->value) + 2 * (EM_SAFE_STRLEN(rule->value2)) + 100;
14576         where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
14577         if (where_pararaph == NULL) {
14578                 EM_DEBUG_EXCEPTION("malloc failed for where_pararaph.");
14579                 error = EMAIL_ERROR_OUT_OF_MEMORY;
14580                 goto FINISH_OFF;
14581         }
14582
14583         if (account_id != ALL_ACCOUNT)
14584                 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d AND mailbox_type NOT in (0)", account_id);
14585         else
14586                 SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_type NOT in (0)");
14587
14588         if (rule->type & EMAIL_FILTER_SUBJECT) {
14589                 if (rule->flag2 == RULE_TYPE_INCLUDES)
14590                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject like \'%%%q%%\'", rule->value);
14591                 else /*  RULE_TYPE_EXACTLY */
14592                         sqlite3_snprintf(where_pararaph_length - (EM_SAFE_STRLEN(where_pararaph) + 1), where_pararaph + EM_SAFE_STRLEN(where_pararaph), " AND subject = \'%q\'", rule->value);
14593         }
14594
14595         if (rule->type & EMAIL_FILTER_FROM) {
14596                 if (rule->flag2 == RULE_TYPE_INCLUDES)
14597                         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);
14598 #ifdef __FEATURE_COMPARE_DOMAIN__
14599                 else if (rule->flag2 == RULE_TYPE_COMPARE_DOMAIN)
14600                         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);
14601 #endif /*__FEATURE_COMPARE_DOMAIN__ */
14602                 else /*  RULE_TYPE_EXACTLY */
14603                         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);
14604         }
14605
14606         if (rule->type == EMAIL_PRIORITY_SENDER) {
14607                 if (rule->flag2 == RULE_TYPE_INCLUDES)
14608                         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);
14609                 else /*  RULE_TYPE_EXACTLY */
14610                         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);
14611
14612                 tag_id = PRIORITY_SENDER_TAG_ID;
14613         }
14614
14615         /* prevent 34361 */
14616         if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14617                 EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14618         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14619
14620         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL); */
14621         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14622         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
14623                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14624
14625         EM_DEBUG_LOG("Count of mails [%d]", count);
14626
14627         if (count) {
14628                 mail_list = malloc(sizeof(int) * count);
14629                 if (mail_list == NULL) {
14630                         EM_DEBUG_EXCEPTION("malloc failed for mail_list.");
14631                         error = EMAIL_ERROR_OUT_OF_MEMORY;
14632                         goto FINISH_OFF;
14633                 }
14634
14635                 col_index = 1;
14636
14637                 for (i = 0; i < count; i++)
14638                         _get_table_field_data_int(result, &(mail_list[i]), col_index++);
14639
14640                 memset(sql_query_string, 0x00, QUERY_SIZE);
14641                 if (reset) {
14642                         switch (rule->action_type) {
14643                         case EMAIL_FILTER_MOVE:
14644                                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = 0 ");
14645                                 break;
14646                         case EMAIL_FILTER_BLOCK:
14647                         default:
14648                                 EM_DEBUG_LOG("Not support : action_type[%d]", rule->action_type);
14649                                 ret = true;
14650                                 goto FINISH_OFF;
14651                         }
14652                 } else {
14653                         switch (rule->action_type) {
14654                         case EMAIL_FILTER_MOVE:
14655                                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET tag_id = %d ", tag_id);
14656                                 break;
14657                         case EMAIL_FILTER_BLOCK:
14658                                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_tbl SET mailbox_id = %d, mailbox_type = %d ", dest_mailbox_id, dest_mailbox_type);
14659                                 break;
14660                         default:
14661                                 EM_DEBUG_LOG("Not support");
14662                                 ret = true;
14663                                 goto FINISH_OFF;
14664                         }
14665                 }
14666                 /* prevent 34361 */
14667                 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14668                         EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14669
14670                 error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14671                 if (error != EMAIL_ERROR_NONE) {
14672                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14673                         goto FINISH_OFF;
14674                 }
14675
14676 #ifdef __FEATURE_BODY_SEARCH__
14677                 /* Updating mail_text_tbl */
14678                 if (rule->action_type == EMAIL_FILTER_BLOCK) {
14679                         memset(sql_query_string, 0x00, QUERY_SIZE);
14680                         SNPRINTF(sql_query_string, QUERY_SIZE, "UPDATE mail_text_tbl SET mailbox_id = %d ", dest_mailbox_id);
14681                         if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
14682                                 EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14683
14684                         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14685                         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14686                         if (error != EMAIL_ERROR_NONE) {
14687                                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
14688                                 goto FINISH_OFF;
14689                         }
14690                 }
14691 #endif
14692         }
14693
14694         ret = true;
14695
14696 FINISH_OFF:
14697
14698         if (ret) {
14699                 if (filtered_mail_id_list)
14700                         *filtered_mail_id_list = mail_list;
14701
14702                 if (count_of_mails)
14703                         *count_of_mails = count;
14704         } else
14705                 EM_SAFE_FREE(mail_list);
14706
14707         sqlite3_free_table(result);
14708         result = NULL;
14709
14710
14711         EM_SAFE_FREE(where_pararaph);
14712
14713         if (err_code != NULL)
14714                 *err_code = error;
14715
14716         EM_DEBUG_FUNC_END("ret [%d]", ret);
14717         return ret;
14718 }
14719
14720 #define EMAIL_SLOT_UNIT 25
14721 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)
14722 {
14723         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);
14724         int ret = false, err = EMAIL_ERROR_NONE;
14725         int where_pararaph_length = 0;
14726         char *where_pararaph = NULL;
14727         char sql_query_string[QUERY_SIZE] = {0, };
14728         int and = 0;
14729         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14730
14731         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
14732
14733         if (new_slot_size > 0)
14734                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = %d ", new_slot_size);
14735         else if (new_slot_size == 0)
14736                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", EMAIL_SLOT_UNIT);
14737         else
14738                 SNPRINTF(sql_query_string, sizeof(sql_query_string), "UPDATE mail_box_tbl SET mail_slot_size = mail_slot_size + %d ", new_slot_size * -1);
14739
14740
14741         if (mailbox_id)
14742                 where_pararaph_length = 80;
14743         else
14744                 where_pararaph_length = 50;
14745
14746         if (new_slot_size == 0)
14747                 where_pararaph_length += 70;
14748
14749         where_pararaph = malloc(sizeof(char) * where_pararaph_length);
14750         if (where_pararaph == NULL) {
14751                 EM_DEBUG_EXCEPTION("Memory allocation failed for where_pararaph");
14752                 err = EMAIL_ERROR_OUT_OF_MEMORY;
14753                 goto FINISH_OFF;
14754         }
14755         memset(where_pararaph, 0x00, where_pararaph_length);
14756
14757         if (account_id > ALL_ACCOUNT) {
14758                 and = 1;
14759                 if (mailbox_id)
14760                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE mailbox_id = %d ", mailbox_id);
14761                 else
14762                         SNPRINTF(where_pararaph, where_pararaph_length, "WHERE account_id = %d ", account_id);
14763         }
14764
14765         if (new_slot_size == 0)
14766                 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"));
14767
14768         if (strlen(sql_query_string) + EM_SAFE_STRLEN(where_pararaph) < QUERY_SIZE) /* prevent 34363 */
14769                 EM_SAFE_STRNCAT(sql_query_string, where_pararaph, QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
14770         else {
14771                 EM_DEBUG_EXCEPTION("Query buffer overflowed !!!");
14772                 err = EMAIL_ERROR_OUT_OF_MEMORY;
14773                 goto FINISH_OFF;
14774         }
14775
14776         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
14777         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
14778         if (err != EMAIL_ERROR_NONE) {
14779                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
14780                 goto FINISH_OFF;
14781         }
14782
14783         ret = true;
14784
14785 FINISH_OFF:
14786         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
14787
14788         EM_SAFE_FREE(where_pararaph);
14789
14790         if (err_code != NULL)
14791                 *err_code = err;
14792
14793         EM_DEBUG_FUNC_END("ret [%d]", ret);
14794         return ret;
14795 }
14796
14797 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)
14798 {
14799         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);
14800
14801         if (!meeting_req || meeting_req->mail_id <= 0) {
14802                 if (meeting_req)
14803                         EM_DEBUG_EXCEPTION("mail_id[%d]", meeting_req->mail_id);
14804
14805                 if (err_code != NULL)
14806                         *err_code = EMAIL_ERROR_INVALID_PARAM;
14807
14808                 return false;
14809         }
14810
14811         int rc = -1;
14812         int ret = false;
14813         int error = EMAIL_ERROR_NONE;
14814         DB_STMT hStmt = NULL;
14815         char sql_query_string[QUERY_SIZE] = {0, };
14816         int col_index = 0;
14817         time_t temp_unix_time = 0;
14818
14819         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
14820         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
14821
14822         SNPRINTF(sql_query_string, sizeof(sql_query_string),
14823                         "INSERT INTO mail_meeting_tbl VALUES "
14824                         "(?"            /*  mail_id */
14825                         ", ?"           /*  account_id */
14826                         ", ?"           /*  mailbox_id */
14827                         ", ?"           /*  meeting_response */
14828                         ", ?"           /*  start_time */
14829                         ", ?"           /*  end_time */
14830                         ", ?"           /*  location */
14831                         ", ?"           /*  global_object_id */
14832                         ", ?"           /*  offset */
14833                         ", ?"           /*  standard_name */
14834                         ", ?"           /*  standard_time_start_date */
14835                         ", ?"           /*  standard_biad */
14836                         ", ?"           /*  daylight_name */
14837                         ", ?"           /*  daylight_time_start_date */
14838                         ", ?"           /*  daylight_bias */
14839                         ")");
14840
14841         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
14842         if (rc != SQLITE_OK) {
14843                 EM_DEBUG_LOG(" before sqlite3_prepare hStmt = %p", hStmt);
14844                 EM_DEBUG_EXCEPTION("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle));
14845
14846                 error = EMAIL_ERROR_DB_FAILURE;
14847                 goto FINISH_OFF;
14848         }
14849
14850         col_index = 0;
14851         /*
14852            EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
14853            EM_DEBUG_LOG_SEC(">>>>> account_id[%d]", account_id);
14854            EM_DEBUG_LOG_SEC(">>>>> mailbox_name[%s]", mailbox_name);
14855            EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
14856            EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
14857            EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
14858            EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
14859            EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
14860            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
14861            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
14862            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
14863            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
14864            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
14865            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
14866            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
14867            */
14868         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->mail_id);
14869         _bind_stmt_field_data_int(hStmt, col_index++, account_id);
14870         _bind_stmt_field_data_int(hStmt, col_index++, input_mailbox_id);
14871         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
14872
14873         temp_unix_time = timegm(&(meeting_req->start_time));
14874         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14875         temp_unix_time = timegm(&(meeting_req->end_time));
14876         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14877
14878         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->location, 0, LOCATION_LEN_IN_MAIL_MEETING_TBL);
14879         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->global_object_id, 0, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
14880
14881         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
14882         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.standard_name, 0, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
14883         temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
14884         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14885         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
14886
14887         _bind_stmt_field_data_string(hStmt, col_index++, (char *)meeting_req->time_zone.daylight_name, 0, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
14888         temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
14889         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
14890         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
14891
14892
14893         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
14894         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
14895                         ("sqlite3_step fail:%d", rc));
14896         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
14897                         ("sqlite3_step fail:%d", rc));
14898
14899         ret = true;
14900
14901 FINISH_OFF:
14902         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
14903         if (hStmt != NULL) {
14904                 rc = sqlite3_finalize(hStmt);
14905                 if (rc != SQLITE_OK) {
14906                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
14907                         error = EMAIL_ERROR_DB_FAILURE;
14908                 }
14909         }
14910
14911         if (err_code != NULL)
14912                 *err_code = error;
14913
14914         EM_DEBUG_FUNC_END("ret [%d]", ret);
14915         return ret;
14916 }
14917
14918 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)
14919 {
14920         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);
14921
14922         int i = 0;
14923         int col_index = 0;
14924         int rc;
14925         int count = 0;
14926         int dummy = 0;
14927         int err = EMAIL_ERROR_NONE;
14928         char **result = NULL;
14929         char sql_query_string[QUERY_SIZE] = {0, };
14930         email_meeting_request_t* p_temp_meeting_req = NULL;
14931         sqlite3 *local_db_handle = NULL;
14932         time_t temp_unix_time;
14933
14934         if (conditional_clause == NULL || output_meeting_req == NULL || output_result_count == NULL) {
14935                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
14936                 err = EMAIL_ERROR_INVALID_PARAM;
14937                 goto FINISH_OFF;
14938         }
14939
14940         col_index = _field_count_of_table[CREATE_TABLE_MAIL_MEETING_TBL];
14941         EM_DEBUG_LOG("col_index [%d]", col_index);
14942
14943         local_db_handle = emstorage_get_db_connection(multi_user_name);
14944
14945         EMSTORAGE_START_READ_TRANSACTION(transaction);
14946
14947         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT * FROM mail_meeting_tbl %s", conditional_clause);
14948
14949         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
14950
14951         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
14952         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
14953                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
14954
14955         if (!count) {
14956                 EM_DEBUG_EXCEPTION("No meeting_request found...");
14957                 err = EMAIL_ERROR_DATA_NOT_FOUND;
14958                 goto FINISH_OFF;
14959         }
14960
14961         EM_DEBUG_LOG("There are [%d] meeting requests.", count);
14962         if (!(p_temp_meeting_req = (email_meeting_request_t*)em_malloc(sizeof(email_meeting_request_t) * count))) {
14963                 EM_DEBUG_EXCEPTION("malloc for emstorage_mail_tbl_t failed...");
14964                 err = EMAIL_ERROR_OUT_OF_MEMORY;
14965                 goto FINISH_OFF;
14966         }
14967
14968         EM_DEBUG_LOG(">>>> DATA ASSIGN START >> ");
14969
14970         for (i = 0; i < count; i++) {
14971                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].mail_id), col_index++);
14972                 _get_table_field_data_int(result, &dummy, col_index++); /* account_id. but why should this field exist in DB table? */
14973                 _get_table_field_data_int(result, &dummy, col_index++); /* mailbox_id */
14974                 _get_table_field_data_int(result, (int*)&(p_temp_meeting_req[i].meeting_response), col_index++);
14975                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* start time */
14976                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].start_time));
14977                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++); /* end time */
14978                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].end_time));
14979                 _get_table_field_data_string(result, &p_temp_meeting_req[i].location, 1, col_index++);
14980                 _get_table_field_data_string(result, &p_temp_meeting_req[i].global_object_id, 1, col_index++);
14981                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.offset_from_GMT), col_index++);
14982                 _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++);
14983                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14984                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.standard_time_start_date));
14985                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.standard_bias), col_index++);
14986                 _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++);
14987                 _get_table_field_data_int(result, (int*)(&temp_unix_time), col_index++);
14988                 gmtime_r(&temp_unix_time, &(p_temp_meeting_req[i].time_zone.daylight_time_start_date));
14989                 _get_table_field_data_int(result, &(p_temp_meeting_req[i].time_zone.daylight_bias), col_index++);
14990         }
14991
14992
14993 FINISH_OFF:
14994         if (result)
14995                 sqlite3_free_table(result);
14996
14997         if (err == EMAIL_ERROR_NONE) {
14998                 if (p_temp_meeting_req)
14999                         *output_meeting_req = p_temp_meeting_req;
15000                 *output_result_count = count;
15001         } else
15002                 EM_SAFE_FREE(p_temp_meeting_req);
15003
15004         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15005
15006         //      sqlite3_db_release_memory(local_db_handle);
15007
15008         EM_DEBUG_FUNC_END("err [%d]", err);
15009         return err;
15010 }
15011
15012 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)
15013 {
15014         EM_DEBUG_FUNC_BEGIN();
15015
15016         int count = 0;
15017         int ret = false;
15018         int error = EMAIL_ERROR_NONE;
15019         char conditional_clause[QUERY_SIZE] = {0, };
15020
15021         EM_IF_NULL_RETURN_VALUE(meeting_req, false);
15022
15023
15024         SNPRINTF(conditional_clause, QUERY_SIZE, " WHERE mail_id = %d", mail_id);
15025         EM_DEBUG_LOG("conditional_clause [%s]", conditional_clause);
15026
15027         if ((error = emstorage_query_meeting_request(multi_user_name, conditional_clause, meeting_req, &count, transaction)) != EMAIL_ERROR_NONE) {
15028                 EM_DEBUG_EXCEPTION("emstorage_query_meeting_request failed. [%d]", error);
15029                 goto FINISH_OFF;
15030         }
15031
15032         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->mail_id[%d]", (*meeting_req)->mail_id);
15033         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->meeting_response[%d]", (*meeting_req)->meeting_response);
15034         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->start_time[%s]", asctime(&((*meeting_req)->start_time)));
15035         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->end_time[%s]", asctime(&((*meeting_req)->end_time)));
15036         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->location[%s]", (*meeting_req)->location);
15037         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->global_object_id[%s]", (*meeting_req)->global_object_id);
15038         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.offset_from_GMT[%d]", (*meeting_req)->time_zone.offset_from_GMT);
15039         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_name[%s]", (*meeting_req)->time_zone.standard_name);
15040         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.standard_time_start_date)));
15041         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.standard_bias[%d]", (*meeting_req)->time_zone.standard_bias);
15042         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_name[%s]", (*meeting_req)->time_zone.daylight_name);
15043         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_time_start_date[%s]", asctime(&((*meeting_req)->time_zone.daylight_time_start_date)));
15044         EM_DEBUG_LOG_SEC(">>>>> (*meeting_req)->time_zone.daylight_bias[%d]", (*meeting_req)->time_zone.daylight_bias);
15045         ret = true;
15046
15047 FINISH_OFF:
15048
15049         if (err_code != NULL)
15050                 *err_code = error;
15051
15052         EM_DEBUG_FUNC_END("ret [%d]", ret);
15053         return ret;
15054 }
15055
15056 INTERNAL_FUNC int emstorage_update_meeting_request(char *multi_user_name, email_meeting_request_t* meeting_req, int transaction, int *err_code)
15057 {
15058         EM_DEBUG_FUNC_BEGIN("meeting_req[%p], transaction[%d], err_code[%p]", meeting_req, transaction, err_code);
15059
15060         int ret = false;
15061         int error = EMAIL_ERROR_NONE;
15062         int rc;
15063         DB_STMT hStmt = NULL;
15064         char sql_query_string[QUERY_SIZE] = {0, };
15065         time_t temp_unix_time = 0;
15066
15067         if (!meeting_req) {
15068                 EM_DEBUG_EXCEPTION("Invalid Parameter!");
15069                 if (err_code != NULL)
15070                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15071                 return false;
15072         }
15073
15074         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15075         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15076
15077         memset(sql_query_string, 0x00, sizeof(sql_query_string));
15078         SNPRINTF(sql_query_string, sizeof(sql_query_string),
15079                         "UPDATE mail_meeting_tbl "
15080                         "SET "
15081                         /* "  account_id = ?, "         //  not update here, this can be changed when move or copy */
15082                         /* "  mailbox_name = ?, "               //  not update here, this can be changed when move or copy */
15083                         "  meeting_response = ?, "
15084                         "  start_time = ?, "
15085                         "  end_time = ?, "
15086                         "  location = ?, "
15087                         "  global_object_id = ?, "
15088                         "  offset = ?, "
15089                         "  standard_name = ?, "
15090                         "  standard_time_start_date = ?, "
15091                         "  standard_bias = ?, "
15092                         "  daylight_name = ?, "
15093                         "  daylight_time_start_date = ?, "
15094                         "  daylight_bias = ? "
15095                         "WHERE mail_id = %d",
15096                         meeting_req->mail_id);
15097
15098         EM_DEBUG_LOG_SEC("SQL(%s)", sql_query_string);
15099
15100         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15101         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15102                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15103         /*
15104            EM_DEBUG_LOG_SEC(">>>>> meeting_req->mail_id[%d]", meeting_req->mail_id);
15105            EM_DEBUG_LOG_SEC(">>>>> meeting_req->meeting_response[%d]", meeting_req->meeting_response);
15106            EM_DEBUG_LOG_SEC(">>>>> meeting_req->start_time[%s]", asctime(&(meeting_req->start_time)));
15107            EM_DEBUG_LOG_SEC(">>>>> meeting_req->end_time[%s]", asctime(&(meeting_req->end_time)));
15108            EM_DEBUG_LOG_SEC(">>>>> meeting_req->location[%s]", meeting_req->location);
15109            EM_DEBUG_LOG_SEC(">>>>> meeting_req->global_object_id[%s]", meeting_req->global_object_id);
15110            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.offset_from_GMT[%d]", meeting_req->time_zone.offset_from_GMT);
15111            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_name[%s]", meeting_req->time_zone.standard_name);
15112            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_time_start_date[%s]", asctime(&(meeting_req->time_zone.standard_time_start_date)));
15113            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.standard_bias[%d]", meeting_req->time_zone.standard_bias);
15114            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_name[%s]", meeting_req->time_zone.daylight_name);
15115            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_time_start_date[%s]", asctime(&(meeting_req->time_zone.daylight_time_start_date)));
15116            EM_DEBUG_LOG_SEC(">>>>> meeting_req->time_zone.daylight_bias[%d]", meeting_req->time_zone.daylight_bias);
15117            */
15118         int col_index = 0;
15119
15120         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->meeting_response);
15121         temp_unix_time = timegm(&(meeting_req->start_time));
15122         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
15123         temp_unix_time = timegm(&(meeting_req->end_time));
15124         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
15125         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->location, 1, LOCATION_LEN_IN_MAIL_MEETING_TBL);
15126         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->global_object_id, 1, GLOBAL_OBJECT_ID_LEN_IN_MAIL_MEETING_TBL);
15127         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.offset_from_GMT);
15128         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.standard_name, 1, STANDARD_NAME_LEN_IN_MAIL_MEETING_TBL);
15129         temp_unix_time = timegm(&(meeting_req->time_zone.standard_time_start_date));
15130         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
15131         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.standard_bias);
15132         _bind_stmt_field_data_string(hStmt, col_index++, meeting_req->time_zone.daylight_name, 1, DAYLIGHT_NAME_LEN_IN_MAIL_MEETING_TBL);
15133         temp_unix_time = timegm(&(meeting_req->time_zone.daylight_time_start_date));
15134         _bind_stmt_field_data_int(hStmt, col_index++, temp_unix_time);
15135         _bind_stmt_field_data_int(hStmt, col_index++, meeting_req->time_zone.daylight_bias);
15136
15137
15138         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15139         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15140                         ("sqlite3_step fail:%d", rc));
15141         ret = true;
15142
15143
15144 FINISH_OFF:
15145         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15146
15147         if (hStmt != NULL) {
15148                 rc = sqlite3_finalize(hStmt);
15149                 if (rc != SQLITE_OK) {
15150                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15151                         error = EMAIL_ERROR_DB_FAILURE;
15152                 }
15153         }
15154
15155         if (err_code != NULL)
15156                 *err_code = error;
15157
15158         EM_DEBUG_FUNC_END("ret [%d]", ret);
15159         return ret;
15160 }
15161
15162 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)
15163 {
15164         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);
15165
15166         if (account_id < ALL_ACCOUNT || mail_id < 0) {
15167                 EM_DEBUG_EXCEPTION(" account_id[%d], mail_id[%d]", account_id, mail_id);
15168
15169                 if (err_code != NULL)
15170                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15171                 return false;
15172         }
15173
15174         int ret = false;
15175         int error = EMAIL_ERROR_NONE;
15176         int and = false;
15177         char sql_query_string[QUERY_SIZE] = {0, };
15178
15179         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15180         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15181
15182         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_meeting_tbl ");
15183
15184         if (account_id != ALL_ACCOUNT) {                /*  NOT '0' means a specific account. '0' means all account */
15185                 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);
15186                 and = true;
15187         }
15188         if (mail_id > 0) {
15189                 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);
15190                 and = true;
15191         }
15192         if (input_mailbox_id > 0) {             /*  0 means all mailbox_id */
15193                 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);
15194         }
15195
15196         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15197         if (error != EMAIL_ERROR_NONE) {
15198                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
15199                 goto FINISH_OFF;
15200         }
15201
15202         ret = true;
15203
15204 FINISH_OFF:
15205         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15206
15207         if (err_code)
15208                 *err_code = error;
15209
15210         EM_DEBUG_FUNC_END("ret [%d]", ret);
15211         return ret;
15212 }
15213
15214
15215 INTERNAL_FUNC void emstorage_free_meeting_request(email_meeting_request_t *meeting_req)
15216 {
15217         EM_DEBUG_FUNC_BEGIN();
15218
15219         if (!meeting_req) return;
15220
15221         EM_SAFE_FREE(meeting_req->location);
15222         EM_SAFE_FREE(meeting_req->global_object_id);
15223
15224         EM_DEBUG_FUNC_END();
15225 }
15226
15227 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)
15228 {
15229         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);
15230         EM_PROFILE_BEGIN(profile_emstorage_get_overflowed_mail_id_list);
15231         char sql_query_string[QUERY_SIZE] = {0, };
15232         char **result = NULL;
15233         int rc = -1, ret = false;
15234         int error = EMAIL_ERROR_NONE;
15235         int counter = 0, col_index = 0;
15236         int result_mail_id_count = 0;
15237         int *result_mail_id_list = NULL;
15238
15239         if (input_mailbox_id <= 0 || !mail_id_list || !mail_id_count || account_id < 1) {
15240                 EM_DEBUG_EXCEPTION("Invalid Parameter");
15241                 if (err_code)
15242                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15243                 return false;
15244         }
15245
15246         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);
15247
15248         EM_DEBUG_LOG_SEC("query[%s].", sql_query_string);
15249
15250         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15251         EMSTORAGE_START_READ_TRANSACTION(transaction);
15252
15253         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_mail_id_count, 0, NULL), rc);
15254         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15255                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15256
15257         if (!result_mail_id_count) {
15258                 EM_DEBUG_LOG("No mail found...");
15259                 ret = false;
15260                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
15261                 goto FINISH_OFF;
15262         }
15263
15264         EM_DEBUG_LOG("There are [%d] overflowed mails in mailbox_id [%d]", result_mail_id_count, input_mailbox_id);
15265
15266         if (!(result_mail_id_list = (int *)malloc(sizeof(int) * result_mail_id_count))) {
15267                 EM_DEBUG_EXCEPTION("malloc for result_mail_id_list failed...");
15268                 error = EMAIL_ERROR_OUT_OF_MEMORY;
15269                 sqlite3_free_table(result);
15270                 goto FINISH_OFF;
15271         }
15272
15273         memset(result_mail_id_list, 0x00, sizeof(int) * result_mail_id_count);
15274
15275         col_index = 1;
15276
15277         for (counter = 0; counter < result_mail_id_count; counter++)
15278                 _get_table_field_data_int(result, result_mail_id_list + counter, col_index++);
15279
15280         ret = true;
15281
15282 FINISH_OFF:
15283         EM_DEBUG_LOG("finish off [%d]", ret);
15284
15285         if (result)
15286                 sqlite3_free_table(result);
15287
15288         if (ret == true) {
15289                 *mail_id_list = result_mail_id_list;
15290                 *mail_id_count = result_mail_id_count;
15291         } else
15292                 EM_SAFE_FREE(result_mail_id_list);
15293
15294         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15295
15296         if (err_code != NULL)
15297                 *err_code = error;
15298
15299         EM_PROFILE_END(profile_emstorage_get_overflowed_mail_id_list);
15300         EM_DEBUG_FUNC_END("ret [%d]", ret);
15301         return ret;
15302 }
15303
15304 INTERNAL_FUNC int emstorage_get_thread_id_by_mail_id(char *multi_user_name, int mail_id, int *thread_id, int *err_code)
15305 {
15306         EM_DEBUG_FUNC_BEGIN("mail_id[%d], thread_id[%p], err_code[%p]", mail_id, thread_id, err_code);
15307
15308         int rc = -1, ret = false;
15309         int err = EMAIL_ERROR_NONE;
15310         char sql_query_string[QUERY_SIZE] = {0, };
15311         char **result;
15312         int result_count = 0;
15313
15314         if (mail_id == 0 || thread_id == NULL) {
15315                 EM_DEBUG_EXCEPTION("Invalid Parameter");
15316                 if (err_code)
15317                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15318                 return false;
15319         }
15320
15321         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15322
15323         memset(sql_query_string, 0, QUERY_SIZE);
15324         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT thread_id FROM mail_tbl WHERE mail_id = %d", mail_id);
15325
15326         /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
15327         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
15328         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15329                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15330
15331         if (!result_count) {
15332                 EM_DEBUG_LOG("No mail found...");
15333                 ret = false;
15334                 err = EMAIL_ERROR_MAIL_NOT_FOUND;
15335                 /* sqlite3_free_table(result); */
15336                 goto FINISH_OFF;
15337         }
15338
15339         _get_table_field_data_int(result, thread_id, 1);
15340
15341         sqlite3_free_table(result);
15342
15343         ret = true;
15344
15345 FINISH_OFF:
15346
15347         if (err_code != NULL)
15348                 *err_code = err;
15349
15350         EM_DEBUG_FUNC_END("ret [%d]", ret);
15351         return ret;
15352 }
15353
15354 INTERNAL_FUNC int emstorage_update_latest_thread_mail(char *multi_user_name,
15355                 int account_id,
15356                 int mailbox_id,
15357                 int mailbox_type,
15358                 int thread_id,
15359                 int *updated_thread_id,
15360                 int latest_mail_id,
15361                 int thread_item_count,
15362                 int noti_type,
15363                 int transaction,
15364                 int *err_code)
15365 {
15366         EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id [%d], thread_id[%d], updated_thread_id[%p], "
15367                         "latest_mail_id [%d], thread_item_count[%d], err_code[%p]",
15368                         account_id, mailbox_id, thread_id, updated_thread_id,
15369                         latest_mail_id, thread_item_count, err_code);
15370
15371         int rc = -1, ret = false;
15372         int err = EMAIL_ERROR_NONE;
15373         char sql_query_string[QUERY_SIZE] = {0, };
15374         char **result = NULL;
15375         int result_count = 0;
15376
15377         if (thread_id == 0) {
15378                 EM_DEBUG_EXCEPTION("Invalid Parameter");
15379                 if (err_code)
15380                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15381                 return false;
15382         }
15383
15384         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15385
15386         if (thread_item_count == 0 && latest_mail_id == 0) {
15387                 memset(sql_query_string, 0, QUERY_SIZE);
15388                 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);
15389
15390                 /*  rc = sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL); */
15391                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &result_count, 0, NULL), rc);
15392                 EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15393                                 ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15394                 EM_DEBUG_LOG("result_count[%d]", result_count);
15395                 if (result_count == 0) {
15396                         EM_DEBUG_LOG("No mail found...");
15397                         ret = false;
15398                         if (err_code)
15399                                 *err_code =  EMAIL_ERROR_MAIL_NOT_FOUND;
15400                         sqlite3_free_table(result);
15401                         return false;
15402                 }
15403
15404                 _get_table_field_data_int(result, &latest_mail_id, 2);
15405                 _get_table_field_data_int(result, &thread_item_count, 3);
15406
15407                 EM_DEBUG_LOG("latest_mail_id[%d]", latest_mail_id);
15408                 EM_DEBUG_LOG("thread_item_count[%d]", thread_item_count);
15409
15410                 sqlite3_free_table(result);
15411         }
15412
15413         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15414
15415         if (thread_item_count < 0) {
15416                 memset(sql_query_string, 0, QUERY_SIZE);
15417                 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);
15418                 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15419                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15420                 if (err != EMAIL_ERROR_NONE) {
15421                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15422                         goto FINISH_OFF;
15423                 }
15424         } else if (thread_id != latest_mail_id) {
15425                 /* Initialize the thread id */
15426                 memset(sql_query_string, 0, QUERY_SIZE);
15427                 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);
15428                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15429                 if (err != EMAIL_ERROR_NONE) {
15430                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15431                         goto FINISH_OFF;
15432                 }
15433
15434                 /* update the thread item count */
15435                 memset(sql_query_string, 0, QUERY_SIZE);
15436                 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);
15437                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15438                 if (err != EMAIL_ERROR_NONE) {
15439                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15440                         goto FINISH_OFF;
15441                 }
15442         } else {
15443                 memset(sql_query_string, 0, QUERY_SIZE);
15444                 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);
15445                 EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
15446                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15447                 if (err != EMAIL_ERROR_NONE) {
15448                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15449                         goto FINISH_OFF;
15450                 }
15451         }
15452         ret = true;
15453
15454 FINISH_OFF:
15455         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15456
15457         if (thread_id != latest_mail_id) {
15458                 if (err == EMAIL_ERROR_NONE) {
15459                         EM_DEBUG_LOG("noti_type[%d]", noti_type);
15460
15461                         if (latest_mail_id > 0 && thread_id > 0 && noti_type > 0) {
15462                                 char mailbox_id_str[25] = {0,};
15463                                 snprintf(mailbox_id_str, sizeof(mailbox_id_str), "%d", mailbox_id);
15464                                 if (!emcore_notify_storage_event(noti_type, thread_id, latest_mail_id, mailbox_id_str, account_id))
15465                                         EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventfailed [NOTI_THREAD_ID_CHANGED] >>>> ");
15466
15467                                 if (updated_thread_id) *updated_thread_id = latest_mail_id;
15468                         }
15469                 }
15470         } else if (thread_item_count >= 0) {
15471                 if (err == EMAIL_ERROR_NONE) {
15472                         char parameter_string[500] = {0,};
15473                         SNPRINTF(parameter_string, sizeof(parameter_string), "%s%c%d", "thread_item_count", 0x01, latest_mail_id);
15474                         if (!emcore_notify_storage_event(NOTI_MAIL_FIELD_UPDATE, account_id, EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT, parameter_string, thread_item_count))
15475                                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_eventfailed [NOTI_MAIL_FIELD_UPDATE] >>>> ");
15476                 }
15477         }
15478
15479         if (err_code != NULL)
15480                 *err_code = err;
15481
15482         EM_DEBUG_FUNC_END("ret [%d]", ret);
15483         return ret;
15484 }
15485
15486 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)
15487 {
15488         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);
15489
15490         int ret = false;
15491         int err = EMAIL_ERROR_NONE;
15492         char sql_query_string[QUERY_SIZE] = {0, };
15493
15494         if (thread_id == 0) {
15495                 EM_DEBUG_EXCEPTION("Invalid Parameter");
15496                 if (err_code)
15497                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15498                 return false;
15499         }
15500
15501         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15502
15503         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
15504
15505         memset(sql_query_string, 0, QUERY_SIZE);
15506         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);
15507         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15508         if (err != EMAIL_ERROR_NONE) {
15509                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
15510                 goto FINISH_OFF;
15511         }
15512
15513         ret = true;
15514
15515 FINISH_OFF:
15516         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
15517
15518         if (err_code != NULL)
15519                 *err_code = err;
15520                                         /* LCOV_EXCL_STOP */
15521         EM_DEBUG_FUNC_END("ret [%d]", ret);
15522         return ret;
15523 }
15524
15525 #ifdef __FEATURE_LOCAL_ACTIVITY__
15526 /**
15527  * emstorage_add_activity - Add Email Local activity during OFFLINE mode
15528  *
15529  */
15530 INTERNAL_FUNC int emstorage_add_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15531 {
15532         EM_DEBUG_FUNC_BEGIN();
15533
15534         EM_DEBUG_LOG(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15535
15536         int rc = -1, ret = false;
15537         int error = EMAIL_ERROR_NONE;
15538         DB_STMT hStmt = NULL;
15539         char sql_query_string[8192] = { 0x00, };
15540         int i = 0;
15541
15542         if (!local_activity) {
15543                 EM_DEBUG_EXCEPTION(" local_activity[%p], transaction[%d], err_code[%p]", local_activity, transaction, err_code);
15544                 if (err_code != NULL)
15545                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15546                 return false;
15547         }
15548
15549         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15550
15551         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15552
15553         memset(sql_query_string, 0x00 , sizeof(sql_query_string));
15554         SNPRINTF(sql_query_string, sizeof(sql_query_string), "INSERT INTO mail_local_activity_tbl VALUES (?, ?, ?, ?, ?, ?, ?)");
15555
15556         EM_DEBUG_LOG(">>>>> ACTIVITY ID [ %d ] ", local_activity->activity_id);
15557         EM_DEBUG_LOG(">>>>> MAIL ID [ %d ] ", local_activity->mail_id);
15558         EM_DEBUG_LOG(">>>>> ACCOUNT ID [ %d ] ", local_activity->account_id);
15559         EM_DEBUG_LOG(">>>>> ACTIVITY TYPE [ %d ] ", local_activity->activity_type);
15560         EM_DEBUG_LOG_SEC(">>>>> SERVER MAIL ID [ %s ] ", local_activity->server_mailid);
15561         EM_DEBUG_LOG(">>>>> SOURCE MAILBOX [ %s ] ", local_activity->src_mbox);
15562         EM_DEBUG_LOG(">>>>> DEST MAILBOX   [ %s ] ", local_activity->dest_mbox);
15563
15564         EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15565
15566
15567         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
15568         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15569                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15570
15571         EM_DEBUG_LOG_SEC(">>>> SQL STMT [ %s ] ", sql_query_string);
15572
15573         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
15574         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
15575         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
15576         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_type);
15577         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->server_mailid, 0, SERVER_MAIL_ID_LEN_IN_MAIL_TBL);
15578         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->src_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15579         _bind_stmt_field_data_string(hStmt, i++ , (char *)local_activity->dest_mbox, 0, MAILBOX_NAME_LEN_IN_MAIL_BOX_TBL);
15580
15581
15582         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
15583
15584         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
15585                         ("sqlite3_step fail:%d", rc));
15586         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
15587                         ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
15588
15589         ret = true;
15590
15591 FINISH_OFF:
15592         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15593
15594         if (hStmt != NULL) {
15595                 rc = sqlite3_finalize(hStmt);
15596                 if (rc != SQLITE_OK) {
15597                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15598                         error = EMAIL_ERROR_DB_FAILURE;
15599                 }
15600         }
15601
15602         if (err_code != NULL)
15603                 *err_code = error;
15604
15605         EM_DEBUG_FUNC_END("ret [%d]", ret);
15606         return ret;
15607 }
15608
15609 /**
15610  *      emstorage_get_activity - Get the Local activity Information
15611  *
15612  *
15613  */
15614 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)
15615 {
15616         EM_DEBUG_FUNC_BEGIN();
15617
15618         int i = 0, count = 0, rc = -1, ret = false;
15619         int error = EMAIL_ERROR_NONE;
15620         emstorage_activity_tbl_t *p_activity_tbl = NULL;
15621         char sql_query_string[1024] = {0x00, };
15622         char **result = NULL;
15623         int col_index ;
15624
15625         EM_IF_NULL_RETURN_VALUE(activity_list, false);
15626         EM_IF_NULL_RETURN_VALUE(select_num, false);
15627
15628
15629         if (!select_num || !activity_list || account_id <= 0 || activityid < 0) {
15630                 EM_DEBUG_LOG(" select_num[%p], activity_list[%p] account_id [%d] activityid [%d] ", select_num, activity_list, account_id, activityid);
15631                 if (err_code)
15632                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15633                 return false;
15634         }
15635
15636         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15637
15638         EMSTORAGE_START_READ_TRANSACTION(transaction);
15639
15640         memset(sql_query_string, 0x00, sizeof(sql_query_string));
15641
15642         if (activityid == ALL_ACTIVITIES) {
15643                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
15644                         "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d order by activity_id", account_id);
15645         } else {
15646                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
15647                         "SELECT * FROM mail_local_activity_tbl WHERE account_id = %d AND activity_id = %d ", account_id, activityid);
15648         }
15649
15650         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
15651
15652
15653
15654         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15655         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15656                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15657
15658         col_index = 7;
15659
15660         if (!(p_activity_tbl = (emstorage_activity_tbl_t*)em_malloc(sizeof(emstorage_activity_tbl_t) * count))) {
15661                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
15662                 error = EMAIL_ERROR_OUT_OF_MEMORY;
15663                 goto FINISH_OFF;
15664         }
15665
15666
15667         for (i = 0; i < count; i++) {
15668                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15669                 if (result[col_index])
15670                         p_activity_tbl[i].activity_id = atoi(result[col_index++]);
15671
15672                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15673                 if (result[col_index])
15674                         p_activity_tbl[i].account_id = atoi(result[col_index++]);
15675
15676                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15677                 if (result[col_index])
15678                         p_activity_tbl[i].mail_id = atoi(result[col_index++]);
15679
15680                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15681                 if (result[col_index])
15682                         p_activity_tbl[i].activity_type = atoi(result[col_index++]);
15683
15684
15685                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15686                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15687                         p_activity_tbl[i].server_mailid = EM_SAFE_STRDUP(result[col_index++]);
15688                 else
15689                         col_index++;
15690
15691                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15692                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15693                         p_activity_tbl[i].src_mbox = EM_SAFE_STRDUP(result[col_index++]);
15694                 else
15695                         col_index++;
15696
15697                 EM_DEBUG_LOG("result[%d] - %s ", col_index, result[col_index]);
15698                 if (result[col_index] && EM_SAFE_STRLEN(result[col_index]) > 0)
15699                         p_activity_tbl[i].dest_mbox = EM_SAFE_STRDUP(result[col_index++]);
15700                 else
15701                         col_index++;
15702
15703         }
15704
15705         if (result)
15706                 sqlite3_free_table(result);
15707
15708         ret = true;
15709
15710 FINISH_OFF:
15711
15712         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15713
15714         if (ret == true) {
15715                 *activity_list = p_activity_tbl;
15716                 *select_num = count;
15717                 EM_DEBUG_LOG(">>>> COUNT : %d >> ", count);
15718         } else if (p_activity_tbl != NULL) {
15719                 emstorage_free_local_activity(&p_activity_tbl, count, NULL);
15720         }
15721
15722
15723         if (err_code != NULL)
15724                 *err_code = error;
15725
15726         EM_DEBUG_FUNC_END("ret [%d]", ret);
15727         return ret;
15728 }
15729
15730
15731 INTERNAL_FUNC int emstorage_get_next_activity_id(int *activity_id, int *err_code)
15732 {
15733
15734         EM_DEBUG_FUNC_BEGIN();
15735
15736         int ret = false;
15737         int err = EMAIL_ERROR_NONE;
15738         int rc = -1;
15739         char *sql = NULL;
15740         char **result = NULL;
15741
15742         if (NULL == activity_id) {
15743                 EM_DEBUG_EXCEPTION(" activity_id[%p]", activity_id);
15744                 if (err_code)
15745                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15746                 return false;
15747         }
15748
15749         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15750
15751         /*  increase unique id */
15752
15753         sql = "SELECT max(rowid) FROM mail_local_activity_tbl;";
15754
15755         /*  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); */
15756         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15757                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
15758
15759         if (NULL == result[1])
15760                 rc = 1;
15761         else
15762                 rc = atoi(result[1])+1;
15763
15764         *activity_id = rc;
15765
15766         if (result)
15767                 sqlite3_free_table(result);
15768
15769         ret = true;
15770
15771 FINISH_OFF:
15772
15773         if (err_code)
15774                 *err_code = err;
15775
15776         EM_DEBUG_FUNC_END("ret [%d]", ret);
15777         return ret;
15778
15779 }
15780
15781 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)
15782 {
15783
15784         EM_DEBUG_FUNC_BEGIN();
15785
15786         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);
15787
15788         if (account_id <= 0 || NULL == activity_id_list || NULL == activity_id_count || lowest_activity_type <= 0 || highest_activity_type <= 0) {
15789                 if (err_code != NULL)
15790                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15791                 return false;
15792         }
15793
15794         int ret = false;
15795         int error = EMAIL_ERROR_NONE;
15796         int i = 0, rc = -1, count = 0;
15797         char sql_query_string[1024] = {0x00, };
15798         int *activity_ids = NULL;
15799         int col_index = 0;
15800         char **result = NULL;
15801         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15802         EMSTORAGE_START_READ_TRANSACTION(transaction);
15803
15804         memset(sql_query_string, 0x00, sizeof(sql_query_string));
15805
15806         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);
15807
15808         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
15809
15810
15811         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, NULL, NULL), rc);
15812         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
15813                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
15814
15815         col_index = 1;
15816
15817         EM_DEBUG_LOG(" Activity COUNT : %d ... ", count);
15818
15819         if (NULL == (activity_ids = (int *)em_malloc(sizeof(int) * count))) {
15820                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
15821                 error = EMAIL_ERROR_OUT_OF_MEMORY;
15822                 goto FINISH_OFF;
15823         }
15824
15825         for (i = 0; i < count; i++) {
15826                 activity_ids[i] = atoi(result[col_index]);
15827                 col_index++;
15828                 EM_DEBUG_LOG("activity_id %d", activity_ids[i]);
15829         }
15830
15831         ret = true;
15832
15833 FINISH_OFF:
15834
15835
15836         if (ret == true) {
15837                 *activity_id_count = count;
15838                 *activity_id_list = activity_ids;
15839         } else if (activity_ids != NULL) /* Prevent defect - 216566 */
15840                 EM_SAFE_FREE(activity_ids);
15841
15842
15843         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
15844         if (err_code != NULL)
15845                 *err_code = error;
15846
15847         EM_DEBUG_FUNC_END("ret [%d]", ret);
15848         return ret;
15849 }
15850
15851 INTERNAL_FUNC int emstorage_free_activity_id_list(int *activity_id_list, int *error_code)
15852 {
15853         EM_DEBUG_FUNC_BEGIN();
15854
15855         int error = EMAIL_ERROR_NONE;
15856         int ret = false;
15857
15858         EM_DEBUG_LOG(" activity_id_list [%p]", activity_id_list);
15859
15860         if (NULL == activity_id_list) {
15861                 error = EMAIL_ERROR_INVALID_PARAM;
15862                 goto FINISH_OFF;
15863         } else {
15864                 EM_SAFE_FREE(activity_id_list);
15865         }
15866
15867
15868         ret = true;
15869
15870 FINISH_OFF:
15871
15872         if (NULL != error_code)
15873                 *error_code = error;
15874
15875         EM_DEBUG_FUNC_END("ret [%d]", ret);
15876         return ret;
15877 }
15878
15879 /**
15880  * emstorage_delete_local_activity - Deletes the Local acitivity Generated based on activity_type
15881  * or based on server mail id
15882  *
15883  */
15884 INTERNAL_FUNC int emstorage_delete_local_activity(emstorage_activity_tbl_t* local_activity, int transaction, int *err_code)
15885 {
15886         EM_DEBUG_FUNC_BEGIN();
15887
15888
15889         EM_DEBUG_LOG(" local_activity[%p] ", local_activity);
15890
15891         if (!local_activity) {
15892                 EM_DEBUG_EXCEPTION(" local_activity[%p] ", local_activity);
15893                 if (err_code != NULL)
15894                         *err_code = EMAIL_ERROR_INVALID_PARAM;
15895                 return false;
15896         }
15897
15898         int rc = -1, ret = false;                       /* Prevent_FIX  */
15899         int err = EMAIL_ERROR_NONE;
15900         int query_and = 0;
15901         int query_where = 0;
15902         char sql_query_string[8192] = { 0x00, };
15903         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
15904         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
15905
15906         memset(sql_query_string, 0x00, sizeof(sql_query_string));
15907
15908         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_local_activity_tbl ");
15909
15910         EM_DEBUG_LOG_SEC(">>> Query [ %s ] ", sql_query_string);
15911
15912         if (local_activity->account_id) {
15913                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15914                                 " WHERE account_id = %d ", local_activity->account_id);
15915                 query_and = 1;
15916                 query_where = 1;
15917         }
15918
15919         if (local_activity->server_mailid) {
15920                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15921                                 " %s %s server_mailid = '%s' ", query_where ? "" : "WHERE", query_and ? "AND" : "", local_activity->server_mailid);
15922                 query_and = 1;
15923                 query_where = 1;
15924         }
15925
15926
15927         if (local_activity->mail_id) {
15928                 EM_DEBUG_LOG(">>>> MAIL ID [ %d ] , ACTIVITY TYPE [%d ]", local_activity->mail_id, local_activity->activity_type);
15929
15930                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15931                                 " %s %s mail_id = %d  ", query_where ? "" : "WHERE", query_and ? "AND" : "", local_activity->mail_id);
15932
15933                 query_and = 1;
15934                 query_where = 1;
15935
15936         }
15937
15938         if (local_activity->activity_type > 0) {
15939                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15940                                 " %s %s activity_type = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "" , local_activity->activity_type);
15941         }
15942
15943         if (local_activity->activity_id > 0) {
15944                 SNPRINTF(sql_query_string + EM_SAFE_STRLEN(sql_query_string), sizeof(sql_query_string)-(EM_SAFE_STRLEN(sql_query_string)+1),
15945                                 " %s %s activity_id = %d ", query_where ? "" : "WHERE", query_and ? "AND" : "" , local_activity->activity_id);
15946
15947         }
15948
15949         EM_DEBUG_LOG_SEC(">>>>> Query [ %s ] ", sql_query_string);
15950         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
15951         if (error != EMAIL_ERROR_NONE) {
15952                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
15953                 goto FINISH_OFF;
15954         }
15955
15956         rc = sqlite3_changes(local_db_handle);
15957         if (rc == 0) {
15958                 EM_DEBUG_EXCEPTION(" no (matched) mailbox_name found...");
15959                 err = EMAIL_ERROR_MAILBOX_NOT_FOUND;
15960         }
15961
15962         ret = true;
15963
15964 FINISH_OFF:
15965         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
15966
15967         if (hStmt != NULL) {
15968                 rc = sqlite3_finalize(hStmt);
15969                 if (rc != SQLITE_OK) {
15970                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
15971                         error = EMAIL_ERROR_DB_FAILURE;
15972                 }
15973         }
15974
15975         if (err_code != NULL)
15976                 *err_code = err;
15977         EM_DEBUG_FUNC_END("ret [%d]", ret);
15978         return ret;
15979 }
15980
15981 /**
15982  *      emstorage_free_local_activity - Free the Local Activity data
15983  */
15984 INTERNAL_FUNC int emstorage_free_local_activity(emstorage_activity_tbl_t **local_activity_list, int count, int *err_code)
15985 {
15986         EM_DEBUG_FUNC_BEGIN();
15987
15988         EM_DEBUG_LOG(" local_activity_list[%p], count[%d], err_code[%p]", local_activity_list, count, err_code);
15989
15990         int ret = false;
15991         int error = EMAIL_ERROR_INVALID_PARAM;
15992
15993         if (count > 0) {
15994                 if (!local_activity_list || !*local_activity_list) {
15995                         EM_DEBUG_EXCEPTION(" local_activity_list[%p], count[%d]", local_activity_list, count);
15996
15997                         error = EMAIL_ERROR_INVALID_PARAM;
15998                         goto FINISH_OFF;
15999                 }
16000
16001                 emstorage_activity_tbl_t* p = *local_activity_list;
16002                 int i = 0;
16003                 if (p) {
16004                         for (; i < count; i++) {
16005                                 EM_SAFE_FREE(p[i].dest_mbox);
16006                                 EM_SAFE_FREE(p[i].src_mbox);
16007                                 EM_SAFE_FREE(p[i].server_mailid);
16008                         }
16009
16010                         EM_SAFE_FREE(p);
16011                         *local_activity_list = NULL;
16012                 }
16013         }
16014
16015         ret = true;
16016
16017 FINISH_OFF:
16018
16019         if (err_code != NULL)
16020                 *err_code = error;
16021
16022         EM_DEBUG_FUNC_END("ret [%d]", ret);
16023         return ret;
16024
16025 }
16026 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
16027
16028                                         /* LCOV_EXCL_START */
16029 static int _get_key_value_string_for_list_filter_rule(email_list_filter_rule_t *input_list_filter_rule, char **output_key_value_string)
16030 {
16031         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_key_value_string [%p]", input_list_filter_rule, output_key_value_string);
16032
16033         int  ret = EMAIL_ERROR_NONE;
16034         char key_value_string[QUERY_SIZE] = { 0, };
16035         char *temp_key_value_1 = NULL;
16036         char *temp_key_value_2 = NULL;
16037
16038         if (input_list_filter_rule == NULL || output_key_value_string == NULL) {
16039                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16040                 return EMAIL_ERROR_INVALID_PARAM;
16041         }
16042
16043         switch (input_list_filter_rule->target_attribute) {
16044         case EMAIL_MAIL_ATTRIBUTE_MAIL_ID:
16045         case EMAIL_MAIL_ATTRIBUTE_ACCOUNT_ID:
16046         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID:
16047         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_TYPE:
16048         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_STATUS:
16049         case EMAIL_MAIL_ATTRIBUTE_REFERENCE_MAIL_ID:
16050         case EMAIL_MAIL_ATTRIBUTE_BODY_DOWNLOAD_STATUS:
16051         case EMAIL_MAIL_ATTRIBUTE_MAIL_SIZE:
16052         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN:
16053         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML:
16054         case EMAIL_MAIL_ATTRIBUTE_FILE_SIZE:
16055         case EMAIL_MAIL_ATTRIBUTE_FLAGS_SEEN_FIELD:
16056         case EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD:
16057         case EMAIL_MAIL_ATTRIBUTE_FLAGS_FLAGGED_FIELD:
16058         case EMAIL_MAIL_ATTRIBUTE_FLAGS_ANSWERED_FIELD:
16059         case EMAIL_MAIL_ATTRIBUTE_FLAGS_RECENT_FIELD:
16060         case EMAIL_MAIL_ATTRIBUTE_FLAGS_DRAFT_FIELD:
16061         case EMAIL_MAIL_ATTRIBUTE_FLAGS_FORWARDED_FIELD:
16062         case EMAIL_MAIL_ATTRIBUTE_DRM_STATUS:
16063         case EMAIL_MAIL_ATTRIBUTE_PRIORITY:
16064         case EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS:
16065         case EMAIL_MAIL_ATTRIBUTE_LOCK_STATUS:
16066         case EMAIL_MAIL_ATTRIBUTE_REPORT_STATUS:
16067         case EMAIL_MAIL_ATTRIBUTE_ATTACHMENT_COUNT:
16068         case EMAIL_MAIL_ATTRIBUTE_INLINE_CONTENT_COUNT:
16069         case EMAIL_MAIL_ATTRIBUTE_THREAD_ID:
16070         case EMAIL_MAIL_ATTRIBUTE_THREAD_ITEM_COUNT:
16071         case EMAIL_MAIL_ATTRIBUTE_MEETING_REQUEST_STATUS:
16072         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_CLASS:
16073         case EMAIL_MAIL_ATTRIBUTE_DIGEST_TYPE:
16074         case EMAIL_MAIL_ATTRIBUTE_SMIME_TYPE:
16075         case EMAIL_MAIL_ATTRIBUTE_REMAINING_RESEND_TIMES:
16076         case EMAIL_MAIL_ATTRIBUTE_TAG_ID:
16077         case EMAIL_MAIL_ATTRIBUTE_EAS_DATA_LENGTH_TYPE:
16078                 SNPRINTF(key_value_string, QUERY_SIZE, "%d", input_list_filter_rule->key_value.integer_type_value);
16079                 break;
16080
16081         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
16082         case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
16083         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
16084         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
16085         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
16086         case EMAIL_MAIL_ATTRIBUTE_FROM:
16087         case EMAIL_MAIL_ATTRIBUTE_TO:
16088         case EMAIL_MAIL_ATTRIBUTE_CC:
16089         case EMAIL_MAIL_ATTRIBUTE_BCC:
16090         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
16091                 if (input_list_filter_rule->key_value.string_type_value == NULL) {
16092                         EM_DEBUG_EXCEPTION("Invalid string_type_value [%p]", input_list_filter_rule->key_value.string_type_value);
16093                         ret = EMAIL_ERROR_INVALID_PARAM;
16094                         goto FINISH_OFF;
16095                 }
16096
16097                 temp_key_value_1 = input_list_filter_rule->key_value.string_type_value;
16098
16099                 temp_key_value_2 = em_replace_all_string(temp_key_value_1, "_", "\\_");
16100                 temp_key_value_1 = em_replace_all_string(temp_key_value_2, "%", "\\%");
16101
16102                 if (input_list_filter_rule->rule_type == EMAIL_LIST_FILTER_RULE_INCLUDE)
16103                         SNPRINTF(key_value_string, QUERY_SIZE, "\'%%%s%%\'", temp_key_value_1);
16104                 else
16105                         SNPRINTF(key_value_string, QUERY_SIZE, "\'%s\'", temp_key_value_1);
16106                 break;
16107
16108         case EMAIL_MAIL_ATTRIBUTE_DATE_TIME:
16109         case EMAIL_MAIL_ATTRIBUTE_SCHEDULED_SENDING_TIME:
16110         case EMAIL_MAIL_ATTRIBUTE_REPLIED_TIME:
16111         case EMAIL_MAIL_ATTRIBUTE_FORWARDED_TIME:
16112                 SNPRINTF(key_value_string, QUERY_SIZE, "%d", (int)input_list_filter_rule->key_value.datetime_type_value);
16113                 break;
16114
16115         default:
16116                 ret = EMAIL_ERROR_INVALID_PARAM;
16117                 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
16118                 break;
16119         }
16120
16121         if (ret == EMAIL_ERROR_NONE && EM_SAFE_STRLEN(key_value_string) > 0)
16122                 *output_key_value_string = strdup(key_value_string);
16123
16124 FINISH_OFF:
16125
16126         EM_SAFE_FREE(temp_key_value_1);
16127         EM_SAFE_FREE(temp_key_value_2);
16128
16129         EM_DEBUG_FUNC_END("ret [%d]", ret);
16130         return ret;
16131 }
16132
16133 #ifdef __FEATURE_SUPPORT_PRIVATE_CERTIFICATE__
16134 static int _get_cert_password_file_name(int index, char *cert_password_file_name)
16135 {
16136         EM_DEBUG_FUNC_BEGIN("index : [%d]", index);
16137
16138         if (index <= 0 || !cert_password_file_name) {
16139                 EM_DEBUG_EXCEPTION("Invalid parameter");
16140                 return EMAIL_ERROR_INVALID_PARAM;
16141         }
16142
16143         sprintf(cert_password_file_name, ".email_cert_%d", index);
16144
16145         EM_DEBUG_FUNC_END();
16146         return EMAIL_ERROR_NONE;
16147 }
16148 #endif
16149
16150 static int _make_filter_rule_string(email_list_filter_rule_t *input_list_filter_rule, char **output_string)
16151 {
16152         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16153
16154         int   ret = EMAIL_ERROR_NONE;
16155         int   is_alpha = 0;
16156         int   length_field_name = 0;
16157         int   length_value = 0;
16158         char  result_rule_string[QUERY_SIZE] = { 0 , };
16159         char *mod_field_name_string = NULL;
16160         char *mod_value_string = NULL;
16161         char *temp_field_name_string = NULL;
16162         char *temp_key_value_string = NULL;
16163
16164         if (input_list_filter_rule == NULL || output_string == NULL) {
16165                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16166                 return  EMAIL_ERROR_INVALID_PARAM;
16167         }
16168
16169         temp_field_name_string = emcore_get_mail_field_name_by_attribute_type(input_list_filter_rule->target_attribute);
16170
16171         if (temp_field_name_string == NULL) {
16172                 EM_DEBUG_EXCEPTION("Invalid target_attribute [%d]", input_list_filter_rule->target_attribute);
16173                 return EMAIL_ERROR_INVALID_PARAM;
16174         }
16175
16176         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) {
16177                 EM_DEBUG_EXCEPTION("_get_key_value_string_for_list_filter_rule failed");
16178                 return EMAIL_ERROR_INVALID_PARAM;
16179         }
16180
16181         length_field_name = EM_SAFE_STRLEN(temp_field_name_string);
16182         length_value      = EM_SAFE_STRLEN(temp_key_value_string);
16183
16184         switch (input_list_filter_rule->target_attribute) {
16185         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
16186         case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
16187         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
16188         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
16189         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
16190         case EMAIL_MAIL_ATTRIBUTE_FROM:
16191         case EMAIL_MAIL_ATTRIBUTE_TO:
16192         case EMAIL_MAIL_ATTRIBUTE_CC:
16193         case EMAIL_MAIL_ATTRIBUTE_BCC:
16194         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
16195                 is_alpha = 1;
16196                 break;
16197         default:
16198                 is_alpha = 0;
16199                 break;
16200         }
16201
16202         if (is_alpha == 1 && input_list_filter_rule->case_sensitivity == false) {
16203                 length_field_name += strlen("UPPER() ");
16204                 length_value      += strlen("UPPER() ");
16205                 mod_field_name_string = em_malloc(sizeof(char) * length_field_name);
16206                 if (mod_field_name_string == NULL) {
16207                         EM_DEBUG_EXCEPTION("em_mallocfailed");
16208                         EM_SAFE_FREE(temp_field_name_string);
16209                         ret = EMAIL_ERROR_OUT_OF_MEMORY;
16210                         goto FINISH_OFF;
16211                 }
16212
16213                 mod_value_string = em_malloc(sizeof(char) * length_value);
16214                 if (mod_value_string == NULL) {
16215                         EM_DEBUG_EXCEPTION("em_mallocfailed");
16216                         EM_SAFE_FREE(temp_field_name_string);
16217                         ret = EMAIL_ERROR_OUT_OF_MEMORY;
16218                         goto FINISH_OFF;
16219                 }
16220
16221                 SNPRINTF(mod_field_name_string, length_field_name, "UPPER(%s)", temp_field_name_string);
16222                 SNPRINTF(mod_value_string,      length_value, "UPPER(%s)", temp_key_value_string);
16223                 EM_SAFE_FREE(temp_key_value_string);
16224         } else {
16225                 mod_field_name_string = strdup(temp_field_name_string);
16226                 mod_value_string      = temp_key_value_string;
16227         }
16228
16229         switch (input_list_filter_rule->rule_type) {
16230         case EMAIL_LIST_FILTER_RULE_EQUAL:
16231                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = %s ", mod_field_name_string, mod_value_string);
16232                 break;
16233         case EMAIL_LIST_FILTER_RULE_NOT_EQUAL:
16234                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s != %s ", mod_field_name_string, mod_value_string);
16235                 break;
16236         case EMAIL_LIST_FILTER_RULE_LESS_THAN:
16237                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s < %s ", mod_field_name_string, mod_value_string);
16238                 break;
16239         case EMAIL_LIST_FILTER_RULE_GREATER_THAN:
16240                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s > %s ", mod_field_name_string, mod_value_string);
16241                 break;
16242         case EMAIL_LIST_FILTER_RULE_LESS_THAN_OR_EQUAL:
16243                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s <= %s ", mod_field_name_string, mod_value_string);
16244                 break;
16245         case EMAIL_LIST_FILTER_RULE_GREATER_THAN_OR_EQUAL:
16246                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s >= %s ", mod_field_name_string, mod_value_string);
16247                 break;
16248         case EMAIL_LIST_FILTER_RULE_INCLUDE:
16249                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s LIKE %s ", mod_field_name_string, mod_value_string);
16250                 break;
16251         case EMAIL_LIST_FILTER_RULE_IN:
16252                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s IN (%s) ", mod_field_name_string, mod_value_string);
16253                 break;
16254         case EMAIL_LIST_FILTER_RULE_NOT_IN:
16255                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s NOT IN (%s) ", mod_field_name_string, mod_value_string);
16256                 break;
16257         default:
16258                 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16259                 ret = EMAIL_ERROR_INVALID_PARAM;
16260                 goto FINISH_OFF;
16261         }
16262
16263         *output_string = strdup(result_rule_string);
16264
16265 FINISH_OFF:
16266         EM_SAFE_FREE(mod_field_name_string);
16267         EM_SAFE_FREE(mod_value_string);
16268
16269         EM_DEBUG_FUNC_END("ret [%d]", ret);
16270         return ret;
16271 }
16272
16273 static int _make_filter_attach_rule_string(char *multi_user_name, email_list_filter_rule_attach_t *input_list_filter_rule, char **output_string)
16274 {
16275         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16276
16277         char *field_name_string = NULL;
16278         char  key_value_string[QUERY_SIZE] = {0,};
16279         char  result_rule_string[QUERY_SIZE] = {0,};
16280         int rc = -1;
16281         int count = 0;
16282         int query_size = 0;
16283         int cur_query = 0;
16284         int col_index = 0;
16285         int error = EMAIL_ERROR_NONE;
16286         char **result = NULL;
16287         char sql_query_string[QUERY_SIZE] = {0,};
16288         char *sql_query_string2 = NULL;
16289         sqlite3 *local_db_handle = NULL;
16290         int *mail_ids = NULL;
16291         int ret = 0;
16292
16293         if (input_list_filter_rule == NULL || output_string == NULL) {
16294                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16295                 return  EMAIL_ERROR_INVALID_PARAM;
16296         }
16297
16298         field_name_string = EM_SAFE_STRDUP("attachment_name");
16299         SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
16300
16301         switch (input_list_filter_rule->rule_type) {
16302
16303         case EMAIL_LIST_FILTER_RULE_INCLUDE:
16304                 ret = SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
16305                 break;
16306
16307         case EMAIL_LIST_FILTER_RULE_MATCH:
16308                 ret = SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
16309                 break;
16310
16311         default:
16312                 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16313                 error = EMAIL_ERROR_INVALID_PARAM;
16314                 goto FINISH_OFF;
16315         }
16316
16317         if(ret < 0){
16318                 EM_DEBUG_LOG("WARNING: result rule string is truncated (%s)", result_rule_string);
16319         }
16320
16321         local_db_handle = emstorage_get_db_connection(multi_user_name);
16322
16323         EMSTORAGE_START_READ_TRANSACTION(true);
16324         ret = SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_attachment_tbl %s", result_rule_string);
16325
16326         if(ret < 0){
16327                 EM_DEBUG_LOG("WARNING: sql query string is truncated (%s)", sql_query_string);
16328         }
16329
16330         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16331         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16332                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16333
16334         col_index = 1;
16335
16336         if (!count) {
16337                 EM_DEBUG_LOG("No mail found...");
16338                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
16339                 *output_string = strdup("mail_id IN () ");
16340                 goto FINISH_OFF;
16341         }
16342
16343         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
16344         int i = 0;
16345         if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
16346                 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
16347                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16348                 goto FINISH_OFF;
16349         }
16350
16351         for (i = 0; i < count; i++) {
16352                 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16353                 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16354         }
16355
16356         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >>", count);
16357         sqlite3_free_table(result);
16358         EMSTORAGE_FINISH_READ_TRANSACTION(true);
16359
16360         //      sqlite3_db_release_memory(local_db_handle);
16361
16362
16363         query_size = (10 * count) + strlen("mail_id IN ()  ");
16364
16365         sql_query_string2 = em_malloc(query_size);
16366         if (sql_query_string2 == NULL) {
16367                 EM_DEBUG_EXCEPTION("em_mallocfailed");
16368                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16369                 goto FINISH_OFF;
16370         }
16371
16372         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN (");
16373         for (i = 0; i < count-1; i++)
16374                 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16375
16376         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d) ", mail_ids[count-1]);
16377
16378         *output_string = strdup(sql_query_string2);
16379 FINISH_OFF:
16380
16381         EM_SAFE_FREE(mail_ids); /* prevent */
16382         EM_SAFE_FREE(sql_query_string2);
16383         EM_SAFE_FREE(field_name_string);
16384         EM_DEBUG_FUNC_END("error [%d]", error);
16385         return error;
16386 }
16387
16388 static int _make_filter_fts_rule_string(char *multi_user_name, email_list_filter_rule_fts_t *input_list_filter_rule, char **output_string)
16389 {
16390         EM_DEBUG_FUNC_BEGIN("input_list_filter_rule [%p], output_string [%p]", input_list_filter_rule, output_string);
16391         char *field_name_string = NULL;
16392         char key_value_string[QUERY_SIZE] = {0,};
16393         char  result_rule_string[QUERY_SIZE] = {0,};
16394         int rc = -1;
16395         int count = 0;
16396         int col_index = 0;
16397         int query_size = 0;
16398         int error = EMAIL_ERROR_NONE;
16399         char **result = NULL;
16400         char sql_query_string[QUERY_SIZE] = {0,};
16401         char *sql_query_string2 = NULL;
16402         sqlite3 *local_db_handle = NULL;
16403         int *mail_ids = NULL;
16404         int ret = 0;
16405
16406         if (input_list_filter_rule == NULL || output_string == NULL) {
16407                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16408                 return  EMAIL_ERROR_INVALID_PARAM;
16409         }
16410
16411         field_name_string = EM_SAFE_STRDUP("body_text");
16412         SNPRINTF(key_value_string, QUERY_SIZE, "%s", input_list_filter_rule->key_value.string_type_value);
16413
16414         switch (input_list_filter_rule->rule_type) {
16415
16416         case EMAIL_LIST_FILTER_RULE_INCLUDE:
16417                 ret = SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s LIKE \'%%%s%%\' ", field_name_string, key_value_string);
16418                 break;
16419
16420         case EMAIL_LIST_FILTER_RULE_MATCH:
16421                 ret = SNPRINTF(result_rule_string, QUERY_SIZE, "WHERE %s MATCH \'%s\' ", field_name_string, key_value_string);
16422                 break;
16423
16424         default:
16425                 EM_DEBUG_EXCEPTION("Invalid rule_type [%d]", input_list_filter_rule->rule_type);
16426                 error = EMAIL_ERROR_INVALID_PARAM;
16427                 goto FINISH_OFF;
16428         }
16429
16430         if(ret < 0){
16431                 EM_DEBUG_LOG("WARNING: result rule string is truncated (%s)", result_rule_string);
16432         }
16433
16434         local_db_handle = emstorage_get_db_connection(multi_user_name);
16435
16436         EMSTORAGE_START_READ_TRANSACTION(true);
16437         ret = SNPRINTF(sql_query_string, QUERY_SIZE, "SELECT mail_id FROM mail_text_tbl %s", result_rule_string);
16438
16439         if(ret < 0){
16440                 EM_DEBUG_LOG("WARNING: sql query string is truncated (%s)", sql_query_string);
16441         }
16442
16443         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
16444         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16445                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16446
16447         col_index = 1;
16448
16449         if (!count) {
16450                 EM_DEBUG_LOG("No mail found...");
16451                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
16452                 *output_string = strdup("mail_id IN () ");
16453                 goto FINISH_OFF;
16454         }
16455
16456         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN START >>");
16457         int i = 0;
16458
16459         if (!(mail_ids = (int *)em_malloc(sizeof(int) * count))) {
16460                 EM_DEBUG_EXCEPTION("malloc for mail_ids failed...");
16461                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16462                 goto FINISH_OFF;
16463         }
16464
16465         for (i = 0; i < count; i++) {
16466                 _get_table_field_data_int(result, &(mail_ids[i]), col_index++);
16467                 EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN [mail_id : %d] >>", mail_ids[i]);
16468         }
16469
16470         EM_DEBUG_LOG_DEV(">>>> DATA ASSIGN END [count : %d] >>", count);
16471         sqlite3_free_table(result);
16472         EMSTORAGE_FINISH_READ_TRANSACTION(true);
16473
16474         //      sqlite3_db_release_memory(local_db_handle);
16475
16476
16477         query_size = (10 * count) + strlen("mail_id IN ()  ");
16478         sql_query_string2 = em_malloc(query_size);
16479         if (sql_query_string2 == NULL) {
16480                 EM_DEBUG_EXCEPTION("em_mallocfailed");
16481                 error = EMAIL_ERROR_OUT_OF_MEMORY;
16482                 goto FINISH_OFF;
16483         }
16484         int cur_query = 0;
16485         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "mail_id IN (");
16486         for (i = 0; i < count-1; i++)
16487                 cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d, ", mail_ids[i]);
16488
16489         cur_query += SNPRINTF_OFFSET(sql_query_string2, cur_query, query_size, "%d) ", mail_ids[count-1]);
16490
16491         *output_string = strdup(sql_query_string2);
16492
16493 FINISH_OFF:
16494         EM_SAFE_FREE(mail_ids); /* prevent */
16495         EM_SAFE_FREE(sql_query_string2);
16496         EM_SAFE_FREE(field_name_string);
16497         EM_DEBUG_FUNC_END("error [%d]", error);
16498         return error;
16499 }
16500
16501 static int _make_order_rule_string(char *multi_user_name, email_list_sorting_rule_t *input_sorting_rule, char **output_string)
16502 {
16503         EM_DEBUG_FUNC_BEGIN("input_sorting_rule [%p], output_string [%p]", input_sorting_rule, output_string);
16504
16505         char  result_rule_string[QUERY_SIZE] = { 0 , };
16506         int   ret = EMAIL_ERROR_NONE;
16507
16508         emstorage_account_tbl_t *account_tbl_array = NULL;
16509         int count = 0;
16510         int i = 0;
16511         char *result_str = NULL;
16512         char *tmp_str1 = NULL;
16513         char *tmp_str2 = NULL;
16514         char query_per_account[QUERY_SIZE] = { 0 , };
16515
16516         if (input_sorting_rule->force_boolean_check) {
16517                 SNPRINTF(result_rule_string, QUERY_SIZE, "%s = 0 ",
16518                         emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16519         } else {
16520                 EM_SAFE_STRNCPY(result_rule_string,
16521                         emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16522                         QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16523         }
16524
16525         switch (input_sorting_rule->sort_order) {
16526         case EMAIL_SORT_ORDER_ASCEND:
16527                 EM_SAFE_STRNCAT(result_rule_string, " ASC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16528                 break;
16529
16530         case EMAIL_SORT_ORDER_DESCEND:
16531                 EM_SAFE_STRNCAT(result_rule_string, " DESC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16532                 break;
16533
16534         case EMAIL_SORT_ORDER_NOCASE_ASCEND:
16535                 EM_SAFE_STRNCAT(result_rule_string, " COLLATE NOCASE ASC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16536                 break;
16537
16538         case EMAIL_SORT_ORDER_NOCASE_DESCEND:
16539                 EM_SAFE_STRNCAT(result_rule_string, " COLLATE NOCASE DESC ", QUERY_SIZE - EM_SAFE_STRLEN(result_rule_string) - 1);
16540                 break;
16541
16542         case EMAIL_SORT_ORDER_TO_CCBCC:
16543                 memset(result_rule_string, 0, QUERY_SIZE);
16544                 if (input_sorting_rule->key_value.string_type_value)
16545                         sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16546                                         " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 ELSE 2 END ",
16547                                         input_sorting_rule->key_value.string_type_value);
16548                 break;
16549
16550         case EMAIL_SORT_ORDER_TO_CC_BCC:
16551                 memset(result_rule_string, 0, QUERY_SIZE);
16552                 if (input_sorting_rule->key_value.string_type_value)
16553                         sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16554                                         " CASE WHEN full_address_to LIKE \'%%%q%%\' THEN 1 WHEN full_address_cc LIKE \'%%%q%%\' THEN 2 ELSE 3 END ",
16555                                         input_sorting_rule->key_value.string_type_value, input_sorting_rule->key_value.string_type_value);
16556                 break;
16557
16558         case EMAIL_SORT_ORDER_TO_CCBCC_ALL:
16559                 if (!emstorage_get_account_list(multi_user_name, &count, &account_tbl_array, true, false, NULL)) {
16560                         EM_DEBUG_EXCEPTION("emstorage_get_account_list failed");
16561                         goto FINISH_OFF;
16562                 }
16563
16564                 if (!count) {
16565                         EM_DEBUG_LOG("No account exist");
16566                         ret = EMAIL_ERROR_INVALID_PARAM;
16567                         goto FINISH_OFF;
16568                 }
16569
16570                 for (i = 0; i < count; i++) {
16571                         if (i > 0 && result_str) {
16572                                 tmp_str2 = result_str;
16573                                 result_str = g_strconcat(tmp_str2, " OR ", NULL);
16574                                 EM_SAFE_FREE(tmp_str2);
16575                         }
16576
16577                         memset(query_per_account, 0, QUERY_SIZE);
16578                         snprintf(query_per_account, QUERY_SIZE,
16579                                         "(account_id = %d AND full_address_to LIKE \'%%%s%%\')",
16580                                         account_tbl_array[i].account_id, account_tbl_array[i].user_email_address);
16581
16582                         tmp_str1 = result_str;
16583                         if (tmp_str1)
16584                                 result_str = g_strconcat(tmp_str1, query_per_account, NULL);
16585                         else
16586                                 result_str = g_strdup(query_per_account);
16587                         EM_SAFE_FREE(tmp_str1);
16588                 }
16589
16590                 snprintf(result_rule_string, QUERY_SIZE,
16591                                 " CASE WHEN %s THEN 1 ELSE 2 END ", result_str);
16592
16593                 EM_SAFE_FREE(result_str);
16594                 if (account_tbl_array)
16595                         emstorage_free_account(&account_tbl_array, count, NULL);
16596                 break;
16597
16598         case EMAIL_SORT_ORDER_LOCALIZE_ASCEND:
16599                 memset(result_rule_string, 0, QUERY_SIZE);
16600                 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16601                                 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END ASC, %s COLLATE NOCASE ASC ",
16602                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16603                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16604                 break;
16605
16606         case EMAIL_SORT_ORDER_LOCALIZE_DESCEND:
16607                 memset(result_rule_string, 0, QUERY_SIZE);
16608                 sqlite3_snprintf(QUERY_SIZE, result_rule_string,
16609                                 " CASE WHEN %s GLOB \'[][~`!@#$%%^&*()_-+=|\\{}:;<>,.?/ ]*\' THEN 1 ELSE 2 END DESC, %s COLLATE NOCASE DESC ",
16610                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute),
16611                                 emcore_get_mail_field_name_by_attribute_type(input_sorting_rule->target_attribute));
16612                 break;
16613
16614         default:
16615                 EM_DEBUG_EXCEPTION("Invalid sort_order [%d]", input_sorting_rule->sort_order);
16616                 ret = EMAIL_ERROR_INVALID_PARAM;
16617                 goto FINISH_OFF;
16618         }
16619
16620         *output_string = strdup(result_rule_string);
16621
16622 FINISH_OFF:
16623         EM_DEBUG_FUNC_END("ret [%d]", ret);
16624         return ret;
16625 }
16626
16627 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)
16628 {
16629         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);
16630         int ret = EMAIL_ERROR_NONE;
16631         int i = 0;
16632         int string_offset = 0;
16633         int query_size = 0;
16634         int new_query_size = 0;
16635         char *conditional_clause_string = NULL;
16636         char *result_string_for_a_item = NULL;
16637
16638         if ((input_filter_count > 0 && !input_filter_list) || (input_sorting_rule_count > 0 && !input_sorting_rule_list) || output_conditional_clause == NULL) {
16639                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16640                 return EMAIL_ERROR_INVALID_PARAM;
16641         }
16642
16643         conditional_clause_string = em_malloc(QUERY_SIZE);
16644         if (conditional_clause_string == NULL) {
16645                 EM_DEBUG_EXCEPTION("Memory is full");
16646                 return EMAIL_ERROR_OUT_OF_MEMORY;
16647         }
16648
16649         if (input_filter_count > 0) {
16650                 query_size = QUERY_SIZE;
16651                 g_strlcpy(conditional_clause_string, " WHERE ", QUERY_SIZE);
16652
16653                 for (i = 0; i < input_filter_count; i++) {
16654                         switch (input_filter_list[i].list_filter_item_type) {
16655                         case EMAIL_LIST_FILTER_ITEM_RULE:
16656                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE", i);
16657                                 _make_filter_rule_string(&(input_filter_list[i].list_filter_item.rule), &result_string_for_a_item);
16658                                 break;
16659
16660                         case EMAIL_LIST_FILTER_ITEM_RULE_FTS:
16661                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_FTS", i);
16662                                 _make_filter_fts_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_fts), &result_string_for_a_item);
16663                                 break;
16664
16665                         case EMAIL_LIST_FILTER_ITEM_RULE_ATTACH:
16666                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_RULE_ATTACH", i);
16667                                 _make_filter_attach_rule_string(multi_user_name, &(input_filter_list[i].list_filter_item.rule_attach), &result_string_for_a_item);
16668                                 break;
16669
16670                         case EMAIL_LIST_FILTER_ITEM_OPERATOR:
16671                                 EM_DEBUG_LOG_DEV("[%d]list_filter_item_type is EMAIL_LIST_FILTER_ITEM_OPERATOR", i);
16672                                 switch (input_filter_list[i].list_filter_item.operator_type) {
16673                                 case EMAIL_LIST_FILTER_OPERATOR_AND:
16674                                         result_string_for_a_item = strdup("AND ");
16675                                         break;
16676                                 case EMAIL_LIST_FILTER_OPERATOR_OR:
16677                                         result_string_for_a_item = strdup("OR ");
16678                                         break;
16679                                 case EMAIL_LIST_FILTER_OPERATOR_LEFT_PARENTHESIS:
16680                                         result_string_for_a_item = strdup(" (");
16681                                         break;
16682                                 case EMAIL_LIST_FILTER_OPERATOR_RIGHT_PARENTHESIS:
16683                                         result_string_for_a_item = strdup(") ");
16684                                         break;
16685                                 }
16686                                 break;
16687
16688                         default:
16689                                 EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", input_filter_list[i].list_filter_item_type);
16690                                 ret = EMAIL_ERROR_INVALID_PARAM;
16691                                 goto FINISH_OFF;
16692                         }
16693
16694                         if (result_string_for_a_item == NULL) {
16695                                 EM_DEBUG_EXCEPTION("result_string_for_a_item is null");
16696                                 ret = EMAIL_ERROR_INVALID_PARAM;
16697                                 goto FINISH_OFF;
16698                         }
16699
16700                         if (strlen(conditional_clause_string) + EM_SAFE_STRLEN(result_string_for_a_item) >= query_size) { /* prevent 34364 */
16701                                 EM_DEBUG_LOG("QUERY is too long");
16702                                 new_query_size = EM_SAFE_STRLEN(result_string_for_a_item) + EM_SAFE_STRLEN(conditional_clause_string) + QUERY_SIZE;
16703                                 conditional_clause_string = realloc(conditional_clause_string, new_query_size);
16704                                 if (conditional_clause_string == NULL) {
16705                                         EM_DEBUG_EXCEPTION("realloc failed");
16706                                         ret = EMAIL_ERROR_OUT_OF_MEMORY;
16707                                         goto FINISH_OFF;
16708                                 }
16709
16710                                 query_size = new_query_size;
16711                         }
16712
16713                         EM_SAFE_STRNCAT(conditional_clause_string, result_string_for_a_item , QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16714                         EM_SAFE_FREE(result_string_for_a_item);
16715                 }
16716         }
16717
16718         if (input_sorting_rule_count > 0) {
16719                 EM_SAFE_STRNCAT(conditional_clause_string, "ORDER BY ", QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16720
16721                 for (i = 0; i < input_sorting_rule_count; i++) {
16722                         if ((ret = _make_order_rule_string(multi_user_name, &input_sorting_rule_list[i], &result_string_for_a_item)) != EMAIL_ERROR_NONE) {
16723                                 EM_DEBUG_EXCEPTION("_make_order_rule_string failed. [%d]", ret);
16724                                 goto FINISH_OFF;
16725                         }
16726                         if (i > 0)
16727                                 EM_SAFE_STRNCAT(conditional_clause_string, ", " , QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16728                         EM_SAFE_STRNCAT(conditional_clause_string, result_string_for_a_item , QUERY_SIZE - EM_SAFE_STRLEN(conditional_clause_string) - 1);
16729                         EM_SAFE_FREE(result_string_for_a_item);
16730                 }
16731         }
16732
16733         if (input_start_index != -1 && input_limit_count != -1) {
16734                 string_offset = strlen(conditional_clause_string);
16735                 SNPRINTF_OFFSET(conditional_clause_string, string_offset, query_size, " LIMIT %d, %d", input_start_index, input_limit_count);
16736         }
16737
16738         *output_conditional_clause = strdup(conditional_clause_string);
16739
16740 FINISH_OFF:
16741         EM_SAFE_FREE(result_string_for_a_item);
16742         EM_SAFE_FREE(conditional_clause_string);
16743
16744         EM_DEBUG_FUNC_END("ret [%d]", ret);
16745         return ret;
16746 }
16747
16748 INTERNAL_FUNC int emstorage_free_list_filter(email_list_filter_t **input_filter_list, int input_filter_count)
16749 {
16750         EM_DEBUG_FUNC_BEGIN("input_filter_list [%p], input_filter_count[%d]", input_filter_list, input_filter_count);
16751         int err = EMAIL_ERROR_NONE;
16752         int i = 0;
16753         email_list_filter_t *temp_filter_list = NULL;
16754
16755         EM_IF_NULL_RETURN_VALUE(input_filter_list, EMAIL_ERROR_INVALID_PARAM);
16756
16757         for (i = 0; i < input_filter_count; i++) {
16758                 temp_filter_list = (*input_filter_list) + i;
16759                 if (!temp_filter_list)
16760                         continue;
16761
16762                 if (temp_filter_list->list_filter_item_type == EMAIL_LIST_FILTER_ITEM_RULE) {
16763                         switch (temp_filter_list->list_filter_item.rule.target_attribute) {
16764                         case EMAIL_MAIL_ATTRIBUTE_MAILBOX_NAME:
16765                         case EMAIL_MAIL_ATTRIBUTE_SUBJECT:
16766                         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAILBOX_NAME:
16767                         case EMAIL_MAIL_ATTRIBUTE_SERVER_MAIL_ID:
16768                         case EMAIL_MAIL_ATTRIBUTE_MESSAGE_ID:
16769                         case EMAIL_MAIL_ATTRIBUTE_FROM:
16770                         case EMAIL_MAIL_ATTRIBUTE_TO:
16771                         case EMAIL_MAIL_ATTRIBUTE_CC:
16772                         case EMAIL_MAIL_ATTRIBUTE_BCC:
16773                         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_PLAIN:
16774                         case EMAIL_MAIL_ATTRIBUTE_FILE_PATH_HTML:
16775                         case EMAIL_MAIL_ATTRIBUTE_PREVIEW_TEXT:
16776                                 EM_SAFE_FREE(temp_filter_list->list_filter_item.rule.key_value.string_type_value);
16777                                 break;
16778                         default:
16779                                 break;
16780                         }
16781                 } 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) {
16782                         EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_fts.key_value.string_type_value);
16783                 } 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) {
16784                         EM_SAFE_FREE(temp_filter_list->list_filter_item.rule_attach.key_value.string_type_value);
16785                 }
16786         }
16787
16788         EM_SAFE_FREE(*input_filter_list);
16789
16790         EM_DEBUG_FUNC_END("err [%d]", err);
16791         return err;
16792 }
16793
16794 /* Tasks --------------------------------------------------------------------------*/
16795 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)
16796 {
16797         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);
16798         int ret = 0;
16799         int i = 0;
16800         int task_id = 0;
16801         int err = EMAIL_ERROR_NONE;
16802         int rc = -1;
16803         DB_STMT hStmt = NULL;
16804         char sql_query_string[QUERY_SIZE] = {0, };
16805         sqlite3 *local_db_handle = NULL;
16806         char *sql = "SELECT max(rowid) FROM mail_task_tbl;";
16807         char **result = NULL;
16808
16809         if (input_task_parameter == NULL || output_task_id == NULL) {
16810                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16811                 return EMAIL_ERROR_INVALID_PARAM;
16812         }
16813
16814         local_db_handle = emstorage_get_db_connection(multi_user_name);
16815         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, input_transaction, err);
16816
16817         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
16818         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16819                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
16820
16821         if (NULL == result[1])
16822                 task_id = 1;
16823         else
16824                 task_id = atoi(result[1])+1;
16825
16826         *output_task_id = task_id;
16827
16828         sqlite3_free_table(result);
16829         result = NULL;
16830
16831         SNPRINTF(sql_query_string, sizeof(sql_query_string),
16832                         "INSERT INTO mail_task_tbl VALUES "
16833                         "(        "
16834                         "    ? "  /*   task_id */
16835                         "  , ? "  /*   task_type */
16836                         "  , ? "  /*   task_status */
16837                         "  , ? "  /*   task_priority */
16838                         "  , ? "  /*   task_parameter_length */
16839                         "  , ? "  /*   task_parameter */
16840                         "  , ? "  /*   date_time */
16841                         ") ");
16842
16843         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16844         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16845                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16846
16847         EM_DEBUG_LOG_SEC(">>>> SQL STMT [%s] ", sql_query_string);
16848
16849
16850         _bind_stmt_field_data_int(hStmt, i++, task_id);
16851         _bind_stmt_field_data_int(hStmt, i++, input_task_type);
16852         _bind_stmt_field_data_int(hStmt, i++, EMAIL_TASK_STATUS_WAIT);
16853         _bind_stmt_field_data_int(hStmt, i++, input_task_priority);
16854         _bind_stmt_field_data_int(hStmt, i++, input_task_parameter_length);
16855         _bind_stmt_field_data_blob(hStmt, i++, input_task_parameter, input_task_parameter_length);
16856         _bind_stmt_field_data_int(hStmt, i++, time(NULL));
16857
16858         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
16859
16860         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {err = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
16861                         ("sqlite3_step fail:%d", rc));
16862         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16863                         ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
16864
16865         ret = (err == EMAIL_ERROR_NONE);
16866
16867 FINISH_OFF:
16868         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, input_transaction, ret, err);
16869
16870         if (hStmt != NULL) {
16871                 rc = sqlite3_finalize(hStmt);
16872                 if (rc != SQLITE_OK) {
16873                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
16874                         err = EMAIL_ERROR_DB_FAILURE;
16875                 }
16876         }
16877
16878         EM_DEBUG_FUNC_END("err [%d]", err);
16879         return err;
16880 }
16881
16882 INTERNAL_FUNC int emstorage_delete_task(char *multi_user_name, int task_id, int transaction)
16883 {
16884         EM_DEBUG_FUNC_BEGIN("task_id[%d], transaction[%d]", task_id, transaction);
16885         int ret = false;
16886         int err = EMAIL_ERROR_NONE;
16887         char sql_query_string[QUERY_SIZE] = {0, };
16888
16889         sqlite3 *local_db_handle = NULL;
16890
16891         if (task_id < 0) {
16892                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
16893                 return EMAIL_ERROR_INVALID_PARAM;
16894         }
16895
16896         local_db_handle = emstorage_get_db_connection(multi_user_name);
16897
16898         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
16899
16900         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_task_tbl WHERE task_id = %d", task_id);
16901         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16902         if (err != EMAIL_ERROR_NONE) {
16903                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16904                 goto FINISH_OFF;
16905         }
16906
16907         ret = true;
16908
16909 FINISH_OFF:
16910         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
16911
16912         EM_DEBUG_FUNC_END("err [%d]", err);
16913         return err;
16914 }
16915
16916 INTERNAL_FUNC int emstorage_update_task_status(char *multi_user_name, int task_id, email_task_status_type_t task_status, int transaction)
16917 {
16918         EM_DEBUG_FUNC_BEGIN("task_id[%d] task_status[%d] transaction[%d]", task_id, task_status, transaction);
16919         int ret = false;
16920         int err = EMAIL_ERROR_NONE;
16921         char sql_query_string[QUERY_SIZE] = {0, };
16922
16923         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
16924         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, err);
16925
16926         SNPRINTF(sql_query_string, sizeof(sql_query_string),
16927                         "UPDATE mail_task_tbl SET"
16928                         " task_status = %d"
16929                         " WHERE task_id = %d"
16930                         , task_status
16931                         , task_id);
16932         err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
16933         if (err != EMAIL_ERROR_NONE) {
16934                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
16935                 goto FINISH_OFF;
16936         }
16937
16938         ret = true;
16939
16940 FINISH_OFF:
16941         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, err);
16942
16943         EM_DEBUG_FUNC_END("err [%d]", err);
16944         return err;
16945 }
16946
16947 INTERNAL_FUNC int emstorage_query_task(char *multi_user_name,
16948                 const char *input_conditional_clause,
16949                 const char *input_ordering_clause,
16950                 email_task_t **output_task_list,
16951                 int *output_task_count)
16952 {
16953         EM_DEBUG_FUNC_BEGIN("input_conditional_clause[%p], input_ordering_clause [%p], "
16954                         "output_task_list[%p], output_task_count[%d]",
16955                         input_conditional_clause, input_ordering_clause, output_task_list, output_task_count);
16956         int i = 0, count = 0, rc = -1;
16957         int cur_query = 0;
16958         int field_index = 0;
16959         int err = EMAIL_ERROR_NONE;
16960         email_task_t *task_item_from_tbl = NULL;
16961         char sql_query_string[QUERY_SIZE] = {0, };
16962         char *field_list = "task_id, task_type, task_status, task_priority, task_parameter_length, task_parameter ";
16963         char **result;
16964         sqlite3 *local_db_handle = NULL;
16965         DB_STMT hStmt = NULL;
16966
16967         EM_IF_NULL_RETURN_VALUE(input_conditional_clause, false);
16968         EM_IF_NULL_RETURN_VALUE(output_task_count, false);
16969
16970         local_db_handle = emstorage_get_db_connection(multi_user_name);
16971
16972         SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
16973                         "SELECT COUNT(*) FROM mail_task_tbl %s", input_conditional_clause);
16974         EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
16975
16976         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
16977         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
16978                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
16979
16980         count = atoi(result[1]);
16981         sqlite3_free_table(result);
16982
16983         EM_DEBUG_LOG("count = %d", rc);
16984
16985         if (count == 0) {
16986                 EM_DEBUG_EXCEPTION("no task found...");
16987                 err = EMAIL_ERROR_TASK_NOT_FOUND;
16988                 goto FINISH_OFF;
16989         }
16990
16991         SNPRINTF_OFFSET(sql_query_string, cur_query, QUERY_SIZE,
16992                         "SELECT %s FROM mail_task_tbl %s %s", field_list, input_conditional_clause, input_ordering_clause);
16993         EM_DEBUG_LOG_SEC("emstorage_query_mail_list : query[%s].", sql_query_string);
16994
16995         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
16996
16997         EM_DEBUG_LOG("After sqlite3_prepare_v2 hStmt = %p", hStmt);
16998         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
16999                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17000
17001         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17002         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17003                         ("sqlite3_step fail:%d", rc));
17004
17005         if (rc == SQLITE_DONE) {
17006                 EM_DEBUG_EXCEPTION("no task found...");
17007                 err = EMAIL_ERROR_TASK_NOT_FOUND;
17008                 count = 0;
17009                 goto FINISH_OFF;
17010         }
17011
17012         if (!(task_item_from_tbl = (email_task_t*)em_malloc(sizeof(email_task_t) * count))) {
17013                 EM_DEBUG_EXCEPTION("malloc for mail_list_item_from_tbl failed...");
17014                 err = EMAIL_ERROR_OUT_OF_MEMORY;
17015                 goto FINISH_OFF;
17016         }
17017
17018         for (i = 0; i < count; i++) {
17019                 /*  get recordset */
17020                 field_index = 0;
17021
17022                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_id), field_index++);
17023                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_type), field_index++);
17024                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_status), field_index++);
17025                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_priority), field_index++);
17026                 _get_stmt_field_data_int(hStmt, (int*)&(task_item_from_tbl[i].task_parameter_length), field_index++);
17027                 _get_stmt_field_data_blob(hStmt, (void**)&(task_item_from_tbl[i].task_parameter), field_index++);
17028
17029                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17030                 EM_DEBUG_LOG("after sqlite3_step(), i = %d, rc = %d.", i,  rc);
17031                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {err = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17032                                 ("sqlite3_step fail:%d", rc));
17033         }
17034
17035 FINISH_OFF:
17036
17037         if (err == EMAIL_ERROR_NONE && output_task_list) {
17038                 *output_task_list = task_item_from_tbl;
17039                 *output_task_count = count;
17040         } else {
17041                 if (task_item_from_tbl) {
17042                         for (i = 0; i < count; i++)
17043                                 EM_SAFE_FREE(task_item_from_tbl[i].task_parameter);
17044
17045                         free(task_item_from_tbl);
17046                 }
17047         }
17048
17049         if (hStmt != NULL) {
17050                 rc = sqlite3_finalize(hStmt);
17051                 hStmt = NULL;
17052                 if (rc != SQLITE_OK) {
17053                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17054                         err = EMAIL_ERROR_DB_FAILURE;
17055                 }
17056         }
17057
17058         EM_DEBUG_FUNC_END("err [%d]", err);
17059         return err;
17060 }
17061
17062 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)
17063 {
17064         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);
17065         int err = EMAIL_ERROR_NONE;
17066
17067         if (message_id == NULL) {
17068                 EM_DEBUG_EXCEPTION("Invalid parameter");
17069                 err = EMAIL_ERROR_INVALID_PARAM;
17070                 return err;
17071         }
17072
17073         int rc = -1;
17074         int count = 0;
17075         int temp_mail_id = 0;
17076         int where_pararaph_length = 0;
17077         char *where_pararaph = NULL;
17078         char sql_query_string[QUERY_SIZE] = {0, };
17079         char **result = NULL;
17080         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17081
17082         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT mail_id from mail_tbl ");
17083
17084         where_pararaph_length = EM_SAFE_STRLEN(message_id) + 100;
17085         where_pararaph = em_malloc(sizeof(char) * where_pararaph_length);
17086         if (where_pararaph == NULL) {
17087                 EM_DEBUG_EXCEPTION("em_mallocfailed");
17088                 err = EMAIL_ERROR_OUT_OF_MEMORY;
17089                 goto FINISH_OFF;
17090         }
17091
17092         if (account_id != ALL_ACCOUNT)
17093                 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);
17094         else
17095                 sqlite3_snprintf(where_pararaph_length, where_pararaph, "WHERE mailbox_type = %d AND message_id like '%q'", input_mailbox_type, message_id);
17096
17097         if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE)
17098                 EM_SAFE_STRNCAT(sql_query_string, where_pararaph , QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
17099
17100         EM_DEBUG_LOG_SEC("query[%s]", sql_query_string);
17101
17102         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, &count, 0, NULL), rc);
17103         EM_DEBUG_DB_EXEC((SQLITE_OK != rc && -1 != rc), {err = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17104                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17105
17106
17107         EM_DEBUG_LOG("Count of mails [%d]", count);
17108
17109         if (count) {
17110                 _get_table_field_data_int(result, &temp_mail_id, 1);
17111                 EM_DEBUG_LOG("Searched mail_id [%d]", temp_mail_id);
17112
17113                 memset(sql_query_string, 0x00, QUERY_SIZE);
17114                 sqlite3_snprintf(sizeof(sql_query_string), sql_query_string, "UPDATE mail_tbl set server_mail_id = '%q'", server_uid);
17115
17116                 if (strlen(sql_query_string) + strlen(where_pararaph) < QUERY_SIZE) {
17117                         EM_SAFE_STRNCAT(sql_query_string, where_pararaph,
17118                                         QUERY_SIZE - EM_SAFE_STRLEN(sql_query_string) - 1);
17119                 }
17120
17121                 err = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17122                 if (err != EMAIL_ERROR_NONE) {
17123                         EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", err);
17124                         goto FINISH_OFF;
17125                 }
17126
17127         } else {
17128                 err = EMAIL_ERROR_MAIL_NOT_FOUND;
17129         }
17130
17131 FINISH_OFF:
17132
17133         sqlite3_free_table(result);
17134         result = NULL;
17135
17136
17137         EM_SAFE_FREE(where_pararaph);
17138
17139         if (mail_id != NULL)
17140                 *mail_id = temp_mail_id;
17141
17142         EM_DEBUG_FUNC_END("err : [%d]", err);
17143         return err;
17144 }
17145 /* Tasks --------------------------------------------------------------------------*/
17146
17147 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
17148 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)
17149 {
17150         EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], transaction[%d], err_code[%p]", local_activity, activity_id, transaction, err_code);
17151
17152         if (!local_activity || !activity_id) {
17153                 EM_DEBUG_EXCEPTION("local_activity[%p], activity_id[%p]", local_activity, activity_id);
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         int i = 0;
17163
17164         char sql_query_string[QUERY_SIZE] = {0, };
17165         DB_STMT hStmt = NULL;
17166
17167         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17168         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17169
17170         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17171         SNPRINTF(sql_query_string, sizeof(sql_query_string),
17172                         "INSERT INTO mail_auto_download_activity_tbl VALUES "
17173                         "("
17174                         "? "  /* Activity ID */
17175                         ",?"  /* Status */
17176                         ",?"  /* Account ID */
17177                         ",?"  /* Local Mail ID */
17178                         ",?"  /* Server mail ID */
17179                         ",?"  /* Mailbox ID*/
17180                         ",?"  /* Multi USER NAME */
17181                         ") ");
17182
17183         char *sql = "SELECT max(rowid) FROM mail_auto_download_activity_tbl;";
17184         char **result = NULL;
17185
17186
17187         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
17188
17189         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17190                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
17191
17192         if (NULL == result[1]) rc = 1;
17193         else rc = atoi(result[1])+1;
17194         sqlite3_free_table(result);
17195         result = NULL;
17196
17197         *activity_id = local_activity->activity_id = rc;
17198
17199         EM_DEBUG_LOG_SEC(">>>>> ACTIVITY ID [%d], MAIL ID [%d], SERVER MAIL ID [%lu]",
17200                         local_activity->activity_id, local_activity->mail_id, local_activity->server_mail_id);
17201
17202         if (local_activity->mailbox_id)
17203                 EM_DEBUG_LOG(" MAILBOX ID [%d]", local_activity->mailbox_id);
17204
17205
17206         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17207         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17208                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17209
17210
17211         _bind_stmt_field_data_int(hStmt, i++, local_activity->activity_id);
17212         _bind_stmt_field_data_int(hStmt, i++, local_activity->status);
17213         _bind_stmt_field_data_int(hStmt, i++, local_activity->account_id);
17214         _bind_stmt_field_data_int(hStmt, i++, local_activity->mail_id);
17215         _bind_stmt_field_data_int(hStmt, i++, local_activity->server_mail_id);
17216         _bind_stmt_field_data_int(hStmt, i++, local_activity->mailbox_id);
17217         _bind_stmt_field_data_string(hStmt, i++, (char *)local_activity->multi_user_name, 0, MAX_USER_NAME_LENGTH);
17218
17219
17220         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17221
17222         EM_DEBUG_DB_EXEC((rc == SQLITE_FULL), {error = EMAIL_ERROR_MAIL_MEMORY_FULL; goto FINISH_OFF; },
17223                         ("sqlite3_step fail:%d", rc));
17224         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17225                         ("sqlite3_step fail:%d, errmsg = %s.", rc, sqlite3_errmsg(local_db_handle)));
17226
17227         ret = true;
17228
17229 FINISH_OFF:
17230         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17231         if (hStmt != NULL) {
17232                 rc = sqlite3_finalize(hStmt);
17233                 hStmt = NULL;
17234                 if (rc != SQLITE_OK) {
17235                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17236                         error = EMAIL_ERROR_DB_FAILURE;
17237                 }
17238         }
17239
17240         if (err_code != NULL)
17241                 *err_code = error;
17242
17243         EM_DEBUG_FUNC_END("ret [%d]", ret);
17244         return ret;
17245 }
17246
17247
17248 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)
17249 {
17250         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);
17251
17252         if (account_id < FIRST_ACCOUNT_ID || activity_id < 0 || mail_id <= 0) {
17253                 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);
17254
17255                 if (err_code != NULL)
17256                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17257                 return false;
17258         }
17259
17260         int rc = -1;
17261         int ret = false;
17262         int error = EMAIL_ERROR_NONE;
17263         char sql_query_string[QUERY_SIZE] = {0, };
17264         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17265
17266         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17267         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17268
17269         if (activity_id == 0)
17270                 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);
17271         else
17272                 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);
17273
17274         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17275         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17276         if (error != EMAIL_ERROR_NONE) {
17277                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17278                 goto FINISH_OFF;
17279         }
17280
17281         /*  validate activity existence */
17282         rc = sqlite3_changes(local_db_handle);
17283         if (rc == 0) {
17284                 EM_DEBUG_EXCEPTION("No matching activity found");
17285                 error = EMAIL_ERROR_DATA_NOT_FOUND;
17286                 ret = true;
17287                 goto FINISH_OFF;
17288         }
17289
17290         ret = true;
17291
17292 FINISH_OFF:
17293         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17294
17295         if (err_code != NULL)
17296                 *err_code = error;
17297
17298         EM_DEBUG_FUNC_END("ret [%d]", ret);
17299         return ret;
17300 }
17301
17302 INTERNAL_FUNC int emstorage_delete_all_auto_download_activity(char *multi_user_name, int account_id, int transaction, int *err_code)
17303 {
17304         EM_DEBUG_FUNC_BEGIN("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17305
17306         if (account_id < FIRST_ACCOUNT_ID) {
17307                 EM_DEBUG_EXCEPTION("account_id[%d], transaction[%d], err_code[%p]", account_id, transaction, err_code);
17308                 if (err_code != NULL)
17309                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17310                 return false;
17311         }
17312
17313         int rc = -1;
17314         int ret = false;
17315         int error = EMAIL_ERROR_NONE;
17316         char sql_query_string[QUERY_SIZE] = {0, };
17317
17318         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17319
17320         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17321         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17322         SNPRINTF(sql_query_string, sizeof(sql_query_string), "DELETE FROM mail_auto_download_activity_tbl WHERE account_id = %d", account_id);
17323
17324         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17325         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17326         if (error != EMAIL_ERROR_NONE) {
17327                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17328                 goto FINISH_OFF;
17329         }
17330
17331         rc = sqlite3_changes(local_db_handle);
17332         if (rc == 0) {
17333                 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17334                 error = EMAIL_ERROR_DATA_NOT_FOUND;
17335                 ret = true;
17336                 goto FINISH_OFF;
17337         }
17338
17339         ret = true;
17340
17341 FINISH_OFF:
17342         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17343
17344         if (err_code != NULL)
17345                 *err_code = error;
17346
17347         EM_DEBUG_FUNC_END("ret [%d]", ret);
17348         return ret;
17349 }
17350
17351
17352 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)
17353 {
17354         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17355
17356         if (account_id < FIRST_ACCOUNT_ID || mailbox_id < 0) {
17357                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_id[%d], transaction[%d], err_code[%p]", account_id, mailbox_id, transaction, err_code);
17358                 if (err_code != NULL)
17359                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17360                 return false;
17361         }
17362
17363         int rc = -1;
17364         int ret = false;
17365         int error = EMAIL_ERROR_NONE;
17366         char sql_query_string[QUERY_SIZE] = {0, };
17367
17368         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17369
17370         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17371         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17372         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);
17373
17374         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17375         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17376         if (error != EMAIL_ERROR_NONE) {
17377                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17378                 goto FINISH_OFF;
17379         }
17380
17381         rc = sqlite3_changes(local_db_handle);
17382         if (rc == 0) {
17383                 EM_DEBUG_EXCEPTION("No matching activities found in mail_auto_download_activity_tbl");
17384                 error = EMAIL_ERROR_DATA_NOT_FOUND;
17385                 ret = true;
17386                 goto FINISH_OFF;
17387         }
17388
17389         ret = true;
17390
17391 FINISH_OFF:
17392         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17393
17394         if (err_code != NULL)
17395                 *err_code = error;
17396
17397         EM_DEBUG_FUNC_END("ret [%d]", ret);
17398         return ret;
17399 }
17400
17401
17402 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)
17403 {
17404         EM_DEBUG_FUNC_BEGIN("account_id[%d], event_start[%p], err_code[%p]", account_id, event_start, err_code);
17405
17406         if (account_id < FIRST_ACCOUNT_ID || !event_start || input_mailbox_id <= 0 || !count) {
17407                 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);
17408
17409                 if (err_code != NULL)
17410                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17411                 return false;
17412         }
17413
17414         int rc = -1;
17415         int ret = false;
17416         char **result;
17417         int error = EMAIL_ERROR_NONE;
17418         int i = 0;
17419         DB_STMT hStmt = NULL;
17420         email_event_auto_download *event_list = NULL;
17421         char sql_query_string[QUERY_SIZE] = {0, };
17422
17423         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17424
17425         EMSTORAGE_START_READ_TRANSACTION(transaction);
17426
17427         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17428         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);
17429
17430
17431         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17432         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17433                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17434
17435         *count = atoi(result[1]);
17436         sqlite3_free_table(result);
17437
17438         EM_DEBUG_LOG_SEC("Query = [%s]", sql_query_string);
17439
17440         if (!*count) {
17441                 EM_DEBUG_LOG("No matched activity found in mail_auto_download_activity_tbl");
17442                 error = EMAIL_ERROR_MAIL_NOT_FOUND;
17443                 ret = true;
17444                 goto FINISH_OFF;
17445         }
17446         EM_DEBUG_LOG("Activity Count = %d", *count);
17447
17448         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17449         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);
17450
17451         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17452
17453
17454         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17455
17456         EM_DEBUG_LOG(" Bbefore sqlite3_prepare hStmt = %p", hStmt);
17457         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17458                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17459
17460
17461         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17462         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17463                         ("sqlite3_step fail:%d", rc));
17464
17465         if (!(event_list = (email_event_auto_download *)em_malloc(sizeof(email_event_auto_download)*(*count)))) {
17466                 EM_DEBUG_EXCEPTION("Malloc failed");
17467
17468                 error = EMAIL_ERROR_OUT_OF_MEMORY;
17469                 goto FINISH_OFF;
17470         }
17471
17472         for (i = 0; i < (*count); i++) {
17473                 _get_stmt_field_data_int(hStmt, &(event_list[i].activity_id), ACTIVITY_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17474                 _get_stmt_field_data_int(hStmt, &(event_list[i].status), STATUS_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17475                 _get_stmt_field_data_int(hStmt, &(event_list[i].account_id), ACCOUNT_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17476                 _get_stmt_field_data_int(hStmt, &(event_list[i].mail_id), MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17477                 _get_stmt_field_data_int(hStmt, (int *)&(event_list[i].server_mail_id), SERVER_MAIL_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17478                 _get_stmt_field_data_int(hStmt, &(event_list[i].mailbox_id), MAILBOX_ID_IDX_MAIL_AUTO_DOWNLOAD_ACTIVITY_TBL);
17479
17480                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17481                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17482                                 ("sqlite3_step fail:%d", rc));
17483         }
17484
17485         ret = true;
17486
17487 FINISH_OFF:
17488
17489         if (true == ret)
17490                 *event_start = event_list;
17491         else {
17492                 EM_SAFE_FREE(event_list);
17493                 *event_start = NULL;
17494                 *count = 0;
17495         }
17496
17497         if (hStmt != NULL) {
17498                 rc = sqlite3_finalize(hStmt);
17499                 hStmt = NULL;
17500                 if (rc != SQLITE_OK) {
17501                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17502                         error = EMAIL_ERROR_DB_FAILURE;
17503                 }
17504         }
17505
17506         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17507         if (err_code != NULL)
17508                 *err_code = error;
17509
17510         EM_DEBUG_FUNC_END("ret [%d]", ret);
17511         return ret;
17512 }
17513
17514
17515 INTERNAL_FUNC int emstorage_get_auto_download_activity_count(char *multi_user_name, int *activity_count, int transaction, int *err_code)
17516 {
17517         EM_DEBUG_FUNC_BEGIN("activity_count[%p], err_code[%p]", activity_count, err_code);
17518
17519         if (!activity_count || !err_code) {
17520                 EM_DEBUG_EXCEPTION("activity_count[%p], err_code[%p]", activity_count, err_code);
17521                 if (err_code != NULL)
17522                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17523                 return false;
17524         }
17525
17526         int rc = -1;
17527         int ret = false;
17528         int error = EMAIL_ERROR_NONE;
17529         DB_STMT hStmt = NULL;
17530         char sql_query_string[QUERY_SIZE] = {0, };
17531
17532         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17533
17534         EMSTORAGE_START_READ_TRANSACTION(transaction);
17535         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17536
17537         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT count(*) FROM mail_auto_download_activity_tbl;");
17538
17539         EM_DEBUG_LOG_DEV(" Query [%s]", sql_query_string);
17540
17541
17542         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17543         EM_DEBUG_LOG_DEV("before sqlite3_prepare hStmt = %p", hStmt);
17544         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17545                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17546
17547
17548         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17549         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17550                         ("sqlite3_step fail:%d", rc));
17551
17552         _get_stmt_field_data_int(hStmt, activity_count, 0);
17553
17554         EM_DEBUG_LOG("counts of activities in activity table [%d]", *activity_count);
17555
17556         ret = true;
17557
17558 FINISH_OFF:
17559
17560         if (hStmt != NULL) {
17561                 rc = sqlite3_finalize(hStmt);
17562                 hStmt = NULL;
17563                 if (rc != SQLITE_OK) {
17564                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17565                         error = EMAIL_ERROR_DB_FAILURE;
17566                 }
17567         }
17568
17569         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17570         if (err_code != NULL)
17571                 *err_code = error;
17572
17573         EM_DEBUG_FUNC_END("ret [%d]", ret);
17574         return ret;
17575 }
17576
17577
17578 INTERNAL_FUNC int emstorage_get_auto_download_account_list(char *multi_user_name, int **account_list, int *count, int transaction, int *err_code)
17579 {
17580         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p] err_code[%p]", account_list, count, err_code);
17581
17582         if (!account_list || !count) {
17583                 EM_DEBUG_EXCEPTION("account_list[%p], count[%p]", account_list, count);
17584                 if (err_code != NULL)
17585                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17586                 return false;
17587         }
17588
17589         int ret = false;
17590         int error = EMAIL_ERROR_NONE;
17591         char *sql = "SELECT count(distinct account_id) FROM mail_auto_download_activity_tbl";
17592         char **result;
17593         int i = 0, rc = -1;
17594         int *result_account_list = NULL;
17595         DB_STMT hStmt = NULL;
17596         char sql_query_string[QUERY_SIZE] = {0, };
17597         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17598
17599         EMSTORAGE_START_READ_TRANSACTION(transaction);
17600
17601         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql, &result, NULL, NULL, NULL), rc);
17602         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17603                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql, rc, sqlite3_errmsg(local_db_handle)));
17604
17605         *count = atoi(result[1]);
17606         sqlite3_free_table(result);
17607
17608         if (!*count) {
17609                 EM_DEBUG_EXCEPTION("no account found...");
17610                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
17611                 ret = true;
17612                 goto FINISH_OFF;
17613         }
17614
17615         EM_DEBUG_LOG("Account count [%d]", *count);
17616
17617         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17618
17619         SNPRINTF(sql_query_string, sizeof(sql_query_string), "SELECT distinct account_id FROM mail_auto_download_activity_tbl");
17620
17621         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17622
17623
17624         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17625
17626         EM_DEBUG_LOG("Before sqlite3_prepare hStmt = %p", hStmt);
17627         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17628                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17629
17630
17631         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17632         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17633                         ("sqlite3_step fail:%d", rc));
17634
17635         if (NULL == (result_account_list = (int *)em_malloc(sizeof(int)*(*count)))) {
17636                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
17637                 error = EMAIL_ERROR_OUT_OF_MEMORY;
17638                 goto FINISH_OFF;
17639         }
17640
17641         for (i = 0; i < (*count); i++) {
17642                 _get_stmt_field_data_int(hStmt, result_account_list + i, 0);
17643
17644                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17645                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17646                                 ("sqlite3_step fail:%d", rc));
17647                 EM_DEBUG_LOG("account id -> %d", result_account_list[i]);
17648         }
17649
17650         ret = true;
17651
17652 FINISH_OFF:
17653
17654         if (ret == true)
17655                 *account_list = result_account_list;
17656         else
17657                 EM_SAFE_FREE(result_account_list);
17658
17659         if (hStmt != NULL) {
17660                 rc = sqlite3_finalize(hStmt);
17661                 hStmt = NULL;
17662                 if (rc != SQLITE_OK) {
17663                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17664                         error = EMAIL_ERROR_DB_FAILURE;
17665                 }
17666         }
17667
17668         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17669         if (err_code != NULL)
17670                 *err_code = error;
17671         EM_DEBUG_FUNC_END("ret [%d]", ret);
17672         return ret;
17673 }
17674
17675
17676 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)
17677 {
17678         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], count[%p] err_code[%p]", account_id, mailbox_list, count, err_code);
17679
17680         if (account_id < FIRST_ACCOUNT_ID || !mailbox_list || !count) {
17681                 EM_DEBUG_EXCEPTION("account_id[%d], mailbox_list[%p], count[%p]", account_id, mailbox_list, count);
17682                 if (err_code != NULL)
17683                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17684                 return false;
17685         }
17686
17687         int ret = false;
17688         int error = EMAIL_ERROR_NONE;
17689         char **result;
17690         int i = 0, rc = -1;
17691         int *mbox_list = NULL;
17692         DB_STMT hStmt = NULL;
17693         char sql_query_string[QUERY_SIZE] = {0, };
17694
17695         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17696
17697         EMSTORAGE_START_READ_TRANSACTION(transaction);
17698
17699         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17700         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);
17701
17702
17703         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_get_table(local_db_handle, sql_query_string, &result, NULL, NULL, NULL), rc);
17704         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {error = EMAIL_ERROR_DB_FAILURE; sqlite3_free_table(result); goto FINISH_OFF; },
17705                         ("SQL(%s) sqlite3_get_table fail:%d -%s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17706
17707         *count = atoi(result[1]);
17708         sqlite3_free_table(result);
17709
17710         if (!*count) {
17711                 EM_DEBUG_EXCEPTION(" no mailbox_name found...");
17712                 error = EMAIL_ERROR_MAILBOX_NOT_FOUND;
17713                 ret = true;
17714                 goto FINISH_OFF;
17715         }
17716         EM_DEBUG_LOG("Mailbox count = %d", *count);
17717
17718         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17719
17720         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);
17721
17722         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
17723
17724
17725         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17726
17727
17728         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17729                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17730
17731
17732         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17733         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17734                         ("sqlite3_step fail:%d", rc));
17735
17736         mbox_list = (int *)em_malloc(sizeof(int)*(*count)); /* prevent */
17737         if (mbox_list == NULL) {
17738                 EM_DEBUG_EXCEPTION(" em_mallocfailed...");
17739                 error = EMAIL_ERROR_OUT_OF_MEMORY;
17740                 goto FINISH_OFF;
17741         }
17742
17743         for (i = 0; i < (*count); i++) {
17744                 _get_stmt_field_data_int(hStmt, mbox_list + i, 0);
17745                 EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17746
17747                 EM_DEBUG_DB_EXEC((rc != SQLITE_ROW && rc != SQLITE_DONE), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17748                                 ("sqlite3_step fail:%d", rc));
17749                 EM_DEBUG_LOG("mbox_list %d", mbox_list[i]);
17750         }
17751
17752         ret = true;
17753
17754 FINISH_OFF:
17755
17756         if (ret == true)
17757                 *mailbox_list = mbox_list;
17758         else
17759                 EM_SAFE_FREE(mbox_list);
17760
17761         if (hStmt != NULL) {
17762                 rc = sqlite3_finalize(hStmt);
17763                 hStmt = NULL;
17764                 if (rc != SQLITE_OK) {
17765                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17766                         error = EMAIL_ERROR_DB_FAILURE;
17767                 }
17768         }
17769
17770         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17771         if (err_code != NULL)
17772                 *err_code = error;
17773         EM_DEBUG_FUNC_END("ret [%d]", ret);
17774         return ret;
17775 }
17776
17777
17778 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)
17779 {
17780         EM_DEBUG_FUNC_BEGIN("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
17781
17782         if (account_id < FIRST_ACCOUNT_ID || !activity_count || !err_code) {
17783                 EM_DEBUG_EXCEPTION("account_id[%d], activity_count[%p], err_code[%p]", account_id, activity_count, err_code);
17784                 if (err_code != NULL)
17785                         *err_code = EMAIL_ERROR_INVALID_PARAM;
17786                 return false;
17787         }
17788
17789         int rc = -1;
17790         int ret = false;
17791         int error = EMAIL_ERROR_NONE;
17792         char sql_query_string[QUERY_SIZE] = {0, };
17793         DB_STMT hStmt = NULL;
17794
17795         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17796
17797         EMSTORAGE_START_READ_TRANSACTION(transaction);
17798         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17799         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);
17800
17801         EM_DEBUG_LOG_SEC(" Query [%s]", sql_query_string);
17802
17803         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_prepare_v2(local_db_handle, sql_query_string, EM_SAFE_STRLEN(sql_query_string), &hStmt, NULL), rc);
17804         EM_DEBUG_LOG("before sqlite3_prepare hStmt = %p", hStmt);
17805         EM_DEBUG_DB_EXEC((SQLITE_OK != rc), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17806                         ("SQL(%s) sqlite3_prepare fail:(%d) %s", sql_query_string, rc, sqlite3_errmsg(local_db_handle)));
17807
17808         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_step(hStmt), rc);
17809         EM_DEBUG_DB_EXEC((rc != SQLITE_ROW), {error = EMAIL_ERROR_DB_FAILURE; goto FINISH_OFF; },
17810                         ("sqlite3_step fail:%d", rc));
17811
17812         _get_stmt_field_data_int(hStmt, activity_count, 0);
17813
17814         EM_DEBUG_LOG("count of activities in activity table [%d]", *activity_count);
17815
17816         ret = true;
17817
17818 FINISH_OFF:
17819
17820         if (hStmt != NULL) {
17821                 rc = sqlite3_finalize(hStmt);
17822                 hStmt = NULL;
17823                 if (rc != SQLITE_OK) {
17824                         EM_DEBUG_EXCEPTION("sqlite3_finalize error [%d]", rc);
17825                         error = EMAIL_ERROR_DB_FAILURE;
17826                 }
17827         }
17828
17829         EMSTORAGE_FINISH_READ_TRANSACTION(transaction);
17830
17831         if (err_code != NULL)
17832                 *err_code = error;
17833
17834         EM_DEBUG_FUNC_END("ret [%d]", ret);
17835         return ret;
17836 }
17837
17838
17839 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)
17840 {
17841         EM_DEBUG_FUNC_BEGIN_SEC("old_server_uid[%s], new_server_uid[%s], mailbox_id[%d]", old_server_uid, new_server_uid, mailbox_id);
17842
17843         int rc = -1, ret = false;
17844         int error = EMAIL_ERROR_NONE;
17845         char sql_query_string[QUERY_SIZE] = {0, };
17846         int transaction = true;
17847
17848         if (!old_server_uid || !new_server_uid || (!mailbox_name && mailbox_id < 0)) {
17849                 EM_DEBUG_EXCEPTION("Invalid parameters");
17850                 error = EMAIL_ERROR_INVALID_PARAM;
17851                 return false;
17852         }
17853
17854         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17855
17856         EMSTORAGE_START_WRITE_TRANSACTION(multi_user_name, transaction, error);
17857         memset(sql_query_string, 0x00, sizeof(sql_query_string));
17858
17859         if (mailbox_id > 0)
17860                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17861                                 "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);
17862         else if (mailbox_name)
17863                 SNPRINTF(sql_query_string, sizeof(sql_query_string),
17864                                 "UPDATE mail_auto_download_activity_tbl SET server_mail_id = %s WHERE server_mail_id = %s ", new_server_uid, old_server_uid);
17865
17866         EM_DEBUG_LOG_SEC("Query [%s]", sql_query_string);
17867         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
17868         if (error != EMAIL_ERROR_NONE) {
17869                 EM_DEBUG_EXCEPTION("emstorage_exec_query_by_prepare_v2 failed:[%d]", error);
17870                 goto FINISH_OFF;
17871         }
17872
17873         rc = sqlite3_changes(local_db_handle);
17874         if (rc == 0)
17875                 EM_DEBUG_LOG("No matching found in mail_auto_download_activity_tbl");
17876
17877         ret = true;
17878
17879 FINISH_OFF:
17880
17881         EMSTORAGE_FINISH_WRITE_TRANSACTION(multi_user_name, transaction, ret, error);
17882
17883         if (err_code != NULL)
17884                 *err_code = error;
17885
17886         EM_DEBUG_FUNC_END("ret [%d]", ret);
17887         return ret;
17888 }
17889
17890 #endif
17891
17892 #ifdef __FEATURE_UPDATE_DB_TABLE_SCHEMA__
17893
17894 typedef struct {
17895         char *column_name;
17896         char *column_type;
17897 } email_column_info_t;
17898
17899 static int get_column_information_from_table_callback(void *arg1, int argc, char **argv, char **input_column_name)
17900 {
17901         EM_DEBUG_FUNC_BEGIN("arg1[%p] argc[%d] argv[%p] column_name[%p]", arg1, argc, argv, input_column_name);
17902
17903         int i = 0;
17904         int validated = 0;
17905         char *column_name = NULL;
17906         char *column_type = NULL;
17907         GList *new_list = *((GList**)arg1);
17908         email_column_info_t *column_info_item = NULL;
17909
17910         for (i = 0; i < argc; ++i) {
17911                 /* EM_DEBUG_LOG("%s = %s", input_column_name[i], argv[i]); */
17912                 if (EM_SAFE_STRCMP(input_column_name[i], "name") == 0) {
17913                         if (column_name)
17914                                 EM_SAFE_FREE(column_name);
17915
17916                         column_name = EM_SAFE_STRDUP(argv[i]);
17917                         validated = 1;
17918                 } else if (EM_SAFE_STRCMP(input_column_name[i], "type") == 0) {
17919                         if (column_type)
17920                                 EM_SAFE_FREE(column_type);
17921
17922                         column_type = EM_SAFE_STRDUP(argv[i]);
17923                 }
17924         }
17925
17926         if (validated) {
17927                 EM_DEBUG_LOG("column_name[%s] column_type[%s]", column_name, column_type);
17928                 column_info_item = em_malloc(sizeof(email_column_info_t));
17929                 if (column_info_item == NULL) {
17930                         EM_DEBUG_EXCEPTION("em_mallocfailed");
17931                         goto FINISH_OFF;
17932                 }
17933
17934                 column_info_item->column_name = EM_SAFE_STRDUP(column_name);
17935                 column_info_item->column_type = EM_SAFE_STRDUP(column_type);
17936                 new_list = g_list_append(new_list, (gpointer)column_info_item);
17937                 *((GList**)arg1) = new_list;
17938         }
17939
17940 FINISH_OFF:
17941
17942         EM_SAFE_FREE(column_name);
17943         EM_SAFE_FREE(column_type);
17944
17945         EM_DEBUG_FUNC_END();
17946         return 0;
17947 }
17948
17949 static int emstorage_get_column_information_from_table(char *multi_user_name, const char *input_table_name, GList **output_column_info)
17950 {
17951         EM_DEBUG_FUNC_BEGIN("input_table_name[%p] output_column_info[%p]", input_table_name, output_column_info);
17952         int err = EMAIL_ERROR_NONE;
17953         int result_from_sqlite = 0;
17954         char *error_message_from_sqlite = NULL;
17955         char sql_query_string[QUERY_SIZE] = {0, };
17956         GList *new_list = NULL;
17957         sqlite3 *local_db_handle = emstorage_get_db_connection(multi_user_name);
17958
17959         SNPRINTF(sql_query_string, QUERY_SIZE, "pragma table_info(%s);", input_table_name);
17960
17961         result_from_sqlite = sqlite3_exec(local_db_handle, sql_query_string, get_column_information_from_table_callback, &new_list, &error_message_from_sqlite);
17962
17963         if (result_from_sqlite != SQLITE_OK)
17964                 EM_DEBUG_EXCEPTION("sqlite3_exec returns [%d]", result_from_sqlite);
17965
17966         EM_DEBUG_LOG("new_list[%p] output_column_info[%p]", new_list, output_column_info);
17967
17968         if (new_list && output_column_info) {
17969                 EM_DEBUG_LOG("g_list_length[%d]", g_list_length(new_list));
17970                 *output_column_info = new_list;
17971         }
17972
17973         EM_DEBUG_FUNC_END("err [%d]", err);
17974         return err;
17975 }
17976
17977 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)
17978 {
17979         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);
17980         int error = EMAIL_ERROR_NONE;
17981         int rc = -1;
17982         sqlite3 *local_db_handle = NULL;
17983         char sql_query_string[QUERY_SIZE] = {0, };
17984
17985         local_db_handle = emstorage_get_db_connection(multi_user_name);
17986
17987         if (input_full_query == NULL || input_source_table_name == NULL || input_new_table_name == NULL) {
17988                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
17989                 error = EMAIL_ERROR_INVALID_PARAM;
17990                 goto FINISH_OFF;
17991         }
17992
17993         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
17994         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
17995
17996         EM_DEBUG_LOG("[%s] will be replaced by [%s]", input_source_table_name, input_new_table_name);
17997
17998         EM_SAFE_STRNCPY(sql_query_string, input_full_query[input_query_index], sizeof(sql_query_string)-1); /*prevent 21984*/
17999         reg_replace(sql_query_string, input_source_table_name, input_new_table_name);
18000
18001         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
18002 FINISH_OFF:
18003
18004         if (error == EMAIL_ERROR_NONE) {
18005                 EMSTORAGE_PROTECTED_FUNC_CALL(
18006                         sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
18007         } else {
18008                 EMSTORAGE_PROTECTED_FUNC_CALL(
18009                         sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
18010         }
18011
18012         EM_DEBUG_FUNC_END("error [%d]", error);
18013         return error;
18014 }
18015
18016 static int emstorage_add_column(char *multi_user_name, char *input_table_name, email_column_info_t *input_new_column)
18017 {
18018         EM_DEBUG_FUNC_BEGIN("input_table_name[%p] input_new_column[%p]", input_table_name, input_new_column);
18019         int error = EMAIL_ERROR_NONE;
18020         int rc = -1;
18021         sqlite3 *local_db_handle = NULL;
18022         char sql_query_string[QUERY_SIZE] = {0, };
18023
18024         local_db_handle = emstorage_get_db_connection(multi_user_name);
18025
18026         if (input_table_name == NULL || input_new_column == NULL) {
18027                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
18028                 error = EMAIL_ERROR_INVALID_PARAM;
18029                 goto FINISH_OFF;
18030         }
18031
18032         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
18033         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
18034         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);
18035         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
18036 FINISH_OFF:
18037
18038         if (error == EMAIL_ERROR_NONE) {
18039                 EMSTORAGE_PROTECTED_FUNC_CALL(
18040                         sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
18041         } else {
18042                 EMSTORAGE_PROTECTED_FUNC_CALL(
18043                         sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
18044         }
18045
18046         EM_DEBUG_FUNC_END("error [%d]", error);
18047         return error;
18048 }
18049
18050 static int emstorage_drop_table(char *multi_user_name, char *input_table_name)
18051 {
18052         EM_DEBUG_FUNC_BEGIN("input_table_name[%p]", input_table_name);
18053         int error = EMAIL_ERROR_NONE;
18054         int rc = -1;
18055         sqlite3 *local_db_handle = NULL;
18056         char sql_query_string[QUERY_SIZE] = {0, };
18057
18058         local_db_handle = emstorage_get_db_connection(multi_user_name);
18059
18060         if (input_table_name == NULL) {
18061                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
18062                 error = EMAIL_ERROR_INVALID_PARAM;
18063                 goto FINISH_OFF;
18064         }
18065
18066         EMSTORAGE_PROTECTED_FUNC_CALL(sqlite3_exec(local_db_handle, "BEGIN;", NULL, NULL, NULL), rc);
18067         EM_DEBUG_DB_EXEC(SQLITE_OK != rc, {goto FINISH_OFF; }, ("SQL(BEGIN EXCLUSIVE) exec fail:%d -%s", rc, sqlite3_errmsg(local_db_handle)));
18068         SNPRINTF(sql_query_string, QUERY_SIZE, "DROP TABLE %s;", input_table_name);
18069         error = emstorage_exec_query_by_prepare_v2(local_db_handle, sql_query_string);
18070 FINISH_OFF:
18071
18072         if (error == EMAIL_ERROR_NONE) {
18073                 EMSTORAGE_PROTECTED_FUNC_CALL(
18074                         sqlite3_exec(local_db_handle, "END;", NULL, NULL, NULL), rc);
18075         } else {
18076                 EMSTORAGE_PROTECTED_FUNC_CALL(
18077                         sqlite3_exec(local_db_handle, "rollback", NULL, NULL, NULL), rc);
18078         }
18079
18080         EM_DEBUG_FUNC_END("error [%d]", error);
18081         return error;
18082 }
18083
18084 gint glist_compare_column_name(gconstpointer old_column_info, gconstpointer new_column_info)
18085 {
18086         EM_DEBUG_FUNC_BEGIN("old_column_info[%p] new_column_info[%p]", old_column_info, new_column_info);
18087         email_column_info_t *left_one  = (email_column_info_t*)old_column_info;
18088         email_column_info_t *right_one = (email_column_info_t*)new_column_info;
18089
18090         if (old_column_info == NULL || new_column_info == NULL)
18091                 return -1;
18092
18093         return EM_SAFE_STRCMP((char*)left_one->column_name, (char*)right_one->column_name);
18094 }
18095
18096 INTERNAL_FUNC int emstorage_update_db_table_schema(char *multi_user_name)
18097 {
18098         EM_DEBUG_FUNC_BEGIN();
18099         int i = 0;
18100         int j = 0;
18101         int error = EMAIL_ERROR_NONE;
18102         int query_len = 0;
18103         email_column_info_t *new_column_info = NULL;
18104         email_column_info_t *p_column_info = NULL;
18105         char **create_table_query = NULL;
18106         GList *found_data = NULL;
18107         GList *column_list_of_old_table = NULL;
18108         GList *column_list_of_new_table = NULL;
18109         char table_names[CREATE_TABLE_MAX][2][50] = { { "mail_account_tbl", "mail_account_tbl_new" },
18110                 { "mail_box_tbl", "mail_box_tbl_new" },
18111                 { "mail_read_mail_uid_tbl", "mail_read_mail_uid_tbl_new" },
18112                 { "mail_rule_tbl", "mail_rule_tbl_new" },
18113                 { "mail_tbl", "mail_tbl_new" },
18114                 { "mail_attachment_tbl", "mail_attachment_tbl_new" },
18115 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
18116                 { "mail_partial_body_activity_tbl", "mail_partial_body_activity_tbl_new" },
18117 #else
18118                 { "", "" },
18119 #endif
18120                 { "mail_meeting_tbl", "mail_meeting_tbl_new" },
18121 #ifdef __FEATURE_LOCAL_ACTIVITY__
18122                 { "mail_local_activity_tbl", "mail_local_activity_tbl_new" },
18123 #else
18124                 { "", "" },
18125 #endif
18126                 { "mail_certificate_tbl", "mail_certificate_tbl_new" },
18127                 { "mail_task_tbl", "mail_task_tbl_new" },
18128 #ifdef __FEATURE_BODY_SEARCH__
18129                 { "mail_text_tbl", "mail_text_tbl_new" },
18130 #else
18131                 { "", "" },
18132 #endif
18133
18134 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
18135                 { "mail_auto_download_activity_tbl", "mail_auto_download_activity_tbl_new" }
18136 #else
18137                 { "", "" }
18138 #endif
18139
18140         };
18141
18142         error = emcore_load_query_from_file((char *)EMAIL_SERVICE_CREATE_TABLE_QUERY_FILE_PATH, &create_table_query, &query_len);
18143
18144         if (error != EMAIL_ERROR_NONE) {
18145                 EM_DEBUG_EXCEPTION("emcore_load_sql_from_file failed [%d]", error);
18146                 goto FINISH_OFF;
18147         }
18148
18149         if (query_len < CREATE_TABLE_MAX) {
18150                 EM_DEBUG_EXCEPTION("SQL string array length is difference from CREATE_TABLE_MAX");
18151                 error = EMAIL_ERROR_SYSTEM_FAILURE;
18152                 goto FINISH_OFF;
18153         }
18154
18155         for (i = CREATE_TABLE_MAIL_ACCOUNT_TBL; i < CREATE_TABLE_MAX; i++) {
18156                 EM_DEBUG_LOG("table [%s] new_table [%s]", table_names[i][0], table_names[i][1]);
18157                 if (EM_SAFE_STRLEN(table_names[i][0]) && EM_SAFE_STRLEN(table_names[i][1])) {
18158                         /* Check existing of _new table */
18159                         emstorage_drop_table(multi_user_name, table_names[i][1]);
18160                         error = emstorage_create_renamed_table(multi_user_name, create_table_query, i, table_names[i][0], table_names[i][1]);
18161                         if (error != EMAIL_ERROR_NONE) {
18162                                 EM_DEBUG_EXCEPTION("emstorage_create_renamed_table failed [%d]", error);
18163                                 goto FINISH_OFF;
18164                         }
18165
18166                         emstorage_get_column_information_from_table(multi_user_name, table_names[i][0], &column_list_of_old_table);
18167                         emstorage_get_column_information_from_table(multi_user_name, table_names[i][1], &column_list_of_new_table);
18168
18169                         /* Compare fields and add new field */
18170                         for (j = 0; j < g_list_length(column_list_of_new_table); j++) {
18171                                 new_column_info = (email_column_info_t*)g_list_nth_data(column_list_of_new_table, j);
18172                                 found_data = g_list_find_custom(column_list_of_old_table, (gconstpointer)new_column_info, glist_compare_column_name);
18173                                 if (found_data == NULL) {
18174                                         /* add new field*/
18175                                         emstorage_add_column(multi_user_name, table_names[i][0], new_column_info);
18176                                 }
18177                         }
18178
18179                         emstorage_drop_table(multi_user_name, table_names[i][1]);
18180                 } else {
18181                         EM_DEBUG_LOG("Skipped");
18182                 }
18183         }
18184
18185 FINISH_OFF:
18186         if (create_table_query) {
18187                 int i = 0;
18188                 for (i = 0; i < query_len; i++) {
18189                         if (create_table_query[i])
18190                                 EM_SAFE_FREE(create_table_query[i]);
18191                 }
18192                 EM_SAFE_FREE(create_table_query);
18193         }
18194
18195         found_data = g_list_first(column_list_of_old_table);
18196         while (found_data != NULL) {
18197                 p_column_info = (email_column_info_t *)found_data->data;
18198                 EM_SAFE_FREE(p_column_info->column_name);
18199                 EM_SAFE_FREE(p_column_info->column_type);
18200                 EM_SAFE_FREE(p_column_info);
18201
18202                 found_data = g_list_next(found_data);
18203         }
18204         g_list_free(column_list_of_old_table);
18205
18206         found_data = g_list_first(column_list_of_new_table);
18207         while (found_data != NULL) {
18208                 p_column_info = (email_column_info_t *)found_data->data;
18209                 EM_SAFE_FREE(p_column_info->column_name);
18210                 EM_SAFE_FREE(p_column_info->column_type);
18211                 EM_SAFE_FREE(p_column_info);
18212
18213                 found_data = g_list_next(found_data);
18214         }
18215         g_list_free(column_list_of_new_table);
18216
18217         EM_DEBUG_FUNC_END("error [%d]", error);
18218         return error;
18219 }
18220 #endif /* __FEATURE_UPDATE_DB_TABLE_SCHEMA__ */
18221                                         /* LCOV_EXCL_STOP */
18222 /*EOF*/